bpo-29418: Implement inspect.ismethodwrapper and fix inspect.isroutine for cases where methodwrapper is given (GH-19261)

Automerge-Triggered-By: GH:isidentical
This commit is contained in:
Hakan Çelik 2022-02-16 15:46:20 +03:00 committed by GitHub
parent 3954fcb83f
commit 562c13f573
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 12 deletions

View file

@ -121,6 +121,7 @@ __all__ = [
"ismemberdescriptor",
"ismethod",
"ismethoddescriptor",
"ismethodwrapper",
"ismodule",
"isroutine",
"istraceback",
@ -509,12 +510,17 @@ def isbuiltin(object):
__self__ instance to which a method is bound, or None"""
return isinstance(object, types.BuiltinFunctionType)
def ismethodwrapper(object):
"""Return true if the object is a method wrapper."""
return isinstance(object, types.MethodWrapperType)
def isroutine(object):
"""Return true if the object is any kind of function or method."""
return (isbuiltin(object)
or isfunction(object)
or ismethod(object)
or ismethoddescriptor(object))
or ismethoddescriptor(object)
or ismethodwrapper(object))
def isabstract(object):
"""Return true if the object is an abstract base class (ABC)."""
@ -1887,13 +1893,9 @@ def getcoroutinelocals(coroutine):
###############################################################################
_WrapperDescriptor = type(type.__call__)
_MethodWrapper = type(all.__call__)
_ClassMethodWrapper = type(int.__dict__['from_bytes'])
_NonUserDefinedCallables = (_WrapperDescriptor,
_MethodWrapper,
_ClassMethodWrapper,
_NonUserDefinedCallables = (types.WrapperDescriptorType,
types.MethodWrapperType,
types.ClassMethodDescriptorType,
types.BuiltinFunctionType)
@ -2533,7 +2535,7 @@ def _signature_from_callable(obj, *,
elif not isinstance(obj, _NonUserDefinedCallables):
# An object with __call__
# We also check that the 'obj' is not an instance of
# _WrapperDescriptor or _MethodWrapper to avoid
# types.WrapperDescriptorType or types.MethodWrapperType to avoid
# infinite recursion (and even potential segfault)
call = _signature_get_user_defined_method(type(obj), '__call__')
if call is not None: