1. Python CFFI
  2. Untitled project
  3. cffi

Commits

Armin Rigo  committed 26390b0

Test and fix for issue #99.

  • Participants
  • Parent commits 3a53adb
  • Branches default

Comments (0)

Files changed (2)

File c/_cffi_backend.c

View file
         }
     }
 
+    /* A fast path for <char[]>[0:N] = b"somestring", which also adds
+       support for Python 3: otherwise, you get integers while enumerating
+       the string, and you can't set them to characters :-/
+    */
+    if (PyBytes_Check(v) && (ct->ct_flags & CT_PRIMITIVE_CHAR)
+            && itemsize == sizeof(char)) {
+        if (PyBytes_GET_SIZE(v) != length) {
+            PyErr_Format(PyExc_ValueError,
+                         "need a string of length %zd, got %zd",
+                         length, PyBytes_GET_SIZE(v));
+            return -1;
+        }
+        memcpy(cdata, PyBytes_AS_STRING(v), length);
+        return 0;
+    }
+
     it = PyObject_GetIter(v);
     if (it == NULL)
         return -1;

File c/test_c.py

View file
         assert p.x[5] == 60
         assert p.x[6] == 70
 
+def test_ass_slice():
+    BChar = new_primitive_type("char")
+    BArray = new_array_type(new_pointer_type(BChar), None)
+    p = newp(BArray, b"foobar")
+    p[2:5] = [b"*", b"Z", b"T"]
+    p[1:3] = b"XY"
+    assert list(p) == [b"f", b"X", b"Y", b"Z", b"T", b"r", b"\x00"]
+    py.test.raises(TypeError, "p[1:5] = u+'XYZT'")
+    py.test.raises(TypeError, "p[1:5] = [1, 2, 3, 4]")
+    #
+    BUniChar = new_primitive_type("wchar_t")
+    BArray = new_array_type(new_pointer_type(BUniChar), None)
+    p = newp(BArray, u+"foobar")
+    p[2:5] = [u+"*", u+"Z", u+"T"]
+    p[1:3] = u+"XY"
+    assert list(p) == [u+"f", u+"X", u+"Y", u+"Z", u+"T", u+"r", u+"\x00"]
+    py.test.raises(TypeError, "p[1:5] = b'XYZT'")
+    py.test.raises(TypeError, "p[1:5] = [1, 2, 3, 4]")
+
 
 def test_version():
     # this test is here mostly for PyPy