Skip to content

gh-91731: Replace Py_BUILD_ASSERT() with static_assert() - #91730

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:static_assert
Apr 20, 2022
Merged

vstinner merged 1 commit into
python:mainfrom
vstinner:static_assert

Conversation

@vstinner

Copy link
Copy Markdown
Member

Python 3.11 now uses C11 standard which adds static_assert()
to <assert.h>.

  • In pytime.c, replace Py_BUILD_ASSERT() with preprocessor checks on
    SIZEOF_TIME_T with #error.
  • On macOS, py_mach_timebase_info() now accepts timebase members with
    the same size than _PyTime_t.
  • py_get_monotonic_clock() now saturates GetTickCount64() to
    _PyTime_MAX: GetTickCount64() is unsigned, whereas _PyTime_t is
    signed.
@vstinner

Copy link
Copy Markdown
Member Author

Failure on macOS:

gcc -c (...) -std=c99 (...) -o Objects/longobject.o Objects/longobject.c
(...)
Objects/longobject.c:5653:5: error: implicit declaration of function 'static_assert' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    static_assert(PyLong_SHIFT <= 32, "digit is larger than uint32_t");

Oh, Python is still built with -std=c99, whereas we switched to C11 in PEP 7.

@vstinner

Copy link
Copy Markdown
Member Author

I will first merge #91733 which replaces -std=c99 with -std=c11.

Python 3.11 now uses C11 standard which adds static_assert()
to <assert.h>.

* In pytime.c, replace Py_BUILD_ASSERT() with preprocessor checks on
  SIZEOF_TIME_T with #error.
* On macOS, py_mach_timebase_info() now accepts timebase members with
  the same size than _PyTime_t.
* py_get_monotonic_clock() now saturates GetTickCount64() to
  _PyTime_MAX: GetTickCount64() is unsigned, whereas _PyTime_t is
  signed.
@vstinner vstinner changed the title Replace Py_BUILD_ASSERT() with static_assert() Apr 20, 2022
@vstinner vstinner added 🔨 test-with-buildbots Test PR w/ buildbots; report in status section and removed skip issue labels Apr 20, 2022
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @vstinner for commit 2a32c43 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 20, 2022
@vstinner

Copy link
Copy Markdown
Member Author

I scheduled buildbot jobs to check how it goes on other platforms/C compilers.

@vstinner

Copy link
Copy Markdown
Member Author

buildbot/AMD64 Fedora Stable Refleaks PR — Build done.

This is unrelated to my PR. The leak is known in the main branch.

@vstinner

Copy link
Copy Markdown
Member Author

Python was built successfully on various versions of Windows, macOS, Ubuntu and FreeBSD on various architectures. I confirm that it confirms that C11 is now well supported. If a platform doesn't support C11, it can reimplement static_assert() with Py_BUILD_ASSERT() or get a C11 compiler :-) I merge my PR.

@vstinner

Copy link
Copy Markdown
Member Author

This is unrelated to my PR. The leak is known in the main branch.

See #91757

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

2 participants