簡體   English   中英

使用Python pdb檢查未處理異常原因的最簡單方法是什么?

[英]What is the simplest way of using Python pdb to inspect the cause of an unhandled exception?

我剛剛將所有單元測試數據從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_citycity的類型和值。 我一直有這樣的問題,並且使用pdb來檢查引發異常的范圍內的本地人的快速且無痛的方法將節省我將來的大量時間。


我確實嘗試了在這個問題的答案中發布的解決方案,但是驗屍功能將我置於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

單元測試超集鼻子有一個選項可以在測試失敗時將你降到pdb ,如果你可以使用nose作為你的測試運行者:

--pdb                 Drop into debugger on errors
--pdb-failures        Drop into debugger on failures

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM