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 — gh-99069: Consolidate checks for static_assert by jmroot · Pull Request #94766 · python/cpython

Conversation

@jmroot

@jmroot jmroot commented Jul 12, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Several platforms don't define the static_assert macro despite having
compiler support for the _Static_assert keyword. The macro needs to be
defined since it is used unconditionally in the Python code. So it
should always be safe to define it if undefined and not in C++11 (or
later) mode.

Hence, remove the checks for particular platforms or libc versions,
and just define static_assert anytime it needs to be defined but isn't.
That way, all platforms that need the fix will get it, regardless of
whether someone specifically thought of them.

Also document that certain macOS versions are among the platforms that
need this.

  • Issue: Build Python with C11 #91731
  • Issue: static_assert undefined (with C11 compiler) on macOS <= 10.10 #99069

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

arhadthedev reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-bot bedevere-bot added the awaiting review label Jul 12, 2022
@jmroot
jmroot force-pushed the static_assert branch from 2240f3b to 6accb7b Compare November 3, 2022 18:23
@bedevere-bot bedevere-bot mentioned this pull request Nov 3, 2022
Build Python with C11 #91731
Closed
@jmroot
jmroot force-pushed the static_assert branch from 6accb7b to 5422432 Compare November 3, 2022 18:40
@jmroot jmroot changed the title gh-91731: Consolidate checks for static_assert gh-99069: Consolidate checks for static_assert Nov 3, 2022
@bedevere-bot bedevere-bot mentioned this pull request Nov 3, 2022
static_assert undefined (with C11 compiler) on macOS <= 10.10 #99069
Closed
@corona10
corona10 requested a review from vstinner November 6, 2022 03:22
vstinner
vstinner reviewed Nov 7, 2022
View reviewed changes
Comment thread Include/pymacro.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jmroot

jmroot commented Nov 7, 2022

Copy link
Copy Markdown
Contributor Author

@vstinner Is there something that is defined iff Python itself is being built? Other users of the headers don't necessarily need static_assert, so it would be nice to avoid defining it as an unexpected side effect. I guess that would allow removing the __cplusplus checks too, since Python won't be built as C++?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Nov 7, 2022

Copy link
Copy Markdown
Member

Sadly, static_assert() is now part of the Python 3.11 C API. Removing it from its public C API in a 3.11.x bugfix release would be worse IMO.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Nov 7, 2022

Copy link
Copy Markdown
Member

In practice, which platform/libc/compiler is impacted by this change? It would be nice to add a NEWS entry to discuss which platforms are affected.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Nov 7, 2022

Copy link
Copy Markdown
Member

cc @pablogsal who modified this macro recently.

