Author: Armin Rigo <[email protected]>
Branch:
Changeset: r1130:c42e0c485fc8
Date: 2013-02-08 10:10 +0100
http://bitbucket.org/cffi/cffi/changeset/c42e0c485fc8/
Log: Passing None as an ``item *`` argument to a function.
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1934,7 +1934,11 @@
ctitem = ctptr->ct_itemdescr;
/* XXX some code duplication, how to avoid it? */
- if (PyBytes_Check(init)) {
+ if (init == Py_None) {
+ *output_data = NULL;
+ return 0;
+ }
+ else if (PyBytes_Check(init)) {
/* from a string: just returning the string here is fine.
We assume that the C code won't modify the 'char *' data. */
if ((ctptr->ct_flags & CT_CAST_ANYTHING) ||
@@ -4683,7 +4687,9 @@
static int _testfunc23(char *p)
{
- return 1000 * p[0];
+ if (p)
+ return 1000 * p[0];
+ return -42;
}
static PyObject *b__testfunc(PyObject *self, PyObject *args)
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -1006,6 +1006,8 @@
f = cast(BFunc23, _testfunc(23))
res = f(b"foo")
assert res == 1000 * ord(b'f')
+ res = f(None)
+ assert res == -42
def test_call_function_23_bis():
# declaring the function as int(unsigned char*)
diff --git a/doc/source/index.rst b/doc/source/index.rst
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -1329,6 +1329,9 @@
you need to specify it in a list of length 1; for example, a ``struct
foo *`` argument might be passed as ``[[field1, field2...]]``.
+.. versionadded:: 0.6
+ You can also pass None to ``item *`` arguments (meaning NULL).
+
As an optimization, the CPython version of CFFI assumes that a function
with a ``char *`` argument to which you pass a Python string will not
actually modify the array of characters passed in, and so passes directly
diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -1512,3 +1512,19 @@
assert lib.sum3chars((b'\x10', b'\x20', b'\x30')) == b'\x60'
p = ffi.new("char[]", b'\x10\x20\x30')
assert lib.sum3chars(p) == b'\x60'
+
+def test_passing_None():
+ ffi = FFI()
+ ffi.cdef("int seeme1(char *); int seeme2(int *);")
+ lib = ffi.verify("""
+ int seeme1(char *x) {
+ return (x == NULL);
+ }
+ int seeme2(int *x) {
+ return (x == NULL);
+ }
+ """)
+ assert lib.seeme1("foo") == 0
+ assert lib.seeme1(None) == 1
+ assert lib.seeme2([42, 43]) == 0
+ assert lib.seeme2(None) == 1
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit