# Ctypes: improving Structure/Union & basic types

**URL:** <https://discuss.python.org/t/ctypes-improving-structure-union-basic-types/77489>\
**Category:** Ideas\
**Created:** [January 17, 2025, 6:04pm UTC](https://discuss.python.org/t/ctypes-improving-structure-union-basic-types/77489 "2025-01-17T18:04:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [January 17, 2025, 6:04pm UTC](https://discuss.python.org/t/ctypes-improving-structure-union-basic-types/77489/1 "2025-01-17T18:04:35Z")

</div>

Hello,  
I’m spending some of my time improving `ctypes`.  
I’m aiming for incremental changes, seeing where that will take me, so I don’t think I can write a PEP first. On the other hand, individual issues feel too discunnected. Perhaps a topic here will work, like [@barneygale’s one on `pathlib`](https://discuss.python.org/t/make-pathlib-extensible/3428).

[So far](https://docs.python.org/3.14/whatsnew/3.14.html#ctypes), I’ve

- Finished Rian Hunter’s PR to add [memoryview\_at](https://docs.python.org/3.14/library/ctypes.html#ctypes.memoryview_at)
- merged a few PRs by Jun Komoda with improvements to COM
- reviewed Peter Bierma’s [free-threading improvemens](https://github.com/python/cpython/pull/128490)
- [started switching to fixed-width integers](https://github.com/python/cpython/issues/127295): N-byte integers are easier to reason about than ints, shorts and longs. IMO, ctypes badly need that abstraction (which C itself [got in 1999](https://en.cppreference.com/w/c/types/integer)). Of course, CPUs, platform ABIs and current ctypes all work in terms of shorts or half-words of platform-dependent sizes; we can’t lose _that_ abstraction.
- merged a PR by Matthias Görgens to improve the layout of Structures (and Unions), and rewritten the algorithm in Python.

Hope I’m not forgetting too much :‍)

I think I’ll continue working a bit longer on Structures, Unions and basic types, aiming to make them and their fields more usable, introspectable and configurable from Python.

For the next step, I [have a pull request up](https://github.com/python/cpython/pull/128950) proposing to improve introspectability of Structure/Union fields, like this:

```python
>>> class Color(Structure):
... _fields_ = (
... ('red', c_uint8),
... ('green', c_uint8),
... ('blue', c_uint8),
... ('intense', c_bool, 1),
... ('blinking', c_bool, 1),
... )
...
>>> Color.red
<ctypes.CField 'red' type=c_ubyte, ofs=0, size=1>
>>> Color.green.type
<class 'ctypes.c_ubyte'>
>>> Color.blue.byte_offset
2
>>> Color.intense
<ctypes.CField 'intense' type=c_bool, ofs=3, bit_size=1, bit_offset=0>
>>> Color.blinking.bit_offset
1

```

These are quite useful for writing tests for struct layouts, and I hope they’ll also be useful for further simplifying that logic. Maybe we can eventually even make the struct layout fully customizable from Python (although I don’t think that’s on the table for Python 3.14).

What do you think? Is it a good idea? Or should these be private?

---

<div class="post-metadata">

**Author:** ![ZeroIntensity](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zerointensity/32/10639_2.png) [@ZeroIntensity](https://discuss.python.org/u/ZeroIntensity)\
**Post date:** [January 18, 2025, 7:08pm UTC](https://discuss.python.org/t/ctypes-improving-structure-union-basic-types/77489/2 "2025-01-18T19:08:15Z")

</div>

If we’re going for usability, then something I’ve been thinking about is using type annotations for defining fields, similar to how things are done for dataclasses and `NamedTuple`.

In the past, I’ve found that defining `_fields_` has been a bit tedious, because it’s difficult to remember some of the rules (e.g. does the type or name go first in the tuple?)

So, something like this:

```py
class Color(Structure):
    red: c_uint8
    green: c_uint8
    blue: c_uint8
    intense: Annotated[c_bool, 1]
    blinking: Annotated[c_bool, 1]

```

I guess this would break some classes that define unrelated class variables, so it would have to be opt-in, but to me it seems a lot nicer than `_fields_`.

---

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [January 20, 2025, 9:10am UTC](https://discuss.python.org/t/ctypes-improving-structure-union-basic-types/77489/3 "2025-01-20T09:10:39Z")

</div>

There’s a feature request for that; I [replied there](https://github.com/python/cpython/issues/104533#issuecomment-2601819216).
