Skip to content

Commit f9ac739

Browse files
committed
Use PrepareExceptionNode in PyErr_Restore
1 parent 759e92e commit f9ac739

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextErrBuiltins.java

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
109109
import com.oracle.graal.python.nodes.call.CallNode;
110110
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
111-
import com.oracle.graal.python.nodes.exception.ValidExceptionNode;
112111
import com.oracle.graal.python.nodes.object.GetClassNode;
113112
import com.oracle.graal.python.nodes.object.IsNode;
114113
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.GetCaughtExceptionNode;
@@ -118,7 +117,6 @@
118117
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
119118
import com.oracle.graal.python.runtime.exception.PException;
120119
import com.oracle.graal.python.runtime.exception.PythonErrorType;
121-
import com.oracle.truffle.api.CompilerDirectives;
122120
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
123121
import com.oracle.truffle.api.dsl.Cached;
124122
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -133,47 +131,31 @@ public final class PythonCextErrBuiltins {
133131

134132
@CApiBuiltin(ret = Void, args = {PyObject, PyObject, PyObject}, call = Direct)
135133
abstract static class PyErr_Restore extends CApiTernaryBuiltinNode {
136-
@Specialization
134+
135+
@Specialization(guards = {"isNoValue(typ)", "isNoValue(val)"})
137136
@SuppressWarnings("unused")
138-
Object run(PNone typ, PNone val, PNone tb) {
137+
Object restore(PNone typ, PNone val, Object tb) {
139138
getContext().setCurrentException(getLanguage(), null);
140-
return PNone.NONE;
141-
}
142-
143-
@Specialization
144-
Object run(@SuppressWarnings("unused") Object typ, PBaseException val, @SuppressWarnings("unused") PNone tb) {
145-
PythonContext context = getContext();
146-
PythonLanguage language = getLanguage();
147-
context.setCurrentException(language, PException.fromExceptionInfo(val, (LazyTraceback) null, PythonOptions.isPExceptionWithJavaStacktrace(language)));
148-
return PNone.NONE;
139+
return PNone.NO_VALUE;
149140
}
150141

151-
@Specialization
152-
Object run(@SuppressWarnings("unused") Object typ, PBaseException val, PTraceback tb) {
142+
@Fallback
143+
Object restore(Object typ, Object val, Object tb,
144+
@Cached PrepareExceptionNode prepareExceptionNode) {
153145
PythonContext context = getContext();
154146
PythonLanguage language = getLanguage();
155-
context.setCurrentException(language, PException.fromExceptionInfo(val, tb, PythonOptions.isPExceptionWithJavaStacktrace(language)));
156-
return PNone.NONE;
157-
}
158-
159-
@Specialization(guards = "!isPBaseException(value)")
160-
Object createAndSet(Object type, Object value, @SuppressWarnings("unused") PNone tb,
161-
@Cached ValidExceptionNode isExcNode,
162-
@Cached CallNode callConstructor) {
163-
if (isExcNode.execute(null, type)) {
164-
PBaseException ex = (PBaseException) callConstructor.execute(type, value);
165-
PythonLanguage language = PythonLanguage.get(this);
166-
PythonContext.get(this).setCurrentException(language,
167-
PException.fromExceptionInfo(ex, (LazyTraceback) null, PythonOptions.isPExceptionWithJavaStacktrace(language)));
168-
return PNone.NONE;
169-
} else {
170-
// CPython just saves all three values, only ensuring that tb is either NULL or a
171-
// PyTraceBack. Throwing here would be incorrect, since it's valid to set
172-
// "unnormalized" exception values and use PyErr_NormalizeException later. And even
173-
// that just keeps going when the type is not an exception. Not sure what the use
174-
// cases are for that, so for now we ignore it.
175-
throw CompilerDirectives.shouldNotReachHere("unnormalized exceptions cannot be stored");
147+
PBaseException exception;
148+
try {
149+
exception = prepareExceptionNode.execute(null, typ, val);
150+
} catch (PException e) {
151+
context.setCurrentException(language, e);
152+
return PNone.NO_VALUE;
176153
}
154+
if (tb instanceof PTraceback pTraceback) {
155+
exception.setTraceback(pTraceback);
156+
}
157+
context.setCurrentException(language, PException.fromExceptionInfo(exception, (LazyTraceback) null, PythonOptions.isPExceptionWithJavaStacktrace(language)));
158+
return PNone.NO_VALUE;
177159
}
178160
}
179161

0 commit comments

Comments
 (0)