[pypy-svn] pypy fast-forward: add bytearray.__reduce__

amauryfa commits-noreply at bitbucket.org
Fri Jan 7 17:56:04 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40460:9f89648e5404
Date: 2011-01-07 14:42 +0100
http://bitbucket.org/pypy/pypy/changeset/9f89648e5404/

Log:	add bytearray.__reduce__

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
@@ -197,3 +197,7 @@
 
     def test_int(self):
         assert int(bytearray('-1234')) == -1234
+
+    def test_reduce(self):
+        assert bytearray('caf\xe9').__reduce__() == (
+            bytearray, (u'caf\xe9', 'latin-1'), None)

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
@@ -78,6 +78,19 @@
 
     return new_bytearray(space, w_bytearraytype, data)
 
+ at gateway.unwrap_spec(gateway.ObjSpace, gateway.W_Root)
+def descr_bytearray__reduce__(space, w_self):
+    from pypy.objspace.std.bytearrayobject import W_BytearrayObject
+    assert isinstance(w_self, W_BytearrayObject)
+    w_dict = w_self.getdict()
+    if w_dict is None:
+        w_dict = space.w_None
+    return space.newtuple([
+        space.type(w_self), space.newtuple([
+            space.wrap(''.join(w_self.data).decode('latin-1')),
+            space.wrap('latin-1')]),
+        w_dict])
+
 # ____________________________________________________________
 
 bytearray_typedef = StdTypeDef("bytearray",
@@ -87,5 +100,6 @@
 If the argument is a bytearray, the return value is the same object.''',
     __new__ = gateway.interp2app(descr__new__),
     __hash__ = None,
+    __reduce__ = gateway.interp2app(descr_bytearray__reduce__),
     )
 bytearray_typedef.registermethods(globals())


More information about the Pypy-commit mailing list