Skip to content

pdb lists source starting from wrong line number if the exception is raised in a try/except block #140415

@dimaqq

Description

@dimaqq

Bug report

Bug description:

MRE

def leaf():
    try:
        assert False
        "padding"
        "padding"
        "padding"
        "padding"
        "padding"
        "padding"
        "padding"
        "padding"
        "padding"
    except IndexError:
        pass


if __name__ == "__main__":
    leaf()

Running this:

> python3.14 -m pdb bar.py
> /code/test-clean/bar.py(1)<module>()
-> def leaf():
(Pdb) c
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/pdb.py", line 3601, in main
    pdb._run(target)
    ~~~~~~~~^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/pdb.py", line 2522, in _run
    self.run(target.code)
    ~~~~~~~~^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/bdb.py", line 899, in run
    exec(cmd, globals, locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "/code/test-clean/bar.py", line 18, in <module>
    leaf()
    ~~~~^^
  File "/code/test-clean/bar.py", line 3, in leaf
    assert False
           ^^^^^
AssertionError
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /code/test-clean/bar.py(3)leaf()
-> assert False
(Pdb) list
  8  	        "padding"
  9  	        "padding"
 10  	        "padding"
 11  	        "padding"
 12  	        "padding"
 13  ->	    except IndexError:
 14  	        pass
 15
 16
 17  	if __name__ == "__main__":
 18  	    leaf()
(Pdb)

Note that the traceback reports correct line number (3), but source code listing is focused on line (13) where an unrelated exception would be caught.

The actual location is here:

(Pdb) list 3
  1  	def leaf():
  2  	    try:
  3  >>	        assert False
  4  	        "padding"
  5  	        "padding"
  6  	        "padding"
  7  	        "padding"
  8  	        "padding"
  9  	        "padding"
 10  	        "padding"
 11  	        "padding"

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Metadata

Metadata

Labels

3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixespendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions