blob: 0ffdc17669852a8e9eceae5dc4af03d5229db757 [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
Tim Peters9ea17ac2001-02-02 05:57:15 +000098static PyMethodDef TestMethods[] = {
99 {"test_config", test_config, METH_VARARGS},
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000100 {"test_list_api", test_list_api, METH_VARARGS},
Tim Peters9ea17ac2001-02-02 05:57:15 +0000101 {NULL, NULL} /* sentinel */
102};
103
104DL_EXPORT(void)
Tim Petersd66595f2001-02-04 03:09:53 +0000105init_testcapi(void)
Tim Peters9ea17ac2001-02-02 05:57:15 +0000106{
107 PyObject *m, *d;
108
Tim Petersd66595f2001-02-04 03:09:53 +0000109 m = Py_InitModule("_testcapi", TestMethods);
Tim Peters9ea17ac2001-02-02 05:57:15 +0000110
Tim Petersd66595f2001-02-04 03:09:53 +0000111 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
Tim Peters9ea17ac2001-02-02 05:57:15 +0000112 d = PyModule_GetDict(m);
113 PyDict_SetItemString(d, "error", TestError);
114}