Skip to content

Commit 4b1a4b3

Browse files
timfelsteve-s
authored andcommitted
extend resources junit test to check we can use resources in embedding with IO disallowed
1 parent af3045c commit 4b1a4b3

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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/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) //

0 commit comments

Comments
 (0)