From f847f1fba757b94938364c29b14bfc1129024d06 Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Wed, 24 Jun 2015 12:49:28 -0400 Subject: [PATCH] Issue #24400, #24325: More tests for types._GeneratorWrapper Also, make 'wrapped' and 'isgen' private. --- Lib/test/test_types.py | 22 ++++++++++++++++++++-- Lib/types.py | 22 +++++++++++----------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index e489898d947..e283f32dd72 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -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) diff --git a/Lib/types.py b/Lib/types.py index 1d44653ef97..38b453ad197 100644 --- a/Lib/types.py +++ b/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__