[Yum-devel] [PATCH 2/2] Convert YumBase exceptions from str(e) to exception2msg(e). BZ 669027.

Toshio Kuratomi a.badger at gmail.com
Wed Jan 12 18:21:29 UTC 2011


On Wed, Jan 12, 2011 at 10:14 AM, Toshio Kuratomi <a.badger at gmail.com> wrote:
>
> I'll give you a couple examples below:
>
I forgot to mention how to prevent new errors along with the examples.
 The fixes I'll mention all assume that you're not getting errors
currently.  (ie: they'll prevent new errors from creeping in but they
won't prevent old errors that we were unaware of from showing up).

> On Wed, Jan 12, 2011 at 09:58:01AM -0500, James Antill wrote:
>>              try:
>>                  self._getTs(needTsRemove)
>>              except yum.Errors.YumBaseError, e:
>> -                return 1, [str(e)]
>> +                return 1, [exception2msg(e)]
>>
> The old code here would always return a byte str.  The new code may return
> unicode.  If you get a unicode exception, it would probably be in code that
> attempts to catch yum.Errors.YumBaseError.
>
Transform this into a byte str before returning it.  For instance:
    return 1, [to_utf8(exception2msg(e))]

>>                  except yum.Errors.ConfigError, e:
>> -                    self.logger.critical(e)
>> +                    self.logger.critical(exception2msg(e))
>>                      self.base.usage()
>>                      sys.exit(1)
>>
> I'm not quite sure how the logger handles conversion between unicode and str
> internally.  You may get UnicodeErrors when using this if exception2msg()
> returns unicode type where the logger's internal handling was extracting the
> message from the exception as a byte string (exercising different code
> paths inside of the logger).
>
I don't have a good answer for this one just by looking at the patch.
It depends on what's happening within logger.

Errors will be more consistent if you make sure exception2msg is
either str or unicode, however.  Example:
    self.logger.critical(to_utf8(exception2msg(e)))

>>          except yum.Errors.YumBaseError, e:
>> -            return 1, [to_unicode(e)]
>> +            return 1, [exception2msg(e)]
>>
> These could trigger UnicodeErrors if exception2msg returns a byte str
> instead of unicode.  The exception would likely occur in code that attempts
> to catch yum.Errors.YumBaseError.

The solution here is to make sure the output is unicode.  In kitchen's
exception_to_unicode() function, the last thing it does is run
to_unicode() on the message we're going to return.  here it's the same
thing:
    return 1, [to_unicode(exception2msg(e))]

-Toshio


More information about the Yum-devel mailing list