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
Error when printing an exception containing a Unicode string #46769
Comments
Python seems to have problems when an exception is thrown that >>> try:
... raise Exception(u'Error when printing ü')
... except Exception, e:
... print e
...
Traceback (most recent call last):
File "", line 4, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in
position 20:
ordinal not in range(128) See |
That is because Python encodes it's error messages as ASCII by default, |
To be more precise: I see no way to convert the encapsulated non-ASCII My solution around this right now is raising an exception with an But as the tutorials speak of simply "print e" I guess the behaviour |
Use: print unicode(e.message).encode("utf-8") |
Thanks, this does work. But, where can I find the piece of information you just gave to me in Further more will this be regarded as a bug? Please reopen if my report does have a point. |
Note the interpreter cannot print the exception either: >>> raise Exception(u'Error when printing ü')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception>>> |
I am going to reopen this issue for Py3k. The recommended encoding for In Py3k, |
Though I welcome the reopening of the bug for Python 3.0 I must say I never believed Python to be a programming language with good Unicode Some modules like the one for CSV are lacking full Unicode support. So I still hope to see this solved for the 2.x versions which I read |
|
Even in 2.5, __str__ is allowed to return a Unicode object; Index: exceptions.c --- exceptions.c (revision 61957)
+++ exceptions.c (working copy)
@@ -108,6 +104,11 @@
break;
case 1:
out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
+ if (out == NULL &&
PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
+ {
+ PyErr_Clear();
+ out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));
+ }
break;
default:
out = PyObject_Str(self->args); Then str(e) still raises UnicodeEncodeError, But I would like the opinion of an experimented core developer... |
After thinking some more, I'm going to add 2.6 to this. I'm attaching a Georg, can you review Amaury's and my patches? Also, would mine be a |
Shouldn't it be an exception rather than a warning? The fact that an Another possibility would be to display the warning, and *then* to |
Have you looked at PyErr_Display? There are many, many possible |
JFTR:
|
We can't do much about that because only security fixes are backported |
One of the examples Christoph tried was unicode(Exception(u'\xe1')) which fails quite oddly with: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in The reason for this is Exception lacks an __unicode__ method Fixing this seems quite important. It's common to want to raise errors I'm attaching a patch which implements __unicode__ for BaseException. A quick look through trunk suggests implementing tp_unicode actually |
Aha - the __unicode__ method was previously there in Python 2.5, and was The reversion is in |
On Mon, Jun 9, 2008 at 8:40 AM, Simon Cross <report@bugs.python.org> wrote:
What version are you using? In Py3k, str is unicode so __str__ can
Email Python-dev for permission. |
Concerning http://bugs.python.org/issue1551432: I'd much rather have working unicode(e) than working unicode(Exception). |
Benjamin Peterson wrote:
I'm sorry it wasn't clear. I'm aware that this issue doesn't apply to Code I'm developing that hits these issues are database exceptions with The patch I submitted is against trunk. |
Removing 3.0 from the versions list. |
So I've got a follow-up patch that adds tp_unicode. It does however generate the desired result in this case :-) |
On Mon, Jun 9, 2008 at 2:04 PM, David Fraser <report@bugs.python.org> wrote:
Unfortunately, adding a slot is a bit more complicated. You have to
|
As far as I am concerned, the implementation of PyObject_Unicode in Line 482 in object.c is the offending line: Fix that bug, then add a __unicode__ method back to Exception objects |
On 2008-06-11 11:32, Nick Coghlan wrote:
The only difference I can spot is that the PyObject_Format() code
I'm not sure whether that would really solve anything. IMHO, it's better to implement the tp_unicode slot and then |
Here's the key difference with the way PyObject_Format looks up the PyObject *method = _PyType_Lookup(Py_TYPE(obj),
str__format__); _PyType_Lookup instead of PyObject_GetAttr - so unicode(Exception) would You then need the PyInstance_Check/PyObject_GetAttr special case for |
Attached a patch which implements Nick Coghlan's suggestion. All |
Minor cleanup of Simon's patch attached - aside from a couple of Not checking it in yet, since it isn't critical for this week's beta |
On 2008-06-11 16:15, Nick Coghlan wrote:
That approach is fine as well. I still like the idea to add a tp_unicode slot, though, since that's Perhaps we can have both ?! |
I'm not sure adding a dedicated method slot would be worth the hassle |
Re msg67974:
Thanks for the clean-up, Nick. The mixture of tabs and spaces in the |
On 2008-06-11 16:49, Nick Coghlan wrote:
AFAIK, _PyType_Lookup will only work for base types, ie. objects |
Justing prodding the issue again now that the betas are out. What's the |
Adding this to my personal to-do list for the next beta release. |
Fixed in 64791. Blocked from being merged to Py3k (since there is no longer a For MAL: the PyInstance_Check included in the patch for the benefit of |
Benjamin Peterson in comment https://bugs.python.org/issue2517#msg64771 wrote: "That is because Python encodes it's error messages as ASCII by default…" Could somebody please point where in the source code of Python 2 this happens? |
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: