Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94ba146

Browse files
committed
asyncio: Add __weakref__ slots to Handle and CoroWrapper. Upstream issue #166.
1 parent 83c1ddd commit 94ba146

4 files changed

Lines changed: 16 additions & 2 deletions

File tree

Lib/asyncio/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class Handle:
1717
"""Object returned by callback registration methods."""
1818

19-
__slots__ = ['_callback', '_args', '_cancelled', '_loop']
19+
__slots__ = ['_callback', '_args', '_cancelled', '_loop', '__weakref__']
2020

2121
def __init__(self, callback, args, loop):
2222
assert not isinstance(callback, Handle), 'A Handle is not a callback'

Lib/asyncio/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
class CoroWrapper:
3737
# Wrapper for coroutine in _DEBUG mode.
3838

39-
__slots__ = ['gen', 'func', '__name__', '__doc__']
39+
__slots__ = ['gen', 'func', '__name__', '__doc__', '__weakref__']
4040

4141
def __init__(self, gen, func):
4242
assert inspect.isgenerator(gen), gen

Lib/test/test_asyncio/test_events.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import errno
2222
import unittest
2323
from unittest import mock
24+
import weakref
2425
from test import support # find_unused_port, IPV6_ENABLED, TEST_HOME_DIR
2526

2627

@@ -1786,6 +1787,11 @@ def callback():
17861787
'handle': h
17871788
})
17881789

1790+
def test_handle_weakref(self):
1791+
wd = weakref.WeakValueDictionary()
1792+
h = asyncio.Handle(lambda: None, (), object())
1793+
wd['h'] = h # Would fail without __weakref__ slot.
1794+
17891795

17901796
class TimerTests(unittest.TestCase):
17911797

Lib/test/test_asyncio/test_tasks.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os.path
55
import types
66
import unittest
7+
import weakref
78
from test.script_helper import assert_python_ok
89

910
import asyncio
@@ -1475,6 +1476,13 @@ def call(arg):
14751476
self.assertEqual(call((1, 2)), (1, 2))
14761477
self.assertEqual(call('spam'), 'spam')
14771478

1479+
def test_corowrapper_weakref(self):
1480+
wd = weakref.WeakValueDictionary()
1481+
def foo(): yield from []
1482+
cw = asyncio.tasks.CoroWrapper(foo(), foo)
1483+
wd['cw'] = cw # Would fail without __weakref__ slot.
1484+
cw.gen = None # Suppress warning from __del__.
1485+
14781486

14791487
class GatherTestsBase:
14801488

0 commit comments

Comments
 (0)