From cd8005ba855a11ebd025e8f8310019e0e342b9d5 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 19 Mar 2024 11:33:35 -0700 Subject: [PATCH 1/5] Remove re pattern check in inspect.findsource as it's not needed anymore --- Lib/inspect.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 7336cea0dc3fdc..00c37b00418024 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1157,15 +1157,8 @@ def findsource(object): if not hasattr(object, 'co_firstlineno'): raise OSError('could not find function definition') lnum = object.co_firstlineno - 1 - pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(? 0: - try: - line = lines[lnum] - except IndexError: - raise OSError('lineno is out of bounds') - if pat.match(line): - break - lnum = lnum - 1 + if lnum > len(lines): + raise OSError('lineno is out of bounds') return lines, lnum raise OSError('could not find code object') From da731b4415a7faf306422e1d09ea376288b18931 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 19 Mar 2024 19:42:28 +0000 Subject: [PATCH 2/5] =?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/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst diff --git a/Misc/NEWS.d/next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst b/Misc/NEWS.d/next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst new file mode 100644 index 00000000000000..45b700fda3bb7e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst @@ -0,0 +1 @@ +Remove regex pattern check in :func:`inspect.findsource` which makes the function work with class code objects. From 5267929e65bab026cf6e48bac2ae47a86b6ed8c0 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 19 Mar 2024 13:14:53 -0700 Subject: [PATCH 3/5] Update 2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst --- .../next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst b/Misc/NEWS.d/next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst index 45b700fda3bb7e..f2da956f66c86b 100644 --- a/Misc/NEWS.d/next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst +++ b/Misc/NEWS.d/next/Library/2024-03-19-19-42-25.gh-issue-116987.ZVKUH1.rst @@ -1 +1 @@ -Remove regex pattern check in :func:`inspect.findsource` which makes the function work with class code objects. +Fixed :func:`inspect.findsource` for class code objects. From 763e23e4da1cb14e9eb4972111ad5f4193869832 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 19 Mar 2024 20:06:05 -0700 Subject: [PATCH 4/5] Fix range --- Lib/inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 00c37b00418024..422c09a92ad141 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1157,7 +1157,7 @@ def findsource(object): if not hasattr(object, 'co_firstlineno'): raise OSError('could not find function definition') lnum = object.co_firstlineno - 1 - if lnum > len(lines): + if lnum >= len(lines): raise OSError('lineno is out of bounds') return lines, lnum raise OSError('could not find code object') From 1f7d62fe79adfd474560599880a25a30c4f5b130 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Wed, 20 Mar 2024 10:29:16 -0700 Subject: [PATCH 5/5] Add test case for class code object --- Lib/test/test_inspect/inspect_fodder2.py | 5 +++++ Lib/test/test_inspect/test_inspect.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/Lib/test/test_inspect/inspect_fodder2.py b/Lib/test/test_inspect/inspect_fodder2.py index 8639cf2e72cd7a..bb9d3e88cfbee1 100644 --- a/Lib/test/test_inspect/inspect_fodder2.py +++ b/Lib/test/test_inspect/inspect_fodder2.py @@ -310,3 +310,8 @@ def f(): class cls310: def g(): pass + +# line 314 +class ClassWithCodeObject: + import sys + code = sys._getframe(0).f_code diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 21d9f96c8c460e..dc46c0bc8ed353 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -983,6 +983,9 @@ def test_findsource_with_out_of_bounds_lineno(self): def test_getsource_on_method(self): self.assertSourceEqual(mod2.ClassWithMethod.method, 118, 119) + def test_getsource_on_class_code_object(self): + self.assertSourceEqual(mod2.ClassWithCodeObject.code, 315, 317) + def test_nested_func(self): self.assertSourceEqual(mod2.cls135.func136, 136, 139)