Skip to content

Commit a13e463

Browse files
committed
gh-133895: provide C99 Annex G return values for cmath functions
Now this information is available as the "value" attribute of the ValueError exception object: ```pycon >>> import cmath >>> try: ... cmath.log(complex(-0.0, 0)) ... except ValueError as exc: ... print(exc.value) ... (-inf+3.141592653589793j) ``` Also uses the AC magic for return value of the cmath.rect().
1 parent cebae97 commit a13e463

File tree

6 files changed

+210
-18
lines changed

6 files changed

+210
-18
lines changed

Doc/library/cmath.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ the function is then applied to the result of the conversion.
3838
1.4142135623730951j
3939

4040

41+
Most functions computes and returns the C99 Annex G recommended result. If the
42+
standard recommends raising ``FE_DIVBYZERO`` or ``FE_INVALID`` floating-point
43+
exceptions --- the :exc:`ValueError` is raised and recommended result available
44+
as the ``value`` attribute of the exception object.
45+
46+
4147
==================================================== ============================================
4248
**Conversions to and from polar coordinates**
4349
--------------------------------------------------------------------------------------------------

Doc/whatsnew/3.15.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ New modules
8989
Improved modules
9090
================
9191

92+
cmath
93+
-----
94+
95+
* Provide C99 Annex G return values for :mod:`cmath`'s functions as the
96+
"value" attribute of the ValueError exception object.
97+
(Contributed by Sergey B Kirpichev in :gh:`133895`.)
98+
99+
92100
difflib
93101
-------
94102

Lib/test/test_cmath.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,13 @@ def polar_complex(z):
318318
if 'divide-by-zero' in flags or 'invalid' in flags:
319319
try:
320320
actual = function(arg)
321-
except ValueError:
322-
continue
321+
except ValueError as exc:
322+
actual = exc.value
323323
else:
324324
self.fail('ValueError not raised in test '
325325
'{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))
326326

327-
if 'overflow' in flags:
327+
elif 'overflow' in flags:
328328
try:
329329
actual = function(arg)
330330
except OverflowError:
@@ -333,7 +333,8 @@ def polar_complex(z):
333333
self.fail('OverflowError not raised in test '
334334
'{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))
335335

336-
actual = function(arg)
336+
else:
337+
actual = function(arg)
337338

338339
if 'ignore-real-sign' in flags:
339340
actual = complex(abs(actual.real), actual.imag)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Provide C99 Annex G return values for :mod:`cmath`'s functions as the
2+
"value" attribute of the ValueError exception object. Patch by Sergey B
3+
Kirpichev.

Modules/clinic/cmathmodule.c.h

Lines changed: 159 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)