[Yum-devel] [PATCH] yum.i18n issues

Luke Macken lmacken at redhat.com
Tue May 6 23:38:07 UTC 2008


On Mon, May 05, 2008 at 04:20:05PM -0400, James Antill wrote:
> 
> On Mon, 2008-05-05 at 15:53 -0400, Luke Macken wrote:
> > Hey,
> > 
> > So I recently ran into a problem with some test cases for another project, caused
> > by yum overriding the builtin '_' and replacing it with the yum.i18n.dummy_wrapper.
> > One of the libraries I am using then tried to pass a 'domain' argument to
> > '_', thus triggering an exception, since the dummy_wrapper does not take
> > any arguments.
> > 
> > The yum.i18n module runs gettext.translation.install(), which ideally
> > should only be used for applications[0], not modules[1].
> > 
> > So, a potential solution would be to define '_' in the i18n module,
> > by doing something like this:
> > 
> >     t = gettext.translation('yum', fallback=True)
> >     _ = t.ugettext
> > 
> > And import '_' from it where necessary.
> > 
> > What do you guys think?
> 
>  So that's how we had the code for a while, the big reason we changed
> from that is that we wanted to be able to turn gettext off from within
> the yum API (this was when unicode was causing _lots_ of problems, due
> to stdout's error handler).
>  Assuming that:
> 
>  i18n._ = i18n.dummy_wrapper
> 
> ...works[1] fine from within __init__.py ... I think it's fine to change
> it back.
>  Oh, and also make sure that test/yum-release-i18n-test.sh works after
> that change is installed on the running system ... just to be sure.
> 
> 
> [1] Read: Changes what _() does for everything that's imported _ from
> yum.i18n ... I'd guess this would happen, but I haven't tested it.

Patch attached.

All of the unit tests run fine, and the yum-release-i18n-test seemed to
run without any explosions.  'gaftonmode' also works as well.
-------------- next part --------------
>From b675e137bf36fe161938b016ff36867305b432f2 Mon Sep 17 00:00:00 2001
From: Luke Macken <lmacken at redhat.com>
Date: Tue, 6 May 2008 18:04:24 -0400
Subject: [PATCH] Make yum.i18n less intrusive.

Instead of altering the global __builtin__ namespace, which can potentially
break applications that import yum as a module, make internationalization
within yum as easy as doing `from yum import _`.  This change also ensures
that the yum.i18n.dummy_wrapper can still be used in 'gaftonmode' to bypass
gettext completely.

diff --git a/callback.py b/callback.py
index 4d75a08..2f6154e 100644
--- a/callback.py
+++ b/callback.py
@@ -22,9 +22,9 @@ import rpm
 import os
 import sys
 import logging
+from yum import _
 from yum.constants import *
 
-import yum.i18n
 
 class RPMInstallCallback:
 
diff --git a/cli.py b/cli.py
index e413797..dcb8e96 100644
--- a/cli.py
+++ b/cli.py
@@ -40,7 +40,7 @@ import rpmUtils.arch
 from rpmUtils.arch import isMultiLibArch
 import rpmUtils.miscutils
 from yum.packages import parsePackages, YumLocalPackage
-import yum.i18n
+from yum import _
 from yum.rpmtrans import RPMTransaction
 import signal
 import yumcommands
diff --git a/output.py b/output.py
index 0f312b9..752e18b 100644
--- a/output.py
+++ b/output.py
@@ -23,7 +23,6 @@ import logging
 import types
 import gettext
 import rpm
-import yum.i18n
 
 import re # For YumTerm
 
@@ -33,7 +32,7 @@ from yum.misc import sortPkgObj, prco_tuple_to_string, to_str, to_unicode_maybe,
 from rpmUtils.miscutils import checkSignals
 from yum.constants import *
 
-from yum import logginglevels
+from yum import logginglevels, _
 from yum.rpmtrans import RPMBaseCallback
 
 from textwrap import fill
diff --git a/utils.py b/utils.py
index 82534cf..d92f533 100644
--- a/utils.py
+++ b/utils.py
@@ -18,8 +18,7 @@ import time
 
 import yum
 from cli import *
-import yum.i18n
-
+from yum import _
 
 import yum.plugins as plugins
 
diff --git a/yum/__init__.py b/yum/__init__.py
index 6c8ad25..2bc6fe7 100644
--- a/yum/__init__.py
+++ b/yum/__init__.py
@@ -32,6 +32,9 @@ import logging.config
 import operator
 import gzip
 
+import yum.i18n
+_ = yum.i18n._
+
 try:
     from iniparse.compat import ParsingError, ConfigParser
 except ImportError:
@@ -63,7 +66,6 @@ warnings.simplefilter("ignore", Errors.YumFutureDeprecationWarning)
 from packages import parsePackages, YumAvailablePackage, YumLocalPackage, YumInstalledPackage
 from constants import *
 from yum.rpmtrans import RPMTransaction,SimpleCliCallBack
-import yum.i18n
 from misc import to_unicode
 
 import string
@@ -163,9 +165,9 @@ class YumBase(depsolve.Depsolve):
 
         startupconf = config.readStartupConfig(fn, root)
         if startupconf.gaftonmode:
-            import __builtin__
-            __builtin__.__dict__['_'] = i18n.dummy_wrapper
-        
+            global _
+            _ = yum.i18n.dummy_wrapper
+
         if debuglevel != None:
             startupconf.debuglevel = debuglevel
         if errorlevel != None:
diff --git a/yum/depsolve.py b/yum/depsolve.py
index 2012a40..a08f481 100644
--- a/yum/depsolve.py
+++ b/yum/depsolve.py
@@ -35,11 +35,12 @@ from constants import *
 import packages
 import logginglevels
 import Errors
-import i18n
 import warnings
 warnings.simplefilter("ignore", Errors.YumFutureDeprecationWarning)
 from operator import itemgetter
 
+from yum import _
+
 try:
     assert max(2, 4) == 4
 except:
diff --git a/yum/i18n.py b/yum/i18n.py
index 84c4882..86f3ce2 100644
--- a/yum/i18n.py
+++ b/yum/i18n.py
@@ -26,12 +26,11 @@ try:
     using ugettext to make sure translated strings are in Unicode.
     '''
     import gettext
-    t = gettext.translation('yum')
-    t.install(unicode=True)
+    t = gettext.translation('yum', fallback=True)
+    _ = t.ugettext
 except:
     '''
     Something went wrong so we make a dummy _() wrapper there is just
     returning the same text
     '''
-    import __builtin__
-    __builtin__.__dict__['_'] = dummy_wrapper
+    _ = dummy_wrapper
diff --git a/yum/plugins.py b/yum/plugins.py
index 86c9e51..9e094ed 100644
--- a/yum/plugins.py
+++ b/yum/plugins.py
@@ -31,12 +31,12 @@ import Errors
 from parser import ConfigPreProcessor
 
 from textwrap import fill
-import i18n
-
 import fnmatch
 
 from weakref import proxy as weakref
 
+from yum import _
+
 # TODO: expose rpm package sack objects to plugins (once finished)
 # TODO: allow plugins to use the existing config stuff to define options for
 # their own configuration files (would replace confString() etc).
diff --git a/yum/rpmtrans.py b/yum/rpmtrans.py
index 2fb51a2..456d7f5 100644
--- a/yum/rpmtrans.py
+++ b/yum/rpmtrans.py
@@ -24,7 +24,7 @@ import logging
 import types
 import sys
 from yum.constants import *
-import i18n
+from yum import _
 
 
 class NoOutputCallBack:
diff --git a/yumcommands.py b/yumcommands.py
index 9dce44f..0ba6689 100644
--- a/yumcommands.py
+++ b/yumcommands.py
@@ -22,9 +22,8 @@ Classes for subcommands of the yum command line interface.
 import os
 import cli
 from yum import logginglevels
+from yum import _
 import yum.Errors
-import yum.i18n
-
 import operator
 
 def checkRootUID(base):
diff --git a/yummain.py b/yummain.py
index 9421d02..15b505d 100755
--- a/yummain.py
+++ b/yummain.py
@@ -27,7 +27,7 @@ import time # test purposes only
 from yum import Errors
 from yum import plugins
 from yum import logginglevels
-import yum.i18n
+from yum import _
 import cli
 
 
-- 
1.5.4.5



More information about the Yum-devel mailing list