Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 10 | static 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 | */ |
| 18 | static PyObject* |
| 19 | sizeof_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 | |
| 29 | static PyObject* |
| 30 | test_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 Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 53 | static PyObject* |
| 54 | test_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 Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 98 | static int |
| 99 | test_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 | |
| 141 | static PyObject* |
| 142 | test_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 Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 159 | static PyMethodDef TestMethods[] = { |
| 160 | {"test_config", test_config, METH_VARARGS}, |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 161 | {"test_list_api", test_list_api, METH_VARARGS}, |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 162 | {"test_dict_iteration", test_dict_iteration, METH_VARARGS}, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 163 | {NULL, NULL} /* sentinel */ |
| 164 | }; |
| 165 | |
| 166 | DL_EXPORT(void) |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 167 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 168 | { |
| 169 | PyObject *m, *d; |
| 170 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 171 | m = Py_InitModule("_testcapi", TestMethods); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 172 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 173 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 174 | d = PyModule_GetDict(m); |
| 175 | PyDict_SetItemString(d, "error", TestError); |
| 176 | } |