Skip to content
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

ctypes: Improve error messages when converting to an incompatible type #107455

Closed
tomasr8 opened this issue Jul 30, 2023 · 1 comment
Closed

ctypes: Improve error messages when converting to an incompatible type #107455

tomasr8 opened this issue Jul 30, 2023 · 1 comment
Labels
topic-ctypes type-feature A feature request or enhancement

Comments

@tomasr8
Copy link
Contributor

tomasr8 commented Jul 30, 2023

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 argument
Traceback (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 type

The source code itself suggests providing a better message:

/* 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_p

The same improvement can be applied to c_wchar_p and c_void_p.

Linked PRs

@kumaraditya303
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-ctypes type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants