cpython/Modules/_ctypes
Petr Viktorin 78ffba4221
gh-127295: ctypes: Switch field accessors to fixed-width integers (GH-127297)
This should be a pure refactoring, without user-visible behaviour changes.

Before this change, ctypes uses traditional native C types, usually identified 
by [`struct` format characters][struct-chars] when a short (and 
identifier-friendly) name is needed:
- `signed char` (`b`) / `unsigned char` (`B`)
- `short` (`h`) / `unsigned short` (`h`)
- `int` (`i`) / `unsigned int` (`i`)
- `long` (`l`) / `unsigned long` (`l`)
- `long long` (`q`) / `unsigned long long` (`q`)

These map to C99 fixed-width types, which this PR switches to: - 
- `int8_t`/`uint8_t`
- `int16_t`/`uint16_t`
- `int32_t`/`uint32_t`
- `int64_t`/`uint64_t`

The C standard doesn't guarantee that the “traditional” types must map to the 
fixints. But, [`ctypes` currently requires it][swapdefs], so the assumption won't 
break anything.

By “map” I mean that the *size* of the types matches. The *alignment* 
requirements might not. This needs to be kept in mind but is not an issue in 
`ctypes` accessors, which [explicitly handle unaligned memory][memcpy] for the 
integer types.

Note that there are 5 “traditional” C type sizes, but 4 fixed-width ones. Two of 
the former are functionally identical to one another; which ones they are is 
platform-specific (e.g. `int`==`long`==`int32_t`.) This means that one of the 
[current][current-impls-1] [implementations][current-impls-2] is redundant on 
any given platform.


The fixint types are parametrized by the number of bytes/bits, and one bit for 
signedness. This makes it easier to autogenerate code for them or to write 
generic macros (though generic API like 
[`PyLong_AsNativeBytes`][PyLong_AsNativeBytes] is problematic for performance 
reasons -- especially compared to a `memcpy` with compile-time-constant size).

When one has a *different* integer type, determining the corresponding fixint  
means a `sizeof` and signedness check. This is easier and more robust than the 
current implementations (see [`wchar_t`][sizeof-wchar_t] or 
[`_Bool`][sizeof-bool]).


[swapdefs]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L420-L444
[struct-chars]: https://docs.python.org/3/library/struct.html#format-characters
[current-impls-1]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L470-L653
[current-impls-2]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L703-L944
[memcpy]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L613
[PyLong_AsNativeBytes]: https://docs.python.org/3/c-api/long.html#c.PyLong_AsNativeBytes
[sizeof-wchar_t]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L1547-L1555
[sizeof-bool]: https://github.com/python/cpython/blob/v3.13.0/Modules/_ctypes/cfield.c#L1562-L1572


Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-12-20 14:28:18 +01:00
..
clinic gh-127946: Use a critical section for `CFuncPtr` attributes (GH-128109) 2024-12-20 14:02:46 +01:00
_ctypes.c gh-127295: ctypes: Switch field accessors to fixed-width integers (GH-127297) 2024-12-20 14:28:18 +01:00
_ctypes_test.c gh-125206: Bug in ctypes with old libffi is fixed (#125322) 2024-10-15 16:17:10 +00:00
_ctypes_test.h
_ctypes_test_generated.c.h gh-97588: Align ctypes struct layout to GCC/MSVC (GH-97702) 2024-05-29 12:02:53 +02:00
callbacks.c gh-127295: ctypes: Switch field accessors to fixed-width integers (GH-127297) 2024-12-20 14:28:18 +01:00
callproc.c gh-126742: Add _PyErr_SetLocaleString, use it for gdbm & dlerror messages (GH-126746) 2024-12-17 12:12:45 +01:00
cfield.c gh-127295: ctypes: Switch field accessors to fixed-width integers (GH-127297) 2024-12-20 14:28:18 +01:00
ctypes.h gh-127295: ctypes: Switch field accessors to fixed-width integers (GH-127297) 2024-12-20 14:28:18 +01:00
malloc_closure.c gh-108765: Python.h no longer includes <unistd.h> (#108783) 2023-09-02 16:50:18 +02:00
stgdict.c gh-126937: ctypes: fix TypeError when a field's size is >65535 bytes (GH-126938) 2024-12-10 13:13:11 +01:00