Skip to content

Commit 9e3c382

Browse files
msimacekcosminbasca
authored andcommitted
Make with node work with interop exceptions
1 parent 9d27c9a commit 9e3c382

File tree

11 files changed

+115
-108
lines changed

11 files changed

+115
-108
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@
109109
import java.util.Map;
110110
import java.util.Set;
111111

112-
import com.oracle.graal.python.builtins.PythonOS;
113-
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
114-
import com.oracle.graal.python.nodes.util.CannotCastException;
115112
import org.graalvm.nativeimage.ImageInfo;
116113

117114
import com.oracle.graal.python.PythonLanguage;
@@ -122,6 +119,7 @@
122119
import com.oracle.graal.python.builtins.Python3Core;
123120
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
124121
import com.oracle.graal.python.builtins.PythonBuiltins;
122+
import com.oracle.graal.python.builtins.PythonOS;
125123
import com.oracle.graal.python.builtins.modules.SysModuleBuiltinsClinicProviders.GetFrameNodeClinicProviderGen;
126124
import com.oracle.graal.python.builtins.modules.io.BufferedReaderBuiltins;
127125
import com.oracle.graal.python.builtins.modules.io.BufferedWriterBuiltins;
@@ -132,6 +130,8 @@
132130
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodes.TextIOWrapperInitNode;
133131
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodesFactory.TextIOWrapperInitNodeGen;
134132
import com.oracle.graal.python.builtins.objects.PNone;
133+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
134+
import com.oracle.graal.python.builtins.objects.dict.PDict;
135135
import com.oracle.graal.python.builtins.objects.dict.PDict;
136136
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
137137
import com.oracle.graal.python.builtins.objects.frame.PFrame;
@@ -179,6 +179,7 @@
179179
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
180180
import com.oracle.graal.python.nodes.object.GetClassNode;
181181
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
182+
import com.oracle.graal.python.nodes.util.CannotCastException;
182183
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
183184
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.GetCaughtExceptionNode;
184185
import com.oracle.graal.python.runtime.PosixSupportLibrary;
@@ -1176,8 +1177,8 @@ protected void printException(VirtualFrame frame, PythonModule sys, Object out,
11761177
}
11771178

