| 1 |
// gh-116869: Basic C test extension to check that the Python C API |
| 2 |
// does not emit C compiler warnings. |
| 3 |
|
| 4 |
// Always enable assertions |
| 5 |
#undef NDEBUG |
| 6 |
|
| 7 |
#include "Python.h" |
| 8 |
|
| 9 |
#ifndef MODULE_NAME |
| 10 |
# error "MODULE_NAME macro must be defined" |
| 11 |
#endif |
| 12 |
|
| 13 |
#define _STR(NAME) #NAME |
| 14 |
#define STR(NAME) _STR(NAME) |
| 15 |
|
| 16 |
PyDoc_STRVAR(_testcext_add_doc, |
| 17 |
"add(x, y)\n" |
| 18 |
"\n" |
| 19 |
"Return the sum of two integers: x + y."); |
| 20 |
|
| 21 |
static PyObject * |
| 22 |
_testcext_add(PyObject *Py_UNUSED(module), PyObject *args) |
| 23 |
{ |
| 24 |
long i, j, res; |
| 25 |
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) { |
| 26 |
return NULL; |
| 27 |
} |
| 28 |
res = i + j; |
| 29 |
return PyLong_FromLong(res); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
static PyMethodDef _testcext_methods[] = { |
| 34 |
{"add", _testcext_add, METH_VARARGS, _testcext_add_doc}, |
| 35 |
{NULL, NULL, 0, NULL} // sentinel |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
static int |
| 40 |
_testcext_exec( |
| 41 |
#ifdef __STDC_VERSION__ |
| 42 |
PyObject *module |
| 43 |
#else |
| 44 |
PyObject *Py_UNUSED(module) |
| 45 |
#endif |
| 46 |
) |
| 47 |
{ |
| 48 |
#ifdef __STDC_VERSION__ |
| 49 |
if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) { |
| 50 |
return -1; |
| 51 |
} |
| 52 |
#endif |
| 53 |
|
| 54 |
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR() |
| 55 |
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int)); |
| 56 |
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0); |
| 57 |
|
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static PyModuleDef_Slot _testcext_slots[] = { |
| 62 |
{Py_mod_exec, (void*)_testcext_exec}, |
| 63 |
{0, NULL} |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
PyDoc_STRVAR(_testcext_doc, "C test extension."); |
| 68 |
|
| 69 |
static struct PyModuleDef _testcext_module = { |
| 70 |
PyModuleDef_HEAD_INIT, // m_base |
| 71 |
STR(MODULE_NAME), // m_name |
| 72 |
_testcext_doc, // m_doc |
| 73 |
0, // m_size |
| 74 |
_testcext_methods, // m_methods |
| 75 |
_testcext_slots, // m_slots |
| 76 |
NULL, // m_traverse |
| 77 |
NULL, // m_clear |
| 78 |
NULL, // m_free |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
#define _FUNC_NAME(NAME) PyInit_ ## NAME |
| 83 |
#define FUNC_NAME(NAME) _FUNC_NAME(NAME) |
| 84 |
|
| 85 |
PyMODINIT_FUNC |
| 86 |
FUNC_NAME(MODULE_NAME)(void) |
| 87 |
{ |
| 88 |
return PyModuleDef_Init(&_testcext_module); |
| 89 |
} |