MCPcopy Index your code
hub / github.com/python/cpython / test_Generator

Method test_Generator

Lib/test/test_collections.py:1172–1241  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1170 self.assertNotIsInstance(NextOnly(), Iterator)
1171
1172 def test_Generator(self):
1173 class NonGen1:
1174 def __iter__(self): return self
1175 def __next__(self): return None
1176 def close(self): pass
1177 def throw(self, typ, val=None, tb=None): pass
1178
1179 class NonGen2:
1180 def __iter__(self): return self
1181 def __next__(self): return None
1182 def close(self): pass
1183 def send(self, value): return value
1184
1185 class NonGen3:
1186 def close(self): pass
1187 def send(self, value): return value
1188 def throw(self, typ, val=None, tb=None): pass
1189
1190 non_samples = [
1191 None, 42, 3.14, 1j, b"", "", (), [], {}, set(),
1192 iter(()), iter([]), NonGen1(), NonGen2(), NonGen3()]
1193 for x in non_samples:
1194 self.assertNotIsInstance(x, Generator)
1195 self.assertNotIsSubclass(type(x), Generator)
1196
1197 class Gen:
1198 def __iter__(self): return self
1199 def __next__(self): return None
1200 def close(self): pass
1201 def send(self, value): return value
1202 def throw(self, typ, val=None, tb=None): pass
1203
1204 class MinimalGen(Generator):
1205 def send(self, value):
1206 return value
1207 def throw(self, typ, val=None, tb=None):
1208 super().throw(typ, val, tb)
1209
1210 def gen():
1211 yield 1
1212
1213 samples = [gen(), (lambda: (yield))(), Gen(), MinimalGen()]
1214 for x in samples:
1215 self.assertIsInstance(x, Iterator)
1216 self.assertIsInstance(x, Generator)
1217 self.assertIsSubclass(type(x), Generator)
1218 self.validate_abstract_methods(Generator, 'send', 'throw')
1219
1220 # mixin tests
1221 mgen = MinimalGen()
1222 self.assertIs(mgen, iter(mgen))
1223 self.assertIs(mgen.send(None), next(mgen))
1224 self.assertEqual(2, mgen.send(2))
1225 self.assertIsNone(mgen.close())
1226 self.assertRaises(ValueError, mgen.throw, ValueError)
1227 self.assertRaisesRegex(ValueError, "^huhu$",
1228 mgen.throw, ValueError, ValueError("huhu"))
1229 self.assertRaises(StopIteration, mgen.throw, StopIteration())

Callers

nothing calls this directly

Calls 15

sendMethod · 0.95
setFunction · 0.85
NonGen1Class · 0.85
NonGen2Class · 0.85
NonGen3Class · 0.85
MinimalGenClass · 0.85
FailOnCloseClass · 0.85
IgnoreGeneratorExitClass · 0.85
assertNotIsInstanceMethod · 0.80
assertNotIsSubclassMethod · 0.80
assertIsInstanceMethod · 0.80
assertIsSubclassMethod · 0.80

Tested by

no test coverage detected