Open applebud opened 9 months ago
See also: https://github.com/python/cpython/issues/107844, and this part of the documentation: https://docs.python.org/3/library/ctypes.html#calling-variadic-functions
In this particular case you shouldn't have to use argtypes for printf, or rather only have to specify the first argument. That doesn't necessarily work in general though.
The issue here is that on macOS/arm64 the calling conventions for regular and "variadic" arguments are different. During the initial port of Python to macOS/arm64 we choose to deduce which arguments are regular and which are variadic by checking the length of argtypes
. That works most of the time, but not always as you have noticed.
We should add some (optional) API that explicitly tells ctypes which arguments are regular and which are variadic (for example by adding an attribute to function pointer object that denotes the index of the first variadic argument).
That is an API change and can be included in 3.13 at the earliest.
Yes, thanks, that workaround does work, with the catch that I have to effectively cast the double by hand:
>>> printf.argtypes=[c_char_p]
>>> printf(b"String '%s', Int %d, Double %f\n", b"Hi", 10, 2.2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 4: TypeError: Don't know how to convert parameter 4
>>> printf(b"String '%s', Int %d, Double %f\n", b"Hi", 10, c_double(2.2))
String 'Hi', Int 10, Double 2.200000
37
Bug report
Bug description:
I see a problem calling variadic functions imported from dynamic libraries under macOS on Apple Silicon. I’m using a bit of code sample from https://docs.python.org/3/library/ctypes.html#specifying-the-required-argument-types-function-prototypes First with Intel macOS Python (running via Rosetta):
And now with native Apple Silicon Python:
As you can see the results are incorrect.
CPython versions tested on:
3.11
Operating systems tested on:
macOS