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 | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 39 | PyOS_snprintf(buf, sizeof(buf), |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 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 |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 59 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 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 |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 175 | 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] | 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 |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 224 | #define TYPENAME PY_LONG_LONG |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 245 | /* 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] | 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; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 253 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 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 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 293 | /* Call PyArg_ParseTuple, and return the result as unsigned long */ |
| 294 | static PyObject * |
| 295 | getargs_ul(PyObject *self, PyObject *args) |
| 296 | { |
| 297 | PyObject *ob, *result = NULL, *argtuple; |
| 298 | char *fmt; |
| 299 | unsigned long value = 0; |
| 300 | |
| 301 | if (!PyArg_ParseTuple(args, "sO", &fmt, &ob)) |
| 302 | return NULL; |
| 303 | argtuple = PyTuple_New(1); |
| 304 | Py_INCREF(ob); |
| 305 | PyTuple_SET_ITEM(argtuple, 0, ob); |
| 306 | if (PyArg_ParseTuple(argtuple, fmt, &value)) |
| 307 | result = PyLong_FromUnsignedLong(value); |
| 308 | Py_DECREF(argtuple); |
| 309 | return result; |
| 310 | } |
| 311 | |
| 312 | /* Call PyArg_ParseTuple, and return the result as signed long */ |
| 313 | static PyObject * |
| 314 | getargs_l(PyObject *self, PyObject *args) |
| 315 | { |
| 316 | PyObject *ob, *result = NULL, *argtuple; |
| 317 | char *fmt; |
| 318 | long value = 0; |
| 319 | |
| 320 | if (!PyArg_ParseTuple(args, "sO", &fmt, &ob)) |
| 321 | return NULL; |
| 322 | argtuple = PyTuple_New(1); |
| 323 | Py_INCREF(ob); |
| 324 | PyTuple_SET_ITEM(argtuple, 0, ob); |
| 325 | if (PyArg_ParseTuple(argtuple, fmt, &value)) |
| 326 | result = PyLong_FromLong(value); |
| 327 | Py_DECREF(argtuple); |
| 328 | return result; |
| 329 | } |
| 330 | |
| 331 | #ifdef HAVE_LONG_LONG |
| 332 | /* Call PyArg_ParseTuple, and return the result as signed long long */ |
| 333 | static PyObject * |
| 334 | getargs_ll(PyObject *self, PyObject *args) |
| 335 | { |
| 336 | PyObject *ob, *result = NULL, *argtuple; |
| 337 | char *fmt; |
| 338 | PY_LONG_LONG value = 0; |
| 339 | |
| 340 | if (!PyArg_ParseTuple(args, "sO", &fmt, &ob)) |
| 341 | return NULL; |
| 342 | argtuple = PyTuple_New(1); |
| 343 | Py_INCREF(ob); |
| 344 | PyTuple_SET_ITEM(argtuple, 0, ob); |
| 345 | if (PyArg_ParseTuple(argtuple, fmt, &value)) |
| 346 | result = PyLong_FromLongLong(value); |
| 347 | Py_DECREF(argtuple); |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | /* Call PyArg_ParseTuple, and return the result as unsigned long long */ |
| 352 | static PyObject * |
| 353 | getargs_ull(PyObject *self, PyObject *args) |
| 354 | { |
| 355 | PyObject *ob, *result = NULL, *argtuple; |
| 356 | char *fmt; |
| 357 | unsigned PY_LONG_LONG value = 0; |
| 358 | |
| 359 | if (!PyArg_ParseTuple(args, "sO", &fmt, &ob)) |
| 360 | return NULL; |
| 361 | argtuple = PyTuple_New(1); |
| 362 | Py_INCREF(ob); |
| 363 | PyTuple_SET_ITEM(argtuple, 0, ob); |
| 364 | if (PyArg_ParseTuple(argtuple, fmt, &value)) |
| 365 | result = PyLong_FromUnsignedLongLong(value); |
| 366 | Py_DECREF(argtuple); |
| 367 | return result; |
| 368 | } |
| 369 | #endif |
| 370 | |
| 371 | /* This function not only tests the 'k' getargs code, but also the |
| 372 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ |
| 373 | static PyObject * |
| 374 | test_k_code(PyObject *self) |
| 375 | { |
| 376 | PyObject *tuple, *num; |
| 377 | unsigned long value; |
| 378 | |
| 379 | tuple = PyTuple_New(1); |
| 380 | if (tuple == NULL) |
| 381 | return NULL; |
| 382 | |
| 383 | /* a number larger than UINT_MAX even on 64-bit platforms */ |
| 384 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 385 | if (num == NULL) |
| 386 | return NULL; |
| 387 | |
| 388 | value = PyInt_AsUnsignedLongMask(num); |
| 389 | if (value != UINT_MAX) |
| 390 | return raiseTestError("test_k_code", |
| 391 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 392 | |
| 393 | PyTuple_SET_ITEM(tuple, 0, num); |
| 394 | |
| 395 | value = -1; |
| 396 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 397 | return NULL; |
| 398 | if (value != UINT_MAX) |
| 399 | return raiseTestError("test_k_code", |
| 400 | "k code returned wrong value for long 0xFFF...FFF"); |
| 401 | |
| 402 | Py_DECREF(num); |
| 403 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 404 | if (num == NULL) |
| 405 | return NULL; |
| 406 | |
| 407 | value = PyInt_AsUnsignedLongMask(num); |
| 408 | if (value != (unsigned long)-0x42) |
| 409 | return raiseTestError("test_k_code", |
| 410 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 411 | |
| 412 | PyTuple_SET_ITEM(tuple, 0, num); |
| 413 | |
| 414 | value = -1; |
| 415 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 416 | return NULL; |
| 417 | if (value != (unsigned long)-0x42) |
| 418 | return raiseTestError("test_k_code", |
| 419 | "k code returned wrong value for long -0xFFF..000042"); |
| 420 | |
| 421 | Py_DECREF(tuple); |
| 422 | Py_INCREF(Py_None); |
| 423 | return Py_None; |
| 424 | } |
| 425 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 426 | #ifdef Py_USING_UNICODE |
| 427 | |
| 428 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 429 | of an error. |
| 430 | */ |
| 431 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 432 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 433 | { |
| 434 | PyObject *tuple, *obj; |
| 435 | Py_UNICODE *value; |
| 436 | int len; |
| 437 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 438 | tuple = PyTuple_New(1); |
| 439 | if (tuple == NULL) |
| 440 | return NULL; |
| 441 | |
| 442 | obj = PyUnicode_Decode("test", strlen("test"), |
| 443 | "ascii", NULL); |
| 444 | if (obj == NULL) |
| 445 | return NULL; |
| 446 | |
| 447 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 448 | |
| 449 | value = 0; |
| 450 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 451 | return NULL; |
| 452 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 453 | return raiseTestError("test_u_code", |
| 454 | "u code returned wrong value for u'test'"); |
| 455 | value = 0; |
| 456 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 457 | return NULL; |
| 458 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 459 | len != PyUnicode_GET_SIZE(obj)) |
| 460 | return raiseTestError("test_u_code", |
| 461 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 462 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 463 | Py_DECREF(tuple); |
| 464 | Py_INCREF(Py_None); |
| 465 | return Py_None; |
| 466 | } |
| 467 | |
| 468 | #endif |
| 469 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 470 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 471 | static PyObject * |
| 472 | test_long_numbits(PyObject *self) |
| 473 | { |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 474 | struct triple { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 475 | long input; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 476 | size_t nbits; |
| 477 | int sign; |
| 478 | } testcases[] = {{0, 0, 0}, |
| 479 | {1L, 1, 1}, |
| 480 | {-1L, 1, -1}, |
| 481 | {2L, 2, 1}, |
| 482 | {-2L, 2, -1}, |
| 483 | {3L, 2, 1}, |
| 484 | {-3L, 2, -1}, |
| 485 | {4L, 3, 1}, |
| 486 | {-4L, 3, -1}, |
| 487 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 488 | {-0x7fffL, 15, -1}, |
| 489 | {0xffffL, 16, 1}, |
| 490 | {-0xffffL, 16, -1}, |
| 491 | {0xfffffffL, 28, 1}, |
| 492 | {-0xfffffffL, 28, -1}}; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 493 | int i; |
| 494 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 495 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 496 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 497 | size_t nbits = _PyLong_NumBits(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 498 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 499 | |
| 500 | Py_DECREF(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 501 | if (nbits != testcases[i].nbits) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 502 | return raiseTestError("test_long_numbits", |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 503 | "wrong result for _PyLong_NumBits"); |
| 504 | if (sign != testcases[i].sign) |
| 505 | return raiseTestError("test_long_numbits", |
| 506 | "wrong result for _PyLong_Sign"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 507 | } |
| 508 | Py_INCREF(Py_None); |
| 509 | return Py_None; |
| 510 | } |
| 511 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 512 | static PyObject * |
| 513 | raise_exception(PyObject *self, PyObject *args) |
| 514 | { |
| 515 | PyObject *exc; |
| 516 | PyObject *exc_args, *v; |
| 517 | int num_args, i; |
| 518 | |
| 519 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 520 | &exc, &num_args)) |
| 521 | return NULL; |
| 522 | |
| 523 | exc_args = PyTuple_New(num_args); |
| 524 | if (exc_args == NULL) |
| 525 | return NULL; |
| 526 | for (i = 0; i < num_args; ++i) { |
| 527 | v = PyInt_FromLong(i); |
| 528 | if (v == NULL) { |
| 529 | Py_DECREF(exc_args); |
| 530 | return NULL; |
| 531 | } |
| 532 | PyTuple_SET_ITEM(exc_args, i, v); |
| 533 | } |
| 534 | PyErr_SetObject(exc, exc_args); |
| 535 | return NULL; |
| 536 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 537 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 538 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 539 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 540 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 541 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 542 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 543 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 544 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 545 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 546 | {"getargs_ul", (PyCFunction)getargs_ul, METH_VARARGS}, |
| 547 | {"getargs_l", (PyCFunction)getargs_l, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 548 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 549 | {"getargs_ll", (PyCFunction)getargs_ll, METH_VARARGS}, |
| 550 | {"getargs_ull", (PyCFunction)getargs_ull, METH_VARARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 551 | {"test_longlong_api", (PyCFunction)test_longlong_api, METH_NOARGS}, |
| 552 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 553 | #endif |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 554 | #ifdef Py_USING_UNICODE |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 555 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 556 | #endif |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 557 | {NULL, NULL} /* sentinel */ |
| 558 | }; |
| 559 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 560 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 561 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 562 | PyMODINIT_FUNC |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 563 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 564 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 565 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 566 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 567 | m = Py_InitModule("_testcapi", TestMethods); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 568 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 569 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); |
| 570 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); |
| 571 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 572 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 573 | PyModule_AddObject(m, "INT_MIN", PyInt_FromLong(INT_MIN)); |
| 574 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); |
| 575 | PyModule_AddObject(m, "INT_MAX", PyInt_FromLong(INT_MAX)); |
| 576 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); |
| 577 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 578 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 579 | Py_INCREF(TestError); |
| 580 | PyModule_AddObject(m, "error", TestError); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 581 | } |