我刚刚将所有单元测试数据从JSON转换为YAML,现在我的代码中出现异常.更具体地说,这是印刷的回溯:
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
现在,第943行的文件“addrtools.py”包含我的问题的答案.我想在引发异常的范围内看到old_city和city的类型和值.我一直有这样的问题,并且使用pdb来检查引发异常的范围内的本地人的快速且无痛的方法将节省我将来的大量时间.
我确实尝试了在this question的答案中发布的解决方案,但是验尸功能将我置于python2.7 / unittest / main.py(231)runTests()中,这对我没有多大帮助.我想这是因为从unittest代码中捕获并重新引发了异常.
解决方法:
包裹它:
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
例:
@debug_on(TypeError)
def buggy_function()
....
raise TypeError