This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [C API] Remove Py_OVERFLOWED(), Py_SET_ERRNO_ON_MATH_ERROR(), Py_ADJUST_ERANGE1()
Type: Stage: resolved
Components: C API Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, serhiy.storchaka, tim.peters, vstinner
Priority: normal Keywords: patch

Created on 2021-10-08 13:39 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28820 merged vstinner, 2021-10-08 13:59
PR 28882 closed vstinner, 2021-10-11 19:36
PR 28884 merged vstinner, 2021-10-11 20:07
PR 28889 merged vstinner, 2021-10-11 21:22
PR 31171 merged vstinner, 2022-02-06 19:32
Messages (17)
msg403473 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-08 13:39
I propose to remove the following macros from the Python C API:

* Py_OVERFLOWED()
* _Py_SET_EDOM_FOR_NAN()
* Py_SET_ERRNO_ON_MATH_ERROR()
* Py_SET_ERANGE_IF_OVERFLOW()
* Py_ADJUST_ERANGE1()
* Py_ADJUST_ERANGE2()

Only Py_ADJUST_ERANGE1() and Py_ADJUST_ERANGE2() are still used by Python itself, other macros are no longer used since Python 2.7:

* Py_OVERFLOWED(): no longer used since Python 2.7
* _Py_SET_EDOM_FOR_NAN(): used by Py_SET_ERRNO_ON_MATH_ERROR() which is no longer used
* Py_SET_ERRNO_ON_MATH_ERROR(): no longer used since Python 2.6
* Py_SET_ERANGE_IF_OVERFLOW(): no longer used since Python 2.4
* Py_ADJUST_ERANGE1(): used by Objects/floatobject.c
* Py_ADJUST_ERANGE2(): used by Objects/complexobject.c

I searched for these macros in the PyPI top 5000 modules: none of these macros are used. There is a single match: frozendict-2.0.6 which contains a Include/pyport.h copy, but it doesn't use these macros.

--

Py_OVERFLOWED() was used by long_true_divide() and PyLong_AsDouble() in Python 2.6, but Python 2.7 no longer used them.


(1) Py_OVERFLOWED() call in long_true_divide() was removed in Python 2.7 by bpo-1811:

commit 465728364749e903fb4293b2f7a266b58de6bde4
Author: Mark Dickinson <dickinsm@gmail.com>
Date:   Sun Dec 27 14:55:57 2009 +0000

    Issue #1811:  Improve accuracy and consistency of true division for integers.


(2) Py_OVERFLOWED() call in PyLong_AsDouble() was removed in Python 2.7 by bpo-3166:

commit 6736cf8d20b67b74e8e959622132963285156242
Author: Mark Dickinson <dickinsm@gmail.com>
Date:   Mon Apr 20 21:13:33 2009 +0000

    Issue #3166: Make long -> float (and int -> float) conversions
    correctly rounded, using round-half-to-even.  This ensures that the
    value of float(n) doesn't depend on whether we're using 15-bit digits
    or 30-bit digits for Python longs.

--

Py_SET_ERRNO_ON_MATH_ERROR() and Py_SET_ERANGE_IF_OVERFLOW() were used in Objects/mathmodule.c in Python 2.5.


(1) The last call to Py_SET_ERRNO_ON_MATH_ERROR() was removed by in Python 2.6 by:

commit 6f34109384f3a78d5f4f8bdd418a89caca19631e
Author: Christian Heimes <christian@cheimes.de>
Date:   Fri Apr 18 23:13:07 2008 +0000

    I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
    
    The patch also adds acosh, asinh, atanh, log1p and copysign to all platforms. Finally it fixes differences between platforms like different results or exceptions for edge cases. Have fun :)


(2) The last call to Py_SET_ERANGE_IF_OVERFLOW() was removed in Python 2.4 by:

commit 77d9a3effa21b8987ceac26d67ad676e1c5afb49
Author: Hye-Shik Chang <hyeshik@gmail.com>
Date:   Mon Mar 22 08:43:55 2004 +0000

    Patch #871657: Set EDOM for `nan' return values on FreeBSD and OpenBSD.
    This fixes a problem that math.sqrt(-1) doesn't raise math.error.
msg403474 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-08 14:00
Py_OVERFLOWED() documentation says that the function is not reliable since C99. Python is using C99 since Python 3.6.

/* Py_OVERFLOWED(X)
 * Return 1 iff a libm function overflowed.  Set errno to 0 before calling
 * a libm function, and invoke this macro after, passing the function
 * result.
 * Caution:
 *    This isn't reliable.  C99 no longer requires libm to set errno under
 *        any exceptional condition, but does require +- HUGE_VAL return
 *        values on overflow.  A 754 box *probably* maps HUGE_VAL to a
 *        double infinity, and we're cool if that's so, unless the input
 *        was an infinity and an infinity is the expected result.  A C89
 *        system sets errno to ERANGE, so we check for that too.  We're
 *        out of luck if a C99 754 box doesn't map HUGE_VAL to +Inf, or
 *        if the returned result is a NaN, or if a C89 box returns HUGE_VAL
 *        in non-overflow cases.
 *    X is evaluated more than once.
 * Some platforms have better way to spell this, so expect some #ifdef'ery.
 *
 * OpenBSD uses 'isinf()' because a compiler bug on that platform causes
 * the longer macro version to be mis-compiled. This isn't optimal, and
 * should be removed once a newer compiler is available on that platform.
 * The system that had the failure was running OpenBSD 3.2 on Intel, with
 * gcc 2.95.3.
 *
 * According to Tim's checkin, the FreeBSD systems use isinf() to work
 * around a FPE bug on that platform.
 */
msg403475 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-08 14:01
Py_SET_ERRNO_ON_MATH_ERROR() documentation also says that it is not reliable:

/* Py_SET_ERRNO_ON_MATH_ERROR(x)
 * If a libm function did not set errno, but it looks like the result
 * overflowed or not-a-number, set errno to ERANGE or EDOM.  Set errno
 * to 0 before calling a libm function, and invoke this macro after,
 * passing the function result.
 * Caution:
 *    This isn't reliable.  See Py_OVERFLOWED comments.
 *    X is evaluated more than once.
 */
msg403476 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-08 14:02
Py_ADJUST_ERANGE1() is used by float ** float.

Py_ADJUST_ERANGE2() is used by complex ** complex.
msg403477 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-08 14:05
I add Mark Dickinson and Tim Peters who were involved in changes removing usage of these macros.
msg403490 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-08 18:28
And I have doubts about Py_ADJUST_ERANGE2(). I think that it is used incorrectly. See issue44970.
msg403522 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-09 07:56
> And I have doubts about Py_ADJUST_ERANGE2(). I think that it is used incorrectly. See issue44970.

The question here is if it should be exported in the C API. I propose to remove it.

Fixing it is a different issue: bpo-44970 :-)
msg403660 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-10-11 16:18
+1 for the removals. (We should fix #44970 too, but as you say that's a separate issue. And I suspect that the Py_ADJUST_ERANGE1() use for float pow should be replaced, too.)
msg403670 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 19:00
New changeset 2f92e2a590f0e5d2d3093549f5af9a4a1889eb5a by Victor Stinner in branch 'main':
bpo-45412: Remove Py_SET_ERRNO_ON_MATH_ERROR() macro (GH-28820)
https://github.com/python/cpython/commit/2f92e2a590f0e5d2d3093549f5af9a4a1889eb5a
msg403674 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 20:04
> +1 for the removals. (We should fix #44970 too, but as you say that's a separate issue. And I suspect that the Py_ADJUST_ERANGE1() use for float pow should be replaced, too.)

Well, it's scary of use functions which are documented as:

 * Caution:
 *    This isn't reliable.  See Py_OVERFLOWED comments.
 *    X and Y may be evaluated more than once.
msg403676 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 20:08
>  *    This isn't reliable.  See Py_OVERFLOWED comments.

Oops, Py_OVERFLOWED() has been removed: I created PR 28884 to fix the comment.
msg403687 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 21:07
New changeset a9fe1a8e5b4698937e06c2c419da92e6f78f2ee7 by Victor Stinner in branch 'main':
bpo-45412: Update _Py_ADJUST_ERANGE1() comment (GH-28884)
https://github.com/python/cpython/commit/a9fe1a8e5b4698937e06c2c419da92e6f78f2ee7
msg403688 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 21:09
New changeset 7103356455c8b0c2ba3523929327756413337a31 by Victor Stinner in branch 'main':
bpo-45412: Move _Py_SET_53BIT_PRECISION_START to pycore_pymath.h (GH-28882)
https://github.com/python/cpython/commit/7103356455c8b0c2ba3523929327756413337a31
msg403693 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 22:12
New changeset 61190e092b8258ede92ac543bb39bad0f7168104 by Victor Stinner in branch 'main':
bpo-45412: Move copysign() define to pycore_pymath.h (GH-28889)
https://github.com/python/cpython/commit/61190e092b8258ede92ac543bb39bad0f7168104
msg403694 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 22:12
Include/pymath.h is now better, I close the issue ;-)
msg413826 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-23 17:16
New changeset 9bbdde218005f552304d9954bb97e3f9185edded by Victor Stinner in branch 'main':
bpo-45412: Add _PY_SHORT_FLOAT_REPR macro (GH-31171)
https://github.com/python/cpython/commit/9bbdde218005f552304d9954bb97e3f9185edded
msg413827 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-23 17:18
See also bpo-46670: "Build Python with -Wundef: don't use undefined macros".
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89575
2022-02-23 17:18:00vstinnersetmessages: + msg413827
2022-02-23 17:16:40vstinnersetmessages: + msg413826
2022-02-06 19:32:47vstinnersetpull_requests: + pull_request29343
2021-10-11 22:12:30vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg403694

stage: patch review -> resolved
2021-10-11 22:12:08vstinnersetmessages: + msg403693
2021-10-11 21:22:10vstinnersetpull_requests: + pull_request27184
2021-10-11 21:09:44vstinnersetmessages: + msg403688
2021-10-11 21:07:44vstinnersetmessages: + msg403687
2021-10-11 20:08:56vstinnersetmessages: + msg403676
2021-10-11 20:07:13vstinnersetpull_requests: + pull_request27178
2021-10-11 20:04:50vstinnersetmessages: + msg403674
2021-10-11 19:36:50vstinnersetpull_requests: + pull_request27177
2021-10-11 19:00:29vstinnersetmessages: + msg403670
2021-10-11 16:18:42mark.dickinsonsetmessages: + msg403660
2021-10-09 07:56:50vstinnersetmessages: + msg403522
2021-10-08 18:28:43serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg403490
2021-10-08 14:05:38vstinnersetnosy: + tim.peters
messages: + msg403477
2021-10-08 14:02:43vstinnersetmessages: + msg403476
2021-10-08 14:01:58vstinnersetmessages: + msg403475
2021-10-08 14:00:48vstinnersetmessages: + msg403474
2021-10-08 13:59:39vstinnersetnosy: + mark.dickinson
2021-10-08 13:59:17vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request27139
2021-10-08 13:39:11vstinnercreate