python / python/cpython

_ctypes fails to extend the return value per libffi contract

Open
#156,933 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules topic-ctypes type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The current implementation is relying on restype->size to write the data into a buffer, however, per libffi contract

integral types narrower than the system register size are widened

Which is not happening here. This has caused a failure on s390x when using libclang. cindex.py calls into libclang through ctypes, which builds the callback closures using libffi. With a c_int return, ctypes writes only the low 32 bits and skips the extension that both the ABI and libffi's closure contract require.

We have also opened a PR to fix this temporarily under llvm: https://github.com/llvm/llvm-project/pull/221024

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs
  • gh-157045

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Modules/_ctypes/callbacks.c at the linked return-value handling and read the referenced libffi closure contract. Reproduce or inspect the c_int callback behavior on s390x, then verify that narrow integral return values follow the required ABI extension rather than only copying restype->size bytes. Check the linked CPython PR for the work already in progress.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.