aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Gustak <jlg@usrsrc.net>2009-02-14 22:37:11 +0000
committerJakub Gustak <jlg@usrsrc.net>2009-02-14 22:37:11 +0000
commit6d55f845bc07ef3531d178a0ff009b9d64afb768 (patch)
treec5e1dea116c4bfc79cad97ca1ce2d1a0b1cc9296 /pypy/module/_locale
parentNot a real change, just indent the code in a "if 1:" block (diff)
downloadpypy-6d55f845bc07ef3531d178a0ff009b9d64afb768.tar.gz
pypy-6d55f845bc07ef3531d178a0ff009b9d64afb768.tar.bz2
pypy-6d55f845bc07ef3531d178a0ff009b9d64afb768.zip
(getxsick, jlg) strcoll partially implemented, does not work with unicode arguments - rffi.uniocode2wcharp() needed
Diffstat (limited to 'pypy/module/_locale')
-rw-r--r--pypy/module/_locale/__init__.py2
-rw-r--r--pypy/module/_locale/interp_locale.py29
-rw-r--r--pypy/module/_locale/test/test_locale.py27
3 files changed, 57 insertions, 1 deletions
diff --git a/pypy/module/_locale/__init__.py b/pypy/module/_locale/__init__.py
index f904ff0642..0a3aae7aa7 100644
--- a/pypy/module/_locale/__init__.py
+++ b/pypy/module/_locale/__init__.py
@@ -7,7 +7,7 @@ class Module(MixedModule):
#'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',
diff --git a/pypy/module/_locale/interp_locale.py b/pypy/module/_locale/interp_locale.py
index efa695d3f7..3b31e121be 100644
--- a/pypy/module/_locale/interp_locale.py
+++ b/pypy/module/_locale/interp_locale.py
@@ -163,3 +163,32 @@ def localeconv(space):
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]
+
diff --git a/pypy/module/_locale/test/test_locale.py b/pypy/module/_locale/test/test_locale.py
index 6b70c5b2ab..1f3dad1b97 100644
--- a/pypy/module/_locale/test/test_locale.py
+++ b/pypy/module/_locale/test/test_locale.py
@@ -92,6 +92,33 @@ class AppTestLocaleTrivia:
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