From 4ca3e9a41ec6fd3afdc6513ada09f04caccb100a Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 14 Jun 2024 12:39:20 +0300 Subject: [PATCH] from 119771: adjust errno on overflows in _Py_c_pow() Before we did this in complex_pow() and behaviour of the public C API function ``_Py_c_pow()`` was different from the pure-python pow(). --- Doc/c-api/complex.rst | 2 ++ Lib/test/test_capi/test_complex.py | 5 ++++- Objects/complexobject.c | 4 +++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Doc/c-api/complex.rst b/Doc/c-api/complex.rst index 5a0474869071d94..8aa1599e7f4b7a8 100644 --- a/Doc/c-api/complex.rst +++ b/Doc/c-api/complex.rst @@ -75,6 +75,8 @@ pointers. This is consistent throughout the API. If *num* is null and *exp* is not a positive real number, this method returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`. + Set :c:data:`errno` to :c:macro:`!ERANGE` on overflows. + Complex Numbers as Python Objects ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/Lib/test/test_capi/test_complex.py b/Lib/test/test_capi/test_complex.py index 58743053a657593..5e9b1fe77d49d37 100644 --- a/Lib/test/test_capi/test_complex.py +++ b/Lib/test/test_capi/test_complex.py @@ -229,7 +229,10 @@ def test_py_c_pow(self): self.assertEqual(_py_c_pow(0j, -1)[1], errno.EDOM) self.assertEqual(_py_c_pow(0j, 1j)[1], errno.EDOM) - self.assertEqual(_py_c_pow(*[DBL_MAX+1j]*2)[0], complex(*[INF]*2)) + self.assertEqual(_py_c_pow(*[DBL_MAX+1j]*2), + (complex(*[INF]*2), errno.ERANGE)) + self.assertEqual(_py_c_pow(DBL_MAX+1j, 2), + (complex(*[INF]*2), errno.ERANGE)) def test_py_c_abs(self): # Test _Py_c_abs() diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 9a57d956d3bc193..97b08fa203d5878 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -278,6 +278,8 @@ _Py_c_pow(Py_complex a, Py_complex b) } r.real = len*cos(phase); r.imag = len*sin(phase); + + _Py_ADJUST_ERANGE2(r.real, r.imag); } return r; } @@ -773,12 +775,12 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z) // a faster and more accurate algorithm. if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= 100.0) { p = c_powi(a, (long)b.real); + _Py_ADJUST_ERANGE2(p.real, p.imag); } else { p = _Py_c_pow(a, b); } - _Py_ADJUST_ERANGE2(p.real, p.imag); if (errno == EDOM) { PyErr_SetString(PyExc_ZeroDivisionError, "zero to a negative or complex power");