You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The bytes() builtin should respect an __bytes__() converter if it exists.
E.g. instead of
>>> classFoo:
... def__bytes__(self): returnb'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'
I took a quick glance at this. It hinges on how the C-API is going to
look. Currently, bytes is known in C as PyString and gets it's
representation from __str__. Although we could just change it to
__bytes__, Christian has said that he is going to rename it to PyBytes
(and what is now PyBytes -> PyByteArray). [1] Further muddying the
waters is the fact that PyObject_Str generates the unicode
representation of an object and should really be called PyObject_Unicode.
Here's a patch. It's only implemented for bytes. Doing this for
bytearray would require a bit of refactoring, and can I think wait for
3.1. I added two new C functions. PyObject_Bytes and PyBytes_FromObject.
You can review it at http://codereview.appspot.com/3245.
Well, if I figured out how to use Rietveld correctly, I've left some
questions for you in the review. It looks basically pretty good, so if
you could answer those questions, you can commit the change.
Activity
warsaw commentedon Mar 19, 2008
The bytes() builtin should respect an __bytes__() converter if it exists.
E.g. instead of
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
benjaminp commentedon Mar 19, 2008
I took a quick glance at this. It hinges on how the C-API is going to
look. Currently, bytes is known in C as PyString and gets it's
representation from __str__. Although we could just change it to
__bytes__, Christian has said that he is going to rename it to PyBytes
(and what is now PyBytes -> PyByteArray). [1] Further muddying the
waters is the fact that PyObject_Str generates the unicode
representation of an object and should really be called PyObject_Unicode.
[1] http://mail.python.org/pipermail/python-3000/2008-March/012477.html
benjaminp commentedon Aug 21, 2008
Here's a patch. It's only implemented for bytes. Doing this for
bytearray would require a bit of refactoring, and can I think wait for
3.1. I added two new C functions. PyObject_Bytes and PyBytes_FromObject.
You can review it at http://codereview.appspot.com/3245.
pitrou commentedon Aug 21, 2008
Isn't it a new feature and, therefore, should wait for 3.1?
benjaminp commentedon Aug 21, 2008
Well, yes I suppose. However, I think it's a serious enough deficiency
that it should block. I'll let Barry decide, though.
warsaw commentedon Aug 26, 2008
Well, if I figured out how to use Rietveld correctly, I've left some
questions for you in the review. It looks basically pretty good, so if
you could answer those questions, you can commit the change.
Should __bytes__ support be backported to 2.6?
pitrou commentedon Aug 26, 2008
Isn't it already there in __str__?
Or do you mean just add support for the alternate method name?
warsaw commentedon Aug 26, 2008
yep, that's all i meant. it might not be worth it though.
benjaminp commentedon Aug 26, 2008
Thanks for the review, Barry! Committed in r66038. Sort of backported in
r66039 by aliasing PyObject_Bytes to PyObject_Str.