About print exception message

W

WaterWalk

Until Python 2.5, the exception object still uses ansi string. Thus,
in the following example:

f = open(u"\u6d4b.log")

Suppose the file to open does not exist, the output message of the
exception maybe like:
[Errno 2] No such file or directory: u'\u6d4b.log'

This is not a clear message.

I finally work out a rough solution. Since the unicode string in the
exception message is always in the form of "\uxxxx" or maybe
"\Uxxxxxxxx", it's possible to manual convert those unicode escape
sequence into the "real" unicode character. The following is the code:

import StringIO
import locale

STATE_NORMAL = 0
STATE_BACK_SLASH = 1
STATE_LOWER_U = 2
STATE_UPPER_U = 3

def ansiu2u(s, enc):
'"convert '\uxxxx' or '\Uxxxxxxxx' sequences in a non-unicode string
to their coresponding unicode characters'''
i = 0
state = STATE_NORMAL
s = unicode(s, enc)
result = StringIO.StringIO()
while i < len(s):
c = s
if state == STATE_NORMAL:
if c == u'\\':
state = STATE_BACK_SLASH
else:
result.write(c)
i += 1
elif state == STATE_BACK_SLASH:
if c == u'u':
state = STATE_LOWER_U
elif c == u'U':
state = STATE_UPPER_U
else:
state = STATE_NORMAL
result.write(u'\\')
result.write(c)
i += 1
elif state == STATE_LOWER_U:
unic = int(s[i : i + 4], 16)
unic = unichr(unic)
result.write(unic)
i += 4
state = STATE_NORMAL
elif state == STATE_UPPER_U:
unic = int(s[i : i + 8], 16)
unic = unichr(unic)
result.write(unic)
i += 8
state = STATE_NORMAL
r = result.getvalue()
result.close()
return r

def obj2unicode(obj):
s = str(obj)
return ansiu2u(s, locale.getdefaultlocale())

Using this function, when printing exceptions, the result will always
be in "good" forms. Any comments?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top