0
0
mirror of https://github.com/python/cpython.git synced 2024-11-24 00:38:00 +01:00
cpython/Modules/_testsinglephase.c
Eric Snow d45cc80452
gh-98627: Add the _testsinglephase Module (gh-99039)
This makes it more clear that a given test is definitely testing against a single-phase init (legacy) extension module.  The new module is a companion to _testmultiphase.

https://github.com/python/cpython/issues/98627
2022-11-08 09:58:11 -07:00

79 lines
1.7 KiB
C

/* Testing module for single-phase initialization of extension modules
*/
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#include "Python.h"
#include "pycore_namespace.h" // _PyNamespace_New()
/* Function of two integers returning integer */
PyDoc_STRVAR(testexport_foo_doc,
"foo(i,j)\n\
\n\
Return the sum of i and j.");
static PyObject *
testexport_foo(PyObject *self, PyObject *args)
{
long i, j;
long res;
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
return NULL;
res = i + j;
return PyLong_FromLong(res);
}
static PyMethodDef TestMethods[] = {
{"foo", testexport_foo, METH_VARARGS,
testexport_foo_doc},
{NULL, NULL} /* sentinel */
};
static struct PyModuleDef _testsinglephase = {
PyModuleDef_HEAD_INIT,
.m_name = "_testsinglephase",
.m_doc = PyDoc_STR("Test module _testsinglephase (main)"),
.m_size = -1, // no module state
.m_methods = TestMethods,
};
PyMODINIT_FUNC
PyInit__testsinglephase(void)
{
PyObject *module = PyModule_Create(&_testsinglephase);
if (module == NULL) {
return NULL;
}
/* Add an exception type */
PyObject *temp = PyErr_NewException("_testsinglephase.error", NULL, NULL);
if (temp == NULL) {
goto error;
}
if (PyModule_AddObject(module, "error", temp) != 0) {
Py_DECREF(temp);
goto error;
}
if (PyModule_AddIntConstant(module, "int_const", 1969) != 0) {
goto error;
}
if (PyModule_AddStringConstant(module, "str_const", "something different") != 0) {
goto error;
}
return module;
error:
Py_DECREF(module);
return NULL;
}