[Zodb-checkins] CVS: Packages/ZConfig - datatypes.py:1.1.2.9

Fred L. Drake, Jr. fred@zope.com
Fri, 13 Dec 2002 12:58:06 -0500


Update of /cvs-repository/Packages/ZConfig
In directory cvs.zope.org:/tmp/cvs-serv21317

Modified Files:
      Tag: zconfig-schema-devel-branch
	datatypes.py 
Log Message:
Simplify implementation of the locale datatype:
- We don't need the class anymore, just a callable
- No need to protect against ImportError; the locale module is always
  available in the standard library


=== Packages/ZConfig/datatypes.py 1.1.2.8 => 1.1.2.9 ===
--- Packages/ZConfig/datatypes.py:1.1.2.8	Fri Dec 13 12:28:11 2002
+++ Packages/ZConfig/datatypes.py	Fri Dec 13 12:58:05 2002
@@ -70,40 +70,22 @@
                              + `value`)
 
 
-class Locale:
-    def convert(self, value):
-        locale = self._get_locale_module()
-        # get current setting of locale
-        prev = locale.setlocale(locale.LC_ALL)
+def check_locale(value):
+    import locale
+    prev = locale.setlocale(locale.LC_ALL)
+    try:
         try:
-            try:
-                locale.setlocale(locale.LC_ALL, value)
-            finally:
-                locale.setlocale(locale.LC_ALL, prev)
-        except locale.Error:
-            raise ValueError(
-                'The specified locale "%s" is not supported by your system.\n'
-                'See your operating system documentation for more\n'
-                'information on locale support.' % value
-                )
+            locale.setlocale(locale.LC_ALL, value)
+        finally:
+            locale.setlocale(locale.LC_ALL, prev)
+    except locale.Error:
+        raise ValueError(
+            'The specified locale "%s" is not supported by your system.\n'
+            'See your operating system documentation for more\n'
+            'information on locale support.' % value)
+    else:
         return value
 
-    def _get_locale_module(self):
-        try:
-            return self._locale
-        except AttributeError:
-            try:
-                import locale
-            except ImportError:
-                raise ValueError(
-                    'The locale module could not be imported.\n'
-                    'To use localization options, you must ensure\n'
-                    'that the locale module is compiled into your\n'
-                    'Python installation.'
-                    )
-            Locale._locale = locale
-            return locale
-
 
 class BasicKeyConversion(RegularExpressionConversion):
     def __init__(self):
@@ -157,7 +139,7 @@
     "integer":       int,
     "float":         float,
     "str":           str,
-    "locale":        MemoizedConversion(Locale().convert).convert,
+    "locale":        MemoizedConversion(check_locale).convert,
     "port-number":   RangeCheckedConversion(int, min=1, max=0xffff).convert,
     "basic-key":     BasicKeyConversion().convert,
     "identifier":    IdentifierConversion().convert,