diff --git a/Include/pymacro.h b/Include/pymacro.h index f31c9505a6a812b..7ecce44a0d2a428 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -107,40 +107,15 @@ # endif #endif -#if ((defined(__GNUC__) || defined(__clang__)) \ - && defined(_Py_TYPEOF) && !defined(__cplusplus)) - // Implement Py_MIN(), Py_MAX() and Py_ABS() using _Py_TYPEOF() and - // statement expression to only evaluate each argument only once. - // It cannot be used in C++: ISO C++ forbids braced-groups within - // expressions. Statement expression is a GNU extension. Use __extension__ - // to avoid compiler warning in pedantic mode. - /* Minimum value between x and y */ -# define Py_MIN(x, y) \ - __extension__ \ - ({ _Py_TYPEOF (x) _x = (x); \ - _Py_TYPEOF (y) _y = (y); \ - _x < _y ? _x : _y; }) - /* Maximum value between x and y */ -# define Py_MAX(x, y) \ - __extension__ \ - ({ _Py_TYPEOF (x) _x = (x); \ - _Py_TYPEOF (y) _y = (y); \ - _x > _y ? _x : _y; }) - /* Absolute value of the number x */ -# define Py_ABS(x) \ - __extension__ \ - ({ _Py_TYPEOF (x) _x = (x); \ - _x < 0 ? -_x : _x; }) -#else - /* Minimum value between x and y */ -# define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) - /* Maximum value between x and y */ -# define Py_MAX(x, y) (((x) > (y)) ? (x) : (y)) - /* Absolute value of the number x */ -# define Py_ABS(x) ((x) < 0 ? -(x) : (x)) -#endif +/* Minimum value between x and y */ +#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x)) + +/* Maximum value between x and y */ +#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y)) +/* Absolute value of the number x */ +#define Py_ABS(x) ((x) < 0 ? -(x) : (x)) /* Safer implementation that avoids an undefined behavior for the minimal value of the signed integer type if its absolute value is larger than the maximal value of the signed integer type (in the two's complement diff --git a/Misc/NEWS.d/next/C_API/2026-09-14-16-10-23.gh-issue-157495.LzMLtE.rst b/Misc/NEWS.d/next/C_API/2026-09-14-16-10-23.gh-issue-157495.LzMLtE.rst deleted file mode 100644 index 0e89171bba86579..000000000000000 --- a/Misc/NEWS.d/next/C_API/2026-09-14-16-10-23.gh-issue-157495.LzMLtE.rst +++ /dev/null @@ -1,3 +0,0 @@ -If ``typeof()`` is available, implement :c:macro:`Py_MIN`, :c:macro:`Py_MAX` -and :c:macro:`Py_ABS` using ``typeof()`` and statement expression to only -evaluate each argument once. Patch by Victor Stinner. diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 19c02e6774e3d48..6f26369d3a8aaf6 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2050,17 +2050,6 @@ test_macros(PyObject *self, PyObject *Py_UNUSED(args)) assert(Py_MAX(5, 11) == 11); assert(Py_ABS(-5) == 5); -#if ((defined(__GNUC__) || defined(__clang__)) \ - && defined(_Py_TYPEOF) && !defined(__cplusplus)) - // When _Py_TYPEOF() is available, arguments are only evaluated once - int x = 5, y = 11; - assert(Py_MIN(++x, ++y) == 6); - x = 5; y = 11; - assert(Py_MAX(++x, ++y) == 12); - x = -5; - assert(Py_ABS(--x) == 6); -#endif - // Py_STRINGIFY() assert(strcmp(Py_STRINGIFY(123), "123") == 0);