| 1 |
"""Generic iterator implementations""" |
| 2 |
|
| 3 |
from pypy.interpreter.baseobjspace import W_Root |
| 4 |
from pypy.interpreter.gateway import interp2app, interpindirect2app |
| 5 |
from pypy.interpreter.error import OperationError |
| 6 |
from pypy.interpreter.typedef import TypeDef |
| 7 |
|
| 8 |
|
| 9 |
class W_AbstractSeqIterObject(W_Root): |
| 10 |
def __init__(self, w_seq, index=0): |
| 11 |
if index < 0: |
| 12 |
index = 0 |
| 13 |
self.w_seq = w_seq |
| 14 |
self.index = index |
| 15 |
|
| 16 |
def getlength(self, space): |
| 17 |
if self.w_seq is None: |
| 18 |
return space.newint(0) |
| 19 |
index = self.index |
| 20 |
w_length = space.len(self.w_seq) |
| 21 |
w_len = space.sub(w_length, space.newint(index)) |
| 22 |
if space.is_true(space.lt(w_len, space.newint(0))): |
| 23 |
w_len = space.newint(0) |
| 24 |
return w_len |
| 25 |
|
| 26 |
def descr_iter(self, space): |
| 27 |
return self |
| 28 |
|
| 29 |
def descr_next(self, space): |
| 30 |
raise NotImplementedError |
| 31 |
|
| 32 |
def descr_reduce(self, space): |
| 33 |
w_seq = self.w_seq |
| 34 |
if w_seq is None: |
| 35 |
return _empty_iterable(space) |
| 36 |
w_callable = space.builtin.get('iter') |
| 37 |
items = [w_callable, space.newtuple([w_seq]), space.newint(self.index)] |
| 38 |
return space.newtuple(items) |
| 39 |
|
| 40 |
def descr_setstate(self, space, w_state): |
| 41 |
index = space.int_w(w_state) |
| 42 |
if self.w_seq is not None: |
| 43 |
if index < 0: |
| 44 |
index = 0 |
| 45 |
self.index = index |
| 46 |
|
| 47 |
def descr_length_hint(self, space): |
| 48 |
return self.getlength(space) |
| 49 |
|
| 50 |
W_AbstractSeqIterObject.typedef = TypeDef( |
| 51 |
"sequenceiterator", |
| 52 |
__doc__ = '''iter(collection) -> iterator |
| 53 |
iter(callable, sentinel) -> iterator |
| 54 |
|
| 55 |
Get an iterator from an object. In the first form, the argument must |
| 56 |
supply its own iterator, or be a sequence. |
| 57 |
In the second form, the callable is called until it returns the sentinel.''', |
| 58 |
__iter__ = interp2app(W_AbstractSeqIterObject.descr_iter), |
| 59 |
__next__ = interpindirect2app(W_AbstractSeqIterObject.descr_next), |
| 60 |
__reduce__ = interp2app(W_AbstractSeqIterObject.descr_reduce), |
| 61 |
__length_hint__ = interp2app(W_AbstractSeqIterObject.descr_length_hint), |
| 62 |
__setstate__ = interpindirect2app(W_AbstractSeqIterObject.descr_setstate), |
| 63 |
) |
| 64 |
W_AbstractSeqIterObject.typedef.acceptable_as_base_class = False |
| 65 |
|
| 66 |
|
| 67 |
class W_SeqIterObject(W_AbstractSeqIterObject): |
| 68 |
"""Sequence iterator implementation for general sequences.""" |
| 69 |
|
| 70 |
def descr_next(self, space): |
| 71 |
if self.w_seq is None: |
| 72 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 73 |
try: |
| 74 |
w_item = space.getitem(self.w_seq, space.newint(self.index)) |
| 75 |
except OperationError as e: |
| 76 |
self.w_seq = None |
| 77 |
if not e.match(space, space.w_IndexError): |
| 78 |
raise |
| 79 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 80 |
self.index += 1 |
| 81 |
return w_item |
| 82 |
|
| 83 |
|
| 84 |
class W_FastListIterObject(W_AbstractSeqIterObject): |
| 85 |
"""Sequence iterator specialized for lists.""" |
| 86 |
|
| 87 |
def descr_next(self, space): |
| 88 |
from pypy.objspace.std.listobject import W_ListObject |
| 89 |
w_seq = self.w_seq |
| 90 |
if w_seq is None: |
| 91 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 92 |
assert isinstance(w_seq, W_ListObject) |
| 93 |
index = self.index |
| 94 |
try: |
| 95 |
w_item = w_seq.getitem(index) |
| 96 |
except IndexError: |
| 97 |
self.w_seq = None |
| 98 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 99 |
self.index = index + 1 |
| 100 |
return w_item |
| 101 |
|
| 102 |
|
| 103 |
class W_FastUnicodeIterObject(W_AbstractSeqIterObject): |
| 104 |
"""Sequence iterator specialized for unicode objects.""" |
| 105 |
|
| 106 |
def __init__(self, w_seq): |
| 107 |
from pypy.objspace.std.unicodeobject import W_UnicodeObject |
| 108 |
W_AbstractSeqIterObject.__init__(self, w_seq) |
| 109 |
assert isinstance(w_seq, W_UnicodeObject) |
| 110 |
self.byteindex = 0 |
| 111 |
|
| 112 |
def descr_next(self, space): |
| 113 |
from pypy.objspace.std.unicodeobject import W_UnicodeObject |
| 114 |
w_seq = self.w_seq |
| 115 |
if w_seq is None: |
| 116 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 117 |
assert isinstance(w_seq, W_UnicodeObject) |
| 118 |
index = self.index |
| 119 |
if index == w_seq._length: |
| 120 |
self.w_seq = None |
| 121 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 122 |
start = self.byteindex |
| 123 |
end = w_seq.next_codepoint_pos_dont_look_inside(start) |
| 124 |
w_res = W_UnicodeObject(w_seq._utf8[start:end], 1) |
| 125 |
self.byteindex = end |
| 126 |
self.index += 1 |
| 127 |
return w_res |
| 128 |
|
| 129 |
def descr_setstate(self, space, w_state): |
| 130 |
from pypy.objspace.std.unicodeobject import W_UnicodeObject |
| 131 |
index = space.int_w(w_state) |
| 132 |
w_seq = self.w_seq |
| 133 |
if w_seq is not None: |
| 134 |
assert isinstance(w_seq, W_UnicodeObject) |
| 135 |
if index < 0: |
| 136 |
index = 0 |
| 137 |
if index >= w_seq._len(): |
| 138 |
index = w_seq._len() |
| 139 |
self.index = index |
| 140 |
self.byteindex = w_seq._index_to_byte(index) |
| 141 |
|
| 142 |
|
| 143 |
class W_FastTupleIterObject(W_AbstractSeqIterObject): |
| 144 |
"""Sequence iterator specialized for tuples, accessing directly |
| 145 |
their RPython-level list of wrapped objects. |
| 146 |
""" |
| 147 |
def __init__(self, w_seq, wrappeditems): |
| 148 |
W_AbstractSeqIterObject.__init__(self, w_seq) |
| 149 |
self.tupleitems = wrappeditems |
| 150 |
|
| 151 |
def descr_next(self, space): |
| 152 |
if self.tupleitems is None: |
| 153 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 154 |
index = self.index |
| 155 |
try: |
| 156 |
w_item = self.tupleitems[index] |
| 157 |
except IndexError: |
| 158 |
self.tupleitems = None |
| 159 |
self.w_seq = None |
| 160 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 161 |
self.index = index + 1 |
| 162 |
return w_item |
| 163 |
|
| 164 |
|
| 165 |
class W_StringIterObject(W_AbstractSeqIterObject): |
| 166 |
"""Sequence iterator specialized for string-like objects, used |
| 167 |
for bytes.__iter__() or str.__iter__() or bytearray.__iter__(). |
| 168 |
Needed because otherwise these methods would call the possibly |
| 169 |
overridden __getitem__() method, which they must not. |
| 170 |
""" |
| 171 |
def __init__(self, w_seq, getitem_fn): |
| 172 |
W_AbstractSeqIterObject.__init__(self, w_seq) |
| 173 |
self.getitem_fn = getitem_fn |
| 174 |
|
| 175 |
def descr_next(self, space): |
| 176 |
if self.w_seq is None: |
| 177 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 178 |
index = self.index |
| 179 |
try: |
| 180 |
w_item = self.getitem_fn(self.w_seq, space, index) |
| 181 |
except OperationError as e: |
| 182 |
self.w_seq = None |
| 183 |
if not e.match(space, space.w_IndexError): |
| 184 |
raise |
| 185 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 186 |
self.index = index + 1 |
| 187 |
return w_item |
| 188 |
|
| 189 |
|
| 190 |
class W_ReverseSeqIterObject(W_Root): |
| 191 |
def __init__(self, space, w_seq, index=-1): |
| 192 |
self.w_seq = w_seq |
| 193 |
self.index = space.len_w(w_seq) + index |
| 194 |
|
| 195 |
def descr_reduce(self, space): |
| 196 |
w_seq = self.w_seq |
| 197 |
if w_seq is None: |
| 198 |
return _empty_iterable(space) |
| 199 |
w_callable = space.builtin.get('reversed') |
| 200 |
items = [w_callable, space.newtuple([w_seq]), space.newint(self.index)] |
| 201 |
return space.newtuple(items) |
| 202 |
|
| 203 |
def descr_setstate(self, space, w_state): |
| 204 |
index = space.int_w(w_state) |
| 205 |
if self.w_seq is not None: |
| 206 |
length = space.int_w(space.len(self.w_seq)) |
| 207 |
if index >= length: index = length-1 |
| 208 |
self.index = index |
| 209 |
|
| 210 |
def descr_length_hint(self, space): |
| 211 |
length = self.index + 1 |
| 212 |
if self.w_seq is None or space.len_w(self.w_seq) < length: |
| 213 |
length = 0 |
| 214 |
return space.newint(length) |
| 215 |
|
| 216 |
def descr_iter(self, space): |
| 217 |
return self |
| 218 |
|
| 219 |
def descr_next(self, space): |
| 220 |
if self.index >= 0: |
| 221 |
w_index = space.newint(self.index) |
| 222 |
try: |
| 223 |
w_item = space.getitem(self.w_seq, w_index) |
| 224 |
except OperationError as e: |
| 225 |
|
| 226 |
self.index = -1 |
| 227 |
self.w_seq = None |
| 228 |
if not e.match(space, space.w_IndexError): |
| 229 |
raise |
| 230 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 231 |
else: |
| 232 |
self.index -= 1 |
| 233 |
return w_item |
| 234 |
|
| 235 |
|
| 236 |
self.index = -1 |
| 237 |
self.w_seq = None |
| 238 |
raise OperationError(space.w_StopIteration, space.w_None) |
| 239 |
|
| 240 |
W_ReverseSeqIterObject.typedef = TypeDef( |
| 241 |
"reversesequenceiterator", |
| 242 |
__iter__ = interp2app(W_ReverseSeqIterObject.descr_iter), |
| 243 |
__next__ = interp2app(W_ReverseSeqIterObject.descr_next), |
| 244 |
__reduce__ = interp2app(W_ReverseSeqIterObject.descr_reduce), |
| 245 |
__setstate__ = interp2app(W_ReverseSeqIterObject.descr_setstate), |
| 246 |
__length_hint__ = interp2app(W_ReverseSeqIterObject.descr_length_hint), |
| 247 |
) |
| 248 |
W_ReverseSeqIterObject.typedef.acceptable_as_base_class = False |
| 249 |
|
| 250 |
|
| 251 |
def _empty_iterable(space): |
| 252 |
w_callable = space.builtin.get('iter') |
| 253 |
return space.newtuple([w_callable, space.newtuple([space.newtuple([])])]) |