Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Assertion error in file 'assertion_hook.py' at line 46:
- assert(j < i)
- Variables at this point:
- {'i': 5, 'j': 5}
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import sys
- import os.path
- import pprint
- sys._old_excepthook = sys.excepthook
- def assert_hook(exc_type, exception, traceback):
- if exc_type.__name__ == 'AssertionError':
- try:
- frame = traceback.tb_frame
- local = frame.f_locals
- line = frame.f_lineno
- codefile = frame.f_code.co_filename
- print "Assertion error in file '%s' at line %d:n"
- % (codefile, line)
- with open(codefile) as f:
- statement = f.readlines()[line - 1]
- print statement
- code = compile(statement.strip(), codefile, 'single')
- names_at_line = filter(lambda name: name
- != 'AssertionError', code.co_names)
- names_and_vars = dict((name, local[name]) for name in
- names_at_line)
- print 'Variables at this point:'
- pprint.pprint(names_and_vars)
- except:
- sys._old_excepthook(exc_type, exception, traceback)
- else:
- sys._old_excepthook(exc_type, exception, traceback)
- sys.excepthook = assert_hook
- if __name__ == '__main__':
- i = 5
- for j in range(10):
- assert j < i
- print j
- local = frame.f_locals
- frame_locals = frame.f_locals
- isinstance(e, AssertionError)
- except:
- except Exception:
- def b():
- i,j=5,10
- assert i==j
- if __name__=='__main__':
- b()
- Assertion error in file 'aser.py' at line 45:
- b()
- Variables at this point:
- {'b': <function b at 0xb77a66f4>}
Add Comment
Please, Sign In to add comment