From 25585fc995d6989ea34e602c85b4ae41f2d7f9e0 Mon Sep 17 00:00:00 2001 From: Artem Mukhin Date: Mon, 3 Apr 2023 01:18:46 +0200 Subject: [PATCH 1/2] GH-103225: Fix `inspect.getsourcelines()` to return 1-based line numbers --- Lib/inspect.py | 2 +- Lib/test/test_inspect.py | 11 +++++++++++ Lib/test/test_pdb.py | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 0eceaaf9a24f5d..5a9174e0d61c5c 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1260,7 +1260,7 @@ def getsourcelines(object): # for module or frame that corresponds to module, return all source lines if (ismodule(object) or (isframe(object) and object.f_code.co_name == "")): - return lines, 0 + return lines, 1 else: return getblock(lines[lnum:]), lnum + 1 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 3a3646f1861e80..e724cf8057ce68 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -654,6 +654,17 @@ def test_getsourcefile(self): finally: del linecache.cache[co.co_filename] + def test_getsourcelines(self): + # Check source lines and starting line number for a module + mod_lines, mod_lineno = inspect.getsourcelines(mod) + self.assertEqual(len(mod_lines), 115) + self.assertEqual(mod_lineno, 1) + + # Check source lines and starting line number for a function + spam_lines, spam_lineno = inspect.getsourcelines(mod.spam) + self.assertEqual(len(spam_lines), 2) + self.assertEqual(spam_lineno, 8) + def test_getfile(self): self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index de2bab46495729..9ad9a1c52ac102 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -1675,6 +1675,31 @@ def test_pdb_issue_gh_101673(): (Pdb) continue """ +def test_pdb_issue_gh_103225(): + """See GH-103225 + + Make sure longlist uses 1-based line numbers in frames that correspond to a module + + >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE + ... 'longlist', + ... 'continue' + ... ]): + ... a = 1 + ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + ... b = 2 + > (7)() + -> b = 2 + (Pdb) longlist + 1 with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE + 2 'longlist', + 3 'continue' + 4 ]): + 5 a = 1 + 6 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + 7 -> b = 2 + (Pdb) continue + """ + @support.requires_subprocess() class PdbTestCase(unittest.TestCase): From d6e40120e8cc59ddd6897def201908a71d3263b4 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 3 Apr 2023 22:11:58 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-04-03-22-11-58.gh-issue-103225.nwprfk.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-04-03-22-11-58.gh-issue-103225.nwprfk.rst diff --git a/Misc/NEWS.d/next/Library/2023-04-03-22-11-58.gh-issue-103225.nwprfk.rst b/Misc/NEWS.d/next/Library/2023-04-03-22-11-58.gh-issue-103225.nwprfk.rst new file mode 100644 index 00000000000000..b54c367422ebfb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-03-22-11-58.gh-issue-103225.nwprfk.rst @@ -0,0 +1 @@ +Fix :func:`inspect.getsourcelines` function to return 1-based line numbers for modules and frames that correspond to modules.