Skip to content

Commit a2843dc

Browse files
authored
Merge branch 'master' into github_ci/fix_conflicts
2 parents 2cebdbd + 2e52832 commit a2843dc

File tree

172 files changed

+5150
-2380
lines changed

Some content is hidden

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

172 files changed

+5150
-2380
lines changed

docs/site/_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
baseurl: "/python"
22
url: "https://graalvm.org"
33
github: "oracle/graalpython"
4-
language_version: 25.0.0
4+
language_version: 25.0.1
55
name: GraalPy
66

77
permalink: pretty
-58.9 KB
Loading
163 KB
Loading
-32.3 KB
Loading

docs/site/index.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,25 @@ graalPy {
335335
<div class="lang-video">
336336
<div class="container">
337337
<h3 class="truffle__subtitle">Videos</h3>
338+
<div class="lang-video__row">
339+
<div class="lang-video__card video-card">
340+
<a href="https://youtu.be/YqrEqXB59rA?si=3-PZGY2WUfltvCp0&t=1406" target="_blank" class="btn btn-primary--filledp">
341+
<img src="{{ '/assets/img/python/intellij2025-cover.png' | relative_url }}" class="video-cover" alt="IntelliJ 2025 thumbnail">
342+
<div class="play-button-container">
343+
<img src="{{ '/assets/img/play_button.svg' | relative_url }}" class="play-button" alt="play button">
344+
</div>
345+
</a>
346+
</div>
347+
<div class="lang-video__card">
348+
<div class="lang-video__title">
349+
<h4>Tips and Tricks for GraalVM and Graal Languages</h4>
350+
</div>
351+
<div class="lang-video__text">
352+
<h5>In this session, Fabio Niephaus from the GraalVM team shows his favourite tips and tricks for using GraalPy and other Graal Languages in IntelliJ IDEA. He also shows how to use IntelliJ IDEA as a multi-language IDE. Language injections and support for various debugging protocols make it easy to embed and debug code written in languages like Python in Java applications.
353+
</h5>
354+
</div>
355+
</div>
356+
</div>
338357
<div class="lang-video__row">
339358
<div class="lang-video__card video-card">
340359
<a href="https://www.youtube.com/watch?v=IdoFsS-mpVw" target="_blank" class="btn btn-primary--filledp">
Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,68 @@
11
# Native Executables with Python
22

3-
GraalPy supports GraalVM Native Image to generate native binaries of Java applications that use GraalPy.
3+
GraalPy supports GraalVM Native Image to generate native binaries of Java applications that embed Python code.
44

55
## Quickstart
66

7-
If you started with the [Maven archetype](README.md), the generated _pom.xml_ makes it easy to generate a native executable using the [Maven plugin for Native Image building](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html).
7+
If you started with the [Maven archetype](README.md), the generated _pom.xml_ file already includes the necessary configuration for creating a native executable using the [Maven plugin for Native Image building](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html).
88

99
To build the application, run:
10+
1011
```bash
1112
mvn -Pnative package
1213
```
13-
The command packages the project and creates a native executable.
1414

15-
Take a look at the generated files _pom.xml_ and _Main.java_.
16-
They are documented to explain how Python resources are included in the resulting binary.
17-
The generated project should be viewed as a starting point.
18-
It includes the entire Python standard library, so the Python code can invoke all of the standard library code.
19-
The resources can be manually pruned to reduce the included Python libraries to the necessary amount, reducing both the size of the package and the time to start up.
20-
This Java example demonstrates some useful default options for the Python context, but other settings may be desirable to further control what the Python code is allowed to do.
15+
This command packages the project and creates a native executable.
2116

22-
## Reducing Binary Size
17+
The generated _pom.xml_ and _Main.java_ files explain how Python resources are included in the resulting binary.
18+
The generated project serves as a starting point and includes the entire Python standard library by default, allowing your Python code to use any standard library modules.
19+
You can manually remove unused Python libraries to reduce both the executable size and startup time.
20+
The created example demonstrates useful default options for the Python context, but you can adjust these settings to control what your Python code can access.
2321

24-
Python is a large language.
25-
"Batteries included" has long been a core tenet of CPython.
26-
As a compatible replacement, GraalPy includes most of those "batteries" as well.
27-
This can result in significant increases in binary size when including GraalPy in a Java application.
28-
29-
Only you as the developer know your specific embedding scenario.
30-
The defaults may include much more than needed for any specific application.
31-
An embedded Python-in-Java application usually has more limited use cases for the Python interpreter than the full GraalPy distribution, and often you can know ahead of time whether certain features are needed.
32-
Some features (for example, cryptographic algorithms or socket access) may even be undesirable to include in some cases.
33-
Thus, when embedding GraalPy in a Java application, the binary size can be reduced and security improved by excluding components of the Python language.
34-
35-
### Excluding Python Components
36-
37-
GraalPy defines a few system properties that can be passed on the `native-image` command line to exclude aspects of the language.
38-
The options can, when taken together, reduce the size of the executable by around 20%.
39-
These are:
40-
41-
* `python.WithoutSSL=true` - This option removes the `ssl` module.
42-
If no secure network access or certificate checking is required, this removes Java's SSL classes and the BouncyCastle library.
43-
* `python.WithoutDigest=true` - This option removes the `_md5`, `_sha1`, `_sha256`, `_sha512`, `_sha3`, and `_hashlib` modules.
44-
This removes the direct usages of `java.security.MessageDigest` and `javax.crypto.Mac` from GraalPy.
45-
* `python.WithoutPlatformAccess=true` - This removes the `signal` and `subprocess` modules, removes accesses to process properties such as the Unix UID and GID, or setting the Java default time zone.
46-
This has no significant impact on binary size, but if these are unwanted capabilities that are dynamically disabled with context options anyway, they can also be removed ahead of time.
47-
* `python.WithoutCompressionLibraries=true` - This removes the `zlib`, `lzma`, `bzip2`, and `zipimporter` modules and related classes.
48-
These modules have both native and pure Java implementations (the former for performance, the latter for better sandboxing); however, if they are not needed, they can be removed entirely.
49-
* `python.WithoutNativePosix=true` - The default `os` module backend is a pure Java implementation when GraalPy is embedded rather than run via its launcher.
50-
The native POSIX backend of GraalPy is recommended only for 100% compatibility with CPython's POSIX interfaces, and if not used, can be removed from the build with this option.
51-
* `python.WithoutJavaInet=true` - The Java implementation of Python's `socket` module is based on Java's networking classes.
52-
If network access is denied for an embedding scenario, this option can reduce the binary size further.
53-
* `python.AutomaticAsyncActions=false` - Signal handling, Python weak reference callbacks, and cleaning up native resources is usually achieved automatically by spawning GraalPy daemon threads that submit safepoint actions to the Python main thread.
54-
This uses an `ExecutorService` with a thread pool.
55-
If you want to disallow such extra threads or avoid pulling in `ExecutorService` and related classes, then set this property to `false` and retrieve the `PollPythonAsyncActions` object from the context's polyglot bindings.
56-
This object is executable and can be used to trigger Python asynchronous actions at the locations you desire.
22+
## Reducing Binary Size
5723

24+
Python is a feature-rich language with an extensive standard library.
25+
This can result in large native executables when embedding GraalPy in Java applications.
26+
You can significantly reduce the size by excluding components your application doesn't need by considering what your Python code actually uses.
5827

5928
### Removing Pre-initialized Python Heap
6029

61-
Another useful option to reduce the size of the native executable is to omit a pre-initialized Python context from the executable.
62-
By default, a default Python context is already pre-initialized and ready for immediate execution.
63-
This can lead to slightly improved startup, at the cost of including a few thousand Python objects in the binary.
64-
In embedded applications that use a custom polyglot engine to allow context sharing, the pre-initialized context cannot be used at all, and including those objects is wasted.
65-
The pre-initialized heap can be omitted by passing the following to the `native-image` command:
30+
By default, GraalPy includes a pre-initialized Python context in the executable for faster startup.
31+
Disabling this reduces the binary size by about 15MiB.
32+
You should remove this if:
33+
- You are creating more than one context
34+
- Binary size is more important than a slight startup delay
35+
36+
To remove the pre-initialized heap, add this flag to your build configuration:
6637

6738
```bash
68-
-Dimage-build-time.PreinitializeContexts=
39+
-Dpolyglot.image-build-time.PreinitializeContexts=
6940
```
7041

7142
### Disabling Runtime Compilation of Python Code
7243

73-
If binary size is significantly more important than execution speed (which may be the case if all Python scripts are expected to be short running, perform a lot of I/O, or scripts are rarely executed more than once), it may make sense to disable JIT compilation entirely.
44+
If binary size is significantly more important than execution speed, you can disable JIT compilation entirely.
45+
This will reduce binary size by around 40%.
46+
You should use this if:
47+
48+
- Your Python scripts are very short-running
49+
- Performance is not critical
50+
- Your scripts spend most time on I/O operations
51+
7452
Be aware that this may significantly impact your Python performance, so be sure to test the runtime behavior of your actual use cases when choosing to use this option.
53+
7554
This can be achieved by passing the following options:
7655

7756
```bash
78-
-Dtruffle.TruffleRuntime=com.oracle.truffle.api.impl.DefaultTruffleRuntime \
57+
-Dtruffle.TruffleRuntime=com.oracle.truffle.api.impl.DefaultTruffleRuntime
7958
-Dpolyglot.engine.WarnInterpreterOnly=false
8059
```
8160

8261
### Summary
8362

84-
Combining all of these approaches can halve the size of the GraalPy binary.
85-
Every embedded application is different and the code pulled in by the rest of the Java code also matters, so combinations of these options should be tried to determine which effect they have in a specific instance.
63+
Combining these approaches can reduce the binary size by 50% or more.
64+
Since every application is different, experiment with different combinations to find what works best for your specific use case.
8665

8766
## Shipping Python Packages
8867

8968
Our Maven archetype by default is set up to include all needed Python files in the native binary itself, so the image is self-contained.
90-
91-
In custom embeddings, the Python standard library is copied next to the native image.
92-
When moving the native image, the standard library folder needs to be kept next to it.

graalpython/com.oracle.graal.python.annotations/src/com/oracle/graal/python/annotations/Builtin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@
7777
boolean needsFrame() default false;
7878

7979
/**
80-
* By default the caller frame bit is set on-demand, but for some builtins it might be useful to
81-
* always force passing the caller frame.
80+
* By default the callerFlags bits are set on-demand, but for some builtins it might be useful
81+
* to pre-set them.
8282
*/
83-
boolean alwaysNeedsCallerFrame() default false;
83+
int callerFlags() default 0;
8484

8585
/**
8686
* Module functions should be bound to their module, meaning they would take the module itself

graalpython/com.oracle.graal.python.annotations/src/com/oracle/graal/python/annotations/Slot.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
* The slot node either needs frame for execution (e.g., may call Python code) and/or is complex
5757
* enough that indirect call to partially evaluated code is preferred over uncached execution
5858
* without frame and without setting up indirect call context.
59-
*
59+
*
6060
* The slot call nodes AST inline slot nodes, but if the inline cache overflows or in the
6161
* uncached case, they need to either call uncached slot node or do indirect call if
6262
* {@code isComplex} is {@code true}.
@@ -97,7 +97,7 @@
9797

9898
boolean needsFrame() default false;
9999

100-
boolean alwaysNeedsCallerFrame() default false;
100+
int callerFlags() default 0;
101101
}
102102

103103
/** See <a href="https://docs.python.org/3/c-api/typeobj.html">slot documentation</a> */

graalpython/com.oracle.graal.python.benchmarks/python/harness.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ def ccompile(name, code):
209209

210210
rootdir = os.path.dirname(__file__)
211211
while os.path.basename(rootdir) != 'graalpython':
212+
prevdir = rootdir
212213
rootdir = os.path.dirname(rootdir)
214+
if rootdir == prevdir:
215+
# Reached the root of the file system
216+
raise RuntimeError("'graalpython' directory not found in path!")
213217

214218
sys.path.append(os.path.join(
215219
rootdir,

graalpython/com.oracle.graal.python.benchmarks/python/micro/c-arith-binop.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -102,6 +102,12 @@
102102
"""
103103

104104

105+
import sys
106+
import os
107+
# Add benchmark directory to path to allow import of harness.py
108+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
109+
from harness import ccompile
110+
105111
ccompile("c_arith_binop_module", code)
106112
from c_arith_binop_module import FloatSubclass
107113

@@ -127,3 +133,28 @@ def measure(num):
127133

128134
def __benchmark__(num=5):
129135
return measure(num)
136+
137+
138+
def run():
139+
__benchmark__(num=2)
140+
141+
142+
def warmupIterations():
143+
return 25
144+
145+
146+
def iterations():
147+
return 30
148+
149+
150+
def summary():
151+
return {
152+
"name": "OutlierRemovalAverageSummary",
153+
"lower-threshold": 0.0,
154+
"upper-threshold": 0.7,
155+
}
156+
157+
158+
def dependencies():
159+
# Required to run `ccompile`
160+
return ["harness.py", "tests/__init__.py"]

0 commit comments

Comments
 (0)