[Yum-devel] [PATCH] Fix a corner case in exception2msg(). BZ 749239.

tim.lauridsen at gmail.com tim.lauridsen at gmail.com
Wed Nov 2 08:03:00 UTC 2011


On Wed, Nov 2, 2011 at 6:35 AM, Toshio Kuratomi <a.badger at gmail.com> wrote:

> 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
>
> _______________________________________________
> Yum-devel mailing list
> Yum-devel at lists.baseurl.org
> http://lists.baseurl.org/mailman/listinfo/yum-devel
>
>
Maybe it is time to add python-kitchen as a requirement to yum and start
using the kitchen converters, they work great and  it has must better naming
to_unicode, to_bytes, exception_to_unicode and exception_to_bytes, much
easier to understand and has good documentation

(Great work, Toshio )

Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.baseurl.org/pipermail/yum-devel/attachments/20111102/f64df362/attachment.html>


More information about the Yum-devel mailing list