[pypy-commit] pypy unicode-utf8: basic getitem implemented

fijal pypy.commits at gmail.com
Thu Mar 2 05:08:59 EST 2017


Author: fijal
Branch: unicode-utf8
Changeset: r90464:4a89aa28bd21
Date: 2017-03-01 21:26 +0100
http://bitbucket.org/pypy/pypy/changeset/4a89aa28bd21/

Log:	basic getitem implemented

diff --git a/pypy/objspace/std/stringmethods.py b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -798,6 +798,7 @@
     def descr_getnewargs(self, space):
         return space.newtuple([self._new(self._val(space))])
 
+
 # ____________________________________________________________
 # helpers for slow paths, moved out because they contain loops
 
diff --git a/pypy/objspace/std/test/test_unicodeobject.py b/pypy/objspace/std/test/test_unicodeobject.py
--- a/pypy/objspace/std/test/test_unicodeobject.py
+++ b/pypy/objspace/std/test/test_unicodeobject.py
@@ -1,3 +1,4 @@
+
 import py
 import sys
 
@@ -64,6 +65,11 @@
         check(u'a' + 'b', u'ab')
         check('a' + u'b', u'ab')
 
+    def test_getitem(self):
+        assert u'abc'[2] == 'c'
+        raises(IndexError, u'abc'.__getitem__, 15)
+        assert u'g\u0105\u015b\u0107'[2] == u'\u015b'
+
     def test_join(self):
         def check(a, b):
             assert a == b
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -612,6 +612,17 @@
 
     descr_rmul = descr_mul
 
+    def _getitem_result(self, space, index):
+        if self._ucs4 is None:
+            self._ucs4 = self._utf8.decode('utf-8')
+        try:
+            return W_UnicodeObject(self._ucs4[index].encode('utf-8'), 1)
+        except IndexError:
+            raise oefmt(space.w_IndexError, "string index out of range")
+
+    def descr_getnewargs(self, space):
+        return space.newtuple([W_UnicodeObject(self._utf8, self._length)])
+
 
 
 def wrapunicode(space, uni):


More information about the pypy-commit mailing list