Skip to content

Commit 4581298

Browse files
committed
Move ABI/feature selection macros to their own header
1 parent 1116e1e commit 4581298

File tree

7 files changed

+92
-71
lines changed

7 files changed

+92
-71
lines changed

Include/Python.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
// is not needed.
1010

1111

12-
// Include Python header files
13-
#include "patchlevel.h"
14-
#include "pyconfig.h"
15-
#include "pymacconfig.h"
12+
// Include Python configuration headers
13+
#include "patchlevel.h" // the Python version
14+
#include "pyconfig.h" // information from configure
15+
#include "pymacconfig.h" // overrides for pyconfig
16+
#include "pyabi.h" // feature/ABI selection
1617

1718

1819
// Include standard header files

Include/patchlevel.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,4 @@
6161
#define PYTHON_ABI_VERSION 3
6262
#define PYTHON_ABI_STRING "3"
6363

64-
65-
/* Stable ABI for free-threaded builds (introduced in PEP 803)
66-
is enabled by one of:
67-
- Py_TARGET_ABI3T, or
68-
- Py_LIMITED_API and Py_GIL_DISABLED.
69-
"Output" macros to be used internally:
70-
- Py_LIMITED_API (defines the subset of API we expose)
71-
- _Py_OPAQUE_PYOBJECT (additionally hides what's ABI-incompatible between
72-
free-threaded & GIL)
73-
(Don't use Py_TARGET_ABI3T directly: it's currently only used to set these
74-
2 macros. It's also available for users' convenience.)
75-
*/
76-
#if defined(Py_LIMITED_API) && defined(Py_GIL_DISABLED) \
77-
&& !defined(Py_TARGET_ABI3T)
78-
# define Py_TARGET_ABI3T Py_LIMITED_API
79-
#endif
80-
#if defined(Py_TARGET_ABI3T)
81-
# define _Py_OPAQUE_PYOBJECT
82-
# if !defined(Py_LIMITED_API)
83-
# define Py_LIMITED_API Py_TARGET_ABI3T
84-
# elif Py_LIMITED_API > Py_TARGET_ABI3T
85-
// if both are defined, use the *lower* version,
86-
// i.e. maximum compatibility
87-
# undef Py_LIMITED_API
88-
# define Py_LIMITED_API Py_TARGET_ABI3T
89-
# endif
90-
#endif
91-
9264
#endif //_Py_PATCHLEVEL_H

Include/pyabi.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* Macros that restrict available definitions and select implementations
2+
* to match an ABI stability promise:
3+
*
4+
* - internal API/ABI (may change at any time) -- Py_BUILD_CORE*
5+
* - general CPython API/ABI (may change in 3.x.0) -- default
6+
* - Stable ABI: abi3, abi3t (long-term stable) -- Py_LIMITED_API,
7+
* Py_TARGET_ABI3T, _Py_OPAQUE_PYOBJECT
8+
* - Free-threading (incompatible with non-free-threading builds)
9+
* -- Py_GIL_DISABLED
10+
*/
11+
12+
#ifndef _Py_PYABI_H
13+
#define _Py_PYABI_H
14+
15+
/* Defines to build Python and its standard library:
16+
*
17+
* - Py_BUILD_CORE: Build Python core. Give access to Python internals, but
18+
* should not be used by third-party modules.
19+
* - Py_BUILD_CORE_BUILTIN: Build a Python stdlib module as a built-in module.
20+
* - Py_BUILD_CORE_MODULE: Build a Python stdlib module as a dynamic library.
21+
*
22+
* Py_BUILD_CORE_BUILTIN and Py_BUILD_CORE_MODULE imply Py_BUILD_CORE.
23+
*
24+
* On Windows, Py_BUILD_CORE_MODULE exports "PyInit_xxx" symbol, whereas
25+
* Py_BUILD_CORE_BUILTIN does not.
26+
*/
27+
#if defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE)
28+
# define Py_BUILD_CORE
29+
#endif
30+
#if defined(Py_BUILD_CORE_MODULE) && !defined(Py_BUILD_CORE)
31+
# define Py_BUILD_CORE
32+
#endif
33+
34+
/* Stable ABI for free-threaded builds (abi3t, introduced in PEP 803)
35+
is enabled by the user setting one of:
36+
- Py_TARGET_ABI3T, or
37+
- Py_LIMITED_API and Py_GIL_DISABLED.
38+
39+
These affect set the following, which Python.h should use internally:
40+
- Py_LIMITED_API (defines the subset of API we expose)
41+
- _Py_OPAQUE_PYOBJECT (additionally hides what's ABI-incompatible between
42+
free-threaded & GIL)
43+
44+
(Don't use Py_TARGET_ABI3T directly. It's currently only used to set these
45+
2 macro, and defined for users' convenience.)
46+
*/
47+
#if defined(Py_LIMITED_API) && defined(Py_GIL_DISABLED) \
48+
&& !defined(Py_TARGET_ABI3T)
49+
# define Py_TARGET_ABI3T Py_LIMITED_API
50+
#endif
51+
#if defined(Py_TARGET_ABI3T)
52+
# define _Py_OPAQUE_PYOBJECT
53+
# if !defined(Py_LIMITED_API)
54+
# define Py_LIMITED_API Py_TARGET_ABI3T
55+
# elif Py_LIMITED_API > Py_TARGET_ABI3T
56+
// if both are defined, use the *lower* version,
57+
// i.e. maximum compatibility
58+
# undef Py_LIMITED_API
59+
# define Py_LIMITED_API Py_TARGET_ABI3T
60+
# endif
61+
#endif
62+
63+
#if defined(Py_TARGET_ABI3T)
64+
# if !defined(Py_GIL_DISABLED)
65+
// Define Py_GIL_DISABLED for users' needs. Users check this macro to see
66+
// whether they need extra synchronization.
67+
# define Py_GIL_DISABLED
68+
# endif
69+
# if defined(_Py_IS_TESTCEXT)
70+
// When compiling for abi3t, contents of Python.h should not depend
71+
// on Py_GIL_DISABLED.
72+
// We ask GCC to error if it sees the macro from this point on.
73+
// Since users are free to the macro, and there's no way to undo the poisoning
74+
// at the end of Python.h, we only do this in a test module.
75+
# ifdef __GNUC__
76+
# undef Py_GIL_DISABLED
77+
# pragma GCC poison Py_GIL_DISABLED
78+
# endif
79+
# endif
80+
#endif
81+
82+
#endif // _Py_PYABI_H

Include/pyport.h

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,45 +58,6 @@
5858
#endif
5959

6060

61-
/* Defines to build Python and its standard library:
62-
*
63-
* - Py_BUILD_CORE: Build Python core. Give access to Python internals, but
64-
* should not be used by third-party modules.
65-
* - Py_BUILD_CORE_BUILTIN: Build a Python stdlib module as a built-in module.
66-
* - Py_BUILD_CORE_MODULE: Build a Python stdlib module as a dynamic library.
67-
*
68-
* Py_BUILD_CORE_BUILTIN and Py_BUILD_CORE_MODULE imply Py_BUILD_CORE.
69-
*
70-
* On Windows, Py_BUILD_CORE_MODULE exports "PyInit_xxx" symbol, whereas
71-
* Py_BUILD_CORE_BUILTIN does not.
72-
*/
73-
#if defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE)
74-
# define Py_BUILD_CORE
75-
#endif
76-
#if defined(Py_BUILD_CORE_MODULE) && !defined(Py_BUILD_CORE)
77-
# define Py_BUILD_CORE
78-
#endif
79-
80-
#if defined(Py_TARGET_ABI3T)
81-
# if !defined(Py_GIL_DISABLED)
82-
// Define Py_GIL_DISABLED for users' needs. This macro is used to enable
83-
// locking needed in for free-threaded interpreters builds.
84-
# define Py_GIL_DISABLED
85-
# endif
86-
# if defined(_Py_IS_TESTCEXT)
87-
// When compiling for abi3t, contents of Python.h should not depend
88-
// on Py_GIL_DISABLED.
89-
// We ask GCC to error if it sees the macro from this point on.
90-
// Since users are free to the macro, and there's no way to undo the poisoning
91-
// at the end of Python.h, we only do this in a test module.
92-
# ifdef __GNUC__
93-
# undef Py_GIL_DISABLED
94-
# pragma GCC poison Py_GIL_DISABLED
95-
# endif
96-
# endif
97-
#endif
98-
99-
10061
/**************************************************************************
10162
Symbols and macros to supply platform-independent interfaces to basic
10263
C language & library operations whose spellings vary across platforms.

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,7 @@ PYTHON_HEADERS= \
12121212
$(srcdir)/Include/osdefs.h \
12131213
$(srcdir)/Include/osmodule.h \
12141214
$(srcdir)/Include/patchlevel.h \
1215+
$(srcdir)/Include/pyabi.h \
12151216
$(srcdir)/Include/pyatomic.h \
12161217
$(srcdir)/Include/pybuffer.h \
12171218
$(srcdir)/Include/pycapsule.h \

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@
359359
<ClInclude Include="..\Include\osmodule.h" />
360360
<ClInclude Include="..\Include\patchlevel.h" />
361361
<ClInclude Include="..\Include\py_curses.h" />
362+
<ClInclude Include="..\Include\pyabi.h" />
362363
<ClInclude Include="..\Include\pyatomic.h" />
363364
<ClInclude Include="..\Include\pybuffer.h" />
364365
<ClInclude Include="..\Include\pycapsule.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@
156156
<ClInclude Include="..\Include\py_curses.h">
157157
<Filter>Include</Filter>
158158
</ClInclude>
159+
<ClInclude Include="..\Include\pyabi.h">
160+
<Filter>Include</Filter>
161+
</ClInclude>
159162
<ClInclude Include="..\Include\pyatomic.h">
160163
<Filter>Include</Filter>
161164
</ClInclude>

0 commit comments

Comments
 (0)