Skip to content

Commit 7de3a20

Browse files
committed
[GR-26072] Documentation fixes
PullRequest: graalpython/1270
2 parents 011fbd5 + 7256538 commit 7de3a20

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ is made available for experimentation and curious end-users.
1212
To try it, you can use the bundled releases from
1313
[www.graalvm.org](https://www.graalvm.org/downloads/). For more information and
1414
some examples of what you can do with it, check out the
15-
[reference](https://www.graalvm.org/docs/reference-manual/languages/python/).
15+
[reference](https://www.graalvm.org/reference-manual/python/).
1616

1717
### Create a virtual environment
1818

@@ -80,7 +80,7 @@ cross-language interop. This will hopefully give you an idea how to use it.
8080
We are working on a mode that is "mostly compatible" with some of Jython's
8181
features, minus of course that Jython implements Python 2.7 and we implement
8282
Python 3.8+. We describe the current status of the compatibility mode
83-
[here](docs/user/JYTHON.md).
83+
[here](docs/user/Jython.md).
8484

8585
### Contributing
8686

docs/user/Interoperability.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,15 @@ Ruby, and LLVM. GraalVM provides a Python API to interact with other languages
55
available on the GraalVM. In fact, GraalVM uses this API internally to
66
execute Python C extensions using the LLVM implementation in GraalVM.
77

8-
You can import the `polyglot` module to interact with other languages.
8+
You can import the `polyglot` module to interact with other languages. In order
9+
to use the polyglot functionality, you need to specify `--jvm --polyglot`
10+
arguments to the `graalpython` executable.
911

1012
```python
1113
import polyglot
1214
```
1315

14-
You can import a global value from the entire polyglot scope:
15-
```python
16-
imported_polyglot_global = polyglot.import_value("global_name")
17-
```
18-
19-
This global should then work as expected; accessing attributes assumes it reads
20-
from the `members` namespace; accessing items is supported both with strings and
21-
numbers; calling methods on the result tries to do a straight invoke and falls
22-
back to reading the member and trying to execute it.
23-
24-
You can evaluate some code in another language:
16+
## Evaluating code in other languages
2517
```python
2618
polyglot.eval(string="1 + 1", language="ruby")
2719
```
@@ -36,6 +28,7 @@ If you pass a file, you can also try to rely on the file-based language detectio
3628
polyglot.eval(path="./my_ruby_file.rb")
3729
```
3830

31+
## Exporting and importing values
3932
To export something from Python to other Polyglot languages so they can import
4033
it:
4134
```python
@@ -51,6 +44,19 @@ def python_method():
5144
return "Hello from Python!"
5245
```
5346

47+
You can import a global value from the entire polyglot scope (exported from
48+
another language):
49+
```python
50+
imported_polyglot_global = polyglot.import_value("global_name")
51+
```
52+
53+
This global should then work as expected; accessing attributes assumes it reads
54+
from the `members` namespace; accessing items is supported both with strings and
55+
numbers; calling methods on the result tries to do a straight invoke and falls
56+
back to reading the member and trying to execute it.
57+
58+
59+
## Examples
5460
Here is an example of how to use JavaScript regular expression engine to
5561
match Python strings. Save this code to the `polyglot_example.py` file:
5662

@@ -135,7 +141,7 @@ the `java` module:
135141
```python
136142
import java
137143
BigInteger = java.type("java.math.BigInteger")
138-
myBigInt = BigInteger(42)
144+
myBigInt = BigInteger.valueOf(42)
139145
myBigInt.shiftLeft(128) # public Java methods can just be called
140146
myBigInt["not"]() # Java method names that are keywords in
141147
# Python can be accessed using "[]"
@@ -144,7 +150,8 @@ print(list(byteArray)) # Java arrays can act like Python lists
144150
```
145151

146152
For packages under the `java` package, you can also use the normal Python import
147-
syntax:
153+
syntax. This syntax is limited to importing concrete classes, it doesn't work
154+
on packages, unless in [Jython Compatibility](Jython.md) mode.
148155
```python
149156
import java.util.ArrayList
150157
from java.util import ArrayList
@@ -179,5 +186,5 @@ print(java.is_function(my_list.add))# prints True, the add method of ArrayList
179186
print(java.instanceof(my_list, ArrayList)) # prints True
180187
```
181188

182-
See the [Polyglot Programming](https://www.graalvm.org/docs/reference-manual/polyglot-programming/) and the [Embed Languages](https://www.graalvm.org/reference-manual/embed-languages/) reference
189+
See the [Polyglot Programming](https://www.graalvm.org/reference-manual/polyglot-programming/) and the [Embed Languages](https://www.graalvm.org/reference-manual/embed-languages/) reference
183190
for more information about interoperability with other programming languages.

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():

graalpython/lib-python/3/test/test_isinstance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def blowstack(fxn, arg, compare_to):
277277
# argument will raise RecursionError eventually.
278278
tuple_arg = (compare_to,)
279279
# TODO: GR-23749 revert back when truffle support is in
280-
for cnt in range(10000 if sys.implementation.name == "graalpython" else sys.getrecursionlimit()+5):
280+
for cnt in range(10000000 if sys.implementation.name == "graalpython" else sys.getrecursionlimit()+5):
281281
tuple_arg = (tuple_arg,)
282282
fxn(arg, tuple_arg)
283283

0 commit comments

Comments
 (0)