blob: 24cf2b3618b9a5556fd7ebfb53dafe9ef4806e95 [file] [log] [blame]
Tim Peters9ea17ac2001-02-02 05:57:15 +00001/*
2 * C Extension module to test Python interpreter C APIs.
3 *
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 */
7
8#include "Python.h"
9
10static PyObject *TestError; /* set to exception object in init */
11
12/* Test #defines from config.h (particularly the SIZEOF_* defines).
13
14 The ones derived from autoconf on the UNIX-like OSes can be relied
15 upon (in the absence of sloppy cross-compiling), but the Windows
16 platforms have these hardcoded. Better safe than sorry.
17*/
18static PyObject*
19sizeof_error(const char* fatname, const char* typename,
20 int expected, int got)
21{
22 char buf[1024];
23 sprintf(buf, "%s #define == %d but sizeof(%s) == %d",
24 fatname, expected, typename, got);
25 PyErr_SetString(TestError, buf);
26 return (PyObject*)NULL;
27}
28
29static PyObject*
30test_config(PyObject *self, PyObject *args)
31{
32 if (!PyArg_ParseTuple(args, ":test_config"))
33 return NULL;
34
35#define CHECK_SIZEOF(FATNAME, TYPE) \
36 if (FATNAME != sizeof(TYPE)) \
37 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
38
39 CHECK_SIZEOF(SIZEOF_INT, int);
40 CHECK_SIZEOF(SIZEOF_LONG, long);
41 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
42 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
43#ifdef HAVE_LONG_LONG
44 CHECK_SIZEOF(SIZEOF_LONG_LONG, LONG_LONG);
45#endif
46
47#undef CHECK_SIZEOF
48
49 Py_INCREF(Py_None);
50 return Py_None;
51}
52
Tim Peters5c4d5bf2001-02-12 22:13:26 +000053static PyObject*
54test_list_api(PyObject *self, PyObject *args)
55{
56 PyObject* list;
57 int i;
58 if (!PyArg_ParseTuple(args, ":test_list_api"))
59 return NULL;
60
61 /* SF bug 132008: PyList_Reverse segfaults */
62#define NLIST 30
63 list = PyList_New(NLIST);
64 if (list == (PyObject*)NULL)
65 return (PyObject*)NULL;
66 /* list = range(NLIST) */
67 for (i = 0; i < NLIST; ++i) {
68 PyObject* anint = PyInt_FromLong(i);
69 if (anint == (PyObject*)NULL) {
70 Py_DECREF(list);
71 return (PyObject*)NULL;
72 }
73 PyList_SET_ITEM(list, i, anint);
74 }
75 /* list.reverse(), via PyList_Reverse() */
76 i = PyList_Reverse(list); /* should not blow up! */
77 if (i != 0) {
78 Py_DECREF(list);
79 return (PyObject*)NULL;
80 }
81 /* Check that list == range(29, -1, -1) now */
82 for (i = 0; i < NLIST; ++i) {
83 PyObject* anint = PyList_GET_ITEM(list, i);
84 if (PyInt_AS_LONG(anint) != NLIST-1-i) {
85 PyErr_SetString(TestError,
86 "test_list_api: reverse screwed up");
87 Py_DECREF(list);
88 return (PyObject*)NULL;
89 }
90 }
91 Py_DECREF(list);
92#undef NLIST
93
94 Py_INCREF(Py_None);
95 return Py_None;
96}
97
Guido van Rossumeb0d9922001-04-13 17:08:15 +000098static int
99test_dict_inner(int count)
100{
101 int pos = 0, iterations = 0, i;
102 PyObject *dict = PyDict_New();
103 PyObject *v, *k;
104
105 if (dict == NULL)
106 return -1;
107
108 for (i = 0; i < count; i++) {
109 v = PyInt_FromLong(i);
110 PyDict_SetItem(dict, v, v);
111 Py_DECREF(v);
112 }
113
114 while (PyDict_Next(dict, &pos, &k, &v)) {
115 PyObject *o;
116 iterations++;
117
118 i = PyInt_AS_LONG(v) + 1;
119 o = PyInt_FromLong(i);
120 if (o == NULL)
121 return -1;
122 if (PyDict_SetItem(dict, k, o) < 0) {
123 Py_DECREF(o);
124 return -1;
125 }
126 Py_DECREF(o);
127 }
128
129 Py_DECREF(dict);
130
131 if (iterations != count) {
132 PyErr_SetString(
133 TestError,
134 "test_dict_iteration: dict iteration went wrong ");
135 return -1;
136 } else {
137 return 0;
138 }
139}
140
141static PyObject*
142test_dict_iteration(PyObject* self, PyObject* args)
143{
144 int i;
145
146 if (!PyArg_ParseTuple(args, ":test_dict_iteration"))
147 return NULL;
148
149 for (i = 0; i < 200; i++) {
150 if (test_dict_inner(i) < 0) {
151 return NULL;
152 }
153 }
154
155 Py_INCREF(Py_None);
156 return Py_None;
157}
158
Tim Peters9ea17ac2001-02-02 05:57:15 +0000159static PyMethodDef TestMethods[] = {
160 {"test_config", test_config, METH_VARARGS},
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000161 {"test_list_api", test_list_api, METH_VARARGS},
Guido van Rossumeb0d9922001-04-13 17:08:15 +0000162 {"test_dict_iteration", test_dict_iteration, METH_VARARGS},
Tim Peters9ea17ac2001-02-02 05:57:15 +0000163 {NULL, NULL} /* sentinel */
164};
165
166DL_EXPORT(void)
Tim Petersd66595f2001-02-04 03:09:53 +0000167init_testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +0000168{
169 PyObject *m, *d;
170
Tim Petersd66595f2001-02-04 03:09:53 +0000171 m = Py_InitModule("_testcapi", TestMethods);
Tim Peters9ea17ac2001-02-02 05:57:15 +0000172
Tim Petersd66595f2001-02-04 03:09:53 +0000173 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
Tim Peters9ea17ac2001-02-02 05:57:15 +0000174 d = PyModule_GetDict(m);
175 PyDict_SetItemString(d, "error", TestError);
176}