Skip to content

Commit b0e59b2

Browse files
committed
Add cwd to path after importing site
Fixes #207
1 parent 9338e03 commit b0e59b2

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
@@ -555,6 +555,44 @@ private void importSiteIfForced() {
555555
CallTarget site = env.parsePublic(IMPORT_WARNINGS_SOURCE);
556556
site.call();
557557
}
558+
if (!getOption(PythonOptions.IsolateFlag)) {
559+
String path0 = computeSysPath0();
560+
if (path0 != null) {
561+
PythonModule sys = core.lookupBuiltinModule("sys");
562+
Object path = sys.getAttribute("path");
563+
PythonObjectLibrary.getUncached().lookupAndCallRegularMethod(path, null, "insert", 0, path0);
564+
}
565+
}
566+
}
567+
568+
// Equivalent of pathconfig.c:_PyPathConfig_ComputeSysPath0
569+
private String computeSysPath0() {
570+
String[] args = env.getApplicationArguments();
571+
if (args.length == 0) {
572+
return null;
573+
}
574+
String argv0 = args[0];
575+
if (argv0.isEmpty()) {
576+
return "";
577+
} else if (argv0.equals("-m")) {
578+
try {
579+
return env.getCurrentWorkingDirectory().getPath();
580+
} catch (SecurityException e) {
581+
return null;
582+
}
583+
} else if (!argv0.equals("-c")) {
584+
TruffleFile scriptFile = env.getPublicTruffleFile(argv0);
585+
TruffleFile parent;
586+
try {
587+
parent = scriptFile.getCanonicalFile().getParent();
588+
} catch (SecurityException | IOException e) {
589+
parent = scriptFile.getParent();
590+
}
591+
if (parent != null) {
592+
return parent.getPath();
593+
}
594+
}
595+
return "";
558596
}
559597

560598
/**

0 commit comments

Comments
 (0)