-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-38302. __rpow__ now called when __ipow__ returns NotImplemented #16459
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
75aba4a
bpo-38302. __rpow__ now called when __ipow__ returns NotImplemented
ashkop c981f0d
📜🤖 Added by blurb_it.
blurb-it[bot] 9005cce
Fix review comments
ashkop 8a357bb
Use `object()` in test
ashkop 6426025
Add tests, update news entry
ashkop 84241a2
Add whatsnew entry
ashkop 25f95b5
Fix whitespace
ashkop c00fb1a
Merge branch 'master' of https://github.com/python/cpython into bpo-3…
ashkop 80cad6c
Remove TERNARY_OP since op_name is now used in error message
ashkop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core and Builtins/2019-09-28-12-23-23.bpo-38302.hsCNgX.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
If :func:`object.__ipow__` returns :const:`NotImplemented`, the operator will correctly fall back to :func:`object.__pow__` and :func:`object.__rpow__` as expected. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -882,10 +882,8 @@ static PyObject * | |
ternary_op(PyObject *v, | ||
PyObject *w, | ||
PyObject *z, | ||
const int op_slot | ||
#ifndef NDEBUG | ||
, const char *op_name | ||
#endif | ||
const int op_slot, | ||
const char *op_name | ||
) | ||
{ | ||
PyNumberMethods *mv = Py_TYPE(v)->tp_as_number; | ||
|
@@ -955,30 +953,25 @@ ternary_op(PyObject *v, | |
if (z == Py_None) { | ||
PyErr_Format( | ||
PyExc_TypeError, | ||
"unsupported operand type(s) for ** or pow(): " | ||
"unsupported operand type(s) for %.100s: " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for fixing this! Really weird that x = None
with self.assertRaises(TypeError) as cm:
x **= 2
self.assertIn('unsupported operand type(s) for **=', str(cm.exception)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, added a new test |
||
"'%.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 pow(): " | ||
"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; | ||
} | ||
|
||
#ifdef NDEBUG | ||
# define TERNARY_OP(v, w, z, op_slot, op_name) ternary_op(v, w, z, op_slot) | ||
#else | ||
# define TERNARY_OP(v, w, z, op_slot, op_name) ternary_op(v, w, z, op_slot, op_name) | ||
#endif | ||
|
||
|
||
#define BINARY_FUNC(func, op, op_name) \ | ||
PyObject * \ | ||
func(PyObject *v, PyObject *w) { \ | ||
|
@@ -1077,7 +1070,7 @@ 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()"); | ||
return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()"); | ||
} | ||
|
||
/* Binary in-place operators */ | ||
|
@@ -1140,6 +1133,24 @@ binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot, | |
return result; | ||
} | ||
|
||
static PyObject * | ||
ternary_iop(PyObject *v, PyObject *w, PyObject *z, const int iop_slot, const int op_slot, | ||
const char *op_name) | ||
{ | ||
PyNumberMethods *mv = Py_TYPE(v)->tp_as_number; | ||
if (mv != NULL) { | ||
ternaryfunc slot = NB_TERNOP(mv, iop_slot); | ||
if (slot) { | ||
PyObject *x = (slot)(v, w, z); | ||
if (x != Py_NotImplemented) { | ||
return x; | ||
} | ||
Py_DECREF(x); | ||
} | ||
} | ||
return ternary_op(v, w, z, op_slot, op_name); | ||
} | ||
|
||
#define INPLACE_BINOP(func, iop, op, op_name) \ | ||
PyObject * \ | ||
func(PyObject *v, PyObject *w) { \ | ||
|
@@ -1237,13 +1248,8 @@ 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), "**="); | ||
} | ||
return ternary_iop(v, w, z, NB_SLOT(nb_inplace_power), | ||
NB_SLOT(nb_power), "**="); | ||
} | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we're also lacking a test for when
A
doesn't define__ipow__
. Do you mind adding one?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, added the test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test for the two fallbacks and ensure the same class is returning
NotImplemented
and implementing the regular power methods i.e something like:The linked bug is only exhibited when a class has an ipow slot but when called it returns
NotImplemented
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understood correctly what you meant, but I updated the test to also test the
__pow__
fallback.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, what you have now looks good. What I meant was that to catch the original bug/properly be a regression test, the class has to look like:
if the class looks like
then the old logic returns a false here and correctly goes to the non-inplace version of power anyway:
cpython/Objects/abstract.c
Lines 1162 to 1163 in 8e19c8b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, you are right. Thanks!