]> granicus.if.org Git - python/commitdiff
asyncio: Add __weakref__ slots to Handle and CoroWrapper. Upstream issue #166.
authorGuido van Rossum <guido@python.org>
Sun, 27 Apr 2014 17:44:22 +0000 (10:44 -0700)
committerGuido van Rossum <guido@python.org>
Sun, 27 Apr 2014 17:44:22 +0000 (10:44 -0700)
Lib/asyncio/events.py
Lib/asyncio/tasks.py
Lib/test/test_asyncio/test_events.py
Lib/test/test_asyncio/test_tasks.py

index 57af68afb641b243b104ccc038597b073012d33f..31592d10b160eaa4be086b211377bd10519f57e4 100644 (file)
@@ -16,7 +16,7 @@ import socket
 class Handle:
     """Object returned by callback registration methods."""
 
-    __slots__ = ['_callback', '_args', '_cancelled', '_loop']
+    __slots__ = ['_callback', '_args', '_cancelled', '_loop', '__weakref__']
 
     def __init__(self, callback, args, loop):
         assert not isinstance(callback, Handle), 'A Handle is not a callback'
index e8ee947501df557c1d609c8d73440f980e0070d7..45a6342eea4eeb42fe390eff84fa4a1a694f4a3a 100644 (file)
@@ -36,7 +36,7 @@ _DEBUG = (not sys.flags.ignore_environment
 class CoroWrapper:
     # Wrapper for coroutine in _DEBUG mode.
 
-    __slots__ = ['gen', 'func', '__name__', '__doc__']
+    __slots__ = ['gen', 'func', '__name__', '__doc__', '__weakref__']
 
     def __init__(self, gen, func):
         assert inspect.isgenerator(gen), gen
index 1e64dd07d604807cc8a345507c51aef332e6d4a5..03c414914a76d3a2d9461966e79eef33cda4f7a8 100644 (file)
@@ -21,6 +21,7 @@ import time
 import errno
 import unittest
 from unittest import mock
+import weakref
 from test import support  # find_unused_port, IPV6_ENABLED, TEST_HOME_DIR
 
 
@@ -1786,6 +1787,11 @@ class HandleTests(unittest.TestCase):
             'handle': h
         })
 
+    def test_handle_weakref(self):
+        wd = weakref.WeakValueDictionary()
+        h = asyncio.Handle(lambda: None, (), object())
+        wd['h'] = h  # Would fail without __weakref__ slot.
+
 
 class TimerTests(unittest.TestCase):
 
index 80571b4191347fde0e1ed120bd1adb19fb07ced8..45a0dc1d210d42f69005dd27254ea261fabc7d8b 100644 (file)
@@ -4,6 +4,7 @@ import gc
 import os.path
 import types
 import unittest
+import weakref
 from test.script_helper import assert_python_ok
 
 import asyncio
@@ -1475,6 +1476,13 @@ class TaskTests(unittest.TestCase):
         self.assertEqual(call((1, 2)), (1, 2))
         self.assertEqual(call('spam'), 'spam')
 
+    def test_corowrapper_weakref(self):
+        wd = weakref.WeakValueDictionary()
+        def foo(): yield from []
+        cw = asyncio.tasks.CoroWrapper(foo(), foo)
+        wd['cw'] = cw  # Would fail without __weakref__ slot.
+        cw.gen = None  # Suppress warning from __del__.
+
 
 class GatherTestsBase: