Skip to content

gh-133895: provide C99 Annex G return values for cmath's functions #134995

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions Doc/library/cmath.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ the function is then applied to the result of the conversion.
1.4142135623730951j


Most functions compute and return the C99 Annex G recommended result.
If the standard recommends raising ``FE_DIVBYZERO`` or ``FE_INVALID``
floating-point exceptions, a :exc:`ValueError` is raised and the recommended
result is available as the ``value`` attribute of the exception object.
If a range error occurs due to an overflow in any component of the result,
an :exc:`OverflowError` is raised.


==================================================== ============================================
**Conversions to and from polar coordinates**
--------------------------------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ New modules
Improved modules
================

cmath
-----

* Provide C99 Annex G return values for :mod:`cmath`'s functions as the
``value`` attribute of exception objects.
(Contributed by Sergey B Kirpichev in :gh:`133895`.)


dbm
---

Expand Down
13 changes: 7 additions & 6 deletions Lib/test/test_cmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,22 +318,23 @@ def polar_complex(z):
if 'divide-by-zero' in flags or 'invalid' in flags:
try:
actual = function(arg)
except ValueError:
continue
except ValueError as exc:
actual = exc.value
else:
self.fail('ValueError not raised in test '
'{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))

if 'overflow' in flags:
elif 'overflow' in flags:
try:
actual = function(arg)
except OverflowError:
continue
except OverflowError as exc:
actual = exc.value if fn != 'polar' else complex(*exc.value)
else:
self.fail('OverflowError not raised in test '
'{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))

actual = function(arg)
else:
actual = function(arg)

if 'ignore-real-sign' in flags:
actual = complex(abs(actual.real), actual.imag)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Provide C99 Annex G return values for :mod:`cmath`'s functions as the
``value`` attribute of exception objects. Patch by Sergey B Kirpichev.
Loading
Loading