Skip to content

Commit 75c8243

Browse files
committed
Merge branch 'master'
2 parents 8f246dd + b64c096 commit 75c8243

File tree

347 files changed

+16119
-7854
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

347 files changed

+16119
-7854
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

bisect-benchmark.ini

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[bisect-benchmark]
2+
# This is the configuration file for bisecting benchmark jobs in the CI.
3+
# Usage:
4+
# - Create a temporary branch based on master (or the bad commit)
5+
# - Fill in this configuration file, commit the changes and push it
6+
# - Execute the bisect-benchmark job for the commit. When you click the "create
7+
# PR" link, you can run jobs in commits UI (Actions). You need to wait a bit
8+
# for the job enumerator to populate the job list. You don't need to actually
9+
# create any PR.
10+
# - Wait for the jobs to finish. You should get an email when it's done
11+
12+
# The command to build particular configuration. You can copy paste this from
13+
# the benchmark job log. Don't worry about sforceimports, the script takes care
14+
# of that
15+
build_command = mx --dy /compiler build
16+
17+
# The commmand to run the particular benchmark on the configuration. You can
18+
# copy paste this from the benchmark job log. Make sure you replace the '*'
19+
# wildcard with a single benchmark, the script only supports bisecting a single
20+
# benchmark at a time
21+
benchmark_command = mx --dy /compiler benchmark micro:try-except-simple
22+
23+
# The first known "bad" merge commit for bisection. Try to use long commit
24+
# SHAs, the CI cache has higher probability of short SHAs being ambiguous
25+
bad = 1234deadbeef
26+
27+
# The last known "good" merge commit for bisection
28+
good = 1234cafebabe
29+
30+
# Whether to checkout graal-enterprise
31+
enterprise = true
32+
33+
# Which result parameter should be used for comparisons, one of BEST, WORST,
34+
# AVG (all runs), AVG (no warmup)
35+
benchmark_criterion = BEST

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "overlay": "09a17d4b400a70768d3123235d66f54b8ea628f2" }
1+
{ "overlay": "5e162687796a6e3c6274904b8d7fdec55594760a" }

docs/contributor/CONTRIBUTING.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,14 @@ A tag file can be regenerated with
180180

181181
There's also multiple other gates that may fail with changes. One of these is
182182
our *style* gate, which checks formatting rules and copyrights. To auto-fix most
183-
issues, run the following command. Anything that's reported as error after this
184-
command you have to fix manually.
183+
issues, run the following commands. Anything that's reported as error after this
184+
command you have to fix manually. Note that to really match what's in the gate,
185+
you have to set the `JDT` environment variable to the path to an Eclipse
186+
compiler Jar file, and the `ECLIPSE_EXE` environment variable to the path of an
187+
eclipse executable.
185188

186189
mx python-style --fix
190+
mx python-gate --tags style
187191

188192
Another important gate is the gate that checks if you broke the native image
189193
building. To test if building a native image still works, you can use the
@@ -225,3 +229,63 @@ For additional arguments to the Python launcher, you can separate them by
225229
another double-dash:
226230

