Skip to content

Instantly share code, notes, and snippets.

@akaptur
Created August 4, 2016 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akaptur/0bddb84b53bd4299b84eb736dd126636 to your computer and use it in GitHub Desktop.
Save akaptur/0bddb84b53bd4299b84eb736dd126636 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