[pypy-commit] pypy py3.3: Add bytearray.copy(), bytearray.clear()

amauryfa noreply at buildbot.pypy.org
Sun Jan 4 19:14:05 CET 2015


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3.3
Changeset: r75224:d8fae768da4c
Date: 2015-01-01 22:51 +0100
http://bitbucket.org/pypy/pypy/changeset/d8fae768da4c/

Log:	Add bytearray.copy(), bytearray.clear()

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
@@ -425,6 +425,11 @@
     def descr_reverse(self, space):
         self.data.reverse()
 
+    def descr_clear(self, space):
+        self.data = []
+
+    def descr_copy(self, space):
+        return self._new(self.data[:])
 
 
 # ____________________________________________________________
@@ -594,6 +599,18 @@
         done using the specified fill character (default is a space).
         """
 
+    def clear():
+        """B.clear() -> None
+
+        Remove all items from B.
+        """
+
+    def copy():
+        """B.copy() -> bytearray
+
+        Return a copy of B.
+        """
+
     def count():
         """B.count(sub[, start[, end]]) -> int
 
@@ -1045,6 +1062,10 @@
                         doc=BytearrayDocstrings.remove.__doc__),
     reverse = interp2app(W_BytearrayObject.descr_reverse,
                          doc=BytearrayDocstrings.reverse.__doc__),
+    clear = interp2app(W_BytearrayObject.descr_clear,
+                       doc=BytearrayDocstrings.clear.__doc__),
+    copy = interp2app(W_BytearrayObject.descr_copy,
+                         doc=BytearrayDocstrings.copy.__doc__),
 )
 W_BytearrayObject.typedef.flag_sequence_bug_compat = True
 
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -323,6 +323,13 @@
         b.remove(Indexable())
         assert b == b''
 
+    def test_clear(self):
+        b = bytearray(b'hello')
+        b2 = b.copy()
+        b.clear()
+        assert b == bytearray()
+        assert b2 == bytearray(b'hello')
+
     def test_reverse(self):
         b = bytearray(b'hello')
         b.reverse()


More information about the pypy-commit mailing list