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

afa at codespeak.net afa at codespeak.net
Sun Nov 7 21:53:49 CET 2010


Author: afa
Date: Sun Nov  7 21:53:42 2010
New Revision: 78842

Modified:
   pypy/branch/fast-forward/pypy/module/cpyext/test/test_unicodeobject.py
   pypy/branch/fast-forward/pypy/module/cpyext/unicodeobject.py
Log:
Add PyUnicode_FromString, PyUnicode_FromStringAndSize


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 21:53:42 2010
@@ -65,6 +65,15 @@
         assert rffi.wcharp2unicode(buf) == 'a'
         rffi.free_wcharp(buf)
 
+    def test_fromstring(self, space, api):
+        s = rffi.str2charp(u'späm'.encode("utf-8"))
+        w_res = api.PyUnicode_FromString(s)
+        assert space.unwrap(w_res) == u'späm'
+
+        w_res = api.PyUnicode_FromStringAndSize(s, 4)
+        assert space.unwrap(w_res) == u'spä'
+        rffi.free_charp(s)
+
     def test_AsUTF8String(self, space, api):
         w_u = space.wrap(u'späm')
         w_res = api.PyUnicode_AsUTF8String(w_u)

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 21:53:42 2010
@@ -287,6 +287,25 @@
                              space.wrap("decoding Unicode is not supported"))
     return space.call_function(w_meth, w_encoding, w_errors)
 
+ at cpython_api([CONST_STRING], PyObject)
+def PyUnicode_FromString(space, s):
+    """Create a Unicode object from an UTF-8 encoded null-terminated char buffer"""
+    w_str = space.wrap(rffi.charp2str(s))
+    return space.call_method(w_str, 'decode', space.wrap("utf-8"))
+
+ at cpython_api([CONST_STRING, Py_ssize_t], PyObject)
+def PyUnicode_FromStringAndSize(space, s, size):
+    """Create a Unicode Object from the char buffer u. The bytes will be
+    interpreted as being UTF-8 encoded. u may also be NULL which causes the
+    contents to be undefined. It is the user's responsibility to fill in the
+    needed data. The buffer is copied into the new object. If the buffer is not
+    NULL, the return value might be a shared object. Therefore, modification of
+    the resulting Unicode object is only allowed when u is NULL."""
+    if not s:
+        raise NotImplementedError
+    w_str = space.wrap(rffi.charpsize2str(s, size))
+    return space.call_method(w_str, 'decode', space.wrap("utf-8"))
+
 @cpython_api([PyObject], PyObject)
 def PyUnicode_AsUTF8String(space, w_unicode):
     """Encode a Unicode object using UTF-8 and return the result as Python string



More information about the Pypy-commit mailing list