11781179
final PBaseException exc = (PBaseException) value;
1179-
final PTraceback tb = getExceptionTraceback(exc);
1180-
if (tb != null) {
1180+
final Object tb = getExceptionTraceback(exc);
1181+
if (tb instanceof PTraceback) {
11811182
printTraceBack(frame, sys, out, tb);
11821183
}
11831184

@@ -1255,8 +1256,8 @@ static Set<Object> createSet() {
12551256
Object doHookWithTb(VirtualFrame frame, PythonModule sys, @SuppressWarnings("unused") Object excType, Object value, PTraceback traceBack) {
12561257
if (PGuards.isPBaseException(value)) {
12571258
final PBaseException exc = (PBaseException) value;
1258-
final PTraceback currTb = getExceptionTraceback(exc);
1259-
if (currTb == null) {
1259+
final Object currTb = getExceptionTraceback(exc);
1260+
if (currTb instanceof PTraceback) {
12601261
exc.setTraceback(traceBack);
12611262
}
12621263
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionBuiltins.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,9 @@ public Object setSuppressContext(PBaseException self, Object value,
292292
public abstract static class TracebackNode extends PythonBuiltinNode {
293293

294294
@Specialization(guards = "isNoValue(tb)")
295-
public static Object getTraceback(PBaseException self, @SuppressWarnings("unused") Object tb,
295+
public static Object getTraceback(Object self, @SuppressWarnings("unused") Object tb,
296296
@Cached GetExceptionTracebackNode getExceptionTracebackNode) {
297-
PTraceback traceback = getExceptionTracebackNode.execute(self);
298-
return traceback == null ? PNone.NONE : traceback;
297+
return getExceptionTracebackNode.execute(self);
299298
}
300299

301300
@Specialization(guards = "!isNoValue(tb)")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/GetExceptionTracebackNode.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.exception;
4242

43+
import com.oracle.graal.python.builtins.objects.PNone;
4344
import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
4445
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
4546
import com.oracle.truffle.api.dsl.Cached;
4647
import com.oracle.truffle.api.dsl.GenerateUncached;
4748
import com.oracle.truffle.api.dsl.Specialization;
49+
import com.oracle.truffle.api.exception.AbstractTruffleException;
4850
import com.oracle.truffle.api.nodes.Node;
49-
import com.oracle.truffle.api.profiles.ConditionProfile;
5051

5152
/**
5253
* Use this node to get the traceback object of an exception object. The traceback may need to be
@@ -55,16 +56,21 @@
5556
@GenerateUncached
5657
public abstract class GetExceptionTracebackNode extends Node {
5758

58-
public abstract PTraceback execute(PBaseException e);
59+
public abstract Object execute(Object e);
5960

6061
@Specialization
61-
static PTraceback doExisting(PBaseException e,
62-
@Cached GetTracebackNode getTracebackNode,
63-
@Cached ConditionProfile nullProfile) {
64-
if (nullProfile.profile(e.getTraceback() == null)) {
65-
return null;
62+
static Object doExisting(PBaseException e,
63+
@Cached GetTracebackNode getTracebackNode) {
64+
PTraceback result = null;
65+
if (e.getTraceback() != null) {
66+
result = getTracebackNode.execute(e.getTraceback());
6667
}
67-
return getTracebackNode.execute(e.getTraceback());
68+
return result != null ? result : PNone.NONE;
69+
}
70+
71+
@Specialization
72+
static Object doForeign(@SuppressWarnings("unused") AbstractTruffleException e) {
73+
return PNone.NONE;
6874
}
6975

7076
public static GetExceptionTracebackNode create() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyTraceBackPrintNode.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
*/
4141
package com.oracle.graal.python.lib;
4242

43+
import static com.oracle.graal.python.builtins.modules.SysModuleBuiltins.MAXSIZE;
44+
import static com.oracle.graal.python.builtins.modules.io.IONodes.FLUSH;
45+
import static com.oracle.graal.python.builtins.modules.io.IONodes.WRITE;
46+
import static com.oracle.graal.python.nodes.BuiltinNames.TRACEBACKLIMIT;
47+
import static com.oracle.graal.python.util.PythonUtils.NEW_LINE;
48+
49+
import java.io.BufferedReader;
50+
import java.io.IOException;
51+
import java.nio.charset.Charset;
52+
import java.nio.charset.StandardCharsets;
53+
4354
import com.oracle.graal.python.PythonFileDetector;
4455
import com.oracle.graal.python.builtins.objects.PNone;
4556
import com.oracle.graal.python.builtins.objects.code.PCode;
@@ -69,17 +80,6 @@
6980
import com.oracle.truffle.api.dsl.Specialization;
7081
import com.oracle.truffle.api.frame.VirtualFrame;
7182

72-
import java.io.BufferedReader;
73-
import java.io.IOException;
74-
import java.nio.charset.Charset;
75-
import java.nio.charset.StandardCharsets;
76-
77-
import static com.oracle.graal.python.builtins.modules.SysModuleBuiltins.MAXSIZE;
78-
import static com.oracle.graal.python.builtins.modules.io.IONodes.FLUSH;
79-
import static com.oracle.graal.python.builtins.modules.io.IONodes.WRITE;
80-
import static com.oracle.graal.python.nodes.BuiltinNames.TRACEBACKLIMIT;
81-
import static com.oracle.graal.python.util.PythonUtils.NEW_LINE;
82-
8383
/**
8484
* Equivalent of {@code PyTraceBack_Print} from CPython. the node contains also a number of utility
8585
* static methods
@@ -190,7 +190,7 @@ public static Object getObjectClass(Object object) {
190190
return GetClassNode.getUncached().execute(object);
191191
}
192192

193-
public static PTraceback getExceptionTraceback(PBaseException e) {
193+
public static Object getExceptionTraceback(PBaseException e) {
194194
return GetExceptionTracebackNode.getUncached().execute(e);
195195
}
196196

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/WriteUnraisableNode.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ static void writeUnraisable(VirtualFrame frame, PBaseException exception, String
8686
Object unraisablehook = lookup.execute(frame, sysModule, BuiltinNames.UNRAISABLEHOOK);
8787
Object exceptionType = getClassNode.execute(exception);
8888
Object traceback = getExceptionTracebackNode.execute(exception);
89-
if (traceback == null) {
90-
traceback = PNone.NONE;
91-
}
9289
Object messageObj = PNone.NONE;
9390
if (message != null) {
9491
messageObj = formatMessage(message);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
5252
import com.oracle.graal.python.builtins.objects.function.PArguments;
5353
import com.oracle.graal.python.builtins.objects.module.PythonModule;
54-
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
5554
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
5655
import com.oracle.graal.python.lib.PyObjectStrAsObjectNode;
5756
import com.oracle.graal.python.nodes.BuiltinNames;
@@ -165,8 +164,7 @@ private PException handlePythonException(PBaseException pythonException) {
165164
}
166165
if (getContext().getOption(PythonOptions.AlwaysRunExcepthook)) {
167166
Object type = GetClassNode.getUncached().execute(pythonException);
168-
PTraceback tracebackOrNull = GetExceptionTracebackNode.getUncached().execute(pythonException);
169-
Object tb = tracebackOrNull != null ? tracebackOrNull : PNone.NONE;
167+
Object tb = GetExceptionTracebackNode.getUncached().execute(pythonException);
170168

171169
PythonModule sys = getContext().lookupBuiltinModule("sys");
172170
sys.setAttribute(BuiltinNames.LAST_TYPE, type);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/GeneratorWithNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ protected Object doBody(VirtualFrame frame, boolean isReturn) {
9999
}
100100

101101
@Override
102-
protected void handleException(VirtualFrame frame, Object withObject, Object exitCallable, PException pException) {
102+
protected boolean handleException(VirtualFrame frame, Object withObject, Object exitCallable, Object exceptionObject, PException exceptionState, PException chain) {
103103
reset(frame);
104-
super.handleException(frame, withObject, exitCallable, pException);
104+
return super.handleException(frame, withObject, exitCallable, exceptionObject, exceptionState, chain);
105105
}
106106

107107
@Override

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5454
import com.oracle.truffle.api.CompilerDirectives;
5555
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
56+
import com.oracle.truffle.api.exception.AbstractTruffleException;
5657
import com.oracle.truffle.api.frame.VirtualFrame;
58+
import com.oracle.truffle.api.interop.InteropLibrary;
5759
import com.oracle.truffle.api.nodes.ControlFlowException;
5860
import com.oracle.truffle.api.nodes.Node;
5961
import com.oracle.truffle.api.profiles.ConditionProfile;
@@ -64,6 +66,7 @@ public abstract class ExceptionHandlingStatementNode extends StatementNode {
6466
@Child private ExceptionStateNodes.RestoreExceptionStateNode restoreExceptionStateNode;
6567
@Child private ExceptionStateNodes.GetCaughtExceptionNode getCaughtExceptionNode;
6668
@Child private PythonObjectFactory ofactory;
69+
@Child private InteropLibrary lib;
6770
@CompilationFinal private LoopConditionProfile contextChainHandledProfile;
6871
@CompilationFinal private LoopConditionProfile contextChainContextProfile;
6972

@@ -168,6 +171,18 @@ protected final PythonObjectFactory factory() {
168171
return ofactory;
169172
}
170173

174+
private InteropLibrary getInteropLibrary() {
175+
if (lib == null) {
176+
CompilerDirectives.transferToInterpreterAndInvalidate();
177+
lib = insert(InteropLibrary.getFactory().createDispatched(3));
178+
}
179+
return lib;
180+
}
181+
182+
protected final boolean isTruffleException(Throwable t) {
183+
return getInteropLibrary().isException(t);
184+
}
185+
171186
protected final boolean shouldCatchAllExceptions() {
172187
return PythonLanguage.get(this).getEngineOption(PythonOptions.CatchAllExceptions);
173188
}
@@ -181,6 +196,10 @@ public static PException wrapJavaException(Throwable e, Node node, PBaseExceptio
181196
return pe;
182197
}
183198

199+
protected PException exceptionStateForTruffleException(AbstractTruffleException exception) {
200+
return wrapJavaException(exception, this, factory().createBaseException(SystemError, "%m", new Object[]{exception}));
201+
}
202+
184203
protected final PException wrapJavaExceptionIfApplicable(Throwable e) {
185204
if (e instanceof ControlFlowException) {
186205
return null;

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
*/
2626
package com.oracle.graal.python.nodes.statement;
2727

28-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
29-
3028
import java.util.ArrayList;
3129

3230
import com.oracle.graal.python.builtins.objects.PNone;
@@ -74,7 +72,6 @@ public class TryExceptNode extends ExceptionHandlingStatementNode implements Tru
7472
@Child private StatementNode body;
7573
@Children private final ExceptNode[] exceptNodes;
7674
@Child private StatementNode orelse;
77-
@Child private InteropLibrary excLib;
7875

7976
private final ConditionProfile everMatched = ConditionProfile.createBinaryProfile();
8077

@@ -116,11 +113,7 @@ private Object executeImpl(VirtualFrame frame, boolean isReturn) {
116113
// If we reach here, no explicit return could have happened as it would throw
117114
return PNone.NONE;
118115
} catch (AbstractTruffleException e) {
119-
if (excLib == null) {
120-
CompilerDirectives.transferToInterpreterAndInvalidate();
121-
excLib = insert(InteropLibrary.getFactory().createDispatched(3));
122-
}
123-
if (excLib.isException(e) && catchTruffleException(frame, e)) {
116+
if (isTruffleException(e) && catchTruffleException(frame, e)) {
124117
return PNone.NONE;
125118
}
126119
throw e;
@@ -180,31 +173,23 @@ private boolean catchTruffleException(VirtualFrame frame, AbstractTruffleExcepti
180173
try {
181174
for (ExceptNode exceptNode : exceptNodes) {
182175
if (everMatched.profile(exceptNode.matchesTruffleException(frame, exception))) {
183-
ExceptionState exceptionState = saveExceptionState(frame);
176+
ExceptionState savedState = saveExceptionState(frame);
184177
/*
185-
* In this case, we are catching not a Python exception. While the exception can
186-
* usually not be accessed by the user, she can at least re-raise it. So, we
178+
* In this case, we are not catching a Python exception. While the exception can
179+
* usually not be accessed by the user, they can at least re-raise it. So, we
187180
* need to wrap the exception into a Python exception.
188181
*/
189-
PException wrappedTruffleException = wrapJavaException(exception, this, factory().createBaseException(SystemError, "%m", new Object[]{exception}));
190-
SetCaughtExceptionNode.execute(frame, wrappedTruffleException);
182+
PException exceptionState = exceptionStateForTruffleException(exception);
183+
SetCaughtExceptionNode.execute(frame, exceptionState);
191184
try {
192185
exceptNode.executeExcept(frame, exception);
193186
} finally {
194-
restoreExceptionState(frame, exceptionState);
187+
restoreExceptionState(frame, savedState);
195188
}
196189
}
197190
}
198191
} catch (ExceptionHandledException eh) {
199192
return true;
200-
} catch (PException handlerException) {
201-
throw handlerException;
202-
} catch (Exception | StackOverflowError | AssertionError e) {
203-
PException handlerException = wrapJavaExceptionIfApplicable(e);
204-
if (handlerException == null) {
205-
throw e;
206-
}
207-
throw handlerException.getExceptionForReraise();
208193
}
209194
return false;
210195
}

0 commit comments

Comments
 (0)