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 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 10 | #ifdef WITH_THREAD |
| 11 | #include "pythread.h" |
| 12 | #endif /* WITH_THREAD */ |
| 13 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 14 | static PyObject *TestError; /* set to exception object in init */ |
| 15 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 16 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 17 | |
| 18 | static PyObject * |
| 19 | raiseTestError(const char* test_name, const char* msg) |
| 20 | { |
| 21 | char buf[2048]; |
| 22 | |
| 23 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 24 | PyErr_SetString(TestError, "internal error msg too large"); |
| 25 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 26 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 27 | PyErr_SetString(TestError, buf); |
| 28 | } |
| 29 | return NULL; |
| 30 | } |
| 31 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 32 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 33 | |
| 34 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 35 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 36 | platforms have these hardcoded. Better safe than sorry. |
| 37 | */ |
| 38 | static PyObject* |
| 39 | sizeof_error(const char* fatname, const char* typename, |
| 40 | int expected, int got) |
| 41 | { |
| 42 | char buf[1024]; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 43 | PyOS_snprintf(buf, sizeof(buf), |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 44 | "%.200s #define == %d but sizeof(%.200s) == %d", |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 45 | fatname, expected, typename, got); |
| 46 | PyErr_SetString(TestError, buf); |
| 47 | return (PyObject*)NULL; |
| 48 | } |
| 49 | |
| 50 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 51 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 52 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 53 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
| 54 | if (FATNAME != sizeof(TYPE)) \ |
| 55 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
| 56 | |
Tim Peters | 208efe5 | 2001-06-26 22:40:47 +0000 | [diff] [blame] | 57 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 58 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 59 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 60 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 61 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
| 62 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 63 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
| 66 | #undef CHECK_SIZEOF |
| 67 | |
| 68 | Py_INCREF(Py_None); |
| 69 | return Py_None; |
| 70 | } |
| 71 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 72 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 73 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 74 | { |
| 75 | PyObject* list; |
| 76 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 77 | |
| 78 | /* SF bug 132008: PyList_Reverse segfaults */ |
| 79 | #define NLIST 30 |
| 80 | list = PyList_New(NLIST); |
| 81 | if (list == (PyObject*)NULL) |
| 82 | return (PyObject*)NULL; |
| 83 | /* list = range(NLIST) */ |
| 84 | for (i = 0; i < NLIST; ++i) { |
| 85 | PyObject* anint = PyInt_FromLong(i); |
| 86 | if (anint == (PyObject*)NULL) { |
| 87 | Py_DECREF(list); |
| 88 | return (PyObject*)NULL; |
| 89 | } |
| 90 | PyList_SET_ITEM(list, i, anint); |
| 91 | } |
| 92 | /* list.reverse(), via PyList_Reverse() */ |
| 93 | i = PyList_Reverse(list); /* should not blow up! */ |
| 94 | if (i != 0) { |
| 95 | Py_DECREF(list); |
| 96 | return (PyObject*)NULL; |
| 97 | } |
| 98 | /* Check that list == range(29, -1, -1) now */ |
| 99 | for (i = 0; i < NLIST; ++i) { |
| 100 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 101 | if (PyInt_AS_LONG(anint) != NLIST-1-i) { |
| 102 | PyErr_SetString(TestError, |
| 103 | "test_list_api: reverse screwed up"); |
| 104 | Py_DECREF(list); |
| 105 | return (PyObject*)NULL; |
| 106 | } |
| 107 | } |
| 108 | Py_DECREF(list); |
| 109 | #undef NLIST |
| 110 | |
| 111 | Py_INCREF(Py_None); |
| 112 | return Py_None; |
| 113 | } |
| 114 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 115 | static int |
| 116 | test_dict_inner(int count) |
| 117 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 118 | Py_ssize_t pos = 0, iterations = 0; |
| 119 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 120 | PyObject *dict = PyDict_New(); |
| 121 | PyObject *v, *k; |
| 122 | |
| 123 | if (dict == NULL) |
| 124 | return -1; |
| 125 | |
| 126 | for (i = 0; i < count; i++) { |
| 127 | v = PyInt_FromLong(i); |
| 128 | PyDict_SetItem(dict, v, v); |
| 129 | Py_DECREF(v); |
| 130 | } |
| 131 | |
| 132 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 133 | PyObject *o; |
| 134 | iterations++; |
| 135 | |
| 136 | i = PyInt_AS_LONG(v) + 1; |
| 137 | o = PyInt_FromLong(i); |
| 138 | if (o == NULL) |
| 139 | return -1; |
| 140 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 141 | Py_DECREF(o); |
| 142 | return -1; |
| 143 | } |
| 144 | Py_DECREF(o); |
| 145 | } |
| 146 | |
| 147 | Py_DECREF(dict); |
| 148 | |
| 149 | if (iterations != count) { |
| 150 | PyErr_SetString( |
| 151 | TestError, |
| 152 | "test_dict_iteration: dict iteration went wrong "); |
| 153 | return -1; |
| 154 | } else { |
| 155 | return 0; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 160 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 161 | { |
| 162 | int i; |
| 163 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 164 | for (i = 0; i < 200; i++) { |
| 165 | if (test_dict_inner(i) < 0) { |
| 166 | return NULL; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | Py_INCREF(Py_None); |
| 171 | return Py_None; |
| 172 | } |
| 173 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 174 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 175 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 176 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 177 | |
| 178 | Note that the meat of the test is contained in testcapi_long.h. |
| 179 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 180 | exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 181 | dependence on type names makes it impossible to use a parameterized |
| 182 | function. A giant macro would be even worse than this. A C++ template |
| 183 | would be perfect. |
| 184 | |
| 185 | The "report an error" functions are deliberately not part of the #include |
| 186 | file: if the test fails, you can set a breakpoint in the appropriate |
| 187 | error function directly, and crawl back from there in the debugger. |
| 188 | */ |
| 189 | |
| 190 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 191 | |
| 192 | static PyObject * |
| 193 | raise_test_long_error(const char* msg) |
| 194 | { |
| 195 | return raiseTestError("test_long_api", msg); |
| 196 | } |
| 197 | |
| 198 | #define TESTNAME test_long_api_inner |
| 199 | #define TYPENAME long |
| 200 | #define F_S_TO_PY PyLong_FromLong |
| 201 | #define F_PY_TO_S PyLong_AsLong |
| 202 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 203 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 204 | |
| 205 | #include "testcapi_long.h" |
| 206 | |
| 207 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 208 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 209 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 210 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | #undef TESTNAME |
| 214 | #undef TYPENAME |
| 215 | #undef F_S_TO_PY |
| 216 | #undef F_PY_TO_S |
| 217 | #undef F_U_TO_PY |
| 218 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 219 | |
| 220 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 221 | |
| 222 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 223 | raise_test_longlong_error(const char* msg) |
| 224 | { |
| 225 | return raiseTestError("test_longlong_api", msg); |
| 226 | } |
| 227 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 228 | #define TESTNAME test_longlong_api_inner |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 229 | #define TYPENAME PY_LONG_LONG |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 230 | #define F_S_TO_PY PyLong_FromLongLong |
| 231 | #define F_PY_TO_S PyLong_AsLongLong |
| 232 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 233 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 234 | |
| 235 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 236 | |
| 237 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 238 | test_longlong_api(PyObject* self) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 239 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 240 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 243 | #undef TESTNAME |
| 244 | #undef TYPENAME |
| 245 | #undef F_S_TO_PY |
| 246 | #undef F_PY_TO_S |
| 247 | #undef F_U_TO_PY |
| 248 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 249 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 250 | /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 251 | for both long and int arguments. The test may leak a little memory if |
| 252 | it fails. |
| 253 | */ |
| 254 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 255 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 256 | { |
| 257 | PyObject *tuple, *num; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 258 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 259 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 260 | tuple = PyTuple_New(1); |
| 261 | if (tuple == NULL) |
| 262 | return NULL; |
| 263 | |
| 264 | num = PyLong_FromLong(42); |
| 265 | if (num == NULL) |
| 266 | return NULL; |
| 267 | |
| 268 | PyTuple_SET_ITEM(tuple, 0, num); |
| 269 | |
| 270 | value = -1; |
| 271 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 272 | return NULL; |
| 273 | if (value != 42) |
| 274 | return raiseTestError("test_L_code", |
| 275 | "L code returned wrong value for long 42"); |
| 276 | |
| 277 | Py_DECREF(num); |
| 278 | num = PyInt_FromLong(42); |
| 279 | if (num == NULL) |
| 280 | return NULL; |
| 281 | |
| 282 | PyTuple_SET_ITEM(tuple, 0, num); |
| 283 | |
| 284 | value = -1; |
| 285 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 286 | return NULL; |
| 287 | if (value != 42) |
| 288 | return raiseTestError("test_L_code", |
| 289 | "L code returned wrong value for int 42"); |
| 290 | |
| 291 | Py_DECREF(tuple); |
| 292 | Py_INCREF(Py_None); |
| 293 | return Py_None; |
| 294 | } |
| 295 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 296 | #endif /* ifdef HAVE_LONG_LONG */ |
| 297 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 298 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 299 | and return the result. |
| 300 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 301 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 302 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 303 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 304 | unsigned char value; |
| 305 | if (!PyArg_ParseTuple(args, "b", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 306 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 307 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 310 | static PyObject * |
| 311 | getargs_B(PyObject *self, PyObject *args) |
| 312 | { |
| 313 | unsigned char value; |
| 314 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 315 | return NULL; |
| 316 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 317 | } |
| 318 | |
| 319 | static PyObject * |
| 320 | getargs_H(PyObject *self, PyObject *args) |
| 321 | { |
| 322 | unsigned short value; |
| 323 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 324 | return NULL; |
| 325 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 326 | } |
| 327 | |
| 328 | static PyObject * |
| 329 | getargs_I(PyObject *self, PyObject *args) |
| 330 | { |
| 331 | unsigned int value; |
| 332 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 333 | return NULL; |
| 334 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 335 | } |
| 336 | |
| 337 | static PyObject * |
| 338 | getargs_k(PyObject *self, PyObject *args) |
| 339 | { |
| 340 | unsigned long value; |
| 341 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 342 | return NULL; |
| 343 | return PyLong_FromUnsignedLong(value); |
| 344 | } |
| 345 | |
| 346 | static PyObject * |
| 347 | getargs_i(PyObject *self, PyObject *args) |
| 348 | { |
| 349 | int value; |
| 350 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 351 | return NULL; |
| 352 | return PyLong_FromLong((long)value); |
| 353 | } |
| 354 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 355 | static PyObject * |
| 356 | getargs_l(PyObject *self, PyObject *args) |
| 357 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 358 | long value; |
| 359 | if (!PyArg_ParseTuple(args, "l", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 360 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 361 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 365 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 366 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 367 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 368 | PY_LONG_LONG value; |
| 369 | if (!PyArg_ParseTuple(args, "L", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 370 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 371 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 374 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 375 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 376 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 377 | unsigned PY_LONG_LONG value; |
| 378 | if (!PyArg_ParseTuple(args, "K", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 379 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 380 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 381 | } |
| 382 | #endif |
| 383 | |
| 384 | /* This function not only tests the 'k' getargs code, but also the |
| 385 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ |
| 386 | static PyObject * |
| 387 | test_k_code(PyObject *self) |
| 388 | { |
| 389 | PyObject *tuple, *num; |
| 390 | unsigned long value; |
| 391 | |
| 392 | tuple = PyTuple_New(1); |
| 393 | if (tuple == NULL) |
| 394 | return NULL; |
| 395 | |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 396 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 397 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 398 | if (num == NULL) |
| 399 | return NULL; |
| 400 | |
| 401 | value = PyInt_AsUnsignedLongMask(num); |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 402 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 403 | return raiseTestError("test_k_code", |
| 404 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 405 | |
| 406 | PyTuple_SET_ITEM(tuple, 0, num); |
| 407 | |
| 408 | value = -1; |
| 409 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 410 | return NULL; |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 411 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 412 | return raiseTestError("test_k_code", |
| 413 | "k code returned wrong value for long 0xFFF...FFF"); |
| 414 | |
| 415 | Py_DECREF(num); |
| 416 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 417 | if (num == NULL) |
| 418 | return NULL; |
| 419 | |
| 420 | value = PyInt_AsUnsignedLongMask(num); |
| 421 | if (value != (unsigned long)-0x42) |
| 422 | return raiseTestError("test_k_code", |
| 423 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 424 | |
| 425 | PyTuple_SET_ITEM(tuple, 0, num); |
| 426 | |
| 427 | value = -1; |
| 428 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 429 | return NULL; |
| 430 | if (value != (unsigned long)-0x42) |
| 431 | return raiseTestError("test_k_code", |
| 432 | "k code returned wrong value for long -0xFFF..000042"); |
| 433 | |
| 434 | Py_DECREF(tuple); |
| 435 | Py_INCREF(Py_None); |
| 436 | return Py_None; |
| 437 | } |
| 438 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 439 | #ifdef Py_USING_UNICODE |
| 440 | |
| 441 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 442 | of an error. |
| 443 | */ |
| 444 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 445 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 446 | { |
| 447 | PyObject *tuple, *obj; |
| 448 | Py_UNICODE *value; |
| 449 | int len; |
| 450 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 451 | tuple = PyTuple_New(1); |
| 452 | if (tuple == NULL) |
| 453 | return NULL; |
| 454 | |
| 455 | obj = PyUnicode_Decode("test", strlen("test"), |
| 456 | "ascii", NULL); |
| 457 | if (obj == NULL) |
| 458 | return NULL; |
| 459 | |
| 460 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 461 | |
| 462 | value = 0; |
| 463 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 464 | return NULL; |
| 465 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 466 | return raiseTestError("test_u_code", |
| 467 | "u code returned wrong value for u'test'"); |
| 468 | value = 0; |
| 469 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 470 | return NULL; |
| 471 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 472 | len != PyUnicode_GET_SIZE(obj)) |
| 473 | return raiseTestError("test_u_code", |
| 474 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 475 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 476 | Py_DECREF(tuple); |
| 477 | Py_INCREF(Py_None); |
| 478 | return Py_None; |
| 479 | } |
| 480 | |
| 481 | #endif |
| 482 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 483 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 484 | static PyObject * |
| 485 | test_long_numbits(PyObject *self) |
| 486 | { |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 487 | struct triple { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 488 | long input; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 489 | size_t nbits; |
| 490 | int sign; |
| 491 | } testcases[] = {{0, 0, 0}, |
| 492 | {1L, 1, 1}, |
| 493 | {-1L, 1, -1}, |
| 494 | {2L, 2, 1}, |
| 495 | {-2L, 2, -1}, |
| 496 | {3L, 2, 1}, |
| 497 | {-3L, 2, -1}, |
| 498 | {4L, 3, 1}, |
| 499 | {-4L, 3, -1}, |
| 500 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 501 | {-0x7fffL, 15, -1}, |
| 502 | {0xffffL, 16, 1}, |
| 503 | {-0xffffL, 16, -1}, |
| 504 | {0xfffffffL, 28, 1}, |
| 505 | {-0xfffffffL, 28, -1}}; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 506 | int i; |
| 507 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 508 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 509 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 510 | size_t nbits = _PyLong_NumBits(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 511 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 512 | |
| 513 | Py_DECREF(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 514 | if (nbits != testcases[i].nbits) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 515 | return raiseTestError("test_long_numbits", |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 516 | "wrong result for _PyLong_NumBits"); |
| 517 | if (sign != testcases[i].sign) |
| 518 | return raiseTestError("test_long_numbits", |
| 519 | "wrong result for _PyLong_Sign"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 520 | } |
| 521 | Py_INCREF(Py_None); |
| 522 | return Py_None; |
| 523 | } |
| 524 | |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 525 | /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */ |
| 526 | |
| 527 | static PyObject * |
| 528 | test_null_strings(PyObject *self) |
| 529 | { |
| 530 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL); |
| 531 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 532 | Py_XDECREF(o1); |
| 533 | Py_XDECREF(o2); |
| 534 | return tuple; |
| 535 | } |
| 536 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 537 | static PyObject * |
| 538 | raise_exception(PyObject *self, PyObject *args) |
| 539 | { |
| 540 | PyObject *exc; |
| 541 | PyObject *exc_args, *v; |
| 542 | int num_args, i; |
| 543 | |
| 544 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 545 | &exc, &num_args)) |
| 546 | return NULL; |
| 547 | |
| 548 | exc_args = PyTuple_New(num_args); |
| 549 | if (exc_args == NULL) |
| 550 | return NULL; |
| 551 | for (i = 0; i < num_args; ++i) { |
| 552 | v = PyInt_FromLong(i); |
| 553 | if (v == NULL) { |
| 554 | Py_DECREF(exc_args); |
| 555 | return NULL; |
| 556 | } |
| 557 | PyTuple_SET_ITEM(exc_args, i, v); |
| 558 | } |
| 559 | PyErr_SetObject(exc, exc_args); |
Michael W. Hudson | f02bcee | 2003-08-15 13:03:30 +0000 | [diff] [blame] | 560 | Py_DECREF(exc_args); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 561 | return NULL; |
| 562 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 563 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 564 | #ifdef WITH_THREAD |
| 565 | |
| 566 | void _make_call(void *callable) |
| 567 | { |
| 568 | PyObject *rc; |
| 569 | PyGILState_STATE s = PyGILState_Ensure(); |
| 570 | rc = PyObject_CallFunction(callable, ""); |
| 571 | Py_XDECREF(rc); |
| 572 | PyGILState_Release(s); |
| 573 | } |
| 574 | |
| 575 | static PyObject * |
| 576 | test_thread_state(PyObject *self, PyObject *args) |
| 577 | { |
| 578 | PyObject *fn; |
| 579 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 580 | return NULL; |
| 581 | /* Ensure Python is setup for threading */ |
| 582 | PyEval_InitThreads(); |
| 583 | /* Start a new thread for our callback. */ |
| 584 | PyThread_start_new_thread( _make_call, fn); |
| 585 | /* Make the callback with the thread lock held by this thread */ |
| 586 | _make_call(fn); |
| 587 | /* Do it all again, but this time with the thread-lock released */ |
| 588 | Py_BEGIN_ALLOW_THREADS |
| 589 | _make_call(fn); |
| 590 | Py_END_ALLOW_THREADS |
| 591 | /* And once more with and without a thread |
| 592 | XXX - should use a lock and work out exactly what we are trying |
| 593 | to test <wink> |
| 594 | */ |
| 595 | Py_BEGIN_ALLOW_THREADS |
| 596 | PyThread_start_new_thread( _make_call, fn); |
| 597 | _make_call(fn); |
| 598 | Py_END_ALLOW_THREADS |
| 599 | Py_INCREF(Py_None); |
| 600 | return Py_None; |
| 601 | } |
| 602 | #endif |
| 603 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 604 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 605 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 606 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 607 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 608 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 609 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 610 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 611 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 612 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 613 | |
| 614 | {"getargs_b", (PyCFunction)getargs_b, METH_VARARGS}, |
| 615 | {"getargs_B", (PyCFunction)getargs_B, METH_VARARGS}, |
| 616 | {"getargs_H", (PyCFunction)getargs_H, METH_VARARGS}, |
| 617 | {"getargs_I", (PyCFunction)getargs_I, METH_VARARGS}, |
| 618 | {"getargs_k", (PyCFunction)getargs_k, METH_VARARGS}, |
| 619 | {"getargs_i", (PyCFunction)getargs_i, METH_VARARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 620 | {"getargs_l", (PyCFunction)getargs_l, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 621 | #ifdef HAVE_LONG_LONG |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 622 | {"getargs_L", (PyCFunction)getargs_L, METH_VARARGS}, |
| 623 | {"getargs_K", (PyCFunction)getargs_K, METH_VARARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 624 | {"test_longlong_api", (PyCFunction)test_longlong_api, METH_NOARGS}, |
| 625 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 626 | #endif |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 627 | #ifdef Py_USING_UNICODE |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 628 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 629 | #endif |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 630 | #ifdef WITH_THREAD |
| 631 | {"_test_thread_state", (PyCFunction)test_thread_state, METH_VARARGS}, |
| 632 | #endif |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 633 | {NULL, NULL} /* sentinel */ |
| 634 | }; |
| 635 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 636 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 637 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 638 | PyMODINIT_FUNC |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 639 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 640 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 641 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 642 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 643 | m = Py_InitModule("_testcapi", TestMethods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 644 | if (m == NULL) |
| 645 | return; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 646 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 647 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); |
| 648 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); |
| 649 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 650 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 651 | PyModule_AddObject(m, "INT_MIN", PyInt_FromLong(INT_MIN)); |
| 652 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); |
| 653 | PyModule_AddObject(m, "INT_MAX", PyInt_FromLong(INT_MAX)); |
| 654 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); |
| 655 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 656 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 657 | Py_INCREF(TestError); |
| 658 | PyModule_AddObject(m, "error", TestError); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 659 | } |