[Python-Dev] unicode Exception messages in py2.7

Steven D'Aprano steve at pearwood.info
Fri Nov 15 00:58:42 CET 2013


On Thu, Nov 14, 2013 at 04:55:19PM -0500, Tres Seaver wrote:

> Fixing any bug is "changing behavior";  2.7 is not frozen for bugfixes.

It's not a given that the current behaviour *is* a bug. Exception 
messages in 2 are byte-strings, not Unicode. Trying to use Unicode 
instead is not, as far as I can tell, supported behaviour.

If the exception message cannot be converted to a byte-string, 
suppressing the display of the message seems like perfectly reasonable 
behaviour to me:

py> class NoString:
...     def __str__(self):
...             raise ValueError
...
py> msg = NoString
py> msg = NoString()
py> print msg
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in __str__
ValueError
py> raise TypeError(msg)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeErrorpy>

although it would be nice if a newline was used so the prompt was bumped 
to the next line.

The point is, I'm not convinced that this is a bug at all.


> The real question is whether third-party code will break when the
> now-empty error messages appear with '?' littered through them?

This behaviour goes back to at least Python 2.4, the oldest version I 
have easy access to at the moment that includes Unicode. Given that this 
alleged bug has been around for so long, I don't think that it effects 
terribly many people. That implies that fixing it won't benefit many 
people either.


> About the only things I can think of which might break would be doctests,
> but people *expect* those to break across third-dot releases of Python

Which people? I certainly don't expect doctests to break unless I've 
done something silly.


> (one reason why I hate them).  Exception repr is explicitly *not* part of
> any backward-compatibility guarantees in Python.

Do you have a link for that explicit non-guarantee from the docs please?



-- 
Steven


More information about the Python-Dev mailing list