Skip to content

Commit cb7b3fd

Browse files
committed
[GR-11916] Don't use fixed context in TopLevelExceptionHandler.
PullRequest: graalpython/206
2 parents 9f5f697 + 5c5bcf6 commit cb7b3fd

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/PythonTests.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,15 @@ public static void assertPrints(String expected, Path scriptName) {
192192
public static void assertPrints(String expected, String code) {
193193
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
194194
final PrintStream printStream = new PrintStream(byteArray);
195-
String source = code;
196-
PythonTests.runScript(new String[0], source, printStream, System.err);
195+
PythonTests.runScript(new String[0], code, printStream, System.err);
196+
String result = byteArray.toString().replaceAll("\r\n", "\n");
197+
assertEquals(expected.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"), result.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"));
198+
}
199+
200+
public static void assertPrints(String expected, org.graalvm.polyglot.Source code) {
201+
final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
202+
final PrintStream printStream = new PrintStream(byteArray);
203+
PythonTests.runScript(new String[0], code, printStream, System.err);
197204
String result = byteArray.toString().replaceAll("\r\n", "\n");
198205
assertEquals(expected.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"), result.replaceAll(" at 0x[0-9a-f]*>", " at 0xabcd>"));
199206
}
@@ -311,6 +318,15 @@ public static void runScript(String[] args, String source, OutputStream out, Out
311318
}
312319
}
313320

