From 7cfb0388fff7a1282141ed50b9b6d6cf65af55a1 Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Wed, 8 May 2013 13:52:42 +0100 Subject: [PATCH] Remove gettext.install() from cinder/__init__.py MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The gettext.install() function installs a builtin _() function which translates a string in the translation domain supplied to the install() function. If gettext.install() is called multiple times, it's the last call to the function which wins and the last supplied translation domain which is used e.g. >>> import os >>> os.environ['LANG'] = 'ja.UTF-8' >>> import gettext >>> gettext.install('keystone', unicode=1, localedir='/opt/stack/keystone/keystone/locale') >>> print _('Invalid syslog facility') n無効な syslog ファシリティ >>> gettext.install('cinder', unicode=1, localedir='/opt/stack/nova/cinder/locale') >>> print _('Invalid syslog facility') Invalid syslog facility Usually this function is called early on in a toplevel script and we assume that no other code will call it and override the installed _(). However, in Cinder, we have taken a shortcut to avoid having to call it explicitly from each script and instead call it from cinder/__init__.py. This shortcut would be perfectly fine if we were absolutely sure that nova modules would never be imported from another program. It's probably quite incorrect for a program to use cinder code (indeed, if we wanted to support this, Cinder code shouldn't use the default _() function) but nevertheless there are some corner cases where it happens. For example, the keystoneclient auth_token middleware tries to import cfg from cinder.openstack and this in turn causes gettext.install('cinder') in other projects like glance or quantum. To avoid any doubt here, let's just rip out the shortcut and always call gettext.install() from the top-level script. However, there's a bit of an annoying detail here - by default, nosetests starts in the current directly and tries to import all modules it finds to look for tests. Without the _() builtin installed, importing some modules like cinder.flags will fail. Since it only ever makes sense to load tests from the cinder/tests dir, we can ask nose to do that by using the --tests argument via setup.cfg. Note, this means that if you previously did this: $> tox -- cinder.tests.foo cinder.tests.bar then you must now do this: $> tox -- --tests cinder.tests.foo,cinder.tests.bar Change-Id: If4125d6bcbde63df95de129ac5c83b4a6d6f130a --- bin/cinder-all | 2 ++ bin/cinder-api | 2 ++ bin/cinder-backup | 2 ++ bin/cinder-volume | 2 ++ cinder/__init__.py | 5 ----- doc/source/devref/il8n.rst | 5 ++--- setup.cfg | 1 + 7 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bin/cinder-all b/bin/cinder-all index e384e89c6..6cfeedcce 100755 --- a/bin/cinder-all +++ b/bin/cinder-all @@ -30,6 +30,7 @@ continue attempting to launch the rest of the services. import eventlet eventlet.monkey_patch() +import gettext import os import sys @@ -39,6 +40,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath( if os.path.exists(os.path.join(possible_topdir, "cinder", "__init__.py")): sys.path.insert(0, possible_topdir) +gettext.install('cinder', unicode=1) from cinder import flags from cinder.openstack.common import log as logging diff --git a/bin/cinder-api b/bin/cinder-api index 13c3db511..68c06782b 100755 --- a/bin/cinder-api +++ b/bin/cinder-api @@ -26,6 +26,7 @@ import eventlet eventlet.monkey_patch() +import gettext import os import sys @@ -35,6 +36,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath( if os.path.exists(os.path.join(possible_topdir, "cinder", "__init__.py")): sys.path.insert(0, possible_topdir) +gettext.install('cinder', unicode=1) from cinder import flags from cinder.openstack.common import log as logging diff --git a/bin/cinder-backup b/bin/cinder-backup index 0ffadec9b..d0278a35a 100755 --- a/bin/cinder-backup +++ b/bin/cinder-backup @@ -17,6 +17,7 @@ """Starter script for Cinder Volume Backup.""" +import gettext import os import sys @@ -32,6 +33,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), if os.path.exists(os.path.join(possible_topdir, 'cinder', '__init__.py')): sys.path.insert(0, possible_topdir) +gettext.install('cinder', unicode=1) from cinder import flags from cinder.openstack.common import log as logging diff --git a/bin/cinder-volume b/bin/cinder-volume index 90f6b1c05..bf144830b 100755 --- a/bin/cinder-volume +++ b/bin/cinder-volume @@ -22,6 +22,7 @@ import eventlet eventlet.monkey_patch() +import gettext import os import sys @@ -33,6 +34,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), if os.path.exists(os.path.join(possible_topdir, 'cinder', '__init__.py')): sys.path.insert(0, possible_topdir) +gettext.install('cinder', unicode=1) from cinder import flags from cinder.openstack.common import log as logging diff --git a/cinder/__init__.py b/cinder/__init__.py index f8db8e875..d765b088e 100644 --- a/cinder/__init__.py +++ b/cinder/__init__.py @@ -30,8 +30,3 @@ .. moduleauthor:: Manish Singh .. moduleauthor:: Andy Smith """ - -import gettext - - -gettext.install('cinder', unicode=1) diff --git a/doc/source/devref/il8n.rst b/doc/source/devref/il8n.rst index fabd2ce49..7e53731d7 100644 --- a/doc/source/devref/il8n.rst +++ b/doc/source/devref/il8n.rst @@ -27,8 +27,7 @@ The ``_()`` function is brought into the global scope by doing:: import gettext gettext.install("cinder", unicode=1) -In general, you shouldn't need to add these to any cinder files, since the lines -are present in ``cinder/__init__.py``. However, if this code is missing, it may -result in an error that looks like like:: +These lines are needed in any toplevel script before any cinder modules are +imported. If this code is missing, it may result in an error that looks like:: NameError: name '_' is not defined diff --git a/setup.cfg b/setup.cfg index 3f72e16c7..37e5093ca 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,6 +23,7 @@ mapping_file = babel.cfg output_file = cinder/locale/cinder.pot [nosetests] +tests=cinder/tests cover-package = cinder cover-erase = true cover-inclusive = true -- 2.45.2