Files
typthon/Lib/test/test_cext/extension.c
copilot-swe-agent[bot] ab21f072ba Fix remaining Py_ patterns - macros and config functions
- Fixed Py_MIN, Py_MAX, Py_ABS → Ty_*
- Fixed _Py_popcount32, _Py_bit_length → _Ty_*
- Fixed Py_ISALPHA, Py_ISSPACE, Py_CHARMASK → Ty_*
- Fixed Py_BUILD_ASSERT, Py_VISIT → Ty_*
- Fixed all PyConfig_* function definitions (InitPythonConfig, Clear, SetArgv, etc.)
- Fixed PyWideStringList → TyWideStringList

Resolving final linking errors.

Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
2025-12-29 19:30:40 +00:00

103 lines
2.2 KiB
C

// 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"
#ifndef MODULE_NAME
# error "MODULE_NAME macro must be defined"
#endif
#define _STR(NAME) #NAME
#define STR(NAME) _STR(NAME)
TyDoc_STRVAR(_testcext_add_doc,
"add(x, y)\n"
"\n"
"Return the sum of two integers: x + y.");
static TyObject *
_testcext_add(TyObject *Py_UNUSED(module), TyObject *args)
{
long i, j, res;
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) {
return NULL;
}
res = i + j;
return TyLong_FromLong(res);
}
static TyMethodDef _testcext_methods[] = {
{"add", _testcext_add, METH_VARARGS, _testcext_add_doc},
{NULL, NULL, 0, NULL} // sentinel
};
static int
_testcext_exec(
#ifdef __STDC_VERSION__
TyObject *module
#else
TyObject *Py_UNUSED(module)
#endif
)
{
#ifdef __STDC_VERSION__
if (TyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
return -1;
}
#endif
// test Ty_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
Ty_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
return 0;
}
// Converting from function pointer to void* has undefined behavior, but
// works on all known platforms, and CPython's module and type slots currently
// need it.
// (GCC doesn't have a narrower category for this than -Wpedantic.)
_Ty_COMP_DIAG_PUSH
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wpedantic"
#elif defined(__clang__)
#pragma clang diagnostic ignored "-Wpedantic"
#endif
static PyModuleDef_Slot _testcext_slots[] = {
{Ty_mod_exec, (void*)_testcext_exec},
{0, NULL}
};
_Ty_COMP_DIAG_POP
TyDoc_STRVAR(_testcext_doc, "C test extension.");
static struct TyModuleDef _testcext_module = {
PyModuleDef_HEAD_INIT, // m_base
STR(MODULE_NAME), // m_name
_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
FUNC_NAME(MODULE_NAME)(void)
{
return PyModuleDef_Init(&_testcext_module);
}