Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8563a70

Browse files
Erlend Egeberg Aaslandtakluyver
Erlend Egeberg Aasland
andauthored
bpo-28528: Fix pdb.checkline() attribute error when 'curframe' is None. (#25438)
Co-authored-by: Thomas Kluyver <[email protected]>
1 parent c1df880 commit 8563a70

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

Lib/pdb.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,8 @@ def checkline(self, filename, lineno):
752752
"""
753753
# this method should be callable before starting debugging, so default
754754
# to "no globals" if there is no current frame
755-
globs = self.curframe.f_globals if hasattr(self, 'curframe') else None
755+
frame = getattr(self, 'curframe', None)
756+
globs = frame.f_globals if frame else None
756757
line = linecache.getline(filename, lineno, globs)
757758
if not line:
758759
self.message('End of file')

Lib/test/test_pdb.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import unittest
1010
import subprocess
1111
import textwrap
12+
import linecache
1213

1314
from contextlib import ExitStack
1415
from io import StringIO
@@ -1807,10 +1808,47 @@ def test_issue42383(self):
18071808
self.assertEqual(stdout.split('\n')[6].rstrip('\r'), expected)
18081809

18091810

1811+
class ChecklineTests(unittest.TestCase):
1812+
def setUp(self):
1813+
linecache.clearcache() # Pdb.checkline() uses linecache.getline()
1814+
1815+
def tearDown(self):
1816+
os_helper.unlink(os_helper.TESTFN)
1817+
1818+
def test_checkline_before_debugging(self):
1819+
with open(os_helper.TESTFN, "w") as f:
1820+
f.write("print(123)")
1821+
db = pdb.Pdb()
1822+
self.assertEqual(db.checkline(os_helper.TESTFN, 1), 1)
1823+
1824+
def test_checkline_after_reset(self):
1825+
with open(os_helper.TESTFN, "w") as f:
1826+
f.write("print(123)")
1827+
db = pdb.Pdb()
1828+
db.reset()
1829+
self.assertEqual(db.checkline(os_helper.TESTFN, 1), 1)
1830+
1831+
def test_checkline_is_not_executable(self):
1832+
with open(os_helper.TESTFN, "w") as f:
1833+
# Test for comments, docstrings and empty lines
1834+
s = textwrap.dedent("""
1835+
# Comment
1836+
\"\"\" docstring \"\"\"
1837+
''' docstring '''
1838+
1839+
""")
1840+
f.write(s)
1841+
db = pdb.Pdb()
1842+
num_lines = len(s.splitlines()) + 2 # Test for EOF
1843+
for lineno in range(num_lines):
1844+
self.assertFalse(db.checkline(os_helper.TESTFN, lineno))
1845+
1846+
18101847
def load_tests(*args):
18111848
from test import test_pdb
18121849
suites = [
18131850
unittest.makeSuite(PdbTestCase),
1851+
unittest.makeSuite(ChecklineTests),
18141852
doctest.DocTestSuite(test_pdb)
18151853
]
18161854
return unittest.TestSuite(suites)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug in :mod:`pdb` where :meth:`~pdb.Pdb.checkline` raises
2+
:exc:`AttributeError` if it is called after :meth:`~pdb.Pdb.reset`.

0 commit comments

Comments
 (0)