Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

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

Merged
merged 6 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 8 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 4 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: XXX Why not Py_SIZE(value)? */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that it's correct to add a test which indirectly specify the behavior and keep this comment which suggests to change the behavior.

I suggest to rewrite the comment to explain that we do use strlen() on purpose, rather than PyBytes_GET_SIZE() since we truncate characters after a null character on purpose. Keep the bpo number in the comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, got it. I updated the description info, do you think it's clear enough?

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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.