FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-29753: fix merging packed bitfields in ctypes struct/union by FFY00 · Pull Request #19850 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
63 changes: 63 additions & 0 deletions Lib/ctypes/test/test_bitfields.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,69 @@ class X(Structure):
else:
self.assertEqual(sizeof(X), sizeof(c_int) * 2)

@unittest.skipIf(os.name == 'nt', reason='Posix only')
Comment thread
jaraco marked this conversation as resolved.
Outdated
def test_packed_posix(self):
test_cases = {
(
("a", c_uint8, 4),
("b", c_uint8, 4),
): 1,
(
("a", c_uint8, 1),
("b", c_uint16, 1),
("c", c_uint32, 1),
("d", c_uint64, 1),
): 1,
(
("a", c_uint8, 8),
("b", c_uint16, 1),
("c", c_uint32, 1),
("d", c_uint64, 1),
): 2,
(
("a", c_uint32, 9),
("b", c_uint16, 10),
("c", c_uint32, 25),
("d", c_uint64, 1),
): 6,
(
("a", c_uint32, 9),
("b", c_uint16, 10),
("c", c_uint32, 25),
("d", c_uint64, 5),
): 7,
(
("a", c_uint16),
("b", c_uint16, 9),
("c", c_uint16, 1),
("d", c_uint16, 1),
("e", c_uint16, 1),
("f", c_uint16, 1),
("g", c_uint16, 3),
("h", c_uint32, 10),
("i", c_uint32, 20),
("j", c_uint32, 2),
): 8,
(
("a", c_uint16, 9),
("b", c_uint16, 10),
("d", c_uint16),
("c", c_uint8, 8),
): 6,
(
("a", c_uint32, 9),
("b", c_uint32),
("c", c_uint32, 8),
): 7,
}

for fields, size in test_cases.items():
with self.subTest(fields=fields):
class X(Structure):
_pack_ = 1
_fields_ = list(fields)
self.assertEqual(sizeof(X), size)

def test_anon_bitfields(self):
# anonymous bit-fields gave a strange error message
class X(Structure):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In ctypes, now packed bitfields are calculated properly and the first item of packed bitfields is now shrank correctly.
35 changes: 27 additions & 8 deletions Modules/_ctypes/cfield.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
Py_DECREF(self);
return NULL;
}

#ifndef MS_WIN32
/* if we have a packed bitfield, calculate the minimum number of bytes we
need to fit it. otherwise use the specified size. */
if (pack && bitsize) {
size = (bitsize - 1) / 8 + 1;

FFY00 Jun 25, 2020
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

(copying the old comment, so that it shows up in the github editor for reviewers)

If we have a packed bitfield, calculate the minimum number of bytes needed to fit the bitfield.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'd follow this rule: if a comment is necessary/helpful/crucial for reviewers it should be in the source code so that everyone looking at this code at any point later on sees it, not in GitHub comments.

} else
#endif
size = dict->size;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Otherwise, just use the field size.


proto = desc;

if (bitsize /* this is a bitfield request */
&& *pfield_size /* we have a bitfield open */
#ifdef MS_WIN32
Expand All @@ -87,25 +99,26 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
} else if (bitsize /* this is a bitfield request */
&& *pfield_size /* we have a bitfield open */
&& dict->size * 8 >= *pfield_size
&& (*pbitofs + bitsize) <= dict->size * 8) {
/* if this is a packed bitfield, always expand it.
otherwise calculate if we need to expand it. */
&& (((*pbitofs + bitsize) <= dict->size * 8) || pack)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

When we have a packed bitfield and it doesn't fit the current open bitfield, always expand it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'm quite convinced this comment should go in the source code.

/* expand bit field */
fieldtype = EXPAND_BITFIELD;
#endif
} else if (bitsize) {
/* start new bitfield */
fieldtype = NEW_BITFIELD;
*pbitofs = 0;
*pfield_size = dict->size * 8;
/* use our calculated size (size) instead of type size (dict->size),
which can be different for packed bitfields */
*pfield_size = size * 8;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Start a new bitfield with the size we calculated above. This means if it is a packed bitfield we start a new bitfield with only the needed size, not the specified field size.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ditto here.

} else {
/* not a bit field */
fieldtype = NO_BITFIELD;
*pbitofs = 0;
*pfield_size = 0;
}

size = dict->size;
proto = desc;

/* Field descriptors for 'c_char * n' are be scpecial cased to
return a Python string instead of an Array object instance...
*/
Expand Down Expand Up @@ -170,10 +183,16 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
break;

case EXPAND_BITFIELD:
*poffset += dict->size - *pfield_size/8;
*psize += dict->size - *pfield_size/8;
/* increase the size if it is a packed bitfield.
EXPAND_BITFIELD should not be selected for non-packed fields if the
current size isn't already enough. */
if (pack)
Comment thread
FFY00 marked this conversation as resolved.
Outdated
size = (*pbitofs + bitsize - 1) / 8 + 1;

*poffset += size - *pfield_size/8;
*psize += size - *pfield_size/8;

*pfield_size = dict->size * 8;
*pfield_size = size * 8;

if (big_endian)
self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize;
Expand Down

Back | FazBrowse Home | New Git URL