classification
Title: math.copysign raises SystemError with non-float x and custom y
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: ZackerySpytz, auscompgeek, mark.dickinson
Priority: normal Keywords: easy (C), patch

Created on 2020-03-06 02:59 by auscompgeek, last changed 2020-03-14 11:53 by mark.dickinson. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18806 closed ZackerySpytz, 2020-03-06 13:16
PR 18989 merged mark.dickinson, 2020-03-14 11:05
PR 18990 merged mark.dickinson, 2020-03-14 11:30
Messages (10)
msg363477 - (view) Author: David Vo (auscompgeek) Date: 2020-03-06 02:59
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
msg363492 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-06 07:48
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".)
msg363493 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-06 07:49
@David: do you have any interest in creating a PR?
msg363495 - (view) Author: David Vo (auscompgeek) Date: 2020-03-06 08:24
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.
msg363496 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-06 08:26
No problem; I'll likely pick it up (if no-one else does) in the next few days.
msg363505 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2020-03-06 13:18
I have created a pull request to fix this issue.  Please consider taking a look.
msg364150 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-14 10:45
New changeset 5208b4b37953a406db0ed6a9db545c2948dde989 by Zackery Spytz in branch 'master':
bpo-39871: Fix possible SystemError in atan2, copysign and remainder (GH-18806)
https://github.com/python/cpython/commit/5208b4b37953a406db0ed6a9db545c2948dde989
msg364152 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-14 11:38
New changeset e634a8ac1f8a2904a2241b83fc5e4c9f40d5c7ca by Mark Dickinson in branch '3.8':
[3.8] bpo-39871: Fix possible SystemError in atan2, copysign and remainder (GH-18806) (GH-18989)
https://github.com/python/cpython/commit/e634a8ac1f8a2904a2241b83fc5e4c9f40d5c7ca
msg364154 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-14 11:51
New changeset 4dcfe5f2e4bad8e03990de75d8d4ec20e8aa23b3 by Mark Dickinson in branch '3.7':
[3.7] bpo-39871: Fix possible SystemError in atan2, copysign and remainder (GH-18806) (GH-18990)
https://github.com/python/cpython/commit/4dcfe5f2e4bad8e03990de75d8d4ec20e8aa23b3
msg364155 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-14 11:53
Zackery: Thanks for the fix! Now merged and backported to 3.8 and 3.7.
History
Date User Action Args
2020-03-14 11:53:04mark.dickinsonsetstatus: open -> closed
resolution: fixed
messages: + msg364155

stage: patch review -> resolved
2020-03-14 11:51:56mark.dickinsonsetmessages: + msg364154
2020-03-14 11:38:55mark.dickinsonsetmessages: + msg364152
2020-03-14 11:30:41mark.dickinsonsetpull_requests: + pull_request18337
2020-03-14 11:05:47mark.dickinsonsetpull_requests: + pull_request18336
2020-03-14 10:45:40mark.dickinsonsetmessages: + msg364150
2020-03-06 13:18:16ZackerySpytzsetmessages: + msg363505
2020-03-06 13:16:39ZackerySpytzsetkeywords: + patch
nosy: + ZackerySpytz

pull_requests: + pull_request18164
stage: needs patch -> patch review
2020-03-06 08:26:26mark.dickinsonsetassignee: mark.dickinson
messages: + msg363496
2020-03-06 08:24:12auscompgeeksetmessages: + msg363495
2020-03-06 07:49:46mark.dickinsonsetmessages: + msg363493
2020-03-06 07:48:46mark.dickinsonsetmessages: + msg363492
2020-03-06 07:44:55mark.dickinsonsetnosy: + mark.dickinson
2020-03-06 06:48:12serhiy.storchakasetkeywords: + easy (C)
stage: needs patch
versions: + Python 3.7, Python 3.8, Python 3.9
2020-03-06 02:59:01auscompgeekcreate