Skip to content

Commit a046ed6

Browse files
eregontimfel
authored andcommitted
Store the paths in PythonContext rather than using OptionValues#set()
* Centralizes the paths logic in one place.
1 parent 0629b84 commit a046ed6

File tree

7 files changed

+157
-174
lines changed

7 files changed

+157
-174
lines changed

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

Lines changed: 4 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package com.oracle.graal.python;
2727

2828
import java.io.IOException;
29-
import java.text.MessageFormat;
3029
import java.util.ArrayList;
3130
import java.util.concurrent.ConcurrentHashMap;
3231
import java.util.function.Supplier;
@@ -179,7 +178,7 @@ protected boolean patchContext(PythonContext context, Env newEnv) {
179178
PythonCore.writeInfo("Cannot use preinitialized context.");
180179
return false;
181180
}
182-
ensureHomeInOptions(newEnv);
181+
context.initializeHomeAndPrefixPaths(newEnv, getLanguageHome());
183182
PythonCore.writeInfo("Using preinitialized context.");
184183
context.patch(newEnv);
185184
return true;
@@ -189,90 +188,10 @@ protected boolean patchContext(PythonContext context, Env newEnv) {
189188
protected PythonContext createContext(Env env) {
190189
assert this.isWithThread == null || this.isWithThread == PythonOptions.isWithThread(env) : "conflicting thread options in the same language!";
191190
this.isWithThread = PythonOptions.isWithThread(env);
192-
ensureHomeInOptions(env);
193191
Python3Core newCore = new Python3Core(new PythonParserImpl());
194-
return new PythonContext(this, env, newCore);
195-
}
196-
197-
private void ensureHomeInOptions(Env env) {
198-
String languageHome = getLanguageHome();
199-
String sysPrefix = env.getOptions().get(PythonOptions.SysPrefix);
200-
String basePrefix = env.getOptions().get(PythonOptions.SysBasePrefix);
201-
String coreHome = env.getOptions().get(PythonOptions.CoreHome);
202-
String stdLibHome = env.getOptions().get(PythonOptions.StdLibHome);
203-
204-
PythonCore.writeInfo((MessageFormat.format("Initial locations:" +
205-
"\n\tLanguage home: {0}" +
206-
"\n\tSysPrefix: {1}" +
207-
"\n\tBaseSysPrefix: {2}" +
208-
"\n\tCoreHome: {3}" +
209-
"\n\tStdLibHome: {4}", languageHome, sysPrefix, basePrefix, coreHome, stdLibHome)));
210-
211-
TruffleFile home = null;
212-
if (languageHome != null) {
213-
home = env.getTruffleFile(languageHome);
214-
}
215-
216-
try {
217-
String envHome = System.getenv("GRAAL_PYTHONHOME");
218-
if (envHome != null) {
219-
TruffleFile envHomeFile = env.getTruffleFile(envHome);
220-
if (envHomeFile.isDirectory()) {
221-
home = envHomeFile;
222-
}
223-
}
224-
} catch (SecurityException e) {
225-
}
226-
227-
if (home != null) {
228-
if (sysPrefix.isEmpty()) {
229-
sysPrefix = home.getAbsoluteFile().getPath();
230-
env.getOptions().set(PythonOptions.SysPrefix, sysPrefix);
231-
}
232-
233-
if (basePrefix.isEmpty()) {
234-
basePrefix = home.getAbsoluteFile().getPath();
235-
env.getOptions().set(PythonOptions.SysBasePrefix, basePrefix);
236-
}
237-
238-
if (coreHome.isEmpty()) {
239-
try {
240-
for (TruffleFile f : home.list()) {
241-
if (f.getName().equals("lib-graalpython") && f.isDirectory()) {
242-
coreHome = f.getPath();
243-
break;
244-
}
245-
}
246-
} catch (SecurityException | IOException e) {
247-
}
248-
env.getOptions().set(PythonOptions.CoreHome, coreHome);
249-
}
250-
251-
if (stdLibHome.isEmpty()) {
252-
try {
253-
outer: for (TruffleFile f : home.list()) {
254-
if (f.getName().equals("lib-python") && f.isDirectory()) {
255-
for (TruffleFile f2 : f.list()) {
256-
if (f2.getName().equals("3") && f.isDirectory()) {
257-
stdLibHome = f2.getPath();
258-
break outer;
259-
}
260-
}
261-
}
262-
}
263-
} catch (SecurityException | IOException e) {
264-
}
265-
env.getOptions().set(PythonOptions.StdLibHome, stdLibHome);
266-
}
267-
268-
PythonCore.writeInfo((MessageFormat.format("Updated locations:" +
269-
"\n\tLanguage home: {0}" +
270-
"\n\tSysPrefix: {1}" +
271-
"\n\tSysBasePrefix: {2}" +
272-
"\n\tCoreHome: {3}" +
273-
"\n\tStdLibHome: {4}" +
274-
"\n\tExecutable: {5}", home.getPath(), sysPrefix, basePrefix, coreHome, stdLibHome, env.getOptions().get(PythonOptions.Executable))));
275-
}
192+
final PythonContext context = new PythonContext(this, env, newCore);
193+
context.initializeHomeAndPrefixPaths(env, getLanguageHome());
194+
return context;
276195
}
277196

278197
@Override

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ public boolean isInitialized() {
408408
public void initialize(PythonContext context) {
409409
singletonContext = context;
410410
initializeJavaCore();
411-
initializePythonCore();
411+
initializePythonCore(context.getCoreHomeOrFail());
412412
initialized = true;
413413
}
414414

@@ -419,8 +419,7 @@ private void initializeJavaCore() {
419419
builtinsModule = builtinModules.get("builtins");
420420
}
421421

422-
private void initializePythonCore() {
423-
String coreHome = PythonCore.getCoreHomeOrFail();
422+
private void initializePythonCore(String coreHome) {
424423
loadFile("builtins", coreHome);
425424
for (String s : coreFiles) {
426425
loadFile(s, coreHome);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
8282
import com.oracle.graal.python.nodes.util.CastToStringNode;
8383
import com.oracle.graal.python.runtime.PythonContext;
84-
import com.oracle.graal.python.runtime.PythonCore;
8584
import com.oracle.graal.python.runtime.PythonOptions;
8685
import com.oracle.graal.python.runtime.exception.PException;
8786
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -244,7 +243,7 @@ private void ensureCapiWasLoaded() {
244243
if (!ctxt.capiWasLoaded()) {
245244
Env env = ctxt.getEnv();
246245
CompilerDirectives.transferToInterpreterAndInvalidate();
247-
TruffleFile capiFile = env.getTruffleFile(PythonCore.getCoreHome(env) + env.getFileNameSeparator() + "capi.bc");
246+
TruffleFile capiFile = env.getTruffleFile(getContext().getCoreHome() + env.getFileNameSeparator() + "capi.bc");
248247
Object capi = null;
249248
try {
250249
SourceBuilder capiSrcBuilder = Source.newBuilder(LLVM_LANGUAGE, capiFile);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
import com.oracle.graal.python.nodes.util.CastToStringNode;
6767
import com.oracle.graal.python.runtime.PythonContext;
6868
import com.oracle.graal.python.runtime.PythonCore;
69-
import com.oracle.graal.python.runtime.PythonOptions;
7069
import com.oracle.graal.python.runtime.exception.PythonErrorType;
7170
import com.oracle.truffle.api.CompilerDirectives;
7271
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -102,7 +101,7 @@ public void initialize(PythonCore core) {
102101

103102
PythonContext context = core.getContext();
104103
Env env = context.getEnv();
105-
String coreHome = PythonOptions.getOption(context, PythonOptions.CoreHome);
104+
String coreHome = context.getCoreHome();
106105
try {
107106
TruffleFile coreDir = env.getTruffleFile(coreHome);
108107
TruffleFile docDir = coreDir.resolveSibling("doc");

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,20 @@ public void postInitialize(PythonCore core) {
165165
String[] args = context.getEnv().getApplicationArguments();
166166
sys.setAttribute("argv", core.factory().createList(Arrays.copyOf(args, args.length, Object[].class)));
167167

168-
String prefix = PythonCore.getSysPrefix(context.getEnv());
168+
String prefix = context.getSysPrefix();
169169
for (String name : SysModuleBuiltins.SYS_PREFIX_ATTRIBUTES) {
170170
sys.setAttribute(name, prefix);
171171
}
172172

173-
String base_prefix = PythonCore.getSysBasePrefix(context.getEnv());
173+
String base_prefix = context.getSysBasePrefix();
174174
for (String name : SysModuleBuiltins.BASE_PREFIX_ATTRIBUTES) {
175175
sys.setAttribute(name, base_prefix);
176176
}
177177

178178
sys.setAttribute("executable", PythonOptions.getOption(context, PythonOptions.Executable));
179179
sys.setAttribute("graal_python_home", context.getLanguage().getHome());
180-
sys.setAttribute("graal_python_core_home", PythonOptions.getOption(context, PythonOptions.CoreHome));
181-
sys.setAttribute("graal_python_stdlib_home", PythonOptions.getOption(context, PythonOptions.StdLibHome));
180+
sys.setAttribute("graal_python_core_home", context.getCoreHome());
181+
sys.setAttribute("graal_python_stdlib_home", context.getStdlibHome());
182182
sys.setAttribute("__flags__", core.factory().createTuple(new Object[]{
183183
false, // bytes_warning
184184
!PythonOptions.getFlag(context, PythonOptions.PythonOptimizeFlag), // debug
@@ -204,7 +204,7 @@ public void postInitialize(PythonCore core) {
204204
boolean doIsolate = PythonOptions.getOption(context, PythonOptions.IsolateFlag);
205205
int defaultPaths = doIsolate ? 2 : 3;
206206
if (option.length() > 0) {
207-
String[] split = option.split(PythonCore.PATH_SEPARATOR);
207+
String[] split = option.split(context.getEnv().getPathSeparator());
208208
path = new Object[split.length + defaultPaths];
209209
System.arraycopy(split, 0, path, 0, split.length);
210210
pathIdx = split.length;
@@ -214,8 +214,8 @@ public void postInitialize(PythonCore core) {
214214
if (!doIsolate) {
215215
path[pathIdx++] = getScriptPath(env, args);
216216
}
217-
path[pathIdx++] = PythonCore.getStdlibHome(env);
218-
path[pathIdx++] = PythonCore.getCoreHome(env) + env.getFileNameSeparator() + "modules";
217+
path[pathIdx++] = context.getStdlibHome();
218+
path[pathIdx++] = context.getCoreHome() + env.getFileNameSeparator() + "modules";
219219
PList sysPaths = core.factory().createList(path);
220220
sys.setAttribute("path", sysPaths);
221221
}

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

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
import static com.oracle.graal.python.nodes.BuiltinNames.__MAIN__;
3131
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FILE__;
3232

33+
import java.io.IOException;
3334
import java.io.InputStream;
3435
import java.io.OutputStream;
36+
import java.text.MessageFormat;
3537
import java.util.HashMap;
3638
import java.util.concurrent.atomic.AtomicLong;
3739
import java.util.concurrent.locks.ReentrantLock;
3840
import java.util.function.Supplier;
3941

42+
import com.oracle.truffle.api.TruffleFile;
4043
import org.graalvm.nativeimage.ImageInfo;
4144
import org.graalvm.options.OptionValues;
4245

@@ -67,6 +70,14 @@
6770

6871
public final class PythonContext {
6972

73+
static final String PREFIX = "/";
74+
static final String LIB_PYTHON_3 = "/lib-python/3";
75+
static final String LIB_GRAALPYTHON = "/lib-graalpython";
76+
static final String NO_CORE_FATAL = "could not determine Graal.Python's core path - you must pass --python.CoreHome.";
77+
static final String NO_PREFIX_WARNING = "could not determine Graal.Python's sys prefix path - you may need to pass --python.SysPrefix.";
78+
static final String NO_CORE_WARNING = "could not determine Graal.Python's core path - you may need to pass --python.CoreHome.";
79+
static final String NO_STDLIB = "could not determine Graal.Python's standard library path. You need to pass --python.StdLibHome if you want to use the standard library.";
80+
7081
private final PythonLanguage language;
7182
private PythonModule mainModule;
7283
private final PythonCore core;
@@ -329,21 +340,149 @@ private void setupRuntimeInformation(boolean isPatching) {
329340
sysModules.setItem(__MAIN__, mainModule);
330341

331342
final String stdLibPlaceholder = "!stdLibHome!";
332-
final String stdLibHome = PythonCore.getStdlibHome(getEnv());
333343
if (ImageInfo.inImageBuildtimeCode()) {
334344
// Patch any pre-loaded packages' paths if we're running
335345
// pre-initialization
336-
patchPackagePaths(stdLibHome, stdLibPlaceholder);
346+
patchPackagePaths(getStdlibHome(), stdLibPlaceholder);
337347
} else if (isPatching && ImageInfo.inImageRuntimeCode()) {
338348
// Patch any pre-loaded packages' paths to the new stdlib home if
339349
// we're patching a pre-initialized context
340-
patchPackagePaths(stdLibPlaceholder, stdLibHome);
350+
patchPackagePaths(stdLibPlaceholder, getStdlibHome());
341351
}
342352

343353
currentException = null;
344354
isInitialized = true;
345355
}
346356

357+
private String sysPrefix, basePrefix, coreHome, stdLibHome;
358+
359+
public void initializeHomeAndPrefixPaths(Env newEnv, String languageHome) {
360+
sysPrefix = newEnv.getOptions().get(PythonOptions.SysPrefix);
361+
basePrefix = newEnv.getOptions().get(PythonOptions.SysBasePrefix);
362+
coreHome = newEnv.getOptions().get(PythonOptions.CoreHome);
363+
stdLibHome = newEnv.getOptions().get(PythonOptions.StdLibHome);
364+
365+
PythonCore.writeInfo((MessageFormat.format("Initial locations:" +
366+
"\n\tLanguage home: {0}" +
367+
"\n\tSysPrefix: {1}" +
368+
"\n\tBaseSysPrefix: {2}" +
369+
"\n\tCoreHome: {3}" +
370+
"\n\tStdLibHome: {4}", languageHome, sysPrefix, basePrefix, coreHome, stdLibHome)));
371+
372+
TruffleFile home = null;
373+
if (languageHome != null) {
374+
home = newEnv.getTruffleFile(languageHome);
375+
}
376+
377+
try {
378+
String envHome = System.getenv("GRAAL_PYTHONHOME");
379+
if (envHome != null) {
380+
TruffleFile envHomeFile = newEnv.getTruffleFile(envHome);
381+
if (envHomeFile.isDirectory()) {
382+
home = envHomeFile;
383+
}
384+
}
385+
} catch (SecurityException e) {
386+
}
387+
388+
if (home != null) {
389+
if (sysPrefix.isEmpty()) {
390+
sysPrefix = home.getAbsoluteFile().getPath();
391+
}
392+
393+
if (basePrefix.isEmpty()) {
394+
basePrefix = home.getAbsoluteFile().getPath();
395+
}
396+
397+
if (coreHome.isEmpty()) {
398+
try {
399+
for (TruffleFile f : home.list()) {
400+
if (f.getName().equals("lib-graalpython") && f.isDirectory()) {
401+
coreHome = f.getPath();
402+
break;
403+
}
404+
}
405+
} catch (SecurityException | IOException e) {
406+
}
407+
}
408+
409+
if (stdLibHome.isEmpty()) {
410+
try {
411+
outer: for (TruffleFile f : home.list()) {
412+
if (f.getName().equals("lib-python") && f.isDirectory()) {
413+
for (TruffleFile f2 : f.list()) {
414+
if (f2.getName().equals("3") && f.isDirectory()) {
415+
stdLibHome = f2.getPath();
416+
break outer;
417+
}
418+
}
419+
}
420+
}
421+
} catch (SecurityException | IOException e) {
422+
}
423+
}
424+
425+
PythonCore.writeInfo((MessageFormat.format("Updated locations:" +
426+
"\n\tLanguage home: {0}" +
427+
"\n\tSysPrefix: {1}" +
428+
"\n\tSysBasePrefix: {2}" +
429+
"\n\tCoreHome: {3}" +
430+
"\n\tStdLibHome: {4}" +
431+
"\n\tExecutable: {5}", home.getPath(), sysPrefix, basePrefix, coreHome, stdLibHome, newEnv.getOptions().get(PythonOptions.Executable))));
432+
}
433+
}
434+
435+
@TruffleBoundary
436+
public String getSysPrefix() {
437+
if (sysPrefix.isEmpty()) {
438+
writeWarning(NO_PREFIX_WARNING);
439+
sysPrefix = PREFIX;
440+
}
441+
return sysPrefix;
442+
}
443+
444+
@TruffleBoundary
445+
public String getSysBasePrefix() {
446+
if (basePrefix.isEmpty()) {
447+
String homePrefix = language.getHome();
448+
if (homePrefix == null || homePrefix.isEmpty()) {
449+
homePrefix = PREFIX;
450+
}
451+
basePrefix = homePrefix;
452+
}
453+
return basePrefix;
454+
}
455+
456+
@TruffleBoundary
457+
public String getCoreHome() {
458+
if (coreHome.isEmpty()) {
459+
writeWarning(NO_CORE_WARNING);
460+
coreHome = LIB_GRAALPYTHON;
461+
}
462+
return coreHome;
463+
}
464+
465+
@TruffleBoundary
466+
public String getStdlibHome() {
467+
if (stdLibHome.isEmpty()) {
468+
writeWarning(NO_STDLIB);
469+
stdLibHome = LIB_PYTHON_3;
470+
}
471+
return stdLibHome;
472+
}
473+
474+
@TruffleBoundary
475+
public String getCoreHomeOrFail() {
476+
if (coreHome.isEmpty()) {
477+
throw new RuntimeException(NO_CORE_FATAL);
478+
}
479+
return coreHome;
480+
}
481+
482+
private static void writeWarning(String warning) {
483+
PythonLanguage.getLogger().warning(warning);
484+
}
485+
347486
public boolean capiWasLoaded() {
348487
return this.capiLibrary != null;
349488
}

0 commit comments

Comments
 (0)