[pypy-commit] cffi cffi-1.0: ffi.new()

arigo noreply at buildbot.pypy.org
Wed Apr 15 14:40:42 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1711:b4ccd7b6cbad
Date: 2015-04-15 14:41 +0200
http://bitbucket.org/cffi/cffi/changeset/b4ccd7b6cbad/

Log:	ffi.new()

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -2796,14 +2796,11 @@
     return (PyObject *)cd;
 }
 
-static PyObject *b_newp(PyObject *self, PyObject *args)
-{
-    CTypeDescrObject *ct, *ctitem;
+static PyObject *direct_newp(CTypeDescrObject *ct, PyObject *init)
+{
+    CTypeDescrObject *ctitem;
     CDataObject *cd;
-    PyObject *init = Py_None;
     Py_ssize_t dataoffset, datasize, explicitlength;
-    if (!PyArg_ParseTuple(args, "O!|O:newp", &CTypeDescr_Type, &ct, &init))
-        return NULL;
 
     explicitlength = -1;
     if (ct->ct_flags & CT_POINTER) {
@@ -2894,6 +2891,15 @@
     return (PyObject *)cd;
 }
 
+static PyObject *b_newp(PyObject *self, PyObject *args)
+{
+    CTypeDescrObject *ct;
+    PyObject *init = Py_None;
+    if (!PyArg_ParseTuple(args, "O!|O:newp", &CTypeDescr_Type, &ct, &init))
+        return NULL;
+    return direct_newp(ct, init);
+}
+
 static int
 _my_PyObject_AsBool(PyObject *ob)
 {
diff --git a/new/ffi_obj.c b/new/ffi_obj.c
--- a/new/ffi_obj.c
+++ b/new/ffi_obj.c
@@ -164,13 +164,10 @@
     return x;
 }
 
-#if 0
-static PyObject *ffi_new(ZefFFIObject *self, PyObject *args)
+static PyObject *ffi_new(FFIObject *self, PyObject *args)
 {
-    CTypeDescrObject *ct, *ctitem;
-    CDataObject *cd;
+    CTypeDescrObject *ct;
     PyObject *arg, *init = Py_None;
-    Py_ssize_t dataoffset, datasize, explicitlength;
     if (!PyArg_ParseTuple(args, "O|O:new", &arg, &init))
         return NULL;
 
@@ -178,76 +175,10 @@
     if (ct == NULL)
         return NULL;
 
-    explicitlength = -1;
-    if (ct->ct_flags & (CT_POINTER | CT_STRUCT | CT_UNION)) {
-        dataoffset = offsetof(CDataObject_own_nolength, alignment);
-        ctitem = (ct->ct_flags & CT_POINTER) ? ct->ct_itemdescr : ct;
-        datasize = ctitem->ct_size;
-        if (datasize < 0) {
-            PyErr_Format(PyExc_TypeError,
-                         "cannot instantiate ctype '%s' of unknown size",
-                         ctitem->ct_name);
-            return NULL;
-        }
-        if (ctitem->ct_flags & CT_PRIMITIVE_CHAR)
-            datasize *= 2;   /* forcefully add another character: a null */
-
-        if ((ctitem->ct_flags & CT_WITH_VAR_ARRAY) && init != Py_None) {
-            Py_ssize_t optvarsize = datasize;
-            if (convert_struct_from_object(NULL,ctitem, init, &optvarsize) < 0)
-                return NULL;
-            datasize = optvarsize;
-        }
-    }
-    else if (ct->ct_flags & CT_ARRAY) {
-        dataoffset = offsetof(CDataObject_own_nolength, alignment);
-        datasize = ct->ct_size;
-        if (datasize < 0) {
-            explicitlength = get_new_array_length(&init);
-            if (explicitlength < 0)
-                return NULL;
-            ctitem = ct->ct_itemdescr;
-            dataoffset = offsetof(CDataObject_own_length, alignment);
-            datasize = explicitlength * ctitem->ct_size;
-            if (explicitlength > 0 &&
-                    (datasize / explicitlength) != ctitem->ct_size) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "array size would overflow a Py_ssize_t");
-                return NULL;
-            }
-        }
-    }
-    else if (ct->ct_flags & CT_PRIMITIVE_ANY) {
-        cd = _new_casted_primitive(ct);
-        datasize = ct->ct_size;
-        goto initialize_casted_primitive;
-    }
-    else {
-        PyErr_Format(PyExc_TypeError,
-                     "cannot create cdata '%s' objects", ct->ct_name);
-        return NULL;
-    }
-
-    cd = allocate_owning_object(dataoffset + datasize, ct);
-    if (cd == NULL)
-        return NULL;
-
-    cd->c_data = ((char *)cd) + dataoffset;
-    if (explicitlength >= 0)
-        ((CDataObject_own_length*)cd)->length = explicitlength;
-
- initialize_casted_primitive:
-    memset(cd->c_data, 0, datasize);
-    if (init != Py_None) {
-        if (convert_from_object(cd->c_data,
-              (ct->ct_flags & CT_POINTER) ? ct->ct_itemdescr : ct, init) < 0) {
-            Py_DECREF(cd);
-            return NULL;
-        }
-    }
-    return (PyObject *)cd;
+    return direct_newp(ct, init);
 }
 
+#if 0
 static PyObject *ffi_cast(ZefFFIObject *self, PyObject *args)
 {
     CTypeDescrObject *ct;
@@ -555,7 +486,9 @@
     {"getctype",      (PyCFunction)ffi_getctype,  METH_VARARGS},
     {"load_library",  (PyCFunction)ffi_load_library,METH_VARARGS|METH_KEYWORDS},
     {"offsetof",      (PyCFunction)ffi_offsetof,  METH_VARARGS},
+#endif
     {"new",           (PyCFunction)ffi_new,       METH_VARARGS},
+#if 0
     {"new_handle",    (PyCFunction)ffi_new_handle,METH_O},
 #endif
     {"sizeof",        (PyCFunction)ffi_sizeof,    METH_O},
diff --git a/new/test_ffi_obj.py b/new/test_ffi_obj.py
new file mode 100644
--- /dev/null
+++ b/new/test_ffi_obj.py
@@ -0,0 +1,7 @@
+import _cffi1_backend
+
+def test_ffi_new():
+    ffi = _cffi1_backend.FFI()
+    p = ffi.new("int *")
+    p[0] = -42
+    assert p[0] == -42


More information about the pypy-commit mailing list