From f0efff0b5c01f9813567a9c39e7c3e297894b7d5 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 23 Mar 2025 13:55:14 +0000 Subject: [PATCH 1/4] gh-131628: use dock-typing in `inspect.getfile` --- Lib/inspect.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index fcfe3b191ab503..275a3de0372b72 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -265,7 +265,7 @@ def isgetsetdescriptor(object): return False def isfunction(object): - """Return true if the object is a user-defined function. + """Return true if the object is a user-defined Python function. Function objects provide these attributes: __doc__ documentation string @@ -828,9 +828,9 @@ def getfile(object): if object.__module__ == '__main__': raise OSError('source code not available') raise TypeError('{!r} is a built-in class'.format(object)) - if ismethod(object): + if hasattr(object, '__func__'): object = object.__func__ - if isfunction(object): + if hasattr(object, '__code__'): object = object.__code__ if istraceback(object): object = object.tb_frame From aa65c2e9e10fc1e22a22b107b6e8a8c247265d28 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sun, 23 Mar 2025 14:04:46 +0000 Subject: [PATCH 2/4] similar changes for findsource --- Lib/inspect.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 275a3de0372b72..8dbde52f89de56 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -988,9 +988,9 @@ def findsource(object): raise OSError('lineno is out of bounds') return lines, lnum - if ismethod(object): + if hasattr(object, '__func__'): object = object.__func__ - if isfunction(object): + if hasattr(object, '__code__'): object = object.__code__ if istraceback(object): object = object.tb_frame From 9bc02cbcff71d94f735750e70d5a05bfde01d71e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 23 Mar 2025 14:13:21 +0000 Subject: [PATCH 3/4] =?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 --- .../2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst new file mode 100644 index 00000000000000..daeb3b36bcc54e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst @@ -0,0 +1 @@ +Use dock-typing in ``inspect`` to support Cython functions in ``getfile`` and ``getsource`` From d33632b8c5815659d622e89255e118d04c26ccd7 Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Sat, 29 Mar 2025 17:35:31 +0100 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst Co-authored-by: Nicolas Trangez --- .../2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst index daeb3b36bcc54e..f5409bc66e60e7 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-03-23-14-13-20.gh-issue-131628.M9Q06m.rst @@ -1 +1 @@ -Use dock-typing in ``inspect`` to support Cython functions in ``getfile`` and ``getsource`` +Use duck-typing in ``inspect`` to support Cython functions in ``getfile`` and ``getsource``