Python types for polyglot objects
This PR was developed during the seminar "Softwaredesign" at the Hasso-Plattner Institute Potsdam Authors: @Olliwehr, @antonykamp Supervisors: @timfel @JensLincke
What did we change?
This PR introduces the CRUD operations for a register of Python types as equivalents for Java classes.
In the following example, we want to use instances of java.util.ArrayList in the Python code. To have a Python idiomatic interface, we register with polyglot.register_java_interop_type or @polyglot.java_interop_type, a Python type jlist equivalent to java.util.ArrayList.
import java
import polyglot
class jList(__graalpython__.ForeignType):
def append(self, element):
self.add(element)
polyglot.register_java_interop_type('java.util.ArrayList', jList)
l = java.type('java.util.ArrayList')()
assert isinstance(l, jList) # == true
We used the existing interoperability extension API as reference.
New operations
We introduced CRUD operations to insert, update, delete, and get mappings and integrated the first three operations into the polyglot module.
Register type mappings with polyglot.register_java_interop_type(javaClassNameAsString, pythonType) or @polyglot.java_interop_type(javaClassNameAsString).
Edit mappings by calling the same function and passing the flag overwrite=True. Otherwise, when registering the same java class two times, it'll throw an Error INTEROP_TYPE_ALREADY_REGISTERED.
Remove a mapping by calling remove_java_interop_type(javaClassNameAsString). If the class is not registered, it will throw an Error INTEROP_TYPE_NOT_REGISTERED.
Whenever we call getClassNode, we look for a registered type for the class. Alternatively, we return Foreign.
How did we change it?
We introduced a WeakHashMap in the Python context. In the map, the keys are string representations of the Java classes, and the values are Python types. In getClassNode, we look the Java classes up in the map. If the key is not found, we return ForeignType as the default Python type.
Additionally, we introduced:
-
register_java_interop_typefunction, -
java_interop_typedecorator and -
remove_java_interop_typefunction
to the polyglot module.
Future work
We identified two possible future work items:
- support multi-inheritance in Python types,
- support Java interfaces
Support multi-inheritance in Python types
In Python types, e.g., list, some functions are based on "base functions." The "base function" is building the base for other functions. It would be helpful to inherit from these types, implement the "base function" and get some tasks for free:
class jList(__graalpython__.ForeignType, list):
def append(self, element):
self.add(element)
Currently, that's not possible. We receive a "Layout Conflict Error" because ForeignType has some predefined attributes and functions.
TypeError: multiple bases have instance lay-out conflict
We recommend splitting ForeignType into:
-
ForeignTypewith the current behavior and -
ForeignTypeBasewithout the predefined attributes and functions.
If we'd inherit from ForeignTypeBase and list now, we probably wouldn't receive the "Layout Conflict Error" but some functions for free.
Support java interfaces
If we want the Python idiomatic interface for different list classes (ArrayList, LinkedList, etc.), we'd register (and unregister) the same Python type repeatedly.
The idea is that we only register a jlist type for the interface java.util.List, and it's applied to ArrayList, LinkedList, and... by default.
Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA). The following contributors of this PR have not signed the OCA:
- PR author: [email protected] (@antonykamp)
- [email protected] (@Olliwehr)
To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.
When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.
If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.
Thank you for signing the OCA.
@Sowasvonbot will integrate this in the next months.
Thank you for your contribution! This has been merged by @Sowasvonbot in https://github.com/oracle/graalpython/commit/6171b1fa9528559318828ec7b6b731e18581d48d and https://github.com/oracle/graalpython/commit/86885bfc1236f7ae4575f9ed43b60958cc9e9388.