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

asyncio.iscoroutinefunction returns false for Cython async function objects #2273

Closed
danigosa opened this issue May 17, 2018 · 32 comments · Fixed by #3427 or #4902
Closed

asyncio.iscoroutinefunction returns false for Cython async function objects #2273

danigosa opened this issue May 17, 2018 · 32 comments · Fixed by #3427 or #4902

Comments

@danigosa
Copy link

Referring to #2092

@scoder the implementation in asyncio is ridiculous

def iscoroutinefunction(func):
    """Return True if func is a decorated coroutine function."""
    return (inspect.iscoroutinefunction(func) or
            getattr(func, '_is_coroutine', None) is _is_coroutine)

So once we agreed inspect returns False, it happens to be that CPython just add a simple attribute to the function _is_coroutine, I think if Cython does this adding up this attribute it will make everything work

Here: https://github.com/python/cpython/blob/789e359f51d2b27bea01b8c6c3bf090aaedf8839/Lib/asyncio/coroutines.py#L152

And here:
https://github.com/python/cpython/blob/789e359f51d2b27bea01b8c6c3bf090aaedf8839/Lib/asyncio/coroutines.py#L160

So the solution as it can't be another it results in this, from my test in iPython:

In [46]: fut = init_connection()
In [47]: asyncio.iscoroutine(fut)
Out[47]: True

In [49]: init_connection.__class__
Out[49]: cython_function_or_method

In [50]: asyncio.iscoroutinefunction(init_connection)
Out[50]: False

In [56]: from asyncio.coroutines import _is_coroutine

In [57]: init_connection._is_coroutine = _is_coroutine

In [58]: asyncio.iscoroutinefunction(init_connection)
Out[58]: True

Dirty but easy. Don't know if this is a 5 seconds issue or a hara-kiri, theoretically it should be easy to plug the attribute runtime from from asyncio.coroutines import _is_coroutine, or not.

