Skip to content

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

Open
skirpichev wants to merge 5 commits intopython:mainfrom
skirpichev:add-F/D_sw/148464
Open

gh-148464: Add missing __ctype_le/be__ attributes for complex types in the ctype module#148485
skirpichev wants to merge 5 commits intopython:mainfrom
skirpichev:add-F/D_sw/148464

Conversation

@skirpichev
Copy link
Copy Markdown
Member

@skirpichev skirpichev commented Apr 13, 2026

@skirpichev skirpichev requested a review from eendebakpt April 14, 2026 08:50
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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants