Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-39593: Adding an unit test of ctypes #18424

Merged
merged 6 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,9 @@ 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: Use strlen() to truncate the string at the first null character.
size = strlen(data);
shihai1991 marked this conversation as resolved.
Show resolved Hide resolved

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