diff --git a/Lib/inspect.py b/Lib/inspect.py index fcfe3b191ab503..8dbde52f89de56 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 @@ -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 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..f5409bc66e60e7 --- /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 duck-typing in ``inspect`` to support Cython functions in ``getfile`` and ``getsource``