Skip to content

Commit c34a4df

Browse files
committed
Add CAPI tests
1 parent 751c560 commit c34a4df

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Lib/test/test_capi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,5 +971,19 @@ def test_state_access(self):
971971
increment_count(1, 2, 3)
972972

973973

974+
class Test_PyModuleConst_Def(unittest.TestCase):
975+
def test_constants(self):
976+
self.assertIs(_testcapi.const_none, None)
977+
self.assertEqual(_testcapi.const_int, 42)
978+
self.assertEqual(_testcapi.const_uint, _testcapi.ULONG_MAX)
979+
self.assertIs(_testcapi.const_true, True)
980+
self.assertIs(_testcapi.const_false, False)
981+
self.assertEqual(_testcapi.const_almost_tau, 6.2831)
982+
self.assertEqual(_testcapi.const_str, "Hello")
983+
self.assertEqual(_testcapi.const_call, b"23")
984+
self.assertEqual(_testcapi.CONST_INT, 7)
985+
self.assertEqual(_testcapi.CONST_STRING, "world")
986+
987+
974988
if __name__ == "__main__":
975989
unittest.main()

Modules/_testcapimodule.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7010,6 +7010,29 @@ static PyTypeObject ContainerNoGC_type = {
70107010
.tp_new = ContainerNoGC_new,
70117011
};
70127012

7013+
static PyObject *
7014+
_testcapimodule_const_call(PyObject *module)
7015+
{
7016+
return PyBytes_FromString("23");
7017+
}
7018+
7019+
#define CONST_INT 7
7020+
#define CONST_STRING "world"
7021+
7022+
static PyModuleConst_Def _testcapimodule_consts[] = {
7023+
PyModuleConst_None("const_none"),
7024+
PyModuleConst_Long("const_int", 42),
7025+
PyModuleConst_ULong("const_uint", ULONG_MAX),
7026+
PyModuleConst_Bool("const_false", 0),
7027+
PyModuleConst_Bool("const_true", 1),
7028+
PyModuleConst_Double("const_almost_tau", 6.2831),
7029+
PyModuleConst_String("const_str", "Hello"),
7030+
PyModuleConst_Call("const_call", _testcapimodule_const_call),
7031+
PyModuleConst_LongMacro(CONST_INT),
7032+
PyModuleConst_StringMacro(CONST_STRING),
7033+
{NULL},
7034+
};
7035+
70137036

70147037
static struct PyModuleDef _testcapimodule = {
70157038
PyModuleDef_HEAD_INIT,
@@ -7035,6 +7058,11 @@ PyInit__testcapi(void)
70357058
if (m == NULL)
70367059
return NULL;
70377060

7061+
if (PyModule_AddConstants(m, _testcapimodule_consts) < 0) {
7062+
Py_DECREF(m);
7063+
return NULL;
7064+
}
7065+
70387066
Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type);
70397067

70407068
Py_SET_TYPE(&test_structmembersType, &PyType_Type);

0 commit comments

Comments
 (0)