@@ -430,7 +430,8 @@ def get_instructions(x, *, first_line=None, show_caches=False, adaptive=False):
430430 co .co_names , co .co_consts ,
431431 linestarts , line_offset ,
432432 co_positions = co .co_positions (),
433- show_caches = show_caches )
433+ show_caches = show_caches ,
434+ original_code = co .co_code )
434435
435436def _get_const_value (op , arg , co_consts ):
436437 """Helper to get the value of the const in a hasconst op.
@@ -504,7 +505,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
504505 names = None , co_consts = None ,
505506 linestarts = None , line_offset = 0 ,
506507 exception_entries = (), co_positions = None ,
507- show_caches = False ):
508+ show_caches = False , original_code = None ):
508509 """Iterate over the instructions in a bytecode string.
509510
510511 Generates a sequence of Instruction namedtuples giving the details of each
@@ -513,14 +514,18 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
513514 arguments.
514515
515516 """
517+ # Use the basic, unadaptive code for finding labels and actually walking the
518+ # bytecode, since replacements like ENTER_EXECUTOR and INSTRUMENTED_* can
519+ # mess that logic up pretty badly:
520+ original_code = original_code or code
516521 co_positions = co_positions or iter (())
517522 get_name = None if names is None else names .__getitem__
518- labels = set (findlabels (code ))
523+ labels = set (findlabels (original_code ))
519524 for start , end , target , _ , _ in exception_entries :
520525 for i in range (start , end ):
521526 labels .add (target )
522527 starts_line = None
523- for offset , start_offset , op , arg in _unpack_opargs (code ):
528+ for offset , start_offset , op , arg in _unpack_opargs (original_code ):
524529 if linestarts is not None :
525530 starts_line = linestarts .get (offset , None )
526531 if starts_line is not None :
@@ -531,6 +536,7 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
531536 positions = Positions (* next (co_positions , ()))
532537 deop = _deoptop (op )
533538 caches = _inline_cache_entries [deop ]
539+ op = code [offset ]
534540 if arg is not None :
535541 # Set argval to the dereferenced value of the argument when
536542 # available, and argrepr to the string representation of argval.
@@ -591,7 +597,6 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
591597 yield Instruction (_all_opname [op ], op ,
592598 arg , argval , argrepr ,
593599 offset , start_offset , starts_line , is_jump_target , positions )
594- caches = _inline_cache_entries [deop ]
595600 if not caches :
596601 continue
597602 if not show_caches :
@@ -622,7 +627,8 @@ def disassemble(co, lasti=-1, *, file=None, show_caches=False, adaptive=False):
622627 lasti , co ._varname_from_oparg ,
623628 co .co_names , co .co_consts , linestarts , file = file ,
624629 exception_entries = exception_entries ,
625- co_positions = co .co_positions (), show_caches = show_caches )
630+ co_positions = co .co_positions (), show_caches = show_caches ,
631+ original_code = co .co_code )
626632
627633def _disassemble_recursive (co , * , file = None , depth = None , show_caches = False , adaptive = False ):
628634 disassemble (co , file = file , show_caches = show_caches , adaptive = adaptive )
@@ -640,7 +646,7 @@ def _disassemble_recursive(co, *, file=None, depth=None, show_caches=False, adap
640646def _disassemble_bytes (code , lasti = - 1 , varname_from_oparg = None ,
641647 names = None , co_consts = None , linestarts = None ,
642648 * , file = None , line_offset = 0 , exception_entries = (),
643- co_positions = None , show_caches = False ):
649+ co_positions = None , show_caches = False , original_code = None ):
644650 # Omit the line number column entirely if we have no line number info
645651 show_lineno = bool (linestarts )
646652 if show_lineno :
@@ -661,7 +667,8 @@ def _disassemble_bytes(code, lasti=-1, varname_from_oparg=None,
661667 line_offset = line_offset ,
662668 exception_entries = exception_entries ,
663669 co_positions = co_positions ,
664- show_caches = show_caches ):
670+ show_caches = show_caches ,
671+ original_code = original_code ):
665672 new_source_line = (show_lineno and
666673 instr .starts_line is not None and
667674 instr .offset > 0 )
@@ -823,7 +830,8 @@ def __iter__(self):
823830 line_offset = self ._line_offset ,
824831 exception_entries = self .exception_entries ,
825832 co_positions = co .co_positions (),
826- show_caches = self .show_caches )
833+ show_caches = self .show_caches ,
834+ original_code = co .co_code )
827835
828836 def __repr__ (self ):
829837 return "{}({!r})" .format (self .__class__ .__name__ ,
@@ -859,7 +867,8 @@ def dis(self):
859867 lasti = offset ,
860868 exception_entries = self .exception_entries ,
861869 co_positions = co .co_positions (),
862- show_caches = self .show_caches )
870+ show_caches = self .show_caches ,
871+ original_code = co .co_code )
863872 return output .getvalue ()
864873
865874
0 commit comments