]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #1070046: Marshal new-style objects like InstanceType
authorMartin v. Löwis <martin@v.loewis.de>
Sun, 19 Nov 2006 18:51:54 +0000 (18:51 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sun, 19 Nov 2006 18:51:54 +0000 (18:51 +0000)
in xmlrpclib.

Doc/lib/libxmlrpclib.tex
Lib/test/test_xmlrpc.py
Lib/xmlrpclib.py
Misc/NEWS

index 3645b824d8641c1323ade0d0b93230e301787b22..e3caab30eb2b33863e2578d58d3778920a706708 100644 (file)
@@ -68,7 +68,10 @@ Python type):
   \lineii{arrays}{Any Python sequence type containing conformable
                   elements. Arrays are returned as lists}
   \lineii{structures}{A Python dictionary. Keys must be strings,
-                      values may be any conformable type.}
+                      values may be any conformable type. Objects
+                      of user-defined classes can be passed in;
+                      only their \var{__dict__} attribute is 
+                      transmitted.}
   \lineii{dates}{in seconds since the epoch (pass in an instance of the
                  \class{DateTime} class) or a
                  \class{\refmodule{datetime}.datetime},
@@ -100,6 +103,10 @@ described below.
 compatibility.  New code should use \class{ServerProxy}.
 
 \versionchanged[The \var{use_datetime} flag was added]{2.5}
+
+\versionchanged[Instances of new-style classes can be passed in
+if they have an \var{__dict__} attribute and don't have a base class
+that is marshalled in a special way.}{2.6}
 \end{classdesc}
 
 
index 64d8fe86bc1af04b51f9c69efefac769efa0f640..ccc1b602ecb8130ec1669daab17772f10018e70b 100644 (file)
@@ -86,6 +86,15 @@ class XMLRPCTestCase(unittest.TestCase):
         s = xmlrpclib.dumps((new_d,), methodresponse=True)
         self.assert_(isinstance(s, str))
 
+    def test_newstyle_class(self):
+        class T(object):
+            pass
+        t = T()
+        t.x = 100
+        t.y = "Hello"
+        ((t2,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((t,)))
+        self.assertEquals(t2, t.__dict__)
+
     def test_dump_big_long(self):
         self.assertRaises(OverflowError, xmlrpclib.dumps, (2L**99,))
 
index 6fb6c68db51819ae1d7344701285e23bceb5cd20..3864dadc0b241d269d5896528bcf316bd3705379 100644 (file)
@@ -630,9 +630,19 @@ class Marshaller:
         try:
             f = self.dispatch[type(value)]
         except KeyError:
-            raise TypeError, "cannot marshal %s objects" % type(value)
-        else:
-            f(self, value, write)
+            # check if this object can be marshalled as a structure
+            try:
+                value.__dict__
+            except:
+                raise TypeError, "cannot marshal %s objects" % type(value)
+            # check if this class is a sub-class of a basic type,
+            # because we don't know how to marshal these types
+            # (e.g. a string sub-class)
+            for type_ in type(value).__mro__:
+                if type_ in self.dispatch.keys():
+                    raise TypeError, "cannot marshal %s objects" % type(value)
+            f = self.dispatch[InstanceType]
+        f(self, value, write)
 
     def dump_nil (self, value, write):
         if not self.allow_none:
index 9b2aebae3b0f167fe13cf17651b1e27baf88150b..849911bcdd8e5d78d659c9110fc2626237c59f7f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -101,6 +101,9 @@ Core and builtins
 Library
 -------
 
+- Patch #1070046: Marshal new-style objects like InstanceType
+  in xmlrpclib.
+
 - cStringIO.truncate(-1) now raises an IOError, like StringIO and
   regular files.