2024-03-18 20:15:20 +01:00
|
|
|
// gh-116869: Basic C test extension to check that the Python C API
|
|
|
|
// does not emit C compiler warnings.
|
|
|
|
|
|
|
|
// Always enable assertions
|
|
|
|
#undef NDEBUG
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
2024-03-19 13:19:45 +01:00
|
|
|
#ifndef MODULE_NAME
|
|
|
|
# error "MODULE_NAME macro must be defined"
|
2024-03-18 20:15:20 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define _STR(NAME) #NAME
|
|
|
|
#define STR(NAME) _STR(NAME)
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_testcext_add_doc,
|
|
|
|
"add(x, y)\n"
|
|
|
|
"\n"
|
|
|
|
"Return the sum of two integers: x + y.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_testcext_add(PyObject *Py_UNUSED(module), PyObject *args)
|
|
|
|
{
|
2024-03-19 13:19:45 +01:00
|
|
|
long i, j, res;
|
2024-03-18 20:15:20 +01:00
|
|
|
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-03-19 13:19:45 +01:00
|
|
|
res = i + j;
|
2024-03-18 20:15:20 +01:00
|
|
|
return PyLong_FromLong(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef _testcext_methods[] = {
|
|
|
|
{"add", _testcext_add, METH_VARARGS, _testcext_add_doc},
|
|
|
|
{NULL, NULL, 0, NULL} // sentinel
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2024-09-19 22:32:01 +02:00
|
|
|
_testcext_exec(
|
|
|
|
#ifdef __STDC_VERSION__
|
|
|
|
PyObject *module
|
|
|
|
#else
|
|
|
|
PyObject *Py_UNUSED(module)
|
|
|
|
#endif
|
|
|
|
)
|
2024-03-18 20:15:20 +01:00
|
|
|
{
|
2024-03-19 15:03:27 +01:00
|
|
|
#ifdef __STDC_VERSION__
|
2024-03-18 20:15:20 +01:00
|
|
|
if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-19 15:03:27 +01:00
|
|
|
#endif
|
2024-04-30 22:29:48 +02:00
|
|
|
|
|
|
|
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
|
|
|
|
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
|
|
|
|
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
|
|
|
|
|
2024-03-18 20:15:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyModuleDef_Slot _testcext_slots[] = {
|
2024-09-19 22:32:01 +02:00
|
|
|
{Py_mod_exec, (void*)_testcext_exec},
|
2024-03-18 20:15:20 +01:00
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PyDoc_STRVAR(_testcext_doc, "C test extension.");
|
|
|
|
|
|
|
|
static struct PyModuleDef _testcext_module = {
|
|
|
|
PyModuleDef_HEAD_INIT, // m_base
|
2024-03-19 13:19:45 +01:00
|
|
|
STR(MODULE_NAME), // m_name
|
2024-03-18 20:15:20 +01:00
|
|
|
_testcext_doc, // m_doc
|
|
|
|
0, // m_size
|
|
|
|
_testcext_methods, // m_methods
|
|
|
|
_testcext_slots, // m_slots
|
|
|
|
NULL, // m_traverse
|
|
|
|
NULL, // m_clear
|
|
|
|
NULL, // m_free
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define _FUNC_NAME(NAME) PyInit_ ## NAME
|
|
|
|
#define FUNC_NAME(NAME) _FUNC_NAME(NAME)
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
2024-03-19 13:19:45 +01:00
|
|
|
FUNC_NAME(MODULE_NAME)(void)
|
2024-03-18 20:15:20 +01:00
|
|
|
{
|
|
|
|
return PyModuleDef_Init(&_testcext_module);
|
|
|
|
}
|