Skip to content

Commit fa55a68

Browse files
committed
[GR-52931] Provide api for a precofigured context builder for polyglot apps.
PullRequest: graalpython/3327
2 parents 353da45 + a0f5d2b commit fa55a68

File tree

12 files changed

+156
-314
lines changed

12 files changed

+156
-314
lines changed

graalpython/com.oracle.graal.python.test.integration/src/org/graalvm/python/embedding/utils/test/VirtualFileSystemTest.java

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,27 @@
5454

5555
import org.graalvm.polyglot.Context;
5656
import org.graalvm.polyglot.HostAccess;
57-
import org.graalvm.polyglot.PolyglotAccess;
57+
import org.graalvm.polyglot.PolyglotException;
5858
import org.graalvm.polyglot.io.IOAccess;
5959
import org.graalvm.python.embedding.vfs.VirtualFileSystem;
6060
import org.junit.Test;
6161

6262
public class VirtualFileSystemTest {
6363

64-
private static final String VFS_UNIX_MOUNT_POINT = "/test_mount_point";
65-
private static final String VFS_WIN_MOUNT_POINT = "X:\\test_win_mount_point";
66-
private static final String VFS_MOUNT_POINT = IS_WINDOWS ? VFS_WIN_MOUNT_POINT : VFS_UNIX_MOUNT_POINT;
64+
static final String VFS_UNIX_MOUNT_POINT = "/test_mount_point";
65+
static final String VFS_WIN_MOUNT_POINT = "X:\\test_win_mount_point";
66+
static final String VFS_MOUNT_POINT = IS_WINDOWS ? VFS_WIN_MOUNT_POINT : VFS_UNIX_MOUNT_POINT;
6767

68-
private static final String PYTHON = "python";
68+
static final String PYTHON = "python";
6969

7070
@Test
7171
public void defaultValues() throws Exception {
7272
VirtualFileSystem vfs = VirtualFileSystem.create();
7373
VirtualFileSystem vfs2 = VirtualFileSystem.newBuilder().build();
7474

7575
assertEquals(vfs.getMountPoint(), vfs2.getMountPoint());
76-
assertEquals(vfs.vfsHomePath(), vfs2.vfsHomePath());
77-
assertEquals(vfs.vfsProjPath(), vfs2.vfsProjPath());
78-
assertEquals(vfs.vfsVenvPath(), vfs2.vfsVenvPath());
7976

8077
assertEquals(IS_WINDOWS ? "X:\\graalpy_vfs" : "/graalpy_vfs", vfs.getMountPoint());
81-
assertEquals(IS_WINDOWS ? "X:\\graalpy_vfs\\home" : "/graalpy_vfs/home", vfs.vfsHomePath());
82-
assertEquals(IS_WINDOWS ? "X:\\graalpy_vfs\\proj" : "/graalpy_vfs/proj", vfs.vfsProjPath());
83-
assertEquals(IS_WINDOWS ? "X:\\graalpy_vfs\\venv" : "/graalpy_vfs/venv", vfs.vfsVenvPath());
8478
}
8579

8680
@Test
@@ -90,9 +84,6 @@ public void mountPoints() throws Exception {
9084
windowsMountPoint(VFS_WIN_MOUNT_POINT).build();
9185

9286
assertEquals(VFS_MOUNT_POINT, vfs.getMountPoint());
93-
assertEquals(IS_WINDOWS ? VFS_WIN_MOUNT_POINT + "\\home" : VFS_UNIX_MOUNT_POINT + "/home", vfs.vfsHomePath());
94-
assertEquals(IS_WINDOWS ? VFS_WIN_MOUNT_POINT + "\\proj" : VFS_UNIX_MOUNT_POINT + "/proj", vfs.vfsProjPath());
95-
assertEquals(IS_WINDOWS ? VFS_WIN_MOUNT_POINT + "\\venv" : VFS_UNIX_MOUNT_POINT + "/venv", vfs.vfsVenvPath());
9687
}
9788

9889
@Test
@@ -421,13 +412,16 @@ private void eval(String s) {
421412
}
422413

423414
private void eval(String s, Function<VirtualFileSystem.Builder, VirtualFileSystem.Builder> builderFunction) {
424-
String src;
415+
String src = patchMountPoint(s);
416+
417+
getContext(builderFunction).eval(PYTHON, src);
418+
}
419+
420+
private static String patchMountPoint(String src) {
425421
if (IS_WINDOWS) {
426-
src = s.replace(VFS_UNIX_MOUNT_POINT, "X:\\\\test_win_mount_point");
427-
} else {
428-
src = s;
422+
return src.replace(VFS_UNIX_MOUNT_POINT, "X:\\\\test_win_mount_point");
429423
}
430-
getContext(builderFunction).eval(PYTHON, src);
424+
return src;
431425
}
432426

433427
private Context cachedContext;
@@ -444,28 +438,46 @@ public Context getContext(Function<VirtualFileSystem.Builder, VirtualFileSystem.
444438
builder = builderFunction.apply(builder);
445439
}
446440
VirtualFileSystem vfs = builder.build();
447-
Context context = Context.newBuilder().//
448-
allowExperimentalOptions(false).//
449-
allowAllAccess(false).//
450-
allowHostAccess(HostAccess.ALL).//
451-
allowIO(IOAccess.newBuilder().//
452-
allowHostSocketAccess(true).//
453-
fileSystem(vfs).//
454-
build()).//
455-
allowCreateThread(true).//
456-
allowNativeAccess(true).//
457-
allowPolyglotAccess(PolyglotAccess.ALL).//
458-
option("python.PosixModuleBackend", "java").//
459-
option("python.DontWriteBytecodeFlag", "true").//
460-
option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false").//
461-
option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE").//
462-
option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS")).//
463-
option("python.AlwaysRunExcepthook", "true").//
464-
option("python.ForceImportSite", "true").//
465-
option("engine.WarnInterpreterOnly", "false").build();
441+
Context context = VirtualFileSystem.contextBuilder(vfs).build();
466442
if (builderFunction == null) {
467443
cachedContext = context;
468444
}
469445
return context;
470446
}
447+
448+
@Test
449+
public void builderTest() {
450+
Context context = VirtualFileSystem.contextBuilder().allowAllAccess(true).allowHostAccess(HostAccess.ALL).build();
451+
context.eval(PYTHON, "import java; java.type('java.lang.String')");
452+
453+
context = VirtualFileSystem.contextBuilder().allowAllAccess(false).allowHostAccess(HostAccess.NONE).build();
454+
context.eval(PYTHON, """
455+
import java
456+
try:
457+
java.type('java.lang.String');
458+
except NotImplementedError:
459+
pass
460+
else:
461+
assert False, 'expected NotImplementedError'
462+
""");
463+
464+
VirtualFileSystem vfs = VirtualFileSystem.newBuilder().//
465+
unixMountPoint(VFS_MOUNT_POINT).//
466+
windowsMountPoint(VFS_WIN_MOUNT_POINT).//
467+
resourceLoadingClass(VirtualFileSystemTest.class).build();
468+
context = VirtualFileSystem.contextBuilder(vfs).build();
469+
context.eval(PYTHON, patchMountPoint("from os import listdir; listdir('/test_mount_point')"));
470+
471+
context = VirtualFileSystem.contextBuilder().build();
472+
context.eval(PYTHON, "from os import listdir; listdir('.')");
473+
474+
context = VirtualFileSystem.contextBuilder().allowIO(IOAccess.NONE).build();
475+
boolean gotPE = false;
476+
try {
477+
context.eval(PYTHON, "from os import listdir; listdir('.')");
478+
} catch (PolyglotException pe) {
479+
gotPE = true;
480+
}
481+
assert gotPE : "expected PolyglotException";
482+
}
471483
}

