Debian Codegraph @

Lib/test/test_cext/extension.c

raw
1 // gh-116869: Basic C test extension to check that the Python C API
2 // does not emit C compiler warnings.
3 //
4 // Test also the internal C API if the TEST_INTERNAL_C_API macro is defined.
5
6 // Always enable assertions
7 #undef NDEBUG
8
9 #ifdef TEST_INTERNAL_C_API
10 # define Py_BUILD_CORE_MODULE 1
11 #endif
12
13 #include "Python.h"
14 #include "datetime.h"
15
16 #ifdef TEST_INTERNAL_C_API
17 // gh-135906: Check for compiler warnings in the internal C API.
18 // - Cython uses pycore_critical_section.h, pycore_frame.h and
19 // pycore_template.h.
20 // - greenlet uses pycore_frame.h, pycore_interpframe_structs.h and
21 // pycore_interpframe.h.
22 # include "internal/pycore_critical_section.h"
23 # include "internal/pycore_frame.h"
24 # include "internal/pycore_gc.h"
25 # include "internal/pycore_interp.h"
26 # include "internal/pycore_interpframe.h"
27 # include "internal/pycore_interpframe_structs.h"
28 # include "internal/pycore_object.h"
29 # include "internal/pycore_pystate.h"
30 # include "internal/pycore_template.h"
31 #endif
32
33 #ifndef MODULE_NAME
34 # error "MODULE_NAME macro must be defined"
35 #endif
36
37 #define _STR(NAME) #NAME
38 #define STR(NAME) _STR(NAME)
39
40 PyDoc_STRVAR(_testcext_add_doc,
41 "add(x, y)\n"
42 "\n"
43 "Return the sum of two integers: x + y.");
44
45 static PyObject *
46 _testcext_add(PyObject *Py_UNUSED(module), PyObject *args)
47 {
48 long i, j, res;
49 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) {
50 return NULL;
51 }
52 res = i + j;
53 return PyLong_FromLong(res);
54 }
55
56
57 static PyObject *
58 test_datetime(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
59 {
60 // datetime.h is excluded from the limited C API
61 #ifndef Py_LIMITED_API
62 PyDateTime_IMPORT;
63 if (PyErr_Occurred()) {
64 return NULL;
65 }
66 #endif
67
68 Py_RETURN_NONE;
69 }
70
71
72 static PyMethodDef _testcext_methods[] = {
73 {"add", _testcext_add, METH_VARARGS, _testcext_add_doc},
74 {"test_datetime", test_datetime, METH_NOARGS, NULL},
75 {NULL, NULL, 0, NULL} // sentinel
76 };
77
78
79 static int
80 _testcext_exec(PyObject *module)
81 {
82 PyObject *result;
83
84 #ifdef __STDC_VERSION__
85 if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
86 return -1;
87 }
88 #endif
89
90 result = PyObject_CallMethod(module, "test_datetime", "");
91 if (!result) return -1;
92 Py_DECREF(result);
93
94 // test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
95 Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
96 assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
97
98 return 0;
99 }
100
101 // Converting from function pointer to void* has undefined behavior, but
102 // works on all known platforms, and CPython's module and type slots currently
103 // need it.
104 // (GCC doesn't have a narrower category for this than -Wpedantic.)
105 _Py_COMP_DIAG_PUSH
106 #if defined(__GNUC__)
107 #pragma GCC diagnostic ignored "-Wpedantic"
108 #elif defined(__clang__)
109 #pragma clang diagnostic ignored "-Wpedantic"
110 #endif
111
112 static PyModuleDef_Slot _testcext_slots[] = {
113 {Py_mod_exec, (void*)_testcext_exec},
114 {0, NULL}
115 };
116
117 _Py_COMP_DIAG_POP
118
119
120 PyDoc_STRVAR(_testcext_doc, "C test extension.");
121
122 static struct PyModuleDef _testcext_module = {
123 PyModuleDef_HEAD_INIT, // m_base
124 STR(MODULE_NAME), // m_name
125 _testcext_doc, // m_doc
126 0, // m_size
127 _testcext_methods, // m_methods
128 _testcext_slots, // m_slots
129 NULL, // m_traverse
130 NULL, // m_clear
131 NULL, // m_free
132 };
133
134
135 #define _FUNC_NAME(NAME) PyInit_ ## NAME
136 #define FUNC_NAME(NAME) _FUNC_NAME(NAME)
137
138 PyMODINIT_FUNC
139 FUNC_NAME(MODULE_NAME)(void)
140 {
141 return PyModuleDef_Init(&_testcext_module);
142 }