[Python-checkins] gh-103225: Fixed zero lineno issue for pdb (#103265)

iritkatriel webhook-mailer at python.org
Fri Apr 7 13:57:53 EDT 2023


https://github.com/python/cpython/commit/2667452945eb0a3b8993bb4298ca8da54dc0155a
commit: 2667452945eb0a3b8993bb4298ca8da54dc0155a
branch: main
author: Tian Gao <gaogaotiantian at hotmail.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2023-04-07T18:57:46+01:00
summary:

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

Co-authored-by: Artem Mukhin <ortem00 at gmail.com>

files:
A Misc/NEWS.d/next/Library/2023-04-05-01-28-53.gh-issue-103225.QD3JVU.rst
M Lib/pdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index 3a06cd00ad2b..e043b0d46f7d 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -1351,7 +1351,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
@@ -1367,7 +1367,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
@@ -1662,6 +1662,16 @@ def _compile_error_message(self, expr):
             return _rstr(self._format_exc(exc))
         return ""
 
+    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:
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index de2bab464957..9ad9a1c52ac1 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -1675,6 +1675,31 @@ def test_pdb_issue_gh_101673():
     (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):
diff --git a/Misc/NEWS.d/next/Library/2023-04-05-01-28-53.gh-issue-103225.QD3JVU.rst b/Misc/NEWS.d/next/Library/2023-04-05-01-28-53.gh-issue-103225.QD3JVU.rst
new file mode 100644
index 000000000000..5d1a063acdeb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-04-05-01-28-53.gh-issue-103225.QD3JVU.rst
@@ -0,0 +1 @@
+Fix a bug in :mod:`pdb` when displaying line numbers of module-level source code.



More information about the Python-checkins mailing list