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: Remove the Py_NO_NAN macro: require NAN to build Python 3.11
Type: behavior Stage: resolved
Components: Build, Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, vstinner
Priority: normal Keywords: patch

Created on 2022-02-06 09:54 by mark.dickinson, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31160 merged vstinner, 2022-02-06 13:07
PR 31557 merged vstinner, 2022-02-24 23:51
Messages (13)
msg412620 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-06 09:54
The macro Py_NAN may or may not be defined: in particular, a platform that doesn't have NaNs is supposed to be able to define Py_NO_NAN in pyport.h to indicate that.

But not all of our uses of `Py_NAN` are guarded by suitable #ifdef conditionals. As a result, compilation fails if Py_NAN is not defined.
msg412621 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-06 09:55
Here's the first point of failure on my machine. Fixing this shows up more failures.

gcc -c -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -fvisibility=hidden  -I./Include/internal  -I. -I./Include    -DPy_BUILD_CORE -o Objects/complexobject.o Objects/complexobject.c
Objects/complexobject.c:120:27: error: use of undeclared identifier 'Py_NAN'
        r.real = r.imag = Py_NAN;
                          ^
Objects/complexobject.c:206:16: error: use of undeclared identifier 'Py_NAN'
        return Py_NAN;
               ^
2 errors generated.
make: *** [Objects/complexobject.o] Error 1
msg412625 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-06 12:29
Python uses Py_NAN without "#ifdef Py_NAN" guard since 2008. Building Python with Py_NO_NAN never worked. Nobody reported such build failure in the last 14 years...

IMO it's time to stop supporting platforms without NaN support.


> Objects/complexobject.c:120:27: error: use of undeclared identifier 'Py_NAN'

I only try a few Python versions: I reproduce this issue in Python 3.11, 3.5, 3.2 and... even Python 2.7!

The Py_NO_NAN macro was introduced in Python 2.6 (2007) by bpo-1635 "Float patch for inf and nan on Windows (and other platforms)":
--
commit 0a8143f6462b491d3f12bfb899efd6e044e350be
Author: Christian Heimes <christian@cheimes.de>
Date:   Tue Dec 18 23:22:54 2007 +0000

    Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
    
    The patch unifies float("inf") and repr(float("inf")) on all platforms.

(...)

+#if !defined(Py_NAN) && !defined(Py_NO_NAN)
+#define Py_NAN (Py_HUGE_VAL * 0.)
+#endif
---

The following change started to use Py_NAN in many C files:

* Modules/cmathmodule.c
* Objects/complexobject.c
* Objects/floatobject.c
* Python/pymath.c 

---
commit 6f34109384f3a78d5f4f8bdd418a89caca19631e (HEAD)
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 w
ork 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 differen
ces between platforms like different results or exceptions for edge cases. Have fun :)
---

At this commit, floatobject.c and pymath.c use "#ifdef Py_NAN" carefully, whereas cmathmodule.c and complexobject.c use Py_NAN with no "#ifdef Py_NAN" guard.
msg412629 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-06 13:08
If someone suddenly requires platforms without NaN support, they can maintain a (downstream) patch, or we can revert the change in Python.
msg412630 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-06 13:11
See also bpo-46640 "Python can now use the C99 NAN constant or __builtin_nan()".
msg412632 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-06 13:36
Okay, the comments I made on #46640 still apply (even though they didn't properly apply on that issue). I think this needs a python-dev discussion before it can be moved forward - requiring the existence of NaNs is very close to requiring IEEE 754 floating-point, and that's something we've been historically reluctant to do.
msg412636 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-06 14:11
Is the macro PY_NO_SHORT_FLOAT_REPR also related to platforms which don't support IEEE 754?

In 2022, which platforms don't support IEEE 754?
msg412640 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-06 14:50
> Is the macro PY_NO_SHORT_FLOAT_REPR also related to platforms which don't support IEEE 754?

Yes, though it's a bit more than that: we also need the platform either not to have issues with double rounding for normal numbers, or we need to be able to control the x87 rounding mode in the case that double rounding might be an issue. See the explanations in the source.

https://github.com/python/cpython/blob/025cbe7a9b5d3058ce2eb8015d3650e396004545/Include/pyport.h#L345-L355

> In 2022, which platforms don't support IEEE 754?

None that CPython might plausibly run on that I'm aware of.
msg412642 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-02-06 15:01
> See the explanations in the source.

Hmm. Those explanations made more sense before PR GH-28882. :-(
msg412864 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-08 20:02
Requiring IEEE 754 support is being discussed on python-dev: https://mail.python.org/archives/list/python-dev@python.org/thread/J5FSP6J4EITPY5C2UJI7HSL2GQCTCUWN/
msg413944 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-25 00:31
New changeset 5f8b5e2f2150d2223ff9e286bd146de92ff16865 by Victor Stinner in branch 'main':
bpo-46656: Building Python now requires a C11 compiler (GH-31557)
https://github.com/python/cpython/commit/5f8b5e2f2150d2223ff9e286bd146de92ff16865
msg413945 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-25 00:33
New changeset 1b2611eb0283055835e5df632a7a735db8c894b8 by Victor Stinner in branch 'main':
bpo-46656: Remove Py_NO_NAN macro (GH-31160)
https://github.com/python/cpython/commit/1b2611eb0283055835e5df632a7a735db8c894b8
msg414864 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-03-10 16:52
> bpo-46656: Remove Py_NO_NAN macro (GH-31160)

Change documented by:

New changeset 7854012077009b9f364f198a8ae38b546ec58313 by Victor Stinner in branch 'main':
bpo-46917: math.nan is now always available (GH-31793)
https://github.com/python/cpython/commit/7854012077009b9f364f198a8ae38b546ec58313
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90814
2022-03-10 16:52:54vstinnersetmessages: + msg414864
2022-02-25 00:45:52vstinnersetstatus: open -> closed
versions: - Python 3.9, Python 3.10
title: Compile fails if Py_NO_NAN is defined -> Remove the Py_NO_NAN macro: require NAN to build Python 3.11
components: + Build, Interpreter Core
resolution: fixed
stage: patch review -> resolved
2022-02-25 00:33:01vstinnersetmessages: + msg413945
2022-02-25 00:31:35vstinnersetmessages: + msg413944
2022-02-24 23:51:32vstinnersetpull_requests: + pull_request29679
2022-02-08 20:02:28vstinnersetmessages: + msg412864
2022-02-06 15:01:09mark.dickinsonsetmessages: + msg412642
2022-02-06 14:50:58mark.dickinsonsetmessages: + msg412640
2022-02-06 14:11:39vstinnersetmessages: + msg412636
2022-02-06 13:36:52mark.dickinsonsetmessages: + msg412632
2022-02-06 13:11:52vstinnersetmessages: + msg412630
2022-02-06 13:08:30vstinnersetmessages: + msg412629
2022-02-06 13:07:08vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request29333
2022-02-06 12:29:29vstinnersetnosy: + vstinner
messages: + msg412625
2022-02-06 09:55:07mark.dickinsonsetmessages: + msg412621
2022-02-06 09:54:35mark.dickinsoncreate