[Python-checkins] cpython (3.4): asyncio: Add __weakref__ slots to Handle and CoroWrapper. Upstream issue #166.

guido.van.rossum python-checkins at python.org
Sun Apr 27 19:47:38 CEST 2014


http://hg.python.org/cpython/rev/c27cb341de7d
changeset:   90479:c27cb341de7d
branch:      3.4
user:        Guido van Rossum <guido at python.org>
date:        Sun Apr 27 10:44:22 2014 -0700
summary:
  asyncio: Add __weakref__ slots to Handle and CoroWrapper. Upstream issue #166.

files:
  Lib/asyncio/events.py                |  2 +-
  Lib/asyncio/tasks.py                 |  2 +-
  Lib/test/test_asyncio/test_events.py |  6 ++++++
  Lib/test/test_asyncio/test_tasks.py  |  8 ++++++++
  4 files changed, 16 insertions(+), 2 deletions(-)


diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -16,7 +16,7 @@
 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'
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -36,7 +36,7 @@
 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
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -21,6 +21,7 @@
 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 @@
             '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):
 
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -4,6 +4,7 @@
 import os.path
 import types
 import unittest
+import weakref
 from test.script_helper import assert_python_ok
 
 import asyncio
@@ -1475,6 +1476,13 @@
         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:
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list