[yum-git] Branch 'yum-3_2_X' - yummain.py yum/misc.py

James Antill james at linux.duke.edu
Sun Aug 10 22:01:45 UTC 2008


 yum/misc.py |   21 +++++++++++++++++++++
 yummain.py  |   19 ++-----------------
 2 files changed, 23 insertions(+), 17 deletions(-)

New commits:
commit 0f717c3d0ac690a31cfeb674211baa7e8c11c51a
Author: James Antill <james at and.org>
Date:   Sun Aug 10 18:01:33 2008 -0400

    Create helper function setup_locale() to work around broken python unicode

diff --git a/yum/misc.py b/yum/misc.py
index daa948a..a60c17b 100644
--- a/yum/misc.py
+++ b/yum/misc.py
@@ -620,6 +620,27 @@ def to_xml(item, attrib=False):
     return item
 
 # ---------- i18n ----------
+import locale
+import sys
+def setup_locale(override_codecs=True, override_time=False):
+    # This test needs to be before locale.getpreferredencoding() as that
+    # does setlocale(LC_CTYPE, "")
+    try:
+        locale.setlocale(locale.LC_ALL, '')
+        # set time to C so that we output sane things in the logs (#433091)
+        if override_time:
+            locale.setlocale(locale.LC_TIME, 'C')
+    except locale.Error, e:
+        # default to C locale if we get a failure.
+        print >> sys.stderr, 'Failed to set locale, defaulting to C'
+        os.environ['LC_ALL'] = 'C'
+        locale.setlocale(locale.LC_ALL, 'C')
+        
+    if override_codecs:
+        import codecs
+        sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
+        sys.stdout.errors = 'replace'
+
 def to_unicode(obj, encoding='utf-8', errors='replace'):
     ''' convert a 'str' to 'unicode' '''
     if isinstance(obj, basestring):
diff --git a/yummain.py b/yummain.py
index 27c6b2c..fde590c 100755
--- a/yummain.py
+++ b/yummain.py
@@ -20,7 +20,6 @@ Entrance point for the yum command line interface.
 
 import os
 import sys
-import locale
 import logging
 import time # test purposes only
 
@@ -29,28 +28,14 @@ from yum import plugins
 from yum import logginglevels
 from yum import _
 from yum.misc import to_unicode
+import yum.misc
 import cli
 
 
 def main(args):
     """This does all the real work"""
 
-    # This test needs to be before locale.getpreferredencoding() as that
-    # does setlocale(LC_CTYPE, "")
-    try:
-        locale.setlocale(locale.LC_ALL, '')
-        # set time to C so that we output sane things in the logs (#433091)
-        locale.setlocale(locale.LC_TIME, 'C')
-    except locale.Error, e:
-        # default to C locale if we get a failure.
-        print >> sys.stderr, 'Failed to set locale, defaulting to C'
-        os.environ['LC_ALL'] = 'C'
-        locale.setlocale(locale.LC_ALL, 'C')
-        
-    if True: # not sys.stdout.isatty():
-        import codecs
-        sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
-        sys.stdout.errors = 'replace'
+    yum.misc.setup_locale(override_time=True)
 
     def exUserCancel():
         logger.critical(_('\n\nExiting on user cancel'))



More information about the Yum-cvs-commits mailing list