[pypy-commit] pypy default: getslice support for bytearrays

fijal noreply at buildbot.pypy.org
Tue Oct 8 16:58:45 CEST 2013


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: 
Changeset: r67211:99817a6f0494
Date: 2013-10-07 18:03 +0200
http://bitbucket.org/pypy/pypy/changeset/99817a6f0494/

Log:	getslice support for bytearrays

diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -10,7 +10,7 @@
     SomeUnicodeCodePoint, SomeInstance, SomeBuiltin, SomeFloat, SomeIterator,
     SomePBC, SomeTypedAddressAccess, SomeAddress, SomeType, s_ImpossibleValue,
     s_Bool, s_None, unionof, missing_operation, add_knowntypedata,
-    HarmlesslyBlocked, SomeWeakRef, SomeUnicodeString)
+    HarmlesslyBlocked, SomeWeakRef, SomeUnicodeString, SomeByteArray)
 from rpython.annotator.bookkeeper import getbookkeeper
 from rpython.annotator import builtin
 from rpython.annotator.binaryop import _clone ## XXX where to put this?
@@ -520,6 +520,11 @@
     op_contains.can_only_throw = []
 
 
+class __extend__(SomeByteArray):
+    def getslice(ba, s_start, s_stop):
+        check_negative_slice(s_start, s_stop)
+        return SomeByteArray()
+
 class __extend__(SomeUnicodeString):
     def method_encode(uni, s_enc):
         if not s_enc.is_constant():
diff --git a/rpython/rtyper/lltypesystem/rbytearray.py b/rpython/rtyper/lltypesystem/rbytearray.py
--- a/rpython/rtyper/lltypesystem/rbytearray.py
+++ b/rpython/rtyper/lltypesystem/rbytearray.py
@@ -16,6 +16,9 @@
                                                                   lltype.Char,
                                                                   'bytearray_from_str')
 
+def _empty_bytearray():
+    return empty
+
 BYTEARRAY.become(lltype.GcStruct('rpy_bytearray',
                  ('chars', lltype.Array(lltype.Char)), adtmeths={
     'malloc' : lltype.staticAdtMethod(mallocbytearray),
@@ -23,8 +26,11 @@
     'copy_contents_from_str': lltype.staticAdtMethod(
                                          copy_bytearray_contents_from_str),
     'length': rstr.LLHelpers.ll_length,
+    'empty': lltype.staticAdtMethod(_empty_bytearray),
 }))
 
+empty = lltype.malloc(BYTEARRAY, 0, immortal=True)
+
 class LLHelpers(rstr.LLHelpers):
     @classmethod
     def ll_strsetitem(cls, s, i, item):
diff --git a/rpython/rtyper/test/test_rbytearray.py b/rpython/rtyper/test/test_rbytearray.py
--- a/rpython/rtyper/test/test_rbytearray.py
+++ b/rpython/rtyper/test/test_rbytearray.py
@@ -50,3 +50,10 @@
 
         ll_res = self.interpret(f, [123])
         assert hlstr(ll_res) == "123"
+
+    def test_getslice(self):
+        def f(x):
+            return str(bytearray(str(x))[1:2])
+
+        ll_res = self.interpret(f, [123])
+        assert hlstr(ll_res) == "2"


More information about the pypy-commit mailing list