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 { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 22 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 23 | PyErr_SetString(TestError, buf); |
| 24 | } |
| 25 | return NULL; |
| 26 | } |
| 27 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 28 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 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]; |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 39 | PyOS_snprintf(buf, sizeof(buf), |
| 40 | "%.200s #define == %d but sizeof(%.200s) == %d", |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 41 | fatname, expected, typename, got); |
| 42 | PyErr_SetString(TestError, buf); |
| 43 | return (PyObject*)NULL; |
| 44 | } |
| 45 | |
| 46 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 47 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 48 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 49 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
| 50 | if (FATNAME != sizeof(TYPE)) \ |
| 51 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
| 52 | |
Tim Peters | 208efe5 | 2001-06-26 22:40:47 +0000 | [diff] [blame] | 53 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 54 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 55 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 56 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 57 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
| 58 | #ifdef HAVE_LONG_LONG |
| 59 | CHECK_SIZEOF(SIZEOF_LONG_LONG, LONG_LONG); |
| 60 | #endif |
| 61 | |
| 62 | #undef CHECK_SIZEOF |
| 63 | |
| 64 | Py_INCREF(Py_None); |
| 65 | return Py_None; |
| 66 | } |
| 67 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 68 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 69 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 70 | { |
| 71 | PyObject* list; |
| 72 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 73 | |
| 74 | /* SF bug 132008: PyList_Reverse segfaults */ |
| 75 | #define NLIST 30 |
| 76 | list = PyList_New(NLIST); |
| 77 | if (list == (PyObject*)NULL) |
| 78 | return (PyObject*)NULL; |
| 79 | /* list = range(NLIST) */ |
| 80 | for (i = 0; i < NLIST; ++i) { |
| 81 | PyObject* anint = PyInt_FromLong(i); |
| 82 | if (anint == (PyObject*)NULL) { |
| 83 | Py_DECREF(list); |
| 84 | return (PyObject*)NULL; |
| 85 | } |
| 86 | PyList_SET_ITEM(list, i, anint); |
| 87 | } |
| 88 | /* list.reverse(), via PyList_Reverse() */ |
| 89 | i = PyList_Reverse(list); /* should not blow up! */ |
| 90 | if (i != 0) { |
| 91 | Py_DECREF(list); |
| 92 | return (PyObject*)NULL; |
| 93 | } |
| 94 | /* Check that list == range(29, -1, -1) now */ |
| 95 | for (i = 0; i < NLIST; ++i) { |
| 96 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 97 | if (PyInt_AS_LONG(anint) != NLIST-1-i) { |
| 98 | PyErr_SetString(TestError, |
| 99 | "test_list_api: reverse screwed up"); |
| 100 | Py_DECREF(list); |
| 101 | return (PyObject*)NULL; |
| 102 | } |
| 103 | } |
| 104 | Py_DECREF(list); |
| 105 | #undef NLIST |
| 106 | |
| 107 | Py_INCREF(Py_None); |
| 108 | return Py_None; |
| 109 | } |
| 110 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 111 | static int |
| 112 | test_dict_inner(int count) |
| 113 | { |
| 114 | int pos = 0, iterations = 0, i; |
| 115 | PyObject *dict = PyDict_New(); |
| 116 | PyObject *v, *k; |
| 117 | |
| 118 | if (dict == NULL) |
| 119 | return -1; |
| 120 | |
| 121 | for (i = 0; i < count; i++) { |
| 122 | v = PyInt_FromLong(i); |
| 123 | PyDict_SetItem(dict, v, v); |
| 124 | Py_DECREF(v); |
| 125 | } |
| 126 | |
| 127 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 128 | PyObject *o; |
| 129 | iterations++; |
| 130 | |
| 131 | i = PyInt_AS_LONG(v) + 1; |
| 132 | o = PyInt_FromLong(i); |
| 133 | if (o == NULL) |
| 134 | return -1; |
| 135 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 136 | Py_DECREF(o); |
| 137 | return -1; |
| 138 | } |
| 139 | Py_DECREF(o); |
| 140 | } |
| 141 | |
| 142 | Py_DECREF(dict); |
| 143 | |
| 144 | if (iterations != count) { |
| 145 | PyErr_SetString( |
| 146 | TestError, |
| 147 | "test_dict_iteration: dict iteration went wrong "); |
| 148 | return -1; |
| 149 | } else { |
| 150 | return 0; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 155 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 156 | { |
| 157 | int i; |
| 158 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 159 | for (i = 0; i < 200; i++) { |
| 160 | if (test_dict_inner(i) < 0) { |
| 161 | return NULL; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | Py_INCREF(Py_None); |
| 166 | return Py_None; |
| 167 | } |
| 168 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 169 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 170 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 171 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 172 | |
| 173 | Note that the meat of the test is contained in testcapi_long.h. |
| 174 | This is revolting, but delicate code duplication is worse: "almost |
| 175 | exactly the same" code is needed to test LONG_LONG, but the ubiquitous |
| 176 | dependence on type names makes it impossible to use a parameterized |
| 177 | function. A giant macro would be even worse than this. A C++ template |
| 178 | would be perfect. |
| 179 | |
| 180 | The "report an error" functions are deliberately not part of the #include |
| 181 | file: if the test fails, you can set a breakpoint in the appropriate |
| 182 | error function directly, and crawl back from there in the debugger. |
| 183 | */ |
| 184 | |
| 185 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 186 | |
| 187 | static PyObject * |
| 188 | raise_test_long_error(const char* msg) |
| 189 | { |
| 190 | return raiseTestError("test_long_api", msg); |
| 191 | } |
| 192 | |
| 193 | #define TESTNAME test_long_api_inner |
| 194 | #define TYPENAME long |
| 195 | #define F_S_TO_PY PyLong_FromLong |
| 196 | #define F_PY_TO_S PyLong_AsLong |
| 197 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 198 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 199 | |
| 200 | #include "testcapi_long.h" |
| 201 | |
| 202 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 203 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 204 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 205 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | #undef TESTNAME |
| 209 | #undef TYPENAME |
| 210 | #undef F_S_TO_PY |
| 211 | #undef F_PY_TO_S |
| 212 | #undef F_U_TO_PY |
| 213 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 214 | |
| 215 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 216 | |
| 217 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 218 | raise_test_longlong_error(const char* msg) |
| 219 | { |
| 220 | return raiseTestError("test_longlong_api", msg); |
| 221 | } |
| 222 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 223 | #define TESTNAME test_longlong_api_inner |
| 224 | #define TYPENAME LONG_LONG |
| 225 | #define F_S_TO_PY PyLong_FromLongLong |
| 226 | #define F_PY_TO_S PyLong_AsLongLong |
| 227 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 228 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 229 | |
| 230 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 231 | |
| 232 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 233 | test_longlong_api(PyObject* self) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 234 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 235 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 238 | #undef TESTNAME |
| 239 | #undef TYPENAME |
| 240 | #undef F_S_TO_PY |
| 241 | #undef F_PY_TO_S |
| 242 | #undef F_U_TO_PY |
| 243 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 244 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 245 | /* Test the L code for PyArg_ParseTuple. This should deliver a LONG_LONG |
| 246 | for both long and int arguments. The test may leak a little memory if |
| 247 | it fails. |
| 248 | */ |
| 249 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 250 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 251 | { |
| 252 | PyObject *tuple, *num; |
| 253 | LONG_LONG value; |
| 254 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 255 | tuple = PyTuple_New(1); |
| 256 | if (tuple == NULL) |
| 257 | return NULL; |
| 258 | |
| 259 | num = PyLong_FromLong(42); |
| 260 | if (num == NULL) |
| 261 | return NULL; |
| 262 | |
| 263 | PyTuple_SET_ITEM(tuple, 0, num); |
| 264 | |
| 265 | value = -1; |
| 266 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 267 | return NULL; |
| 268 | if (value != 42) |
| 269 | return raiseTestError("test_L_code", |
| 270 | "L code returned wrong value for long 42"); |
| 271 | |
| 272 | Py_DECREF(num); |
| 273 | num = PyInt_FromLong(42); |
| 274 | if (num == NULL) |
| 275 | return NULL; |
| 276 | |
| 277 | PyTuple_SET_ITEM(tuple, 0, num); |
| 278 | |
| 279 | value = -1; |
| 280 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 281 | return NULL; |
| 282 | if (value != 42) |
| 283 | return raiseTestError("test_L_code", |
| 284 | "L code returned wrong value for int 42"); |
| 285 | |
| 286 | Py_DECREF(tuple); |
| 287 | Py_INCREF(Py_None); |
| 288 | return Py_None; |
| 289 | } |
| 290 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 291 | #endif /* ifdef HAVE_LONG_LONG */ |
| 292 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 293 | #ifdef Py_USING_UNICODE |
| 294 | |
| 295 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 296 | of an error. |
| 297 | */ |
| 298 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 299 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 300 | { |
| 301 | PyObject *tuple, *obj; |
| 302 | Py_UNICODE *value; |
| 303 | int len; |
| 304 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 305 | tuple = PyTuple_New(1); |
| 306 | if (tuple == NULL) |
| 307 | return NULL; |
| 308 | |
| 309 | obj = PyUnicode_Decode("test", strlen("test"), |
| 310 | "ascii", NULL); |
| 311 | if (obj == NULL) |
| 312 | return NULL; |
| 313 | |
| 314 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 315 | |
| 316 | value = 0; |
| 317 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 318 | return NULL; |
| 319 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 320 | return raiseTestError("test_u_code", |
| 321 | "u code returned wrong value for u'test'"); |
| 322 | value = 0; |
| 323 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 324 | return NULL; |
| 325 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 326 | len != PyUnicode_GET_SIZE(obj)) |
| 327 | return raiseTestError("test_u_code", |
| 328 | "u# code returned wrong values for u'test'"); |
| 329 | |
| 330 | Py_DECREF(tuple); |
| 331 | Py_INCREF(Py_None); |
| 332 | return Py_None; |
| 333 | } |
| 334 | |
| 335 | #endif |
| 336 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 337 | static PyObject * |
| 338 | raise_exception(PyObject *self, PyObject *args) |
| 339 | { |
| 340 | PyObject *exc; |
| 341 | PyObject *exc_args, *v; |
| 342 | int num_args, i; |
| 343 | |
| 344 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 345 | &exc, &num_args)) |
| 346 | return NULL; |
| 347 | |
| 348 | exc_args = PyTuple_New(num_args); |
| 349 | if (exc_args == NULL) |
| 350 | return NULL; |
| 351 | for (i = 0; i < num_args; ++i) { |
| 352 | v = PyInt_FromLong(i); |
| 353 | if (v == NULL) { |
| 354 | Py_DECREF(exc_args); |
| 355 | return NULL; |
| 356 | } |
| 357 | PyTuple_SET_ITEM(exc_args, i, v); |
| 358 | } |
| 359 | PyErr_SetObject(exc, exc_args); |
| 360 | return NULL; |
| 361 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 362 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 363 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 364 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 365 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 366 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 367 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 368 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 369 | #ifdef HAVE_LONG_LONG |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 370 | {"test_longlong_api", (PyCFunction)test_longlong_api, METH_NOARGS}, |
| 371 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 372 | #endif |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 373 | #ifdef Py_USING_UNICODE |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 374 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 375 | #endif |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 376 | {NULL, NULL} /* sentinel */ |
| 377 | }; |
| 378 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 379 | PyMODINIT_FUNC |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 380 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 381 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 382 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 383 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 384 | m = Py_InitModule("_testcapi", TestMethods); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 385 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 386 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 387 | Py_INCREF(TestError); |
| 388 | PyModule_AddObject(m, "error", TestError); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 389 | } |