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 ...@@ -24,15 +24,16 @@ Features added
* When generators are used in a Cython module and the module imports the * When generators are used in a Cython module and the module imports the
modules "inspect" and/or "asyncio", Cython enables interoperability by modules "inspect" and/or "asyncio", Cython enables interoperability by
patching these modules during the import to recognise Cython's internal patching these modules during the import to recognise Cython's internal
generator type. This can be disabled by C compiling the module with generator and coroutine types. This can be disabled by C compiling the
"-D CYTHON_PATCH_ASYNCIO=0" or "-D CYTHON_PATCH_INSPECT=0" 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 * When generators or coroutines are used in a Cython module, their types
will be patched into the ``collections`` or ``collections.abc`` are registered with the ``Generator`` and ``Coroutine`` ABCs in the
stdlib module at import time if it is not there yet. It allows type ``collections`` or ``collections.abc`` stdlib module at import time to
checks for ``isinstance(obj, Generator)`` which includes both Python enable interoperability with code that needs to detect and process Python
generators and Cython generators. This can be disabled by C compiling the generators/coroutines. These ABCs were added in CPython 3.5 and are
module with "-D CYTHON_PATCH_ABC=0". See https://bugs.python.org/issue24018 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 * Adding/subtracting/dividing/modulus and equality comparisons with
constant Python floats and small integers are faster. constant Python floats and small integers are faster.
......
This diff is collapsed.
...@@ -95,12 +95,12 @@ def runloop(task): ...@@ -95,12 +95,12 @@ def runloop(task):
result = loop.run_until_complete(task()) result = loop.run_until_complete(task())
assert 3 == result, result assert 3 == result, result
import import_asyncio # patches Generator into ABCs if missing import import_asyncio
runloop(import_asyncio.wait3) # 1a) runloop(import_asyncio.wait3) # 1a)
import from_asyncio_import import from_asyncio_import
runloop(from_asyncio_import.wait3) # 1b) runloop(from_asyncio_import.wait3) # 1b)
import async_def # patches Awaitable/Coroutine into ABCs if missing import async_def
if ASYNCIO_SUPPORTS_COROUTINE: if ASYNCIO_SUPPORTS_COROUTINE:
runloop(async_def.wait3) # 1c) runloop(async_def.wait3) # 1c)
...@@ -117,7 +117,11 @@ if ASYNCIO_SUPPORTS_COROUTINE: ...@@ -117,7 +117,11 @@ if ASYNCIO_SUPPORTS_COROUTINE:
try: try:
from collections.abc import Generator from collections.abc import Generator
except ImportError: except ImportError:
from collections import Generator 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(from_asyncio_import.wait3(), Generator)
assert isinstance(import_asyncio.wait3(), Generator) assert isinstance(import_asyncio.wait3(), Generator)
...@@ -126,14 +130,22 @@ assert isinstance((lambda:(yield))(), Generator) ...@@ -126,14 +130,22 @@ assert isinstance((lambda:(yield))(), Generator)
try: try:
from collections.abc import Awaitable from collections.abc import Awaitable
except ImportError: except ImportError:
from collections import Awaitable 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) assert isinstance(async_def.wait3(), Awaitable)
try: try:
from collections.abc import Coroutine from collections.abc import Coroutine
except ImportError: except ImportError:
from collections import Coroutine 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) 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