Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r1746:182cb9f15913
Date: 2015-04-18 11:38 +0200
http://bitbucket.org/cffi/cffi/changeset/182cb9f15913/

Log:    Prebuilt FFI.NULL

diff --git a/new/cffi1_module.c b/new/cffi1_module.c
--- a/new/cffi1_module.c
+++ b/new/cffi1_module.c
@@ -20,6 +20,8 @@
         return -1;
     if (!PyType_Ready(&Lib_Type) < 0)
         return -1;
+    if (init_global_types_dict(FFI_Type.tp_dict) < 0)
+        return -1;
 
     FFIError = PyErr_NewException("ffi.error", NULL, NULL);
     if (FFIError == NULL)
diff --git a/new/realize_c_type.c b/new/realize_c_type.c
--- a/new/realize_c_type.c
+++ b/new/realize_c_type.c
@@ -6,13 +6,42 @@
 
 
 static PyObject *all_primitives[_CFFI__NUM_PRIM];
+static PyObject *global_types_dict;
 
-static PyObject *fetch_global_types_dict(void)
+static PyObject *build_primitive_type(int num);   /* forward */
+
+static int init_global_types_dict(PyObject *ffi_type_dict)
 {
-    static PyObject *result = NULL;
-    if (!result)
-        result = PyDict_New();
-    return result;
+    int err;
+    PyObject *ct, *ct2, *pnull;
+
+    global_types_dict = PyDict_New();
+    if (global_types_dict == NULL)
+        return -1;
+
+    ct = build_primitive_type(_CFFI_PRIM_VOID);         // 'void'
+    if (ct == NULL)
+        return -1;
+    if (PyDict_SetItemString(global_types_dict,
+                             ((CTypeDescrObject *)ct)->ct_name, ct) < 0) {
+        return -1;
+    }
+    ct2 = new_pointer_type((CTypeDescrObject *)ct);     // 'void *'
+    if (ct2 == NULL)
+        return -1;
+    if (PyDict_SetItemString(global_types_dict,
+                             ((CTypeDescrObject *)ct2)->ct_name, ct2) < 0) {
+        Py_DECREF(ct2);
+        return -1;
+    }
+
+    pnull = new_simple_cdata(NULL, (CTypeDescrObject *)ct2);
+    Py_DECREF(ct2);
+    if (pnull == NULL)
+        return -1;
+    err = PyDict_SetItemString(ffi_type_dict, "NULL", pnull);
+    Py_DECREF(pnull);
+    return err;
 }
 
 static void free_builder_c(builder_c_t *builder)
@@ -83,11 +112,7 @@
            ffi instances.  Look it up and possibly add it to the global
            types dict.
         */
-        PyObject *gdict = fetch_global_types_dict();
-        if (gdict == NULL)
-            goto no_memory;
-
-        y = PyDict_GetItem(gdict, name);
+        y = PyDict_GetItem(global_types_dict, name);
         if (y != NULL) {
             Py_INCREF(y);
             Py_DECREF(x);
@@ -95,7 +120,7 @@
         }
         else {
             /* Not found in the global dictionary.  Put it there. */
-            if (PyDict_SetItem(gdict, name, x) < 0)
+            if (PyDict_SetItem(global_types_dict, name, x) < 0)
                 goto no_memory;
         }
     }
diff --git a/new/test_ffi_obj.py b/new/test_ffi_obj.py
--- a/new/test_ffi_obj.py
+++ b/new/test_ffi_obj.py
@@ -50,3 +50,7 @@
             if isinstance(method, check_type):
                 assert method.__doc__, "method FFI.%s() has no docstring" % (
                     methname,)
+
+def test_ffi_NULL():
+    NULL = _cffi1_backend.FFI.NULL
+    assert _cffi1_backend.FFI().typeof(NULL).cname == "void *"
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to