Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r86611:6810d750b914
Date: 2016-08-27 19:14 +0200
http://bitbucket.org/pypy/pypy/changeset/6810d750b914/

Log:    Hopefully a correct fix for marshal.dumps() with objects that are
        subclasses of one of the built-in class. The previous logic was only
        applied on the outermost object, not on any object referenced from
        there.

diff --git a/pypy/module/marshal/interp_marshal.py 
b/pypy/module/marshal/interp_marshal.py
--- a/pypy/module/marshal/interp_marshal.py
+++ b/pypy/module/marshal/interp_marshal.py
@@ -225,15 +225,6 @@
 
     def dump_w_obj(self, w_obj):
         space = self.space
-        if space.type(w_obj).is_heaptype():
-            try:
-                buf = space.readbuf_w(w_obj)
-            except OperationError as e:
-                if not e.match(space, space.w_TypeError):
-                    raise
-                self.raise_exc("unmarshallable object")
-            else:
-                w_obj = space.newbuffer(buf)
         try:
             self.put_w_obj(w_obj)
         except rstackovf.StackOverflow:
diff --git a/pypy/module/marshal/test/test_marshal.py 
b/pypy/module/marshal/test/test_marshal.py
--- a/pypy/module/marshal/test/test_marshal.py
+++ b/pypy/module/marshal/test/test_marshal.py
@@ -186,6 +186,8 @@
             assert str(exc.value) == 'unmarshallable object'
             exc = raises(ValueError, marshal.dumps, subtype())
             assert str(exc.value) == 'unmarshallable object'
+            exc = raises(ValueError, marshal.dumps, (subtype(),))
+            assert str(exc.value) == 'unmarshallable object'
 
     def test_valid_subtypes(self):
         import marshal
diff --git a/pypy/objspace/std/marshal_impl.py 
b/pypy/objspace/std/marshal_impl.py
--- a/pypy/objspace/std/marshal_impl.py
+++ b/pypy/objspace/std/marshal_impl.py
@@ -65,10 +65,13 @@
 
 def marshal(space, w_obj, m):
     # _marshallers_unroll is defined at the end of the file
-    for type, func in _marshallers_unroll:
-        if isinstance(w_obj, type):
-            func(space, w_obj, m)
-            return
+    # NOTE that if w_obj is a heap type, like an instance of a
+    # user-defined subclass, then we skip that part completely!
+    if not space.type(w_obj).is_heaptype():
+        for type, func in _marshallers_unroll:
+            if isinstance(w_obj, type):
+                func(space, w_obj, m)
+                return
 
     # any unknown object implementing the buffer protocol is
     # accepted and encoded as a plain string
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to