diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 03fed30171dc62..4b091c54a4875f 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -287,70 +287,58 @@ class ModifyTest(unittest.TestCase): 2j, ] - def modify_f_locals(self, g, local, new_iter): - g.gi_frame.f_locals[local] = new_iter - return g + def genexpr(self): + return (x for x in range(10)) - def new_gen_from_gi_code(self, g, new_iter): - generator_func = types.FunctionType(g.gi_code, {}) - return generator_func(new_iter) - - def test_modify_genexpr(self): - def genexpr(): - return (x for x in range(10)) + def genfunc(self): + def gen(it): + for x in it: + yield x + return gen(range(10)) - def get_genexpr_f_locals(self, new_iter): - return self.modify_f_locals(genexpr(), '.0', new_iter) + def process_tests(self, get_generator): + for obj in self.iterables: + g_obj = get_generator(obj) + with self.subTest(g_obj=g_obj, obj=obj): + self.assertListEqual(list(g_obj), list(obj)) - def get_genexpr_gi_code(self, new_iter): - return self.new_gen_from_gi_code(genexpr(), new_iter) + g_iter = get_generator(iter(obj)) + with self.subTest(g_iter=g_iter, obj=obj): + self.assertListEqual(list(g_iter), list(obj)) err_regex = "'.*' object is not iterable" + for obj in self.non_iterables: + g_obj = get_generator(obj) + with self.subTest(g_obj=g_obj): + self.assertRaisesRegex(TypeError, err_regex, list, g_obj) - for get_genexpr in [get_genexpr_f_locals, get_genexpr_gi_code]: - for obj in self.iterables: - g_obj = get_genexpr(self, obj) - with self.subTest(g_obj=g_obj, err_regex=err_regex): - self.assertListEqual(list(g_obj), list(obj)) - - g_iter = get_genexpr(self, iter(obj)) - with self.subTest(g_iter=g_iter, obj=obj): - self.assertListEqual(list(g_iter), list(obj)) - - for obj in self.non_iterables: - g_obj = get_genexpr(self, obj) - with self.subTest(g_obj=g_obj, err_regex=err_regex): - self.assertRaisesRegex(TypeError, err_regex, list, g_obj) - - def test_modify_genfunc(self): - def genfunc(): - def gen(it): - for x in it: - yield x - return gen(range(10)) + def test_modify_f_locals(self): + def modify_f_locals(g, local, obj): + g.gi_frame.f_locals[local] = obj + return g - def get_genfunc_f_locals(self, new_iter): - return self.modify_f_locals(genfunc(), 'it', new_iter) + def get_generator_genexpr(obj): + return modify_f_locals(self.genexpr(), '.0', obj) - def get_genfunc_gi_code(self, new_iter): - return self.new_gen_from_gi_code(genfunc(), new_iter) + def get_generator_genfunc(obj): + return modify_f_locals(self.genfunc(), 'it', obj) - err_regex = "'.*' object is not iterable" + self.process_tests(get_generator_genexpr) + self.process_tests(get_generator_genfunc) + + def test_new_gen_from_gi_code(self): + def new_gen_from_gi_code(g, obj): + generator_func = types.FunctionType(g.gi_code, {}) + return generator_func(obj) - for get_genfunc in [get_genfunc_f_locals, get_genfunc_gi_code]: - for obj in self.iterables: - g_obj = get_genfunc(self, obj) - with self.subTest(g_obj=g_obj, obj=obj): - self.assertListEqual(list(g_obj), list(obj)) + def get_generator_genexpr(obj): + return new_gen_from_gi_code(self.genexpr(), obj) - g_iter = get_genfunc(self, iter(obj)) - with self.subTest(g_iter=g_iter, obj=obj): - self.assertListEqual(list(g_iter), list(obj)) + def get_generator_genfunc(obj): + return new_gen_from_gi_code(self.genfunc(), obj) - for obj in self.non_iterables: - g_obj = get_genfunc(self, obj) - with self.subTest(g_obj=g_obj, err_regex=err_regex): - self.assertRaisesRegex(TypeError, err_regex, list, g_obj) + self.process_tests(get_generator_genexpr) + self.process_tests(get_generator_genfunc) class ExceptionTest(unittest.TestCase):