New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bytes() should respect __bytes__ #46667
Comments
The bytes() builtin should respect an __bytes__() converter if it exists. >>> 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 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 [1] http://mail.python.org/pipermail/python-3000/2008-March/012477.html |
Here's a patch. It's only implemented for bytes. Doing this for |
Isn't it a new feature and, therefore, should wait for 3.1? |
Well, yes I suppose. However, I think it's a serious enough deficiency |
Well, if I figured out how to use Rietveld correctly, I've left some Should __bytes__ support be backported to 2.6? |
Isn't it already there in __str__? |
yep, that's all i meant. it might not be worth it though. |
Thanks for the review, Barry! Committed in r66038. Sort of backported in |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: