[pypy-commit] creflect default: clean-ups, small stuff

arigo noreply at buildbot.pypy.org
Thu Dec 4 13:18:14 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r144:aab3d6122236
Date: 2014-12-04 12:15 +0100
http://bitbucket.org/cffi/creflect/changeset/aab3d6122236/

Log:	clean-ups, small stuff

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,6 @@
 setup(
     ext_modules=[
         Extension(name = 'zeffir',
-                  sources=['zeffir/zeffir.c',
-                           'creflect/creflect_cdecl.c'],
+                  sources=['zeffir/zeffir.c']
         )
     ])
diff --git a/zeffir/builder.c b/zeffir/builder.c
--- a/zeffir/builder.c
+++ b/zeffir/builder.c
@@ -99,7 +99,7 @@
 
 static _crx_type_t *zef_get_void_type(_crx_builder_t *cb)
 {
-    return _zef_primitive(cb, -1, "void", CT_VOID | CT_IS_OPAQUE);
+    return _zef_primitive(cb, -1, "void", CT_VOID);
 }
 
 static _crx_type_t *zef_get_char_type(_crx_builder_t *cb)
@@ -285,7 +285,7 @@
 
 static _crx_type_t *zef_get_unknown_type(_crx_builder_t *cb, const char *name)
 {
-    abort();
+    return _zef_primitive(cb, -1, name, CT_UNKNOWN);
 }
 
 static void zef_complete(_crx_builder_t *cb, _crx_type_t *t,
diff --git a/zeffir/ctype.c b/zeffir/ctype.c
--- a/zeffir/ctype.c
+++ b/zeffir/ctype.c
@@ -8,13 +8,13 @@
 #define CT_ARRAY             32    /* array */
 #define CT_STRUCT            64    /* struct */
 #define CT_UNION            128    /* union */
-#define CT_FUNCTIONPTR      256    /* pointer to function */
+#define CT_UNKNOWN          256    /* unknown type */
 #define CT_VOID             512    /* void */
 
 /* other flags that may also be set in addition to the base flag: */
 #define CT_CAST_ANYTHING         1024    /* 'char *' and 'void *' only */
 #define CT_PRIMITIVE_FITS_LONG   2048
-#define CT_IS_OPAQUE             4096
+//#define CT_IS_OPAQUE           4096    /* == (ct_size < 0) */
 #define CT_IS_ENUM               8192
 #define CT_IS_PTR_TO_OWNED      16384
 #define CT_IS_LONGDOUBLE        65536
diff --git a/zeffir/ffi_obj.c b/zeffir/ffi_obj.c
--- a/zeffir/ffi_obj.c
+++ b/zeffir/ffi_obj.c
@@ -86,6 +86,8 @@
 
     lib = lib_create(path);
     Py_DECREF(path);
+    if (lib == NULL)
+        return NULL;
 
     if (load_creflect_main(self, lib) < 0) {
         Py_DECREF(lib);
@@ -108,22 +110,53 @@
     return Py_None;
 }
 
+static CTypeDescrObject *_ffi_type(ZefFFIObject *ffi, PyObject *arg)
+{
+    /* Returns the CTypeDescrObject from the user-supplied 'arg'.
+       Does not return a new reference!
+    */
+    if (PyString_Check(arg)) {
+        PyObject *x = PyDict_GetItem(ffi->types_dict, arg);
+        if (x != NULL && CTypeDescr_Check(x))
+            return (CTypeDescrObject *)x;
+
+        CTypeDescrObject *ct = parse_c_decl(ffi, PyString_AS_STRING(arg));
+        if (ct == NULL)
+            return NULL;
+
+        x = (PyObject *)ct;
+        if (PyDict_SetItem(ffi->types_dict, arg, x) < 0)
+            ct = NULL;
+        Py_DECREF(x);
+        return ct;
+    }
+    else if (CTypeDescr_Check(arg)) {
+        return (CTypeDescrObject *)arg;
+    }
+    else {
+        PyErr_SetString(PyExc_TypeError, "expected a string or a CType object");
+        return NULL;
+    }
+}
+
+static PyObject *ffi_sizeof(ZefFFIObject *self, PyObject *arg)
+{
+    CTypeDescrObject *ct = _ffi_type(self, arg);
+    if (ct == NULL)
+        return NULL;
+
+    return PyInt_FromSsize_t(ct->ct_size);
+}
+
 static PyObject *ffi_typeof(ZefFFIObject *self, PyObject *arg)
 {
     if (!PyString_Check(arg)) {
-        PyErr_SetString(PyExc_TypeError, "XXX");
+        PyErr_SetString(PyExc_TypeError, "expected a string");
         return NULL;
     }
 
-    PyObject *x = PyDict_GetItem(self->types_dict, arg);
-    if (x != NULL) {
-        Py_INCREF(x);
-        return x;
-    }
-
-    x = (PyObject *)parse_c_decl(self, PyString_AS_STRING(arg));
-    if (x != NULL)
-        PyDict_SetItem(self->types_dict, arg, x);
+    PyObject *x = (PyObject *)_ffi_type(self, arg);
+    Py_XINCREF(x);
     return x;
 }
 
@@ -131,6 +164,7 @@
     {"close_library", ffi_close_library,         METH_VARARGS | METH_STATIC},
     {"load_library",  (PyCFunction)ffi_load_library,
                                                  METH_VARARGS | METH_KEYWORDS},
+    {"sizeof",        (PyCFunction)ffi_sizeof,   METH_O},
     {"typeof",        (PyCFunction)ffi_typeof,   METH_O},
     {NULL}
 };
diff --git a/zeffir/test/function.crx b/zeffir/test/function.crx
--- a/zeffir/test/function.crx
+++ b/zeffir/test/function.crx
@@ -1,11 +1,11 @@
+int simple_function(int x)
+{
+    return x + 1;
+}
+
+
 // CREFLECT: start
 
 int simple_function(int);
 
 // CREFLECT: end
-
-
-int simple_function(int x)
-{
-    return x + 1;
-}
diff --git a/zeffir/test/test_ctype.py b/zeffir/test/test_ctype.py
--- a/zeffir/test/test_ctype.py
+++ b/zeffir/test/test_ctype.py
@@ -10,6 +10,8 @@
 def test_typeof_char():
     ffi = support.new_ffi()
     assert repr(ffi.typeof("char")) == "<ctype 'char'>"
+    assert ffi.sizeof("char") == 1
+    assert ffi.sizeof(ffi.typeof("char")) == 1
 
 def test_typeof_bool():
     ffi = support.new_ffi()


More information about the pypy-commit mailing list