This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author barry
Recipients barry
Date 2008-03-19.03:04:13
SpamBayes Score 0.07070151
Marked as misclassified No
Message-id <1205895854.9.0.402459814747.issue2415@psf.upfronthosting.co.za>
In-reply-to
Content
The bytes() builtin should respect an __bytes__() converter if it exists.
E.g. instead of

>>> class Foo:
...  def __bytes__(self): return b'foo'
... 
>>> bytes(Foo())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Foo' object is not iterable
>>> 

bytes(Foo()) should return b'foo'

Here's one use case.  email.header.Header instances represent email headers
(naturally) that conceptually are bytes, but also have a string
representation.  Say for example, a Subject header comes across the wire in
RFC 2033 encoded utf-8.  The unicode representation would be the value
of the
header decoded according to the RFC.  The bytes representation would be the
raw bytes seen on the wire.

The most natural way to retrieve each representation would be

>>> header = msg['subject']
>>> str(header)
'some string with non-ascii'
>>> bytes(header)
b'the rfc 2033 encoded raw header value'
History
Date User Action Args
2008-03-19 03:04:15barrysetspambayes_score: 0.0707015 -> 0.07070151
recipients: + barry
2008-03-19 03:04:14barrysetspambayes_score: 0.0707015 -> 0.0707015
messageid: <1205895854.9.0.402459814747.issue2415@psf.upfronthosting.co.za>
2008-03-19 03:04:13barrylinkissue2415 messages
2008-03-19 03:04:13barrycreate