gh-128715: Expose ctypes.CField, with info attributes #128950
+742
−163
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Expose the type of struct/union field descriptors (
_ctypes._CField
) asctypes.CField
. (This is mainly to make it easier to refer to it -- for typing and documentation. CreatingCField
s manually is not supported.)Add attributes for easier introspection:
name
&type
, as defined in_fields_
byte_size
&byte_offset
, specify the chunk of memory to read/writebit_size
&bit_offset
, which specify the shift & mask for bitfieldsis_bitfield
&is_anonymous
booleansThe existing
offset
remains, as an alias forbyte_offset
.The existing
size
is unchanged. Usually it is the same asbyte_size
, but for bitfields, it contains a bit-packed combination ofbit_size
&bit_offset
.Update the
repr()
ofCField
.Use the same values internally as well. (Except
bit_size
, which might overflowPy_ssize_t
for very large types. Instead, in C usebitfield_size
, which is 0 for non-bitfields and thus serves asis_bitfield
flag. Different name used clarity.)Old names are removed from the C implementation to ensure a clean transition.
For simplicity, I keep
byte_size
inCFieldObject
for now, even though it's redundant: it's the size of the underlying type.Add a generic test that ensures the values are consistent, and that we don't have overlapping fields in structs. Use this check throughout
test_ctypes
, wherever a potentially interesting Structure or Union is created. (Tests for how simple structs behave after they're created don't need the checks, but I erred on the side of adding checks.)Lift the restriction on maximum field size that was temporarily added in GH-126938. The max size is now
Py_ssize_t
.This PR does not yet touch
cfield.c
getters & setters: the bit-packed “size” argument is computed and passed to those.📚 Documentation preview 📚: https://cpython-previews--128950.org.readthedocs.build/