Skip to content

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

Open
skirpichev wants to merge 2 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 2 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
stginfo->ffi_type_pointer.size = fmt->pffi_type->size;
stginfo->ffi_type_pointer.alignment = fmt->pffi_type->alignment;
stginfo->ffi_type_pointer.type = fmt->pffi_type->type;
stginfo->ffi_type_pointer.elements = PyMem_Malloc(els_size);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

According to https://www.chiark.greenend.org.uk/doc/libffi-dev/html/Structures.html the elements should be a NULL terminated list of pointers. With this allocation we are missing the NULL. According to Claude this is no problem for the current implementation, but it is a breach of contract and a latent bug.

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.

This is for primitive types (section above), not for structures.

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 put the duplicated code in a helper function, like fill_stginfo_from_pffi_type?

stginfo->ffi_type_pointer.size = fmt->pffi_type->size;
stginfo->ffi_type_pointer.alignment = fmt->pffi_type->alignment;
stginfo->ffi_type_pointer.type = fmt->pffi_type->type;
stginfo->ffi_type_pointer.elements = PyMem_Malloc(els_size);
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 put the duplicated code in a helper function, like fill_stginfo_from_pffi_type?

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.

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.

3 participants