On Apr 9, 2009, at 11:55 AM, Daniel Stutzbach wrote:

On Thu, Apr 9, 2009 at 6:01 AM, Barry Warsaw <ba...@python.org> wrote:
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).

Won't this work? (assuming dumps() always returns a string)

def dumpb(obj, encoding='utf-8', *args, **kw):
    s = dumps(obj, *args, **kw)
    return s.encode(encoding)

So, what I'm really asking is this. Let's say you agree that there are use cases for accessing a header value as either the raw encoded bytes or the decoded unicode. What should this return:

>>> message['Subject']

The raw bytes or the decoded unicode?

Okay, so you've picked one.  Now how do you spell the other way?

The Message class probably has these explicit methods:

>>> Message.get_header_bytes('Subject')
>>> Message.get_header_string('Subject')

(or better names... it's late and I'm tired ;). One of those maps to message['Subject'] but which is the more obvious choice?

Now, setting headers. Sometimes you have some unicode thing and sometimes you have some bytes. You need to end up with bytes in the ASCII range and you'd like to leave the header value unencoded if so. But in both cases, you might have bytes or characters outside that range, so you need an explicit encoding, defaulting to utf-8 probably.

>>> Message.set_header('Subject', 'Some text', encoding='utf-8')
>>> Message.set_header('Subject', b'Some bytes')

One of those maps to

>>> message['Subject'] = ???

I'm open to any suggestions here!
-Barry

Attachment: PGP.sig
Description: This is a digitally signed message part

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to