]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-28528: Fix pdb.checkline() attribute error when 'curframe' is None (GH...
authorErlend Egeberg Aasland <erlend.aasland@innova.no>
Wed, 12 May 2021 09:04:10 +0000 (11:04 +0200)
committerGitHub <noreply@github.com>
Wed, 12 May 2021 09:04:10 +0000 (02:04 -0700)
Co-authored-by: Thomas Kluyver <takowl@gmail.com>
(cherry picked from commit 8563a7052ccd98e6a381d361664ce567afd5eb6e)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
Automerge-Triggered-By: GH:iritkatriel
Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2021-04-29-00-48-00.bpo-28528.JLAVWj.rst [new file with mode: 0644]

index 98dc975eafafdcf364c2de8d04e591fbe64cfb2b..a888a0a287f9c2c50d1c5ef1fb382a77b0f70ea9 100755 (executable)
@@ -752,7 +752,8 @@ class Pdb(bdb.Bdb, cmd.Cmd):
         """
         # 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')
index 2523a5d691ac7eb1dc11a42fa87a7e84ec082e56..b1cad22c830ffecd0f39accaa3eb2fbf2f676961 100644 (file)
@@ -9,6 +9,7 @@ import codecs
 import unittest
 import subprocess
 import textwrap
+import linecache
 
 from contextlib import ExitStack
 from io import StringIO
@@ -1743,10 +1744,47 @@ def bœr():
             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):
+        support.unlink(support.TESTFN)
+
+    def test_checkline_before_debugging(self):
+        with open(support.TESTFN, "w") as f:
+            f.write("print(123)")
+        db = pdb.Pdb()
+        self.assertEqual(db.checkline(support.TESTFN, 1), 1)
+
+    def test_checkline_after_reset(self):
+        with open(support.TESTFN, "w") as f:
+            f.write("print(123)")
+        db = pdb.Pdb()
+        db.reset()
+        self.assertEqual(db.checkline(support.TESTFN, 1), 1)
+
+    def test_checkline_is_not_executable(self):
+        with open(support.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(support.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 (file)
index 0000000..97731ad
--- /dev/null
@@ -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`.