]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-141004: soft-deprecate Py_INFINITY macro (#141033)
authorSergey B Kirpichev <skirpichev@gmail.com>
Wed, 12 Nov 2025 12:44:49 +0000 (15:44 +0300)
committerGitHub <noreply@github.com>
Wed, 12 Nov 2025 12:44:49 +0000 (13:44 +0100)
Co-authored-by: Victor Stinner <vstinner@python.org>
13 files changed:
Doc/c-api/conversion.rst
Doc/c-api/float.rst
Doc/whatsnew/3.14.rst
Doc/whatsnew/3.15.rst
Include/floatobject.h
Include/internal/pycore_pymath.h
Include/pymath.h
Misc/NEWS.d/next/C_API/2025-11-05-04-38-16.gh-issue-141004.rJL43P.rst [new file with mode: 0644]
Modules/cmathmodule.c
Modules/mathmodule.c
Objects/complexobject.c
Objects/floatobject.c
Python/pystrtod.c

index 533e5460da895205c876452e0a4de22ff7e6b2ff..a18bbf4e0e37d76f98dac1a9443a11dbc66a3817 100644 (file)
@@ -105,7 +105,7 @@ The following functions provide locale-independent string to number conversions.
 
    If ``s`` represents a value that is too large to store in a float
    (for example, ``"1e500"`` is such a string on many platforms) then
-   if ``overflow_exception`` is ``NULL`` return ``Py_INFINITY`` (with
+   if ``overflow_exception`` is ``NULL`` return :c:macro:`!INFINITY` (with
    an appropriate sign) and don't set any exception.  Otherwise,
    ``overflow_exception`` must point to a Python exception object;
    raise that exception and return ``-1.0``.  In both cases, set
index eae4792af7d2999901e1aaf0dda06fac3b316a00..b6020533a2b9d949cc10ad9e6aad4a4e3fa079a0 100644 (file)
@@ -83,8 +83,11 @@ Floating-Point Objects
    This macro expands a to constant expression of type :c:expr:`double`, that
    represents the positive infinity.
 
-   On most platforms, this is equivalent to the :c:macro:`!INFINITY` macro from
-   the C11 standard ``<math.h>`` header.
+   It is equivalent to the :c:macro:`!INFINITY` macro from the C11 standard
+   ``<math.h>`` header.
+
+   .. deprecated:: 3.15
+      The macro is soft deprecated.
 
 
 .. c:macro:: Py_NAN
index 1a2fbda0c4ce8136d1164eb027e36db9574b469f..9459b73bcb502faddfbdb37ded8e5c3821e9da78 100644 (file)
@@ -3045,7 +3045,7 @@ Deprecated C APIs
 -----------------
 
 * The :c:macro:`!Py_HUGE_VAL` macro is now :term:`soft deprecated`.
-  Use :c:macro:`!Py_INFINITY` instead.
+  Use :c:macro:`!INFINITY` instead.
   (Contributed by Sergey B Kirpichev in :gh:`120026`.)
 
 * The :c:macro:`!Py_IS_NAN`, :c:macro:`!Py_IS_INFINITY`,
index c543b6e6c2a7794242f9fbd63211e140401d0455..f0fd49c9033bc15541e2c29ea1379e737dbadeaf 100644 (file)
@@ -1095,6 +1095,10 @@ Deprecated C APIs
   since 3.15 and will be removed in 3.17.
   (Contributed by Nikita Sobolev in :gh:`136355`.)
 
+* :c:macro:`!Py_INFINITY` macro is :term:`soft deprecated`,
+  use the C11 standard ``<math.h>`` :c:macro:`!INFINITY` instead.
+  (Contributed by Sergey B Kirpichev in :gh:`141004`.)
+
 * :c:macro:`!Py_MATH_El` and :c:macro:`!Py_MATH_PIl` are deprecated
   since 3.15 and will be removed in 3.20.
   (Contributed by Sergey B Kirpichev in :gh:`141004`.)
index 4d24a76edd5de1bd88b901f27f624c75ef4f861c..814337b070ab50a0755e4007bbd887b61cb58e4e 100644 (file)
@@ -18,14 +18,14 @@ PyAPI_DATA(PyTypeObject) PyFloat_Type;
 
 #define Py_RETURN_NAN return PyFloat_FromDouble(Py_NAN)
 
-#define Py_RETURN_INF(sign)                          \
-    do {                                             \
-        if (copysign(1., sign) == 1.) {              \
-            return PyFloat_FromDouble(Py_INFINITY);  \
-        }                                            \
-        else {                                       \
-            return PyFloat_FromDouble(-Py_INFINITY); \
-        }                                            \
+#define Py_RETURN_INF(sign)                       \
+    do {                                          \
+        if (copysign(1., sign) == 1.) {           \
+            return PyFloat_FromDouble(INFINITY);  \
+        }                                         \
+        else {                                    \
+            return PyFloat_FromDouble(-INFINITY); \
+        }                                         \
     } while(0)
 
 PyAPI_FUNC(double) PyFloat_GetMax(void);
index eea8996ba68ca0868ab0dfda35ee7c2e7e037053..4fcac3aab8bf51c8efb21a0c227e6c6cdab1be41 100644 (file)
@@ -33,7 +33,7 @@ extern "C" {
 static inline void _Py_ADJUST_ERANGE1(double x)
 {
     if (errno == 0) {
-        if (x == Py_INFINITY || x == -Py_INFINITY) {
+        if (x == INFINITY || x == -INFINITY) {
             errno = ERANGE;
         }
     }
@@ -44,8 +44,8 @@ static inline void _Py_ADJUST_ERANGE1(double x)
 
 static inline void _Py_ADJUST_ERANGE2(double x, double y)
 {
-    if (x == Py_INFINITY || x == -Py_INFINITY ||
-        y == Py_INFINITY || y == -Py_INFINITY)
+    if (x == INFINITY || x == -INFINITY ||
+        y == INFINITY || y == -INFINITY)
     {
         if (errno == 0) {
             errno = ERANGE;
index 0f9f0f3b2990fe59af737b6edfbc845a486f4f08..7cfe441365df78d491220d54db06252cec131c24 100644 (file)
 #define Py_IS_FINITE(X) isfinite(X)
 
 // Py_INFINITY: Value that evaluates to a positive double infinity.
+// Soft deprecated since Python 3.15, use INFINITY instead.
 #ifndef Py_INFINITY
 #  define Py_INFINITY ((double)INFINITY)
 #endif
 
 /* Py_HUGE_VAL should always be the same as Py_INFINITY.  But historically
  * this was not reliable and Python did not require IEEE floats and C99
- * conformity.  The macro was soft deprecated in Python 3.14, use Py_INFINITY instead.
+ * conformity.  The macro was soft deprecated in Python 3.14, use INFINITY instead.
  */
 #ifndef Py_HUGE_VAL
 #  define Py_HUGE_VAL HUGE_VAL
diff --git a/Misc/NEWS.d/next/C_API/2025-11-05-04-38-16.gh-issue-141004.rJL43P.rst b/Misc/NEWS.d/next/C_API/2025-11-05-04-38-16.gh-issue-141004.rJL43P.rst
new file mode 100644 (file)
index 0000000..a054f8e
--- /dev/null
@@ -0,0 +1 @@
+The :c:macro:`!Py_INFINITY` macro is :term:`soft deprecated`.
index a4ea5557a6a415cc09edc36a0aaa9733345098d8..aee3e4f343d8be4a3213bff913dde747b6459fc4 100644 (file)
@@ -150,7 +150,7 @@ special_type(double d)
 #define P14 0.25*Py_MATH_PI
 #define P12 0.5*Py_MATH_PI
 #define P34 0.75*Py_MATH_PI
-#define INF Py_INFINITY
+#define INF INFINITY
 #define N Py_NAN
 #define U -9.5426319407711027e33 /* unlikely value, used as placeholder */
 
@@ -1186,11 +1186,11 @@ cmath_exec(PyObject *mod)
     if (PyModule_Add(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
         return -1;
     }
-    if (PyModule_Add(mod, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
+    if (PyModule_Add(mod, "inf", PyFloat_FromDouble(INFINITY)) < 0) {
         return -1;
     }
 
-    Py_complex infj = {0.0, Py_INFINITY};
+    Py_complex infj = {0.0, INFINITY};
     if (PyModule_Add(mod, "infj", PyComplex_FromCComplex(infj)) < 0) {
         return -1;
     }
index de1886451eda8f7423d2b7eb3e922c9719f5dc31..11c46c987e146a6b383128839821b84d6fe682e1 100644 (file)
@@ -395,7 +395,7 @@ m_tgamma(double x)
     if (x == 0.0) {
         errno = EDOM;
         /* tgamma(+-0.0) = +-inf, divide-by-zero */
-        return copysign(Py_INFINITY, x);
+        return copysign(INFINITY, x);
     }
 
     /* integer arguments */
@@ -426,7 +426,7 @@ m_tgamma(double x)
         }
         else {
             errno = ERANGE;
-            return Py_INFINITY;
+            return INFINITY;
         }
     }
 
@@ -490,14 +490,14 @@ m_lgamma(double x)
         if (isnan(x))
             return x;  /* lgamma(nan) = nan */
         else
-            return Py_INFINITY; /* lgamma(+-inf) = +inf */
+            return INFINITY; /* lgamma(+-inf) = +inf */
     }
 
     /* integer arguments */
     if (x == floor(x) && x <= 2.0) {
         if (x <= 0.0) {
             errno = EDOM;  /* lgamma(n) = inf, divide-by-zero for */
-            return Py_INFINITY; /* integers n <= 0 */
+            return INFINITY; /* integers n <= 0 */
         }
         else {
             return 0.0; /* lgamma(1) = lgamma(2) = 0.0 */
@@ -633,7 +633,7 @@ m_log(double x)
             return log(x);
         errno = EDOM;
         if (x == 0.0)
-            return -Py_INFINITY; /* log(0) = -inf */
+            return -INFINITY; /* log(0) = -inf */
         else
             return Py_NAN; /* log(-ve) = nan */
     }
@@ -676,7 +676,7 @@ m_log2(double x)
     }
     else if (x == 0.0) {
         errno = EDOM;
-        return -Py_INFINITY; /* log2(0) = -inf, divide-by-zero */
+        return -INFINITY; /* log2(0) = -inf, divide-by-zero */
     }
     else {
         errno = EDOM;
@@ -692,7 +692,7 @@ m_log10(double x)
             return log10(x);
         errno = EDOM;
         if (x == 0.0)
-            return -Py_INFINITY; /* log10(0) = -inf */
+            return -INFINITY; /* log10(0) = -inf */
         else
             return Py_NAN; /* log10(-ve) = nan */
     }
@@ -1500,7 +1500,7 @@ math_ldexp_impl(PyObject *module, double x, PyObject *i)
         errno = 0;
     } else if (exp > INT_MAX) {
         /* overflow */
-        r = copysign(Py_INFINITY, x);
+        r = copysign(INFINITY, x);
         errno = ERANGE;
     } else if (exp < INT_MIN) {
         /* underflow to +-0 */
@@ -2983,7 +2983,7 @@ math_ulp_impl(PyObject *module, double x)
     if (isinf(x)) {
         return x;
     }
-    double inf = Py_INFINITY;
+    double inf = INFINITY;
     double x2 = nextafter(x, inf);
     if (isinf(x2)) {
         /* special case: x is the largest positive representable float */
@@ -3007,7 +3007,7 @@ math_exec(PyObject *module)
     if (PyModule_Add(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
         return -1;
     }
-    if (PyModule_Add(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
+    if (PyModule_Add(module, "inf", PyFloat_FromDouble(INFINITY)) < 0) {
         return -1;
     }
     if (PyModule_Add(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
index 6247376a0e68f5ea16ffd082564b4701d42199d0..3612c2699a557db37361b7a781e877b81caf8c4d 100644 (file)
@@ -139,8 +139,8 @@ _Py_c_prod(Py_complex z, Py_complex w)
             recalc = 1;
         }
         if (recalc) {
-            r.real = Py_INFINITY*(a*c - b*d);
-            r.imag = Py_INFINITY*(a*d + b*c);
+            r.real = INFINITY*(a*c - b*d);
+            r.imag = INFINITY*(a*d + b*c);
         }
     }
 
@@ -229,8 +229,8 @@ _Py_c_quot(Py_complex a, Py_complex b)
         {
             const double x = copysign(isinf(a.real) ? 1.0 : 0.0, a.real);
             const double y = copysign(isinf(a.imag) ? 1.0 : 0.0, a.imag);
-            r.real = Py_INFINITY * (x*b.real + y*b.imag);
-            r.imag = Py_INFINITY * (y*b.real - x*b.imag);
+            r.real = INFINITY * (x*b.real + y*b.imag);
+            r.imag = INFINITY * (y*b.real - x*b.imag);
         }
         else if ((isinf(abs_breal) || isinf(abs_bimag))
                  && isfinite(a.real) && isfinite(a.imag))
index ef613efe4e7f445e74e9e1467d46cb34ea0eb711..78006783c6ec782aedb595699e8f6afc583fb666 100644 (file)
@@ -2415,7 +2415,7 @@ PyFloat_Unpack2(const char *data, int le)
     if (e == 0x1f) {
         if (f == 0) {
             /* Infinity */
-            return sign ? -Py_INFINITY : Py_INFINITY;
+            return sign ? -INFINITY : INFINITY;
         }
         else {
             /* NaN */
index 7b74f613ed563b56ddce89e10da63596191d7ba8..e8aca939d1fb98ca8342dfd91e2f3be10b57e4e9 100644 (file)
@@ -43,7 +43,7 @@ _Py_parse_inf_or_nan(const char *p, char **endptr)
         s += 3;
         if (case_insensitive_match(s, "inity"))
             s += 5;
-        retval = negate ? -Py_INFINITY : Py_INFINITY;
+        retval = negate ? -INFINITY : INFINITY;
     }
     else if (case_insensitive_match(s, "nan")) {
         s += 3;
@@ -286,7 +286,7 @@ _PyOS_ascii_strtod(const char *nptr, char **endptr)
    string, -1.0 is returned and again ValueError is raised.
 
    On overflow (e.g., when trying to convert '1e500' on an IEEE 754 machine),
-   if overflow_exception is NULL then +-Py_INFINITY is returned, and no Python
+   if overflow_exception is NULL then +-INFINITY is returned, and no Python
    exception is raised.  Otherwise, overflow_exception should point to
    a Python exception, this exception will be raised, -1.0 will be returned,
    and *endptr will point just past the end of the converted value.