[pypy-svn] r74526 - in pypy/trunk/pypy/module/cpyext: . test

agaynor at codespeak.net agaynor at codespeak.net
Mon May 17 15:33:20 CEST 2010


Author: agaynor
Date: Mon May 17 15:33:18 2010
New Revision: 74526

Modified:
   pypy/trunk/pypy/module/cpyext/stubs.py
   pypy/trunk/pypy/module/cpyext/test/test_unicodeobject.py
   pypy/trunk/pypy/module/cpyext/unicodeobject.py
Log:
Implemented PyUnicode_DecodeUTF8 in cpyext

Modified: pypy/trunk/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubs.py	Mon May 17 15:33:18 2010
@@ -3091,15 +3091,6 @@
     changes in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
- at cpython_api([rffi.CCHARP, Py_ssize_t, rffi.CCHARP], PyObject)
-def PyUnicode_DecodeUTF8(space, s, size, errors):
-    """Create a Unicode object by decoding size bytes of the UTF-8 encoded string
-    s. Return NULL if an exception was raised by the codec.
-    
-    This function used an int type for size. This might require
-    changes in your code for properly supporting 64-bit systems."""
-    raise NotImplementedError
-
 @cpython_api([rffi.CCHARP, Py_ssize_t, rffi.CCHARP, Py_ssize_t], PyObject)
 def PyUnicode_DecodeUTF8Stateful(space, s, size, errors, consumed):
     """If consumed is NULL, behave like PyUnicode_DecodeUTF8(). If

Modified: pypy/trunk/pypy/module/cpyext/test/test_unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_unicodeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_unicodeobject.py	Mon May 17 15:33:18 2010
@@ -63,6 +63,17 @@
         w_res = api.PyUnicode_AsUTF8String(w_u)
         assert space.type(w_res) is space.w_str
         assert space.unwrap(w_res) == 'sp\xc3\xa4m'
+    
+    def test_decode_utf8(self, space, api):
+        u = rffi.str2charp(u'späm'.encode("utf-8"))
+        w_u = api.PyUnicode_DecodeUTF8(u, 5, None)
+        assert space.type(w_u) is space.w_unicode
+        assert space.unwrap(w_u) == u'späm'
+        
+        w_u = api.PyUnicode_DecodeUTF8(u, 2, None)
+        assert space.type(w_u) is space.w_unicode
+        assert space.unwrap(w_u) == 'sp'
+        rffi.free_charp(u)
 
     def test_IS(self, space, api):
         for char in [0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x1c, 0x1d, 0x1e, 0x1f,

Modified: pypy/trunk/pypy/module/cpyext/unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/unicodeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/unicodeobject.py	Mon May 17 15:33:18 2010
@@ -288,6 +288,18 @@
         PyErr_BadArgument(space)
     return unicodetype.encode_object(space, w_unicode, "utf-8", "strict")
 
+ at cpython_api([rffi.CCHARP, Py_ssize_t, rffi.CCHARP], PyObject)
+def PyUnicode_DecodeUTF8(space, s, size, errors):
+    """Create a Unicode object by decoding size bytes of the UTF-8 encoded string
+    s. Return NULL if an exception was raised by the codec.
+    """
+    w_str = space.wrap(rffi.charpsize2str(s, size))
+    if errors:
+        w_errors = space.wrap(rffi.charp2str(errors))
+    else:
+        w_errors = space.w_None
+    return space.call_method(w_str, 'decode', space.wrap("utf-8"), w_errors)
+    
 if sys.platform == 'win32':
     @cpython_api([CONST_WSTRING, Py_ssize_t, CONST_STRING], PyObject)
     def PyUnicode_EncodeMBCS(space, wchar_p, length, errors):



More information about the Pypy-commit mailing list