[pypy-svn] r61849 - in pypy/trunk/pypy/module/_locale: . test

jlg at codespeak.net jlg at codespeak.net
Fri Feb 13 20:49:48 CET 2009


Author: jlg
Date: Fri Feb 13 20:49:46 2009
New Revision: 61849

Modified:
   pypy/trunk/pypy/module/_locale/__init__.py
   pypy/trunk/pypy/module/_locale/interp_locale.py
   pypy/trunk/pypy/module/_locale/test/test_locale.py
Log:
(getxsick, jlg) setlocal() fully implemented


Modified: pypy/trunk/pypy/module/_locale/__init__.py
==============================================================================
--- pypy/trunk/pypy/module/_locale/__init__.py	(original)
+++ pypy/trunk/pypy/module/_locale/__init__.py	Fri Feb 13 20:49:46 2009
@@ -4,10 +4,7 @@
     """Support for POSIX locales."""
 
     interpleveldefs  = {
-            #'Error':               'interp_locale.Error',
-            #'LocalConfigure':      'interp_locale.LocaleConfigure',
             #'lconv':               'interp_locale.lconv',
-            #'fixup_ulcase':        'interp_locale.fixup_ulcase',
             'setlocale':           'interp_locale.setlocale',
             'localeconv':          'interp_locale.localeconv',
             #'strcoll':             'interp_locale.strcoll',
@@ -17,6 +14,8 @@
             }
 
     appleveldefs  = {
+            'Error':               'app_locale.Error',
+            '_fixup_ulcase':        'app_locale._fixup_ulcase',
             }
 
     def buildloaders(cls):

Modified: pypy/trunk/pypy/module/_locale/interp_locale.py
==============================================================================
--- pypy/trunk/pypy/module/_locale/interp_locale.py	(original)
+++ pypy/trunk/pypy/module/_locale/interp_locale.py	Fri Feb 13 20:49:46 2009
@@ -1,7 +1,8 @@
 from pypy.rpython.tool import rffi_platform as platform
 from pypy.rpython.lltypesystem import rffi, lltype
 
-from pypy.interpreter.gateway import interp2app, ObjSpace, W_Root
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import ObjSpace, W_Root
 
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 
@@ -92,13 +93,37 @@
 def external(name, args, result):
     return rffi.llexternal(name, args, result, compilation_info=CConfig._compilation_info_)
 
+def make_error(space, msg):
+    w_module = space.getbuiltinmodule('_locale')
+    w_exception_class = space.getattr(w_module, space.wrap('Error'))
+    w_exception = space.call_function(w_exception_class, space.wrap(msg))
+    return OperationError(w_exception_class, w_exception)
+
 _setlocale = external('setlocale', [rffi.INT, rffi.CCHARP], rffi.CCHARP)
 
-def setlocale(space, category, locale):
-    result = _setlocale(rffi.cast(rffi.INT, category), rffi.str2charp(locale))
+def setlocale(space, category, w_locale=None):
+    if space.is_w(w_locale, space.w_None) or w_locale is None:
+
+        result = _setlocale(rffi.cast(rffi.INT, category), None)
+        if not result:
+            raise make_error(space, "locale query failed")
+
+    else:
+        locale = rffi.str2charp(space.str_w(w_locale))
+
+        result = _setlocale(rffi.cast(rffi.INT, category), locale)
+        if not result:
+            raise make_error(space, "unsupported locale setting")
+
+        # record changes to LC_CTYPE
+        if category in (LC_CTYPE, LC_ALL):
+            w_module = space.getbuiltinmodule('_locale')
+            w_fun = space.getattr(w_module, space.wrap('_fixup_ulcase'))
+            space.call_function(w_fun)
+
     return space.wrap(rffi.charp2str(result))
 
-setlocale.unwrap_spec = [ObjSpace, int, str]
+setlocale.unwrap_spec = [ObjSpace, int, W_Root]
 
 _lconv = lltype.Ptr(cConfig.lconv)
 _localeconv = external('localeconv', [], _lconv)

Modified: pypy/trunk/pypy/module/_locale/test/test_locale.py
==============================================================================
--- pypy/trunk/pypy/module/_locale/test/test_locale.py	(original)
+++ pypy/trunk/pypy/module/_locale/test/test_locale.py	Fri Feb 13 20:49:46 2009
@@ -39,6 +39,30 @@
         for constant in _CONSTANTS:
             assert hasattr(_locale, constant)
 
+    def test_setlocale(self):
+        import _locale
+
+        raises(TypeError, _locale.setlocale, "", "en_US")
+        raises(TypeError, _locale.setlocale, _locale.LC_ALL, 6)
+        raises(_locale.Error, _locale.setlocale, 123456, "en_US")
+
+        assert _locale.setlocale(_locale.LC_ALL, None)
+        assert _locale.setlocale(_locale.LC_ALL)
+
+    def test_string_ulcase(self):
+        import _locale, string
+
+        lcase = "abcdefghijklmnopqrstuvwxyz"
+        ucase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
+        _locale.setlocale(_locale.LC_ALL, "en_US.UTF-8")
+        assert string.lowercase == lcase
+        assert string.uppercase == ucase
+
+        _locale.setlocale(_locale.LC_ALL, "en_US")
+        assert string.lowercase != lcase
+        assert string.uppercase != ucase
+
     def test_str_float(self):
         import _locale
         import locale



More information about the Pypy-commit mailing list