diff options
author | Guido van Rossum <guido@python.org> | 2014-04-27 17:44:22 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2014-04-27 17:44:22 (GMT) |
commit | 94ba146d11869288ab3def8c7426b3b36701416a (patch) | |
tree | 40e5d7317ea6ff177ffc99e62a13c6034ef8b45c | |
parent | 83c1ddda469e7a99f11afc7d6758b3d80ad9aa3b (diff) | |
download | cpython-94ba146d11869288ab3def8c7426b3b36701416a.zip cpython-94ba146d11869288ab3def8c7426b3b36701416a.tar.gz cpython-94ba146d11869288ab3def8c7426b3b36701416a.tar.bz2 |
asyncio: Add __weakref__ slots to Handle and CoroWrapper. Upstream issue #166.
-rw-r--r-- | Lib/asyncio/events.py | 2 | ||||
-rw-r--r-- | Lib/asyncio/tasks.py | 2 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_events.py | 6 | ||||
-rw-r--r-- | 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 index 57af68a..31592d1 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -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' diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index e8ee947..45a6342 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -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 diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 1e64dd0..03c4149 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -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): diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 80571b4..45a0dc1 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -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: |