82
82
import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiUnaryBuiltinNode ;
83
83
import com .oracle .graal .python .builtins .modules .cext .PythonCextFileBuiltins .PyFile_WriteObject ;
84
84
import com .oracle .graal .python .builtins .objects .PNone ;
85
+ import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .ClearCurrentExceptionNode ;
85
86
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .TransformExceptionToNativeNode ;
86
87
import com .oracle .graal .python .builtins .objects .cext .capi .PThreadState ;
87
88
import com .oracle .graal .python .builtins .objects .cext .common .NativePointer ;
111
112
import com .oracle .graal .python .nodes .WriteUnraisableNode ;
112
113
import com .oracle .graal .python .nodes .attributes .WriteAttributeToObjectNode ;
113
114
import com .oracle .graal .python .nodes .call .CallNode ;
114
- import com .oracle .graal .python .nodes .object .BuiltinClassProfiles ;
115
+ import com .oracle .graal .python .nodes .object .BuiltinClassProfiles . IsBuiltinObjectProfile ;
115
116
import com .oracle .graal .python .nodes .object .GetClassNode ;
116
117
import com .oracle .graal .python .nodes .util .ExceptionStateNodes .GetCaughtExceptionNode ;
117
118
import com .oracle .graal .python .runtime .PythonContext ;
121
122
import com .oracle .graal .python .runtime .exception .PException ;
122
123
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
123
124
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
125
+ import com .oracle .truffle .api .CompilerAsserts ;
124
126
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
125
127
import com .oracle .truffle .api .dsl .Bind ;
126
128
import com .oracle .truffle .api .dsl .Cached ;
@@ -142,16 +144,17 @@ abstract static class PyErr_Restore extends CApiTernaryBuiltinNode {
142
144
143
145
@ Specialization
144
146
Object restore (Object typ , Object val , Object tb ,
147
+ @ Bind ("this" ) Node inliningTarget ,
148
+ @ Cached GetThreadStateNode getThreadStateNode ,
145
149
@ Cached PrepareExceptionNode prepareExceptionNode ,
146
- @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ) {
147
- PythonLanguage language = getLanguage ();
148
- PythonContext .PythonThreadState threadState = getContext ().getThreadState (language );
150
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ,
151
+ @ Cached ClearCurrentExceptionNode clearCurrentExceptionNode ) {
149
152
if (typ == PNone .NO_VALUE && val == PNone .NO_VALUE ) {
150
- threadState . clearCurrentException ( );
153
+ clearCurrentExceptionNode . execute ( inliningTarget , getThreadStateNode . execute ( inliningTarget ) );
151
154
} else {
152
155
Object exception = prepareExceptionNode .execute (null , typ , val );
153
- PException e = PException .fromExceptionInfo (exception , PythonOptions .isPExceptionWithJavaStacktrace (language ));
154
- transformExceptionToNativeNode .execute (this , e , tb instanceof PTraceback ptb ? new LazyTraceback (ptb ) : null );
156
+ PException e = PException .fromExceptionInfo (exception , PythonOptions .isPExceptionWithJavaStacktrace (getLanguage () ));
157
+ transformExceptionToNativeNode .execute (inliningTarget , e , tb instanceof PTraceback ptb ? new LazyTraceback (ptb ) : null );
155
158
}
156
159
return PNone .NO_VALUE ;
157
160
}
@@ -194,7 +197,8 @@ Object run(
194
197
@ Cached GetThreadStateNode getThreadStateNode ,
195
198
@ Cached GetClassNode getClassNode ,
196
199
@ Cached MaterializeLazyTracebackNode materializeTraceback ,
197
- @ Cached PythonObjectFactory factory ) {
200
+ @ Cached PythonObjectFactory factory ,
201
+ @ Cached ClearCurrentExceptionNode clearCurrentExceptionNode ) {
198
202
PythonContext .PythonThreadState threadState = getThreadStateNode .execute (inliningTarget );
199
203
PException currentException = threadState .getCurrentException ();
200
204
Object result ;
@@ -210,36 +214,21 @@ Object run(
210
214
traceback = getNativeNull ();
211
215
}
212
216
result = factory .createTuple (new Object []{getClassNode .execute (inliningTarget , exception ), exception , traceback });
213
- threadState . clearCurrentException ( );
217
+ clearCurrentExceptionNode . execute ( inliningTarget , threadState );
214
218
}
215
219
return result ;
216
220
}
217
221
}
218
222
219
- @ CApiBuiltin (ret = PyObjectBorrowed , args = {}, call = Direct )
220
- abstract static class PyErr_Occurred extends CApiNullaryBuiltinNode {
221
- @ Specialization
222
- Object run (
223
- @ Bind ("this" ) Node inliningTarget ,
224
- @ Cached GetThreadStateNode getThreadStateNode ,
225
- @ Cached GetClassNode getClassNode ) {
226
- PException currentException = getThreadStateNode .execute (inliningTarget ).getCurrentException ();
227
- if (currentException != null ) {
228
- // getClassNode acts as a branch profile
229
- return getClassNode .execute (inliningTarget , currentException .getUnreifiedException ());
230
- }
231
- return getNativeNull ();
232
- }
233
- }
234
-
235
- @ CApiBuiltin (ret = PyObjectBorrowed , args = {PyThreadState }, call = Direct )
236
- abstract static class _PyErr_Occurred extends CApiUnaryBuiltinNode {
223
+ @ CApiBuiltin (ret = PyObjectBorrowed , args = {PyThreadState }, call = Ignored )
224
+ abstract static class _PyTruffleErr_Occurred extends CApiUnaryBuiltinNode {
237
225
@ Specialization
238
226
Object run (PThreadState state ,
239
227
@ Bind ("this" ) Node inliningTarget ,
240
228
@ Cached GetClassNode getClassNode ) {
241
229
PException currentException = state .getThreadState ().getCurrentException ();
242
230
if (currentException != null ) {
231
+ // getClassNode acts as a branch profile
243
232
return getClassNode .execute (inliningTarget , currentException .getUnreifiedException ());
244
233
}
245
234
return getNativeNull ();
@@ -423,7 +412,8 @@ abstract static class _PyErr_WriteUnraisableMsg extends CApiBinaryBuiltinNode {
423
412
static Object write (Object msg , Object obj ,
424
413
@ Bind ("this" ) Node inliningTarget ,
425
414
@ Cached GetThreadStateNode getThreadStateNode ,
426
- @ Cached WriteUnraisableNode writeUnraisableNode ) {
415
+ @ Cached WriteUnraisableNode writeUnraisableNode ,
416
+ @ Cached ClearCurrentExceptionNode clearCurrentExceptionNode ) {
427
417
PythonContext .PythonThreadState threadState = getThreadStateNode .execute (inliningTarget , PythonContext .get (inliningTarget ));
428
418
if (threadState .getCurrentException () == null ) {
429
419
// This means an invalid call, but this function is not supposed to raise exceptions
@@ -436,7 +426,7 @@ static Object write(Object msg, Object obj,
436
426
m = (TruffleString ) msg ;
437
427
}
438
428
writeUnraisableNode .execute (exc , m , (obj == PNone .NO_VALUE ) ? PNone .NONE : obj );
439
- threadState . clearCurrentException ( );
429
+ clearCurrentExceptionNode . execute ( inliningTarget , threadState );
440
430
return PNone .NONE ;
441
431
}
442
432
}
@@ -445,10 +435,8 @@ static Object write(Object msg, Object obj,
445
435
abstract static class PyErr_PrintEx extends CApiUnaryBuiltinNode {
446
436
@ TruffleBoundary
447
437
@ Specialization
448
- Object raise (int set_sys_last_vars ,
449
- @ Bind ("this" ) Node inliningTarget ,
450
- @ Cached BuiltinClassProfiles .IsBuiltinObjectProfile exceptionProfile ,
451
- @ Cached PyErr_Occurred errOccuredNode ,
438
+ static Object raise (int set_sys_last_vars ,
439
+ @ Cached _PyTruffleErr_Occurred errOccuredNode ,
452
440
@ Cached TupleBuiltins .GetItemNode getItemNode ,
453
441
@ Cached IsInstanceNode isInstanceNode ,
454
442
@ Cached ExcInfoNode excInfoNode ,
@@ -457,11 +445,12 @@ Object raise(int set_sys_last_vars,
457
445
@ Cached ExitNode exitNode ,
458
446
@ Cached PyTruffleErr_Fetch fetchNode ,
459
447
@ Cached PyErr_Display errDisplayNode ) {
460
- NativePointer nativeNull = getNativeNull ();
448
+ PythonContext context = PythonContext .get (null );
449
+ NativePointer nativeNull = context .getNativeNull ();
461
450
462
- Object err = errOccuredNode .execute ();
463
- PythonModule sys = getCore () .getSysModule ();
464
- if (err != nativeNull && exceptionProfile . profileObject ( inliningTarget , err , PythonBuiltinClassType .SystemExit )) {
451
+ Object err = errOccuredNode .execute (context . getThreadState ( PythonLanguage . get ( null )) );
452
+ PythonModule sys = context .getSysModule ();
453
+ if (err != nativeNull && IsBuiltinObjectProfile . profileObjectUncached ( err , PythonBuiltinClassType .SystemExit )) {
465
454
handleSystemExit (excInfoNode , getItemNode , isInstanceNode , restoreNode , (SysModuleBuiltins ) sys .getBuiltins (), writeFileNode , exitNode );
466
455
}
467
456
Object fetched = fetchNode .execute (null );
@@ -495,8 +484,8 @@ Object raise(int set_sys_last_vars,
495
484
return PNone .NONE ;
496
485
}
497
486
498
- @ TruffleBoundary
499
487
private static void writeLastVars (PythonModule sys , Object type , Object val , Object tb , PyErr_Restore restoreNode ) {
488
+ CompilerAsserts .neverPartOfCompilation ();
500
489
try {
501
490
WriteAttributeToObjectNode .getUncached ().execute (sys , T_LAST_TYPE , type );
502
491
WriteAttributeToObjectNode .getUncached ().execute (sys , T_LAST_VALUE , val );
@@ -506,9 +495,9 @@ private static void writeLastVars(PythonModule sys, Object type, Object val, Obj
506
495
}
507
496
}
508
497
509
- @ TruffleBoundary
510
498
private static void handleExceptHook (Object exceptHook , Object type , Object val , Object tb , ExcInfoNode excInfoNode ,
511
499
GetItemNode getItemNode , PythonModule sys , PyErr_Display errDisplayNode ) {
500
+ CompilerAsserts .neverPartOfCompilation ();
512
501
try {
513
502
CallNode .getUncached ().execute (exceptHook , type , val , tb );
514
503
} catch (PException e ) {
@@ -527,9 +516,9 @@ private static void handleExceptHook(Object exceptHook, Object type, Object val,
527
516
}
528
517
}
529
518
530
- @ TruffleBoundary
531
519
private static void handleSystemExit (ExcInfoNode excInfoNode , TupleBuiltins .GetItemNode getItemNode , IsInstanceNode isInstanceNode ,
532
520
PyErr_Restore restoreNode , SysModuleBuiltins sys , PyFile_WriteObject writeFileNode , ExitNode exitNode ) {
521
+ CompilerAsserts .neverPartOfCompilation ();
533
522
PTuple sysInfo = excInfoNode .execute (null );
534
523
int rc = 0 ;
535
524
Object returnObject = null ;
0 commit comments