Closed antonykamp closed 1 month ago
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:
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.
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 withpolyglot.register_java_interop_type
or@polyglot.java_interop_type
, a Python typejlist
equivalent tojava.util.ArrayList
.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 ErrorINTEROP_TYPE_ALREADY_REGISTERED
.Remove a mapping by calling
remove_java_interop_type(javaClassNameAsString)
. If the class is not registered, it will throw an ErrorINTEROP_TYPE_NOT_REGISTERED
.Whenever we call
getClassNode
, we look for a registered type for the class. Alternatively, we returnForeign
.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. IngetClassNode,
we look the Java classes up in the map. If the key is not found, we returnForeignType
as the default Python type.Additionally, we introduced:
register_java_interop_type
function,java_interop_type
decorator andremove_java_interop_type
functionto the polyglot module.
Future work
We identified two possible future work items:
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:Currently, that's not possible. We receive a "Layout Conflict Error" because
ForeignType
has some predefined attributes and functions.We recommend splitting
ForeignType
into:ForeignType
with the current behavior andForeignTypeBase
without the predefined attributes and functions.If we'd inherit from
ForeignTypeBase
andlist
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 interfacejava.util.List
, and it's applied to ArrayList, LinkedList, and... by default.