This is a super annoying problem (I don't blame anyone with the word bug) that make cythonized code to be very dramatic in several asyncio frameworks. asyncio.iscoroutine work well with cythonized funcs for the silliest reason I think.

Following the cyfunctions above, create_token is NOT a coroutine, while init_connection is an async function, this workaround works in both so it's ok if you can schedule the functions whether are coros or not, no way to easily fix this with asyncio.iscoroutinefunction or at least easier than that:

In [59]: async def test():
    ...:     await create_token({})
    ...:
In [61]: asyncio.get_event_loop().run_until_complete(test())
TypeError: object str can't be used in 'await' expression

In [73]: async def test():
    ...:     return await init_connection()
Out[74]: <asyncpg.pool.Pool at 0x7f78e04752c8>

Here comes the magic:

In [69]: async def test():
    ...:     return await asyncio.coroutine(create_token)({})
In [70]: asyncio.get_event_loop().run_until_complete(test())
Out[70]: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1MjU1NTk0NTB9.v8wRZGIke6RYizIpJhl4oaypKyvuVARKaiq0KdpJg6XJ0qTB0o76BuTLera6kSQ_5qXnAb7_9DQSadwdRqgPmw'

In [71]: async def test():
    ...:     return await asyncio.coroutine(init_connection)()
In [72]: asyncio.get_event_loop().run_until_complete(test())
Out[72]: <asyncpg.pool.Pool at 0x7f78df48af48>

With that the workaround:

def nasty_iscoroutinefunction(func):
      is_coro = asyncio. iscoroutinefunction(func)
      if func.__class__.__name__ == 'cython_function_or_method':
            # It's cythonized, danger!
            is_coro = True  # We'll make it possible
            func = asyncio.coroutine(func)
      return is_coro

This works!

@danigosa
Copy link
Author

danigosa commented Jun 10, 2018

Found an interesting, better and working workaround:

https://github.com/timothycrosley/hug/blob/develop/hug/introspect.py#L33

def is_coroutinefunction(function):
    """Returns True if the passed in function is a coroutine"""
    return function.__code__.co_flags & 0x0080 or getattr(function, '_is_coroutine', False)

@pohmelie
Copy link

Is there plans to fix this?
@danigosa, AFAIR you suggest to replace asyncio.iscoroutinefunction in asyncio module, but I think this should be fixed on cython side instead, so the original asyncio method will work as expected.

@pohmelie
Copy link

Hm, it looks like tests with code similar to inspect.py are disabled.

@scoder
Copy link
Contributor

scoder commented Aug 18, 2018

The problem is that if we make Cython coroutines look too much like Python coroutines at the C level (e,g, by setting the CO_COROUTINE flag on them), then CPython might start doing things with them that it shouldn't. Even if we find that it doesn't do that right now, it might change at any time, and then people have their Cython modules out there that break. So this is risky business.

Similarly, unconditionally patching inspect might have unfortunate side effects as well, even if requires an opt-in. It's impossible to guess what mixture of code end users will have on their systems.

@pohmelie
Copy link

@scoder
I'm half way to make PR to add CO_COROUTINE to coroutine wrappers. But if it will not be accepted anyway, I will stop.

AFAIR, right now c wrapper looks like:

def c_wrapper(...):
    return coroutine(...)

It looks like this can be changed to

@types.coroutine
def c_wrapper(...):
    return (yield from coroutine(...))

In this case all stuff is on python shoulders.

@pohmelie
Copy link

pohmelie commented Aug 20, 2018

@scoder
I have made some dirty hack and it looks like it works. Any thoughts about such solution?
pohmelie@5a84869

import asyncio


async def foo():
    pass


print("foo", foo.__code__.co_flags)
print("foo", asyncio.iscoroutinefunction(foo))
print(__cython_coroutine_wrapper)

compiled code output:

('foo', 319)
('foo', True)
<function coroutine at 0x7f30b9a0e620>

@pohmelie
Copy link

Linked code is not working in all cases, it is just quick proof of concept.

@pohmelie
Copy link

By the way, I can't find any problem with add CO_COROUTINE flag, since

async def afoo():
    ...

is same as

def foo():
    return afoo()

you can await foo() and it will be absolutely same as await afoo().

@scoder
Copy link
Contributor

scoder commented Aug 21, 2018

There is at least this code in CPython, which I'd rather not want to trigger for Cython coroutines:
https://github.com/python/cpython/blob/caba55b3b735405b280273f7d99866a046c18281/Python/ceval.c#L3906-L3949
Can't say right now if it's possible to do that from Python code, probably via eval&friends.

Also, what we are discussing here actually applies to these three functions in inspect.py: isgeneratorfunction(), iscoroutinefunction() and isasyncgenfunction().

https://github.com/python/cpython/blob/41254ebd5e4f40a2e095d8aaea60bf3973de4647/Lib/inspect.py#L171-L194

@pohmelie
Copy link

pohmelie commented Aug 21, 2018

But why old-style yield from coroutines with asyncio.coroutine works fine? Why we can't just wrap new-style coroutines with same decorator on building ast step?

@keakon
Copy link

keakon commented Jun 11, 2019

@scoder Can you consider adding new flags like CO_CYTHON_COROUTINE?

I'm using Starlette, it requires asyncio.iscoroutinefunction() to distinguish coroutine functions. I can't fix it because Cython hides everything about coroutine functions.

@scoder
Copy link
Contributor

scoder commented Jun 11, 2019

Can't starlette do a type-check against the Coroutine ABC instead?

@keakon
Copy link

keakon commented Jun 12, 2019

How can I know a function will return a coroutine without call it?

@scoder
Copy link
Contributor

scoder commented Jun 12, 2019

How can I know a function will return a coroutine without call it?

You cannot. There is no way to do that. It's called the "halting problem".
https://en.wikipedia.org/wiki/Halting_problem

@keakon
Copy link

keakon commented Jun 12, 2019

But CPython can:

>>> async def f():
...   pass
...
>>> import inspect
>>> inspect.iscoroutinefunction(f)
True
>>> import asyncio
>>> asyncio.iscoroutinefunction(f)
True

@scoder
Copy link
Contributor

scoder commented Jun 12, 2019

No, it can't:

>>> async def func(): pass
>>> import inspect
>>> inspect.iscoroutinefunction(func)
True
>>> def retfunc(): return func()
>>> inspect.iscoroutinefunction(retfunc)
False
>>> type(func())
<class 'coroutine'>
>>> type(retfunc())
<class 'coroutine'>

@dhiltonp
Copy link

Is there currrently a way to test if a cython function or method is async? Without that, we can't programmatically fix it even on a case-by-case basis...

@scoder
Copy link
Contributor

scoder commented Sep 14, 2019

There doesn't have to be anything special about a callable that returns an awaitable. It does not have to be an async function. It can be any callable, even including a class. The idea of guessing the return type of a callable, without actually looking at the type of the object that it returns, seems inherently flawed to me.

IMHO, the work-around is to call the callable and look at the value it returns. If that is something to await, then await it. If not, then not.

However, as a clean fix, I would suggest to always require/pass/return something that is awaitable, and not handle the callables differently based on their type.

@pohmelie
Copy link

pohmelie commented Sep 14, 2019

the work-around is to call the callable and look at the value it returns

I think it is bad. Since this promotes bad practice of callable returns awaitable vs awaitable is callable. This should not work like this.

@dhiltonp
Copy link

I don't really care that a regular function may return an awaitable, I care about compatibility (or a possibility of compatibility) with CPython. For CPython, iscoroutinefunction returns true if the function was defined with async. As far as I can tell, Cython does not expose this information in any way.

There may be reasons for object.__code__.co_flags & CO_COROUTINE to not be true for async functions in Cython - but at a minimum there must be a programmatic way to determine that a function was defined with async.

@scoder
Copy link
Contributor

scoder commented Sep 15, 2019

I'll accept a PR that adds an _is_coroutine flag to CyFunction and sets it for async functions.

There are already tests in test_coroutine_pep492.pyx that have their call to inspect.iscoroutinefunction() commented out. Note that this file is copied from CPython, so don't change it too much. Just enable those lines for the CPython versions (sys.version_info) that support them.

EDIT: sorry, we are aiming for asyncio.iscoroutinefunction() in this ticket, not inspect.iscoroutinefunction(). The test to extend is tests/run/asyncio_generators.srctree.

@scoder
Copy link
Contributor

scoder commented Sep 15, 2019

@pohmelie "callable returns awaitable vs awaitable is callable" – note that for async functions, the callable is the function, and the awaitable is the return value of the call. An async function (object) is not awaitable.

@scoder scoder changed the title Reopen: asyncio.iscoroutinefunction not working (workaround) asyncio.iscoroutinefunction returns false for Cython async function objects Sep 15, 2019
@fornellas
Copy link

There doesn't have to be anything special about a callable that returns an awaitable.
(...)
IMHO, the work-around is to call the callable and look at the value it returns. If that is something to await, then await it. If not, then not.

@scoder for good or bad, Python really does not make a lot of distinctions between async / sync functions. When writing tests though, we do mock things that maybe sync / async and calling them to see if they are sync / async is just not an option. Knowing before calling, is extremely useful, as it enables mocks to detect bugs (eg: mocking an async function with a sync function).

This is how I bumped into this bug. I work on TestSlide, which Facebook open sourced last year. The gist of it is that its mocks have significantly more interface validation than vanilla Mock(spec=...). When asyncio support was added recently, iscoroutinefunction was used to validate mocking of sync / async methods, which turned out to be completely broken for Thrift clients, which are all Cython async.

Perhaps having cython.iscoroutinefunction would be simpler to implement, so we can start using right on? I have limited bandwidth at the moment to deeply dig into this, but with some nice pointers to where to look, I might be able to craft a PR for this.

@dhiltonp
Copy link

dhiltonp commented Dec 10, 2019

Just for tracking, the corresponding python issue:

https://bugs.python.org/issue38225

And a related cython issue: #3143

scoder pushed a commit that referenced this issue Sep 5, 2020
…es. (GH-3427)

Python's asyncio.coroutines uses an object to tag objects as coroutine functions. We now read this object and use it to tag Cython compiled coroutines as well.

It also includes tests to make sure `asyncio.iscoroutinefunction()` works as expected.
This doesn't fix `inspect.iscouroutinefunction()` (which uses a flag that can trigger undesirable behaviour for cython functions).

Closes #2273
@scoder scoder added this to the 3.0 milestone Sep 28, 2020
sairam4123 added a commit to sairam4123/cython that referenced this issue Nov 4, 2020
* Fix test in Py2.

* Check for exceptions also when @returns() is used, not only for "->" return type annotations. (cythonGH-3664)

When you use Python type annotations, it would be weird if you lost Python exception propagation semantics along the way, just by compiling the code. So the default behaviour is "except? -1" here for C integer types.

Arguably, this would also be a better default for the decorator case.

See cython#3625 (comment)

* Add tests that exception propagation works with the "@returns()" decorator.

* Update changelog.

* Allow selecting specific doctests in test modules with "-k pattern", instead of always running all tests.

* Correct the positions reported for errors in f-strings.
Closes cython#3674

* Fix f-string error positions in nogil test.

* Clarify backwards incompatible special method change in changelog.

* Fix many indentation and whitespace issues throughout the code base (cythonGH-3673)

… and enforce them with pycodestyle.

* Allow selecting specific doctests in test modules with "-k pattern", instead of always running all tests.

* Prepare release of 0.29.20.

* Update changelog.

* Avoid calling PyUnicode_FromUnicode() in Py3.
Closes cython#3677

* Update changelog.

* Minor code simplification.

* Always bind Cython functions

* Update changelog.

* Avoid an "unused variable" warning for code that gets compiled out in Py3.

* Minor code simplification.

* Revert "Always bind Cython functions"

This reverts commit 6677326.

* Update changelog.

* Revert "Limited API updates and cleanup for cython#2056. cythonGH-3635)"

This reverts commit 02bb311.

* Revert "Invoke binop super method via direct slot access."

This reverts commit bcb9387.

* Revert "Add support for pow operator."

This reverts commit d849fb2.

* Revert "Python-style binary operation methods."

This reverts commit e6a8124.

* Re-add "c_api_binop_methods" directive for backwards compatibility after reverting cython#3633 and force it to "False".
Closes cython#3688.

* Change the default of the "c_api_binop_methods" directive to False. (cythonGH-3644)

This is a backwards incompatible change that enables Python semantics for special methods by default, while the directive allows users to opt out of it and go back to the previous C-ish Cython semantics.

See cython#2056

* Always bind Cython functions in Py3 (cythonGH-3683)

Rebased 1bb26b9 for cython 0.29.x,
and made conditional to Python 3.  This does not solve the original staticmethod problem
in Python 2 but it does resolve classmethod problems in Python 3.9.  Therefore, it unbreaks
other packages on Python 3.9, improves overall state for Python 3 and does not introduce
regressions in Python 2.

* Limit the scope of a local variable in a helper function.

* Avoid a C compiler warning about a constant condition.

* Remove dead code.

* Update changelog.

* Add support for C++ scoped enums with "enum class" and "enum struct" (cythonGH-3640)

Closes cython#1603.

* Add missing "PyUnicode_GET_LENGTH" to unicode.pxd (cythonGH-3692)

* Add missing "PyUnicode_GET_LENGTH" to unicode.pxd (cythonGH-3692)

* Fix usage of deprecated Py_UNICODE API.

* Add safety fix to avoid reading a character from the empty string.

* Fix prefix of internal function name.

* Fix definition of "__Pyx_CyFunction_USED", which should only be #defined and not have a value.

* Avoid using "tp_name" when CYTHON_COMPILING_IN_LIMITED_API (cythonGH-3693)

* exec() did not allow recent Python syntax features in Py3.8+ due to https://bugs.python.org/issue35975
Closes cython#3695

* Avoid using the "tp_iternext" slot when CYTHON_USE_TYPE_SLOTS is disabled.

* Give the "__Pyx_PyObject_GetIterNext" helper macro a more explanatory name.

* Avoid unused variable in PyPy etc.

* Avoid a call to PyTuple_GET_ITEM() to get the item array pointer if CYTHON_ASSUME_SAFE_MACROS is disabled.
See cython#3701

* Enable travis for all branches.

* Fix indentation counter for module init function.

* Fix FunctionState handling for module cleanup function.

* Validate that all temps were correctly released at the end of a function.

* Keep reference to module dict around also in limited mode (cythonGH-3707)

`PyModule_GetDict` is part of the limited API so we can keep a reference
to the module dict around regardless of limited mode being enabled or not.

* Validate and fix temp releasing (cythonGH-3708)

* Fix a temp leak in the type init code.
* Fix temp leaks in fused types initialisation code.
* Correctly release the buffer index temps allocated for index calculations.
* Make tests fails hard if a temp variable is not released at the end of a generated function.
* Fix temp leak in switch statement code.
* Make end-to-end tests fail on refnanny output.
* Fix result temp leak in PyTypeTestNode.
* Fix result temp leak in external type/function import code and enable the refnanny check for them.
* Fix temp leak when try-return-finally is used in generators.
* Make it explicit when an allocated temp is not meant to be reused.
* Fix temp leak when assigning to the real/imag attributes of complex numbers.
* Fix temp leak when assigning to a memoryview slice.
* Clean up "num_threads" result temp in parallel section, not only in prange loop.
* Fix temp leak in Pythran buffer setitem code.
* Simplify NumPyMethodCallNode since it does not need the Python function anymore. Previously, it generated code that needlessly looked up the Python function without actually using it.
* Fix temp leak when deleting C++ objects.
* Add a test that actually reusing temps when deleting C++ objects works correctly.

* Test runner: disable keep-alive output in --debug mode to keep a potential pdb console clean.

* Fix argument name usage in finally blocks (cythonGH-3713)

Fixes cython#3712 (hopefully) by reverting a small part of bbef4d7

* Fix argument name usage in finally blocks (cythonGH-3713)

Fixes cython#3712 (hopefully) by reverting a small part of bbef4d7

* Document version-tagged pxd files (cythonGH-3587)

* Implement generic optimized loop iterator with indexing and type inference for memoryviews (cythonGH-3617)

* Adds bytearray iteration since that was not previously optimised (because it allows changing length during iteration).
* Always set `entry.init` for memoryviewslice.

* Update changelog.

* Make end-to-end tests fail on refnanny output.

* Fix FunctionState handling for module cleanup function.

* Update change log.

* Really only use PyUnicode_FromUnicode() when needed (cythonGH-3697)

* Really only use PyUnicode_FromUnicode() for strings that contain lone surrogate, not for normal non-BMP strings and not for surrogate pairs on 16bit Unicode platforms.

See cython#3678

* Extend buildenv test to debug a MacOS problem.
* Add a test for surrogate pairs in Unicode strings.
* Limit PyUnicode_FromUnicode() usage to strings containing lone surrogates.
* Accept ambiguity of surrogate pairs in Unicode string literals when generated on 16bit Py2 systems.

* Disable testing against NumPy 1.19+ in the 0.29.x branch, which removed C-API features.

* Disable testing against NumPy 1.19.0 in the 0.29.x branch, which breaks a C-API call.

* Validate and fix temp releasing (cythonGH-3708) (cythonGH-3717)

* Validate and fix temp releasing (cythonGH-3708)
Backports 92147ba.

    * Fix a temp leak in the type init code.
    * Fix temp leaks in fused types initialisation code.
    * Correctly release the buffer index temps allocated for index calculations.
    * Make tests fails hard if a temp variable is not released at the end of a generated function.
    * Fix temp leak in switch statement code.
    * Make end-to-end tests fail on refnanny output.
    * Fix result temp leak in PyTypeTestNode.
    * Fix result temp leak in external type/function import code and enable the refnanny check for them.
    * Fix temp leak when try-return-finally is used in generators.
    * Make it explicit when an allocated temp is not meant to be reused.
    * Fix temp leak when assigning to the real/imag attributes of complex numbers.
    * Fix temp leak when assigning to a memoryview slice.
    * Clean up "num_threads" result temp in parallel section, not only in prange loop.
    * Fix temp leak in Pythran buffer setitem code.
    * Simplify NumPyMethodCallNode since it does not need the Python function anymore. Previously, it generated code that needlessly looked up the Python function without actually using it.
    * Fix temp leak when deleting C++ objects.
    * Add a test that actually reusing temps when deleting C++ objects works correctly.

* Improve syntax feature support of Cython CodeWriter (cythonGH-3514)

* Disable Py_UNICODE fallback for __Pyx_UnicodeContainsUCS4() in Py3.9 since Py_UNICODE is deprecated and PEP-393 unicode is practically required.

* Avoid calling PyUnicode_FromUnicode() in Py3 (cythonGH-3721)

See cython#3677

* Add missing name substitution in __Pyx_PyUnicode_Substring() utility code.

* Update changelog.

* Prepare release of 0.29.21.

* Really only use PyUnicode_FromUnicode() when needed (cythonGH-3697)

* Really only use PyUnicode_FromUnicode() for strings that contain lone surrogate, not for normal non-BMP strings and not for surrogate pairs on 16bit Unicode platforms.

See cython#3678

* Extend buildenv test to debug a MacOS problem.
* Add a test for surrogate pairs in Unicode strings.
* Limit PyUnicode_FromUnicode() usage to strings containing lone surrogates.
* Accept ambiguity of surrogate pairs in Unicode string literals when generated on 16bit Py2 systems.

* Disable Py_UNICODE fallback for __Pyx_UnicodeContainsUCS4() in Py3.9 since Py_UNICODE is deprecated and PEP-393 unicode is practically required.

* Fix test in 16-bit Unicode deployments.

* Update changelog.

* Always consider 0-sized arrays as C- and F-contiguous (cythonGH-3728)

Fixes cython#2093

* Always consider 0-sized arrays as C- and F-contiguous (cythonGH-3728)

Fixes cython#2093

* Update changelog.

* Add missing unordered_map template defaults (cythonGH-3686)

* Add missing unordered_map template defaults (cythonGH-3686)

* Update changelog.

* Improve test output in case of failures.

* Using Py_UNICODE to store lone surrogates makes Py3 join surrogate pairs on 16-bit Unicode platforms (Windows) when reading them back in, although we correctly processed them before.
Instead, we now use the "unicode_escape" codec to store byte strings, because it can return surrogate characters (which the other codecs cannot).

* Disable test in Py2.6.

* Update changelog.

* Use the more appropriate CYTHON_USE_TYPE_SLOTS guard for accessing the binop number slot.

* Fix typo in error message.

* Update the documentation on the arithmetic special methods and issue a "backwards compatibility" warning when the reversed method is not implemented.

See cython#2056

* Add the new "c_api_binop_methods" directive to the documentation.

* PyPy does not support PyType_GetSlot(). Use type slots instead.

* Reformat doc paragraph.

* Document C inline properties.

* Beautify example output.

* Use inline properties on the "PyComplex" builtin type declared in "cpython.complex" to provide C level access to the "real" and "imag" attributes (which Cython provides anyway for the 'undeclared' builtin type).

* Prevent compile error when the result of repr() is assigned to a "unicode" LHS with language_level=3.
Closes cython#3736

* Handle `for x in cpp_function_call()` (cythonGH-3667)

Fixes cython#3663

This ensures that rvalues here are saved as temps, while keeping the
existing behaviour for `for x in deref(vec)`, where the pointer for vec
is copied, meaning it doesn't crash if vec is reassigned.

The bit of this change liable to have the biggest effect is that I've
changed the result type of dereference(x) and x[0] (where x is a c++
type) to a reference rather than value type. I think this is OK because
it matches what C++ does. If that isn't a sensible change then I can
probably inspect the loop sequence more closely to try to detect this.

* Clarify changelog entry on ways to deal with the incompatible binop method change.

* Remove Google Analytics script from documentation to avoid tracking our users.

* Add donation banner to documentation.

* Create bug template

* Update issue templates

* Delete unused custom ticket template

* Add handshake emoji to donations banner to make it more visible.

* Don't create CReference in C (only c++) (cythonGH-3746)

* Fixed reference types being passed to getitemint (cythonGH-3755)

* Reorder test module to restore the "invalid - valid" order.

* Update changelog.

* Prepare release of 3.0a6.

* Add test comments on how "memslice.pyx" and "memoryview.pyx" relate.

* Print test dependency versions to help with test failure debugging.

* Add CI builds for different CPU architectures on travis.

* Update issue templates

* Add type "Py_hash_t" in pure Python mode.

* Update changelog.

* Clarify changelog entry.

* Remove dead code and dead comments from "numpy/__init__.pxd".

* Change "Py_intptr_t" declaration in numpy.pxd to what CPython uses as fallback. "int" seems overly lazy if it tends to be larger on many systems.

* Readability improvements in "numpy.pxd".

* Add a comment that numpy.pxd is maintained by the NumPy project.

* Try to fix NumPy test failures by not setting the "NPY_NO_DEPRECATED_API" #define for NumPy 1.19.[01].

* Use NumPy 1.18.x for testing on travis as long as 1.19.[01+] ships a numpy.pxd that is incompatible with Cython 3.0.

* Remove useless "extern" modifiers from cdef classes declared in "extern" sections.

* Do not depend on the default type inference in "cpython/array.pxd".

* Avoid unused C variable warning by moving declaration and usage under the same condition.
Closes cython#3763

* Use a generic shape accessor for pythranized array (cythonGH-3762)

This is a follow up to cython#3307

* Silence a C compiler warning about a constant condition.

* Remove an unused function that had been added for the now-deleted "__getbuffer__" implementation.

* Support PEP-560 ("__class_getitem__") for extension classes (cythonGH-3765)

* Add "make checks" target to run code checks.

* In bug template, ask for Python version in addition to Cython version

* Support simple, non-strided views of "cython.array".
Closes cython#3775

* Remove unused cimports.

* Update changelog.

* Set PYTHONHOME in embedding test to fix compilation issues in Py3.8/macOS.

* Fix unrelated test after changing MemoryView.pyx.

* Revert "Set PYTHONHOME in embedding test to fix compilation issues in Py3.8/macOS."

This reverts commit a333d6a.

The change did not resolve the test issue in travis.

* Fix Python 3.4 + MSVC issue with elaborated type specifier for enum (cythonGH-3782)

* Add more cimport_from_pyx tests (cythonGH-3786)

There's now a fairly wide range of valid syntax for declaring things in pyx files and it should all be supported when cimporting from them.

* Split known types into separate lines to let them have their own VCS history.

* Add a note on PayPal fees for small payments.

* Avoid merged-in code picking up directives from main module (cythonGH-3785)

Fixes cython#1071

* Fix cygdb (cythonGH-3542)

* Cython debugger documentation: Added link to an installation script.
* Got a new libpython.py from the cpython source distribution.
* Default language level in tests is now 3 instead of 2
* Migrated codefile from python 2 to python 3.
* Added testcase for the cy list command in cygdb.
* Temporarily removing test case that freezes gdb.
* Fixed a bug that broke several Cygdb tests.

The cython_debug/cython_debug_info_* files map the names of the C-functions generated by the Cython compiler to the names of the functions in the *.pyx source. If the function was defined using "def" (and not "cpdef" or "cdef") in the *.pyx source file, the C-function named in cython_debug/cython_debug_info_* used to be __pyx_pw_*, which is the name of the wrapper function and now it is __pyx_f_*, which is the name of the actual function. This makes some Cygdb tests pass that did not pass before.

* Better error messages: If a cygdb command raises, a traceback will be printed.
* Fixed a bug in cygdb.

The following now works:
1. Start cygdb
2. Type "cy exec" and hit enter
3. Type some other lines
4. Type "end" and hit enter.
-> These "other lines" will get executed

* Fixed a bug in cygdb: cy list now works outside of functions.
* Added print_hr_allmarkers function for easier debugging.
* Fixed a bug that broke cygdb:

cy break did not work if you put the breakpoint outside of a function if there was e.g. the following somewhere in your *.pyx file:
cdef class SomeClass():
    pass

* Added a Cygdb test for printing global variables.
* Fixing cygdb: Replaced cy print with a simple, working solution.
* If an exception in Cygdb occurs, a stacktrace will be printed.
* Fixed a bug that broke cy break -p
* Bugfix: The compiler now writes out correctly which cython linenumber and path corresponds to which c linenumber.
* Set language_level=2 in runtests.py

* Update changelog.

* Add a "gdb" test tag that depends on being able to run gdb.

* Update CPython "test_fstring" copy to Py3.9.

* Improved documentation for annotation typing (cythonGH-3799)

Mainly by moving it to a separate section to make it easier
to find, however also added a small amount of extra information
about some of the obvious limitations.

* Fix `return None` in CodeWriter. (cythonGH-3795)

* Rename "GCC_DIAGNOSTIC" macro to make it Cython specific and make it available to other utility code sections.

* Add a test for unpacking large PyLong values.

* Silence gcc diagnostics whenever -1 is cast to something user provided. (cythonGH-3803)

Fixes cythonGH-2749.

* Define extern `PyBUF_MAX_NDIM` (cythonGH-3811)

Ensure that Cython exposes `PyBUF_MAX_NDIM` from Python as part of
`cpython.buffer` to allow access to developers.

* Change test to use only integer calculations to prevent platform specific rounding issues, while keeping a reasonable distribution of integers across the PyLong digit ranges.

* Created a glossary and added one new entry (cythonGH-3810)

* Call destructors for structs in C++ (cythonGH-3228)

In C++ mode, structs can contain C++ classes. Therefore structs
should have their destructors called to ensure any class
contained is also destructed.

Also, a bit more thorough about ensuring constructor is generated where necessary.

Closes cythonGH-3226.

* Rename "GCC_DIAGNOSTIC" macro to make it Cython specific and make it available to other utility code sections.

* Silence gcc diagnostics whenever -1 is cast to something user provided. (cythonGH-3803)

Fixes cythonGH-2749.

* Allow cast to ctuple (cythonGH-3812)

* Make asyncio.iscoroutinefunction() recognise Cython compiled coroutines. (cythonGH-3427)

Python's asyncio.coroutines uses an object to tag objects as coroutine functions. We now read this object and use it to tag Cython compiled coroutines as well.

It also includes tests to make sure `asyncio.iscoroutinefunction()` works as expected.
This doesn't fix `inspect.iscouroutinefunction()` (which uses a flag that can trigger undesirable behaviour for cython functions).

Closes cython#2273

* Do not cover lines that were excluded in the coveragerc config file (cythonGH-3682)

Closes cython#3680.

* Add doc support for cpdef enum (cythonGH-3813)

* Update changelog.

* Add "check_size ignore" to all NumPy.pxd class declarations to silence the useless size warnings about them.
See numpy/numpy#432 (comment)
Also remove the useless "extern" modifiers from cdef classes declared in "extern" sections.

* Update changelog.

* Update changelog.

* Support utility code in headers (cythonGH-3779)

Specifically this allows public packed structs but may also allow other public declarations that require small amounts of utility code.

* Prevent overflowing hash values for "Machines.Node" due to arbitrarily large unsigned pointer values.
Closes cython#3840

* Extract "error_type" handling from the type creation functions.

* Remember in the AnnotationsWriter when a serialised expression contained unknown/unsupported nodes.

* Do not include "u" string prefix in annotations since tools that process them probably expect Py3 string semantics anyway.

* Keep AnnotationNode instead of the bare annotation expression in "entry.annotation" to get access to the string representation.

* Fix test after removing the 'u' prefix from unicode string annotation values.

* Add support for PEP 526 `__annotations__` in class body. (cythonGH-3829)

Closes cython#2552

* Some more glossary entries (cythonGH-3836)

* Rename test file to avoid ambiguity with the C "const" modifier.

* Join '*' and '**' parsing in declarators to avoid differences for 'const' parsing etc.

* Set language level in Cython's speed-up .pxd files since it no longer depends on the .py files that they correspond to.

* Declare "scanner.sy" as "unicode" string to optimise its usage.

* Use more recent C-API functions on tear-down of the embedding code.

* Improve some wordings in README.rst (cythonGH-3852)

* Restore Py2 compatibility in test.

* Replace useless comment.

* Fixed assorted crashes with fused types (cythonGH-3853)

Show a more detailed error for unspecializable fused return types.

* Add a more common and more versatile example to the Verbatim C-Code documentation.

* Fix RST typo.

* Make C code C89 again.

* Add support for forwarding references (cythonGH-3821)

See, for example, https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers

Closes cython#3814

* Avoid C compiler warnings about unused variables in test.

* Avoid C compiler warning about unused variable in test.

* Test Py3.7 and Py3.8 in C and C++ mode under appveyor.
Run both in the same job since the machines are quite fast with parallel processes, but setting one up is slow.

* Fix test compile failure in MSVC.

* Allow creation of wrappers for cdef functions with memoryviews (cythonGH-3856)

Fixes cython#3843

* Clean up and test type identifier escaping.
- hash() hashing lead to unpredictable random prefixes for long names across multiple runs
- use a single regex run instead of repeated calls to replace()

* Move a memoryview test out of the "run" test directory since memoryview tests tend to be slow and have their own test directory.

* Fix test compile failure in MSVC.

* Clarify the section on exception return values.

* Be a bit more paranoid about macro usage in the refnanny code.

* Looks like the usual macro on Windows is "_WIN32" and not "WIN32". Let's support both, to be on the safe side.

* Try to fix platform specific test once more.
"synchapi.h" is not supposed to be included directly, and "windows.h" can break ... other stuff.
Let's see what we can do.

* Fix ReST typo.

* docs: fix typos, minor clarification.

* Minor docs clarification on error return values. (cythonGH-3859)

* Replace deprecated Element.getiterator() with Element.iter(). (cythonGH-3864)

`xml.etree.ElementTree.Element.getiterator()` was deprecated in Python
2.7 & 3.2 and removed in the freshly released Python 3.9.

* Prefer Element.iter() over the deprecated Element.getiterator(). (cythonGH-3865)

`xml.etree.ElementTree.Element.getiterator()` was deprecated in Python
2.7 & 3.2 and removed in Python 3.9.

* Split a combined assert in two to avoid an unused C temp variable when assertions are disabled. (cythonGH-3870)

* Split a combined assert in two to avoid an unused C temp variable when assertions are disabled. (cythonGH-3870)

* Disable a test that fails in Py2 in 0.29.x since 'str' is 'unicode' in cython3, also in Python 2.

* Rename test file to avoid ambiguity with the C "const" modifier.

* Use more recent C-API functions on tear-down of the embedding code.

* Make type identifier escaping deterministic: hash() hashing lead to unpredictable random prefixes for long names across multiple Python runs.

* Clarify the section on exception return values.

* Fix ReST typo.

* docs: fix typos, minor clarification.

* Minor docs clarification on error return values. (cythonGH-3859)

* Add Python 3.9 to appveyor build.

* Resolve merge conflict.

* Removed `same_as` methods from Plex.Actions.Action (cythonGH-3847)

It seems to be unused and it looked like the existing implementation was faulty.

* Fix test after changing the hash method used for escaping long type descriptions.

* Add warning for common user mistake and fix crash relating to annotated attributes. (cythonGH-3850)

Closes cython#3830

* Improve error reporting when users mistakenly write "&&" or "||" instead of Python's "and" and "or" operators. (cythonGH-3858)

* Disable the embedding test on MacOS-X to get the builds green again. Too many PRs depend on the travis tests to give a green light.

* Simplify the output stream capturing for the C compiler runs by using a temp file instead of threads.

* Help reporters see the first paragraph of the bug report template.

* Detect _MSC_VER for __PYX_STD_MOVE_IF_SUPPORTED to support MSVC support even when "__cplusplus" is not set appropriately (cythonGH-3792)

* Fix memoryview casts involving fused types (cythonGH-3882)

I think this approach is more satisfactory than the old way it used to "work", where "fused_to_specific" was permanently added to the module scope containing the fused type (in this case the Cython scope), was applied in "Scope.lookup_type", but continued to have an effect on the scope forever.

Closes cython#3881

* Add O_DIRECT to posix/fcntl.pxd (cythonGH-3894)

Closes cython#3242

Co-authored-by: Stefan Behnel <stefan_ml@behnel.de>
Co-authored-by: Jeroen Demeyer <jeroen.k.demeyer@gmail.com>
Co-authored-by: Robert Bradshaw <robertwb@gmail.com>
Co-authored-by: Michał Górny <mgorny@gentoo.org>
Co-authored-by: Ashwin Srinath <3190405+shwina@users.noreply.github.com>
Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
Co-authored-by: Matthias Braun <matze@braunis.de>
Co-authored-by: da-woods <dw-git@d-woods.co.uk>
Co-authored-by: Tao He <sighingnow@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Clemens <chofreither@gmail.com>
Co-authored-by: will <timwilloney@gmail.com>
Co-authored-by: serge-sans-paille <serge.guelton@telecom-bretagne.eu>
Co-authored-by: Yuan <pyslvs@gmail.com>
Co-authored-by: Volker-Weissmann <39418860+Volker-Weissmann@users.noreply.github.com>
Co-authored-by: Tao He <linzhu.ht@alibaba-inc.com>
Co-authored-by: cf-natali <cf.natali@gmail.com>
Co-authored-by: jakirkham <jakirkham@gmail.com>
Co-authored-by: Tasha "Ren" Chin <tasha.s.chin@gmail.com>
Co-authored-by: Pedro Marques da Luz <pmdaluz@gmail.com>
Co-authored-by: Mathias Laurin <Mathias.Laurin+github.com@gmail.com>
Co-authored-by: matham <moiein2000@gmail.com>
Co-authored-by: Sean <stewart.d.sean@gmail.com>
Co-authored-by: Rajat Dash <49777622+DARKPROGRAMMER16@users.noreply.github.com>
Co-authored-by: ptype <ptype@users.noreply.github.com>
Co-authored-by: Nick Pope <nick.pope@flightdataservices.com>
Co-authored-by: Jeppe Dakin <jeppe_dakin@hotmail.com>
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
@graingert
Copy link

graingert commented Jul 18, 2022

The problem is that if we make Cython coroutines look too much like Python coroutines at the C level (e,g, by setting the CO_COROUTINE flag on them), then CPython might start doing things with them that it shouldn't. Even if we find that it doesn't do that right now, it might change at any time, and then people have their Cython modules out there that break. So this is risky business.

inspect.iscoroutinefunction is now documented as using this mechanism to support duck-type async functions in this way
in addition unittest.mock.AsyncMock has always set CO_COROUTINE in self.__code__.co_flags https://github.com/python/cpython/blame/main/Lib/unittest/mock.py#L2175-L2176

@scoder
Copy link
Contributor

scoder commented Jul 19, 2022

The fact that unittest.mock.AyncMock uses a fake code object and sets a flag on it does not mean that the same thing will work for us at the C level with a real code object using the CO_COROUTINE flag. However, this should at least work safely in CPython <= 3.10, probably up to 3.11, and from those versions on, the inspect change in python/cpython#94050 would take over (from the PoV of a user) if we ever need to remove the flag again. So I'd deem it ok to generally enable the flag for coroutines in Cython 3.0. Until further notice. :)

@scoder
Copy link
Contributor

scoder commented Jul 19, 2022

I revived an old change that I made for this back in 2019 as #4902.

@scoder
Copy link
Contributor

scoder commented Jul 19, 2022

… and noticed that it probably doesn't help anyone because inspect ignores it in earlier Python versions. Not sure if that leaves any value in adding the flag.

@graingert
Copy link

inspect.iscoroutinefunction ignores it in earlier python versions however asyncio.iscoroutinefunction still calls inspect.iscoroutinefunctionand the old decorator behavior of asyncio.iscoroutinefunction is undocumented and pending deprecation

@gshmu
Copy link

gshmu commented Jul 27, 2022

encode/starlette#1777 '_cython_3_0_0a10.coroutine' object is not callable

is cython bug?

@da-woods
Copy link
Contributor

encode/starlette#1777 '_cython_3_0_0a10.coroutine' object is not callable

is cython bug?

It's the same issue: that Cython coroutines aren't enough like Python coroutines to pass inspect. That's mainly down to inspect rather than Cython. It's unlikely to be possible to fix on Python <3.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment