[3.12] gh-120200: Fix inspect.iscoroutinefunction(inspect) is True corner case (GH-120214) (#120239)

* [3.12] gh-120200: Fix `inspect.iscoroutinefunction(inspect) is True` corner case (GH-120214)
(cherry picked from commit 10fb1b8f36)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Nikita Sobolev 2024-06-07 19:41:45 +03:00 committed by GitHub
parent ec7c9d330f
commit 1e7903d549
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 3 deletions

View file

@ -401,13 +401,13 @@ def isgeneratorfunction(obj):
return _has_code_flag(obj, CO_GENERATOR) return _has_code_flag(obj, CO_GENERATOR)
# A marker for markcoroutinefunction and iscoroutinefunction. # A marker for markcoroutinefunction and iscoroutinefunction.
_is_coroutine_marker = object() _is_coroutine_mark = object()
def _has_coroutine_mark(f): def _has_coroutine_mark(f):
while ismethod(f): while ismethod(f):
f = f.__func__ f = f.__func__
f = functools._unwrap_partial(f) f = functools._unwrap_partial(f)
return getattr(f, "_is_coroutine_marker", None) is _is_coroutine_marker return getattr(f, "_is_coroutine_marker", None) is _is_coroutine_mark
def markcoroutinefunction(func): def markcoroutinefunction(func):
""" """
@ -415,7 +415,7 @@ def markcoroutinefunction(func):
""" """
if hasattr(func, '__func__'): if hasattr(func, '__func__'):
func = func.__func__ func = func.__func__
func._is_coroutine_marker = _is_coroutine_marker func._is_coroutine_marker = _is_coroutine_mark
return func return func
def iscoroutinefunction(obj): def iscoroutinefunction(obj):

View file

@ -199,6 +199,7 @@ class TestPredicates(IsTestBase):
inspect.iscoroutinefunction( inspect.iscoroutinefunction(
functools.partial(functools.partial( functools.partial(functools.partial(
gen_coroutine_function_example)))) gen_coroutine_function_example))))
self.assertFalse(inspect.iscoroutinefunction(inspect))
self.assertFalse(inspect.iscoroutine(gen_coro)) self.assertFalse(inspect.iscoroutine(gen_coro))
self.assertTrue( self.assertTrue(