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): 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.