Guest User

Untitled

a guest
Jul 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. Assertion error in file 'assertion_hook.py' at line 46:
  2.  
  3. assert(j < i)
  4.  
  5. Variables at this point:
  6. {'i': 5, 'j': 5}
  7.  
  8. #!/usr/bin/python
  9. # -*- coding: utf-8 -*-
  10. import sys
  11. import os.path
  12. import pprint
  13.  
  14. sys._old_excepthook = sys.excepthook
  15.  
  16. def assert_hook(exc_type, exception, traceback):
  17. if exc_type.__name__ == 'AssertionError':
  18. try:
  19.  
  20. frame = traceback.tb_frame
  21. local = frame.f_locals
  22. line = frame.f_lineno
  23. codefile = frame.f_code.co_filename
  24.  
  25. print "Assertion error in file '%s' at line %d:n"
  26. % (codefile, line)
  27.  
  28. with open(codefile) as f:
  29. statement = f.readlines()[line - 1]
  30. print statement
  31.  
  32. code = compile(statement.strip(), codefile, 'single')
  33. names_at_line = filter(lambda name: name
  34. != 'AssertionError', code.co_names)
  35. names_and_vars = dict((name, local[name]) for name in
  36. names_at_line)
  37.  
  38. print 'Variables at this point:'
  39. pprint.pprint(names_and_vars)
  40. except:
  41. sys._old_excepthook(exc_type, exception, traceback)
  42. else:
  43. sys._old_excepthook(exc_type, exception, traceback)
  44.  
  45. sys.excepthook = assert_hook
  46.  
  47. if __name__ == '__main__':
  48. i = 5
  49. for j in range(10):
  50. assert j < i
  51. print j
  52.  
  53. local = frame.f_locals
  54.  
  55. frame_locals = frame.f_locals
  56.  
  57. isinstance(e, AssertionError)
  58.  
  59. except:
  60.  
  61. except Exception:
  62.  
  63. def b():
  64. i,j=5,10
  65. assert i==j
  66.  
  67. if __name__=='__main__':
  68. b()
  69.  
  70. Assertion error in file 'aser.py' at line 45:
  71.  
  72. b()
  73.  
  74. Variables at this point:
  75. {'b': <function b at 0xb77a66f4>}
Add Comment
Please, Sign In to add comment