@@ -333,6 +333,68 @@ def _fstring(a, b, c, d):
333333 28 RETURN_VALUE
334334""" % (_fstring .__code__ .co_firstlineno + 1 ,)
335335
336+ def _tryfinally (a , b ):
337+ try :
338+ return a
339+ finally :
340+ b ()
341+
342+ def _tryfinallyconst (b ):
343+ try :
344+ return 1
345+ finally :
346+ b ()
347+
348+ dis_tryfinally = """\
349+ %3d 0 SETUP_FINALLY 12 (to 14)
350+
351+ %3d 2 LOAD_FAST 0 (a)
352+ 4 POP_BLOCK
353+
354+ %3d 6 LOAD_FAST 1 (b)
355+ 8 CALL_FUNCTION 0
356+ 10 POP_TOP
357+
358+ %3d 12 RETURN_VALUE
359+
360+ %3d >> 14 LOAD_FAST 1 (b)
361+ 16 CALL_FUNCTION 0
362+ 18 POP_TOP
363+ 20 RERAISE
364+ 22 LOAD_CONST 0 (None)
365+ 24 RETURN_VALUE
366+ """ % (_tryfinally .__code__ .co_firstlineno + 1 ,
367+ _tryfinally .__code__ .co_firstlineno + 2 ,
368+ _tryfinally .__code__ .co_firstlineno + 4 ,
369+ _tryfinally .__code__ .co_firstlineno + 2 ,
370+ _tryfinally .__code__ .co_firstlineno + 4 ,
371+ )
372+
373+ dis_tryfinallyconst = """\
374+ %3d 0 SETUP_FINALLY 12 (to 14)
375+
376+ %3d 2 POP_BLOCK
377+
378+ %3d 4 LOAD_FAST 0 (b)
379+ 6 CALL_FUNCTION 0
380+ 8 POP_TOP
381+
382+ %3d 10 LOAD_CONST 1 (1)
383+ 12 RETURN_VALUE
384+
385+ %3d >> 14 LOAD_FAST 0 (b)
386+ 16 CALL_FUNCTION 0
387+ 18 POP_TOP
388+ 20 RERAISE
389+ 22 LOAD_CONST 0 (None)
390+ 24 RETURN_VALUE
391+ """ % (_tryfinallyconst .__code__ .co_firstlineno + 1 ,
392+ _tryfinallyconst .__code__ .co_firstlineno + 2 ,
393+ _tryfinallyconst .__code__ .co_firstlineno + 4 ,
394+ _tryfinallyconst .__code__ .co_firstlineno + 2 ,
395+ _tryfinallyconst .__code__ .co_firstlineno + 4 ,
396+ )
397+
336398def _g (x ):
337399 yield x
338400
@@ -563,6 +625,10 @@ def test_disassemble_coroutine(self):
563625 def test_disassemble_fstring (self ):
564626 self .do_disassembly_test (_fstring , dis_fstring )
565627
628+ def test_disassemble_try_finally (self ):
629+ self .do_disassembly_test (_tryfinally , dis_tryfinally )
630+ self .do_disassembly_test (_tryfinallyconst , dis_tryfinallyconst )
631+
566632 def test_dis_none (self ):
567633 try :
568634 del sys .last_traceback
0 commit comments