Skip to content

Instantly share code, notes, and snippets.

@Elizaveta239
Created December 18, 2016 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elizaveta239/1805506fe9e27b202f9a0488e0923887 to your computer and use it in GitHub Desktop.
Save Elizaveta239/1805506fe9e27b202f9a0488e0923887 to your computer and use it in GitHub Desktop.
import sys
class Debugger(object):
def __init__(self):
breakpoints = {}
def call_trace(self):
frame = sys._getframe(1)
print(frame.f_code.co_filename, frame.f_lineno, frame.f_locals)
import dis
from types import CodeType
from debug_info import Debugger
debugger = Debugger()
def foo():
a = 1
b = 2
c = 3
d = 4
def trace():
debugger.call_trace()
instruction_for_line = 4
code = foo.__code__
x = code.co_code[0:instruction_for_line]
code2 = trace.__code__
new_bytes = x + code2.co_code + code.co_code[instruction_for_line:]
new_code = CodeType(
code.co_argcount, # integer
code.co_kwonlyargcount, # integer
code.co_nlocals, # integer
code.co_stacksize, # integer
code.co_flags, # integer
new_bytes, # bytes
code.co_consts, # tuple
code.co_names + code2.co_names, # tuple
code.co_varnames, # tuple
code.co_filename, # string
code.co_name, # string
code.co_firstlineno, # integer
code.co_lnotab, # bytes
code.co_freevars, # tuple
code.co_cellvars # tuple
)
print("New code:")
dis.dis(new_code)
exec(new_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment