Skip to content

Commit

Permalink
pythongh-120200: Fix inspect.iscoroutinefunction(inspect) is True c…
Browse files Browse the repository at this point in the history
…orner case (python#120214)
  • Loading branch information
sobolevn authored Jun 7, 2024
1 parent 9d66042 commit 10fb1b8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,21 +403,21 @@ def isgeneratorfunction(obj):
return _has_code_flag(obj, CO_GENERATOR)

# A marker for markcoroutinefunction and iscoroutinefunction.
_is_coroutine_marker = object()
_is_coroutine_mark = object()

def _has_coroutine_mark(f):
while ismethod(f):
f = f.__func__
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):
"""
Decorator to ensure callable is recognised as a coroutine function.
"""
if hasattr(func, '__func__'):
func = func.__func__
func._is_coroutine_marker = _is_coroutine_marker
func._is_coroutine_marker = _is_coroutine_mark
return func

def iscoroutinefunction(obj):
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class PMClass:
gen_coroutine_function_example))))
self.assertFalse(inspect.iscoroutinefunction(gen_coro_pmi))
self.assertFalse(inspect.iscoroutinefunction(gen_coro_pmc))
self.assertFalse(inspect.iscoroutinefunction(inspect))
self.assertFalse(inspect.iscoroutine(gen_coro))

self.assertTrue(
Expand Down

0 comments on commit 10fb1b8

Please sign in to comment.