[Python-checkins] bpo-28528: Fix pdb.checkline() attribute error when 'curframe' is None. (#25438)

iritkatriel webhook-mailer at python.org
Tue May 11 19:26:44 EDT 2021


https://github.com/python/cpython/commit/8563a7052ccd98e6a381d361664ce567afd5eb6e
commit: 8563a7052ccd98e6a381d361664ce567afd5eb6e
branch: main
author: Erlend Egeberg Aasland <erlend.aasland at innova.no>
committer: iritkatriel <iritkatriel at yahoo.com>
date: 2021-05-12T00:26:30+01:00
summary:

bpo-28528: Fix pdb.checkline() attribute error when 'curframe' is None. (#25438)


Co-authored-by: Thomas Kluyver <takowl at gmail.com>

files:
A Misc/NEWS.d/next/Library/2021-04-29-00-48-00.bpo-28528.JLAVWj.rst
M Lib/pdb.py
M Lib/test/test_pdb.py

diff --git a/Lib/pdb.py b/Lib/pdb.py
index 98dc975eafafdc..a888a0a287f9c2 100755
--- a/Lib/pdb.py
+++ b/Lib/pdb.py
@@ -752,7 +752,8 @@ def checkline(self, filename, lineno):
         """
         # this method should be callable before starting debugging, so default
         # to "no globals" if there is no current frame
-        globs = self.curframe.f_globals if hasattr(self, 'curframe') else None
+        frame = getattr(self, 'curframe', None)
+        globs = frame.f_globals if frame else None
         line = linecache.getline(filename, lineno, globs)
         if not line:
             self.message('End of file')
diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py
index 870eab4e33e633..cd096e7dd56e8d 100644
--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -9,6 +9,7 @@
 import unittest
 import subprocess
 import textwrap
+import linecache
 
 from contextlib import ExitStack
 from io import StringIO
@@ -1807,10 +1808,47 @@ def test_issue42383(self):
             self.assertEqual(stdout.split('\n')[6].rstrip('\r'), expected)
 
 
+class ChecklineTests(unittest.TestCase):
+    def setUp(self):
+        linecache.clearcache()  # Pdb.checkline() uses linecache.getline()
+
+    def tearDown(self):
+        os_helper.unlink(os_helper.TESTFN)
+
+    def test_checkline_before_debugging(self):
+        with open(os_helper.TESTFN, "w") as f:
+            f.write("print(123)")
+        db = pdb.Pdb()
+        self.assertEqual(db.checkline(os_helper.TESTFN, 1), 1)
+
+    def test_checkline_after_reset(self):
+        with open(os_helper.TESTFN, "w") as f:
+            f.write("print(123)")
+        db = pdb.Pdb()
+        db.reset()
+        self.assertEqual(db.checkline(os_helper.TESTFN, 1), 1)
+
+    def test_checkline_is_not_executable(self):
+        with open(os_helper.TESTFN, "w") as f:
+            # Test for comments, docstrings and empty lines
+            s = textwrap.dedent("""
+                # Comment
+                \"\"\" docstring \"\"\"
+                ''' docstring '''
+
+            """)
+            f.write(s)
+        db = pdb.Pdb()
+        num_lines = len(s.splitlines()) + 2  # Test for EOF
+        for lineno in range(num_lines):
+            self.assertFalse(db.checkline(os_helper.TESTFN, lineno))
+
+
 def load_tests(*args):
     from test import test_pdb
     suites = [
         unittest.makeSuite(PdbTestCase),
+        unittest.makeSuite(ChecklineTests),
         doctest.DocTestSuite(test_pdb)
     ]
     return unittest.TestSuite(suites)
diff --git a/Misc/NEWS.d/next/Library/2021-04-29-00-48-00.bpo-28528.JLAVWj.rst b/Misc/NEWS.d/next/Library/2021-04-29-00-48-00.bpo-28528.JLAVWj.rst
new file mode 100644
index 00000000000000..97731ad882e032
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-04-29-00-48-00.bpo-28528.JLAVWj.rst
@@ -0,0 +1,2 @@
+Fix a bug in :mod:`pdb` where :meth:`~pdb.Pdb.checkline` raises
+:exc:`AttributeError` if it is called after :meth:`~pdb.Pdb.reset`.



More information about the Python-checkins mailing list