Shouldn't use unicode() when exception used in msgs

Bug #1380806 reported by James Carey
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Cinder
Fix Released
Low
Ivan Kolodyazhny
OpenStack Compute (nova)
Fix Released
Undecided
James Carey

Bug Description

There are cases (identified by mriedem) where an exception is used as replacement text and is coerced using unicode():

http://git.openstack.org/cgit/openstack/nova/tree/nova/compute/manager.py?id=2014.2.rc2#n3264

   reason=_("Driver Error: %s") % unicode(e))

http://git.openstack.org/cgit/openstack/nova/tree/nova/api/ec2/__init__.py?id=2014.2.rc2#n89

   LOG.exception(_("FaultWrapper: %s"), unicode(ex))

doing this can interfere with translation by causing things to be prematurely translated.

Need to scan for and correct any occurrences. Also need to look at adding/updating a hacking check.

James Carey (jecarey)
Changed in nova:
assignee: nobody → James Carey (jecarey)
Changed in nova:
status: New → Confirmed
Revision history for this message
Matt Riedemann (mriedem) wrote :

Is there a way to have a hacking check for this?

Revision history for this message
Matt Riedemann (mriedem) wrote :

For example, could we find out we're in an except block and handling an exception somehow, and that unicode() is being used? Then figure out it's the thing we're trying to avoid.

Revision history for this message
James Carey (jecarey) wrote :

I'm in the process of extending N325 to include this so it would go from:

      N325 str() cannot be used on an exception. Remove or use six.text_type()

to:

      N325 str() and unicode() cannot be used on an exception. Remove or use six.text_type()

I'm working through the things this flags now and will push up a patch.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to nova (master)

Fix proposed to branch: master
Review: https://review.openstack.org/129473

Changed in nova:
status: Confirmed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to nova (master)

Reviewed: https://review.openstack.org/129473
Committed: https://git.openstack.org/cgit/openstack/nova/commit/?id=6683905b532b6030989064636aa5506d4cbe5b22
Submitter: Jenkins
Branch: master

commit 6683905b532b6030989064636aa5506d4cbe5b22
Author: James Carey <email address hidden>
Date: Fri Oct 17 13:14:26 2014 -0700

    Remove use of unicode on exceptions

    Casting exceptions to unicode using unicode() can interfere
    with the proper translation of the exception message. This
    is especially true when lazy translation is enabled, since it
    causes the exception message to be translated immediately using
    the default locale.

    This is the same problem caused by using str on exceptions,
    which was fixed by https://review.openstack.org/#/c/116054/

    In addition to fixing these cases, this patch updates
    hacking check N325, which checks for the use of str() on
    exceptions, to also check for the use of unicode().

    In order to make it clear which cases that cannot be caught
    by the hacking check have been inspected, they have been
    converted to using six.text_type() instead of unicode().

    Closes-bug: #1380806
    Change-Id: I87bb94fa76458e028beba28d092320057b53f70a

Changed in nova:
status: In Progress → Fix Committed
Thierry Carrez (ttx)
Changed in nova:
milestone: none → kilo-1
status: Fix Committed → Fix Released
lvdongbing (dbcocle)
Changed in cinder:
assignee: nobody → lvdongbing (dbcocle)
Ivan Kolodyazhny (e0ne)
Changed in cinder:
status: New → Confirmed
importance: Undecided → Low
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to cinder (master)

Fix proposed to branch: master
Review: https://review.openstack.org/170156

Changed in cinder:
assignee: lvdongbing (dbcocle) → Ivan Kolodyazhny (e0ne)
status: Confirmed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to cinder (master)

Reviewed: https://review.openstack.org/170156
Committed: https://git.openstack.org/cgit/openstack/cinder/commit/?id=319bddcc03fd28a709de08f52ebe4f8ae20a6061
Submitter: Jenkins
Branch: master

commit 319bddcc03fd28a709de08f52ebe4f8ae20a6061
Author: Ivan Kolodyazhny <email address hidden>
Date: Thu Apr 2 17:55:48 2015 +0300

    Use six.text_type instead of unicode

    Add hacking checks for unicode() calls

    Change-Id: I27062986e69a90e3f4fc26a76080b146e825149b
    Closes-Bug: #1380806

Changed in cinder:
status: In Progress → Fix Committed
Ivan Kolodyazhny (e0ne)
Changed in cinder:
milestone: none → kilo-rc1
Thierry Carrez (ttx)
Changed in cinder:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in nova:
milestone: kilo-1 → 2015.1.0
Thierry Carrez (ttx)
Changed in cinder:
milestone: kilo-rc1 → 2015.1.0
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.