227231
mx benchmark meso:nbody3 -- --python-vm=graalpython -- --python.EmulateJython -Dgraal.Dump= -Dgraal.MethodFilter=*measure*
232+
233+
#### A note on terminology
234+
235+
Note that there may be a little confusion about the configuration names of
236+
benchmarks.
237+
238+
##### GraalVM Community and GraalVM Enterprise configurations
239+
240+
We have benchmarks for GraalVM Community and Enterprise. For historical reasons,
241+
these are sometimes referred to in some config files as *CE* and *EE*; *core*
242+
and *enterprise*; *graalvm_ce* and *graalvm_ee*; or *graalpython_core* and
243+
*graalpython_enterprise*, respectively.
244+
245+
##### Different GraalVM Python configurations
246+
247+
There are also different options for how the Python interpreter is run, passed
248+
via the `--python-vm-config` parameter:
249+
* `default` - run using the standard options
250+
* `default-multi` - run using a shared engine, which is the mode that is
251+
recommended to embedders that want to spawn multiple isolated Python contexts
252+
* `native` - same as `default`, its name is due to the fact that it runs C
253+
extensions using a mixture of LLVM bitcode interpreted and compiled via
254+
GraalVM and real native libraries
255+
* `sandboxed` - this name is historical - this configuration requires a GraalVM
256+
Enterprise and runs all C extensions purely as LLVM bitcode on the GraalVM,
257+
without any access to the native OS libraries, i.e., using the
258+
`--llvm.managed` option for GraalVM.
259+
260+
##### Configuration of the underlying GraalVM runtime
261+
262+
Finally, there are the `--jvm` and `--jvm-config` configuration options for `mx
263+
benchmark`. By default, the commands presented above will run on the JVM in
264+
*server* mode, using the Graal compiler in what we call *hosted* mode. This is
265+
almost the same but not quite the `--jvm` mode you will get when running the
266+
`graalpython` executable from a full GraalVM, and usually good enough if you
267+
want to look at the compiler graphs or peak performance numbers. In our CI,
268+
however, we always build a full GraalVM and benchmark using that, since that is
269+
what we ship. There, we have two different configurations corresponding to the
270+
launcher flags available for the GraalVM `graalpython` executable: *jvm* and
271+
*native*. The first runs on top of Hotspot using the Graal compiler, the second
272+
runs the AOT compiled GraalVM native image of Python.
273+
274+
Building a GraalVM Python configuration can be achieved for the CE version like
275+
so:
276+
277+
mx --env ../../graal/vm/mx.vm/ce --exclude-components=slgm --dynamicimports /vm graalvm-show
278+
mx --env ../../graal/vm/mx.vm/ce --exclude-components=slgm --dynamicimports /vm build
279+
280+
The first command will print some information about the GraalVM configuration
281+
that is about to be built, and the second will build it. **IMPORTANT:** The
282+
first command should tell you that the `Config name` is `ce_python`, otherwise
283+
the next commands will not work.
284+
285+
To run the JVM configuration:
286+
287+
mx --env ../../graal/vm/mx.vm/ce --exclude-components=slgm --dynamicimports /vm benchmark meso:nbody3 -- --python-vm=graalpython --jvm=graalvm-ce-python --jvm-config=jvm --python-vm-config=default --
288+
289+
To run the Native Image configuration:
290+
291+
mx --env ../../graal/vm/mx.vm/ce --exclude-components=slgm --dynamicimports /vm benchmark meso:nbody3 -- --python-vm=graalpython --jvm=graalvm-ce-python --jvm-config=native --python-vm-config=default --

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/com.oracle.graal.python.annotations/src/com/oracle/graal/python/annotations/ArgumentClinic.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,14 @@ enum ClinicConversion {
127127
* and {@link #useDefaultForNone()}.
128128
*/
129129
Index,
130+
/**
131+
* Corresponds to CPython's {@code int(accept={str})} convertor. Supports
132+
* {@link #defaultValue()}, and {@link #useDefaultForNone()}.
133+
*/
134+
CodePoint,
135+
/**
136+
* Corresponds to CPython's {@code Py_buffer} convertor.
137+
*/
138+
Buffer,
130139
}
131140
}

graalpython/com.oracle.graal.python.cext/src/bytesobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
// taken from CPython "Objects/bytesobject.c"
4747
#define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1)
4848

49-
PyTypeObject PyBytes_Type = PY_TRUFFLE_TYPE("bytes", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_BYTES_SUBCLASS, PyBytesObject_SIZE);
49+
PyTypeObject PyBytes_Type = PY_TRUFFLE_TYPE_WITH_ITEMSIZE("bytes", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_BYTES_SUBCLASS, PyBytesObject_SIZE, sizeof(char));
5050

5151
typedef PyObject* (*fromStringAndSize_fun_t)(int8_t* str, int64_t sz);
5252

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ PyObject* get_tp_dict(PyTypeObject* obj) {
300300
return native_to_java(obj->tp_dict);
301301
}
302302

303+
/** to be used from Java code only; reads native 'tp_base' field */
304+
PyObject* get_tp_base(PyTypeObject* obj) {
305+
return native_to_java((PyObject*) obj->tp_base);
306+
}
307+
303308
/** to be used from Java code only; reads native 'tp_bases' field */
304309
PyObject* get_tp_bases(PyTypeObject* obj) {
305310
return native_to_java(obj->tp_bases);
@@ -475,13 +480,16 @@ const char* PyTruffle_StringToCstr(void* o, int32_t strLen) {
475480
return str;
476481
}
477482

483+
/* Use this function to decode a C char array to a Java string using the source file encoding. */
478484
void* PyTruffle_CstrToString(void* o) {
479-
if (polyglot_fits_in_i64(o)) {
480-
return polyglot_from_string((const char*)polyglot_as_i64(o), SRC_CS);
481-
}
482485
return polyglot_from_string(o, SRC_CS);
483486
}
484487

488+
/* Use this function to decode a C ASCII string to a Java string. */
489+
void* PyTruffle_AsciiToString(void* ptr) {
490+
return polyglot_from_string(ptr, "ascii");
491+
}
492+
485493
/* To be used from Java code only.
486494
* This function is used if a native class inherits from a managed class but uses the 'object.__new__'.
487495
* This function roughly corresponds to CPython's 'object_new'. */

0 commit comments

Comments
 (0)