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

jlg at codespeak.net jlg at codespeak.net
Sat Feb 14 23:37:12 CET 2009


Author: jlg
Date: Sat Feb 14 23:37:11 2009
New Revision: 61895

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) strcoll partially implemented, does not work with unicode arguments - rffi.uniocode2wcharp() needed


Modified: pypy/trunk/pypy/module/_locale/__init__.py
==============================================================================
--- pypy/trunk/pypy/module/_locale/__init__.py	(original)
+++ pypy/trunk/pypy/module/_locale/__init__.py	Sat Feb 14 23:37:11 2009
@@ -7,7 +7,7 @@
             #'lconv':               'interp_locale.lconv',
             'setlocale':           'interp_locale.setlocale',
             'localeconv':          'interp_locale.localeconv',
-            #'strcoll':             'interp_locale.strcoll',
+            'strcoll':             'interp_locale.strcoll',
             #'strxfrm':             'interp_locale.strxfrm',
             #'getdefaultlocale':    'interp_locale.getdefaultlocale',
             #'gettext':             'interp_locale.gettext',

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	Sat Feb 14 23:37:11 2009
@@ -163,3 +163,32 @@
 
 localeconv.unwrap_spec = [ObjSpace]
 
+_strcoll = external('strcoll', [rffi.CCHARP, rffi.CCHARP], rffi.INT)
+_wcscoll = external('wcscoll', [rffi.WCHAR_TP, rffi.WCHAR_TP], rffi.INT)
+
+def strcoll(space, w_s1, w_s2):
+    "string,string -> int. Compares two strings according to the locale."
+
+    if space.is_true(space.isinstance(w_s1, space.w_str)) and \
+       space.is_true(space.isinstance(w_s2, space.w_str)):
+
+        s1, s2 = space.str_w(w_s1), space.str_w(w_s2)
+        return space.wrap(_strcoll(rffi.str2charp(s1), rffi.str2charp(s2)))
+
+    #if not space.is_true(space.isinstance(w_s1, space.w_unicode)) and \
+    #   not space.is_true(space.isinstance(w_s2, space.w_unicode)):
+    #    raise OperationError(space.w_ValueError,
+    #                         space.wrap("strcoll arguments must be strings"))
+
+    s1, s2 = space.unicode_w(w_s1), space.unicode_w(w_s2)
+
+    # XXX rffi.unicode2wcharp needed
+    raise OperationError(space.w_NotImplementedError,
+                         space.wrap("PyPy specific - not implemented for unicode arguments"))
+
+    #s1_c = rffi.str2charp(s1)
+    #s2_c = rffi.str2charp(s2)
+    #return space.wrap(_wcscoll(s1_c, s2_c))
+
+strcoll.unwrap_spec = [ObjSpace, W_Root, W_Root]
+

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	Sat Feb 14 23:37:11 2009
@@ -92,6 +92,33 @@
         for k, v in lconv_c.items():
             assert lconv[k] == v
 
+    def test_strcoll(self):
+        import _locale
+
+        _locale.setlocale(_locale.LC_ALL, "pl_PL.UTF-8")
+        assert _locale.strcoll("a", "b") < 0
+        assert _locale.strcoll('\xc4\x85', "b") < 0
+
+        assert _locale.strcoll('\xc4\x87', "b") > 0
+        assert _locale.strcoll("c", "b") > 0
+
+        assert _locale.strcoll("b", "b") == 0
+
+        raises(TypeError, _locale.strcoll, 1, "b")
+        raises(TypeError, _locale.strcoll, "b", 1)
+
+    def test_strcoll_unicode(self):
+        skip("not implemented, rffi.unicode2wcharp needed")
+        import _locale
+
+        _locale.setlocale(_locale.LC_ALL, "pl_PL.UTF-8")
+        assert _locale.strcoll(u"b", u"b") == 0
+        assert _locale.strcoll(u'\xc4\x85', "b") < 0
+        assert _locale.strcoll(u'\xc4\x87', "b") > 0
+
+        raises(TypeError, _locale.strcoll, 1, u"b")
+        raises(TypeError, _locale.strcoll, u"b", 1)
+
     def test_str_float(self):
         import _locale
         import locale



More information about the Pypy-commit mailing list