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

afa at codespeak.net afa at codespeak.net
Thu Apr 22 14:07:08 CEST 2010


Author: afa
Date: Thu Apr 22 14:07:06 2010
New Revision: 73968

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/stringobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_stringobject.py
Log:
PyString_AsStringAndSize


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stringobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stringobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stringobject.py	Thu Apr 22 14:07:06 2010
@@ -84,6 +84,29 @@
         ref_str.c_buffer = rffi.str2charp(s)
     return ref_str.c_buffer
 
+ at cpython_api([PyObject, rffi.CCHARPP, rffi.CArrayPtr(Py_ssize_t)], rffi.INT_real, error=-1)
+def PyString_AsStringAndSize(space, ref, buffer, length):
+    if not PyString_Check(space, ref):
+        raise OperationError(space.w_TypeError, space.wrap(
+            "PyString_AsStringAndSize only support strings"))
+    ref_str = rffi.cast(PyStringObject, ref)
+    if not ref_str.c_buffer:
+        # copy string buffer
+        w_str = from_ref(space, ref)
+        s = space.str_w(w_str)
+        ref_str.c_buffer = rffi.str2charp(s)
+    buffer[0] = ref_str.c_buffer
+    if length:
+        length[0] = ref_str.c_size
+    else:
+        i = 0
+        while ref_str.c_buffer[i] != '\0':
+            i += 1
+        if i != ref_str.c_size:
+            raise OperationError(space.w_TypeError, space.wrap(
+                "expected string without null bytes"))
+    return 0
+
 @cpython_api([PyObject], Py_ssize_t, error=-1)
 def PyString_Size(space, ref):
     if from_ref(space, rffi.cast(PyObject, ref.c_ob_type)) is space.w_str:

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_stringobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_stringobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_stringobject.py	Thu Apr 22 14:07:06 2010
@@ -105,6 +105,31 @@
             )])
         assert module.string_as_string("huheduwe") == "huhe"
 
+    def test_AsStringAndSize(self):
+        module = self.import_extension('foo', [
+            ("getstring", "METH_NOARGS",
+             """
+                 PyObject* s1 = PyString_FromStringAndSize("te\\0st", 5);
+                 char *buf;
+                 Py_ssize_t len;
+                 if (PyString_AsStringAndSize(s1, &buf, &len) < 0)
+                     return NULL;
+                 if (len != 5) {
+                     PyErr_SetString(PyExc_AssertionError, "Bad Length");
+                     return NULL;
+                 }
+                 if (PyString_AsStringAndSize(s1, &buf, NULL) >= 0) {
+                     PyErr_SetString(PyExc_AssertionError, "Should Have failed");
+                     return NULL;
+                 }
+                 PyErr_Clear();
+                 Py_DECREF(s1);
+                 Py_INCREF(Py_None);
+                 return Py_None;
+             """),
+            ])
+        module.getstring()
+
     def test_format_v(self):
         module = self.import_extension('foo', [
             ("test_string_format_v", "METH_VARARGS",



More information about the Pypy-commit mailing list