[Python-Dev] Dropping bytes "support" in json
steve at holdenweb.com
Thu Apr 9 14:07:15 CEST 2009
Barry Warsaw wrote:
> On Apr 9, 2009, at 1:15 AM, Antoine Pitrou wrote:
>> Guido van Rossum <guido <at> python.org> writes:
>>> I'm kind of surprised that a serialization protocol like JSON wouldn't
>>> support reading/writing bytes (as the serialized format -- I don't
>>> something equivalent AFAIK, and hence JSON doesn't allow it IIRC).
>>> Marshal and Pickle, for example, *always* treat the serialized format
>>> as bytes. And since in most cases it will be sent over a socket, at
>>> some point the serialized representation *will* be bytes, I presume.
>>> What makes supporting this hard?
>> It's not hard, it just means a lot of duplicated code if the library
>> wants to
>> support both str and bytes in an optimized way as Martin alluded to. This
>> duplicated code already exists in the C parts to support the 2.x
>> semantics of
>> accepting unicode objects as well as str, but not in the Python parts,
>> explains why the bytes support is broken in py3k - in 2.x, the same
>> Python code
>> can be used for str and unicode.
> This is an interesting question, and something I'm struggling with for
> the email package for 3.x. It turns out to be pretty convenient to have
> both a bytes and a string API, both for input and output, but I think
> email really wants to be represented internally as bytes. Maybe. Or
> maybe just for content bodies and not headers, or maybe both. Anyway,
> aside from that decision, I haven't come up with an elegant way to allow
> /output/ in both bytes and strings (input is I think theoretically
> easier by sniffing the arguments).
The real problem I came across in storing email in a relational database
was the inability to store messages as Unicode. Some messages have a
body in one encoding and an attachment in another, so the only ways to
store the messages are either as a monolithic bytes string that gets
parsed when the individual components are required or as a sequence of
components in the database's preferred encoding (if you want to keep the
original encoding most relational databases won't be able to help unless
you store the components as bytes).
All in all, as you might expect from a system that's been growing up
since 1970 or so, it can be quite intractable.
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Watch PyCon on video now! http://pycon.blip.tv/
More information about the Python-Dev