Skip to content

bpo-39632: Fix ctypes variadic function call convention #18560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ndessart
Copy link

@ndessart ndessart commented Feb 19, 2020

On armhf and for variadic functions (and contrary to non-variadic
functions), the VFP co-processor registers are not used for float
argument parameter passing. This specificity was completely
disregarded by ctypes which used ffi_prep_cif systematically
to prepare the parameter passing of a function while it should
use ffi_prep_cif_var for variadic functions instead.

As such variadic function call with float arguments through ctypes
was broken on armhf targets.

This change fixes and improves ctypes variadic function support.
Functions that take a variable number of arguments should now be
declared using an Ellipsis (...) as their last argument (just as
a prototype for a C function). ctypes variadic functions declared
this way automatically perform the default argument promotion.
Small integer types are promoted to c_int and c_float is promoted
to c_double.

I am new to CPython development but I think that without the
introduction of the Ellipsis in functions argtypes this could be
backported to the 3.7 and 3.8 maintenance branches.

For reference/see also:

https://bugs.python.org/issue39632

@ndessart ndessart changed the title bpo 39632: Fix ctypes variadic function call convention bpo-39632: Fix ctypes variadic function call convention Feb 19, 2020
@ndessart
Copy link
Author

The build is failing on macOS because ctypes uses an obsolete libffi version bundled into Modules/_ctypes/libffi_osx for this platform. This old version of libffi is missing ffi_prep_cif_var.

I don't know why libffi is bundled only for mac but I guess that we could just bundle the latest release of libffi (currently libffi-3.3 for every target.

Since this is my very first (rather ambitious) contribution to CPython, I think I will need some guidance moving forward. Thanks

@ndessart ndessart force-pushed the ctypes_variadic_functions branch from 6e4a5ef to 045c53c Compare June 22, 2021 11:02
On armhf and for variadic functions (and contrary to non-variadic
functions), the VFP co-processor registers are not used for float
argument parameter passing. This specificity was completely
disregarded by ctypes which used `ffi_prep_cif` systematically
to prepare the parameter passing of a function while it should
use `ffi_prep_cif_var` for variadic functions instead.

As such variadic function call with float arguments through ctypes
was broken on armhf targets.

This change fixes and improves ctypes variadic function support.
Functions that take a variable number of arguments should now be
declared using an Ellipsis (...) as their last argument (just as
a prototype for a C function). ctypes variadic functions declared
this way automatically perform the default argument promotion.
Small integer types are promoted to c_int and c_float is promoted
to c_double.

For reference/see also:
- https://sourceware.org/ml/libffi-discuss/2016/msg00043.html
- https://bugs.python.org/issue17580
- spacepy/spacepy#20
- https://en.cppreference.com/w/c/language/conversion#Default_argument_promotions
@skirpichev
Copy link
Member

@ndessart, are you interested in this? Could you fix merge conflicts?

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

The following commit authors need to sign the Contributor License Agreement:

CLA signed

@skirpichev skirpichev self-assigned this Apr 7, 2025
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.

5 participants