[pypy-svn] r78846 - in pypy/branch/fast-forward/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Sun Nov 7 23:12:49 CET 2010


Author: afa
Date: Sun Nov  7 23:12:47 2010
New Revision: 78846

Modified:
   pypy/branch/fast-forward/pypy/module/cpyext/stubs.py
   pypy/branch/fast-forward/pypy/module/cpyext/test/test_unicodeobject.py
   pypy/branch/fast-forward/pypy/module/cpyext/unicodeobject.py
Log:
PyUnicode_FromObject, PyUnicode_Compare


Modified: pypy/branch/fast-forward/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/stubs.py	Sun Nov  7 23:12:47 2010
@@ -2741,12 +2741,6 @@
     possible.  This macro does not raise exceptions."""
     raise NotImplementedError
 
- at cpython_api([PyObject], PyObject)
-def PyUnicode_FromObject(space, obj):
-    """Shortcut for PyUnicode_FromEncodedObject(obj, NULL, "strict") which is used
-    throughout the interpreter whenever coercion to Unicode is needed."""
-    raise NotImplementedError
-
 @cpython_api([rffi.CWCHARP, Py_ssize_t, rffi.CCHARP, rffi.CCHARP], PyObject)
 def PyUnicode_Encode(space, s, size, encoding, errors):
     """Encode the Py_UNICODE buffer of the given size and return a Python
@@ -3133,12 +3127,6 @@
     require changes in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
- at cpython_api([PyObject, PyObject], rffi.INT_real, error=-2)
-def PyUnicode_Compare(space, left, right):
-    """Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
-    respectively."""
-    raise NotImplementedError
-
 @cpython_api([PyObject, PyObject, rffi.INT_real], PyObject)
 def PyUnicode_RichCompare(space, left, right, op):
     """Rich compare two unicode strings and return one of the following:

Modified: pypy/branch/fast-forward/pypy/module/cpyext/test/test_unicodeobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/test/test_unicodeobject.py	(original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/test/test_unicodeobject.py	Sun Nov  7 23:12:47 2010
@@ -122,6 +122,12 @@
         assert api.Py_UNICODE_TOUPPER(u'ä') == u'Ä'
         assert api.Py_UNICODE_TOUPPER(u'Ä') == u'Ä'
 
+    def test_fromobject(self, space, api):
+        w_u = space.wrap(u'a')
+        assert api.PyUnicode_FromObject(w_u) is w_u
+        assert space.unwrap(
+            api.PyUnicode_FromObject(space.wrap('test'))) == 'test'
+
     def test_decode(self, space, api):
         b_text = rffi.str2charp('caf\x82xx')
         b_encoding = rffi.str2charp('cp437')
@@ -216,3 +222,6 @@
 
         test("\xFE\xFF\x00\x61\x00\x62\x00\x63\x00\x64", 0, 1)
         test("\xFF\xFE\x61\x00\x62\x00\x63\x00\x64\x00", 0, -1)
+
+    def test_compare(self, space, api):
+        assert api.PyUnicode_Compare(space.wrap('a'), space.wrap('b')) == -1

Modified: pypy/branch/fast-forward/pypy/module/cpyext/unicodeobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/unicodeobject.py	(original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/unicodeobject.py	Sun Nov  7 23:12:47 2010
@@ -253,6 +253,15 @@
         w_errors = space.w_None
     return space.call_method(w_str, 'decode', w_encoding, w_errors)
 
+ at cpython_api([PyObject], PyObject)
+def PyUnicode_FromObject(space, w_obj):
+    """Shortcut for PyUnicode_FromEncodedObject(obj, NULL, "strict") which is used
+    throughout the interpreter whenever coercion to Unicode is needed."""
+    if space.is_w(space.type(w_obj), space.w_unicode):
+        return w_obj
+    else:
+        return space.call_function(space.w_unicode, w_obj)
+
 @cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject)
 def PyUnicode_FromEncodedObject(space, w_obj, encoding, errors):
     """Coerce an encoded object obj to an Unicode object and return a reference with
@@ -424,3 +433,10 @@
         else:
             w_errors = space.w_None
         return space.call_method(w_str, 'decode', w_encoding, w_errors)
+
+ at cpython_api([PyObject, PyObject], rffi.INT_real, error=-2)
+def PyUnicode_Compare(space, w_left, w_right):
+    """Compare two strings and return -1, 0, 1 for less than, equal, and greater
+    than, respectively."""
+    return space.int_w(space.cmp(w_left, w_right))
+



More information about the Pypy-commit mailing list