]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-76961: Fix the PEP3118 format string for ctypes.Structure (#5561)
authorEric Wieser <wieser.eric@gmail.com>
Sun, 5 Feb 2023 17:10:53 +0000 (17:10 +0000)
committerGitHub <noreply@github.com>
Sun, 5 Feb 2023 17:10:53 +0000 (17:10 +0000)
commit90d85a9b4136aa1feb02f88aab614a3c29f20ed3
treebe37c9c00a26da338ff35621e9e399315e2e64bb
parent0672a6c23b2b72666e10d9c61fc025e66aad9c2b
gh-76961: Fix the PEP3118 format string for ctypes.Structure (#5561)

The summary of this diff is that it:

* adds a `_ctypes_alloc_format_padding` function to append strings like `37x` to a format string to indicate 37 padding bytes
* removes the branches that amount to "give up on producing a valid format string if the struct is packed"
* combines the resulting adjacent `if (isStruct) {`s now that neither is `if (isStruct && !isPacked) {`
* invokes `_ctypes_alloc_format_padding` to add padding between structure fields, and after the last structure field. The computation used for the total size is unchanged from ctypes already used.

This patch does not affect any existing aligment computation; all it does is use subtraction to deduce the amount of paddnig introduced by the existing code.

---

Without this fix, it would never include padding bytes - an assumption that was only
valid in the case when `_pack_` was set - and this case was explicitly not implemented.

This should allow conversion from ctypes structs to numpy structs

Fixes https://github.com/numpy/numpy/issues/10528
Doc/library/ctypes.rst
Lib/test/test_ctypes/test_pep3118.py
Misc/NEWS.d/next/Core and Builtins/2018-02-05-21-54-46.bpo-32780.Dtiz8z.rst [new file with mode: 0644]
Modules/_ctypes/stgdict.c