About print exception message

WaterWalk toolmaster at 163.com
Thu Oct 9 15:37:04 CEST 2008


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[i]
        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?



More information about the Python-list mailing list