[pypy-svn] pypy bytearray: (mfoord, holger) implement bytearray.pop

mfoord commits-noreply at bitbucket.org
Tue Jan 18 15:52:50 CET 2011


Author: Michael Foord <michael at voidspace.org.uk>
Branch: bytearray
Changeset: r40866:145484340eff
Date: 2011-01-18 15:52 +0100
http://bitbucket.org/pypy/pypy/changeset/145484340eff/

Log:	(mfoord, holger) implement bytearray.pop

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
@@ -267,6 +267,19 @@
     w_bytearray.data.insert(index, val)
     return space.w_None
 
+def bytearray_pop__Bytearray_Int(space, w_bytearray, w_idx):
+    index = w_idx.intval
+    try:
+        result = w_bytearray.data.pop(index)
+    except IndexError:
+        if not w_bytearray.data:
+            raise OperationError(space.w_OverflowError, space.wrap(
+                "cannot pop an empty bytearray"))
+        raise OperationError(space.w_IndexError, space.wrap(
+            "pop index out of range"))
+    return space.wrap(ord(result))
+
+
 # These methods could just delegate to the string implementation,
 # but they have to return a bytearray.
 def str_replace__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_str1, w_str2, w_max):

diff --git a/pypy/objspace/std/test/test_bytes.py b/pypy/objspace/std/test/test_bytes.py
--- a/pypy/objspace/std/test/test_bytes.py
+++ b/pypy/objspace/std/test/test_bytes.py
@@ -192,6 +192,15 @@
         raises(ValueError, b.insert, 1, 'go')
         raises(TypeError, b.insert, 'g', 'o')
 
+    def test_pop(self):
+        b = bytearray('world')
+        assert b.pop() == ord('d')
+        assert b.pop(0) == ord('w')
+        assert b.pop(-2) == ord('r')
+        raises(IndexError, b.pop, 10)
+        raises(OverflowError, bytearray().pop)
+        assert bytearray(b'\xff').pop() == 0xff
+
     def test_delitem(self):
         b = bytearray('abc')
         del b[1]

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
@@ -23,6 +23,10 @@
                     doc="B.insert(index, int) -> None\n\n"
                     "Insert a single item into the bytearray before "
                     "the given index.")
+bytearray_pop  = SMM('pop',2, defaults=(-1,),
+                    doc="B.pop([index]) -> int\n\nRemove and return a "
+                    "single item from B. If no index\nargument is given, "
+                    "will pop the last value.")
 
 def getbytevalue(space, w_value):
     if space.isinstance_w(w_value, space.w_str):


More information about the Pypy-commit mailing list