Skip to content

Commit dbb1568

Browse files
committed
Allow IsolateNativeModules for core modules without a venv
1 parent f7cd4c8 commit dbb1568

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

graalpython/com.oracle.graal.python.test/src/org/graalvm/python/embedding/cext/test/MultiContextCExtTest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,9 @@ public void testCreatingVenvForMulticontext() throws IOException {
243243
} catch (PolyglotException e) {
244244
assertTrue("We rely on sys.prefix", e.getMessage().contains("sys.prefix must be a str"));
245245
}
246-
// Fifth one does not work because we don't have the venv configured
247-
try {
248-
c5.eval(code);
249-
fail("should not reach here");
250-
} catch (PolyglotException e) {
251-
assertTrue("We need a venv", e.getMessage().contains("sys.prefix must point to a venv"));
252-
}
253-
// Using a context without isolation in the same process needs to use LLVM
254-
assertFalse("have not had a context use LLVM, yet", log.truffleLog.toString().contains("as bitcode"));
246+
// Fifth works even without a venv
247+
c5.eval(code);
248+
// Using a context without isolation in the same process does not work
255249
try {
256250
c0.eval(code);
257251
fail("should not reach here");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/NativeLibraryLocator.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -189,11 +189,31 @@ private static String resolve(PythonContext context, TruffleFile original, int c
189189
String newName = copyNameOf(original.getName(), capiSlot);
190190

191191
if (original.getAbsoluteFile().startsWith(context.getCoreHome().toJavaStringUncached())) {
192-
// must be relocated to venv
192+
// must be relocated to venv or tmpdir
193193
Object sysPrefix = context.getSysModule().getAttribute(T_PREFIX);
194194
Object sysBasePrefix = context.getSysModule().getAttribute(T_BASE_PREFIX);
195195
if (sysPrefix.equals(sysBasePrefix)) {
196-
throw new ApiInitException(ErrorMessages.SYS_PREFIX_MUST_POINT_TO_A_VENV_FOR_CAPI_ISOLATION);
196+
TruffleFile tempDir;
197+
try {
198+
tempDir = context.getEnv().createTempDirectory(null, null);
199+
} catch (IOException e) {
200+
throw new ApiInitException(ErrorMessages.SYS_PREFIX_MUST_POINT_TO_A_VENV_FOR_CAPI_ISOLATION);
201+
}
202+
context.registerAtexitHook((ctx) -> {
203+
try {
204+
tempDir.delete();
205+
} catch (IOException e) {
206+
// Nothing we can do at this point
207+
}
208+
});
209+
copy = tempDir.resolve(newName);
210+
context.registerAtexitHook((ctx) -> {
211+
try {
212+
copy.delete();
213+
} catch (IOException e) {
214+
// Nothing we can do at this point
215+
}
216+
});
197217
} else {
198218
try {
199219
var tsSysPrefix = CastToTruffleStringNode.executeUncached(sysPrefix);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,6 @@ public abstract class ErrorMessages {
16871687
"The sys.prefix must be a str, not '%p' when the `IsolateNativeModules' option is used, because it is the base path for searching the relocated C API. " +
16881688
"Refer to https://www.graalvm.org/latest/reference-manual/python/Native-Extensions for details on native module isolation.");
16891689
public static final TruffleString SYS_PREFIX_MUST_POINT_TO_A_VENV_FOR_CAPI_ISOLATION = tsLiteral(
1690-
"The sys.prefix must point to a venv, not be identical to sys.base_prefix when the `IsolateNativeModules' option is used, because it is the base path for searching and creating the relocated C API and extension modules. " +
1690+
"The sys.prefix must point to a writable venv folder or the system default temporary directory needs to be writable when the `IsolateNativeModules' option is used, because it is the base path for searching and creating the relocated C API and extension modules. " +
16911691
"Refer to https://www.graalvm.org/latest/reference-manual/python/Native-Extensions for details on native module isolation.");
16921692
}

0 commit comments

Comments
 (0)