Skip to content

gh-81368: Update PyCField_get_size #14647

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

shihai1991
Copy link
Member

@shihai1991 shihai1991 commented Jul 8, 2019

@eric-wieser
Copy link
Contributor

It's not clear to me that this is the right fix. With this change, it's now impossible to get the bit offset of a struct field.

@shihai1991
Copy link
Member Author

It's not clear to me that this is the right fix. With this change, it's now impossible to get the bit offset of a struct field.

Getting offset should be use https://github.com/python/cpython/blob/master/Modules/_ctypes/cfield.c#L254

@eric-wieser
Copy link
Contributor

eric-wieser commented Jul 9, 2019

Getting offset should be use https://github.com/python/cpython/blob/master/Modules/_ctypes/cfield.c#L254

What if I want the bit offset and not the byte offset? Consider:

class S(ctypes.Structure):
    _fields_ = [
        ('a', ctypes.c_uint8),
        ('b', ctypes.c_uint8, 6),
        ('c', ctypes.c_uint8, 2),
    ]

Previously, I could do:

>>> S.c.size >> 16  # number of bits
2
>>> S.c.size & 0xFFFF  # bit offset
6
>>> S.c.offset  # byte offset
1

Obviously this API didn't make much sense from the current documentation, but it was at least possible to extract this information. How would I calculate those three values with your patch?

@shihai1991
Copy link
Member Author

Getting offset should be use https://github.com/python/cpython/blob/master/Modules/_ctypes/cfield.c#L254

What if I want the bit offset and not the byte offset? Consider:

class S(ctypes.Structure):
    _fields_ = [
        ('a', ctypes.c_uint8),
        ('b', ctypes.c_uint8, 6),
        ('c', ctypes.c_uint8, 2),
    ]

Previously, I could do:

>>> S.c.size >> 16  # number of bits
2
>>> S.c.size & 0xFFFF  # bit offset
6
>>> S.c.offset  # byte offset
1

Obviously this API didn't make much sense from the current documentation, but it was at least possible to extract this information. How would I calculate those three values with your patch?

en, I think it should be a inner behavior. Why the user should know this detail?

@erlend-aasland erlend-aasland changed the title bpo-37187: Update PyCField_get_size gh-81368: Update PyCField_get_size Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants