bpo-44698: Fix undefined behaviour in complex exponentiation. (GH-27278) (#27366)

(cherry picked from commit 1d582bbc969e05896addf97844ddf17ce9830e5e)

Co-authored-by: T. Wouters <thomas@python.org>
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 91e06a8..05cae32 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -514,8 +514,6 @@ static PyObject *
 complex_pow(PyObject *v, PyObject *w, PyObject *z)
 {
     Py_complex p;
-    Py_complex exponent;
-    long int_exponent;
     Py_complex a, b;
     TO_COMPLEX(v, a);
     TO_COMPLEX(w, b);
@@ -525,12 +523,21 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
         return NULL;
     }
     errno = 0;
-    exponent = b;
-    int_exponent = (long)exponent.real;
-    if (exponent.imag == 0. && exponent.real == int_exponent)
-        p = c_powi(a, int_exponent);
-    else
-        p = _Py_c_pow(a, exponent);
+    // Check if w is an integer value that fits inside a C long, so we can
+    // use a faster algorithm. TO_COMPLEX(w, b), above, already handled the
+    // conversion from larger longs, as well as other types.
+    if (PyLong_Check(w)) {
+        int overflow = 0;
+        long int_exponent = PyLong_AsLongAndOverflow(w, &overflow);
+        if (int_exponent == -1 && PyErr_Occurred())
+            return NULL;
+        if (overflow == 0)
+            p = c_powi(a, int_exponent);
+        else
+            p = _Py_c_pow(a, b);
+    } else {
+        p = _Py_c_pow(a, b);
+    }
 
     Py_ADJUST_ERANGE2(p.real, p.imag);
     if (errno == EDOM) {