Skip to content

Commit 19abf69

Browse files
[3.11] gh-103225: Fixed zero lineno issue for pdb (#103265) (#104262)
gh-103225: Fixed zero lineno issue for pdb (#103265) Co-authored-by: Artem Mukhin <ortem00@gmail.com>
1 parent a09d390 commit 19abf69

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Lib/pdb.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ def do_longlist(self, arg):
13481348
filename = self.curframe.f_code.co_filename
13491349
breaklist = self.get_file_breaks(filename)
13501350
try:
1351-
lines, lineno = inspect.getsourcelines(self.curframe)
1351+
lines, lineno = self._getsourcelines(self.curframe)
13521352
except OSError as err:
13531353
self.error(err)
13541354
return
@@ -1364,7 +1364,7 @@ def do_source(self, arg):
13641364
except:
13651365
return
13661366
try:
1367-
lines, lineno = inspect.getsourcelines(obj)
1367+
lines, lineno = self._getsourcelines(obj)
13681368
except (OSError, TypeError) as err:
13691369
self.error(err)
13701370
return
@@ -1643,6 +1643,16 @@ def _run(self, target: Union[_ModuleTarget, _ScriptTarget]):
16431643
self.run(target.code)
16441644

16451645

1646+
def _getsourcelines(self, obj):
1647+
# GH-103319
1648+
# inspect.getsourcelines() returns lineno = 0 for
1649+
# module-level frame which breaks our code print line number
1650+
# This method should be replaced by inspect.getsourcelines(obj)
1651+
# once this bug is fixed in inspect
1652+
lines, lineno = inspect.getsourcelines(obj)
1653+
lineno = max(1, lineno)
1654+
return lines, lineno
1655+
16461656
# Collect all command help into docstring, if not run with -OO
16471657

16481658
if __doc__ is not None:

Lib/test/test_pdb.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,32 @@ def test_pdb_issue_gh_101517():
16731673
(Pdb) continue
16741674
"""
16751675

1676+
def test_pdb_issue_gh_103225():
1677+
"""See GH-103225
1678+
1679+
Make sure longlist uses 1-based line numbers in frames that correspond to a module
1680+
1681+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
1682+
... 'longlist',
1683+
... 'continue'
1684+
... ]):
1685+
... a = 1
1686+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
1687+
... b = 2
1688+
> <doctest test.test_pdb.test_pdb_issue_gh_103225[0]>(7)<module>()
1689+
-> b = 2
1690+
(Pdb) longlist
1691+
1 with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
1692+
2 'longlist',
1693+
3 'continue'
1694+
4 ]):
1695+
5 a = 1
1696+
6 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
1697+
7 -> b = 2
1698+
(Pdb) continue
1699+
"""
1700+
1701+
16761702
@support.requires_subprocess()
16771703
class PdbTestCase(unittest.TestCase):
16781704
def tearDown(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug in :mod:`pdb` when displaying line numbers of module-level source code.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy