When I get an exception as cPickle.UnpicklingError: invalid load key, 'ÿ'. and I try to print it, it raises a unicode decode error when I try to insert it into my (unicode) error message:
try:
settings = _load()
except cPickle.UnpicklingError, err:
msg = _(u"Error reading ... (the error is: '%s')")
cont = askYes(msg % err, _(u"Settings Load Error")) # raises
Tried workarounds as in msg % unicode(err.message, encoding='utf-8') but apparently err.message is not valid unicode string ("UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 19: invalid start byte")
So what is the most pythonic way to handle this ? Should I pass 'ignore' or 'replace' to unicode() ?
Edit: askYes(None, msg % repr(err), _(u"Settings Load Error")) gives something like:
(the error is: 'UnpicklingError("invalid load key, '\xff'.",)'). # ff is ÿ
Does not blow but still...
Edit2: the errors I reported are a bit mixed up with artificial ones:
u'%s' % "cPickle.UnpicklingError: invalid load key, 'ÿ'."
Traceback (most recent call last):
File "<input>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 44: ordinal \
not in range(128)
That's from the interpreter inside pycharm - apparently ÿ is '\xc3\xbf' there (...)
repr, or more directly%rrather than%s, is the best way to display a string of dubious content -- it may or may not be intended to represent Unicode, but either the\x0fyou show or the0xffyou mention earlier make one ponder about the encoding. Iferr.messageis a random collection of bytes with no rhyme or reason, how could you possibly display it better than byrepr?!ignoreorreplacewould hide potentially precious information for debugging purposes -- never do that in an error message!repr- would it be better to use repr(err.message)? I'd appreciate a full answer(the error is: '"invalid load key, '\x0f'."')while repr(err):(the error is: 'UnpicklingError("invalid load key, '\x0f'.",)'). I'd rather have something in the lines of ` (the error is: UnpicklingError: "invalid load key, '\x0f'.")` - do I have to construct it manually ? Also I admit that whyrepr()manages to decode the string escapes me.'\x0f'gives no such problem -- while'\xff'would. Try decoding err.message as 'iso-8859-1', which cannot fail (it decodes every byte, though perhaps to a nonsense glyph), and you may learn more. BTW, no surprise thatreprhas no problem --reprnever fails -- it's the alchemic transmutation between'\xff'and'\x0f'that leaves me puzzled!err.messagewasinvalid load key, ' + chr(0xff)in the "UnicodeDecodeError: 'utf8' codec...position 19". How come repr never fails ? Does it use iso-8859-1 ?