Author: Armin Rigo <[email protected]>
Branch:
Changeset: r1394:26390b01617e
Date: 2013-11-08 21:47 +0100
http://bitbucket.org/cffi/cffi/changeset/26390b01617e/
Log: Test and fix for issue #99.
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1939,6 +1939,22 @@
}
}
+ /* 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;
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -3089,6 +3089,25 @@
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
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit