Skip to content

Commit f759308

Browse files
committed
Restore constant-expression support in Py_MIN, Py_MAX, and Py_ABS
1 parent 1a85213 commit f759308

3 files changed

Lines changed: 7 additions & 46 deletions

File tree

‎Include/pymacro.h‎

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -107,40 +107,15 @@
107107
# endif
108108
#endif
109109

110-
#if ((defined(__GNUC__) || defined(__clang__)) \
111-
&& defined(_Py_TYPEOF) && !defined(__cplusplus))
112-
// Implement Py_MIN(), Py_MAX() and Py_ABS() using _Py_TYPEOF() and
113-
// statement expression to only evaluate each argument only once.
114-
// It cannot be used in C++: ISO C++ forbids braced-groups within
115-
// expressions. Statement expression is a GNU extension. Use __extension__
116-
// to avoid compiler warning in pedantic mode.
117110

118-
/* Minimum value between x and y */
119-
# define Py_MIN(x, y) \
120-
__extension__ \
121-
({ _Py_TYPEOF (x) _x = (x); \
122-
_Py_TYPEOF (y) _y = (y); \
123-
_x < _y ? _x : _y; })
124-
/* Maximum value between x and y */
125-
# define Py_MAX(x, y) \
126-
__extension__ \
127-
({ _Py_TYPEOF (x) _x = (x); \
128-
_Py_TYPEOF (y) _y = (y); \
129-
_x > _y ? _x : _y; })
130-
/* Absolute value of the number x */
131-
# define Py_ABS(x) \
132-
__extension__ \
133-
({ _Py_TYPEOF (x) _x = (x); \
134-
_x < 0 ? -_x : _x; })
135-
#else
136-
/* Minimum value between x and y */
137-
# define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
138-
/* Maximum value between x and y */
139-
# define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
140-
/* Absolute value of the number x */
141-
# define Py_ABS(x) ((x) < 0 ? -(x) : (x))
142-
#endif
111+
/* Minimum value between x and y */
112+
#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
113+
114+
/* Maximum value between x and y */
115+
#define Py_MAX(x, y) (((x) > (y)) ? (x) : (y))
143116

117+
/* Absolute value of the number x */
118+
#define Py_ABS(x) ((x) < 0 ? -(x) : (x))
144119
/* Safer implementation that avoids an undefined behavior for the minimal
145120
value of the signed integer type if its absolute value is larger than
146121
the maximal value of the signed integer type (in the two's complement

‎Misc/NEWS.d/next/C_API/2026-09-14-16-10-23.gh-issue-157495.LzMLtE.rst‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎Modules/_testcapimodule.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,17 +2050,6 @@ test_macros(PyObject *self, PyObject *Py_UNUSED(args))
20502050
assert(Py_MAX(5, 11) == 11);
20512051
assert(Py_ABS(-5) == 5);
20522052

2053-
#if ((defined(__GNUC__) || defined(__clang__)) \
2054-
&& defined(_Py_TYPEOF) && !defined(__cplusplus))
2055-
// When _Py_TYPEOF() is available, arguments are only evaluated once
2056-
int x = 5, y = 11;
2057-
assert(Py_MIN(++x, ++y) == 6);
2058-
x = 5; y = 11;
2059-
assert(Py_MAX(++x, ++y) == 12);
2060-
x = -5;
2061-
assert(Py_ABS(--x) == 6);
2062-
#endif
2063-
20642053
// Py_STRINGIFY()
20652054
assert(strcmp(Py_STRINGIFY(123), "123") == 0);
20662055

0 commit comments

Comments
 (0)