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

getxsick at codespeak.net getxsick at codespeak.net
Sun Feb 15 01:48:46 CET 2009


Author: getxsick
Date: Sun Feb 15 01:48:43 2009
New Revision: 61909

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) strxfrm() implementation. some cleanups.


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 01:48:43 2009
@@ -7,7 +7,7 @@
             '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',

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 01:48:43 2009
@@ -189,6 +189,27 @@
 
 strcoll.unwrap_spec = [ObjSpace, W_Root, W_Root]
 
+_strxfrm = external('strxfrm', [rffi.CCHARP, rffi.CCHARP, rffi.SIZE_T],
+                                                                rffi.SIZE_T)
+
+def strxfrm(space, s):
+    "string -> string. Returns a string that behaves for cmp locale-aware."
+    n1 = len(s) + 1
+    try:
+        buf = lltype.malloc(rffi.CCHARP.TO, n1, flavor="raw", zero=True)
+        n2 = _strxfrm(buf, rffi.str2charp(s), n1) + 1
+        if n2 > n1:
+            # more space needed
+            lltype.free(buf, flavor="raw")
+            buf = lltype.malloc(rffi.CCHARP.TO, n2, flavor="raw", zero=True)
+            _strxfrm(buf, rffi.str2charp(s), n2)
+        val = rffi.charp2str(buf)
+    finally:
+        lltype.free(buf, flavor="raw")
+    return space.wrap(val)
+
+strxfrm.unwrap_spec = [ObjSpace, str]
+
 _gettext = external('gettext', [rffi.CCHARP], rffi.CCHARP)
 
 def gettext(space, msg):
@@ -214,7 +235,8 @@
 
 dgettext.unwrap_spec = [ObjSpace, W_Root, str]
 
-_dcgettext = external('dcgettext', [rffi.CCHARP, rffi.CCHARP, rffi.INT], rffi.CCHARP)
+_dcgettext = external('dcgettext', [rffi.CCHARP, rffi.CCHARP, rffi.INT],
+                                                                rffi.CCHARP)
 
 def dcgettext(space, w_domain, msg, category):
     """dcgettext(domain, msg, category) -> string

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 01:48:43 2009
@@ -7,10 +7,6 @@
     def setup_class(cls):
         cls.space = gettestobjspace(usemodules=['_locale'])
 
-        #cls.w_locale = cls.space.appexec([], """():
-        #    import locale
-        #    return locale""")
-
     def test_import(self):
         import _locale
         assert _locale
@@ -119,6 +115,17 @@
         raises(TypeError, _locale.strcoll, 1, u"b")
         raises(TypeError, _locale.strcoll, u"b", 1)
 
+    def test_strxfrm(self):
+        # TODO more tests would be nice
+        import _locale
+
+        a = "1234"
+        b = _locale.strxfrm(a)
+        assert a is not b
+        assert a == b
+
+        raises(TypeError, _locale.strxfrm, 1)
+
     def test_str_float(self):
         import _locale
         import locale



More information about the Pypy-commit mailing list