What is the easiest way to use pdon pdb to check the cause of an unhandled exception?

I just converted all my unit test data from JSON to YAML and now an exception is thrown somewhere in my code. In particular, this is a printed trace:

  Traceback (most recent call last):
   File "tests / test_addrtools.py", line 95, in test_validate_correctable_addresses
     self.assertTrue (self.validator (addr), msg)
   File "/Users/tomas/Dropbox/Broadnet/broadpy/lib/broadpy/addrtools.py", line 608, in __call__
     self.validate (addr)
   File "/Users/tomas/Dropbox/Broadnet/broadpy/lib/broadpy/addrtools.py", line 692, in validate
     if self._correction_citytypo (addr): return
   File "/Users/tomas/Dropbox/Broadnet/broadpy/lib/broadpy/addrtools.py", line 943, in _correction_citytypo
     ratio = lev_ratio (old_city, city)
 TypeError: ratio expected two Strings or two Unicodes 

Now the file "addrtools.py" on line 943 contains the answer to my problem. I want to see the type and values โ€‹โ€‹of old_city and city in the area where the exception occurs. I have such a question all the time, and a quick and painless method of using pdb to check local residents in the area where the exception occurs will save me in the future.


I tried the solution posted in answer to this question , but the post-mortem function puts me in python2.7 / unittest / main.py (231) runTests (), which doesn't help me much. I think this is because the exception is caught and re-raised from unittest code.

+4
source share
2 answers

Wrap it with this:

 def debug_on(*exceptions): if not exceptions: exceptions = (AssertionError, ) def decorator(f): @functools.wraps(f) def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except exceptions: pdb.post_mortem(sys.exc_info()[2]) return wrapper return decorator 

Example:

 @debug_on(TypeError) def buggy_function() .... raise TypeError 
+3
source

Unittest superset nose has an option that drops you to pdb when the test fails if it is good for you to use the nose as a test runner:

 --pdb Drop into debugger on errors --pdb-failures Drop into debugger on failures 
+1
source

Source: https://habr.com/ru/post/1437372/


All Articles