1
from pypy.interpreter.error import OperationError
2
from pypy.interpreter.baseobjspace import Wrappable
5
class GeneratorIterator(Wrappable):
6
"An iterator created by a generator."
8
def __init__(self, frame):
9
self.space = frame.space
13
def descr__reduce__(self, space):
14
from pypy.interpreter.mixedmodule import MixedModule
15
w_mod = space.getbuiltinmodule('_pickle_support')
16
mod = space.interp_w(MixedModule, w_mod)
17
new_inst = mod.get('generator_new')
25
return space.newtuple([new_inst, space.newtuple(tup)])
27
def descr__iter__(self):
28
"""x.__iter__() <==> iter(x)"""
29
return self.space.wrap(self)
32
"""x.next() -> the next value, or raise StopIteration"""
35
raise OperationError(space.w_ValueError,
36
space.wrap('generator already executing'))
37
if self.frame.frame_finished_execution:
38
raise OperationError(space.w_StopIteration, space.w_None)
42
w_result = self.frame.execute_frame()
43
except OperationError:
44
# errors finish a frame
45
self.frame.frame_finished_execution = True
47
# if the frame is now marked as finished, it was RETURNed from
48
if self.frame.frame_finished_execution:
49
raise OperationError(space.w_StopIteration, space.w_None)
51
return w_result # YIELDed
53
self.frame.f_back = None