Skip to content

bpo-38302: when __ipow__ returns NotImplemented, fall back to __pow__/__rpow__ #24587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ Other Language Changes
``__globals__['__builtins__']``.
(Contributed by Mark Shannon in :issue:`42990`.)

* `**=` will try ``__pow__``/``__rpow__`` when ``__ipow__`` returns
:class:`NotImplemented` as defined by the language specification.
(Contributed by Brett Cannon in :issue:`38302`.)


New Modules
===========
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3903,6 +3903,25 @@ def __ipow__(self, other):
a = C()
a **= 2

def test_pow_fallback_for_ipow(self):
# If __ipow__ returns NotImplemented, call __pow__.
# https://bugs.python.org/issue38302
class C:
def __init__(self, val):
self._val = val

def __ipow__(self, other):
return NotImplemented

def __pow__(self, other):
return self._val ** other

base = 3
exponent = 5
a = C(base)
a **= exponent
self.assertEqual(a, base ** exponent)

def test_mutable_bases(self):
# Testing mutable bases...

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When __ipow__ returns NotImplemented, fall back to trying __pow__/__rpow__.
66 changes: 41 additions & 25 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,31 @@ binop_type_error(PyObject *v, PyObject *w, const char *op_name)
return NULL;
}

static PyObject *
ternaryop_type_error(PyObject *v, PyObject *w, PyObject *z, const char *op_name)
{
if (z == Py_None) {
PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for %.100s: "
"'%.100s' and '%.100s'",
op_name,
Py_TYPE(v)->tp_name,
Py_TYPE(w)->tp_name);
}
else {
PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for %.100s: "
"'%.100s', '%.100s', '%.100s'",
op_name,
Py_TYPE(v)->tp_name,
Py_TYPE(w)->tp_name,
Py_TYPE(z)->tp_name);
}
return NULL;
}

static PyObject *
binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
{
Expand Down Expand Up @@ -952,24 +977,7 @@ ternary_op(PyObject *v,
}
}

if (z == Py_None) {
PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for ** or pow(): "
"'%.100s' and '%.100s'",
Py_TYPE(v)->tp_name,
Py_TYPE(w)->tp_name);
}
else {
PyErr_Format(
PyExc_TypeError,
"unsupported operand type(s) for pow(): "
"'%.100s', '%.100s', '%.100s'",
Py_TYPE(v)->tp_name,
Py_TYPE(w)->tp_name,
Py_TYPE(z)->tp_name);
}
return NULL;
Py_RETURN_NOTIMPLEMENTED;
}

#ifdef NDEBUG
Expand Down Expand Up @@ -1077,7 +1085,12 @@ PyNumber_Remainder(PyObject *v, PyObject *w)
PyObject *
PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
{
return TERNARY_OP(v, w, z, NB_SLOT(nb_power), "** or pow()");
PyObject *result = TERNARY_OP(v, w, z, NB_SLOT(nb_power), "** or pow()");
if (result == Py_NotImplemented) {
Py_DECREF(result);
return ternaryop_type_error(v, w, z, "** or pow()");
}
return result;
}

/* Binary in-place operators */
Expand Down Expand Up @@ -1237,13 +1250,16 @@ PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
PyObject *
PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
{
if (Py_TYPE(v)->tp_as_number &&
Py_TYPE(v)->tp_as_number->nb_inplace_power != NULL) {
return TERNARY_OP(v, w, z, NB_SLOT(nb_inplace_power), "**=");
}
else {
return TERNARY_OP(v, w, z, NB_SLOT(nb_power), "**=");
PyObject *result = TERNARY_OP(v, w, z, NB_SLOT(nb_inplace_power), "**=");
if (result == Py_NotImplemented) {
Py_DECREF(result);
result = TERNARY_OP(v, w, z, NB_SLOT(nb_power), "**=");
if (result == Py_NotImplemented) {
Py_DECREF(result);
return ternaryop_type_error(v, w, z, "**=");
}
}
return result;
}


Expand Down