Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/ctypes/test/test_struct_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class Y(X):
Y._fields_ = []
self.assertRaises(AttributeError, setattr, X, "_fields_", [])

def test_5(self):
class X(Structure):
_fields_ = (("char", c_char * 5),)

x = X(b'#' * 5)
x.char = b'a\0b\0'
self.assertEqual(bytes(x), b'a\x00###')

# __set__ and __get__ should raise a TypeError in case their self
# argument is not a ctype instance.
def test___set__(self):
Expand Down
5 changes: 4 additions & 1 deletion Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,10 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
}

data = PyBytes_AS_STRING(value);
size = strlen(data); /* XXX Why not Py_SIZE(value)? */
/* bpo-39593: value will be truncated after a null character on purpose.
If we use PyBytes_GET_SIZE(value) in here, it will break the feature.*/
size = strlen(data);

if (size < length) {
/* This will copy the terminating NUL character
* if there is space for it.
Expand Down