gh-128972: Add `_Py_ALIGN_AS` and revert `PyASCIIObject` memory layou… · python/cpython@987e45e · GitHub
Skip to content

Commit 987e45e

Browse files
authored
gh-128972: Add _Py_ALIGN_AS and revert PyASCIIObject memory layout. (GH-133085)
Add `_Py_ALIGN_AS` as per C API WG vote: capi-workgroup/decisions#61 This patch only adds it to free-threaded builds; the `#ifdef Py_GIL_DISABLED` can be removed in the future. Use this to revert `PyASCIIObject` memory layout for non-free-threaded builds. The long-term plan is to deprecate the entire struct; until that happens it's better to keep it unchanged, as courtesy to people that rely on it despite it not being stable ABI.
1 parent d78768e commit 987e45e

4 files changed

Lines changed: 67 additions & 13 deletions

File tree

Include/cpython/unicodeobject.h

Lines changed: 20 additions & 10 deletions

Include/pymacro.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,47 @@
2323
# define static_assert _Static_assert
2424
#endif
2525

26+
27+
// _Py_ALIGN_AS: this compiler's spelling of `alignas` keyword,
28+
// We currently use alignas for free-threaded builds only; additional compat
29+
// checking would be great before we add it to the default build.
30+
// Standards/compiler support:
31+
// - `alignas` is a keyword in C23 and C++11.
32+
// - `_Alignas` is a keyword in C11
33+
// - GCC & clang has __attribute__((aligned))
34+
// (use that for older standards in pedantic mode)
35+
// - MSVC has __declspec(align)
36+
// - `_Alignas` is common C compiler extension
37+
// Older compilers may name it differently; to allow compilation on such
38+
// unsupported platforms, we don't redefine _Py_ALIGN_AS if it's already
39+
// defined. Note that defining it wrong (including defining it to nothing) will
40+
// cause ABI incompatibilities.
41+
#ifdef Py_GIL_DISABLED
42+
# ifndef _Py_ALIGN_AS
43+
# ifdef __cplusplus
44+
# if __cplusplus >= 201103L
45+
# define _Py_ALIGN_AS(V) alignas(V)
46+
# elif defined(__GNUC__) || defined(__clang__)
47+
# define _Py_ALIGN_AS(V) __attribute__((aligned(V)))
48+
# elif defined(_MSC_VER)
49+
# define _Py_ALIGN_AS(V) __declspec(align(V))
50+
# else
51+
# define _Py_ALIGN_AS(V) alignas(V)
52+
# endif
53+
# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
54+
# define _Py_ALIGN_AS(V) alignas(V)
55+
# elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
56+
# define _Py_ALIGN_AS(V) _Alignas(V)
57+
# elif (defined(__GNUC__) || defined(__clang__))
58+
# define _Py_ALIGN_AS(V) __attribute__((aligned(V)))
59+
# elif defined(_MSC_VER)
60+
# define _Py_ALIGN_AS(V) __declspec(align(V))
61+
# else
62+
# define _Py_ALIGN_AS(V) _Alignas(V)
63+
# endif
64+
# endif
65+
#endif
66+
2667
/* Minimum value between x and y */
2768
#define Py_MIN(x, y) (((x) > (y)) ? (y) : (x))
2869

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
For non-free-threaded builds, the memory layout of :c:struct:`PyASCIIObject`
2+
is reverted to match Python 3.13. (Note that the structure is not part of
3+
stable ABI and so its memory layout is *guaranteed* to remain stable.)

Objects/unicodeobject.c

Lines changed: 3 additions & 3 deletions

0 commit comments

Comments
 (0)