| 1 |
# mode: run |
| 2 |
# tag: asyncio, pep492 |
| 3 |
# ticket: gh6878, gh6850 |
| 4 |
|
| 5 |
""" |
| 6 |
PYTHON setup.py build_ext -i |
| 7 |
PYTHON test_no_leak.py |
| 8 |
""" |
| 9 |
|
| 10 |
######## setup.py ######## |
| 11 |
|
| 12 |
from Cython.Build.Dependencies import cythonize |
| 13 |
from distutils.core import setup |
| 14 |
|
| 15 |
setup( |
| 16 |
ext_modules = cythonize("*.pyx"), |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
######## leaky_coros.pyx ######## |
| 21 |
# See https://github.com/cython/cython/issues/6878 |
| 22 |
|
| 23 |
import asyncio |
| 24 |
|
| 25 |
async def helper(func): |
| 26 |
await asyncio.sleep(.1) # force delegation |
| 27 |
return func() |
| 28 |
|
| 29 |
async def leak_return_value_of(func): |
| 30 |
await helper(func) |
| 31 |
|
| 32 |
|
| 33 |
######## test_no_leak.py ######## |
| 34 |
# See https://github.com/cython/cython/issues/6878 |
| 35 |
|
| 36 |
import asyncio |
| 37 |
import gc |
| 38 |
import platform |
| 39 |
from contextlib import closing |
| 40 |
|
| 41 |
import leaky_coros |
| 42 |
|
| 43 |
class Leaked: |
| 44 |
pass |
| 45 |
|
| 46 |
async def run_test(repeat): |
| 47 |
for _ in range(repeat): |
| 48 |
x = await leaky_coros.leak_return_value_of(Leaked) |
| 49 |
del x |
| 50 |
|
| 51 |
gc.collect() |
| 52 |
|
| 53 |
refcount = sum((type(x) is Leaked) for x in gc.get_objects()) |
| 54 |
|
| 55 |
if refcount != 0: |
| 56 |
if platform.python_implementation() == 'CPython': |
| 57 |
raise RuntimeError(f"{refcount}/{repeat} references leaked") |
| 58 |
else: |
| 59 |
# GC is not instantaneous in PyPy etc. |
| 60 |
print(f"{refcount}/{repeat} references left over - may have leaked!") |
| 61 |
|
| 62 |
|
| 63 |
with closing(asyncio.new_event_loop()) as loop: |
| 64 |
loop.run_until_complete(run_test(10)) |