https://github.com/python/cpython/commit/8a284e189673582e262744618f293f9901a32e49
commit: 8a284e189673582e262744618f293f9901a32e49
branch: main
author: Sergey B Kirpichev <[email protected]>
committer: vstinner <[email protected]>
date: 2024-09-18T10:39:11+02:00
summary:
gh-119771: Set errno on overflows in _Py_c_pow() (#120256)
Before we did this in complex_pow() and behavior of the public C API
function _Py_c_pow() was different from the pure-python pow().
files:
A Misc/NEWS.d/next/C API/2024-06-08-08-33-40.gh-issue-119771.Oip2dL.rst
M Doc/c-api/complex.rst
M Lib/test/test_capi/test_complex.py
M Objects/complexobject.c
diff --git a/Doc/c-api/complex.rst b/Doc/c-api/complex.rst
index 67d0c5f144e075..16bd79475dc1e6 100644
--- a/Doc/c-api/complex.rst
+++ b/Doc/c-api/complex.rst
@@ -79,6 +79,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 328ea12f97462c..368edfbf2ce97e 100644
--- a/Lib/test/test_capi/test_complex.py
+++ b/Lib/test/test_capi/test_complex.py
@@ -226,7 +226,11 @@ 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))
+ max_num = DBL_MAX+1j
+ self.assertEqual(_py_c_pow(max_num, max_num),
+ (complex(INF, INF), errno.ERANGE))
+ self.assertEqual(_py_c_pow(max_num, 2),
+ (complex(INF, INF), errno.ERANGE))
def test_py_c_abs(self):
diff --git a/Misc/NEWS.d/next/C
API/2024-06-08-08-33-40.gh-issue-119771.Oip2dL.rst b/Misc/NEWS.d/next/C
API/2024-06-08-08-33-40.gh-issue-119771.Oip2dL.rst
new file mode 100644
index 00000000000000..61619082487c3b
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2024-06-08-08-33-40.gh-issue-119771.Oip2dL.rst
@@ -0,0 +1,2 @@
+Set :data:`errno` in :c:func:`_Py_c_pow` on overflows. Patch by Sergey B
+Kirpichev.
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 4a8dac6c53f529..787235c63a6be1 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -173,6 +173,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;
}
@@ -567,12 +569,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");
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]