Skip to content

gh-112014: correct buffer protocol support in ctypes (use native formats) - #155472

Open
skirpichev wants to merge 1 commit into
python:mainfrom
skirpichev:use-native-endianness-in-ctypes/112014
Open

gh-112014: correct buffer protocol support in ctypes (use native formats)#155472
skirpichev wants to merge 1 commit into
python:mainfrom
skirpichev:use-native-endianness-in-ctypes/112014

Conversation

@skirpichev

@skirpichev skirpichev commented Aug 10, 2026

Copy link
Copy Markdown
Member

This allows better interoperation with the memoryview, e.g. support for ctypes arrays. Just like NumPy arrays, they also don't specify endianness:

>>> import numpy as np
>>> memoryview(np.ndarray(3, dtype=np.float16)).format
'e'

But keep specified byteorder for byte-swapped types.

…e formats)

This allows better interoperation with the memoryview, e.g. support for
ctypes arrays.  Just like NumPy arrays, they also don't specify
endianness:
```pycon
>>> import numpy as np
>>> memoryview(np.ndarray(3, dtype=np.float16)).format
'e'
```

But keep specified byteorder for byte-swapped types.
@skirpichev
skirpichev force-pushed the use-native-endianness-in-ctypes/112014 branch from 45bebeb to feb9f5a Compare August 10, 2026 07:55
@skirpichev

This comment was marked as resolved.

@bedevere-bot

This comment was marked as resolved.

@skirpichev
skirpichev marked this pull request as ready for review August 10, 2026 08:53
@skirpichev
skirpichev requested review from encukou and vstinner and removed request for encukou August 10, 2026 08:53

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that I can understand the rationale for this change, but it seems like @encukou has a different opinion on how the issue should be addressed: #112014 (comment).

Correct the :ref:`buffer protocol <bufferobjects>` support in the
:mod:`ctypes` module to use the machine’s native format and byte order,
rather than explicitly specify endianness (by ``'<'`` or ``'>'``). The
later kept for byte-swapped types. Patch by Sergey B Kirpichev.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From this NEWS entry, it's uneasy to understand the rationale for the change. According to #112014 (comment), I understand that before, it was not possible to modify a ctypes type using memoryview, since the buffer format was rejected by memoryview when attempting to modify the view.

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.

3 participants