cc @ambv who may have an opinion on the macOS 10.10 support (issue #99069), purpose of this PR if I got it correctly

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pablogsal
pablogsal reviewed Nov 7, 2022
View reviewed changes
Comment thread Include/pymacro.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jmroot

jmroot commented Nov 7, 2022

Copy link
Copy Markdown
Contributor Author

In practice, which platform/libc/compiler is impacted by this change?

I only know of the ones mentioned in the comment, i.e. FreeBSD <= 12, macOS <= 10.10, and glibc < 2.16.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Nov 7, 2022

Copy link
Copy Markdown
Member

I was very excited to be able to get rid of Py_BUILD_ASSERT() "hack" with a well defined (!) static_assert() function of the C11 standard. But month after month, I see that: weeeeeell, it depends, maybe it's available, maybe not. It depends on the libc, on the compiler, on the OS, etc.

Maybe we should give up with static_assert() and use _Static_assert(). Coming from Python, for me it's weird to use a name starting with an underscore :-( It reminds me non-standard APIs on Windows which looks like POSIX but are different, like _open().

Maybe Py_BUILD_ASSERT() is just better since it's battle tested (in Python) and it "just works" :-) I didn't expect that it would be so complicated to get static_assert() on all compilers on all platforms in 2022 :-(

A tradeoff might be to only use static_assert() inside Python and remove it from the public Python C API. Then the funny part is that depending if the proper (hypothetical) Python internal header file is included or not, using static_assert() might work or not. The problem is that some Python macros use assert(), so Python.h includes <assert.h>. But as you can see, static_assert() may or may not be available, Python currently has a complicated macro to make sure that it's always present. What if an internal Python C file uses <Python.h> but forgets to include "pycore_assert.h"? Maybe it works on all platforms. Maybe it fails on some specific platforms. Fun. Isn't it? That's why I put it in the public C API.


"Well defined": people are now discussing static_assert() with a single argument. I'm curious when it will be standard and when all compilers on all platforms will support it :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmroot

jmroot commented Nov 20, 2022

Copy link
Copy Markdown
Contributor Author

@vstinner @pablogsal Is there a consensus for how you want to proceed here?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Nov 21, 2022

Copy link
Copy Markdown
Member

@pablogsal asked to add __has_feature(c_static_assert) check. I don't see this check in your PR yet. Does it mean that you are against using it, and if yes, why?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jmroot

jmroot commented Nov 21, 2022

Copy link
Copy Markdown
Contributor Author

@vstinner Sorry if I misinterpreted your comments above; it looked to me like you were disagreeing with @pablogsal's proposal.

My own view is that a clang-specific check is not necessary, because the existence of _Static_assert does not depend on the stdlib at all, and all versions of clang that accept -std=c11 or -std=c1x provide that keyword. Restricting the define to clang and gcc would be reasonable though, just in case other compilers do something weird.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jmroot added 4 commits November 21, 2022 22:53
@jmroot
pythongh-99069: Consolidate checks for static_assert
e75e612
Several platforms don't define the static_assert macro despite having
compiler support for the _Static_assert keyword. The macro needs to be
defined since it is used unconditionally in the Python code. So it
should always be safe to define it if undefined and not in C++11 (or
later) mode.

Hence, remove the checks for particular platforms or libc versions,
and just define static_assert anytime it needs to be defined but isn't.
That way, all platforms that need the fix will get it, regardless of
whether someone specifically thought of them.

Also document that certain macOS versions are among the platforms that
need this.
@jmroot
Workaround for MSVC
783604d
@jmroot
Indicate blank lines are part of the comment
08106d7
@jmroot
Restrict static_assert define to gcc/clang C11+
6c36647
@jmroot
jmroot force-pushed the static_assert branch from 65ae5c2 to 6c36647 Compare November 21, 2022 12:01
@jmroot

jmroot commented Nov 29, 2022

Copy link
Copy Markdown
Contributor Author

@pablogsal Is this acceptable now?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Nov 29, 2022

Copy link
Copy Markdown
Member

Yup, this looks good to me. Thanks for working on this!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner reviewed Dec 1, 2022
View reviewed changes
Comment thread Include/pymacro.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

@jmroot
Add more explanation of __STDC_VERSION__ check
ce67d71
vstinner
vstinner reviewed Dec 3, 2022
View reviewed changes
Comment thread Include/pymacro.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Update Include/pymacro.h
cdd52ce
@jmroot
jmroot requested a review from vstinner December 3, 2022 13:52
vstinner
vstinner reviewed Dec 5, 2022
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, it's strange to check for GNUC with clang. The first code was added for FreeBSD which uses clang.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Include/pymacro.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
Update Include/pymacro.h
a5ceb00
@jmroot
jmroot requested a review from pablogsal December 17, 2022 23:27
@jmroot

jmroot commented Jan 1, 2023

Copy link
Copy Markdown
Contributor Author

@vstinner What is still needed for this to be ready to merge?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303
kumaraditya303 requested a review from vstinner January 1, 2023 15:34
blurb-it Bot and others added 2 commits February 11, 2023 05:31
@blurb-it
📜🤖 Added by blurb_it.
57950a3
@jmroot
C2x compatibility for static_assert
3ca0a69
The C2x draft (currently expected to become C23) makes static_assert
a keyword to match C++. So only define the macro for up to C17.
@vstinner
vstinner merged commit 96e1901 into python:main Apr 5, 2023
@bedevere-bot bedevere-bot removed the awaiting review label Apr 5, 2023
@vstinner vstinner added the needs backport to 3.11 only security fixes label Apr 5, 2023
@miss-islington

miss-islington commented Apr 5, 2023

Copy link
Copy Markdown
Contributor

Thanks @jmroot for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 5, 2023
@jmroot @vstinner @miss-islington
pythongh-99069: Consolidate checks for static_assert (pythonGH-94766)
61c1055
Several platforms don't define the static_assert macro despite having
compiler support for the _Static_assert keyword. The macro needs to be
defined since it is used unconditionally in the Python code. So it
should always be safe to define it if undefined and not in C++11 (or
later) mode.

Hence, remove the checks for particular platforms or libc versions,
and just define static_assert anytime it needs to be defined but isn't.
That way, all platforms that need the fix will get it, regardless of
whether someone specifically thought of them.

Also document that certain macOS versions are among the platforms that
need this.

The C2x draft (currently expected to become C23) makes static_assert
a keyword to match C++. So only define the macro for up to C17.

(cherry picked from commit 96e1901)

Co-authored-by: Joshua Root <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@bedevere-bot

bedevere-bot commented Apr 5, 2023

Copy link
Copy Markdown

GH-103282 is a backport of this pull request to the 3.11 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Apr 5, 2023
@vstinner

vstinner commented Apr 5, 2023

Copy link
Copy Markdown
Member

Merged, thanks.

jmroot reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington added a commit that referenced this pull request Apr 5, 2023
@miss-islington @jmroot @vstinner
gh-99069: Consolidate checks for static_assert (GH-94766)
851e744
Several platforms don't define the static_assert macro despite having
compiler support for the _Static_assert keyword. The macro needs to be
defined since it is used unconditionally in the Python code. So it
should always be safe to define it if undefined and not in C++11 (or
later) mode.

Hence, remove the checks for particular platforms or libc versions,
and just define static_assert anytime it needs to be defined but isn't.
That way, all platforms that need the fix will get it, regardless of
whether someone specifically thought of them.

Also document that certain macOS versions are among the platforms that
need this.

The C2x draft (currently expected to become C23) makes static_assert
a keyword to match C++. So only define the macro for up to C17.

(cherry picked from commit 96e1901)

Co-authored-by: Joshua Root <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
@jmroot
jmroot deleted the static_assert branch April 5, 2023 18:53
gaogaotiantian pushed a commit to gaogaotiantian/cpython that referenced this pull request Apr 8, 2023
@jmroot @vstinner @gaogaotiantian
pythongh-99069: Consolidate checks for static_assert (python#94766)
7c02ae9
Several platforms don't define the static_assert macro despite having
compiler support for the _Static_assert keyword. The macro needs to be
defined since it is used unconditionally in the Python code. So it
should always be safe to define it if undefined and not in C++11 (or
later) mode.

Hence, remove the checks for particular platforms or libc versions,
and just define static_assert anytime it needs to be defined but isn't.
That way, all platforms that need the fix will get it, regardless of
whether someone specifically thought of them.

Also document that certain macOS versions are among the platforms that
need this.

The C2x draft (currently expected to become C23) makes static_assert
a keyword to match C++. So only define the macro for up to C17.

Co-authored-by: Victor Stinner <[email protected]>
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@jmroot @vstinner @warsaw
pythongh-99069: Consolidate checks for static_assert (python#94766)
5247f86
Several platforms don't define the static_assert macro despite having
compiler support for the _Static_assert keyword. The macro needs to be
defined since it is used unconditionally in the Python code. So it
should always be safe to define it if undefined and not in C++11 (or
later) mode.

Hence, remove the checks for particular platforms or libc versions,
and just define static_assert anytime it needs to be defined but isn't.
That way, all platforms that need the fix will get it, regardless of
whether someone specifically thought of them.

Also document that certain macOS versions are among the platforms that
need this.

The C2x draft (currently expected to become C23) makes static_assert
a keyword to match C++. So only define the macro for up to C17.

Co-authored-by: Victor Stinner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@pablogsal pablogsal Awaiting requested review from pablogsal
@vstinner vstinner Awaiting requested review from vstinner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@jmroot @vstinner @pablogsal @miss-islington @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.