-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
PyNumber_InPlacePower ignores o3 if o1 implements __ipow__ #86069
Comments
The documentation for PyNumber_InPlacePower [1] reads: This is the equivalent of the Python statement o1 **= o2 when o3 is Py_None, or an in-place variant of pow(o1, o2, o3) otherwise. If o3 is to be ignored, pass Py_None in its place (passing NULL for o3 would cause an illegal memory access). However, if a class A implements __ipow__ then PyNumber_InPlacePower(o1, o2, o3) ALWAYS ignores o3 if o1 is an instance of A. This happens because if A implements __ipow__ then PyNumber_InPlacePower always calls the nb_inplace_power slot [2] and the slot always drops the third argument [3]. This appears to have always been the case in Python, so likely a small documentation patch is all that is required. If people agree, I will open a documentation pull request. [1] https://docs.python.org/3/c-api/number.html?highlight=pynumber_inplacepower#c.PyNumber_InPlacePower [2] https://github.com/python/cpython/blob/master/Objects/abstract.c#L1164 [3] https://github.com/python/cpython/blob/master/Objects/typeobject.c#L6631-L6636 |
The documentation for __ipow__ [4] suggests that the intention was to support the modulus argument, so perhaps that argues for fixing the behaviour of PyNumber_InPlacePower. [4] https://docs.python.org/3/reference/datamodel.html#object.\_\_ipow__ |
Huh, this is weird. I wonder why the data model provides the signature object.__ipow__(self, other[, modulo]) if the slot always seems to drop the third argument. Adding Brett to the nosy who found another issue around the special casing of pow/** recently. See also bpo-38302 |
Also of note, there is actually no way to call |
Whoops, forgot the numpy link: https://github.com/numpy/numpy/blob/33e1dbee8d9e11a3e96efaae822ff6f3c44e3cef/numpy/core/src/multiarray/number.c#L729-L741 |
This looks like a documentation issue for me. CC @picnixz |
There is no way to call ternary form of this dunder.
The default implementation of the This looks like a design mistake. The fact that the third argument of |
Lets correct this! There is no way to call the 3-arg dunder with some Python API (like pow()), only directly. Or even with the current C-API (PyNumber_InPlacePower).
Never. At least in the current implementation. |
Test it with the third argument.
Test it with the third argument. (cherry picked from commit cfe4103) Co-authored-by: Serhiy Storchaka <[email protected]>
Test it with the third argument. (cherry picked from commit cfe41037eb5293a051846ddc0b4afdb7a5f60540) Co-authored-by: Serhiy Storchaka <[email protected]>
…H-130211) Test it with the third argument. (cherry picked from commit cfe4103) Co-authored-by: Serhiy Storchaka <[email protected]>
…H-130212) Test it with the third argument. (cherry picked from commit cfe4103) Co-authored-by: Serhiy Storchaka <[email protected]>
…ower() The default implementation of the nb_inplace_power slot no longer ignores the third argument, but passes it to the __ipow__ method if its value is not None.
The current behavior is inconsistent. If One way to solve this is to stop ignoring the third argument for Other way -- deprecate passing non-None third argument in |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: