Skip to content

Commit 4a93c46

Browse files
committed
[GR-31315] Add cwd to path after importing site
PullRequest: graalpython/1801
2 parents 2aafb77 + b0e59b2 commit 4a93c46

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
143143
inspectFlag = true;
144144
break;
145145
case "-m":
146-
if (i + 1 < arguments.size()) {
147-
// don't increment i here so that we capture the correct args
148-
String module = arguments.get(i + 1);
146+
programArgs.add(arg);
147+
i++;
148+
if (i < arguments.size()) {
149+
String module = arguments.get(i);
149150
commandString = "import runpy; runpy._run_module_as_main('" + module + "')";
150151
} else {
151152
print("Argument expected for the -m option");

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
import com.oracle.truffle.api.CompilerDirectives;
100100
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
101101
import com.oracle.truffle.api.Truffle;
102-
import com.oracle.truffle.api.TruffleFile;
103102
import com.oracle.truffle.api.TruffleLanguage.Env;
104103
import com.oracle.truffle.api.dsl.Cached;
105104
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -401,15 +400,11 @@ public void postInitialize(PythonCore core) {
401400
Env env = context.getEnv();
402401
String option = context.getOption(PythonOptions.PythonPath);
403402

404-
boolean isIsolated = context.getOption(PythonOptions.IsolateFlag);
405403
boolean capiSeparate = !capiHome.equals(coreHome);
406404

407405
Object[] path;
408406
int pathIdx = 0;
409407
int defaultPathsLen = 2;
410-
if (!isIsolated) {
411-
defaultPathsLen++;
412-
}
413408
if (capiSeparate) {
414409
defaultPathsLen++;
415410
}
@@ -421,9 +416,6 @@ public void postInitialize(PythonCore core) {
421416
} else {
422417
path = new Object[defaultPathsLen];
423418
}
424-
if (!isIsolated) {
425-
path[pathIdx++] = getScriptPath(env, args);
426-
}
427419
path[pathIdx++] = stdlibHome;
428420
path[pathIdx++] = coreHome + env.getFileNameSeparator() + "modules";
429421
if (capiSeparate) {
@@ -451,29 +443,6 @@ public void postInitialize(PythonCore core) {
451443
));
452444
}
453445

454-
private static String getScriptPath(Env env, String[] args) {
455-
String scriptPath;
456-
if (args.length > 0) {
457-
String argv0 = args[0];
458-
if (argv0 != null && !argv0.startsWith("-") && !argv0.isEmpty()) {
459-
TruffleFile scriptFile = env.getPublicTruffleFile(argv0);
460-
try {
461-
scriptPath = scriptFile.getAbsoluteFile().getParent().getPath();
462-
} catch (SecurityException e) {
463-
scriptPath = scriptFile.getParent().getPath();
464-
}
465-
if (scriptPath == null) {
466-
scriptPath = ".";
467-
}
468-
} else {
469-
scriptPath = "";
470-
}
471-
} else {
472-
scriptPath = "";
473-
}
474-
return scriptPath;
475-
}
476-
477446
@Builtin(name = "exc_info", needsFrame = true)
478447
@GenerateNodeFactory
479448
public abstract static class ExcInfoNode extends PythonBuiltinNode {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,44 @@ private void importSiteIfForced() {
698698
CallTarget site = env.parsePublic(IMPORT_WARNINGS_SOURCE);
699699
site.call();
700700
}
701+
if (!getOption(PythonOptions.IsolateFlag)) {
702+
String path0 = computeSysPath0();
703+
if (path0 != null) {
704+
PythonModule sys = core.lookupBuiltinModule("sys");
705+
Object path = sys.getAttribute("path");
706+
PythonObjectLibrary.getUncached().lookupAndCallRegularMethod(path, null, "insert", 0, path0);
707+
}
708+
}
709+
}
710+
711+
// Equivalent of pathconfig.c:_PyPathConfig_ComputeSysPath0
712+
private String computeSysPath0() {
713+
String[] args = env.getApplicationArguments();
714+
if (args.length == 0) {
715+
return null;
716+
}
717+
String argv0 = args[0];
718+
if (argv0.isEmpty()) {
719+
return "";
720+
} else if (argv0.equals("-m")) {
721+
try {
722+
return env.getCurrentWorkingDirectory().getPath();
723+
} catch (SecurityException e) {
724+
return null;
725+
}
726+
} else if (!argv0.equals("-c")) {
727+
TruffleFile scriptFile = env.getPublicTruffleFile(argv0);
728+
TruffleFile parent;
729+
try {
730+
parent = scriptFile.getCanonicalFile().getParent();
731+
} catch (SecurityException | IOException e) {
732+
parent = scriptFile.getParent();
733+
}
734+
if (parent != null) {
735+
return parent.getPath();
736+
}
737+
}
738+
return "";
701739
}
702740

703741
/**

0 commit comments

Comments
 (0)