5778 Inability to set locale leads to stack trace
authorDanek Duvall <danek.duvall@sun.com>
Thu, 18 Dec 2008 11:15:47 -0800
changeset 791 123e9401e939
parent 790 e2cf47b23e7a
child 792 060a802056a4
5778 Inability to set locale leads to stack trace
src/client.py
src/depot.py
src/modules/misc.py
src/packagemanager.py
src/updatemanager.py
src/updatemanagernotifier.py
--- a/src/client.py	Thu Dec 18 13:08:47 2008 -0600
+++ b/src/client.py	Thu Dec 18 11:15:47 2008 -0800
@@ -2068,7 +2068,7 @@
         global __img
         __img = img = image.Image()
 
-        locale.setlocale(locale.LC_ALL, '')
+        misc.setlocale(locale.LC_ALL, "", error)
         gettext.install("pkg", "/usr/share/locale")
 
         try:
--- a/src/depot.py	Thu Dec 18 13:08:47 2008 -0600
+++ b/src/depot.py	Thu Dec 18 11:15:47 2008 -0800
@@ -94,7 +94,7 @@
 import pkg.server.repository as repo
 import pkg.server.repositoryconfig as rc
 import pkg.search_errors as search_errors
-from pkg.misc import port_available, msg, emsg
+from pkg.misc import port_available, msg, emsg, setlocale
 
 class LogSink(object):
         """This is a dummy object that we can use to discard log entries
@@ -151,7 +151,7 @@
 
 if __name__ == "__main__":
 
-        locale.setlocale(locale.LC_ALL, "")
+        setlocale(locale.LC_ALL, "")
         gettext.install("pkg", "/usr/share/locale")
 
         port = PORT_DEFAULT
--- a/src/modules/misc.py	Thu Dec 18 13:08:47 2008 -0600
+++ b/src/modules/misc.py	Thu Dec 18 11:15:47 2008 -0800
@@ -38,6 +38,7 @@
 import time
 import calendar
 import shutil
+import locale
 from stat import *
 
 import pkg.urlhelpers as urlhelpers
@@ -300,6 +301,26 @@
                         raise PipeError, e
                 raise
 
+def setlocale(category, loc=None, printer=None):
+        """Wraps locale.setlocale(), falling back to the C locale if the desired
+        locale is broken or unavailable.  The 'printer' parameter should be a
+        function which takes a string and displays it.  If 'None' (the default),
+        setlocale() will print the message to stderr."""
+
+        if printer is None:
+                printer = emsg
+
+        try:
+                locale.setlocale(category, loc)
+        except locale.Error:
+                try:
+                        dl = " '%s.%s'" % locale.getdefaultlocale()
+                except ValueError:
+                        dl = ""
+                printer("Unable to set locale%s; locale package may be broken "
+                    "or\nnot installed.  Reverting to C locale." % dl)
+                locale.setlocale(category, "C")
+
 def port_available(host, port):
         """Returns True if the indicated port is available to bind to;
         otherwise returns False."""
--- a/src/packagemanager.py	Thu Dec 18 13:08:47 2008 -0600
+++ b/src/packagemanager.py	Thu Dec 18 11:15:47 2008 -0800
@@ -100,7 +100,7 @@
                         self.application_dir = os.environ["PACKAGE_MANAGER_ROOT"]
                 except KeyError:
                         self.application_dir = "/"
-                locale.setlocale(locale.LC_ALL, '')
+                misc.setlocale(locale.LC_ALL, "")
                 for module in (gettext, gtk.glade):
                         module.bindtextdomain("pkg", self.application_dir + \
                             "/usr/share/locale")
--- a/src/updatemanager.py	Thu Dec 18 13:08:47 2008 -0600
+++ b/src/updatemanager.py	Thu Dec 18 11:15:47 2008 -0800
@@ -211,7 +211,7 @@
                         self.application_dir = os.environ["UPDATE_MANAGER_ROOT"]
                 except KeyError:
                         self.application_dir = "/"
-                locale.setlocale(locale.LC_ALL, '')
+                misc.setlocale(locale.LC_ALL, "")
                 for module in (gettext, gtk.glade):
                         module.bindtextdomain("pkg", self.application_dir +
                             "/usr/share/locale")
--- a/src/updatemanagernotifier.py	Thu Dec 18 13:08:47 2008 -0600
+++ b/src/updatemanagernotifier.py	Thu Dec 18 11:15:47 2008 -0800
@@ -48,6 +48,7 @@
         sys.exit(1)
 import pkg.client.image as image
 import pkg.client.progress as progress
+import pkg.misc as misc
 
 # Put _() in the global namespace
 import __builtin__
@@ -90,7 +91,7 @@
                         self.application_dir = os.environ["UPDATE_MANAGER_NOTIFIER_ROOT"]
                 except KeyError:
                         self.application_dir = "/"
-                locale.setlocale(locale.LC_ALL, '')
+                misc.setlocale(locale.LC_ALL, "")
                 for module in (gettext, gtk.glade):
                         module.bindtextdomain("pkg", self.application_dir + \
                             "/usr/share/locale")