2 * C Extension module to test Python interpreter C APIs.
4 * The 'test_*' functions exported by this module are run as part of the
5 * standard Python regression test, via Lib/test/test_capi.py.
10 static PyObject
*TestError
; /* set to exception object in init */
12 /* Raise TestError with test_name + ": " + msg, and return NULL. */
15 raiseTestError(const char* test_name
, const char* msg
)
19 if (strlen(test_name
) + strlen(msg
) > sizeof(buf
) - 50)
20 PyErr_SetString(TestError
, "internal error msg too large");
22 PyOS_snprintf(buf
, sizeof(buf
), "%s: %s", test_name
, msg
);
23 PyErr_SetString(TestError
, buf
);
28 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
30 The ones derived from autoconf on the UNIX-like OSes can be relied
31 upon (in the absence of sloppy cross-compiling), but the Windows
32 platforms have these hardcoded. Better safe than sorry.
35 sizeof_error(const char* fatname
, const char* typename
,
36 int expected
, int got
)
39 PyOS_snprintf(buf
, sizeof(buf
),
40 "%.200s #define == %d but sizeof(%.200s) == %d",
41 fatname
, expected
, typename
, got
);
42 PyErr_SetString(TestError
, buf
);
43 return (PyObject
*)NULL
;
47 test_config(PyObject
*self
, PyObject
*args
)
49 if (!PyArg_ParseTuple(args
, ":test_config"))
52 #define CHECK_SIZEOF(FATNAME, TYPE) \
53 if (FATNAME != sizeof(TYPE)) \
54 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
56 CHECK_SIZEOF(SIZEOF_SHORT
, short);
57 CHECK_SIZEOF(SIZEOF_INT
, int);
58 CHECK_SIZEOF(SIZEOF_LONG
, long);
59 CHECK_SIZEOF(SIZEOF_VOID_P
, void*);
60 CHECK_SIZEOF(SIZEOF_TIME_T
, time_t);
62 CHECK_SIZEOF(SIZEOF_LONG_LONG
, LONG_LONG
);
72 test_list_api(PyObject
*self
, PyObject
*args
)
76 if (!PyArg_ParseTuple(args
, ":test_list_api"))
79 /* SF bug 132008: PyList_Reverse segfaults */
81 list
= PyList_New(NLIST
);
82 if (list
== (PyObject
*)NULL
)
83 return (PyObject
*)NULL
;
84 /* list = range(NLIST) */
85 for (i
= 0; i
< NLIST
; ++i
) {
86 PyObject
* anint
= PyInt_FromLong(i
);
87 if (anint
== (PyObject
*)NULL
) {
89 return (PyObject
*)NULL
;
91 PyList_SET_ITEM(list
, i
, anint
);
93 /* list.reverse(), via PyList_Reverse() */
94 i
= PyList_Reverse(list
); /* should not blow up! */
97 return (PyObject
*)NULL
;
99 /* Check that list == range(29, -1, -1) now */
100 for (i
= 0; i
< NLIST
; ++i
) {
101 PyObject
* anint
= PyList_GET_ITEM(list
, i
);
102 if (PyInt_AS_LONG(anint
) != NLIST
-1-i
) {
103 PyErr_SetString(TestError
,
104 "test_list_api: reverse screwed up");
106 return (PyObject
*)NULL
;
117 test_dict_inner(int count
)
119 int pos
= 0, iterations
= 0, i
;
120 PyObject
*dict
= PyDict_New();
126 for (i
= 0; i
< count
; i
++) {
127 v
= PyInt_FromLong(i
);
128 PyDict_SetItem(dict
, v
, v
);
132 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
136 i
= PyInt_AS_LONG(v
) + 1;
137 o
= PyInt_FromLong(i
);
140 if (PyDict_SetItem(dict
, k
, o
) < 0) {
149 if (iterations
!= count
) {
152 "test_dict_iteration: dict iteration went wrong ");
160 test_dict_iteration(PyObject
* self
, PyObject
* args
)
164 if (!PyArg_ParseTuple(args
, ":test_dict_iteration"))
167 for (i
= 0; i
< 200; i
++) {
168 if (test_dict_inner(i
) < 0) {
178 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
179 PyLong_{As, From}{Unsigned,}LongLong().
181 Note that the meat of the test is contained in testcapi_long.h.
182 This is revolting, but delicate code duplication is worse: "almost
183 exactly the same" code is needed to test LONG_LONG, but the ubiquitous
184 dependence on type names makes it impossible to use a parameterized
185 function. A giant macro would be even worse than this. A C++ template
188 The "report an error" functions are deliberately not part of the #include
189 file: if the test fails, you can set a breakpoint in the appropriate
190 error function directly, and crawl back from there in the debugger.
193 #define UNBIND(X) Py_DECREF(X); (X) = NULL
196 raise_test_long_error(const char* msg
)
198 return raiseTestError("test_long_api", msg
);
201 #define TESTNAME test_long_api_inner
202 #define TYPENAME long
203 #define F_S_TO_PY PyLong_FromLong
204 #define F_PY_TO_S PyLong_AsLong
205 #define F_U_TO_PY PyLong_FromUnsignedLong
206 #define F_PY_TO_U PyLong_AsUnsignedLong
208 #include "testcapi_long.h"
211 test_long_api(PyObject
* self
, PyObject
* args
)
213 if (!PyArg_ParseTuple(args
, ":test_long_api"))
216 return TESTNAME(raise_test_long_error
);
226 #ifdef HAVE_LONG_LONG
229 raise_test_longlong_error(const char* msg
)
231 return raiseTestError("test_longlong_api", msg
);
234 #define TESTNAME test_longlong_api_inner
235 #define TYPENAME LONG_LONG
236 #define F_S_TO_PY PyLong_FromLongLong
237 #define F_PY_TO_S PyLong_AsLongLong
238 #define F_U_TO_PY PyLong_FromUnsignedLongLong
239 #define F_PY_TO_U PyLong_AsUnsignedLongLong
241 #include "testcapi_long.h"
244 test_longlong_api(PyObject
* self
, PyObject
* args
)
246 if (!PyArg_ParseTuple(args
, ":test_longlong_api"))
249 return TESTNAME(raise_test_longlong_error
);
259 /* Test the L code for PyArg_ParseTuple. This should deliver a LONG_LONG
260 for both long and int arguments. The test may leak a little memory if
264 test_L_code(PyObject
*self
, PyObject
*args
)
266 PyObject
*tuple
, *num
;
269 if (!PyArg_ParseTuple(args
, ":test_L_code"))
272 tuple
= PyTuple_New(1);
276 num
= PyLong_FromLong(42);
280 PyTuple_SET_ITEM(tuple
, 0, num
);
283 if (PyArg_ParseTuple(tuple
, "L:test_L_code", &value
) < 0)
286 return raiseTestError("test_L_code",
287 "L code returned wrong value for long 42");
290 num
= PyInt_FromLong(42);
294 PyTuple_SET_ITEM(tuple
, 0, num
);
297 if (PyArg_ParseTuple(tuple
, "L:test_L_code", &value
) < 0)
300 return raiseTestError("test_L_code",
301 "L code returned wrong value for int 42");
308 #endif /* ifdef HAVE_LONG_LONG */
311 raise_exception(PyObject
*self
, PyObject
*args
)
314 PyObject
*exc_args
, *v
;
317 if (!PyArg_ParseTuple(args
, "Oi:raise_exception",
321 exc_args
= PyTuple_New(num_args
);
322 if (exc_args
== NULL
)
324 for (i
= 0; i
< num_args
; ++i
) {
325 v
= PyInt_FromLong(i
);
330 PyTuple_SET_ITEM(exc_args
, i
, v
);
332 PyErr_SetObject(exc
, exc_args
);
336 static PyMethodDef TestMethods
[] = {
337 {"raise_exception", raise_exception
, METH_VARARGS
},
338 {"test_config", test_config
, METH_VARARGS
},
339 {"test_list_api", test_list_api
, METH_VARARGS
},
340 {"test_dict_iteration", test_dict_iteration
, METH_VARARGS
},
341 {"test_long_api", test_long_api
, METH_VARARGS
},
342 #ifdef HAVE_LONG_LONG
343 {"test_longlong_api", test_longlong_api
, METH_VARARGS
},
344 {"test_L_code", test_L_code
, METH_VARARGS
},
346 {NULL
, NULL
} /* sentinel */
354 m
= Py_InitModule("_testcapi", TestMethods
);
356 TestError
= PyErr_NewException("_testcapi.error", NULL
, NULL
);
357 d
= PyModule_GetDict(m
);
358 PyDict_SetItemString(d
, "error", TestError
);