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

trundle at codespeak.net trundle at codespeak.net
Wed Apr 7 01:55:29 CEST 2010


Author: trundle
Date: Wed Apr  7 01:55:27 2010
New Revision: 73484

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/api.py
   pypy/branch/cpython-extension/pypy/module/cpyext/include/unicodeobject.h
   pypy/branch/cpython-extension/pypy/module/cpyext/pyobject.py
   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_AS_UNICODE and PyUnicode_AsUnicode.


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/api.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/api.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/api.py	Wed Apr  7 01:55:27 2010
@@ -290,7 +290,7 @@
 PyUnicodeObjectStruct = lltype.ForwardReference()
 PyUnicodeObject = lltype.Ptr(PyUnicodeObjectStruct)
 PyUnicodeObjectFields = (PyObjectFields +
-    (("buffer", rffi.VOIDP), ("size", Py_ssize_t)))
+    (("buffer", rffi.CWCHARP), ("size", Py_ssize_t)))
 cpython_struct("PyUnicodeObject", PyUnicodeObjectFields, PyUnicodeObjectStruct)
 
 VA_TP_LIST = {}

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/include/unicodeobject.h
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/include/unicodeobject.h	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/include/unicodeobject.h	Wed Apr  7 01:55:27 2010
@@ -6,13 +6,6 @@
 #endif
 
 
-typedef struct {
-    PyObject_HEAD
-    void *buffer;
-    Py_ssize_t size;
-} PyUnicodeObject;
-
-
 typedef unsigned int Py_UCS4;
 #ifdef HAVE_USABLE_WCHAR_T
 #define PY_UNICODE_TYPE wchar_t
@@ -23,7 +16,14 @@
 #endif
 typedef PY_UNICODE_TYPE Py_UNICODE;
 
-  
+
+typedef struct {
+    PyObject_HEAD
+    Py_UNICODE *buffer;
+    Py_ssize_t size;
+} PyUnicodeObject;
+
+
 #ifdef __cplusplus
 }
 #endif

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/pyobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/pyobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/pyobject.py	Wed Apr  7 01:55:27 2010
@@ -77,7 +77,7 @@
         elif isinstance(w_obj, W_UnicodeObject):
             py_obj_unicode = lltype.malloc(PyUnicodeObject.TO, flavor='raw', zero=True)
             py_obj_unicode.c_size = len(space.unicode_w(w_obj))
-            py_obj_unicode.c_buffer = lltype.nullptr(rffi.VOIDP.TO)
+            py_obj_unicode.c_buffer = lltype.nullptr(rffi.CWCHARP.TO)
             pto = make_ref(space, space.w_unicode)
             py_obj = rffi.cast(PyObject, py_obj_unicode)
             py_obj.c_ob_refcnt = 1

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	Wed Apr  7 01:55:27 2010
@@ -5653,12 +5653,6 @@
     """
     raise NotImplementedError
 
- at cpython_api([PyObject], {Py_UNICODE*})
-def PyUnicode_AS_UNICODE(space, o):
-    """Return a pointer to the internal Py_UNICODE buffer of the object.  o
-    has to be a PyUnicodeObject (not checked)."""
-    raise NotImplementedError
-
 @cpython_api([], rffi.INT_real)
 def PyUnicode_ClearFreeList(space, ):
     """Clear the free list. Return the total number of freed items.
@@ -5726,12 +5720,6 @@
     changes in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
- at cpython_api([PyObject], {Py_UNICODE*})
-def PyUnicode_AsUnicode(space, unicode):
-    """Return a read-only pointer to the Unicode object's internal Py_UNICODE
-    buffer, NULL if unicode is not a Unicode object."""
-    raise NotImplementedError
-
 @cpython_api([PyObject], Py_ssize_t)
 def PyUnicode_GetSize(space, unicode):
     """Return the length of the Unicode object.

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	Wed Apr  7 01:55:27 2010
@@ -9,11 +9,17 @@
         unichar = rffi.sizeof(Py_UNICODE)
         assert api.PyUnicode_GET_DATA_SIZE(space.wrap(u'späm')) == 4 * unichar
 
-    def test_AS_DATA(self, space, api):
+    def test_AS(self, space, api):
         word = space.wrap(u'spam')
         array = rffi.cast(rffi.CWCHARP, api.PyUnicode_AS_DATA(word))
+        array2 = api.PyUnicode_AS_UNICODE(word)
+        array3 = api.PyUnicode_AsUnicode(word)
         for (i, char) in enumerate(space.unwrap(word)):
             assert array[i] == char
+            assert array2[i] == char
+            assert array3[i] == char
+        raises(TypeError, api.PyUnicode_AsUnicode(space.wrap('spam')))
+        api.PyErr_Clear()
 
     def test_IS(self, space, api):
         for char in [0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x1c, 0x1d, 0x1e, 0x1f,

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	Wed Apr  7 01:55:27 2010
@@ -1,3 +1,4 @@
+from pypy.interpreter.error import OperationError
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.unicodedata import unicodedb_4_1_0 as unicodedb
 from pypy.module.cpyext.api import (CANNOT_FAIL, Py_ssize_t, PyUnicodeObject,
@@ -58,13 +59,7 @@
 def PyUnicode_AS_DATA(space, ref):
     """Return a pointer to the internal buffer of the object. o has to be a
     PyUnicodeObject (not checked)."""
-    ref_unicode = rffi.cast(PyUnicodeObject, ref)
-    if not ref_unicode.c_buffer:
-        # Copy unicode buffer
-        w_unicode = from_ref(space, ref)
-        u = space.unicode_w(w_unicode)
-        ref_unicode.c_buffer = rffi.cast(rffi.VOIDP, rffi.unicode2wcharp(u))
-    return rffi.cast(rffi.CCHARP, ref_unicode.c_buffer)
+    return rffi.cast(rffi.CCHARP, PyUnicode_AS_UNICODE(space, ref))
 
 @cpython_api([PyObject], Py_ssize_t, error=CANNOT_FAIL)
 def PyUnicode_GET_DATA_SIZE(space, w_obj):
@@ -81,3 +76,24 @@
     in your code for properly supporting 64-bit systems."""
     assert isinstance(w_obj, unicodeobject.W_UnicodeObject)
     return space.int_w(space.len(w_obj))
+
+ at cpython_api([PyObject], rffi.CWCHARP, error=CANNOT_FAIL)
+def PyUnicode_AS_UNICODE(space, ref):
+    """Return a pointer to the internal Py_UNICODE buffer of the object.  ref
+    has to be a PyUnicodeObject (not checked)."""
+    ref_unicode = rffi.cast(PyUnicodeObject, ref)
+    if not ref_unicode.c_buffer:
+        # Copy unicode buffer
+        w_unicode = from_ref(space, ref)
+        u = space.unicode_w(w_unicode)
+        ref_unicode.c_buffer = rffi.unicode2wcharp(u)
+    return ref_unicode.c_buffer
+
+ at cpython_api([PyObject], rffi.CWCHARP, error=0)
+def PyUnicode_AsUnicode(space, ref):
+    """Return a read-only pointer to the Unicode object's internal Py_UNICODE
+    buffer, NULL if unicode is not a Unicode object."""
+    if not PyUnicode_Check(space, ref):
+        raise OperationError(space.w_TypeError,
+                             space.wrap("expected unicode object"))
+    return PyUnicode_AS_UNICODE(space, ref)



More information about the Pypy-commit mailing list