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 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 12 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 13 | |
| 14 | static PyObject * |
| 15 | raiseTestError(const char* test_name, const char* msg) |
| 16 | { |
| 17 | char buf[2048]; |
| 18 | |
| 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); |
| 24 | } |
| 25 | return NULL; |
| 26 | } |
| 27 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 28 | /* Test #defines from config.h (particularly the SIZEOF_* defines). |
| 29 | |
| 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. |
| 33 | */ |
| 34 | static PyObject* |
| 35 | sizeof_error(const char* fatname, const char* typename, |
| 36 | int expected, int got) |
| 37 | { |
| 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; |
| 43 | } |
| 44 | |
| 45 | static PyObject* |
| 46 | test_config(PyObject *self, PyObject *args) |
| 47 | { |
| 48 | if (!PyArg_ParseTuple(args, ":test_config")) |
| 49 | return NULL; |
| 50 | |
| 51 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
| 52 | if (FATNAME != sizeof(TYPE)) \ |
| 53 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
| 54 | |
| 55 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 56 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 57 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 58 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
| 59 | #ifdef HAVE_LONG_LONG |
| 60 | CHECK_SIZEOF(SIZEOF_LONG_LONG, LONG_LONG); |
| 61 | #endif |
| 62 | |
| 63 | #undef CHECK_SIZEOF |
| 64 | |
| 65 | Py_INCREF(Py_None); |
| 66 | return Py_None; |
| 67 | } |
| 68 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 69 | static PyObject* |
| 70 | test_list_api(PyObject *self, PyObject *args) |
| 71 | { |
| 72 | PyObject* list; |
| 73 | int i; |
| 74 | if (!PyArg_ParseTuple(args, ":test_list_api")) |
| 75 | return NULL; |
| 76 | |
| 77 | /* SF bug 132008: PyList_Reverse segfaults */ |
| 78 | #define NLIST 30 |
| 79 | list = PyList_New(NLIST); |
| 80 | if (list == (PyObject*)NULL) |
| 81 | return (PyObject*)NULL; |
| 82 | /* list = range(NLIST) */ |
| 83 | for (i = 0; i < NLIST; ++i) { |
| 84 | PyObject* anint = PyInt_FromLong(i); |
| 85 | if (anint == (PyObject*)NULL) { |
| 86 | Py_DECREF(list); |
| 87 | return (PyObject*)NULL; |
| 88 | } |
| 89 | PyList_SET_ITEM(list, i, anint); |
| 90 | } |
| 91 | /* list.reverse(), via PyList_Reverse() */ |
| 92 | i = PyList_Reverse(list); /* should not blow up! */ |
| 93 | if (i != 0) { |
| 94 | Py_DECREF(list); |
| 95 | return (PyObject*)NULL; |
| 96 | } |
| 97 | /* Check that list == range(29, -1, -1) now */ |
| 98 | for (i = 0; i < NLIST; ++i) { |
| 99 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 100 | if (PyInt_AS_LONG(anint) != NLIST-1-i) { |
| 101 | PyErr_SetString(TestError, |
| 102 | "test_list_api: reverse screwed up"); |
| 103 | Py_DECREF(list); |
| 104 | return (PyObject*)NULL; |
| 105 | } |
| 106 | } |
| 107 | Py_DECREF(list); |
| 108 | #undef NLIST |
| 109 | |
| 110 | Py_INCREF(Py_None); |
| 111 | return Py_None; |
| 112 | } |
| 113 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 114 | static int |
| 115 | test_dict_inner(int count) |
| 116 | { |
| 117 | int pos = 0, iterations = 0, i; |
| 118 | PyObject *dict = PyDict_New(); |
| 119 | PyObject *v, *k; |
| 120 | |
| 121 | if (dict == NULL) |
| 122 | return -1; |
| 123 | |
| 124 | for (i = 0; i < count; i++) { |
| 125 | v = PyInt_FromLong(i); |
| 126 | PyDict_SetItem(dict, v, v); |
| 127 | Py_DECREF(v); |
| 128 | } |
| 129 | |
| 130 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 131 | PyObject *o; |
| 132 | iterations++; |
| 133 | |
| 134 | i = PyInt_AS_LONG(v) + 1; |
| 135 | o = PyInt_FromLong(i); |
| 136 | if (o == NULL) |
| 137 | return -1; |
| 138 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 139 | Py_DECREF(o); |
| 140 | return -1; |
| 141 | } |
| 142 | Py_DECREF(o); |
| 143 | } |
| 144 | |
| 145 | Py_DECREF(dict); |
| 146 | |
| 147 | if (iterations != count) { |
| 148 | PyErr_SetString( |
| 149 | TestError, |
| 150 | "test_dict_iteration: dict iteration went wrong "); |
| 151 | return -1; |
| 152 | } else { |
| 153 | return 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static PyObject* |
| 158 | test_dict_iteration(PyObject* self, PyObject* args) |
| 159 | { |
| 160 | int i; |
| 161 | |
| 162 | if (!PyArg_ParseTuple(args, ":test_dict_iteration")) |
| 163 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 165 | for (i = 0; i < 200; i++) { |
| 166 | if (test_dict_inner(i) < 0) { |
| 167 | return NULL; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | Py_INCREF(Py_None); |
| 172 | return Py_None; |
| 173 | } |
| 174 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 175 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 176 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 177 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 178 | |
| 179 | Note that the meat of the test is contained in testcapi_long.h. |
| 180 | This is revolting, but delicate code duplication is worse: "almost |
| 181 | exactly the same" code is needed to test LONG_LONG, but the ubiquitous |
| 182 | dependence on type names makes it impossible to use a parameterized |
| 183 | function. A giant macro would be even worse than this. A C++ template |
| 184 | would be perfect. |
| 185 | |
| 186 | The "report an error" functions are deliberately not part of the #include |
| 187 | file: if the test fails, you can set a breakpoint in the appropriate |
| 188 | error function directly, and crawl back from there in the debugger. |
| 189 | */ |
| 190 | |
| 191 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 192 | |
| 193 | static PyObject * |
| 194 | raise_test_long_error(const char* msg) |
| 195 | { |
| 196 | return raiseTestError("test_long_api", msg); |
| 197 | } |
| 198 | |
| 199 | #define TESTNAME test_long_api_inner |
| 200 | #define TYPENAME long |
| 201 | #define F_S_TO_PY PyLong_FromLong |
| 202 | #define F_PY_TO_S PyLong_AsLong |
| 203 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 204 | #define F_PY_TO_U PyLong_AsUnsignedLong |
| 205 | #define F_ERROR raise_test_long_error |
| 206 | |
| 207 | #include "testcapi_long.h" |
| 208 | |
| 209 | static PyObject * |
| 210 | test_long_api(PyObject* self, PyObject* args) |
| 211 | { |
| 212 | if (!PyArg_ParseTuple(args, ":test_long_api")) |
| 213 | return NULL; |
| 214 | |
| 215 | return TESTNAME(); |
| 216 | } |
| 217 | |
| 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 |
| 224 | #undef F_ERROR |
| 225 | |
| 226 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 227 | |
| 228 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 229 | raise_test_longlong_error(const char* msg) |
| 230 | { |
| 231 | return raiseTestError("test_longlong_api", msg); |
| 232 | } |
| 233 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 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 |
| 240 | #define F_ERROR raise_test_longlong_error |
| 241 | |
| 242 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 243 | |
| 244 | static PyObject * |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 245 | test_longlong_api(PyObject* self, PyObject* args) |
| 246 | { |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 247 | if (!PyArg_ParseTuple(args, ":test_longlong_api")) |
| 248 | return NULL; |
| 249 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 250 | return TESTNAME(); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 253 | #undef TESTNAME |
| 254 | #undef TYPENAME |
| 255 | #undef F_S_TO_PY |
| 256 | #undef F_PY_TO_S |
| 257 | #undef F_U_TO_PY |
| 258 | #undef F_PY_TO_U |
| 259 | #undef F_ERROR |
| 260 | |
| 261 | #endif /* ifdef HAVE_LONG_LONG */ |
| 262 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 263 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 264 | static PyMethodDef TestMethods[] = { |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 265 | {"test_config", test_config, METH_VARARGS}, |
| 266 | {"test_list_api", test_list_api, METH_VARARGS}, |
| 267 | {"test_dict_iteration", test_dict_iteration, METH_VARARGS}, |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 268 | {"test_long_api", test_long_api, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 269 | #ifdef HAVE_LONG_LONG |
| 270 | {"test_longlong_api", test_longlong_api, METH_VARARGS}, |
| 271 | #endif |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 272 | {NULL, NULL} /* sentinel */ |
| 273 | }; |
| 274 | |
| 275 | DL_EXPORT(void) |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 276 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 277 | { |
| 278 | PyObject *m, *d; |
| 279 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 280 | m = Py_InitModule("_testcapi", TestMethods); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 281 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 282 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 283 | d = PyModule_GetDict(m); |
| 284 | PyDict_SetItemString(d, "error", TestError); |
| 285 | } |