108
108
import com .oracle .graal .python .nodes .attributes .WriteAttributeToObjectNode ;
109
109
import com .oracle .graal .python .nodes .call .CallNode ;
110
110
import com .oracle .graal .python .nodes .classes .IsSubtypeNode ;
111
- import com .oracle .graal .python .nodes .exception .ValidExceptionNode ;
112
111
import com .oracle .graal .python .nodes .object .GetClassNode ;
113
112
import com .oracle .graal .python .nodes .object .IsNode ;
114
113
import com .oracle .graal .python .nodes .util .ExceptionStateNodes .GetCaughtExceptionNode ;
118
117
import com .oracle .graal .python .runtime .exception .ExceptionUtils ;
119
118
import com .oracle .graal .python .runtime .exception .PException ;
120
119
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
121
- import com .oracle .truffle .api .CompilerDirectives ;
122
120
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
123
121
import com .oracle .truffle .api .dsl .Cached ;
124
122
import com .oracle .truffle .api .dsl .Cached .Shared ;
@@ -133,47 +131,31 @@ public final class PythonCextErrBuiltins {
133
131
134
132
@ CApiBuiltin (ret = Void , args = {PyObject , PyObject , PyObject }, call = Direct )
135
133
abstract static class PyErr_Restore extends CApiTernaryBuiltinNode {
136
- @ Specialization
134
+
135
+ @ Specialization (guards = {"isNoValue(typ)" , "isNoValue(val)" })
137
136
@ SuppressWarnings ("unused" )
138
- Object run (PNone typ , PNone val , PNone tb ) {
137
+ Object restore (PNone typ , PNone val , Object tb ) {
139
138
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 ;
149
140
}
150
141
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 ) {
153
145
PythonContext context = getContext ();
154
146
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 ;
176
153
}
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 ;
177
159
}
178
160
}
179
161
0 commit comments