Debian Codegraph @

Lib/test/test_cppext/extension.cpp

raw
1 // gh-91321: Very basic C++ test extension to check that the Python C API is
2 // compatible with C++ and does not emit C++ compiler warnings.
3 //
4 // The code is only built, not executed.
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_frame.h"
23 # include "internal/pycore_interpframe_structs.h"
24 # include "internal/pycore_template.h"
25
26 // mimalloc emits compiler warnings on Windows.
27 # if !defined(MS_WINDOWS)
28 # include "internal/pycore_backoff.h"
29 # include "internal/pycore_cell.h"
30 # include "internal/pycore_critical_section.h"
31 # include "internal/pycore_interpframe.h"
32 # endif
33 #endif
34
35 #ifndef MODULE_NAME
36 # error "MODULE_NAME macro must be defined"
37 #endif
38
39 #define _STR(NAME) #NAME
40 #define STR(NAME) _STR(NAME)
41
42 PyDoc_STRVAR(_testcppext_add_doc,
43 "add(x, y)\n"
44 "\n"
45 "Return the sum of two integers: x + y.");
46
47 static PyObject *
48 _testcppext_add(PyObject *Py_UNUSED(module), PyObject *args)
49 {
50 long i, j;
51 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) {
52 return _Py_NULL;
53 }
54 long res = i + j;
55 return PyLong_FromLong(res);
56 }
57
58
59 // Class to test operator casting an object to PyObject*
60 class StrongRef
61 {
62 public:
63 StrongRef(PyObject *obj) : m_obj(obj) {
64 Py_INCREF(this->m_obj);
65 }
66
67 ~StrongRef() {
68 Py_DECREF(this->m_obj);
69 }
70
71 // Cast to PyObject*: get a borrowed reference
72 inline operator PyObject*() const { return this->m_obj; }
73
74 private:
75 PyObject *m_obj; // Strong reference
76 };
77
78
79 static PyObject *
80 test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
81 {
82 PyObject *obj = Py_BuildValue("(ii)", 1, 2);
83 if (obj == _Py_NULL) {
84 return _Py_NULL;
85 }
86 Py_ssize_t refcnt = Py_REFCNT(obj);
87 assert(refcnt >= 1);
88
89 #ifndef Py_LIMITED_API
90 // gh-92138: For backward compatibility, functions of Python C API accepts
91 // "const PyObject*". Check that using it does not emit C++ compiler
92 // warnings.
93 const PyObject *const_obj = obj;
94 Py_INCREF(const_obj);
95 Py_DECREF(const_obj);
96 PyTypeObject *type = Py_TYPE(const_obj);
97 assert(Py_REFCNT(const_obj) == refcnt);
98 assert(type == &PyTuple_Type);
99 assert(PyTuple_GET_SIZE(const_obj) == 2);
100 PyObject *one = PyTuple_GET_ITEM(const_obj, 0);
101 assert(PyLong_AsLong(one) == 1);
102 #endif
103
104 // gh-92898: StrongRef doesn't inherit from PyObject but has an operator to
105 // cast to PyObject*.
106 StrongRef strong_ref(obj);
107 assert(Py_TYPE(strong_ref) == &PyTuple_Type);
108 assert(Py_REFCNT(strong_ref) == (refcnt + 1));
109 Py_INCREF(strong_ref);
110 Py_DECREF(strong_ref);
111
112 // gh-93442: Pass 0 as NULL for PyObject*
113 Py_XINCREF(0);
114 Py_XDECREF(0);
115 #if __cplusplus >= 201103
116 // Test nullptr passed as PyObject*
117 Py_XINCREF(nullptr);
118 Py_XDECREF(nullptr);
119 #endif
120
121 Py_DECREF(obj);
122 Py_RETURN_NONE;
123 }
124
125
126 static PyObject *
127 test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
128 {
129 PyObject *str = PyUnicode_FromString("abc");
130 if (str == _Py_NULL) {
131 return _Py_NULL;
132 }
133
134 assert(PyUnicode_Check(str));
135
136 assert(PyUnicode_GetLength(str) == 3);
137 assert(PyUnicode_ReadChar(str, 0) == 'a');
138 assert(PyUnicode_ReadChar(str, 1) == 'b');
139
140 #ifndef Py_LIMITED_API
141 assert(PyUnicode_GET_LENGTH(str) == 3);
142
143 // gh-92800: test PyUnicode_READ()
144 const void* data = PyUnicode_DATA(str);
145 assert(data != _Py_NULL);
146 int kind = PyUnicode_KIND(str);
147 assert(kind == PyUnicode_1BYTE_KIND);
148 assert(PyUnicode_READ(kind, data, 0) == 'a');
149
150 // gh-92800: test PyUnicode_READ() casts
151 const void* const_data = PyUnicode_DATA(str);
152 unsigned int ukind = static_cast<unsigned int>(kind);
153 assert(PyUnicode_READ(ukind, const_data, 2) == 'c');
154
155 assert(PyUnicode_READ_CHAR(str, 1) == 'b');
156 #endif
157
158 Py_DECREF(str);
159 Py_RETURN_NONE;
160 }
161
162 /* Test a `new`-allocated object with a virtual method.
163 * (https://github.com/python/cpython/issues/94731) */
164
165 class VirtualPyObject : public PyObject {
166 public:
167 VirtualPyObject();
168 virtual ~VirtualPyObject() {
169 delete [] internal_data;
170 --instance_count;
171 }
172 virtual void set_internal_data() {
173 internal_data[0] = 1;
174 }
175 static void dealloc(PyObject* o) {
176 delete static_cast<VirtualPyObject*>(o);
177 }
178
179 // Number of "living" instances
180 static int instance_count;
181 private:
182 // buffer that can get corrupted
183 int* internal_data;
184 };
185
186 int VirtualPyObject::instance_count = 0;
187
188 // Converting from function pointer to void* has undefined behavior, but
189 // works on all known platforms, and CPython's module and type slots currently
190 // need it.
191 // (GCC doesn't have a narrower category for this than -Wpedantic.)
192 _Py_COMP_DIAG_PUSH
193 #if defined(__GNUC__)
194 #pragma GCC diagnostic ignored "-Wpedantic"
195 #elif defined(__clang__)
196 #pragma clang diagnostic ignored "-Wpedantic"
197 #endif
198
199 PyType_Slot VirtualPyObject_Slots[] = {
200 {Py_tp_free, (void*)VirtualPyObject::dealloc},
201 {0, _Py_NULL},
202 };
203
204 _Py_COMP_DIAG_POP
205
206 PyType_Spec VirtualPyObject_Spec = {
207 /* .name */ STR(MODULE_NAME) ".VirtualPyObject",
208 /* .basicsize */ sizeof(VirtualPyObject),
209 /* .itemsize */ 0,
210 /* .flags */ Py_TPFLAGS_DEFAULT,
211 /* .slots */ VirtualPyObject_Slots,
212 };
213
214 VirtualPyObject::VirtualPyObject() {
215 // Create a temporary type (just so we don't need to store it)
216 PyObject *type = PyType_FromSpec(&VirtualPyObject_Spec);
217 // no good way to signal failure from a C++ constructor, so use assert
218 // for error handling
219 assert(type);
220 assert(PyObject_Init(this, (PyTypeObject *)type));
221 Py_DECREF(type);
222 internal_data = new int[50];
223 ++instance_count;
224 }
225
226 static PyObject *
227 test_virtual_object(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
228 {
229 VirtualPyObject* obj = new VirtualPyObject();
230 obj->set_internal_data();
231 Py_DECREF(obj);
232 if (VirtualPyObject::instance_count != 0) {
233 return PyErr_Format(
234 PyExc_AssertionError,
235 "instance_count should be 0, got %d",
236 VirtualPyObject::instance_count);
237 }
238 Py_RETURN_NONE;
239 }
240
241 static PyObject *
242 test_datetime(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
243 {
244 // datetime.h is excluded from the limited C API
245 #ifndef Py_LIMITED_API
246 PyDateTime_IMPORT;
247 if (PyErr_Occurred()) {
248 return NULL;
249 }
250 #endif
251
252 Py_RETURN_NONE;
253 }
254
255 static PyMethodDef _testcppext_methods[] = {
256 {"add", _testcppext_add, METH_VARARGS, _testcppext_add_doc},
257 {"test_api_casts", test_api_casts, METH_NOARGS, _Py_NULL},
258 {"test_unicode", test_unicode, METH_NOARGS, _Py_NULL},
259 {"test_virtual_object", test_virtual_object, METH_NOARGS, _Py_NULL},
260 {"test_datetime", test_datetime, METH_NOARGS, _Py_NULL},
261 // Note: _testcppext_exec currently runs all test functions directly.
262 // When adding a new one, add a call there.
263
264 {_Py_NULL, _Py_NULL, 0, _Py_NULL} /* sentinel */
265 };
266
267
268 static int
269 _testcppext_exec(PyObject *module)
270 {
271 if (PyModule_AddIntMacro(module, __cplusplus) < 0) {
272 return -1;
273 }
274
275 PyObject *result;
276
277 result = PyObject_CallMethod(module, "test_api_casts", "");
278 if (!result) return -1;
279 Py_DECREF(result);
280
281 result = PyObject_CallMethod(module, "test_unicode", "");
282 if (!result) return -1;
283 Py_DECREF(result);
284
285 result = PyObject_CallMethod(module, "test_virtual_object", "");
286 if (!result) return -1;
287 Py_DECREF(result);
288
289 result = PyObject_CallMethod(module, "test_datetime", "");
290 if (!result) return -1;
291 Py_DECREF(result);
292
293 // test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
294 Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
295 assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
296
297 return 0;
298 }
299
300 // Need to ignore "-Wpedantic" warnings; see VirtualPyObject_Slots above
301 _Py_COMP_DIAG_PUSH
302 #if defined(__GNUC__)
303 #pragma GCC diagnostic ignored "-Wpedantic"
304 #elif defined(__clang__)
305 #pragma clang diagnostic ignored "-Wpedantic"
306 #endif
307
308 static PyModuleDef_Slot _testcppext_slots[] = {
309 {Py_mod_exec, reinterpret_cast<void*>(_testcppext_exec)},
310 {0, _Py_NULL}
311 };
312
313 _Py_COMP_DIAG_POP
314
315 PyDoc_STRVAR(_testcppext_doc, "C++ test extension.");
316
317 static struct PyModuleDef _testcppext_module = {
318 PyModuleDef_HEAD_INIT, // m_base
319 STR(MODULE_NAME), // m_name
320 _testcppext_doc, // m_doc
321 0, // m_size
322 _testcppext_methods, // m_methods
323 _testcppext_slots, // m_slots
324 _Py_NULL, // m_traverse
325 _Py_NULL, // m_clear
326 _Py_NULL, // m_free
327 };
328
329 #define _FUNC_NAME(NAME) PyInit_ ## NAME
330 #define FUNC_NAME(NAME) _FUNC_NAME(NAME)
331
332 PyMODINIT_FUNC
333 FUNC_NAME(MODULE_NAME)(void)
334 {
335 return PyModuleDef_Init(&_testcppext_module);
336 }