Skip to content

Commit 4dbd6f4

Browse files
committed
Warn loudly and report native access as denied for PythonContext on unsupported platforms
1 parent 73c1f3c commit 4dbd6f4

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
import com.oracle.graal.python.builtins.Python3Core;
6161
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
62+
import com.oracle.graal.python.builtins.PythonOS;
6263
import com.oracle.graal.python.builtins.modules.MarshalModuleBuiltins;
6364
import com.oracle.graal.python.builtins.modules.SignalModuleBuiltins;
6465
import com.oracle.graal.python.builtins.objects.PNone;
@@ -509,6 +510,13 @@ protected void initializeContext(PythonContext context) {
509510
}
510511
initializeLanguage();
511512
}
513+
if (PythonOS.isUnsupported() && context.getEnv().isNativeAccessAllowed()) {
514+
LOGGER.log(Level.WARNING, "Loading native libraries into GraalPy on unsupported platforms. " +
515+
"You can ensure that native access is disallowed for this context and configure GraalPy to use Java backends where possible. " +
516+
"Refer to https://www.graalvm.org/python/docs/ for more information on native and Java module backends. " +
517+
"This is not fatal, because other languages may allow native libraries to run on this platform in the same context, " +
518+
"but attempting to load a native library on GraalPy will fail.");
519+
}
512520
context.initializeHomeAndPrefixPaths(context.getEnv(), getLanguageHome());
513521
context.initialize();
514522
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonOS.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static PythonOS getPythonOS() {
114114
"If you are running on a different platform and accept that any functionality that interacts with the system may be " +
115115
"incorrect and Python native extensions will not work, you can specify the system property \"UnsupportedPlatformEmulates\" " +
116116
"with a value of either \"linux\", \"macos\", or \"windows\" to continue further and have GraalPy behave as if it were running on " +
117-
"the OS specified. Loading native libraries will not work and should be disabled using the context options. " +
117+
"the OS specified. Loading native libraries will not work and must be disabled using the context options. " +
118118
"See https://www.graalvm.org/python/docs/ for details on GraalPy modules with both native and Java backends, " +
119119
"and https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/Context.Builder.html to learn about disallowing native access.");
120120
}
@@ -123,12 +123,16 @@ public static PythonOS getPythonOS() {
123123
}
124124

125125
public static void throwIfUnsupported(String msg) {
126-
if (current == PLATFORM_ANY) {
126+
if (isUnsupported()) {
127127
throw new UnsupportedPlatform(msg +
128128
"\nThis point was reached as earlier platform checks were overridden using the system property UnsupportedPlatformEmulates");
129129
}
130130
}
131131

132+
public static boolean isUnsupported() {
133+
return current == PLATFORM_ANY;
134+
}
135+
132136
public static final class UnsupportedPlatform extends AbstractTruffleException {
133137
public UnsupportedPlatform(String msg) {
134138
super(msg);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ public PythonContext(PythonLanguage language, TruffleLanguage.Env env) {
12561256
this.in = env.in();
12571257
this.out = env.out();
12581258
this.err = env.err();
1259-
this.nativeAccessAllowed = env.isNativeAccessAllowed();
1259+
this.nativeAccessAllowed = env.isNativeAccessAllowed() && !PythonOS.isUnsupported();
12601260
}
12611261

12621262
private static final ContextReference<PythonContext> REFERENCE = ContextReference.create(PythonLanguage.class);
@@ -1446,7 +1446,7 @@ public void setEnv(TruffleLanguage.Env newEnv) {
14461446
err = env.err();
14471447
posixSupport.setEnv(env);
14481448
optionValues = PythonOptions.createOptionValuesStorage(newEnv);
1449-
nativeAccessAllowed = newEnv.isNativeAccessAllowed();
1449+
nativeAccessAllowed = newEnv.isNativeAccessAllowed() && !PythonOS.isUnsupported();
14501450
}
14511451

14521452
/**

0 commit comments

Comments
 (0)