[Python-ideas] xmlrpc.client to deprecate Binary for bytes?

Masklinn masklinn at masklinn.net
Tue Sep 6 17:00:56 CEST 2011

Not sure if this is an actual idea/feature request or a bug. Might be half/half

In Python 2, many actual strings were "packaged" through the str type, so xmlrpclib had to provide an other wrapper (xmlrpclib.Binary) to differentiate between actual strings (to insert as-is) and binary blobs, to b64encode.

Among Python 3 fixes are the stricter separation between bytestrings (bytes) and text strings (str), but xmlrpc.client *still* requires the usage of a Binary wrapper, and trying to encode a bytes instance just blows up:

>>> xmlrpc.client.dumps((b'foo',))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/xmlrpc/client.py", line 956, in dumps
    data = m.dumps(params)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/xmlrpc/client.py", line 506, in dumps
    dump(v, write)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/xmlrpc/client.py", line 530, in __dump
    f(self, value, write)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/xmlrpc/client.py", line 569, in dump_string
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/xmlrpc/client.py", line 156, in escape
    s = s.replace("&", "&")
TypeError: expected an object with the buffer interface
>>> xmlrpc.client.dumps((xmlrpc.client.Binary(b'foo'),))

I think it would be worthwhile to do the following:
* Have xmlrpc.client treat dumping `bytes` (and related objects, such as bytearray or memoryview) as if they were `Binary`-wrapped (by b64encoding them and shipping them as binary data, which they are)
* Add a flag to xmlrpc.client's `ServerProxy` and `loads` to have binary data (<base64> nodes of xmlrpc) decode to `bytes` rather than `Binary` instances. This option would likely be `False` by default, at least to start with, so as not to break compatibility with Python 3.1 and 3.2 code.

PS: as seen above, xmlrpc claims to require objects "with the buffer interface", Python's documentation (http://docs.python.org/py3k/library/stdtypes.html?highlight=memoryview#memoryview) claims bytes and bytearray implement "the buffer protocol". Who is lying?

More information about the Python-ideas mailing list