Merge 3.5 (issue #24325 & #24400)

This commit is contained in:
Yury Selivanov 2015-06-24 12:51:55 -04:00
commit 2cb6b7a7ff
2 changed files with 31 additions and 13 deletions

View File

@ -1316,6 +1316,11 @@ class CoroutineTests(unittest.TestCase):
wrapper.send(1) wrapper.send(1)
gen.send.assert_called_once_with(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) wrapper.throw(1, 2, 3)
gen.throw.assert_called_once_with(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') self.fail('StopIteration was expected')
def test_gen(self): def test_gen(self):
def gen(): yield def gen_func():
gen = gen() yield 1
return (yield 2)
gen = gen_func()
@types.coroutine @types.coroutine
def foo(): return gen def foo(): return gen
wrapper = foo() wrapper = foo()
@ -1426,6 +1433,17 @@ class CoroutineTests(unittest.TestCase):
getattr(gen, name)) getattr(gen, name))
self.assertIs(foo().cr_code, gen.gi_code) 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 test_genfunc(self):
def gen(): yield def gen(): yield
self.assertIs(types.coroutine(gen), gen) self.assertIs(types.coroutine(gen), gen)

View File

@ -169,33 +169,33 @@ import collections.abc as _collections_abc
class _GeneratorWrapper: class _GeneratorWrapper:
# TODO: Implement this in C. # TODO: Implement this in C.
def __init__(self, gen): def __init__(self, gen):
self.__wrapped__ = gen self.__wrapped = gen
self.__isgen__ = gen.__class__ is GeneratorType self.__isgen = gen.__class__ is GeneratorType
self.__name__ = getattr(gen, '__name__', None) self.__name__ = getattr(gen, '__name__', None)
self.__qualname__ = getattr(gen, '__qualname__', None) self.__qualname__ = getattr(gen, '__qualname__', None)
def send(self, val): def send(self, val):
return self.__wrapped__.send(val) return self.__wrapped.send(val)
def throw(self, tp, *rest): def throw(self, tp, *rest):
return self.__wrapped__.throw(tp, *rest) return self.__wrapped.throw(tp, *rest)
def close(self): def close(self):
return self.__wrapped__.close() return self.__wrapped.close()
@property @property
def gi_code(self): def gi_code(self):
return self.__wrapped__.gi_code return self.__wrapped.gi_code
@property @property
def gi_frame(self): def gi_frame(self):
return self.__wrapped__.gi_frame return self.__wrapped.gi_frame
@property @property
def gi_running(self): def gi_running(self):
return self.__wrapped__.gi_running return self.__wrapped.gi_running
cr_code = gi_code cr_code = gi_code
cr_frame = gi_frame cr_frame = gi_frame
cr_running = gi_running cr_running = gi_running
def __next__(self): def __next__(self):
return next(self.__wrapped__) return next(self.__wrapped)
def __iter__(self): def __iter__(self):
if self.__isgen__: if self.__isgen:
return self.__wrapped__ return self.__wrapped
return self return self
__await__ = __iter__ __await__ = __iter__