Skip to content

Commit 8298b35

Browse files
committed
Check for 'static inline'
Build a C extension with two C files to check that all pythoncapi_compat.h functions are declared with "static inline".
1 parent b7e486f commit 8298b35

4 files changed

Lines changed: 64 additions & 4 deletions

File tree

‎tests/extension.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// C extension made of two C files (extension.c and static_inline.c) to detect
2+
// when a pythoncapi_compat.h function is implemented without "static inline".
3+
4+
#ifndef MODULE_NAME
5+
# error "MODULE_NAME macro is not set"
6+
#endif
7+
8+
#include "pythoncapi_compat.h"
9+
10+
#define STR(NAME) #NAME
11+
#define CONCAT(a, b) a ## b
12+
13+
// No method and no module exec method needed, most basic C extension
14+
15+
static struct PyModuleDef module_def = {
16+
PyModuleDef_HEAD_INIT,
17+
STR(MODULE_NAME), // m_name
18+
_Py_NULL, // m_doc
19+
0, // m_size
20+
_Py_NULL, // m_methods
21+
_Py_NULL, // m_slots
22+
_Py_NULL, // m_traverse
23+
_Py_NULL, // m_clear
24+
_Py_NULL, // m_free
25+
};
26+
27+
28+
#define INIT_FUNC CONCAT(PyInit_, MODULE_NAME)
29+
30+
PyMODINIT_FUNC
31+
INIT_FUNC(void)
32+
{
33+
return PyModule_Create(&module_def);
34+
}

‎tests/setup.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,26 @@ def add_common_flags(flags, limited):
202202
language='c++')
203203
extensions.append(ext)
204204

205+
# Build a C extension with two C files to detect when a pythoncapi_compat.h
206+
# function is implemented without "static inline".
207+
sources = [
208+
'extension.c',
209+
'static_inline.c',
210+
]
211+
for limited in (False, True):
212+
name = 'test_pythoncapi_compat_ext'
213+
assert name != C_EXTENSION_PREFIX
214+
flags = list(COMMON_FLAGS)
215+
if limited:
216+
name += LIMITED_SUFFIX
217+
flags.append(limited_flag)
218+
flags.append(f'-DMODULE_NAME={name}')
219+
ext = Extension(
220+
name,
221+
sources=sources,
222+
extra_compile_args=flags)
223+
extensions.append(ext)
224+
205225
setup(name="test_pythoncapi_compat", ext_modules=extensions)
206226

207227

‎tests/static_inline.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// C extension made of two C files (extension.c and static_inline.c) to detect
2+
// when a pythoncapi_compat.h function is implemented without "static inline".
3+
4+
#include "pythoncapi_compat.h"
5+
6+
// No function defined, only #include "pythoncapi_compat.h"

‎tests/test_pythoncapi_compat_cext.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Always enable assertions
22
#undef NDEBUG
33

4+
#ifndef MODULE_NAME
5+
# error "MODULE_NAME macro is not set"
6+
#endif
7+
48
#include "pythoncapi_compat.h"
59
#include <structmember.h> // T_SHORT, READONLY
610

711
#ifdef NDEBUG
812
# error "assertions must be enabled"
913
#endif
1014

11-
#ifndef MODULE_NAME
12-
# error "MODULE_NAME macro is not set"
13-
#endif
14-
1515
#define _STR(NAME) #NAME
1616
#define STR(NAME) _STR(NAME)
1717
#define _CONCAT(a, b) a ## b

0 commit comments

Comments
 (0)