-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Labels
Description
This code produces an error with a somewhat unhelpful message:
from ctypes import *
printf = CDLL('libc.so.6').printf
printf.argtypes = [c_char_p, c_char_p]
printf(b"Value %s\n", 10) # call with an incompatible argumentTraceback (most recent call last):
File "/home/tomas/dev/cpython/error.py", line 5, in <module>
printf(b"Value: %s\n", 10)
ctypes.ArgumentError: argument 2: TypeError: wrong typeThe source code itself suggests providing a better message:
cpython/Modules/_ctypes/_ctypes.c
Lines 1795 to 1797 in 3979150
| /* XXX better message */ | |
| PyErr_SetString(PyExc_TypeError, | |
| "wrong type"); |
I suggest making the message more helpful by including the expected argument type and the type that was actually provided e.g.
- TypeError: wrong type
+ TypeError: 'int' object cannot be interpreted as ctypes.c_char_pThe same improvement can be applied to c_wchar_p and c_void_p.
Linked PRs
Eclips4