Skip to content

Commit 0b93b97

Browse files
committed
Fix/clarify Jython compatibility examples
Fixes #158
1 parent d4401ee commit 0b93b97

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

docs/user/Jython.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ command line flag on Graal: `--python.EmulateJython`.
2222

2323
### Importing
2424
Import statements allow you to import Java classes, but (unlike Jython), only
25-
packages in the `java` namespace can be directly imported. This will work:
25+
packages in the `java` namespace can be directly imported. Importing classes
26+
from packages outside `java` namespace also requires the
27+
`--python.EmulateJython` option to be active.
28+
This will work:
2629
```
2730
import java.lang as lang
2831
```
@@ -33,7 +36,7 @@ from javax.swing import *
3336
```
3437
Instead, you will have to import one of the classes you are interested in directly:
3538
```
36-
import javax.swing.Window as Window
39+
import javax.swing.JWindow as JWindow
3740
```
3841

3942
### Basic Object Usage
@@ -47,7 +50,7 @@ methods:
4750
>>> boundNextInt = rg.nextInt
4851
>>> rg.nextInt()
4952
1491444859
50-
>>> boundNextInt = rg.nextInt
53+
>>> boundNextInt()
5154
1672896916
5255

5356
### Java-to-Python Types: Automatic Conversion
@@ -60,18 +63,18 @@ more dynamic approach to matching — Python types emulating `int` or
6063
example, to use Pandas frames as `double[][]` or NumPy array elements as `int[]`
6164
when the elements fit into those Java primitive types.
6265

63-
| Java type | Python type |
64-
|:-----------------------|:----------------------------------------------------------------------------------|
65-
| null | None |
66-
| boolean | bool |
67-
| byte, short, int, long | int, any object that has an `__int__` method |
68-
| float | float, any object that has a `__float__` method |
69-
| char | str of length 1 |
70-
| java.lang.String | str |
71-
| byte[] | bytes, bytearray, wrapped Java array, Python list with only the appropriate types |
72-
| Java arrays | Wrapped Java array or Python list with only the appropriate types |
73-
| Java objects | Wrapped Java object of the appropriate type |
74-
| java.lang.Object | Any object |
66+
| Java type | Python type |
67+
|:--------------------------------|:--------------------------------------------------------------------------|
68+
| `null` | `None` |
69+
| `boolean` | `bool` |
70+
| `byte`, `short`, `int` , `long` | `int`, any object that has an `__int__` method |
71+
| `float`, `double` | `float`, any object that has a `__float__` method |
72+
| `char` | `str` of length 1 |
73+
| `java.lang.String` | `str` |
74+
| `byte[]` | `bytes`, `bytearray`, wrapped Java array, Python list with only the appropriate types |
75+
| Java arrays | Wrapped Java array or Python list with only the appropriate types |
76+
| Java objects | Wrapped Java object of the appropriate type |
77+
| `java.lang.Object` | Any object |
7578

7679
### Special Jython Modules
7780
Any of the special Jython modules are not available. For example, the `jarray`
@@ -85,6 +88,8 @@ The code that only needs to pass a Java array can also use Python types. However
8588
implicitly, this may entail a copy of the array data, which can be deceiving when
8689
using Java arrays as output parameters:
8790

91+
>>> # This example needs the --python.EmulateJython flag for the java.io import
92+
>>> import java
8893
>>> i = java.io.ByteArrayInputStream(b"foobar")
8994
>>> buf = [0, 0, 0]
9095
>>> i.read(buf) # buf is automatically converted to a byte[] array
@@ -115,6 +120,8 @@ There is no automatic mapping of the Python syntax for accessing dictionary
115120
elements to the `java.util` mapping and list classes' ` get`, `set`, or `put`
116121
methods. To use these mapping and list clases, you must call the Java methods:
117122

123+
>>> # This example needs the --python.EmulateJython flag for the java.util import
124+
>>> import java
118125
>>> ht = java.util.Hashtable()
119126
>>> ht.put("foo", "bar")
120127
>>> ht.get("foo")
@@ -153,6 +160,7 @@ public class PythonHandler extends Handler {
153160
```
154161
Then you can use it like this in Python:
155162
```
163+
# This example needs the --python.EmulateJython flag for the java.util import
156164
from java.util.logging import LogManager, Logger
157165
158166
class MyHandler():

0 commit comments

Comments
 (0)