55
55
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
56
56
import com .oracle .graal .python .builtins .PythonBuiltins ;
57
57
import com .oracle .graal .python .builtins .objects .PNone ;
58
+ import com .oracle .graal .python .builtins .objects .exception .ExceptionNodes ;
58
59
import com .oracle .graal .python .builtins .objects .exception .PBaseException ;
59
60
import com .oracle .graal .python .builtins .objects .exception .PrepareExceptionNode ;
60
61
import com .oracle .graal .python .builtins .objects .frame .PFrame ;
61
62
import com .oracle .graal .python .builtins .objects .function .PArguments ;
62
- import com .oracle .graal .python .builtins .objects .traceback .MaterializeLazyTracebackNode ;
63
63
import com .oracle .graal .python .builtins .objects .traceback .PTraceback ;
64
64
import com .oracle .graal .python .nodes .ErrorMessages ;
65
65
import com .oracle .graal .python .nodes .PGuards ;
80
80
import com .oracle .graal .python .runtime .exception .PException ;
81
81
import com .oracle .graal .python .util .PythonUtils ;
82
82
import com .oracle .truffle .api .CallTarget ;
83
- import com .oracle .truffle .api .CompilerDirectives ;
84
83
import com .oracle .truffle .api .RootCallTarget ;
85
84
import com .oracle .truffle .api .dsl .Bind ;
86
85
import com .oracle .truffle .api .dsl .Cached ;
@@ -274,38 +273,38 @@ Object send(VirtualFrame frame, PGenerator self, Object value,
274
273
@ GenerateNodeFactory
275
274
public abstract static class ThrowNode extends PythonQuaternaryBuiltinNode {
276
275
277
- @ Child private MaterializeFrameNode materializeFrameNode ;
278
- @ Child private MaterializeLazyTracebackNode materializeLazyTracebackNode ;
279
-
280
276
@ Specialization
281
- Object sendThrow (VirtualFrame frame , PGenerator self , Object typ , Object val , @ SuppressWarnings ("unused" ) PNone tb ,
277
+ Object sendThrow (VirtualFrame frame , PGenerator self , Object typ , Object val , Object tb ,
278
+ @ Bind ("this" ) Node inliningTarget ,
279
+ @ Cached InlinedConditionProfile hasTbProfile ,
280
+ @ Cached InlinedConditionProfile startedProfile ,
281
+ @ Cached InlinedBranchProfile invalidTbProfile ,
282
+ @ Cached InlinedBranchProfile runningProfile ,
282
283
@ Cached PrepareExceptionNode prepareExceptionNode ,
283
- @ Cached ResumeGeneratorNode resumeGeneratorNode ) {
284
- if (self .isRunning ()) {
285
- throw raise (ValueError , ErrorMessages .GENERATOR_ALREADY_EXECUTING );
284
+ @ Cached ResumeGeneratorNode resumeGeneratorNode ,
285
+ @ Cached ExceptionNodes .GetTracebackNode getTracebackNode ,
286
+ @ Cached ExceptionNodes .SetTracebackNode setTracebackNode ,
287
+ @ Cached ExceptionNodes .SetContextNode setContextNode ) {
288
+ boolean hasTb = hasTbProfile .profile (inliningTarget , !(tb instanceof PNone ));
289
+ if (hasTb && !(tb instanceof PTraceback )) {
290
+ invalidTbProfile .enter (inliningTarget );
291
+ throw raise (TypeError , ErrorMessages .THROW_THIRD_ARG_MUST_BE_TRACEBACK );
286
292
}
287
- PBaseException instance = prepareExceptionNode .execute (frame , typ , val );
288
- return doThrow (frame , resumeGeneratorNode , self , instance , getLanguage ());
289
- }
290
-
291
- @ Specialization
292
- Object sendThrow (VirtualFrame frame , PGenerator self , Object typ , Object val , PTraceback tb ,
293
- @ Cached PrepareExceptionNode prepareExceptionNode ,
294
- @ Cached ResumeGeneratorNode resumeGeneratorNode ) {
295
293
if (self .isRunning ()) {
294
+ runningProfile .enter (inliningTarget );
296
295
throw raise (ValueError , ErrorMessages .GENERATOR_ALREADY_EXECUTING );
297
296
}
298
297
PBaseException instance = prepareExceptionNode .execute (frame , typ , val );
299
- instance . setTraceback ( tb );
300
- return doThrow ( frame , resumeGeneratorNode , self , instance , getLanguage () );
301
- }
302
-
303
- private Object doThrow ( VirtualFrame frame , ResumeGeneratorNode resumeGeneratorNode , PGenerator self , PBaseException instance , PythonLanguage language ) {
304
- instance . setContext ( null ); // Will be filled when caught
298
+ if ( hasTb ) {
299
+ setTracebackNode . execute ( inliningTarget , instance , tb );
300
+ }
301
+ PythonLanguage language = getLanguage ();
302
+ setContextNode . execute ( inliningTarget , instance , PNone . NONE ); // Will be filled when
303
+ // caught
305
304
if (self .isCoroutine () && self .isFinished ()) {
306
305
throw raise (PythonBuiltinClassType .RuntimeError , ErrorMessages .CANNOT_REUSE_CORO );
307
306
}
308
- if (self .isStarted () && !self .isFinished ()) {
307
+ if (startedProfile . profile ( inliningTarget , self .isStarted () && !self .isFinished () )) {
309
308
instance .ensureReified ();
310
309
// Pass it to the generator where it will be thrown by the last yield, the location
311
310
// will be filled there
@@ -321,37 +320,12 @@ private Object doThrow(VirtualFrame frame, ResumeGeneratorNode resumeGeneratorNo
321
320
PFrame pFrame = MaterializeFrameNode .materializeGeneratorFrame (location , generatorFrame , PFrame .Reference .EMPTY , factory ());
322
321
FrameInfo info = (FrameInfo ) generatorFrame .getFrameDescriptor ().getInfo ();
323
322
pFrame .setLine (info .getRootNode ().getFirstLineno ());
324
- PTraceback existingTraceback = null ;
325
- if (instance .getTraceback () != null ) {
326
- existingTraceback = ensureGetTracebackNode ().execute (instance .getTraceback ());
327
- }
328
- PTraceback newTraceback = factory ().createTraceback (pFrame , pFrame .getLine (), existingTraceback );
329
- instance .setTraceback (newTraceback );
323
+ Object existingTracebackObj = getTracebackNode .execute (inliningTarget , instance );
324
+ PTraceback newTraceback = factory ().createTraceback (pFrame , pFrame .getLine (), (existingTracebackObj instanceof PTraceback existingTraceback ) ? existingTraceback : null );
325
+ setTracebackNode .execute (inliningTarget , instance , newTraceback );
330
326
throw PException .fromObject (instance , location , PythonOptions .isPExceptionWithJavaStacktrace (language ));
331
327
}
332
328
}
333
-
334
- @ Specialization (guards = {"!isPNone(tb)" , "!isPTraceback(tb)" })
335
- @ SuppressWarnings ("unused" )
336
- Object doError (VirtualFrame frame , PGenerator self , Object typ , Object val , Object tb ) {
337
- throw raise (TypeError , ErrorMessages .THROW_THIRD_ARG_MUST_BE_TRACEBACK );
338
- }
339
-
340
- private MaterializeFrameNode ensureMaterializeFrameNode () {
341
- if (materializeFrameNode == null ) {
342
- CompilerDirectives .transferToInterpreterAndInvalidate ();
343
- materializeFrameNode = insert (MaterializeFrameNode .create ());
344
- }
345
- return materializeFrameNode ;
346
- }
347
-
348
- private MaterializeLazyTracebackNode ensureGetTracebackNode () {
349
- if (materializeLazyTracebackNode == null ) {
350
- CompilerDirectives .transferToInterpreterAndInvalidate ();
351
- materializeLazyTracebackNode = insert (MaterializeLazyTracebackNode .create ());
352
- }
353
- return materializeLazyTracebackNode ;
354
- }
355
329
}
356
330
357
331
@ Builtin (name = "close" , minNumOfPositionalArgs = 1 )
0 commit comments