[Python-Dev] CVS: python/dist/src/Lib xmlrpclib.py,1.11,1.12

Skip Montanaro skip@pobox.com (Skip Montanaro)
Wed, 10 Oct 2001 16:17:16 -0500


    >> 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