Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does pdb.set_trace() always overwrite an error traceback?

Tags:

python

pdb

I've got a loop processing sockets, and I've set a pdb.set_trace() breakpoint to stop and inspect the results of the call to select.select() every time through the loop. However, there are also bugs elsewhere in my code, and it seems that the standard traceback is being overwritten by pdb.set_trace. As a result, when the program quits, the traceback points to the line immediately following the set_trace(), not the line that contained the error.

Is there a way to access the actual traceback, or does pdb.set_trace() clobber it?

Here's the relevant code snippet:

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

and then I get the traceback as follows:

Traceback (most recent call last):
  File "socktactoe_server.py", line 62, in <module>
    if listen_sock.fileno() in read_socks:
AssertionError

Here is a short reproducible test case; run it, hit c every time there is a breakpoint, after the second continue the assert raises an exception and it's reported for the wrong line:

import pdb
x = 0
while True:
    pdb.set_trace()
    y = "line of code not triggering an error"
    x += 1
    assert x != 3

Output:

Traceback (most recent call last):
  File "minimal_pdb_traceback.py", line 7, in <module>
    y = "line of code not triggering an error"
AssertionError
like image 279
A Kaptur Avatar asked Mar 04 '26 18:03

A Kaptur


1 Answers

It looks like you found a bug in the Python pdb module! I can reproduce your simple case in Python versions 2.7, 3.2 and 3.3, while the problem is not present in Python 2.4, 2.5, 2.6 or 3.1.

At first glance, I don't see a preexisting bug in the python bug tracker. Please report your problem there, with your minimal test case, as well as what python versions it can be reproduced on.

like image 58
Martijn Pieters Avatar answered Mar 07 '26 07:03

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!