Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.10] GH-101673: Fix pdb bug where local variable changes are lost after longlist (#101674) #102633

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,6 @@ def find_function(funcname, filename):
return funcname, filename, lineno
return None

def getsourcelines(obj):
lines, lineno = inspect.findsource(obj)
if inspect.isframe(obj) and obj.f_globals is obj.f_locals:
# must be a module frame: do not try to cut a block out of it
return lines, 1
elif inspect.ismodule(obj):
return lines, 1
return inspect.getblock(lines[lineno:]), lineno+1

def lasti2lineno(code, lasti):
linestarts = list(dis.findlinestarts(code))
linestarts.reverse()
Expand Down Expand Up @@ -1273,7 +1264,7 @@ def do_longlist(self, arg):
filename = self.curframe.f_code.co_filename
breaklist = self.get_file_breaks(filename)
try:
lines, lineno = getsourcelines(self.curframe)
lines, lineno = inspect.getsourcelines(self.curframe)
except OSError as err:
self.error(err)
return
Expand All @@ -1289,7 +1280,7 @@ def do_source(self, arg):
except:
return
try:
lines, lineno = getsourcelines(obj)
lines, lineno = inspect.getsourcelines(obj)
except (OSError, TypeError) as err:
self.error(err)
return
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,35 @@ def test_pdb_issue_43318():
4
"""

def test_pdb_issue_gh_101673():
"""See GH-101673

Make sure ll won't revert local variable assignment

>>> def test_function():
... a = 1
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()

>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
... '!a = 2',
... 'll',
... 'p a',
... 'continue'
... ]):
... test_function()
--Return--
> <doctest test.test_pdb.test_pdb_issue_gh_101673[0]>(3)test_function()->None
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) !a = 2
(Pdb) ll
1 def test_function():
2 a = 1
3 -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
(Pdb) p a
2
(Pdb) continue
"""


class PdbTestCase(unittest.TestCase):
def tearDown(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a :mod:`pdb` bug where ``ll`` clears the changes to local variables.