graalpython/com.oracle.graal.python.test/src/tests/standalone/micronaut/hello/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
<parent>
1111
<groupId>io.micronaut.platform</groupId>
1212
<artifactId>micronaut-parent</artifactId>
13-
<version>4.0.5</version>
13+
<version>4.4.2</version>
1414
</parent>
1515
<properties>
1616
<packaging>jar</packaging>
1717
<jdk.version>17</jdk.version>
1818
<release.version>17</release.version>
19-
<micronaut.version>4.0.5</micronaut.version>
20-
<micronaut.runtime>netty</micronaut.runtime>
19+
<micronaut.version>4.4.2</micronaut.version>
2120
<micronaut.aot.enabled>false</micronaut.aot.enabled>
2221
<micronaut.aot.packageName>example.micronaut.aot.generated</micronaut.aot.packageName>
22+
<micronaut.runtime>netty</micronaut.runtime>
2323
<exec.mainClass>example.micronaut.Application</exec.mainClass>
2424

2525
<!-- graalpy -->
@@ -100,6 +100,11 @@
100100
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
101101

102102
<annotationProcessorPaths combine.children="append">
103+
<path>
104+
<groupId>io.micronaut</groupId>
105+
<artifactId>micronaut-http-validation</artifactId>
106+
<version>${micronaut.core.version}</version>
107+
</path>
103108
<path>
104109
<groupId>io.micronaut.serde</groupId>
105110
<artifactId>micronaut-serde-processor</artifactId>

