[pypy-commit] cffi cffi-1.0: ffi.cast() and fix (can't use any other bits of ml_flags)

arigo noreply at buildbot.pypy.org
Sat Apr 18 17:46:32 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1763:ef894ff04dea
Date: 2015-04-18 17:47 +0200
http://bitbucket.org/cffi/cffi/changeset/ef894ff04dea/

Log:	ffi.cast() and fix (can't use any other bits of ml_flags)

diff --git a/new/ffi_obj.c b/new/ffi_obj.c
--- a/new/ffi_obj.c
+++ b/new/ffi_obj.c
@@ -244,6 +244,11 @@
     return direct_newp(ct, init);
 }
 
+PyDoc_STRVAR(ffi_cast_doc,
+"Similar to a C cast: returns an instance of the named C\n"
+"type initialized with the given 'source'.  The source is\n"
+"casted between integers or pointers of any type.");
+
 static PyObject *ffi_cast(FFIObject *self, PyObject *args)
 {
     CTypeDescrObject *ct;
@@ -521,7 +526,7 @@
 #if 0
     {"addressof",     (PyCFunction)ffi_addressof, METH_VARARGS},
 #endif
-    {"cast",          (PyCFunction)ffi_cast,      METH_VARARGS},
+    {"cast",          (PyCFunction)ffi_cast,      METH_VARARGS, ffi_cast_doc},
 #if 0
     {"close_library", ffi_close_library,          METH_VARARGS | METH_STATIC},
     {"from_handle",   (PyCFunction)ffi_from_handle,METH_O},
diff --git a/new/lib_obj.c b/new/lib_obj.c
--- a/new/lib_obj.c
+++ b/new/lib_obj.c
@@ -15,8 +15,8 @@
     PyMethodDef md;
     int type_index;
 };
-
-#define METH_CPYEXTFUNC   0x40000000
+static const char cpyextfunc_doc[] =
+    "direct call to the C function of the same name";
 
 struct LibObject_s {
     PyObject_HEAD
@@ -29,22 +29,23 @@
 
 static PyObject *_cpyextfunc_type_index(PyObject *x)
 {
+    struct CPyExtFunc_s *exf;
     assert(PyErr_Occurred());
 
     if (!PyCFunction_Check(x))
         return NULL;
     if (!LibObject_Check(PyCFunction_GET_SELF(x)))
         return NULL;
-    if (!(PyCFunction_GET_FLAGS(x) & METH_CPYEXTFUNC))
+
+    exf = (struct CPyExtFunc_s *)(((PyCFunctionObject *)x) -> m_ml);
+    if (exf->md.ml_doc != cpyextfunc_doc)
         return NULL;
 
     PyErr_Clear();
 
     LibObject *lib = (LibObject *)PyCFunction_GET_SELF(x);
-    struct CPyExtFunc_s *exf;
     PyObject *tuple, *result;
 
-    exf = (struct CPyExtFunc_s *)(((PyCFunctionObject *)x) -> m_ml);
     tuple = _realize_c_type_or_func(lib->l_types_builder,
                                     lib->l_types_builder->ctx.types,
                                     exf->type_index);
@@ -112,9 +113,9 @@
         goto no_memory;
 
     xfunc->md.ml_meth = (PyCFunction)g->address;
-    xfunc->md.ml_flags = flags | METH_CPYEXTFUNC;
+    xfunc->md.ml_flags = flags;
     xfunc->md.ml_name = g->name;
-    /*xfunc->md.ml_doc = ... */
+    xfunc->md.ml_doc = cpyextfunc_doc;
     if (xfunc->md.ml_name == NULL)
         goto no_memory;
 


More information about the pypy-commit mailing list