Skip to content

Commit 896f4d0

Browse files
committed
Generate feature macros dict for runtime check from the manifest
1 parent e623b12 commit 896f4d0

File tree

3 files changed

+71
-43
lines changed

3 files changed

+71
-43
lines changed
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: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5927,49 +5927,7 @@ get_feature_macros(PyObject *self, PyObject *Py_UNUSED(args))
59275927
return NULL;
59285928
}
59295929
int res;
5930-
// Add an entry in the dict for each of the listed macro.
5931-
// To regenerate, run:
5932-
// python Tools/clinic/clinic.py Modules/_testcapimodule.c
5933-
/*[python input]
5934-
for macro in 'MS_WINDOWS', 'HAVE_FORK', 'USE_STACKCHECK', 'PY_HAVE_THREAD_NATIVE_ID', 'Py_REF_DEBUG':
5935-
print(f'#ifdef {macro}')
5936-
print(f' res = PyDict_SetItemString(result, "{macro}", Py_True);')
5937-
print('#else')
5938-
print(f' res = PyDict_SetItemString(result, "{macro}", Py_False);')
5939-
print('#endif')
5940-
print(' if (res) { Py_DECREF(result); return NULL; }')
5941-
[python start generated code]*/
5942-
#ifdef MS_WINDOWS
5943-
res = PyDict_SetItemString(result, "MS_WINDOWS", Py_True);
5944-
#else
5945-
res = PyDict_SetItemString(result, "MS_WINDOWS", Py_False);
5946-
#endif
5947-
if (res) { Py_DECREF(result); return NULL; }
5948-
#ifdef HAVE_FORK
5949-
res = PyDict_SetItemString(result, "HAVE_FORK", Py_True);
5950-
#else
5951-
res = PyDict_SetItemString(result, "HAVE_FORK", Py_False);
5952-
#endif
5953-
if (res) { Py_DECREF(result); return NULL; }
5954-
#ifdef USE_STACKCHECK
5955-
res = PyDict_SetItemString(result, "USE_STACKCHECK", Py_True);
5956-
#else
5957-
res = PyDict_SetItemString(result, "USE_STACKCHECK", Py_False);
5958-
#endif
5959-
if (res) { Py_DECREF(result); return NULL; }
5960-
#ifdef PY_HAVE_THREAD_NATIVE_ID
5961-
res = PyDict_SetItemString(result, "PY_HAVE_THREAD_NATIVE_ID", Py_True);
5962-
#else
5963-
res = PyDict_SetItemString(result, "PY_HAVE_THREAD_NATIVE_ID", Py_False);
5964-
#endif
5965-
if (res) { Py_DECREF(result); return NULL; }
5966-
#ifdef Py_REF_DEBUG
5967-
res = PyDict_SetItemString(result, "Py_REF_DEBUG", Py_True);
5968-
#else
5969-
res = PyDict_SetItemString(result, "Py_REF_DEBUG", Py_False);
5970-
#endif
5971-
if (res) { Py_DECREF(result); return NULL; }
5972-
/*[python end generated code: output=ac764c7dc1c0abdd input=58ecf3e00ea5b9fa]*/
5930+
#include "_testcapi_feature_macros.inc"
59735931
return result;
59745932
}
59755933

Tools/scripts/stable_abi.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,27 @@ def test_windows_feature_macros(self):
370370
write(f"WINDOWS_IFDEFS = {windows_ifdef_values}")
371371

372372

373+
@generator("testcapi_feature_macros", 'Modules/_testcapi_feature_macros.inc')
374+
def gen_testcapi_feature_macros(manifest, args, outfile):
375+
"""Generate/check the stable ABI list for documentation annotations"""
376+
write = partial(print, file=outfile)
377+
write('// Generated by Tools/scripts/stable_abi.py')
378+
write()
379+
write('// Add an entry in dict `result` for each Stable ABI feature macro.')
380+
write()
381+
for macro in manifest.select({'ifdef'}):
382+
name = macro.name
383+
write(f'#ifdef {name}')
384+
write(f' res = PyDict_SetItemString(result, "{name}", Py_True);')
385+
write('#else')
386+
write(f' res = PyDict_SetItemString(result, "{name}", Py_False);')
387+
write('#endif')
388+
write('if (res) {')
389+
write(' Py_DECREF(result); return NULL;')
390+
write('}')
391+
write()
392+
393+
373394
def generate_or_check(manifest, args, path, func):
374395
"""Generate/check a file with a single generator
375396

0 commit comments

Comments
 (0)