[pypy-commit] creflect default: more fixes to addressof()

arigo noreply at buildbot.pypy.org
Fri Dec 5 19:09:23 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r175:6fc30b084312
Date: 2014-12-05 19:09 +0100
http://bitbucket.org/cffi/creflect/changeset/6fc30b084312/

Log:	more fixes to addressof()

diff --git a/zeffir/cfunc.c b/zeffir/cfunc.c
--- a/zeffir/cfunc.c
+++ b/zeffir/cfunc.c
@@ -11,6 +11,8 @@
 
 } ZefFuncSupportObject;
 
+#define ZefFuncSupport_Check(ob)  (Py_TYPE(ob) == &ZefFuncSupport_Type)
+
 static void zef_func_support_dealloc(ZefFuncSupportObject *zfs)
 {
     int i;
diff --git a/zeffir/ffi_obj.c b/zeffir/ffi_obj.c
--- a/zeffir/ffi_obj.c
+++ b/zeffir/ffi_obj.c
@@ -400,6 +400,7 @@
     }
     else if (ZefLib_Check(obj)) {
         PyObject *attr, *name;
+        char *reason;
 
         if (fieldname == NULL) {
             PyErr_SetString(PyExc_TypeError, "addressof(Lib, fieldname) "
@@ -409,17 +410,23 @@
         name = PyString_FromString(fieldname);
         if (name == NULL)
             return NULL;
-        attr = lib_findattr((ZefLibObject *)obj, name);
+        attr = lib_findattr((ZefLibObject *)obj, name, ZefError);
         Py_DECREF(name);
         if (attr == NULL)
             return NULL;
 
-        if (ZefGlobSupport_Check(attr))
+        if (ZefGlobSupport_Check(attr)) {
             return addressof_global_var((ZefGlobSupportObject *)attr);
+        }
 
-        PyErr_Format(PyExc_TypeError,
-                     "cannot take the address of constant '%s'",
-                     fieldname);
+        if (PyCFunction_Check(attr))
+            reason = "declare that function as a function pointer instead";
+        else
+            reason = "numeric constants don't have addresses";
+
+        PyErr_Format(PyExc_NotImplementedError,
+                     "cannot take the address of '%s' (%s)",
+                     fieldname, reason);
         return NULL;
     }
     else {
diff --git a/zeffir/lib_obj.c b/zeffir/lib_obj.c
--- a/zeffir/lib_obj.c
+++ b/zeffir/lib_obj.c
@@ -27,7 +27,7 @@
                              lib->l_dl_lib == NULL ? " (closed)" : "");
 }
 
-static PyObject *lib_findattr(ZefLibObject *lib, PyObject *name)
+static PyObject *lib_findattr(ZefLibObject *lib, PyObject *name, PyObject *exc)
 {
     /* does not return a new reference! */
 
@@ -38,7 +38,7 @@
 
     PyObject *x = PyDict_GetItem(lib->l_dict, name);
     if (x == NULL) {
-        PyErr_Format(PyExc_AttributeError,
+        PyErr_Format(exc,
                      "lib '%.200s' has no function,"
                      " global variable or constant '%.200s'",
                      lib->l_libname,
@@ -50,7 +50,7 @@
 
 static PyObject *lib_getattr(ZefLibObject *lib, PyObject *name)
 {
-    PyObject *x = lib_findattr(lib, name);
+    PyObject *x = lib_findattr(lib, name, PyExc_AttributeError);
     if (x == NULL)
         return NULL;
 
@@ -63,7 +63,7 @@
 
 static int lib_setattr(ZefLibObject *lib, PyObject *name, PyObject *val)
 {
-    PyObject *x = lib_findattr(lib, name);
+    PyObject *x = lib_findattr(lib, name, PyExc_AttributeError);
     if (x == NULL)
         return -1;
 
diff --git a/zeffir/test/test_basic.py b/zeffir/test/test_basic.py
--- a/zeffir/test/test_basic.py
+++ b/zeffir/test/test_basic.py
@@ -44,3 +44,7 @@
     for i in range(50):
         p = ffi.new("char[]", "12345678" * i)
         assert ffi.string(p) == "12345678" * i
+
+def test_addressof_missing_glob():
+    ffi, lib = support.compile_and_open('basic')
+    py.test.raises(ffi.error, ffi.addressof, lib, "some_missing_name")
diff --git a/zeffir/test/test_function.py b/zeffir/test/test_function.py
--- a/zeffir/test/test_function.py
+++ b/zeffir/test/test_function.py
@@ -26,3 +26,7 @@
     p = ffi.new("char[]", 10)
     lib.returning_nothing(p)
     assert ffi.string(p) == "OK"
+
+def test_addressof_function():
+    ffi, lib = support.compile_and_open('function')
+    py.test.raises(NotImplementedError, ffi.addressof, lib, 'simple_function')
diff --git a/zeffir/zeffir.c b/zeffir/zeffir.c
--- a/zeffir/zeffir.c
+++ b/zeffir/zeffir.c
@@ -15,8 +15,8 @@
 #include "cdata.c"
 #include "cglob.c"
 #include "lib_obj.c"
+#include "cfunc.c"
 #include "ffi_obj.c"
-#include "cfunc.c"
 #include "builder.c"
 #include "../creflect/creflect_cdecl.c"
 


More information about the pypy-commit mailing list