classification
Title: Py_IS_TYPE(): cast discards ‘const’ qualifier from pointer target type
Type: Stage: resolved
Components: C API Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: miss-islington, vstinner
Priority: normal Keywords: patch

Created on 2021-06-10 13:19 by vstinner, last changed 2021-06-11 09:44 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26644 merged vstinner, 2021-06-10 13:23
PR 26668 merged miss-islington, 2021-06-11 08:35
Messages (4)
msg395534 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-06-10 13:19
The following change introduced a compiler warning:

commit c5cb077ab3c88394b7ac8ed4e746bd31b53e39f1
Author: Victor Stinner <vstinner@python.org>
Date:   Tue Sep 22 12:42:28 2020 +0200

    Py_IS_TYPE() macro uses Py_TYPE() (GH-22341)

Steps to Reproduce:

1. dnf install -y python3-devel pkgconf-pkg-config gcc
2. printf '#include <Python.h>' > test.c
3. gcc -Wcast-qual -Werror -c test.c `pkg-config --cflags --libs python-3.10`

Actual results:
sh-5.1# gcc -Wcast-qual -Werror -c test.c `pkg-config --cflags --libs python-3.10`
In file included from /usr/include/python3.10/Python.h:78,
                 from test.c:1:
/usr/include/python3.10/object.h: In function ‘_Py_IS_TYPE’:
/usr/include/python3.10/object.h:112:29: error: cast discards ‘const’ qualifier from pointer target type [-Werror=cast-qual]
  112 | #define _PyObject_CAST(op) ((PyObject*)(op))
      |                             ^
/usr/include/python3.10/object.h:137:34: note: in expansion of macro ‘_PyObject_CAST’
  137 | #define Py_TYPE(ob)             (_PyObject_CAST(ob)->ob_type)
      |                                  ^~~~~~~~~~~~~~
/usr/include/python3.10/object.h:144:12: note: in expansion of macro ‘Py_TYPE’
  144 |     return Py_TYPE(ob) == type;
      |            ^~~~~~~
cc1: all warnings being treated as errors


Attached PR fix the compiler warning.


Issue reported on Fedora: https://bugzilla.redhat.com/show_bug.cgi?id=1969663
msg395535 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-06-10 13:26
Comment from my PR:

// bpo-44378: Don't use Py_TYPE() since Py_TYPE() requires a non-const
// object.

By the way, I wrote a change in Python 3.11 to convert Py_TYPE() macro into a static inline function which accepts a *const* PyObject pointer ("const PyObject*"):
https://github.com/python/cpython/commit/f3fa63ec75fdbb4a08a10957a5c631bf0c4a5970

But this change had to be reverted since it caused a buildbot regression (bpo-44348).
msg395617 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-06-11 08:35
New changeset 304dfec8d3c0763734ea8b5fa2af1d9e1ce69ffa by Victor Stinner in branch 'main':
bpo-44378: Fix a compiler warning in Py_IS_TYPE() (GH-26644)
https://github.com/python/cpython/commit/304dfec8d3c0763734ea8b5fa2af1d9e1ce69ffa
msg395622 - (view) Author: miss-islington (miss-islington) Date: 2021-06-11 08:57
New changeset e6d28a1a6ad22125fc3a6df2d611d79aa8d6f67e by Miss Islington (bot) in branch '3.10':
bpo-44378: Fix a compiler warning in Py_IS_TYPE() (GH-26644)
https://github.com/python/cpython/commit/e6d28a1a6ad22125fc3a6df2d611d79aa8d6f67e
History
Date User Action Args
2021-06-11 09:44:59vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-06-11 08:57:37miss-islingtonsetmessages: + msg395622
2021-06-11 08:35:47miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request25255
2021-06-11 08:35:44vstinnersetmessages: + msg395617
2021-06-10 13:26:30vstinnersetmessages: + msg395535
2021-06-10 13:23:18vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request25230
2021-06-10 13:19:54vstinnercreate