Author: Simon Cross <[email protected]>
Branch: remove-string-smm
Changeset: r62622:c7554e6db542
Date: 2013-03-22 01:42 +0200
http://bitbucket.org/pypy/pypy/changeset/c7554e6db542/

Log:    Re-implement bytearray.insert.

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1322,6 +1322,11 @@
             return None
         return self.str_w(w_obj)
 
+    def str_or_int_w(self, w_obj):
+        if self.is_true(self.isinstance(w_obj, self.w_str)):
+            return self.str_w(w_obj)
+        return self.int_w(w_obj)
+
     def str_w(self, w_obj):
         return w_obj.str_w(self)
 
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -129,6 +129,9 @@
     def visit_str_or_None(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
+    def visit_str_or_int(self, el, app_sig):
+        self.checked_space_method(el, app_sig)
+
     def visit_str0(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
@@ -247,6 +250,9 @@
     def visit_str_or_None(self, typ):
         self.run_args.append("space.str_or_None_w(%s)" % (self.scopenext(),))
 
+    def visit_str_or_int(self, typ):
+        self.run_args.append("space.str_or_int_w(%s)" % (self.scopenext(),))
+
     def visit_str0(self, typ):
         self.run_args.append("space.str0_w(%s)" % (self.scopenext(),))
 
diff --git a/pypy/objspace/std/bytearrayobject.py 
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -397,14 +397,6 @@
     w_str = str__Bytearray(space, w_bytearray)
     return stringobject.str_isspace__String(space, w_str)
 
-def bytearray_insert__Bytearray_Int_ANY(space, w_bytearray, w_idx, w_other):
-    where = space.int_w(w_idx)
-    length = len(w_bytearray.data)
-    index = get_positive_index(where, length)
-    val = getbytevalue(space, w_other)
-    w_bytearray.data.insert(index, val)
-    return space.w_None
-
 def bytearray_pop__Bytearray_Int(space, w_bytearray, w_idx):
     index = space.int_w(w_idx)
     try:
diff --git a/pypy/objspace/std/bytearraytype.py 
b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -1,8 +1,9 @@
 from pypy.interpreter.baseobjspace import ObjSpace, W_Root
 from pypy.interpreter.error import OperationError
-from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.objspace.std.register_all import register_all
 from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
+from pypy.objspace.std.listobject import get_positive_index
 
 from pypy.objspace.std.stringtype import (
     str_decode,
@@ -21,10 +22,21 @@
 
 str_join = SMM('join', 2, defaults=(None,-1))
 
-bytearray_insert  = SMM('insert', 3,
-                    doc="B.insert(index, int) -> None\n\n"
-                    "Insert a single item into the bytearray before "
-                    "the given index.")
+
+@unwrap_spec(w_self=W_Root, index=int, val='str_or_int')
+def bytearray_insert(w_self, space, index, val):
+    """B.insert(index, int) -> None
+
+    Insert a single item into the bytearray before the given index.
+    """
+    if isinstance(val, int):
+        val = chr(val)
+    elif len(val) != 1:
+        raise OperationError(space.w_ValueError,
+                             space.wrap("string must be of size 1"))
+    w_self.data.insert(index, val)
+    return space.w_None
+
 
 bytearray_pop  = SMM('pop', 2, defaults=(-1,),
                     doc="B.pop([index]) -> int\n\nRemove and return a "
@@ -175,6 +187,7 @@
     __hash__ = None,
     __reduce__ = interp2app(descr_bytearray__reduce__),
     fromhex = interp2app(descr_fromhex, as_classmethod=True),
+    insert = interp2app(bytearray_insert),
     **bytearray_interface_methods()
     )
 bytearray_typedef.registermethods(globals())
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to