蟒蛇PDB例外

问题描述:

在我的iMac,调试器不给正确的痕迹,运行故障python脚本(在Mac OS塞拉利昂),始终指向代码的第一有效行作为例外的原因,没有调试器启动时,同时确定了正确的行。有没有人知道为什么会出现这种情况,以及如何解决这个问题?蟒蛇PDB例外

这里有一个简单的例子,一个 “找不到文件” 异常:

脚本exception_test.py:

1 # Some dummy lines... 
2 a=1 
3 b=2 
4 c=a+b 
5 # Lines casuing the exception: 
6 with open("filename","r") as fid: 
7  lines=fid.readlines() 

当没有调试器中运行,如python exception_test.py它产生

Traceback (most recent call last): 
    File "exception_test.py", line 6, in <module> 
    with open("filename","r") as fid: 
IOError: [Errno 2] No such file or directory: 'filename' 

识别正确的路线,即LINE6, 而python -m pdb exception_test.py和连续c继续产量

Traceback (most recent call last): 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pdb.py", line 1314, in main 
    pdb._runscript(mainpyfile) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pdb.py", line 1233, in _runscript 
    self.run(statement) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bdb.py", line 400, in run 
    exec cmd in globals, locals 
    File "<string>", line 1, in <module> 
    File "exception_test.py", line 2, in <module> 
    a=1 
IOError: [Errno 2] No such file or directory: 'filename' 
Uncaught exception. Entering post mortem debugging 

指示代码的第一有效行,即第2行

+0

什么是您的Python源代码文件的名称? –

+0

exception_test.py – momme

它更可能的CPython和PDB的问题,而不是你的代码,pypy可以打印正确的回溯行号。

frameline_no发生异常的地方不对-m pdb

一些猜测和跟踪后,我可以缩小到this path

sys.settrace(self.trace_dispatch) 
    -> trace_dispatch() -> dispatch_line() -> user_line() -> interaction() 

真正的根本原因尚不清楚。