From d4f8fafd2df3a6a1828c96f9347a14595ec438d1 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Mon, 13 Mar 2023 14:24:01 +0000 Subject: [PATCH] [3.10] GH-101673: Fix pdb bug where local variable changes are lost after longlist (#101674) (#102633) GH-101673: Fix pdb bug where local variable changes are lost after longlist (#101674) (cherry picked from commit 5d677c556f03a34d1c2d86e4cc96025870c20c12) Co-authored-by: gaogaotiantian --- Lib/pdb.py | 13 ++------- Lib/test/test_pdb.py | 29 +++++++++++++++++++ ...-02-09-19-40-41.gh-issue-101673.mX-Ppq.rst | 1 + 3 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-02-09-19-40-41.gh-issue-101673.mX-Ppq.rst diff --git a/Lib/pdb.py b/Lib/pdb.py index 7401a675253d78..3bdb2ac36f7192 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -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() @@ -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 @@ -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 diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 7f0bc719f0afa7..889262b1f1939a 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -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-- + > (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): diff --git a/Misc/NEWS.d/next/Library/2023-02-09-19-40-41.gh-issue-101673.mX-Ppq.rst b/Misc/NEWS.d/next/Library/2023-02-09-19-40-41.gh-issue-101673.mX-Ppq.rst new file mode 100644 index 00000000000000..4e673ba9811568 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-02-09-19-40-41.gh-issue-101673.mX-Ppq.rst @@ -0,0 +1 @@ +Fix a :mod:`pdb` bug where ``ll`` clears the changes to local variables.