Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-120200: Fix inspect.iscoroutinefunction(inspect) is True corner …
…case (GH-120214)

(cherry picked from commit 10fb1b8)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
  • Loading branch information
sobolevn authored and miss-islington committed Jun 7, 2024
commit 01499eac605f4e186bff8169c7b7fc1a87c7e957
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