diff options
-rw-r--r-- | c/_cffi_backend.c | 10 | ||||
-rw-r--r-- | c/test_c.py | 8 |
2 files changed, 16 insertions, 2 deletions
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c index c6004bb..6451a0c 100644 --- a/c/_cffi_backend.c +++ b/c/_cffi_backend.c @@ -1550,7 +1550,8 @@ convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init) /* for backward compatibility, accept "char *" as either source of target. This is not what C does, though, so emit a warning that will eventually turn into an - error. */ + error. The warning is turned off if both types are + pointers to single bytes. */ char *msg = (ct->ct_flags & CT_IS_VOIDCHAR_PTR ? "implicit cast to 'char *' from a different pointer type: " "will be forbidden in the future (check that the types " @@ -1560,7 +1561,12 @@ convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init) "will be forbidden in the future (check that the types " "are as you expect; use an explicit ffi.cast() if they " "are correct)"); - if (PyErr_WarnEx(PyExc_UserWarning, msg, 1)) + if ((ct->ct_flags & ctinit->ct_flags & CT_POINTER) && + ct->ct_itemdescr->ct_size == 1 && + ctinit->ct_itemdescr->ct_size == 1) { + /* no warning */ + } + else if (PyErr_WarnEx(PyExc_UserWarning, msg, 1)) return -1; } else { diff --git a/c/test_c.py b/c/test_c.py index 1dbec90..6814338 100644 --- a/c/test_c.py +++ b/c/test_c.py @@ -3918,9 +3918,11 @@ def test_char_pointer_conversion(): BCharP = new_pointer_type(new_primitive_type("char")) BIntP = new_pointer_type(new_primitive_type("int")) BVoidP = new_pointer_type(new_void_type()) + BUCharP = new_pointer_type(new_primitive_type("unsigned char")) z1 = cast(BCharP, 0) z2 = cast(BIntP, 0) z3 = cast(BVoidP, 0) + z4 = cast(BUCharP, 0) with warnings.catch_warnings(record=True) as w: newp(new_pointer_type(BIntP), z1) # warn assert len(w) == 1 @@ -3934,6 +3936,12 @@ def test_char_pointer_conversion(): assert len(w) == 2 newp(new_pointer_type(BIntP), z3) # fine assert len(w) == 2 + newp(new_pointer_type(BCharP), z4) # fine (ignore signedness here) + assert len(w) == 2 + newp(new_pointer_type(BUCharP), z1) # fine (ignore signedness here) + assert len(w) == 2 + newp(new_pointer_type(BUCharP), z3) # fine + assert len(w) == 2 # check that the warnings are associated with lines in this file assert w[1].lineno == w[0].lineno + 4 |