Skip to content

Commit 4f317c2

Browse files
committed
Make sure that rethrown RecursionError collects lazy frames
1 parent aacf794 commit 4f317c2

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.nodes.control;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.RecursionError;
4344
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
4445
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemExit;
4546

@@ -141,7 +142,7 @@ public Object execute(VirtualFrame frame) {
141142
}
142143
return null;
143144
} catch (StackOverflowError e) {
144-
PException pe = ExceptionHandlingStatementNode.createRecursionError(e, factory(), this);
145+
PException pe = ExceptionHandlingStatementNode.wrapJavaException(e, this, factory().createBaseException(RecursionError, "maximum recursion depth exceeded", new Object[]{}));
145146
PBaseException pythonException = pe.getExceptionObject();
146147
printExc(frame, pythonException);
147148
if (getContext().getOption(PythonOptions.WithJavaStacktrace)) {

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package com.oracle.graal.python.nodes.statement;
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.RecursionError;
44+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
4445

4546
import com.oracle.graal.python.PythonLanguage;
4647
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
@@ -49,7 +50,6 @@
4950
import com.oracle.graal.python.runtime.PythonContext;
5051
import com.oracle.graal.python.runtime.PythonOptions;
5152
import com.oracle.graal.python.runtime.exception.PException;
52-
import com.oracle.graal.python.runtime.exception.PythonErrorType;
5353
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5454
import com.oracle.truffle.api.CompilerDirectives;
5555
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -187,41 +187,31 @@ protected boolean shouldCatchAllExceptions() {
187187
return shouldCatchAllExceptions;
188188
}
189189

190-
private PException wrapJavaException(Throwable e) {
191-
PException pe = PException.fromObject(getBaseException(e), this);
190+
public static PException wrapJavaException(Throwable e, Node node, PBaseException pythonException) {
191+
PException pe = PException.fromObject(pythonException, node);
192192
pe.setHideLocation(true);
193193
// Re-attach truffle stacktrace
194194
moveTruffleStackTrace(e, pe);
195-
return pe;
195+
// Create a new traceback chain, because the current one has been finalized by Truffle
196+
return pe.getExceptionForReraise();
196197
}
197198

199+
@TruffleBoundary
198200
protected PException wrapJavaExceptionIfApplicable(Throwable e) {
199201
if (e instanceof StackOverflowError) {
200-
return createRecursionError(e, factory(), this);
202+
return wrapJavaException(e, this, factory().createBaseException(RecursionError, "maximum recursion depth exceeded", new Object[]{}));
201203
}
202204
if (shouldCatchAllExceptions() && (e instanceof Exception || e instanceof AssertionError)) {
203-
return wrapJavaException(e);
205+
return wrapJavaException(e, this, factory().createBaseException(SystemError, "%m", new Object[]{e}));
204206
}
205207
return null;
206208
}
207209

208-
public static PException createRecursionError(Throwable e, PythonObjectFactory factory, Node location) {
209-
PException pe = PException.fromObject(factory.createBaseException(RecursionError, "maximum recursion depth exceeded", new Object[]{}), location);
210-
pe.setHideLocation(true);
211-
moveTruffleStackTrace(e, pe);
212-
return pe;
213-
}
214-
215210
@TruffleBoundary
216211
private static void moveTruffleStackTrace(Throwable e, PException pe) {
217212
pe.initCause(e.getCause());
218213
// Host exceptions have their stacktrace already filled in, call this to set
219214
// the cutoff point to the catch site
220215
pe.getTruffleStackTrace();
221216
}
222-
223-
@TruffleBoundary
224-
private PBaseException getBaseException(Throwable e) {
225-
return factory().createBaseException(PythonErrorType.SystemError, "%m", new Object[]{e});
226-
}
227217
}

0 commit comments

Comments
 (0)