This commit was manufactured by cvs2svn to create tag 'r22a4-fork'.
[python/dscho.git] / Modules / _testcapimodule.c
blob1a875f77c0167f8b8df61b82a29a90c426ab6f0a
1 /*
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.
6 */
8 #include "Python.h"
10 static PyObject *TestError; /* set to exception object in init */
12 /* Raise TestError with test_name + ": " + msg, and return NULL. */
14 static PyObject *
15 raiseTestError(const char* test_name, const char* msg)
17 char buf[2048];
19 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
20 PyErr_SetString(TestError, "internal error msg too large");
21 else {
22 sprintf(buf, "%s: %s", test_name, msg);
23 PyErr_SetString(TestError, buf);
25 return NULL;
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.
34 static PyObject*
35 sizeof_error(const char* fatname, const char* typename,
36 int expected, int got)
38 char buf[1024];
39 sprintf(buf, "%s #define == %d but sizeof(%s) == %d",
40 fatname, expected, typename, got);
41 PyErr_SetString(TestError, buf);
42 return (PyObject*)NULL;
45 static PyObject*
46 test_config(PyObject *self, PyObject *args)
48 if (!PyArg_ParseTuple(args, ":test_config"))
49 return NULL;
51 #define CHECK_SIZEOF(FATNAME, TYPE) \
52 if (FATNAME != sizeof(TYPE)) \
53 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
55 CHECK_SIZEOF(SIZEOF_SHORT, short);
56 CHECK_SIZEOF(SIZEOF_INT, int);
57 CHECK_SIZEOF(SIZEOF_LONG, long);
58 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
59 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
60 #ifdef HAVE_LONG_LONG
61 CHECK_SIZEOF(SIZEOF_LONG_LONG, LONG_LONG);
62 #endif
64 #undef CHECK_SIZEOF
66 Py_INCREF(Py_None);
67 return Py_None;
70 static PyObject*
71 test_list_api(PyObject *self, PyObject *args)
73 PyObject* list;
74 int i;
75 if (!PyArg_ParseTuple(args, ":test_list_api"))
76 return NULL;
78 /* SF bug 132008: PyList_Reverse segfaults */
79 #define NLIST 30
80 list = PyList_New(NLIST);
81 if (list == (PyObject*)NULL)
82 return (PyObject*)NULL;
83 /* list = range(NLIST) */
84 for (i = 0; i < NLIST; ++i) {
85 PyObject* anint = PyInt_FromLong(i);
86 if (anint == (PyObject*)NULL) {
87 Py_DECREF(list);
88 return (PyObject*)NULL;
90 PyList_SET_ITEM(list, i, anint);
92 /* list.reverse(), via PyList_Reverse() */
93 i = PyList_Reverse(list); /* should not blow up! */
94 if (i != 0) {
95 Py_DECREF(list);
96 return (PyObject*)NULL;
98 /* Check that list == range(29, -1, -1) now */
99 for (i = 0; i < NLIST; ++i) {
100 PyObject* anint = PyList_GET_ITEM(list, i);
101 if (PyInt_AS_LONG(anint) != NLIST-1-i) {
102 PyErr_SetString(TestError,
103 "test_list_api: reverse screwed up");
104 Py_DECREF(list);
105 return (PyObject*)NULL;
108 Py_DECREF(list);
109 #undef NLIST
111 Py_INCREF(Py_None);
112 return Py_None;
115 static int
116 test_dict_inner(int count)
118 int pos = 0, iterations = 0, i;
119 PyObject *dict = PyDict_New();
120 PyObject *v, *k;
122 if (dict == NULL)
123 return -1;
125 for (i = 0; i < count; i++) {
126 v = PyInt_FromLong(i);
127 PyDict_SetItem(dict, v, v);
128 Py_DECREF(v);
131 while (PyDict_Next(dict, &pos, &k, &v)) {
132 PyObject *o;
133 iterations++;
135 i = PyInt_AS_LONG(v) + 1;
136 o = PyInt_FromLong(i);
137 if (o == NULL)
138 return -1;
139 if (PyDict_SetItem(dict, k, o) < 0) {
140 Py_DECREF(o);
141 return -1;
143 Py_DECREF(o);
146 Py_DECREF(dict);
148 if (iterations != count) {
149 PyErr_SetString(
150 TestError,
151 "test_dict_iteration: dict iteration went wrong ");
152 return -1;
153 } else {
154 return 0;
158 static PyObject*
159 test_dict_iteration(PyObject* self, PyObject* args)
161 int i;
163 if (!PyArg_ParseTuple(args, ":test_dict_iteration"))
164 return NULL;
166 for (i = 0; i < 200; i++) {
167 if (test_dict_inner(i) < 0) {
168 return NULL;
172 Py_INCREF(Py_None);
173 return Py_None;
177 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
178 PyLong_{As, From}{Unsigned,}LongLong().
180 Note that the meat of the test is contained in testcapi_long.h.
181 This is revolting, but delicate code duplication is worse: "almost
182 exactly the same" code is needed to test LONG_LONG, but the ubiquitous
183 dependence on type names makes it impossible to use a parameterized
184 function. A giant macro would be even worse than this. A C++ template
185 would be perfect.
187 The "report an error" functions are deliberately not part of the #include
188 file: if the test fails, you can set a breakpoint in the appropriate
189 error function directly, and crawl back from there in the debugger.
192 #define UNBIND(X) Py_DECREF(X); (X) = NULL
194 static PyObject *
195 raise_test_long_error(const char* msg)
197 return raiseTestError("test_long_api", msg);
200 #define TESTNAME test_long_api_inner
201 #define TYPENAME long
202 #define F_S_TO_PY PyLong_FromLong
203 #define F_PY_TO_S PyLong_AsLong
204 #define F_U_TO_PY PyLong_FromUnsignedLong
205 #define F_PY_TO_U PyLong_AsUnsignedLong
207 #include "testcapi_long.h"
209 static PyObject *
210 test_long_api(PyObject* self, PyObject* args)
212 if (!PyArg_ParseTuple(args, ":test_long_api"))
213 return NULL;
215 return TESTNAME(raise_test_long_error);
218 #undef TESTNAME
219 #undef TYPENAME
220 #undef F_S_TO_PY
221 #undef F_PY_TO_S
222 #undef F_U_TO_PY
223 #undef F_PY_TO_U
225 #ifdef HAVE_LONG_LONG
227 static PyObject *
228 raise_test_longlong_error(const char* msg)
230 return raiseTestError("test_longlong_api", msg);
233 #define TESTNAME test_longlong_api_inner
234 #define TYPENAME LONG_LONG
235 #define F_S_TO_PY PyLong_FromLongLong
236 #define F_PY_TO_S PyLong_AsLongLong
237 #define F_U_TO_PY PyLong_FromUnsignedLongLong
238 #define F_PY_TO_U PyLong_AsUnsignedLongLong
240 #include "testcapi_long.h"
242 static PyObject *
243 test_longlong_api(PyObject* self, PyObject* args)
245 if (!PyArg_ParseTuple(args, ":test_longlong_api"))
246 return NULL;
248 return TESTNAME(raise_test_longlong_error);
251 #undef TESTNAME
252 #undef TYPENAME
253 #undef F_S_TO_PY
254 #undef F_PY_TO_S
255 #undef F_U_TO_PY
256 #undef F_PY_TO_U
258 #endif /* ifdef HAVE_LONG_LONG */
260 static PyObject *
261 raise_exception(PyObject *self, PyObject *args)
263 PyObject *exc;
264 PyObject *exc_args, *v;
265 int num_args, i;
267 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
268 &exc, &num_args))
269 return NULL;
271 exc_args = PyTuple_New(num_args);
272 if (exc_args == NULL)
273 return NULL;
274 for (i = 0; i < num_args; ++i) {
275 v = PyInt_FromLong(i);
276 if (v == NULL) {
277 Py_DECREF(exc_args);
278 return NULL;
280 PyTuple_SET_ITEM(exc_args, i, v);
282 PyErr_SetObject(exc, exc_args);
283 return NULL;
286 static PyMethodDef TestMethods[] = {
287 {"raise_exception", raise_exception, METH_VARARGS},
288 {"test_config", test_config, METH_VARARGS},
289 {"test_list_api", test_list_api, METH_VARARGS},
290 {"test_dict_iteration", test_dict_iteration, METH_VARARGS},
291 {"test_long_api", test_long_api, METH_VARARGS},
292 #ifdef HAVE_LONG_LONG
293 {"test_longlong_api", test_longlong_api, METH_VARARGS},
294 #endif
295 {NULL, NULL} /* sentinel */
298 DL_EXPORT(void)
299 init_testcapi(void)
301 PyObject *m, *d;
303 m = Py_InitModule("_testcapi", TestMethods);
305 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
306 d = PyModule_GetDict(m);
307 PyDict_SetItemString(d, "error", TestError);