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

getxsick at codespeak.net getxsick at codespeak.net
Sun Feb 15 02:55:42 CET 2009


Author: getxsick
Date: Sun Feb 15 02:55:38 2009
New Revision: 61910

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:
(jlg, getxsick) nl_langinfo() implementation

Modified: pypy/trunk/pypy/module/_locale/__init__.py
==============================================================================
--- pypy/trunk/pypy/module/_locale/__init__.py	(original)
+++ pypy/trunk/pypy/module/_locale/__init__.py	Sun Feb 15 02:55:38 2009
@@ -7,12 +7,13 @@
             'setlocale':            'interp_locale.setlocale',
             'localeconv':           'interp_locale.localeconv',
             'strcoll':              'interp_locale.strcoll',
-            'strxfrm':             'interp_locale.strxfrm',
+            'strxfrm':              'interp_locale.strxfrm',
             #'getdefaultlocale':    'interp_locale.getdefaultlocale',
             'gettext':              'interp_locale.gettext',
             'dgettext':             'interp_locale.dgettext',
             'dcgettext':            'interp_locale.dcgettext',
             'textdomain':           'interp_locale.textdomain',
+            'nl_langinfo':          'interp_locale.nl_langinfo',
             }
 
     appleveldefs  = {

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	Sun Feb 15 02:55:38 2009
@@ -8,7 +8,7 @@
 
 class CConfig:
     _compilation_info_ = ExternalCompilationInfo(
-        includes = ['locale.h']
+        includes = ['locale.h', 'langinfo.h']
     )
     lconv = platform.Struct("struct lconv", [
             # Numeric (non-monetary) information.
@@ -76,6 +76,19 @@
 for name in constant_names:
     setattr(CConfig, name, platform.DefinedConstantInteger(name))
 
+
+langinfo_names = ('CODESET D_T_FMT D_FMT T_FMT RADIXCHAR THOUSEP '
+                  'YESEXPR NOEXPR CRNCYSTR AM_STR PM_STR').split(" ")
+for i in range(1, 8):
+    langinfo_names.append("DAY_%d" % i)
+    langinfo_names.append("ABDAY_%d" % i)
+for i in range(1, 13):
+    langinfo_names.append("MON_%d" % i)
+    langinfo_names.append("ABMON_%d" % i)
+
+for name in langinfo_names:
+    setattr(CConfig, name, platform.DefinedConstantInteger(name))
+
 class cConfig(object):
     pass
 
@@ -88,6 +101,11 @@
     if value is not None:
         constants[name] = value
 
+for name in langinfo_names:
+    value = getattr(cConfig, name)
+    if value is not None:
+        constants[name] = value
+
 locals().update(constants)
 
 def external(name, args, result):
@@ -272,3 +290,17 @@
     return space.wrap(rffi.charp2str(result))
 
 textdomain.unwrap_spec = [ObjSpace, W_Root]
+
+nl_item = rffi.INT
+_nl_langinfo = external('nl_langinfo', [nl_item], rffi.CCHARP)
+
+def nl_langinfo(space, key):
+    """nl_langinfo(key) -> string
+    Return the value for the locale information associated with key."""
+
+    if key in constants.values():
+        result = _nl_langinfo(rffi.cast(nl_item, key))
+        return space.wrap(rffi.charp2str(result))
+    raise OperationError(space.w_ValueError, "unsupported langinfo constant")
+
+nl_langinfo.unwrap_spec = [ObjSpace, int]

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	Sun Feb 15 02:55:38 2009
@@ -143,3 +143,71 @@
         assert _locale.dgettext(None, "1234") == "1234"
         assert _locale.dcgettext(None, "1234", _locale.LC_MESSAGES) == "1234"
         assert _locale.textdomain("1234") == "1234"
+
+    def test_nl_langinfo(self):
+        import _locale
+
+        langinfo_consts = [
+                            'ABDAY_1',
+                            'ABDAY_2',
+                            'ABDAY_3',
+                            'ABDAY_4',
+                            'ABDAY_5',
+                            'ABDAY_6',
+                            'ABDAY_7',
+                            'ABMON_1',
+                            'ABMON_10',
+                            'ABMON_11',
+                            'ABMON_12',
+                            'ABMON_2',
+                            'ABMON_3',
+                            'ABMON_4',
+                            'ABMON_5',
+                            'ABMON_6',
+                            'ABMON_7',
+                            'ABMON_8',
+                            'ABMON_9',
+                            'CODESET',
+                            'CRNCYSTR',
+                            'DAY_1',
+                            'DAY_2',
+                            'DAY_3',
+                            'DAY_4',
+                            'DAY_5',
+                            'DAY_6',
+                            'DAY_7',
+                            'D_FMT',
+                            'D_T_FMT',
+                            'MON_1',
+                            'MON_10',
+                            'MON_11',
+                            'MON_12',
+                            'MON_2',
+                            'MON_3',
+                            'MON_4',
+                            'MON_5',
+                            'MON_6',
+                            'MON_7',
+                            'MON_8',
+                            'MON_9',
+                            'NOEXPR',
+                            'RADIXCHAR',
+                            'THOUSEP',
+                            'T_FMT',
+                            'YESEXPR',
+                            'AM_STR',
+                            'PM_STR',
+                            ]
+        for constant in langinfo_consts:
+            assert hasattr(_locale, constant)
+
+        _locale.setlocale(_locale.LC_ALL, "C")
+        assert _locale.nl_langinfo(_locale.ABDAY_1) == "Sun"
+        assert _locale.nl_langinfo(_locale.ABMON_1) == "Jan"
+        assert _locale.nl_langinfo(_locale.T_FMT) == "%H:%M:%S"
+        assert _locale.nl_langinfo(_locale.YESEXPR) == '^[yY]'
+        assert _locale.nl_langinfo(_locale.NOEXPR) == "^[nN]"
+        assert _locale.nl_langinfo(_locale.THOUSEP) == ''
+
+        raises(ValueError, _locale.nl_langinfo, 12345)
+        raises(TypeError, _locale.nl_langinfo, None)



More information about the Pypy-commit mailing list