Skip to content

Commit 5dcef39

Browse files
committed
provide a graalpy context builder from python.embedding
1 parent 7b5d84a commit 5dcef39

File tree

7 files changed

+92
-243
lines changed

7 files changed

+92
-243
lines changed

graalpython/graalpy-archetype-polyglot-app/src/main/resources/archetype-resources/src/main/java/GraalPy.java

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,74 +41,18 @@
4141

4242
package ${package};
4343

44-
#set( $symbol_pound = '#' )
45-
#set( $symbol_dollar = '$' )
46-
#set( $symbol_escape = '\' )
4744
import org.graalvm.polyglot.Context;
48-
import org.graalvm.polyglot.PolyglotAccess;
4945
import org.graalvm.polyglot.PolyglotException;
5046
import org.graalvm.polyglot.Source;
5147
import org.graalvm.polyglot.Value;
52-
import org.graalvm.polyglot.HostAccess;
53-
import org.graalvm.polyglot.io.IOAccess;
5448
import java.io.IOException;
5549
import org.graalvm.python.embedding.vfs.VirtualFileSystem;
5650

5751
public class GraalPy {
5852
private static final String PYTHON = "python";
5953

60-
public static Context getContext() {
61-
VirtualFileSystem vfs = VirtualFileSystem.newBuilder()
62-
.extractFilter(p -> {
63-
String s = p.toString();
64-
// Specify what files in the virtual filesystem need to be accessed outside the Truffle sandbox.
65-
// e.g. if they need to be accessed by the operating system loader.
66-
return s.endsWith(".ttf");
67-
})
68-
.build();
69-
Context context = Context.newBuilder()
70-
// set true to allow experimental options
71-
.allowExperimentalOptions(false)
72-
// setting false will deny all privileges unless configured below
73-
.allowAllAccess(false)
74-
// allows python to access the java language
75-
.allowHostAccess(HostAccess.ALL)
76-
// allow access to the virtual and the host filesystem, as well as sockets
77-
.allowIO(IOAccess.newBuilder()
78-
.allowHostSocketAccess(true)
79-
.fileSystem(vfs)
80-
.build())
81-
// allow creating python threads
82-
.allowCreateThread(true)
83-
// allow running Python native extensions
84-
.allowNativeAccess(true)
85-
// allow exporting Python values to polyglot bindings and accessing Java from Python
86-
.allowPolyglotAccess(PolyglotAccess.ALL)
87-
// choose the backend for the POSIX module
88-
.option("python.PosixModuleBackend", "java")
89-
// equivalent to the Python -B flag
90-
.option("python.DontWriteBytecodeFlag", "true")
91-
// equivalent to the Python -v flag
92-
.option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false")
93-
// log level
94-
.option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE")
95-
// equivalent to setting the PYTHONWARNINGS environment variable
96-
.option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS"))
97-
// print Python exceptions directly
98-
.option("python.AlwaysRunExcepthook", "true")
99-
// Force to automatically import site.py module, to make Python packages available
100-
.option("python.ForceImportSite", "true")
101-
// The sys.executable path, a virtual path that is used by the interpreter to discover packages
102-
.option("python.Executable", vfs.vfsVenvPath() + (VirtualFileSystem.isWindows() ? "${symbol_escape}${symbol_escape}Scripts${symbol_escape}${symbol_escape}python.exe" : "/bin/python"))
103-
// Set the python home to be read from the embedded resources
104-
.option("python.PythonHome", vfs.vfsHomePath())
105-
// Do not warn if running without JIT. This can be desirable for short running scripts
106-
// to reduce memory footprint.
107-
.option("engine.WarnInterpreterOnly", "false")
108-
// Set python path to point to sources stored in src/main/resources/org.graalvm.python.vfs/proj
109-
.option("python.PythonPath", vfs.vfsProjPath())
110-
.build();
111-
return context;
54+
public static Context getContext() {
55+
return VirtualFileSystem.contextBuilder().build();
11256
}
11357

11458
public static void main(String[] args) {

graalpython/graalpy-jbang/examples/hello.java

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@
4747
//PIP termcolor
4848

4949
import org.graalvm.polyglot.Context;
50-
import org.graalvm.polyglot.PolyglotAccess;
50+
import org.graalvm.polyglot.Context.Builder;
5151
import org.graalvm.polyglot.PolyglotException;
52-
import org.graalvm.polyglot.io.IOAccess;
5352
import org.graalvm.python.embedding.vfs.VirtualFileSystem;
5453

5554
public class hello {
@@ -79,46 +78,6 @@ public static void main(String[] args) {
7978
final class VirtualGraalPyContext {
8079

8180
public static Context getContext() {
82-
VirtualFileSystem vfs = VirtualFileSystem.create();
83-
var builder = Context.newBuilder()
84-
// set true to allow experimental options
85-
.allowExperimentalOptions(false)
86-
// deny all privileges unless configured below
87-
.allowAllAccess(false)
88-
// allow access to the virtual and the host filesystem, as well as sockets
89-
.allowIO(IOAccess.newBuilder()
90-
.allowHostSocketAccess(true)
91-
.fileSystem(vfs)
92-
.build())
93-
// allow creating python threads
94-
.allowCreateThread(true)
95-
// allow running Python native extensions
96-
.allowNativeAccess(true)
97-
// allow exporting Python values to polyglot bindings and accessing Java from Python
98-
.allowPolyglotAccess(PolyglotAccess.ALL)
99-
// choose the backend for the POSIX module
100-
.option("python.PosixModuleBackend", "java")
101-
// equivalent to the Python -B flag
102-
.option("python.DontWriteBytecodeFlag", "true")
103-
// equivalent to the Python -v flag
104-
.option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false")
105-
// log level
106-
.option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE")
107-
// equivalent to setting the PYTHONWARNINGS environment variable
108-
.option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS"))
109-
// print Python exceptions directly
110-
.option("python.AlwaysRunExcepthook", "true")
111-
// Force to automatically import site.py module, to make Python packages available
112-
.option("python.ForceImportSite", "true")
113-
// The sys.executable path, a virtual path that is used by the interpreter to discover packages
114-
.option("python.Executable", vfs.vfsVenvPath() + (VirtualFileSystem.isWindows() ? "\\Scripts\\python.exe" : "/bin/python"))
115-
// Do not warn if running without JIT. This can be desirable for short running scripts
116-
// to reduce memory footprint.
117-
.option("engine.WarnInterpreterOnly", "false");
118-
if (System.getProperty("org.graalvm.nativeimage.imagecode") != null) {
119-
// Set the python home to be read from the embedded resources
120-
builder.option("python.PythonHome", vfs.vfsHomePath());
121-
}
122-
return builder.build();
81+
return VirtualFileSystem.contextBuilder().build();
12382
}
12483
}

graalpython/graalpy-jbang/templates/graalpy-template.java.qute

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
//PIP termcolor
1212
|}
1313
import org.graalvm.polyglot.Context;
14+
import org.graalvm.polyglot.Context.Builder;
1415
import org.graalvm.polyglot.PolyglotAccess;
1516
import org.graalvm.polyglot.PolyglotException;
1617
import org.graalvm.polyglot.io.IOAccess;
17-
import org.graalvm.python.embedding.utils.VirtualFileSystem;
18+
import org.graalvm.python.embedding.vfs.VirtualFileSystem;
1819

1920
public class {baseName} {
2021
public static void main(String[] args) {
@@ -42,47 +43,7 @@ public class {baseName} {
4243

4344
final class VirtualGraalPyContext {
4445
public static Context getContext() {
45-
VirtualFileSystem vfs = VirtualFileSystem.create();
46-
var builder = Context.newBuilder()
47-
// set true to allow experimental options
48-
.allowExperimentalOptions(false)
49-
// deny all privileges unless configured below
50-
.allowAllAccess(false)
51-
// allow access to the virtual and the host filesystem, as well as sockets
52-
.allowIO(IOAccess.newBuilder()
53-
.allowHostSocketAccess(true)
54-
.fileSystem(vfs)
55-
.build())
56-
// allow creating python threads
57-
.allowCreateThread(true)
58-
// allow running Python native extensions
59-
.allowNativeAccess(true)
60-
// allow exporting Python values to polyglot bindings and accessing Java from Python
61-
.allowPolyglotAccess(PolyglotAccess.ALL)
62-
// choose the backend for the POSIX module
63-
.option("python.PosixModuleBackend", "java")
64-
// equivalent to the Python -B flag
65-
.option("python.DontWriteBytecodeFlag", "true")
66-
// equivalent to the Python -v flag
67-
.option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false")
68-
// log level
69-
.option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE")
70-
// equivalent to setting the PYTHONWARNINGS environment variable
71-
.option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS"))
72-
// print Python exceptions directly
73-
.option("python.AlwaysRunExcepthook", "true")
74-
// Force to automatically import site.py module, to make Python packages available
75-
.option("python.ForceImportSite", "true")
76-
// The sys.executable path, a virtual path that is used by the interpreter to discover packages
77-
.option("python.Executable", vfs.vfsVenvPath() + (VirtualFileSystem.isWindows() ? "\\Scripts\\python.exe" : "/bin/python"))
78-
// Do not warn if running without JIT. This can be desirable for short running scripts
79-
// to reduce memory footprint.
80-
.option("engine.WarnInterpreterOnly", "false");
81-
if (System.getProperty("org.graalvm.nativeimage.imagecode") != null) {
82-
// Set the python home to be read from the embedded resources
83-
builder.option("python.PythonHome", vfs.vfsHomePath());
84-
}
85-
return builder.build();
46+
return VirtualFileSystem.contextBuilder().build();
8647
}
8748
}
8849

graalpython/graalpy-jbang/templates/graalpy-template_local_repo.java.qute

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
//PIP termcolor
1515
|}
1616
import org.graalvm.polyglot.Context;
17+
import org.graalvm.polyglot.Context.Builder;
1718
import org.graalvm.polyglot.PolyglotAccess;
1819
import org.graalvm.polyglot.PolyglotException;
1920
import org.graalvm.polyglot.io.IOAccess;
20-
import org.graalvm.python.embedding.utils.VirtualFileSystem;
21+
import org.graalvm.python.embedding.vfs.VirtualFileSystem;
2122

2223
public class {baseName} {
2324
public static void main(String[] args) {
@@ -45,47 +46,7 @@ public class {baseName} {
4546

4647
final class VirtualGraalPyContext {
4748
public static Context getContext() {
48-
VirtualFileSystem vfs = VirtualFileSystem.create();
49-
var builder = Context.newBuilder()
50-
// set true to allow experimental options
51-
.allowExperimentalOptions(false)
52-
// deny all privileges unless configured below
53-
.allowAllAccess(false)
54-
// allow access to the virtual and the host filesystem, as well as sockets
55-
.allowIO(IOAccess.newBuilder()
56-
.allowHostSocketAccess(true)
57-
.fileSystem(vfs)
58-
.build())
59-
// allow creating python threads
60-
.allowCreateThread(true)
61-
// allow running Python native extensions
62-
.allowNativeAccess(true)
63-
// allow exporting Python values to polyglot bindings and accessing Java from Python
64-
.allowPolyglotAccess(PolyglotAccess.ALL)
65-
// choose the backend for the POSIX module
66-
.option("python.PosixModuleBackend", "java")
67-
// equivalent to the Python -B flag
68-
.option("python.DontWriteBytecodeFlag", "true")
69-
// equivalent to the Python -v flag
70-
.option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false")
71-
// log level
72-
.option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE")
73-
// equivalent to setting the PYTHONWARNINGS environment variable
74-
.option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS"))
75-
// print Python exceptions directly
76-
.option("python.AlwaysRunExcepthook", "true")
77-
// Force to automatically import site.py module, to make Python packages available
78-
.option("python.ForceImportSite", "true")
79-
// The sys.executable path, a virtual path that is used by the interpreter to discover packages
80-
.option("python.Executable", vfs.vfsVenvPath() + (VirtualFileSystem.isWindows() ? "\\Scripts\\python.exe" : "/bin/python"))
81-
// Do not warn if running without JIT. This can be desirable for short running scripts
82-
// to reduce memory footprint.
83-
.option("engine.WarnInterpreterOnly", "false");
84-
if (System.getProperty("org.graalvm.nativeimage.imagecode") != null) {
85-
// Set the python home to be read from the embedded resources
86-
builder.option("python.PythonHome", vfs.vfsHomePath());
87-
}
88-
return builder.build();
49+
return VirtualFileSystem.contextBuilder().build();
8950
}
9051
}
9152

graalpython/graalpy-micronaut-embedding/src/main/java/org/graalvm/python/embedding/micronaut/GraalPyContextFactory.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@
4343
import io.micronaut.context.annotation.Factory;
4444
import jakarta.annotation.PreDestroy;
4545
import jakarta.inject.Singleton;
46-
import org.graalvm.polyglot.*;
47-
import org.graalvm.polyglot.io.IOAccess;
46+
import org.graalvm.polyglot.Context;
4847
import org.graalvm.python.embedding.vfs.VirtualFileSystem;
49-
import org.graalvm.polyglot.HostAccess;
5048

5149
import java.io.IOException;
5250
import java.time.Duration;
@@ -60,30 +58,7 @@ public final class GraalPyContextFactory {
6058

6159
@Singleton
6260
Context createContext() {
63-
VirtualFileSystem vfs = VirtualFileSystem.create();
64-
context = Context.newBuilder()
65-
.allowExperimentalOptions(false)
66-
.allowAllAccess(false)
67-
.allowHostAccess(HostAccess.ALL)
68-
.allowIO(IOAccess.newBuilder()
69-
.allowHostSocketAccess(true)
70-
.fileSystem(vfs)
71-
.build())
72-
.allowCreateThread(true)
73-
.allowNativeAccess(true)
74-
.allowPolyglotAccess(PolyglotAccess.ALL)
75-
.option("python.PosixModuleBackend", "java")
76-
.option("python.DontWriteBytecodeFlag", "true")
77-
.option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false")
78-
.option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE")
79-
.option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS"))
80-
.option("python.AlwaysRunExcepthook", "true")
81-
.option("python.ForceImportSite", "true")
82-
.option("python.Executable", vfs.vfsVenvPath() + (VirtualFileSystem.isWindows() ? "\\Scripts\\python.exe" : "/bin/python"))
83-
.option("python.PythonHome", vfs.vfsHomePath())
84-
.option("engine.WarnInterpreterOnly", "false")
85-
.option("python.PythonPath", vfs.vfsProjPath())
86-
.build();
61+
context = VirtualFileSystem.contextBuilder().build();
8762
context.initialize("python");
8863
return context;
8964
}

graalpython/lib-graalpython/modules/standalone/resources/Py2BinLauncher.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@
4343

4444
import org.graalvm.nativeimage.ImageInfo;
4545
import org.graalvm.nativeimage.ProcessProperties;
46-
import org.graalvm.polyglot.Context;
4746
import org.graalvm.polyglot.PolyglotException;
4847
import org.graalvm.polyglot.Source;
49-
import org.graalvm.polyglot.io.IOAccess;
48+
import org.graalvm.polyglot.Context.Builder;
5049
import org.graalvm.python.embedding.vfs.VirtualFileSystem;
5150

5251
/**
@@ -62,31 +61,13 @@
6261
*/
6362
public class Py2BinLauncher {
6463

65-
public static void main(String[] args) throws IOException {
66-
VirtualFileSystem vfs = VirtualFileSystem.newBuilder()
67-
.extractFilter(p -> {
68-
String s = p.toString();
69-
return s.endsWith(".ttf");
70-
})
71-
.build();
72-
IOAccess ioAccess = IOAccess.newBuilder().fileSystem(vfs).allowHostSocketAccess(true).build();
73-
var builder = Context.newBuilder()
74-
.allowExperimentalOptions(true)
75-
.allowAllAccess(true)
76-
.allowIO(ioAccess)
77-
.arguments("python", Stream.concat(Stream.of(getProgramName()), Stream.of(args)).toArray(String[]::new))
78-
.option("python.PosixModuleBackend", "java")
79-
.option("python.DontWriteBytecodeFlag", "true")
80-
.option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false")
81-
.option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE")
82-
.option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS"))
83-
.option("python.AlwaysRunExcepthook", "true")
84-
.option("python.ForceImportSite", "true")
85-
.option("python.RunViaLauncher", "true")
86-
.option("python.Executable", vfs.vfsVenvPath() + (VirtualFileSystem.isWindows() ? "\\Scripts\\python.cmd" : "/bin/python"))
87-
.option("python.InputFilePath", vfs.vfsProjPath())
88-
.option("python.PythonHome", vfs.vfsHomePath())
89-
.option("python.CheckHashPycsMode", "never");
64+
public static void main(String[] args) throws IOException {
65+
VirtualFileSystem vfs = VirtualFileSystem.create();
66+
Builder builder = VirtualFileSystem.contextBuilder()
67+
.allowExperimentalOptions(true)
68+
.allowAllAccess(true)
69+
.arguments("python", Stream.concat(Stream.of(getProgramName()), Stream.of(args)).toArray(String[]::new))
70+
.option("python.RunViaLauncher", "true");
9071
if(ImageInfo.inImageRuntimeCode()) {
9172
builder.option("engine.WarnInterpreterOnly", "false");
9273
}
@@ -102,7 +83,7 @@ public static void main(String[] args) throws IOException {
10283
}
10384
}
10485
}
105-
}
86+
}
10687

10788
private static String getProgramName() {
10889
if (ImageInfo.inImageRuntimeCode()) {

0 commit comments

Comments
 (0)