-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
math.copysign raises SystemError with non-float x and custom y #84052
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
Comments
If math.copysign(x, y) is passed an x that cannot be converted to a float and a y that implements __float__() in Python, math.copysign() will raise a SystemError from the TypeError resulting from the attempted float conversion of x. math.copysign() should probably return immediately if converting the first argument to a float raises an error. Example: >>> import math
>>> from fractions import Fraction
>>> float(Fraction(-1, 1)) # this is needed to avoid an AttributeError?
-1.0
>>> math.copysign((-1) ** 0.5, Fraction(-1, 1))
TypeError: can't convert complex to float
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/numbers.py", line 291, in __float__
return self.numerator / self.denominator
SystemError: PyEval_EvalFrameEx returned a result with an error set |
Nice find. This affects not just copysign, but all of the math module functions that use FUNC2 under the hood. (I think that's just "remainder", "atan2" and "copysign".) |
@david: do you have any interest in creating a PR? |
I'm currently rather busy with other work, but if this happens to still be open in a couple of months I might pick it up. |
No problem; I'll likely pick it up (if no-one else does) in the next few days. |
I have created a pull request to fix this issue. Please consider taking a look. |
Zackery: Thanks for the fix! Now merged and backported to 3.8 and 3.7. |
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:
The text was updated successfully, but these errors were encountered: