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
unicode(exception) and str(exception) should return the same message on Py2.6 #50358
Comments
On Python 2.5 str(exception) and unicode(exception) return the same text:
>>> err
UnicodeDecodeError('ascii', '\xc3\xa0', 0, 1, 'ordinal not in range(128)')
>>> str(err)
"'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in
range(128)"
>>> unicode(err)
u"'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in
range(128)"
On Python 2.6 unicode(exception) returns unicode(exception.args):
>>> err
UnicodeDecodeError('ascii', '\xc3\xa0', 0, 1, 'ordinal not in range(128)')
>>> str(err)
"'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in
range(128)"
>>> unicode(err)
u"('ascii', '\\xc3\\xa0', 0, 1, 'ordinal not in range(128)')" This seems to affect only exceptions with more than 1 arg (e.g. Note that when an exception like ValueError() is instantiated with more Probably __str__() checks the number of args before returning a specific Attached there's a script that prints the repr(), str() and unicode() of |
Perhaps also worth noting is that in Python 2.4 as well, str(exception) |
Looks like a potentially annoying bug to me. |
Since we do not yet have a patch for this, I'm knocking it off the list |
In r64791, BaseException gained a new __unicode__ method that does the
Before this, BaseException only had a __str__ method, so unicode(e)
Now, all the derived exceptions that don't implement their own Possible solutions:
Attached there's a proof of concept (bpo-6108.diff) where I tried to The patch seems to work fine for me (note: this is my first attempt to |
It remains to be seen why that behaviour was chosen. Apparently Nick |
Following this down the rabbit hole a little further: Issue bpo-2517 (the At the time of the r64791 checkin, BaseException_str and However, it looks like several exceptions with __str__ overrides (i.e. |
If BaseException has both the methods they have to be both overridden by |
Well the obvious problem with this approach is that it won't work if |
As Antoine said, there's a reason BaseException now implements both >>> str(Exception(u"\xc3\xa0"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-1: ordinal not in range(128)
>>> unicode(Exception(u"\xc3\xa0"))
u'\xc3\xa0' For some of the exception subclasses that will always return ASCII (e.g. For others (as happened with BaseException itself), the __unicode__ |
What you said is only a special case, and I agree that the solution To summarize the possible cases and the behaviours I prepared the
3a) 1 str arg, e = Exception('foo'): 3b) 1 non-ascii unicode arg, e = Exception(u'föö'):
As you can see, your example corresponds just to the case 3b) (now Making this list allowed me to come out with a new patch, that seems to Attached new patch that passes all the tests in issue6108_testcase |
"2) 0 args, e = MyException(), with overridden __str__: I'm not sure how you justify raising an unnecessary error when trying to __str__ should not decode its arguments if they are already strings: __str__ and __unicode__ are /different/ things, claiming they have to Surely there is space for both things, which does imply that Why _should_ that be the same anyway? |
I agree the 2.6 implementation creates backwards compatibility problems An alternative approach that should work even for the KeyError case is That way subclasses that only override __str__ would continue to see the |
Assume the case of e = MyException() (note: 0 args) with a __str__ that |
This is even better, I'll try to do it. |
Here is a new patch (bpo-6108-3.patch) that checks if __str__ has been All the tests (including the one with KeyError) in If the patch is OK I'll make sure that the tests cover all the possible |
You should check the return value from PyObject_Str(). |
I created a comprehensive set of tests to check all the possibilities |
Great! |
I updated the patch and moved the helper class outside the __init__. |
This looks fine, module the slight style issue mentioned on IRC. Please |
This should be the final patch (bpo-6108-6.patch). I update the |
It's ok for me. |
Fixed in r77045 (trunk) and r77046 (release26-maint). No need to port it |
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: