On Tue, Nov 01, 2011 at 06:13:16PM -0400, James Antill wrote:
> On Mon, 2011-10-31 at 10:01 +0100, Zdeněk Pavlas wrote:
> > Under some locales, python creates UTF8 encoded non-unicode
> > exceptions.  For such, exception2msg() returns values that
> > fail to convert to unicode.
> 
>  In general we can't have generic functions (like exception2msg())
> returning str and unicode in different code paths ... it almost
> guarantees unicode errors. Also changing the return type is probably
> bad, even though it's relatively new.
>
The problem is that the current exception2msg() may return unicode or may
return str.  It all depends on the exception passed in and the locale
settings.

>  I think I originally had it return str() objects because it was
> replacing code like: "str(e)" with "exception2msg(e)" ... although not
> it's used in a few places where it'd be better as unicode now. So maybe
> another function (uexception2msg?) or just lots of to_unicode calls?

Currently, most of the time, exception2msg() will return a unicode object.
Sometimes it will return a byte str instead.  Take a look at how kitchen
does it:

http://lists.baseurl.org/pipermail/yum-devel/2011-January/007798.html
http://bzr.fedorahosted.org/bzr/kitchen/devel/annotate/head:/kitchen/text/converters.py#L430

The code is pretty simple (the only tricky part is that we loop through
several possible converter functions to get the msg out instead of
hardcoding the possibilities in the function.)  If you wanted to copy the
byte and unicode separation from kitchen but keep using the yum functions and
coding style, it'd look something like this:


def exception2bmsg(e):
    try:
        return to_utf8(e.value)
    except:
        pass

    try:
        return to_utf8(e)
    except:
        pass

    try:
        # Yes, it's possible for a class to define a __str__() method that 
returns unicode
        return to_utf8(str(e))
    except:
        pass
    return "<exception failed to convert to text>"


def exception2umsg(e):
    try:
        return to_unicode(e.value)
    except:
        pass

    try:
        return to_unicode(e)
    except:
        pass

    try:
        return to_unicode(str(e))
    except:
        pass
    return u"<exception failed to convert to text>"

My personal preference would be to stick closer to the kitchen code for
exception_to_unicode() and exception_to_bytes() instead but then again,
I wrote them.

https://fedorahosted.org/releases/k/i/kitchen/docs/api-text-converters.html#kitchen.text.converters.exception_to_unicode
https://fedorahosted.org/releases/k/i/kitchen/docs/api-text-converters.html#kitchen.text.converters.exception_to_bytes

-Toshio

Attachment: pgpzcPwOc7s8K.pgp
Description: PGP signature

_______________________________________________
Yum-devel mailing list
Yum-devel@lists.baseurl.org
http://lists.baseurl.org/mailman/listinfo/yum-devel

Reply via email to