-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
[C API] Avoid "const PyObject*" type #91768
Comments
Using The C language is weird because it allows to cast See also this question of the C FAQ: Why can't I pass a char ** to a function which expects a const char **?. |
This problem will become worse with PEP 670 (convert macros to functions) which removes the implicit cast of function arguments to |
Py_REFCNT(), Py_TYPE(), Py_SIZE() and Py_IS_TYPE() functions argument type is now "PyObject*", rather than "const PyObject*". * Replace also "const PyObject*" with "PyObject*" in functions: * _Py_strhex_impl() * _Py_strhex_with_sep() * _Py_strhex_bytes_with_sep() * Remove _PyObject_CAST_CONST() and _PyVarObject_CAST_CONST() macros. * Py_IS_TYPE() can now use Py_TYPE() in its implementation.
Fixed by #91769 |
See also python/cpython#91768 Fixes #302 Also stops building the mac wheels we ship with Ofast and debugging assertions. That's not meant for production use.
See also python/cpython#91768 Fixes #302 Also stops building the mac wheels we ship with Ofast and debugging assertions. That's not meant for production use.
When I converted
Py_REFCNT()
,Py_TYPE()
andPy_SIZE()
macros to static inline functions (issue #83754), I choseconst PyObject*
for their argument because these macros don't modify any member of the PyObject structure. When the Py_IS_TYPE() function was added, it followed the trend (commit 8767ce9). The_PyObject_CAST_CONST()
macro was added to easily convert any pointer toconst PyObject*
for these macros expectingconst PyObject*
.The problem is that passing
PyObject*
to one of these functions emits a compiler warning usinggcc -Wcast-qual
. The_PyObject_CAST_CONST()
doesn't make the warning quiet.Example explaining the issue:
Output:
In practice, the problem was that building a C extension (which includes
<Python.h>
) withgcc -Wcast-qual -Werror
on Python 3.10 failed with an error on Py_IS_TYPE() implemented as:See the issue #88544 for the compiler error. I removed the Py_TYPE() call in Py_IS_TYPE() to work around the issue:
But this problem strikes back when I try to convert unicodeobject.h macros to static inline functions for PEP 670 which removes the cast to
PyObject*
in the limited C API version 3.11: see PR #91696 and PR #91705. For example, _Py_strhex_with_sep() and _Py_strhex_bytes_with_sep() function get aconst PyObject*
argument and use PyUnicode C functions like PyUnicode_READY(), PyUnicode_KIND() and PyUnicode_READ_CHAR(), but these PyUnicode functions expectPyObject*
: theconst
qualifier is lost. If Python/pystrhex.c is built withgcc -Wcast-qual
, gcc emits warnings on PyUnicode_KIND() and PyUnicode_READ_CHAR() calls. That's just an example of problem which can happen in C extensions as well.To avoid these problems, I propose to avoid
const PyObject*
in Python: only usePyObject*
.The text was updated successfully, but these errors were encountered: