Skip to content

Instantly share code, notes, and snippets.

@JohnCoates
Created August 21, 2017 03:44
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 JohnCoates/a6bc413626df10a224a292f724d81bbd to your computer and use it in GitHub Desktop.
Save JohnCoates/a6bc413626df10a224a292f724d81bbd to your computer and use it in GitHub Desktop.
Hook Python function calls for debugging
import subprocess
import os
def hook(hookfunc, oldfunc):
def foo(*args, **kwargs):
hookfunc(*args, **kwargs)
return oldfunc(*args, **kwargs)
return foo
def printFunction(*args, **kwargs):
command = args[0]
print(command)
def printCD(*args, **kwargs):
path = args[0]
print("cd " + path)
subprocess.call = hook(printFunction, subprocess.call)
os.chdir = hook(printCD, os.chdir)
@JohnCoates
Copy link
Author

I used this to figure out what a python build script was running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment