GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pyatomic_gcc.h with GCC discards const qualifier · Issue #120593 · python/cpython

pyatomic_gcc.h with GCC discards const qualifier  #120593

New issue
Copy link
New issue
Copy link
Closed
Closed
pyatomic_gcc.h with GCC discards const qualifier #120593
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@ndparker
ndparker
opened on Jun 16, 2024
Issue body actions

Bug report

Bug description:

Hi,

including Python.h with gcc and -Wcast-qual raises the following issue (Regression from 3.12):

    /usr/include/python3.13/cpython/pyatomic_gcc.h: In function '_Py_atomic_load_ptr':
    /usr/include/python3.13/cpython/pyatomic_gcc.h:300:34: error: cast discards 'const' qualifier from pointer target type [-Werror=cast-qual]
      300 | { return (void *)__atomic_load_n((void **)obj, __ATOMIC_SEQ_CST); }
          |                                  ^
    /usr/include/python3.13/cpython/pyatomic_gcc.h: In function '_Py_atomic_load_ptr_relaxed':
    /usr/include/python3.13/cpython/pyatomic_gcc.h:359:34: error: cast discards 'const' qualifier from pointer target xt (line 8))
$ python3.13 -V
Python 3.13.0b1

This is a follow-up to #120293

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

  • gh-120593: Fix const qualifier in pyatomic.h #121052
  • gh-120593: Fix const qualifier in _PyLong_CompactValue() #121053
  • gh-120593: Fix const qualifier in pyatomic.h #121055
  • [3.13] gh-120593: Fix const qualifier in pyatomic.h (GH-121055) #121057
  • gh-120593: Check -Wcast-qual flag in test_cext #121081
  • gh-120593: Make _PyLong_CompactValue() parameter const again #122367

Activity

  1. ndparker
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jun 16, 2024
  2. picnixz commented on Jun 17, 2024

    @picnixz
    picnixz
    on Jun 17, 2024
    Member
    More actions

    For the record, this is not the only place where const qualifiers are lost. If you compile cpython with make CFLAGS=-Wcast-qual -j12 you end up with a lot of warnings. For the line 359, the fix would be:

    // pyatomic_gcc.h
    static inline void *
    _Py_atomic_load_ptr_relaxed(const void *obj)
    -{ return (void *)__atomic_load_n((void **)obj, __ATOMIC_RELAXED); }
    +{ return (void *)__atomic_load_n((void * const *)obj, __ATOMIC_RELAXED); }

    As you can see you lose constness twice, namely when calling __atomic_load_n and when returning the type. Note that we have

    // pyatomic_std.h
    static inline void *
    _Py_atomic_load_ptr_relaxed(const void *obj)
    {
        _Py_USING_STD;
        return atomic_load_explicit((const _Atomic(void*)*)obj,
                                    memory_order_relaxed);
    }

    As you can see, the cast here is essentially using a const void **, but still the return type should be a const as well (in the case of __atomic_load_n, the signature is type __atomic_load_n(type *ptr, int memorder);).

    EDIT: I edited my comment in order to reflect the real signature of _Py_atomic_load_ptr_relaxed in cpython. The return type is a void * and not a const void * but the inner cast fix remains correct.

  3. ndparker commented on Jun 17, 2024

    @ndparker
    ndparker
    on Jun 17, 2024
    ContributorAuthor
    More actions

    Yes, I'd like to clarify that this is specifically not about compiling CPython, it's about including Python.h only.

  4. picnixz commented on Jun 18, 2024

    @picnixz
    picnixz
    on Jun 18, 2024
    Member
    More actions

    Yes, I understood that from the other issue but since you only mentioned 2 lines, I wanted to 1) give a way to fix it 2) say that it's not just 2 lines that are affected but a lot of calls.

    Reacted by n.d. parker
  5. vstinner
    changed the title [-]Python.h with GCC discards const qualifier [/-] [+]pyatomic_gcc.h with GCC discards const qualifier [/+] on Jun 26, 2024
  6. vstinner
    added 2 commits that reference this issue on Jun 26, 2024

    pythongh-120593: Fix const qualified in pyatomic.h

    eb1e734

    pythongh-120593: Fix const qualifier in _PyLong_CompactValue()

    a742884
  7. vstinner
    added
    needs backport to 3.13only security fixesonly security fixes
    and removed
    needs backport to 3.13only security fixesonly security fixes
    on Jun 26, 2024
  8. vstinner
    added 2 commits that reference this issue on Jun 26, 2024

    pythongh-120593: Fix const qualifier in pyatomic.h

    03fd9d2

    gh-120593: Fix const qualifier in pyatomic.h (#121055)

    9cd2dcb
  9. miss-islington
    added a commit that references this issue on Jun 26, 2024

    pythongh-120593: Fix const qualifier in pyatomic.h (pythonGH-121055)

    2573edf
  10. vstinner
    added 5 commits that reference this issue on Jun 26, 2024

    gh-120593: Fix const qualifier in _PyLong_CompactValue() (#121053)

    e51e880

    [3.13] gh-120593: Fix const qualifier in pyatomic.h (GH-121055) (#121057

    64c4139

    pythongh-120593: Add -Wcast-qual flag to test_cext

    461e3fe

    pythongh-120593: Check -Wcast-qual flag in test_cext

    c558559

    gh-120593: Check -Wcast-qual flag in test_cext (#121081)

    b7a95df
  11. vstinner commented on Jun 27, 2024

    @vstinner
    vstinner
    on Jun 27, 2024
    Member
    More actions

    The warnings were fixed by 9cd2dcb and e51e880.

    I also added a check in test_cext for check for non-regression: b7a95df.

    Thanks @ndparker for your bug report. It's now fixed, I close the issue.

  12. vstinner
    closed this as completedon Jun 27, 2024
  13. mrahtz
    added 3 commits that reference this issue on Jun 30, 2024

    pythongh-120593: Fix const qualifier in pyatomic.h (python#121055)

    e182748

    pythongh-120593: Fix const qualifier in _PyLong_CompactValue() (pytho…

    f003fd6

    pythongh-120593: Check -Wcast-qual flag in test_cext (python#121081)

    c774a03
  14. markshannon commented on Jul 15, 2024

    @markshannon
    markshannon
    on Jul 15, 2024
    Member
    More actions

    I don't think weakening the type of calls that should be const is the correct thing to do here.
    Where there is a conflict in types we should be strengthening the type, not weakening it.
    Why not strengthen the types in the callees, rather than weakening them in the callers?

  15. estyxx
    added 3 commits that reference this issue on Jul 17, 2024

    pythongh-120593: Fix const qualifier in pyatomic.h (python#121055)

    76cb488

    pythongh-120593: Fix const qualifier in _PyLong_CompactValue() (pytho…

    bbf3d03

    pythongh-120593: Check -Wcast-qual flag in test_cext (python#121081)

    7f9992b
  16. vstinner
    added a commit that references this issue on Jul 27, 2024

    pythongh-120593: Make _PyLong_CompactValue() parameter const again

    8628469
  17. vstinner commented on Jul 27, 2024

    @vstinner
    vstinner
    on Jul 27, 2024
    Member
    More actions

    @markshannon:

    I don't think weakening the type of calls that should be const is the correct thing to do here.

    If you're talking about _PyLong_CompactValue(): I wrote PR gh-122367 to restore the const modifier.

    If you're talking about something else, please elaborate which function/code you are referring to.

  18. vstinner
    added a commit that references this issue on Jul 28, 2024

    gh-120593: Make _PyLong_CompactValue() parameter const again (#122367)

    b359f66
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.