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

dan at codespeak.net dan at codespeak.net
Tue Jun 15 08:46:50 CEST 2010


Author: dan
Date: Tue Jun 15 08:46:48 2010
New Revision: 75402

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:
Added PyUnicodeAsASCIIString() and test.

Modified: pypy/trunk/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubs.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubs.py	Tue Jun 15 08:46:48 2010
@@ -3036,13 +3036,6 @@
     changes in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
- at cpython_api([PyObject], PyObject)
-def PyUnicode_AsASCIIString(space, unicode):
-    """Encode a Unicode object using ASCII and return the result as Python string
-    object.  Error handling is "strict".  Return NULL if an exception was raised
-    by the codec."""
-    raise NotImplementedError
-
 @cpython_api([rffi.CCHARP, Py_ssize_t, PyObject, rffi.CCHARP], PyObject)
 def PyUnicode_DecodeCharmap(space, s, size, mapping, errors):
     """Create a Unicode object by decoding size bytes of the encoded string s using

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	Tue Jun 15 08:46:48 2010
@@ -161,3 +161,15 @@
         test('\\u0105\\u0107\\u017c\\u017a')
         test('El Ni\\xf1o')
 
+    def test_ascii(self, space, api):
+        ustr = "abcdef"
+        w_ustr = space.wrap(ustr.decode("ascii"))
+        result = api.PyUnicode_AsASCIIString(w_ustr)
+        
+        assert space.eq_w(space.wrap(ustr), result)
+
+        w_ustr = space.wrap(u"abcd\xe9f")
+        result = api.PyUnicode_AsASCIIString(w_ustr)
+        assert result is None
+
+

Modified: pypy/trunk/pypy/module/cpyext/unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/unicodeobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/unicodeobject.py	Tue Jun 15 08:46:48 2010
@@ -307,6 +307,19 @@
         w_errors = space.w_None
     return space.call_method(w_str, 'decode', space.wrap("utf-8"), w_errors)
 
+ at cpython_api([PyObject], PyObject)
+def PyUnicode_AsASCIIString(space, w_unicode):
+    """Encode a Unicode object using ASCII and return the result as Python string
+    object.  Error handling is "strict".  Return NULL if an exception was raised
+    by the codec."""
+    try:
+        return space.call_method(w_unicode, 'encode', space.wrap('ascii')) #space.w_None for errors?
+    except OperationError, e:
+        if e.match(space, space.w_UnicodeEncodeError):
+            return None
+        else:
+            raise
+
 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