Skip to content

Commit

Permalink
[3.11] pythongh-103225: Fixed zero lineno issue for pdb (python#103265)…
Browse files Browse the repository at this point in the history
… (python#104262)

pythongh-103225: Fixed zero lineno issue for pdb (python#103265)

Co-authored-by: Artem Mukhin <[email protected]>
  • Loading branch information
gaogaotiantian and artemmukhin authored May 7, 2023
1 parent a09d390 commit 19abf69
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ def do_longlist(self, arg):
filename = self.curframe.f_code.co_filename
breaklist = self.get_file_breaks(filename)
try:
lines, lineno = inspect.getsourcelines(self.curframe)
lines, lineno = self._getsourcelines(self.curframe)
except OSError as err:
self.error(err)
return
Expand All @@ -1364,7 +1364,7 @@ def do_source(self, arg):
except:
return
try:
lines, lineno = inspect.getsourcelines(obj)
lines, lineno = self._getsourcelines(obj)
except (OSError, TypeError) as err:
self.error(err)
return
Expand Down Expand Up @@ -1643,6 +1643,16 @@ def _run(self, target: Union[_ModuleTarget, _ScriptTarget]):
self.run(target.code)


def _getsourcelines(self, obj):
# GH-103319
# inspect.getsourcelines() returns lineno = 0 for
# module-level frame which breaks our code print line number
# This method should be replaced by inspect.getsourcelines(obj)
# once this bug is fixed in inspect
lines, lineno = inspect.getsourcelines(obj)
lineno = max(1, lineno)
return lines, lineno

# Collect all command help into docstring, if not run with -OO

if __doc__ is not None:
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,32 @@ def test_pdb_issue_gh_101517():
(Pdb) continue
"""

def test_pdb_issue_gh_103225():
"""See GH-103225
Make sure longlist uses 1-based line numbers in frames that correspond to a module
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... 'longlist',
... 'continue'
... ]):
... a = 1
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
... b = 2
> <doctest test.test_pdb.test_pdb_issue_gh_103225[0]>(7)<module>()
-> b = 2
(Pdb) longlist
1 with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
2 'longlist',
3 'continue'
4 ]):
5 a = 1
6 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
7 -> b = 2
(Pdb) continue
"""


@support.requires_subprocess()
class PdbTestCase(unittest.TestCase):
def tearDown(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug in :mod:`pdb` when displaying line numbers of module-level source code.

0 comments on commit 19abf69

Please sign in to comment.