Commit 7cad6432 authored by Stefan Behnel's avatar Stefan Behnel

remove ABC implementations from imports patching code and instead only...

remove ABC implementations from imports patching code and instead only register Cython's types with the ABCs *iff* they exist
also remove the inspect.iscoroutine() patching as it would only help new code which should use the ABCs in the first place
parent 2fa5bfa0
......@@ -24,15 +24,16 @@ Features added
* When generators are used in a Cython module and the module imports the
modules "inspect" and/or "asyncio", Cython enables interoperability by
patching these modules during the import to recognise Cython's internal
generator type. This can be disabled by C compiling the module with
"-D CYTHON_PATCH_ASYNCIO=0" or "-D CYTHON_PATCH_INSPECT=0"
* When generators are used in a Cython module, the new ``Generator`` ABC
will be patched into the ``collections`` or ``collections.abc``
stdlib module at import time if it is not there yet. It allows type
checks for ``isinstance(obj, Generator)`` which includes both Python
generators and Cython generators. This can be disabled by C compiling the
module with "-D CYTHON_PATCH_ABC=0". See https://bugs.python.org/issue24018
generator and coroutine types. This can be disabled by C compiling the
module with "-D CYTHON_PATCH_ASYNCIO=0" or "-D CYTHON_PATCH_INSPECT=0"
* When generators or coroutines are used in a Cython module, their types
are registered with the ``Generator`` and ``Coroutine`` ABCs in the
``collections`` or ``collections.abc`` stdlib module at import time to
enable interoperability with code that needs to detect and process Python
generators/coroutines. These ABCs were added in CPython 3.5 and are
available for older Python versions through the ``backports_abc`` module
on PyPI. See https://bugs.python.org/issue24018
* Adding/subtracting/dividing/modulus and equality comparisons with
constant Python floats and small integers are faster.
......
This diff is collapsed.
......@@ -95,12 +95,12 @@ def runloop(task):
result = loop.run_until_complete(task())
assert 3 == result, result
import import_asyncio # patches Generator into ABCs if missing
import import_asyncio
runloop(import_asyncio.wait3) # 1a)
import from_asyncio_import
runloop(from_asyncio_import.wait3) # 1b)
import async_def # patches Awaitable/Coroutine into ABCs if missing
import async_def
if ASYNCIO_SUPPORTS_COROUTINE:
runloop(async_def.wait3) # 1c)
......@@ -117,7 +117,11 @@ if ASYNCIO_SUPPORTS_COROUTINE:
try:
from collections.abc import Generator
except ImportError:
try:
from collections import Generator
except ImportError:
assert sys.version_info < (3,5), "Python 3.5+ should have collections.abc.Generator"
Generator = object # easy win :)
assert isinstance(from_asyncio_import.wait3(), Generator)
assert isinstance(import_asyncio.wait3(), Generator)
......@@ -126,14 +130,22 @@ assert isinstance((lambda:(yield))(), Generator)
try:
from collections.abc import Awaitable
except ImportError:
try:
from collections import Awaitable
except ImportError:
assert sys.version_info < (3,5), "Python 3.5+ should have collections.abc.Awaitable"
Awaitable = object # easy win :)
assert isinstance(async_def.wait3(), Awaitable)
try:
from collections.abc import Coroutine
except ImportError:
try:
from collections import Coroutine
except ImportError:
assert sys.version_info < (3,5), "Python 3.5+ should have collections.abc.Coroutine"
Coroutine = object # easy win :)
assert isinstance(async_def.wait3(), Coroutine)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment