# Pastebin TrjoHYDe diff -r 37a9de9e88b0 pypy/objspace/std/iterobject.py --- a/pypy/objspace/std/iterobject.py Sat Feb 16 14:52:38 2019 +0100 +++ b/pypy/objspace/std/iterobject.py Sat Feb 16 22:37:02 2019 +0100 @@ -92,6 +92,33 @@ return w_item +class W_FastUnicodeIterObject(W_AbstractSeqIterObject): + """Sequence iterator specialized for unicode objects.""" + + def __init__(self, w_seq): + from pypy.objspace.std.unicodeobject import W_UnicodeObject + W_AbstractSeqIterObject.__init__(self, w_seq) + assert isinstance(w_seq, W_UnicodeObject) + self.byteindex = 0 + + def descr_next(self, space): + from pypy.objspace.std.unicodeobject import W_UnicodeObject + from rpython.rlib import rutf8 + w_seq = self.w_seq + if w_seq is None: + raise OperationError(space.w_StopIteration, space.w_None) + assert isinstance(w_seq, W_UnicodeObject) + index = self.index + if index == w_seq._length: + self.w_seq = None + raise OperationError(space.w_StopIteration, space.w_None) + start = self.byteindex + end = rutf8.next_codepoint_pos(w_seq._utf8, start) + w_res = W_UnicodeObject(w_seq._utf8[start:end], 1) + self.byteindex = end + return w_res + + class W_FastTupleIterObject(W_AbstractSeqIterObject): """Sequence iterator specialized for tuples, accessing directly their RPython-level list of wrapped objects. diff -r 37a9de9e88b0 pypy/objspace/std/test/test_unicodeobject.py --- a/pypy/objspace/std/test/test_unicodeobject.py Sat Feb 16 14:52:38 2019 +0100 +++ b/pypy/objspace/std/test/test_unicodeobject.py Sat Feb 16 22:37:02 2019 +0100 @@ -41,6 +41,18 @@ space.w_unicode, "__new__", space.w_unicode, w_uni) assert w_new is w_uni + def test_fast_iter(self): + space = self.space + w_uni = space.newutf8(u"aƤ".encode("utf-8"), 2) + old_index_storage = w_uni._index_storage + w_iter = space.iter(w_uni) + w_char1 = w_iter.descr_next(space) + w_char2 = w_iter.descr_next(space) + assert w_uni._index_storage is old_index_storage + assert space.eq_w(w_char1, w_uni._getitem_result(space, 0)) + assert space.eq_w(w_char2, w_uni._getitem_result(space, 1)) + + if HAS_HYPOTHESIS: @given(strategies.text(), strategies.integers(min_value=0, max_value=10), strategies.integers(min_value=-1, max_value=10)) diff -r 37a9de9e88b0 pypy/objspace/std/unicodeobject.py --- a/pypy/objspace/std/unicodeobject.py Sat Feb 16 14:52:38 2019 +0100 +++ b/pypy/objspace/std/unicodeobject.py Sat Feb 16 22:37:02 2019 +0100 @@ -224,6 +224,10 @@ def descr_str(self, space): return encode_object(space, self, 'ascii', 'strict') + def descr_iter(self, space): + from pypy.objspace.std.iterobject import W_FastUnicodeIterObject + return W_FastUnicodeIterObject(self) + def hash_w(self): # shortcut for UnicodeDictStrategy x = compute_hash(self._utf8) @@ -1590,6 +1594,7 @@ doc=UnicodeDocstrings.__repr__.__doc__), __str__ = interp2app(W_UnicodeObject.descr_str, doc=UnicodeDocstrings.__str__.__doc__), + __iter__ = interp2app(W_UnicodeObject.descr_iter), __hash__ = interp2app(W_UnicodeObject.descr_hash, doc=UnicodeDocstrings.__hash__.__doc__),