commit
2cb6b7a7ff
|
@ -1316,6 +1316,11 @@ class CoroutineTests(unittest.TestCase):
|
|||
|
||||
wrapper.send(1)
|
||||
gen.send.assert_called_once_with(1)
|
||||
gen.reset_mock()
|
||||
|
||||
next(wrapper)
|
||||
gen.__next__.assert_called_once_with()
|
||||
gen.reset_mock()
|
||||
|
||||
wrapper.throw(1, 2, 3)
|
||||
gen.throw.assert_called_once_with(1, 2, 3)
|
||||
|
@ -1412,8 +1417,10 @@ class CoroutineTests(unittest.TestCase):
|
|||
self.fail('StopIteration was expected')
|
||||
|
||||
def test_gen(self):
|
||||
def gen(): yield
|
||||
gen = gen()
|
||||
def gen_func():
|
||||
yield 1
|
||||
return (yield 2)
|
||||
gen = gen_func()
|
||||
@types.coroutine
|
||||
def foo(): return gen
|
||||
wrapper = foo()
|
||||
|
@ -1426,6 +1433,17 @@ class CoroutineTests(unittest.TestCase):
|
|||
getattr(gen, name))
|
||||
self.assertIs(foo().cr_code, gen.gi_code)
|
||||
|
||||
self.assertEqual(next(wrapper), 1)
|
||||
self.assertEqual(wrapper.send(None), 2)
|
||||
with self.assertRaisesRegex(StopIteration, 'spam'):
|
||||
wrapper.send('spam')
|
||||
|
||||
gen = gen_func()
|
||||
wrapper = foo()
|
||||
wrapper.send(None)
|
||||
with self.assertRaisesRegex(Exception, 'ham'):
|
||||
wrapper.throw(Exception, Exception('ham'))
|
||||
|
||||
def test_genfunc(self):
|
||||
def gen(): yield
|
||||
self.assertIs(types.coroutine(gen), gen)
|
||||
|
|
22
Lib/types.py
22
Lib/types.py
|
@ -169,33 +169,33 @@ import collections.abc as _collections_abc
|
|||
class _GeneratorWrapper:
|
||||
# TODO: Implement this in C.
|
||||
def __init__(self, gen):
|
||||
self.__wrapped__ = gen
|
||||
self.__isgen__ = gen.__class__ is GeneratorType
|
||||
self.__wrapped = gen
|
||||
self.__isgen = gen.__class__ is GeneratorType
|
||||
self.__name__ = getattr(gen, '__name__', None)
|
||||
self.__qualname__ = getattr(gen, '__qualname__', None)
|
||||
def send(self, val):
|
||||
return self.__wrapped__.send(val)
|
||||
return self.__wrapped.send(val)
|
||||
def throw(self, tp, *rest):
|
||||
return self.__wrapped__.throw(tp, *rest)
|
||||
return self.__wrapped.throw(tp, *rest)
|
||||
def close(self):
|
||||
return self.__wrapped__.close()
|
||||
return self.__wrapped.close()
|
||||
@property
|
||||
def gi_code(self):
|
||||
return self.__wrapped__.gi_code
|
||||
return self.__wrapped.gi_code
|
||||
@property
|
||||
def gi_frame(self):
|
||||
return self.__wrapped__.gi_frame
|
||||
return self.__wrapped.gi_frame
|
||||
@property
|
||||
def gi_running(self):
|
||||
return self.__wrapped__.gi_running
|
||||
return self.__wrapped.gi_running
|
||||
cr_code = gi_code
|
||||
cr_frame = gi_frame
|
||||
cr_running = gi_running
|
||||
def __next__(self):
|
||||
return next(self.__wrapped__)
|
||||
return next(self.__wrapped)
|
||||
def __iter__(self):
|
||||
if self.__isgen__:
|
||||
return self.__wrapped__
|
||||
if self.__isgen:
|
||||
return self.__wrapped
|
||||
return self
|
||||
__await__ = __iter__
|
||||
|
||||
|
|
Loading…
Reference in New Issue