321+
public static void runScript(String[] args, org.graalvm.polyglot.Source source, OutputStream out, OutputStream err) {
322+
try {
323+
enterContext(args);
324+
context.eval(source);
325+
} finally {
326+
flush(out, err);
327+
}
328+
}
329+
314330
public static void runScript(String[] args, String source, OutputStream out, OutputStream err, Runnable cb) {
315331
try {
316332
enterContext(args);

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/basic/HelloWorldTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,11 @@ public class HelloWorldTests {
3434
public void helloworld() {
3535
assertPrints("hello world\n", "print(\"hello world\")");
3636
}
37+
38+
@Test
39+
public void helloworldAgain() {
40+
org.graalvm.polyglot.Source source = org.graalvm.polyglot.Source.create("python", "try: print(value)\nexcept:print('hello')\nvalue='world'");
41+
assertPrints("hello\n", source);
42+
assertPrints("hello\n", source);
43+
}
3744
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/TopLevelExceptionHandler.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,33 @@
6868
import com.oracle.truffle.api.RootCallTarget;
6969
import com.oracle.truffle.api.Truffle;
7070
import com.oracle.truffle.api.TruffleException;
71+
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
7172
import com.oracle.truffle.api.frame.VirtualFrame;
7273
import com.oracle.truffle.api.nodes.RootNode;
7374
import com.oracle.truffle.api.source.SourceSection;
7475

7576
public class TopLevelExceptionHandler extends RootNode {
7677
private final RootCallTarget innerCallTarget;
7778
private final PException exception;
78-
private final PythonContext context;
79+
private final ContextReference<PythonContext> context;
80+
private final SourceSection sourceSection;
81+
7982
@Child private CreateArgumentsNode createArgs = CreateArgumentsNode.create();
8083
@Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode.create(__STR__);
8184
@Child private CallNode callNode = CallNode.create();
82-
private SourceSection sourceSection;
8385

8486
public TopLevelExceptionHandler(PythonLanguage language, RootNode child) {
8587
super(language);
8688
this.sourceSection = child.getSourceSection();
87-
this.context = language.getContextReference().get();
89+
this.context = language.getContextReference();
8890
this.innerCallTarget = Truffle.getRuntime().createCallTarget(child);
8991
this.exception = null;
9092
}
9193

9294
public TopLevelExceptionHandler(PythonLanguage language, PException exception) {
9395
super(language);
9496
this.sourceSection = exception.getSourceLocation();
95-
this.context = language.getContextReference().get();
97+
this.context = language.getContextReference();
9698
this.innerCallTarget = null;
9799
this.exception = exception;
98100
}
@@ -103,14 +105,14 @@ public Object execute(VirtualFrame frame) {
103105
printExc(exception);
104106
return null;
105107
} else {
106-
assert context.getCurrentException() == null;
108+
assert context.get().getCurrentException() == null;
107109
try {
108110
return run(frame);
109111
} catch (PException e) {
110112
printExc(e);
111113
return null;
112114
} catch (Exception e) {
113-
if (PythonOptions.getOption(context, PythonOptions.WithJavaStacktrace)) {
115+
if (PythonOptions.getOption(context.get(), PythonOptions.WithJavaStacktrace)) {
114116
boolean exitException = e instanceof TruffleException && ((TruffleException) e).isExit();
115117
if (!exitException) {
116118
printStackTrace(e);
@@ -132,7 +134,8 @@ public SourceSection getSourceSection() {
132134
*/
133135
private void printExc(PException e) {
134136
CompilerDirectives.transferToInterpreter();
135-
PythonCore core = context.getCore();
137+
PythonContext theContext = context.get();
138+
PythonCore core = theContext.getCore();
136139
if (core.getErrorClass(SystemExit) == e.getType()) {
137140
handleSystemExit(e);
138141
}
@@ -147,7 +150,7 @@ private void printExc(PException e) {
147150
sys.setAttribute(BuiltinNames.LAST_TRACEBACK, tb);
148151

149152
Object hook = sys.getAttribute(BuiltinNames.EXCEPTHOOK);
150-
if (PythonOptions.getOption(context, PythonOptions.AlwaysRunExcepthook)) {
153+
if (PythonOptions.getOption(theContext, PythonOptions.AlwaysRunExcepthook)) {
151154
if (hook != PNone.NO_VALUE) {
152155
try {
153156
callNode.execute(null, hook, new Object[]{type, value, tb}, PKeyword.EMPTY_KEYWORDS);
@@ -161,7 +164,7 @@ private void printExc(PException e) {
161164
}
162165
} else {
163166
try {
164-
context.getEnv().err().write("sys.excepthook is missing\n".getBytes());
167+
theContext.getEnv().err().write("sys.excepthook is missing\n".getBytes());
165168
} catch (IOException ioException) {
166169
ioException.printStackTrace();
167170
}
@@ -171,7 +174,8 @@ private void printExc(PException e) {
171174
}
172175

173176
private void handleSystemExit(PException e) {
174-
if (PythonOptions.getOption(context, PythonOptions.InspectFlag) && !getSourceSection().getSource().isInteractive()) {
177+
PythonContext theContext = context.get();
178+
if (PythonOptions.getOption(theContext, PythonOptions.InspectFlag) && !getSourceSection().getSource().isInteractive()) {
175179
// Don't exit if -i flag was given and we're not yet running interactively
176180
return;
177181
}
@@ -187,11 +191,11 @@ private void handleSystemExit(PException e) {
187191
if (exitcode != null) {
188192
throw new PythonExitException(this, exitcode);
189193
}
190-
if (PythonOptions.getOption(context, PythonOptions.AlwaysRunExcepthook)) {
194+
if (PythonOptions.getOption(theContext, PythonOptions.AlwaysRunExcepthook)) {
191195
// If we failed to dig out the exit code we just print and leave
192196
try {
193-
context.getEnv().err().write(callStrNode.executeObject(e.getExceptionObject()).toString().getBytes());
194-
context.getEnv().err().write('\n');
197+
theContext.getEnv().err().write(callStrNode.executeObject(e.getExceptionObject()).toString().getBytes());
198+
theContext.getEnv().err().write('\n');
195199
} catch (IOException e1) {
196200
}
197201
throw new PythonExitException(this, 1);
@@ -207,7 +211,7 @@ private static void printStackTrace(Exception e) {
207211

208212
private Object run(VirtualFrame frame) {
209213
Object[] arguments = createArgs.execute(frame.getArguments());
210-
PArguments.setGlobals(arguments, context.getMainModule());
214+
PArguments.setGlobals(arguments, context.get().getMainModule());
211215
return innerCallTarget.call(arguments);
212216
}
213217

0 commit comments

Comments
 (0)