[pypy-commit] cffi default: verify() with _Bool
arigo
noreply at buildbot.pypy.org
Thu Sep 13 15:01:18 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r929:4903af961f97
Date: 2012-09-13 15:01 +0200
http://bitbucket.org/cffi/cffi/changeset/4903af961f97/
Log: verify() with _Bool
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -4435,6 +4435,16 @@
return PyFloat_AsDouble(obj);
}
+static _Bool _cffi_to_c__Bool(PyObject *obj)
+{
+ long tmp = PyCffiInt_AsLong(obj);
+ switch (tmp) {
+ case 0: return 0;
+ case 1: return 1;
+ default: return (_Bool)_convert_overflow(obj, "_Bool");
+ }
+}
+
static PyObject *_cffi_get_struct_layout(Py_ssize_t nums[])
{
PyObject *result;
@@ -4500,6 +4510,7 @@
0,
#endif
_cffi_to_c_long_double,
+ _cffi_to_c__Bool,
};
/************************************************************/
diff --git a/cffi/vengine_cpy.py b/cffi/vengine_cpy.py
--- a/cffi/vengine_cpy.py
+++ b/cffi/vengine_cpy.py
@@ -684,6 +684,7 @@
#define _cffi_from_c_unsigned_short PyInt_FromLong
#define _cffi_from_c_unsigned_long PyLong_FromUnsignedLong
#define _cffi_from_c_unsigned_long_long PyLong_FromUnsignedLongLong
+#define _cffi_from_c__Bool PyInt_FromLong
#if SIZEOF_INT < SIZEOF_LONG
# define _cffi_from_c_unsigned_int PyInt_FromLong
@@ -752,7 +753,9 @@
((PyObject *(*)(wchar_t))_cffi_exports[20])
#define _cffi_to_c_long_double \
((long double(*)(PyObject *))_cffi_exports[21])
-#define _CFFI_NUM_EXPORTS 22
+#define _cffi_to_c__Bool \
+ ((_Bool(*)(PyObject *))_cffi_exports[22])
+#define _CFFI_NUM_EXPORTS 23
#if SIZEOF_LONG < SIZEOF_LONG_LONG
# define _cffi_to_c_long_long PyLong_AsLongLong
diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -1061,3 +1061,15 @@
tdl_handle_t *rh;
} my_error_code_t;
""")
+
+def test_bool():
+ ffi = FFI()
+ ffi.cdef("_Bool foo(_Bool);")
+ lib = ffi.verify("""
+ int foo(int arg) {
+ return !arg;
+ }
+ """)
+ assert lib.foo(1) == 0
+ assert lib.foo(0) == 1
+ py.test.raises(OverflowError, lib.foo, 42)
More information about the pypy-commit
mailing list