Skip to content

Commit 6c8249a

Browse files
committed
[GR-40306] Temporarily switch to AST interpreter when instrumentation is requested
PullRequest: graalpython/2392
2 parents 129e955 + 7fe49ea commit 6c8249a

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public static void main(String[] args) {
102102
private String warnOptions = null;
103103
private String checkHashPycsMode = "default";
104104

105+
boolean useASTInterpreter = false;
106+
105107
protected static void setStartupTime() {
106108
if (GraalPythonMain.startupNanoTime == -1) {
107109
GraalPythonMain.startupNanoTime = System.nanoTime();
@@ -122,6 +124,7 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
122124
List<String> subprocessArgs = new ArrayList<>();
123125
programArgs = new ArrayList<>();
124126
boolean posixBackendSpecified = false;
127+
boolean toolInstrumentWarning = true;
125128
for (Iterator<String> argumentIterator = arguments.iterator(); argumentIterator.hasNext();) {
126129
String arg = argumentIterator.next();
127130
if (arg.startsWith("-")) {
@@ -192,8 +195,19 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
192195
}
193196
checkHashPycsMode = argumentIterator.next();
194197
continue;
198+
case "--use-ast-interpreter":
199+
case "--python.UseASTInterpreter":
200+
useASTInterpreter = true;
201+
continue;
195202
default:
196-
if (arg.startsWith("--llvm.") ||
203+
if (argStartsWith(arg, "--agentscript", "--coverage", "--cpusampler", "--cputracer", "--dap",
204+
"--heap.", "--heapmonitor", "--insight", "--inspect", "--lsp", "--memtracer", "--sandbox.")) {
205+
useASTInterpreter = true;
206+
if (toolInstrumentWarning) {
207+
toolInstrumentWarning = false;
208+
System.out.println("WARNING: Switching to AST interpreter due to instruments option " + arg);
209+
}
210+
} else if (arg.startsWith("--llvm.") ||
197211
matchesPythonOption(arg, "CoreHome") ||
198212
matchesPythonOption(arg, "StdLibHome") ||
199213
matchesPythonOption(arg, "CAPI") ||
@@ -520,6 +534,8 @@ private String getExecutable() {
520534

521535
@Override
522536
protected void launch(Builder contextBuilder) {
537+
String cachePrefix = null;
538+
523539
// prevent the use of System.out/err - they are PrintStreams which suppresses exceptions
524540
contextBuilder.out(new FileOutputStream(FileDescriptor.out));
525541
contextBuilder.err(new FileOutputStream(FileDescriptor.err));
@@ -547,10 +563,7 @@ protected void launch(Builder contextBuilder) {
547563
warnOptions = envWarnOptions + "," + warnOptions;
548564
}
549565
}
550-
String cachePrefix = System.getenv("PYTHONPYCACHEPREFIX");
551-
if (cachePrefix != null) {
552-
contextBuilder.option("python.PyCachePrefix", cachePrefix);
553-
}
566+
cachePrefix = System.getenv("PYTHONPYCACHEPREFIX");
554567

555568
String encoding = System.getenv("PYTHONIOENCODING");
556569
if (encoding != null) {
@@ -577,7 +590,6 @@ protected void launch(Builder contextBuilder) {
577590
contextBuilder.option("python.VerboseFlag", Boolean.toString(verboseFlag));
578591
contextBuilder.option("python.IsolateFlag", Boolean.toString(isolateFlag));
579592
contextBuilder.option("python.WarnOptions", warnOptions);
580-
contextBuilder.option("python.DontWriteBytecodeFlag", Boolean.toString(dontWriteBytecode));
581593
if (verboseFlag) {
582594
contextBuilder.option("log.python.level", "FINE");
583595
}
@@ -603,6 +615,18 @@ protected void launch(Builder contextBuilder) {
603615
contextBuilder.option("python.InputFilePath", inputFile);
604616
}
605617

618+
if (useASTInterpreter) {
619+
contextBuilder.option("python.DontWriteBytecodeFlag", "true");
620+
contextBuilder.option("python.PyCachePrefix", "/dev/null");
621+
contextBuilder.option("python.EnableBytecodeInterpreter", "false");
622+
contextBuilder.option("python.DisableFrozenModules", "true");
623+
} else {
624+
contextBuilder.option("python.DontWriteBytecodeFlag", Boolean.toString(dontWriteBytecode));
625+
if (cachePrefix != null) {
626+
contextBuilder.option("python.PyCachePrefix", cachePrefix);
627+
}
628+
}
629+
606630
if (multiContext) {
607631
contextBuilder.engine(Engine.newBuilder().allowExperimentalOptions(true).options(enginePolyglotOptions).build());
608632
}
@@ -655,6 +679,15 @@ private static boolean matchesPythonOption(String arg, String key) {
655679
return arg.startsWith("--python." + key) || arg.startsWith("--" + key);
656680
}
657681

682+
private static boolean argStartsWith(String arg, String... tools) {
683+
for (String t : tools) {
684+
if (arg.startsWith(t)) {
685+
return true;
686+
}
687+
}
688+
return false;
689+
}
690+
658691
private String getContextOptionIfSetViaCommandLine(String key) {
659692
if (System.getProperty("polyglot.python." + key) != null) {
660693
return System.getProperty("polyglot.python." + key);

0 commit comments

Comments
 (0)