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 — C API: Implement Py_MIN(), Py_MAX() and Py_ABS() macros using typeof() · Issue #157495 · python/cpython

C API: Implement Py_MIN(), Py_MAX() and Py_ABS() macros using typeof() #157495

New issue
Copy link
New issue
Copy link
Closed
Closed
C API: Implement Py_MIN(), Py_MAX() and Py_ABS() macros using typeof()#157495
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)

Description

@vstinner
vstinner
opened on Sep 14, 2026
Issue body actions

Currently, arguments of Py_MIN(), Py_MAX() and Py_ABS() macros are evaluated twice. It's inefficient. We can do better using typeof(). I propose adding a typeof() implementation for these macros.

See also:

  • GCC typeof: https://gcc.gnu.org/onlinedocs/gcc/Typeof.html
  • n3543 "Add type-safe minimum and maximum type-generic macros" proposition for the C standard.

Linked PRs

  • gh-157495: Implement Py_MIN() and Py_MAX() using typeof() #157496
  • gh-157495: Avoid Py_MIN()/Py_MAX() side effects #157591

Activity

  1. vstinner
    added
    topic-C-API
    on Sep 14, 2026
  2. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Sep 14, 2026
  3. StanFromIreland
    added
    type-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)
    and removed
    type-featureA feature request or enhancementA feature request or enhancement
    on Sep 14, 2026
  4. serhiy-storchaka commented on Sep 14, 2026

    @serhiy-storchaka
    serhiy-storchaka
    on Sep 14, 2026
    Member
    More actions

    How would it work in C++?

  5. vstinner commented on Sep 14, 2026

    @vstinner
    vstinner
    on Sep 14, 2026
    MemberAuthor
    More actions

    How would it work in C++?

    The current implement (without typeof) is used for C++.

    Statement expressions are not allowed in C++. My PR adds explicit tests on Py_MIN() and Py_MAX() in test_cppext: so the macros are tested in C++ with different compiler flags, with and without the limited C API.

    typeof() cannot be used on MSVC neither because statement expression is a GNU extension which is only supported by GCC and clang (not by MSVC).

  6. skirpichev commented on Sep 15, 2026

    @skirpichev
    skirpichev
    on Sep 15, 2026
    Member
    More actions

    Currently, arguments of Py_MIN(), Py_MAX() and Py_ABS() macros are evaluated twice. It's inefficient.

    Does it matter for default builds, when optimizations turned on?

  7. vstinner commented on Sep 15, 2026

    @vstinner
    vstinner
    on Sep 15, 2026
    MemberAuthor
    More actions

    The documentation of Py_MIN(), Py_MAX() and Py_ABS() macros says:

    The argument may be evaluated more than once. Consequently, do not pass an expression with side-effects directly to this macro.

    min=Py_MIN(var, expensive_func()) should be replaced with result=expensive_func(); min=Py_MIN(var, result) for example.

    Since my proposal change is only available on some compilers (GCC and clang, not on MSVC), Python code base should continue to follow the documentation advices.

    I didn't check the 221 calls to these macros (git grep -E '(Py_MIN|Py_MAX|Py_ABS)\('|wc -l).

  8. vstinner
    added a commit that references this issue on Sep 15, 2026

    pythongh-157495: Avoid Py_MIN()/Py_MAX() side effects

    e70baf3
  9. vstinner commented on Sep 15, 2026

    @vstinner
    vstinner
    on Sep 15, 2026
    MemberAuthor
    More actions

    I didn't check the 221 calls to these macros (git grep -E '(Py_MIN|Py_MAX|Py_ABS)('|wc -l).

    Ok, I checked Py_MIN/MAX/ABS calls in the Python code base and I found a few calls which are inefficient. I wrote this PR gh-157591 to fix them.

  10. vstinner
    added 2 commits that reference this issue on Sep 15, 2026

    gh-157495: Implement Py_MIN/MAX/ABS using typeof() (#157496)

    e2ca4dc

    gh-157495: Avoid Py_MIN()/Py_MAX() side effects (#157591)

    ba91215
  11. vstinner
    closed this as completedon Sep 18, 2026
  12. ZeroIntensity
    added a commit that references this issue on Nov 12, 2026

    [3.15] gh-161712: Read the clock once in `ThreadHandle_join()` (GH-16…

    88d137a
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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)

    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.