You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Besides being primarily recommended to use in your Java application, GraalPy can interoperate with other Graal
11
-
languages (languages implemented on
12
-
the [Truffle framework](https://www.graalvm.org/latest/graalvm-as-a-platform/language-implementation-framework/)).
13
-
This means that you can use the objects and functions provided by those other languages directly from your Python
14
-
scripts.
9
+
Besides being primarily recommended to use in your Java application, GraalPy can interoperate with other Graal languages (languages implemented on the [Truffle framework](https://www.graalvm.org/latest/graalvm-as-a-platform/language-implementation-framework/)).
10
+
This means that you can use the objects and functions provided by those other languages directly from your Python scripts.
15
11
16
12
## Interacting with Java from Python scripts
17
13
18
14
Java is the host language of the JVM and runs the GraalPy interpreter itself.
19
15
To interoperate with Java from Python scripts, use the `java` module:
20
-
21
16
```python
22
17
import java
23
-
24
18
BigInteger = java.type("java.math.BigInteger")
25
19
myBigInt = BigInteger.valueOf(42)
26
20
# a public Java methods can just be called
27
-
myBigInt.shiftLeft(128) # returns a <JavaObject[java.math.BigInteger] at ...>
21
+
myBigInt.shiftLeft(128) # returns a <JavaObject[java.math.BigInteger] at ...>
28
22
# Java method names that are keywords in Python must be accessed using `getattr`
29
-
getattr(myBigInt, "not")() # returns a <JavaObject[java.math.BigInteger] at ...>
23
+
getattr(myBigInt, "not")() # returns a <JavaObject[java.math.BigInteger] at ...>
30
24
byteArray = myBigInt.toByteArray()
31
25
# Java arrays can act like Python lists
32
26
assertlen(byteArray) ==1and byteArray[0] ==42
@@ -37,11 +31,9 @@ For plain Python users, the `java` module is only available when running on the
37
31
</aside>
38
32
39
33
To import packages from the `java` namespace, you can also use the conventional Python import syntax:
40
-
41
34
```python
42
35
import java.util.ArrayList
43
36
from java.util import ArrayList
44
-
45
37
assert java.util.ArrayList == ArrayList
46
38
47
39
al = ArrayList()
@@ -52,12 +44,12 @@ assert list(al) == [1, 12]
52
44
53
45
In addition to the `type` built-in method, the `java` module exposes the following methods:
`instanceof(obj, class)` | returns `True` if `obj` is an instance of `class` (`class` must be a foreign object class)
58
-
`is_function(obj)` | returns `True` if `obj` is a Java host language function wrapped using interop
59
-
`is_object(obj)` | returns `True` if `obj` if the argument is Java host language object wrapped using interop
60
-
`is_symbol(obj)` | returns `True` if `obj` if the argument is a Java host symbol, representing the constructor and static members of a Java class, as obtained by `java.type`
47
+
Built-in | Specification
48
+
--- | ---
49
+
`instanceof(obj, class)` | returns `True` if `obj` is an instance of `class` (`class` must be a foreign object class)
50
+
`is_function(obj)` | returns `True` if `obj` is a Java host language function wrapped using interop
51
+
`is_object(obj)` | returns `True` if `obj` if the argument is Java host language object wrapped using interop
52
+
`is_symbol(obj)` | returns `True` if `obj` if the argument is a Java host symbol, representing the constructor and static members of a Java class, as obtained by `java.type`
See [Polyglot Programming](https://github.com/oracle/graal/blob/master/docs/reference-manual/polyglot-programming.md)
73
-
and [Embed Languages](https://github.com/oracle/graal/blob/master/docs/reference-manual/embedding/embed-languages.md)
74
-
for more information about interoperability with other programming languages.
64
+
See [Polyglot Programming](https://github.com/oracle/graal/blob/master/docs/reference-manual/polyglot-programming.md) and [Embed Languages](https://github.com/oracle/graal/blob/master/docs/reference-manual/embedding/embed-languages.md) for more information about interoperability with other programming languages.
75
65
76
66
## Interacting with other dynamic languages from Python scripts
77
67
78
-
More general, non-JVM specific interactions with other languages from Python scripts are achieved via the _polyglot_
79
-
API.
80
-
This includes all interactions with dynamic languages supported via
81
-
the [Truffle framework](https://www.graalvm.org/latest/graalvm-as-a-platform/language-implementation-framework/),
82
-
including JavaScript and Ruby.
68
+
More general, non-JVM specific interactions with other languages from Python scripts are achieved via the _polyglot_ API.
69
+
This includes all interactions with dynamic languages supported via the [Truffle framework](https://www.graalvm.org/latest/graalvm-as-a-platform/language-implementation-framework/), including JavaScript and Ruby.
83
70
84
71
### Installing other dynamic languages
85
72
86
73
Other languages can be included by using their respective Maven dependencies in the same manner as GraalPy.
87
-
For example, if you have already configured a Maven project with GraalPy, add the following dependency to gain access to
88
-
JavaScript:
89
-
74
+
For example, if you have already configured a Maven project with GraalPy, add the following dependency to gain access to JavaScript:
This program matches Python strings using the JavaScript regular expression object. Python reads the captured group
162
-
from the JavaScript result and checks for a substring in it.
144
+
This program matches Python strings using the JavaScript regular expression object. Python reads the captured group from the JavaScript result and checks for a substring in it.
163
145
164
146
## Exporting Python Objects to other Languages
165
147
166
-
The `polyglot` module can be used to expose Python objects to JVM languages and other Graal languages (languages
167
-
implemented on
168
-
the [Truffle framework](https://www.graalvm.org/latest/graalvm-as-a-platform/language-implementation-framework/)).
148
+
The `polyglot` module can be used to expose Python objects to JVM languages and other Graal languages (languages implemented on the [Truffle framework](https://www.graalvm.org/latest/graalvm-as-a-platform/language-implementation-framework/)).
169
149
170
150
1. You can export some objectfrom Python to other languages so they can import it:
@@ -207,24 +187,21 @@ the [Truffle framework](https://www.graalvm.org/latest/graalvm-as-a-platform/lan
207
187
208
188
## Mapping Types between Python and Other Languages
209
189
210
-
The interop protocol defines different "types" which can overlap in all kinds of ways and have restrictions on how they
211
-
can interact with Python.
190
+
The interop protocol defines different "types" which can overlap in all kinds of ways and have restrictions on how they can interact with Python.
212
191
213
192
### Interop Types to Python
214
193
215
194
Most importantly and upfront: all foreign objects passed into Python have the Python type `foreign`.
216
195
There is no emulation of (for example) objects that are of interop type "boolean" to have the Python type `bool`.
217
-
This is because interop types can overlap in ways that the Python built-in types cannot, and we have yet to define which
218
-
type should take precedence and such situations.
196
+
This is because interop types can overlap in ways that the Python built-in types cannot, and we have yet to define which type should take precedence and such situations.
219
197
We do expect to change this in the future, however.
220
-
For now, the `foreign` type defines all of the Python special methods for type conversion that are used throughout the
221
-
interpreter (methods such as `__add__`, `__int__`, `__str__`, `__getitem__`, and so on)
198
+
For now, the `foreign` type defines all of the Python special methods for type conversion that are used throughout the interpreter (methods such as `__add__`, `__int__`, `__str__`, `__getitem__`, and so on)
222
199
and these try to "do the right thing" based on the interop type (or raise an exception).
223
200
224
201
Types not listed in the table below have no special interpretation in Python.
| `null` | `null` is like `None`. Important to know: interop `null` values are all identical to `None`. JavaScript defines two "null-like" values; `undefined` and `null`, which are *not* identical, but when passed to Python, they are treated so. |
229
206
| `boolean` | `boolean` behaves like Python booleans, including the fact that in Python, all booleans are also integers (1 and 0 for true and false, respectively). |
230
207
| `number` | `number` Behaves like Python numbers. Python only has one integer and one floating point type, but ranges are imported in some places such as typed arrays. |
@@ -238,12 +215,12 @@ Types not listed in the table below have no special interpretation in Python.
238
215
|`exception`| An `exception` can be caught in a generic `except` clause. |
239
216
|`MetaObject`| Meta objects can be used in subtype and `isinstance` checks. |
240
217
|`executable`| An `executable` object can be executed as a function, but never with keyword arguments. |
241
-
| `instantiable` | An `instantiable` object can be called just like a Python type, but never with keyword arguments. |
218
+
| `instantiable` | An `instantiable` object can be called just like a Python type, but never with keyword arguments. |
| register_interop_behavior | Takes the receiver **type** as first argument. The remainder keyword arguments correspond to the [respective interop messages](#supported-messages). Not All interop messages are supported. |
275
-
| get_registered_interop_behavior | Takes the receiver **type** as first argument. Returns the list of extended interop messages for the given type. |
276
-
| @interop_behavior | Class decorator, takes the receiver **type** as only argument. The interop messages are extended via **static** methods defined in the decorated class (supplier). |
| register_interop_behavior | Takes the receiver **type** as first argument. The remainder keyword arguments correspond to the respective interop messages. Not All interop messages are supported. |
250
+
| get_registered_interop_behavior | Takes the receiver **type** as first argument. Returns the list of extended interop messages for the given type. |
251
+
| @interop_behavior | Class decorator, takes the receiver **type** as only argument. The interop messages are extended via **static** methods defined in the decorated class (supplier). |
277
252
| register_interop_type | Takes a `foreign class` and `python class` as positional arguments and `allow_method_overwrites` as optional argument (default: `False`). Every instance of foreign class is then treated as an instance of the given python class. |
278
253
| @interop_type | Class decorator, takes the `foreign class` and optionally `allow_method_overwrites` as arguments. The instances of foreign class will be treated as an instance of the annotated python class. |
0 commit comments