code_junkie

How to detect that Python code is being executed through the debugger?

Nov 12th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. $ USING_PDB=1 pdb yourprog.py
  2.  
  3. import os
  4. if os.environ.get('USING_PDB'):
  5. # debugging actions
  6. pass
  7.  
  8. import inspect
  9.  
  10. def isdebugging():
  11. for frame in inspect.stack():
  12. if frame[1].endswith("pydevd.py"):
  13. return True
  14. return False
  15.  
  16. import sys
  17. if 'pydevd' in sys.modules:
  18. print "Debugger"
  19. else:
  20. print "commandline"
  21.  
  22. __debug__
  23. This constant is true if Python was not started with an -O option.
  24.  
  25. if __debug__:
  26. print 'Python started without optimization'
Add Comment
Please, Sign In to add comment