Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged main into regmachine branch #50

Merged
merged 75 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
1cf3d78
Correct CVE-2020-10735 documentation (#100306)
ucodery Dec 18, 2022
532aa4e
gh-94912: Added marker for non-standard coroutine function detection …
carltongibson Dec 18, 2022
702a5bc
Docs: Don't upload CI artifacts (#100330)
hugovk Dec 19, 2022
797edb2
gh-89727: Fix os.walk RecursionError on deep trees (#99803)
jonburdo Dec 19, 2022
36a0b1d
gh-69929: re docs: Add more specific definition of \w (#92015)
slateny Dec 20, 2022
79ccc03
gh-89051: Add ssl.OP_LEGACY_SERVER_CONNECT (#93927)
graingert Dec 20, 2022
39dfbb2
gh-88211: Change lower-case and upper-case to match recommendations i…
tbwolfe Dec 20, 2022
a6331b6
gh-100348: Fix ref cycle in `asyncio._SelectorSocketTransport` with `…
rkojedzinszky Dec 20, 2022
d98ca81
gh-99925: Fix inconsistency in `json.dumps()` error messages (GH-99926)
fnesveda Dec 20, 2022
cb60b61
Clarify that every thread has its own default context in contextvars …
pablogsal Dec 20, 2022
44892d4
gh-99576: Fix cookiejar file that was not truncated for some classes …
sobolevn Dec 20, 2022
c18d831
gh-100188: Reduce misses in BINARY_SUBSCR_(LIST/TUPLE)_INT (#100189)
sweeneyde Dec 20, 2022
a2bb3b7
gh-99991: improve docs on str.encode and bytes.decode (#100198)
bizzyvinci Dec 21, 2022
c615286
gh-91081: Add note on WeakKeyDictionary behavior when deleting a repl…
slateny Dec 21, 2022
79311cb
gh-85267: Improvements to inspect.signature __text_signature__ handli…
hauntsaninja Dec 21, 2022
4994f24
GH-100363: Speed up `asyncio.get_running_loop` (#100364)
kumaraditya303 Dec 21, 2022
a7715cc
GH-100133: fix `asyncio` subprocess losing `stderr` and `stdout` outp…
kumaraditya303 Dec 21, 2022
12be23c
gh-100374: Fixed a bug in socket.getfqdn() (gh-100375)
MTAwsl Dec 21, 2022
b98d2d3
gh-100129: Add tests for pickling all builtin types and functions (GH…
serhiy-storchaka Dec 21, 2022
e0b4d96
Remove unused variable from `dis._find_imports` (#100396)
sobolevn Dec 21, 2022
d713c54
gh-78878: Fix crash when creating an instance of `_ctypes.CField` (#1…
shihai1991 Dec 21, 2022
bbf4a66
GH-69564: Clarify use of octal format of mode argument in help(os.chm…
amaajemyfren Dec 21, 2022
3c033a2
GH-99554: Pack location tables more effectively (GH-99556)
brandtbucher Dec 22, 2022
6898157
Correct typo in typing.py (#100423)
david-why Dec 22, 2022
2b82c36
gh-99761: Add `_PyLong_IsPositiveSingleDigit` function to check for s…
eendebakpt Dec 22, 2022
a021612
GH-99770: Make the correct call specialization fail kind show up in t…
penguin-wwy Dec 22, 2022
aa878f0
gh-78997: fix bad rebase of moved test file (#100424)
eli-schwartz Dec 22, 2022
4cc63e0
gh-100344: Add C implementation for `asyncio.current_task` (#100345)
itamaro Dec 22, 2022
09edde9
GH-99554: Trim trailing whitespace (GH-100435)
brandtbucher Dec 22, 2022
9cdb642
gh-85432: Harmonise parameter names between C and pure-Python impleme…
AlexWaygood Dec 22, 2022
ad23da0
gh-57762: fix misleading tkinter.Tk docstring (#98837)
hauntsaninja Dec 22, 2022
f3db68e
gh-48496: Added example and link to faq for UnboundLocalError in refe…
slateny Dec 22, 2022
73c08ee
Fix typo in 3.12 What's New (#100449)
hauntsaninja Dec 23, 2022
84bc6a4
gh-76963: PEP3118 itemsize of an empty ctypes array should not be 0 (…
eric-wieser Dec 23, 2022
2659036
GH-100459: fix copy-paste errors in specialization stats (GH-100460)
iritkatriel Dec 23, 2022
88d565f
gh-99110: Initialize `frame->previous` in init_frame to fix segmentat…
byllyfish Dec 23, 2022
49f6ff7
gh-98712: Clarify "readonly bytes-like object" semantics in C arg-par…
encukou Dec 23, 2022
7fc7909
gh-92216: improve performance of `hasattr` for type objects (GH-99979)
eendebakpt Dec 23, 2022
c3c7848
gh-100288: Specialise LOAD_ATTR_METHOD for managed dictionaries (GH-1…
Fidget-Spinner Dec 23, 2022
36d3583
Revert "gh-100288: Specialise LOAD_ATTR_METHOD for managed dictionari…
Fidget-Spinner Dec 23, 2022
a98d9ea
gh-94155: Reduce hash collisions for code objects (#100183)
sweeneyde Dec 23, 2022
c5726b7
gh-83076: 3.8x speed improvement in (Async)Mock instantiation (#100252)
carljm Dec 23, 2022
745545b
gh-99482: remove `jython` compatibility parts from stdlib and tests (…
sobolevn Dec 23, 2022
1ecfd1e
bpo-40447: accept all path-like objects in compileall.compile_file (#…
FFY00 Dec 23, 2022
5d84966
GH-100425: Improve accuracy of builtin sum() for float inputs (GH-100…
rhettinger Dec 23, 2022
a68e585
gh-68320, gh-88302 - Allow for private `pathlib.Path` subclassing (GH…
barneygale Dec 23, 2022
474220e
gh-99947: Ensure unreported errors are chained for SystemError during…
seberg Dec 23, 2022
f89de67
Add "strict" to dotproduct(). Add docstring. Factor-out common code. …
rhettinger Dec 23, 2022
7ca45e5
gh-94808: improve test coverage of number formatting (#99472)
sobolevn Dec 24, 2022
a23cb72
gh-100454: Start running SSL tests with OpenSSL 3.1.0-beta1 (#100456)
illia-v Dec 24, 2022
3e46f9f
gh-100268: Add is_integer method to int (#100439)
hauntsaninja Dec 24, 2022
0f64206
gh-77771: Add enterabs example in sched (#92716)
slateny Dec 24, 2022
c122390
GH-91166: Implement zero copy writes for `SelectorSocketTransport` in…
kumaraditya303 Dec 24, 2022
2eea959
GH-91166: Implement zero copy writes for `SelectorSocketTransport` in…
kumaraditya303 Dec 24, 2022
0769f95
Misc Itertools recipe tweaks (GH-100493)
rhettinger Dec 24, 2022
bdfb694
gh-100357: Convert several functions in `bltinsmodule` to AC (#100358)
sobolevn Dec 24, 2022
c6dac12
Remove wrong comment about `repr` in `test_unicode` (#100495)
sobolevn Dec 24, 2022
00afa50
gh-99908: Tutorial: Modernize the 'data-record class' example (#100499)
JosephSBoyle Dec 24, 2022
46e6a28
gh-100474: Fix handling of dirs named index.html in http.server (GH-1…
Fraetor Dec 24, 2022
e4b43eb
gh-100287: Fix unittest.mock.seal with AsyncMock (#100496)
hauntsaninja Dec 24, 2022
f5b7b19
gh-99535: Add test for inheritance of annotations and update document…
MonadChains Dec 24, 2022
2e1a9ce
gh-100428: Make float documentation more accurate (#100437)
hauntsaninja Dec 24, 2022
efccd04
[Minor PR] Quotes in documentation changed into code blocks (#99536)
bartbroere Dec 24, 2022
046cbc2
gh-100472: Fix docs claim that compileall parameters could be bytes (…
hauntsaninja Dec 24, 2022
b9aa14a
gh-100519: simplification to `eff_request_host` in cookiejar.py (#99588)
glyph Dec 25, 2022
dbc1e69
gh-99308: Clarify re docs for byte pattern group names (#99311)
Kentzo Dec 25, 2022
ad3c99e
gh-92446: Improve argparse choices docs; revert bad change to lzma do…
yourlefthandman Dec 26, 2022
3ccc98f
Fix name of removed `inspect.Signature.from_builtin` method in 3.11.0…
Jackenmen Dec 26, 2022
199507b
gh-100520: Fix `rst` markup in `configparser` docstrings (#100524)
sobolevn Dec 26, 2022
ce39aaf
gh-99509: Add `__class_getitem__` to `multiprocessing.queues.Queue` (…
sobolevn Dec 27, 2022
b3da698
gh-94603: micro optimize list.pop (gh-94604)
eendebakpt Dec 27, 2022
b0ea289
Remove `NoneType` redefinition from `clinic.py` (#100551)
sobolevn Dec 27, 2022
3dc48da
gh-100553: Improve accuracy of sqlite3.Row iter test (#100555)
sobolevn Dec 28, 2022
08e5594
GH-98831: Modernize a ton of simpler instructions (#100545)
gvanrossum Dec 28, 2022
b8b8250
Merge remote-tracking branch 'origin/main' into regmachine
gvanrossum Dec 28, 2022
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
Prev Previous commit
Next Next commit
pythongh-94912: Added marker for non-standard coroutine function dete…
…ction (python#99247)

This introduces a new decorator `@inspect.markcoroutinefunction`,
which, applied to a sync function, makes it appear async to
`inspect.iscoroutinefunction()`.
  • Loading branch information
carltongibson authored Dec 18, 2022
commit 532aa4e4e019812d0388920768ede7c04232ebe1
25 changes: 23 additions & 2 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,36 @@ attributes (see :ref:`import-mod-attrs` for module attributes):

.. function:: iscoroutinefunction(object)

Return ``True`` if the object is a :term:`coroutine function`
(a function defined with an :keyword:`async def` syntax).
Return ``True`` if the object is a :term:`coroutine function` (a function
defined with an :keyword:`async def` syntax), a :func:`functools.partial`
wrapping a :term:`coroutine function`, or a sync function marked with
:func:`markcoroutinefunction`.

.. versionadded:: 3.5

.. versionchanged:: 3.8
Functions wrapped in :func:`functools.partial` now return ``True`` if the
wrapped function is a :term:`coroutine function`.

.. versionchanged:: 3.12
Sync functions marked with :func:`markcoroutinefunction` now return
``True``.


.. function:: markcoroutinefunction(func)

Decorator to mark a callable as a :term:`coroutine function` if it would not
otherwise be detected by :func:`iscoroutinefunction`.

This may be of use for sync functions that return a :term:`coroutine`, if
the function is passed to an API that requires :func:`iscoroutinefunction`.

When possible, using an :keyword:`async def` function is preferred. Also
acceptable is calling the function and testing the return with
:func:`iscoroutine`.

.. versionadded:: 3.12


.. function:: iscoroutine(object)

Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ asyncio
a custom event loop factory.
(Contributed by Kumar Aditya in :gh:`99388`.)

inspect
-------

* Add :func:`inspect.markcoroutinefunction` to mark sync functions that return
a :term:`coroutine` for use with :func:`iscoroutinefunction`.
(Contributed Carlton Gibson in :gh:`99247`.)

pathlib
-------
Expand Down
26 changes: 24 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"ismodule",
"isroutine",
"istraceback",
"markcoroutinefunction",
"signature",
"stack",
"trace",
Expand Down Expand Up @@ -391,12 +392,33 @@ def isgeneratorfunction(obj):
See help(isfunction) for a list of attributes."""
return _has_code_flag(obj, CO_GENERATOR)

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

def _has_coroutine_mark(f):
while ismethod(f):
f = f.__func__
f = functools._unwrap_partial(f)
if not (isfunction(f) or _signature_is_functionlike(f)):
return False
return getattr(f, "_is_coroutine_marker", None) is _is_coroutine_marker

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
return func

def iscoroutinefunction(obj):
"""Return true if the object is a coroutine function.

Coroutine functions are defined with "async def" syntax.
Coroutine functions are normally defined with "async def" syntax, but may
be marked via markcoroutinefunction.
"""
return _has_code_flag(obj, CO_COROUTINE)
return _has_code_flag(obj, CO_COROUTINE) or _has_coroutine_mark(obj)

def isasyncgenfunction(obj):
"""Return true if the object is an asynchronous generator function.
Expand Down
45 changes: 45 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,51 @@ def test_iscoroutine(self):
gen_coroutine_function_example))))
self.assertTrue(inspect.isgenerator(gen_coro))

async def _fn3():
pass

@inspect.markcoroutinefunction
def fn3():
return _fn3()

self.assertTrue(inspect.iscoroutinefunction(fn3))
self.assertTrue(
inspect.iscoroutinefunction(
inspect.markcoroutinefunction(lambda: _fn3())
)
)

class Cl:
async def __call__(self):
pass

self.assertFalse(inspect.iscoroutinefunction(Cl))
# instances with async def __call__ are NOT recognised.
self.assertFalse(inspect.iscoroutinefunction(Cl()))

class Cl2:
@inspect.markcoroutinefunction
def __call__(self):
pass

self.assertFalse(inspect.iscoroutinefunction(Cl2))
# instances with marked __call__ are NOT recognised.
self.assertFalse(inspect.iscoroutinefunction(Cl2()))

class Cl3:
@inspect.markcoroutinefunction
@classmethod
def do_something_classy(cls):
pass

@inspect.markcoroutinefunction
@staticmethod
def do_something_static():
pass

self.assertTrue(inspect.iscoroutinefunction(Cl3.do_something_classy))
self.assertTrue(inspect.iscoroutinefunction(Cl3.do_something_static))

self.assertFalse(
inspect.iscoroutinefunction(unittest.mock.Mock()))
self.assertTrue(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`inspect.markcoroutinefunction` decorator which manually marks
a function as a coroutine for the benefit of :func:`iscoroutinefunction`.