Commits

Felix Schwarz committed 4ae814b

catch exception if environment contains an invalid locale setting (fixes #200)

Comments (0)

Files changed (2)

                 locale = 'en_US_POSIX'
             elif aliases and locale in aliases:
                 locale = aliases[locale]
-            return '_'.join(filter(None, parse_locale(locale)))
+            try:
+                return '_'.join(filter(None, parse_locale(locale)))
+            except ValueError:
+                pass
 
 def negotiate_locale(preferred, available, sep='_', aliases=LOCALE_ALIASES):
     """Find the best match between available and requested locale strings.

babel/tests/core.py

 # history and logs, available at http://babel.edgewall.org/log/.
 
 import doctest
+import os
 import unittest
 
 from babel import core
+from babel.core import default_locale
+
+class DefaultLocaleTest(unittest.TestCase):
+    
+    def setUp(self):
+        self._old_locale_settings = self._current_locale_settings()
+    
+    def tearDown(self):
+        self._set_locale_settings(self._old_locale_settings)
+    
+    def _current_locale_settings(self):
+        settings = {}
+        for name in ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG'):
+            settings[name] = os.environ[name]
+        return settings
+    
+    def _set_locale_settings(self, settings):
+        for name, value in settings.items():
+            os.environ[name] = value
+    
+    def test_ignore_invalid_locales_in_lc_ctype(self):
+        # This is a regression test specifically for a bad LC_CTYPE setting on
+        # MacOS X 10.6 (#200)
+        os.environ['LC_CTYPE'] = 'UTF-8'
+        # must not throw an exception
+        default_locale('LC_CTYPE')
 
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(core))
+    suite.addTest(unittest.makeSuite(DefaultLocaleTest))
     return suite
 
 if __name__ == '__main__':