graalpython/com.oracle.graal.python.test/src/tests/standalone/micronaut/hello/src/main/java/example/micronaut/ContextFactory.j

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.graalvm.polyglot.HostAccess;
1010
import org.graalvm.polyglot.PolyglotAccess;
1111
import org.graalvm.polyglot.io.IOAccess;
1212
import org.graalvm.python.embedding.micronaut.GraalPyContextFactory;
13-
import org.graalvm.python.embedding.utils.VirtualFileSystem;
13+
import org.graalvm.python.embedding.vfs.VirtualFileSystem;
1414

1515
import java.io.IOException;
1616
import java.time.Duration;
@@ -25,30 +25,7 @@ class ContextFactory {
2525
@Singleton
2626
@Replaces(value = Context.class, factory = GraalPyContextFactory.class)
2727
public Context createContext() {
28-
VirtualFileSystem vfs = VirtualFileSystem.create();
29-
context = Context.newBuilder()
30-
.allowExperimentalOptions(false)
31-
.allowAllAccess(false)
32-
.allowHostAccess(HostAccess.ALL)
33-
.allowIO(IOAccess.newBuilder()
34-
.allowHostSocketAccess(true)
35-
.fileSystem(vfs)
36-
.build())
37-
.allowCreateThread(true)
38-
.allowNativeAccess(true)
39-
.allowPolyglotAccess(PolyglotAccess.ALL)
40-
.option("python.PosixModuleBackend", "java")
41-
.option("python.DontWriteBytecodeFlag", "true")
42-
.option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false")
43-
.option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE")
44-
.option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS"))
45-
.option("python.AlwaysRunExcepthook", "true")
46-
.option("python.ForceImportSite", "true")
47-
.option("python.Executable", vfs.vfsVenvPath() + (VirtualFileSystem.isWindows() ? "\\Scripts\\python.exe" : "/bin/python"))
48-
.option("python.PythonHome", vfs.vfsHomePath())
49-
.option("engine.WarnInterpreterOnly", "false")
50-
.option("python.PythonPath", vfs.vfsProjPath())
51-
.build();
28+
context = VirtualFileSystem.contextBuilder().build();
5229
context.initialize("python");
5330
System.out.println("=== CREATED REPLACE CONTEXT ===");
5431
return context;

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
}

0 commit comments

Comments
 (0)