Skip to content

Commit b37a27c

Browse files
committed
Handle stack overflow on the top level too
1 parent 635374f commit b37a27c

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@
6262
import com.oracle.graal.python.nodes.call.CallNode;
6363
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
6464
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
65+
import com.oracle.graal.python.nodes.statement.ExceptionHandlingStatementNode;
6566
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext;
6667
import com.oracle.graal.python.runtime.PythonContext;
6768
import com.oracle.graal.python.runtime.PythonCore;
6869
import com.oracle.graal.python.runtime.PythonOptions;
6970
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
7071
import com.oracle.graal.python.runtime.exception.PException;
7172
import com.oracle.graal.python.runtime.exception.PythonExitException;
73+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
7274
import com.oracle.truffle.api.CompilerDirectives;
7375
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
7476
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -89,6 +91,7 @@ public class TopLevelExceptionHandler extends RootNode {
8991
@Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode.create(__STR__);
9092
@Child private CallNode exceptionHookCallNode = CallNode.create();
9193
@Child private GetExceptionTracebackNode getExceptionTracebackNode;
94+
@Child private PythonObjectFactory pythonObjectFactory;
9295

9396
public TopLevelExceptionHandler(PythonLanguage language, RootNode child) {
9497
super(language);
@@ -112,6 +115,14 @@ private PythonContext getContext() {
112115
return context.get();
113116
}
114117

118+
private PythonObjectFactory factory() {
119+
if (pythonObjectFactory == null) {
120+
CompilerDirectives.transferToInterpreterAndInvalidate();
121+
pythonObjectFactory = insert(PythonObjectFactory.create());
122+
}
123+
return pythonObjectFactory;
124+
}
125+
115126
@Override
116127
public Object execute(VirtualFrame frame) {
117128
if (exception != null) {
@@ -129,7 +140,15 @@ public Object execute(VirtualFrame frame) {
129140
printStackTrace(e);
130141
}
131142
return null;
132-
} catch (Exception | StackOverflowError e) {
143+
} catch (StackOverflowError e) {
144+
PException pe = ExceptionHandlingStatementNode.createRecursionError(e, factory(), this);
145+
PBaseException pythonException = pe.getExceptionObject();
146+
printExc(frame, pythonException);
147+
if (getContext().getOption(PythonOptions.WithJavaStacktrace)) {
148+
printStackTrace(e);
149+
}
150+
return null;
151+
} catch (Exception e) {
133152
boolean exitException = e instanceof TruffleException && ((TruffleException) e).isExit();
134153
if (!exitException) {
135154
ExceptionUtils.printPythonLikeStackTrace(e);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/ExceptionHandlingStatementNode.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.oracle.truffle.api.TruffleException;
5858
import com.oracle.truffle.api.TruffleLanguage;
5959
import com.oracle.truffle.api.frame.VirtualFrame;
60+
import com.oracle.truffle.api.nodes.Node;
6061
import com.oracle.truffle.api.profiles.ConditionProfile;
6162
import com.oracle.truffle.api.profiles.LoopConditionProfile;
6263

@@ -195,16 +196,16 @@ private PException wrapJavaException(Throwable e) {
195196

196197
protected PException wrapJavaExceptionIfApplicable(Throwable e) {
197198
if (e instanceof StackOverflowError) {
198-
return createRecursionError(e);
199+
return createRecursionError(e, factory(), this);
199200
}
200201
if (shouldCatchAllExceptions() && (e instanceof Exception || e instanceof AssertionError)) {
201202
return wrapJavaException(e);
202203
}
203204
return null;
204205
}
205206

206-
private PException createRecursionError(Throwable e) {
207-
PException pe = PException.fromObject(factory().createBaseException(RecursionError, "maximum recursion depth exceeded", new Object[]{}), this);
207+
public static PException createRecursionError(Throwable e, PythonObjectFactory factory, Node location) {
208+
PException pe = PException.fromObject(factory.createBaseException(RecursionError, "maximum recursion depth exceeded", new Object[]{}), location);
208209
moveTruffleStackTrace(e, pe);
209210
return pe;
210211
}

0 commit comments

Comments
 (0)