Skip to content

Commit a0f5d2b

Browse files
committed
added graalpy context builder test
1 parent 5dcef39 commit a0f5d2b

File tree

2 files changed

+51
-62
lines changed
  • graalpython
    • com.oracle.graal.python.test.integration/src/org/graalvm/python/embedding/utils/test
    • com.oracle.graal.python.test/src/tests/standalone/micronaut/hello/src/main/java/example/micronaut

2 files changed

+51
-62
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
@@ -396,13 +387,16 @@ private void eval(String s) {
396387
}
397388

398389
private void eval(String s, Function<VirtualFileSystem.Builder, VirtualFileSystem.Builder> builderFunction) {
399-
String src;
390+
String src = patchMountPoint(s);
391+
392+
getContext(builderFunction).eval(PYTHON, src);
393+
}
394+
395+
private static String patchMountPoint(String src) {
400396
if (IS_WINDOWS) {
401-
src = s.replace(VFS_UNIX_MOUNT_POINT, "X:\\\\test_win_mount_point");
402-
} else {
403-
src = s;
397+
return src.replace(VFS_UNIX_MOUNT_POINT, "X:\\\\test_win_mount_point");
404398
}
405-
getContext(builderFunction).eval(PYTHON, src);
399+
return src;
406400
}
407401

408402
private Context cachedContext;
@@ -419,28 +413,46 @@ public Context getContext(Function<VirtualFileSystem.Builder, VirtualFileSystem.
419413
builder = builderFunction.apply(builder);
420414
}
421415
VirtualFileSystem vfs = builder.build();
422-
Context context = Context.newBuilder().//
423-
allowExperimentalOptions(false).//
424-
allowAllAccess(false).//
425-
allowHostAccess(HostAccess.ALL).//
426-
allowIO(IOAccess.newBuilder().//
427-
allowHostSocketAccess(true).//
428-
fileSystem(vfs).//
429-
build()).//
430-
allowCreateThread(true).//
431-
allowNativeAccess(true).//
432-
allowPolyglotAccess(PolyglotAccess.ALL).//
433-
option("python.PosixModuleBackend", "java").//
434-
option("python.DontWriteBytecodeFlag", "true").//
435-
option("python.VerboseFlag", System.getenv("PYTHONVERBOSE") != null ? "true" : "false").//
436-
option("log.python.level", System.getenv("PYTHONVERBOSE") != null ? "FINE" : "SEVERE").//
437-
option("python.WarnOptions", System.getenv("PYTHONWARNINGS") == null ? "" : System.getenv("PYTHONWARNINGS")).//
438-
option("python.AlwaysRunExcepthook", "true").//
439-
option("python.ForceImportSite", "true").//
440-
option("engine.WarnInterpreterOnly", "false").build();
416+
Context context = VirtualFileSystem.contextBuilder(vfs).build();
441417
if (builderFunction == null) {
442418
cachedContext = context;
443419
}
444420
return context;
445421
}
422+
423+
@Test
424+
public void builderTest() {
425+
Context context = VirtualFileSystem.contextBuilder().allowAllAccess(true).allowHostAccess(HostAccess.ALL).build();
426+
context.eval(PYTHON, "import java; java.type('java.lang.String')");
427+
428+
context = VirtualFileSystem.contextBuilder().allowAllAccess(false).allowHostAccess(HostAccess.NONE).build();
429+
context.eval(PYTHON, """
430+
import java
431+
try:
432+
java.type('java.lang.String');
433+
except NotImplementedError:
434+
pass
435+
else:
436+
assert False, 'expected NotImplementedError'
437+
""");
438+
439+
VirtualFileSystem vfs = VirtualFileSystem.newBuilder().//
440+
unixMountPoint(VFS_MOUNT_POINT).//
441+
windowsMountPoint(VFS_WIN_MOUNT_POINT).//
442+
resourceLoadingClass(VirtualFileSystemTest.class).build();
443+
context = VirtualFileSystem.contextBuilder(vfs).build();
444+
context.eval(PYTHON, patchMountPoint("from os import listdir; listdir('/test_mount_point')"));
445+
446+
context = VirtualFileSystem.contextBuilder().build();
447+
context.eval(PYTHON, "from os import listdir; listdir('.')");
448+
449+
context = VirtualFileSystem.contextBuilder().allowIO(IOAccess.NONE).build();
450+
boolean gotPE = false;
451+
try {
452+
context.eval(PYTHON, "from os import listdir; listdir('.')");
453+
} catch (PolyglotException pe) {
454+
gotPE = true;
455+
}
456+
assert gotPE : "expected PolyglotException";
457+
}
446458
}

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

0 commit comments

Comments
 (0)