Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0107655

Browse files
committed
Issue #19433: test_capi: add tests on the size of some C types
1 parent 68b674c commit 0107655

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Modules/_testcapimodule.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,45 @@ test_config(PyObject *self)
6464
return Py_None;
6565
}
6666

67+
static PyObject*
68+
test_sizeof_c_types(PyObject *self)
69+
{
70+
#define CHECK_SIZEOF(EXPECTED, TYPE) \
71+
if (EXPECTED != sizeof(TYPE)) { \
72+
PyErr_Format(TestError, \
73+
"sizeof(%s) = %u instead of %u", \
74+
#TYPE, sizeof(TYPE), EXPECTED); \
75+
return (PyObject*)NULL; \
76+
}
77+
78+
/* integer types */
79+
CHECK_SIZEOF(1, Py_UCS1);
80+
CHECK_SIZEOF(2, Py_UCS2);
81+
CHECK_SIZEOF(4, Py_UCS4);
82+
#ifdef HAVE_INT32_T
83+
CHECK_SIZEOF(4, PY_INT32_T);
84+
#endif
85+
#ifdef HAVE_UINT32_T
86+
CHECK_SIZEOF(4, PY_UINT32_T);
87+
#endif
88+
#ifdef HAVE_INT64_T
89+
CHECK_SIZEOF(8, PY_INT64_T);
90+
#endif
91+
#ifdef HAVE_UINT64_T
92+
CHECK_SIZEOF(8, PY_UINT64_T);
93+
#endif
94+
95+
/* pointer/size types */
96+
CHECK_SIZEOF(sizeof(void *), size_t);
97+
CHECK_SIZEOF(sizeof(void *), Py_ssize_t);
98+
99+
Py_INCREF(Py_None);
100+
return Py_None;
101+
102+
#undef CHECK_SIZEOF
103+
}
104+
105+
67106
static PyObject*
68107
test_list_api(PyObject *self)
69108
{
@@ -2783,6 +2822,7 @@ static PyMethodDef TestMethods[] = {
27832822
{"raise_exception", raise_exception, METH_VARARGS},
27842823
{"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
27852824
{"test_config", (PyCFunction)test_config, METH_NOARGS},
2825+
{"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS},
27862826
{"test_datetime_capi", test_datetime_capi, METH_NOARGS},
27872827
{"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
27882828
{"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},

0 commit comments

Comments
 (0)