[pypy-svn] r73567 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

trundle at codespeak.net trundle at codespeak.net
Thu Apr 8 19:14:19 CEST 2010


Author: trundle
Date: Thu Apr  8 19:14:18 2010
New Revision: 73567

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_unicodeobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/unicodeobject.py
Log:
Add PyUnicode_AsEncodedString.


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	Thu Apr  8 19:14:18 2010
@@ -5780,15 +5780,6 @@
     changes in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
- at cpython_api([PyObject, rffi.CCHARP, rffi.CCHARP], PyObject)
-def PyUnicode_AsEncodedString(space, unicode, encoding, errors):
-    """Encode a Unicode object and return the result as Python string object.
-    encoding and errors have the same meaning as the parameters of the same name
-    in the Unicode encode() method. The codec to be used is looked up using
-    the Python codec registry. Return NULL if an exception was raised by the
-    codec."""
-    raise NotImplementedError
-
 @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

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_unicodeobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_unicodeobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_unicodeobject.py	Thu Apr  8 19:14:18 2010
@@ -39,6 +39,14 @@
         raises(TypeError, api.PyUnicode_AsUnicode(space.wrap('spam')))
         api.PyErr_Clear()
 
+        utf_8 = rffi.str2charp('utf-8')
+        encoded = api.PyUnicode_AsEncodedString(space.wrap(u'späm'),
+                                                utf_8, None)
+        assert space.unwrap(encoded) == 'sp\xc3\xa4m'
+        raises(TypeError, api.PyUnicode_AsEncodedString,
+               space.wrap(''), None, None)
+        rffi.free_charp(utf_8)
+
     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/branch/cpython-extension/pypy/module/cpyext/unicodeobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/unicodeobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/unicodeobject.py	Thu Apr  8 19:14:18 2010
@@ -3,7 +3,9 @@
 from pypy.module.unicodedata import unicodedb_4_1_0 as unicodedb
 from pypy.module.cpyext.api import (CANNOT_FAIL, Py_ssize_t, PyUnicodeObject,
                                     build_type_checkers, cpython_api)
+from pypy.module.cpyext.pyerrors import PyErr_BadArgument
 from pypy.module.cpyext.pyobject import PyObject, from_ref
+from pypy.module.cpyext.stringobject import PyString_Check
 from pypy.module.sys.interp_encoding import setdefaultencoding
 from pypy.objspace.std import unicodeobject, unicodetype
 
@@ -128,3 +130,20 @@
     setdefaultencoding(space, w_encoding)
     default_encoding[0] = '\x00'
     return 0
+
+ at cpython_api([PyObject, rffi.CCHARP, rffi.CCHARP], PyObject)
+def PyUnicode_AsEncodedString(space, w_unicode, encoding, errors):
+    """Encode a Unicode object and return the result as Python string object.
+    encoding and errors have the same meaning as the parameters of the same name
+    in the Unicode encode() method. The codec to be used is looked up using
+    the Python codec registry. Return NULL if an exception was raised by the
+    codec."""
+    if not PyUnicode_Check(space, w_unicode):
+        PyErr_BadArgument()
+
+    w_encoding = w_errors = None
+    if encoding:
+        w_encoding = rffi.charp2str(encoding)
+    if errors:
+        w_errors = rffi.charp2str(encoding)
+    return unicodetype.encode_object(space, w_unicode, w_encoding, w_errors)



More information about the Pypy-commit mailing list