Skip to content
Snippets Groups Projects
Commit 56eccc75 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

enabled `SelfClosingGenerator` to accept generator-generating functions as well

parent cccda1d4
No related branches found
No related tags found
No related merge requests found
# v2.7.34
* enabled `SelfClosingGenerator` to accept generator-generating functions as well
__version__ = '2.7.34_a1'
__version__ = '2.7.34'
......@@ -11,6 +11,8 @@ class SelfClosingGenerator:
This will allow generators to complete that don't provide a .close() method.
This will additionally exhaust the generator upon deallocation of the generator.
You can feed it with either generators, or generator-functions, it will behave correctly each time.
"""
__slots__ = ('generator', 'stopped')
......@@ -21,6 +23,12 @@ class SelfClosingGenerator:
def __iter__(self):
return self.generator
def __call__(self, *args, **kwargs):
return SelfClosingGenerator(self.generator(*args, **kwargs))
def send(self, obj: tp.Any):
self.generator.send(obj)
def __next__(self):
try:
return next(self.generator)
......
......@@ -13,9 +13,8 @@ class TestIterators(unittest.TestCase):
g = hint_with_length(generator(), 1000)
self.assertEqual(g.__length_hint__(), 1000)
@unittest.skipUnless(sys.implementation.name == 'cpython', 'Not CPython')
@unittest.skipUnless(sys.implementation.name == 'cpython', 'Not CPython, this needs deterministic GC')
def test_self_closing_generator(self):
a = {'done': False}
def generator():
......@@ -28,3 +27,18 @@ class TestIterators(unittest.TestCase):
break
self.assertTrue(a['done'])
@unittest.skipUnless(sys.implementation.name == 'cpython', 'Not CPython, this needs deterministic GC')
def test_self_closing_generator_function(self):
a = {'done': False}
def generator():
for i in range(5):
yield i
a['done'] = True
for i in SelfClosingGenerator(generator)():
if i == 2:
break
self.assertTrue(a['done'])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment