diff --git a/Misc/NEWS.d/next/Build/2026-08-07-00-00-00.gh-issue-155321.pythonh-posix.rst b/Misc/NEWS.d/next/Build/2026-08-07-00-00-00.gh-issue-155321.pythonh-posix.rst new file mode 100644 index 00000000000000..61b98ee2b297db --- /dev/null +++ b/Misc/NEWS.d/next/Build/2026-08-07-00-00-00.gh-issue-155321.pythonh-posix.rst @@ -0,0 +1,2 @@ +Allow :file:`Python.h` to be included after system headers that define +``_POSIX_C_SOURCE`` without causing a macro redefinition diagnostic. diff --git a/configure b/configure index 836eb88e4cdb6e..1a5b4b59fadb8a 100755 --- a/configure +++ b/configure @@ -4809,7 +4809,7 @@ printf "%s\n" "#define _XOPEN_SOURCE_EXTENDED 1" >>confdefs.h -printf "%s\n" "#define _POSIX_C_SOURCE 202405L" >>confdefs.h +printf "%s\n" "#define _PYTHON_POSIX_C_SOURCE 1" >>confdefs.h # Defining _POSIX_C_SOURCE and _XOPEN_SOURCE hides C23 library diff --git a/configure.ac b/configure.ac index 3a9841b1e7d074..f113a3fd2c7b57 100644 --- a/configure.ac +++ b/configure.ac @@ -237,6 +237,15 @@ AH_BOTTOM([ #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */ #endif +/* Define _POSIX_C_SOURCE only when it was not defined by an earlier + system-header include. */ +#ifdef _PYTHON_POSIX_C_SOURCE +# ifndef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 202405L +# endif +# undef _PYTHON_POSIX_C_SOURCE +#endif + #endif /*Py_PYCONFIG_H*/ ]) @@ -930,8 +939,8 @@ then AC_DEFINE([_XOPEN_SOURCE_EXTENDED], [1], [Define to activate Unix95-and-earlier features]) - AC_DEFINE([_POSIX_C_SOURCE], [202405L], - [Define to activate features from IEEE Std 1003.1-2024]) + AC_DEFINE([_PYTHON_POSIX_C_SOURCE], [1], + [Define to configure _POSIX_C_SOURCE in pyconfig.h]) # Defining _POSIX_C_SOURCE and _XOPEN_SOURCE hides C23 library # declarations on FreeBSD (e.g. sinpi() in math.h) when compiling diff --git a/pyconfig.h.in b/pyconfig.h.in index 691c6c0d9feb6d..159a9b73075a5c 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -2161,15 +2161,15 @@ /* Define on NetBSD to activate all library features */ #undef _NETBSD_SOURCE -/* Define to activate features from IEEE Std 1003.1-2024 */ -#undef _POSIX_C_SOURCE - /* Define if you have POSIX threads, and your system does not define that. */ #undef _POSIX_THREADS /* framework name */ #undef _PYTHONFRAMEWORK +/* Define to configure _POSIX_C_SOURCE in pyconfig.h */ +#undef _PYTHON_POSIX_C_SOURCE + /* Maximum length in bytes of a thread name */ #undef _PYTHREAD_NAME_MAXLEN @@ -2263,5 +2263,14 @@ #define STRICT_SYSV_CURSES /* Don't use ncurses extensions */ #endif +/* Define _POSIX_C_SOURCE only when it was not defined by an earlier + system-header include. */ +#ifdef _PYTHON_POSIX_C_SOURCE +# ifndef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 202405L +# endif +# undef _PYTHON_POSIX_C_SOURCE +#endif + #endif /*Py_PYCONFIG_H*/