[issue36420] f_trace_opcodes setting and accessing opcodes

anthony shaw report at bugs.python.org
Sun Mar 24 22:09:18 EDT 2019


anthony shaw <anthony.p.shaw at gmail.com> added the comment:

Took a while, but I worked out a solution:

import sys
import dis
import traceback
import io

def t(frame, event, args):
   frame.f_trace_opcodes=True
   stack = traceback.extract_stack(frame)
   pad = "   "*len(stack) + "|"
   if event == 'opcode':
      with io.StringIO() as out:
         dis.disco(frame.f_code, frame.f_lasti, file=out)
         lines = out.getvalue().split('\n')
         [print(f"{pad}{l}") for l in lines]
   elif event == 'call':
      print(f"{pad}Calling {frame.f_code}")
   elif event == 'return':
      print(f"{pad}Returning {args}")
   elif event == 'line':
      print(f"{pad}Changing line to {frame.f_lineno}")
   else:
      print(f"{pad}{frame} ({event} - {args})")
   print(f"{pad}----------------------------------")
   return t
sys.settrace(t)
eval('"-".join([letter for letter in "hello"])')

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36420>
_______________________________________


More information about the Python-bugs-list mailing list