Skip to content

Commit 79efed6

Browse files
committed
[GR-47994] Fix creating datetime objects when platform access is denied.
2 parents f8d7a58 + a9c9a87 commit 79efed6

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advance/ExclusionsTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, 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
@@ -41,11 +41,26 @@
4141
package com.oracle.graal.python.test.advance;
4242

4343
import org.graalvm.polyglot.Context;
44+
import org.junit.Test;
4445

4546
public class ExclusionsTest {
4647
public static void main(String[] args) {
4748
try (Context context = Context.create()) {
4849
context.eval("python", "print('Hello Python!');");
4950
}
5051
}
52+
53+
@Test
54+
public void testDatetimeWithoutPlatformAccess() {
55+
if (!"true".equals(System.getProperty("python.WithoutPlatformAccess"))) {
56+
return;
57+
}
58+
var builder = Context.newBuilder().allowExperimentalOptions(true);
59+
if (System.getenv("GRAAL_PYTHONHOME") != null) {
60+
builder.option("python.PythonHome", System.getenv("GRAAL_PYTHONHOME"));
61+
}
62+
try (Context context = builder.build()) {
63+
context.eval("python", "import datetime; datetime.datetime.strptime('2014 7 2 6 14 0 742 +0700', '%Y %m %d %H %M %S %f %z')");
64+
}
65+
}
5166
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/advance/ResourcesTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
import static org.junit.Assert.assertTrue;
4444

4545
import java.io.File;
46+
4647
import org.graalvm.polyglot.Context;
48+
import org.graalvm.polyglot.io.IOAccess;
4749
import org.junit.Test;
4850

4951
public class ResourcesTest {
@@ -59,4 +61,12 @@ public void testResourcesAsHome() {
5961
assertTrue(foundHome, !foundHome.contains("graalpython"));
6062
}
6163
}
64+
65+
@Test
66+
public void testResourcesAlwaysAllowReading() {
67+
try (Context context = Context.newBuilder("python").allowIO(IOAccess.NONE).option("python.PythonHome", "/path/that/does/not/exist").build()) {
68+
String foundHome = context.eval("python", "import email; email.__spec__.origin").asString();
69+
assertTrue(foundHome, foundHome.contains("python" + File.separator + "python-home"));
70+
}
71+
}
6272
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TimeModuleBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Object tzset() {
325325
}
326326
TimeZone.setDefault(TimeZone.getTimeZone(tzEnv));
327327
} else {
328-
PRaiseNode.raiseUncached(this, PythonBuiltinClassType.SystemError, SET_TIMEZONE_ERROR);
328+
PRaiseNode.raiseUncached(this, PythonBuiltinClassType.AttributeError, SET_TIMEZONE_ERROR);
329329
}
330330
return PNone.NONE;
331331
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,7 @@ private void initializePosixSupport() {
16651665
}
16661666

16671667
private TruffleString langHome, sysPrefix, basePrefix, coreHome, capiHome, jniHome, stdLibHome;
1668+
private TruffleFile homeResourcesFile;
16681669

16691670
public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
16701671
if (ImageInfo.inImageBuildtimeCode()) {
@@ -1704,7 +1705,8 @@ public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
17041705
() -> {
17051706
if (PythonLanguage.PYTHON_RESOURCE_CLASS != null && !ImageInfo.inImageCode()) {
17061707
try {
1707-
return newEnv.getInternalResource(PythonLanguage.PYTHON_RESOURCE_CLASS).getAbsoluteFile();
1708+
homeResourcesFile = newEnv.getInternalResource(PythonLanguage.PYTHON_RESOURCE_CLASS).getAbsoluteFile();
1709+
return homeResourcesFile;
17081710
} catch (IOException e) {
17091711
// fall through
17101712
}
@@ -2274,6 +2276,15 @@ void releaseGil() {
22742276
*/
22752277
@TruffleBoundary
22762278
public TruffleFile getPublicTruffleFileRelaxed(TruffleString path, TruffleString... allowedSuffixes) {
2279+
if (homeResourcesFile != null && !env.isFileIOAllowed()) {
2280+
// XXX: Workaround for Truffle resources not being considered internal truffle files
2281+
String jlPath = path.toJavaStringUncached();
2282+
String jlHome = langHome.toJavaStringUncached();
2283+
if (jlPath.startsWith(jlHome)) {
2284+
String homeRelativePath = jlPath.substring(jlHome.length() + 1);
2285+
return homeResourcesFile.resolve(homeRelativePath);
2286+
}
2287+
}
22772288
TruffleFile f = env.getInternalTruffleFile(path.toJavaStringUncached());
22782289
// 'isDirectory' does deliberately not follow symlinks because otherwise this could allow to
22792290
// escape the language home directory.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private PythonOptions() {
139139
}
140140

141141
@Option(category = OptionCategory.EXPERT, help = "Set the home of Python. Equivalent of GRAAL_PYTHONHOME env variable. " +
142-
"Determines default values for the CoreHome, StdLibHome, SysBasePrefix, SysPrefix.", usageSyntax = "<path>", stability = OptionStability.EXPERIMENTAL) //
142+
"Determines default values for the CoreHome, StdLibHome, SysBasePrefix, SysPrefix.", usageSyntax = "<path>", stability = OptionStability.STABLE) //
143143
public static final OptionKey<String> PythonHome = new OptionKey<>("");
144144

145145
@Option(category = OptionCategory.USER, help = "Set the location of sys.prefix. Overrides any environment variables or Java options.", usageSyntax = "<path>", stability = OptionStability.STABLE) //

mx.graalpython/mx_graalpython.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,14 @@ def graalpython_gate_runner(args, tasks):
14121412
)
14131413
else:
14141414
punittest(['--verbose'], report=report())
1415+
# Run tests with static exclusion paths
1416+
jdk = mx.get_jdk()
1417+
prev = jdk.java_args_pfx
1418+
try:
1419+
jdk.java_args_pfx = (mx._opts.java_args or []) + ['-Dpython.WithoutPlatformAccess=true']
1420+
punittest(['--verbose', '--no-leak-tests', '--regex', 'com.oracle.graal.python.test.advance.ExclusionsTest'])
1421+
finally:
1422+
jdk.java_args_pfx = prev
14151423

14161424
# Unittests on JVM
14171425
with Task('GraalPython Python unittests', tasks, tags=[GraalPythonTags.unittest]) as task:

0 commit comments

Comments
 (0)