Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r3035:a60ae346a78f
Date: 2017-09-30 08:39 +0200
http://bitbucket.org/cffi/cffi/changeset/a60ae346a78f/

Log:    Turn off the UserWarning when implicitly trying to convert between
        "char *" and a pointer to a numeric single-char type.

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1550,7 +1550,8 @@
                 /* 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 @@
                     "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
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -3918,9 +3918,11 @@
     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 @@
         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
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to