Skip to content

Instantly share code, notes, and snippets.

@letroot
Forked from akaptur/sys.settrace example
Created August 4, 2016 18:49
Show Gist options
  • Save letroot/891de0ee375f130642aefacd9ffcb805 to your computer and use it in GitHub Desktop.
Save letroot/891de0ee375f130642aefacd9ffcb805 to your computer and use it in GitHub Desktop.
>>> import sys
>>> def stuff():
... print("calling stuff!")
...
>>> def printer(frame, event, arg):
... print(frame, event, arg)
... return printer # return itself to keep tracing
...
>>> sys.settrace(printer) # register the tracing function
>>> stuff()
(<frame object at 0x10d3625c0>, 'call', None)
(<frame object at 0x10d3625c0>, 'line', None)
(<frame object at 0x10d3625c0>, 'return', (u'stuff()\n', 8))
(<frame object at 0x10d259050>, 'call', None)
(<frame object at 0x10d259050>, 'line', None)
(<frame object at 0x10d259898>, 'call', None)
(<frame object at 0x10d259898>, 'line', None)
calling stuff!
(<frame object at 0x10d259898>, 'return', None)
(<frame object at 0x10d259050>, 'return', None)
>>> sys.settrace(None) # turn it off
(<frame object at 0x10d3625c0>, 'call', None)
(<frame object at 0x10d3625c0>, 'line', None)
(<frame object at 0x10d3625c0>, 'return', (u'sys.settrace(None) # turn it off\n', 34))
(<frame object at 0x10d351a70>, 'call', None)
(<frame object at 0x10d351a70>, 'line', None)
>>> print('hi') # tracing is done
hi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment