我有一个循环处理套接字,并且我设置了一个 pdb.set_trace() 断点来停止并检查每次循环调用 select.select() 的结果。但是,我的代码中的其他地方也存在错误,并且标准回溯似乎被 pdb.set_trace 覆盖。结果,当程序退出时,回溯指向紧跟在 set_trace() 之后的行,而不是包含错误的行。
有没有办法访问实际的回溯,或者 pdb.set_trace() 是否会破坏它?
这是相关的代码片段:
while True:
    read_socks, write_socks, _ = select.select(all_sockets, all_sockets, '')
    pdb.set_trace()
    if listen_socket.fileno() in read_socks:
        new_socket, address = listen_socket.accept()
        id_num = new_socket.fileno()
        all_sockets[id_num] = new_socket
    for id_num in write_socks:
        # do something that triggers an Assertion error
然后我得到如下回溯:
Traceback (most recent call last):
  File "socktactoe_server.py", line 62, in <module>
    if listen_sock.fileno() in read_socks:
AssertionError
这是一个简短的可重现测试用例;c运行它,每次有断点时命中,在第二次 continue 之后assert引发异常并报告错误的行:
import pdb
x = 0
while True:
    pdb.set_trace()
    y = "line of code not triggering an error"
    x += 1
    assert x != 3
输出:
Traceback (most recent call last):
  File "minimal_pdb_traceback.py", line 7, in <module>
    y = "line of code not triggering an error"
AssertionError