Commits

Armin Rigo committed f60f68a

Accept strings to initialize signed/unsigned char arrays.

Comments (0)

Files changed (2)

c/_cffi_backend.c

         }
         return 0;
     }
-    else if (ctitem->ct_flags & CT_PRIMITIVE_CHAR) {
+    else if ((ctitem->ct_flags & CT_PRIMITIVE_CHAR) ||
+             ((ctitem->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED))
+              && (ctitem->ct_size == sizeof(char)))) {
         if (ctitem->ct_size == sizeof(char)) {
             char *srcdata;
             Py_ssize_t n;
     #
     d = rawaddressof(BCharP, s, 1)
     assert d == cast(BCharP, p) + 1
+
+def test_newp_signed_unsigned_char():
+    BCharArray = new_array_type(
+        new_pointer_type(new_primitive_type("char")), None)
+    p = newp(BCharArray, b"foo")
+    assert len(p) == 4
+    assert list(p) == [b"f", b"o", b"o", b"\x00"]
+    #
+    BUCharArray = new_array_type(
+        new_pointer_type(new_primitive_type("unsigned char")), None)
+    p = newp(BUCharArray, b"fo\xff")
+    assert len(p) == 4
+    assert list(p) == [ord("f"), ord("o"), 0xff, 0]
+    #
+    BSCharArray = new_array_type(
+        new_pointer_type(new_primitive_type("signed char")), None)
+    p = newp(BSCharArray, b"fo\xff")
+    assert len(p) == 4
+    assert list(p) == [ord("f"), ord("o"), -1, 0]