CVS: python/dist/src/Lib xmlrpclib.py,1.11,1.12
It seems like it ought to handle subclasses of builtin classes like string.
That's desirable, indeed. I think Skip's concern is primarily how to implement that; that is my concern, atleast. I can think of an implementation that replaces def __dump(self, value): try: f = self.dispatch[type(value)] except KeyError: raise TypeError, "cannot marshal %s objects" % type(value) else: f(self, value) with def __dump(self, value): for t in type(value).__mro__: try: f = self.dispatch[t] except KeyError: pass else: f(self, value) return raise TypeError, "cannot marshal %s objects" % type(value) That has several draw-backs, though: - it does not work with Py 1.5, which /F still requires for xmlrpclib, - it may give ambiguous results, in case an object could act as either of the XML-RPC types (it works deterministic, but may still give the undesired outcome) - it doesn't work for classic classes and instances Can you come up with anything better? Regards, Martin P.S. I don't know whether usage of __mro__ itself would be a bug or a feature. I certainly take pride in coming up with that idea :-)
>> It seems like it ought to handle subclasses of builtin classes like >> string. Martin> That's desirable, indeed. I think Skip's concern is primarily Martin> how to implement that; that is my concern, atleast. No, I'm not worried about the implementation. I know one of you wizards can figure that out. I'm worried about the silent loss of information. Given this class definition: >>> class MyString(str): ... def __init__(self, s): ... str.__init__(s) ... self.foo = "xyz" ... >>> s = MyString("sdfsdf") >>> s 'sdfsdf' >>> s.foo 'xyz' Marshalling it into XML-RPC form in the obvious way will only transmit the string "sdfsdf" to the remote end and will lose the object's foo attribute, which might matter to the client and/or the server. XML-RPC does not provide any mechanism for automagically marshalling scalar objects other than ints, booleans, strings, doubles, ISO8601 date/time objects and base64 strings. The only compound objects it will marshal are structs (dicts, and only dicts with strings as keys) and arrays (tuples or lists). If you want to marshal other things, you have to deal with that at the application level. You could subclass xmlrpclib.Marshaller to encode class instances by encoding their __dict__ or whatever __getstate__ returns, but the the marshaller at the other end will have to be aware of your spiffy encoding and recreate an instance of the appropriate class (whatever that might be - remember the remote end just might not be written in Python). Skip
participants (2)
-
Martin v. Loewis -
Skip Montanaro