Merge 3.4 -> default: asyncio: Add __weakref__ slots to Handle and CoroWrapper. Upstream issue #166.
This commit is contained in:
commit
6cd08d6277
|
@ -16,7 +16,7 @@ import socket
|
||||||
class Handle:
|
class Handle:
|
||||||
"""Object returned by callback registration methods."""
|
"""Object returned by callback registration methods."""
|
||||||
|
|
||||||
__slots__ = ['_callback', '_args', '_cancelled', '_loop']
|
__slots__ = ['_callback', '_args', '_cancelled', '_loop', '__weakref__']
|
||||||
|
|
||||||
def __init__(self, callback, args, loop):
|
def __init__(self, callback, args, loop):
|
||||||
assert not isinstance(callback, Handle), 'A Handle is not a callback'
|
assert not isinstance(callback, Handle), 'A Handle is not a callback'
|
||||||
|
|
|
@ -36,7 +36,7 @@ _DEBUG = (not sys.flags.ignore_environment
|
||||||
class CoroWrapper:
|
class CoroWrapper:
|
||||||
# Wrapper for coroutine in _DEBUG mode.
|
# Wrapper for coroutine in _DEBUG mode.
|
||||||
|
|
||||||
__slots__ = ['gen', 'func', '__name__', '__doc__']
|
__slots__ = ['gen', 'func', '__name__', '__doc__', '__weakref__']
|
||||||
|
|
||||||
def __init__(self, gen, func):
|
def __init__(self, gen, func):
|
||||||
assert inspect.isgenerator(gen), gen
|
assert inspect.isgenerator(gen), gen
|
||||||
|
|
|
@ -21,6 +21,7 @@ import time
|
||||||
import errno
|
import errno
|
||||||
import unittest
|
import unittest
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
import weakref
|
||||||
from test import support # find_unused_port, IPV6_ENABLED, TEST_HOME_DIR
|
from test import support # find_unused_port, IPV6_ENABLED, TEST_HOME_DIR
|
||||||
|
|
||||||
|
|
||||||
|
@ -1786,6 +1787,11 @@ class HandleTests(unittest.TestCase):
|
||||||
'handle': h
|
'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):
|
class TimerTests(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import gc
|
||||||
import os.path
|
import os.path
|
||||||
import types
|
import types
|
||||||
import unittest
|
import unittest
|
||||||
|
import weakref
|
||||||
from test.script_helper import assert_python_ok
|
from test.script_helper import assert_python_ok
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
@ -1475,6 +1476,13 @@ class TaskTests(unittest.TestCase):
|
||||||
self.assertEqual(call((1, 2)), (1, 2))
|
self.assertEqual(call((1, 2)), (1, 2))
|
||||||
self.assertEqual(call('spam'), 'spam')
|
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:
|
class GatherTestsBase:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue