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

jandem at codespeak.net jandem at codespeak.net
Fri Apr 30 13:24:00 CEST 2010


Author: jandem
Date: Fri Apr 30 13:23:59 2010
New Revision: 74277

Modified:
   pypy/trunk/pypy/module/cpyext/test/test_unicodeobject.py
   pypy/trunk/pypy/module/cpyext/unicodeobject.py
Log:
Add PyUnicode_AsUTF8String


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	Fri Apr 30 13:23:59 2010
@@ -58,6 +58,12 @@
         assert rffi.wcharp2unicode(buf) == 'a'
         rffi.free_wcharp(buf)
 
+    def test_AsUTF8String(self, space, api):
+        w_u = space.wrap(u'späm')
+        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_IS(self, space, api):
         for char in [0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x1c, 0x1d, 0x1e, 0x1f,
                      0x20, 0x85, 0xa0, 0x1680, 0x2000, 0x2001, 0x2002,

Modified: pypy/trunk/pypy/module/cpyext/unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/unicodeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/unicodeobject.py	Fri Apr 30 13:23:59 2010
@@ -277,6 +277,15 @@
                              space.wrap("decoding Unicode is not supported"))
     return space.call_function(w_meth, w_encoding, w_errors)
 
+ at cpython_api([PyObject], PyObject)
+def PyUnicode_AsUTF8String(space, w_unicode):
+    """Encode a Unicode object using UTF-8 and return the result as Python string
+    object.  Error handling is "strict".  Return NULL if an exception was raised
+    by the codec."""
+    if not PyUnicode_Check(space, w_unicode):
+        PyErr_BadArgument(space)
+    return unicodetype.encode_object(space, w_unicode, "utf-8", "strict")
+
 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