Skip to content

gh-148464: Add missing __ctype_le/be__ attributes for complex types in the ctype module#148485

Merged
encukou merged 13 commits intopython:mainfrom
skirpichev:add-F/D_sw/148464
Apr 17, 2026
Merged

gh-148464: Add missing __ctype_le/be__ attributes for complex types in the ctype module#148485
encukou merged 13 commits intopython:mainfrom
skirpichev:add-F/D_sw/148464

Conversation

@skirpichev
Copy link
Copy Markdown
Member

@skirpichev skirpichev commented Apr 13, 2026

Comment thread Modules/_ctypes/_ctypes.c Outdated
@skirpichev skirpichev requested a review from eendebakpt April 14, 2026 08:50
Comment thread Modules/_ctypes/_ctypes.c Outdated
Comment thread Modules/_ctypes/_ctypes.c Outdated
Comment thread Modules/_ctypes/cfield.c Outdated
D_get_sw(void *ptr, Py_ssize_t size)
{
assert(NUM_BITS(size) || (size == 2*sizeof(double)));
return PyComplex_FromDoubles(PyFloat_Unpack8(ptr, PY_BIG_ENDIAN),
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.

Could you add error handling for the unpacks here and PyFloat_Unpack4 below?

if (x == -1.0 && PyErr_Occurred()) {

I'd be happier if ctypes didn't rely on an undocumented implementation detail here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why undocumented? It's documented and widely used across the CPython codebase, e.g. in same file.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

widely used

Well, that's a little lie. But it indeed was used in this way in the ctypes and some other places. Until v3.15.0a8 that was a bug.

It probably not worth to change other places, where error checks kept. But I don't see good reasons to not rely on documented behavior for new code.

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.

Ah, I missed the docs note!
I still think it's better to be defensive/portable in ctypes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I would prefer rather be consistent across the module (or even for the one file!). Hardly there is a chance that this code will be used outside of the CPython.

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.

OK, I'll leave it to another PR.

Old code relying on implementation detail, that elements[1] for
the FFI_TYPE_COMPLEX was never read.

But this type actually shares same assumption as the FFI_TYPE_STRUCT:
the elements field is a NULL-terminated array of pointers to ffi_type
objects.  So far for primitive types - only complex types have this
struct field as non-NULL (two element array).
@skirpichev
Copy link
Copy Markdown
Member Author

BTW, the _ctypes.PyCSimpleType lacks Py_tp_dealloc, despite it did some memory allocations during initialization (not just in places, touched by the pr). Maybe it should be fixed eventually.

Comment thread Modules/_ctypes/_ctypes.c Outdated
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Comment thread Modules/_ctypes/cfield.c Outdated
@skirpichev skirpichev requested a review from sunmy2019 April 15, 2026 09:45
Comment thread Modules/_ctypes/_ctypes.c
Comment thread Modules/_ctypes/_ctypes.c Outdated
Comment thread Modules/_ctypes/_ctypes.c Outdated
skirpichev and others added 2 commits April 15, 2026 13:08
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Comment thread Modules/_ctypes/_ctypes.c
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
@skirpichev skirpichev requested a review from sunmy2019 April 15, 2026 23:35
@skirpichev skirpichev requested a review from ashm-dev April 15, 2026 23:44
Copy link
Copy Markdown
Contributor

@ashm-dev ashm-dev left a comment

Choose a reason for hiding this comment

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

LGTM

@encukou encukou merged commit 769cc83 into python:main Apr 17, 2026
54 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @skirpichev for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@encukou
Copy link
Copy Markdown
Member

encukou commented Apr 17, 2026

Thank you!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 17, 2026
…ex types in the ctype module (pythonGH-148485)

(cherry picked from commit 769cc8338f35eb134508aca701a59342bcb6a84b)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Apr 17, 2026

GH-148677 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Apr 17, 2026
@skirpichev skirpichev deleted the add-F/D_sw/148464 branch April 17, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants