Skip to content

Commit 6dcbc08

Browse files
authored
gh-91324: List feature macros in the stable ABI manifest, improve tests (GH-32415)
1 parent d1de107 commit 6dcbc08

File tree

6 files changed

+232
-23
lines changed

6 files changed

+232
-23
lines changed

Lib/test/test_stable_abi_ctypes.py

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Misc/stable_abi.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
# value may change.
3030
# - typedef: A C typedef which is used in other definitions in the limited API.
3131
# Its size/layout/signature must not change.
32+
# - ifdef: A feature macro: other items may be conditional on whether the macro
33+
# is defined or not.
3234

3335
# Each top-level item can have details defined below it:
3436
# - added: The version in which the item was added to the stable ABI.
@@ -41,6 +43,10 @@
4143
# of the stable ABI.
4244
# - a combination of the above (functions that were called by macros that
4345
# were public in the past)
46+
# - doc: for `ifdef`, the blurb added in documentation
47+
# - windows: for `ifdef`, this macro is defined on Windows. (This info is used
48+
# to generate the DLL manifest and needs to be available on all platforms.)
49+
# `maybe` marks macros defined on some but not all Windows builds.
4450

4551
# For structs, one of the following must be set:
4652
# - opaque: The struct name is available in the Limited API, but its members
@@ -59,6 +65,24 @@
5965
# https://docs.python.org/3/c-api/stable.html#stable
6066

6167

68+
# Feature macros for optional functionality:
69+
70+
ifdef MS_WINDOWS
71+
doc on Windows
72+
windows
73+
ifdef HAVE_FORK
74+
doc on platforms with fork()
75+
ifdef USE_STACKCHECK
76+
doc on platforms with USE_STACKCHECK
77+
windows maybe
78+
ifdef PY_HAVE_THREAD_NATIVE_ID
79+
doc on platforms with native thread IDs
80+
windows
81+
ifdef Py_REF_DEBUG
82+
doc when Python is compiled in debug mode (with Py_REF_DEBUG)
83+
windows maybe
84+
85+
6286
# Mentioned in PEP 384:
6387

6488
struct PyObject

Modules/_testcapi_feature_macros.inc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Generated by Tools/scripts/stable_abi.py
2+
3+
// Add an entry in dict `result` for each Stable ABI feature macro.
4+
5+
#ifdef HAVE_FORK
6+
res = PyDict_SetItemString(result, "HAVE_FORK", Py_True);
7+
#else
8+
res = PyDict_SetItemString(result, "HAVE_FORK", Py_False);
9+
#endif
10+
if (res) {
11+
Py_DECREF(result); return NULL;
12+
}
13+
14+
#ifdef MS_WINDOWS
15+
res = PyDict_SetItemString(result, "MS_WINDOWS", Py_True);
16+
#else
17+
res = PyDict_SetItemString(result, "MS_WINDOWS", Py_False);
18+
#endif
19+
if (res) {
20+
Py_DECREF(result); return NULL;
21+
}
22+
23+
#ifdef PY_HAVE_THREAD_NATIVE_ID
24+
res = PyDict_SetItemString(result, "PY_HAVE_THREAD_NATIVE_ID", Py_True);
25+
#else
26+
res = PyDict_SetItemString(result, "PY_HAVE_THREAD_NATIVE_ID", Py_False);
27+
#endif
28+
if (res) {
29+
Py_DECREF(result); return NULL;
30+
}
31+
32+
#ifdef Py_REF_DEBUG
33+
res = PyDict_SetItemString(result, "Py_REF_DEBUG", Py_True);
34+
#else
35+
res = PyDict_SetItemString(result, "Py_REF_DEBUG", Py_False);
36+
#endif
37+
if (res) {
38+
Py_DECREF(result); return NULL;
39+
}
40+
41+
#ifdef USE_STACKCHECK
42+
res = PyDict_SetItemString(result, "USE_STACKCHECK", Py_True);
43+
#else
44+
res = PyDict_SetItemString(result, "USE_STACKCHECK", Py_False);
45+
#endif
46+
if (res) {
47+
Py_DECREF(result); return NULL;
48+
}
49+

Modules/_testcapimodule.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5919,6 +5919,18 @@ frame_getlasti(PyObject *self, PyObject *frame)
59195919
return PyLong_FromLong(lasti);
59205920
}
59215921

5922+
static PyObject *
5923+
get_feature_macros(PyObject *self, PyObject *Py_UNUSED(args))
5924+
{
5925+
PyObject *result = PyDict_New();
5926+
if (!result) {
5927+
return NULL;
5928+
}
5929+
int res;
5930+
#include "_testcapi_feature_macros.inc"
5931+
return result;
5932+
}
5933+
59225934

59235935
static PyObject *negative_dictoffset(PyObject *, PyObject *);
59245936
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
@@ -6214,6 +6226,7 @@ static PyMethodDef TestMethods[] = {
62146226
{"frame_getgenerator", frame_getgenerator, METH_O, NULL},
62156227
{"frame_getbuiltins", frame_getbuiltins, METH_O, NULL},
62166228
{"frame_getlasti", frame_getlasti, METH_O, NULL},
6229+
{"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
62176230
{NULL, NULL} /* sentinel */
62186231
};
62196232

PC/python3dll.c

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)