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" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | #include <float.h> |
| 10 | #include "structmember.h" |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 11 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 12 | #ifdef WITH_THREAD |
| 13 | #include "pythread.h" |
| 14 | #endif /* WITH_THREAD */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 15 | static PyObject *TestError; /* set to exception object in init */ |
| 16 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 17 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 18 | |
| 19 | static PyObject * |
| 20 | raiseTestError(const char* test_name, const char* msg) |
| 21 | { |
| 22 | char buf[2048]; |
| 23 | |
| 24 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 25 | PyErr_SetString(TestError, "internal error msg too large"); |
| 26 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 27 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 28 | PyErr_SetString(TestError, buf); |
| 29 | } |
| 30 | return NULL; |
| 31 | } |
| 32 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 33 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 34 | |
| 35 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 36 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 37 | platforms have these hardcoded. Better safe than sorry. |
| 38 | */ |
| 39 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 40 | sizeof_error(const char* fatname, const char* typname, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 41 | int expected, int got) |
| 42 | { |
| 43 | char buf[1024]; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 44 | PyOS_snprintf(buf, sizeof(buf), |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 45 | "%.200s #define == %d but sizeof(%.200s) == %d", |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 46 | fatname, expected, typname, got); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 47 | PyErr_SetString(TestError, buf); |
| 48 | return (PyObject*)NULL; |
| 49 | } |
| 50 | |
| 51 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 52 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 53 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 54 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
| 55 | if (FATNAME != sizeof(TYPE)) \ |
| 56 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
| 57 | |
Tim Peters | 208efe5 | 2001-06-26 22:40:47 +0000 | [diff] [blame] | 58 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 59 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 60 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 61 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 62 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
| 63 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 64 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | #undef CHECK_SIZEOF |
| 68 | |
| 69 | Py_INCREF(Py_None); |
| 70 | return Py_None; |
| 71 | } |
| 72 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 73 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 74 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 75 | { |
| 76 | PyObject* list; |
| 77 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 78 | |
| 79 | /* SF bug 132008: PyList_Reverse segfaults */ |
| 80 | #define NLIST 30 |
| 81 | list = PyList_New(NLIST); |
| 82 | if (list == (PyObject*)NULL) |
| 83 | return (PyObject*)NULL; |
| 84 | /* list = range(NLIST) */ |
| 85 | for (i = 0; i < NLIST; ++i) { |
| 86 | PyObject* anint = PyInt_FromLong(i); |
| 87 | if (anint == (PyObject*)NULL) { |
| 88 | Py_DECREF(list); |
| 89 | return (PyObject*)NULL; |
| 90 | } |
| 91 | PyList_SET_ITEM(list, i, anint); |
| 92 | } |
| 93 | /* list.reverse(), via PyList_Reverse() */ |
| 94 | i = PyList_Reverse(list); /* should not blow up! */ |
| 95 | if (i != 0) { |
| 96 | Py_DECREF(list); |
| 97 | return (PyObject*)NULL; |
| 98 | } |
| 99 | /* Check that list == range(29, -1, -1) now */ |
| 100 | for (i = 0; i < NLIST; ++i) { |
| 101 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 102 | if (PyInt_AS_LONG(anint) != NLIST-1-i) { |
| 103 | PyErr_SetString(TestError, |
| 104 | "test_list_api: reverse screwed up"); |
| 105 | Py_DECREF(list); |
| 106 | return (PyObject*)NULL; |
| 107 | } |
| 108 | } |
| 109 | Py_DECREF(list); |
| 110 | #undef NLIST |
| 111 | |
| 112 | Py_INCREF(Py_None); |
| 113 | return Py_None; |
| 114 | } |
| 115 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 116 | static int |
| 117 | test_dict_inner(int count) |
| 118 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 119 | Py_ssize_t pos = 0, iterations = 0; |
| 120 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 121 | PyObject *dict = PyDict_New(); |
| 122 | PyObject *v, *k; |
| 123 | |
| 124 | if (dict == NULL) |
| 125 | return -1; |
| 126 | |
| 127 | for (i = 0; i < count; i++) { |
| 128 | v = PyInt_FromLong(i); |
| 129 | PyDict_SetItem(dict, v, v); |
| 130 | Py_DECREF(v); |
| 131 | } |
| 132 | |
| 133 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 134 | PyObject *o; |
| 135 | iterations++; |
| 136 | |
| 137 | i = PyInt_AS_LONG(v) + 1; |
| 138 | o = PyInt_FromLong(i); |
| 139 | if (o == NULL) |
| 140 | return -1; |
| 141 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 142 | Py_DECREF(o); |
| 143 | return -1; |
| 144 | } |
| 145 | Py_DECREF(o); |
| 146 | } |
| 147 | |
| 148 | Py_DECREF(dict); |
| 149 | |
| 150 | if (iterations != count) { |
| 151 | PyErr_SetString( |
| 152 | TestError, |
| 153 | "test_dict_iteration: dict iteration went wrong "); |
| 154 | return -1; |
| 155 | } else { |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 161 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 162 | { |
| 163 | int i; |
| 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 |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 181 | 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] | 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 |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 205 | |
| 206 | #include "testcapi_long.h" |
| 207 | |
| 208 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 209 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 210 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 211 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | #undef TESTNAME |
| 215 | #undef TYPENAME |
| 216 | #undef F_S_TO_PY |
| 217 | #undef F_PY_TO_S |
| 218 | #undef F_U_TO_PY |
| 219 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 220 | |
| 221 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 222 | |
| 223 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 224 | raise_test_longlong_error(const char* msg) |
| 225 | { |
| 226 | return raiseTestError("test_longlong_api", msg); |
| 227 | } |
| 228 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 229 | #define TESTNAME test_longlong_api_inner |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 230 | #define TYPENAME PY_LONG_LONG |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 231 | #define F_S_TO_PY PyLong_FromLongLong |
| 232 | #define F_PY_TO_S PyLong_AsLongLong |
| 233 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 234 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 235 | |
| 236 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 237 | |
| 238 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 239 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 240 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 241 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 244 | #undef TESTNAME |
| 245 | #undef TYPENAME |
| 246 | #undef F_S_TO_PY |
| 247 | #undef F_PY_TO_S |
| 248 | #undef F_U_TO_PY |
| 249 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 250 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 251 | /* 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] | 252 | for both long and int arguments. The test may leak a little memory if |
| 253 | it fails. |
| 254 | */ |
| 255 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 256 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 257 | { |
| 258 | PyObject *tuple, *num; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 259 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 260 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 261 | tuple = PyTuple_New(1); |
| 262 | if (tuple == NULL) |
| 263 | return NULL; |
| 264 | |
| 265 | num = PyLong_FromLong(42); |
| 266 | if (num == NULL) |
| 267 | return NULL; |
| 268 | |
| 269 | PyTuple_SET_ITEM(tuple, 0, num); |
| 270 | |
| 271 | value = -1; |
| 272 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 273 | return NULL; |
| 274 | if (value != 42) |
| 275 | return raiseTestError("test_L_code", |
| 276 | "L code returned wrong value for long 42"); |
| 277 | |
| 278 | Py_DECREF(num); |
| 279 | num = PyInt_FromLong(42); |
| 280 | if (num == NULL) |
| 281 | return NULL; |
| 282 | |
| 283 | PyTuple_SET_ITEM(tuple, 0, num); |
| 284 | |
| 285 | value = -1; |
| 286 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 287 | return NULL; |
| 288 | if (value != 42) |
| 289 | return raiseTestError("test_L_code", |
| 290 | "L code returned wrong value for int 42"); |
| 291 | |
| 292 | Py_DECREF(tuple); |
| 293 | Py_INCREF(Py_None); |
| 294 | return Py_None; |
| 295 | } |
| 296 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 297 | #endif /* ifdef HAVE_LONG_LONG */ |
| 298 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 299 | /* Test tuple argument processing */ |
| 300 | static PyObject * |
| 301 | getargs_tuple(PyObject *self, PyObject *args) |
| 302 | { |
| 303 | int a, b, c; |
| 304 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 305 | return NULL; |
| 306 | return Py_BuildValue("iii", a, b, c); |
| 307 | } |
| 308 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 309 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 310 | and return the result. |
| 311 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 312 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 313 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 314 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 315 | unsigned char value; |
| 316 | if (!PyArg_ParseTuple(args, "b", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 317 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 318 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 321 | static PyObject * |
| 322 | getargs_B(PyObject *self, PyObject *args) |
| 323 | { |
| 324 | unsigned char value; |
| 325 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 326 | return NULL; |
| 327 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 328 | } |
| 329 | |
| 330 | static PyObject * |
| 331 | getargs_H(PyObject *self, PyObject *args) |
| 332 | { |
| 333 | unsigned short value; |
| 334 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 335 | return NULL; |
| 336 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 337 | } |
| 338 | |
| 339 | static PyObject * |
| 340 | getargs_I(PyObject *self, PyObject *args) |
| 341 | { |
| 342 | unsigned int value; |
| 343 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 344 | return NULL; |
| 345 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 346 | } |
| 347 | |
| 348 | static PyObject * |
| 349 | getargs_k(PyObject *self, PyObject *args) |
| 350 | { |
| 351 | unsigned long value; |
| 352 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 353 | return NULL; |
| 354 | return PyLong_FromUnsignedLong(value); |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
| 358 | getargs_i(PyObject *self, PyObject *args) |
| 359 | { |
| 360 | int value; |
| 361 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 362 | return NULL; |
| 363 | return PyLong_FromLong((long)value); |
| 364 | } |
| 365 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 366 | static PyObject * |
| 367 | getargs_l(PyObject *self, PyObject *args) |
| 368 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 369 | long value; |
| 370 | if (!PyArg_ParseTuple(args, "l", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 371 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 372 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 375 | static PyObject * |
| 376 | getargs_n(PyObject *self, PyObject *args) |
| 377 | { |
| 378 | Py_ssize_t value; |
| 379 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 380 | return NULL; |
| 381 | return PyInt_FromSsize_t(value); |
| 382 | } |
| 383 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 384 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 385 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 386 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 387 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 388 | PY_LONG_LONG value; |
| 389 | if (!PyArg_ParseTuple(args, "L", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 390 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 391 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 394 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 395 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 396 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 397 | unsigned PY_LONG_LONG value; |
| 398 | if (!PyArg_ParseTuple(args, "K", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 399 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 400 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 401 | } |
| 402 | #endif |
| 403 | |
| 404 | /* This function not only tests the 'k' getargs code, but also the |
| 405 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ |
| 406 | static PyObject * |
| 407 | test_k_code(PyObject *self) |
| 408 | { |
| 409 | PyObject *tuple, *num; |
| 410 | unsigned long value; |
| 411 | |
| 412 | tuple = PyTuple_New(1); |
| 413 | if (tuple == NULL) |
| 414 | return NULL; |
| 415 | |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 416 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 417 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 418 | if (num == NULL) |
| 419 | return NULL; |
| 420 | |
| 421 | value = PyInt_AsUnsignedLongMask(num); |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 422 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 423 | return raiseTestError("test_k_code", |
| 424 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 425 | |
| 426 | PyTuple_SET_ITEM(tuple, 0, num); |
| 427 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 428 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 429 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 430 | return NULL; |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 431 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 432 | return raiseTestError("test_k_code", |
| 433 | "k code returned wrong value for long 0xFFF...FFF"); |
| 434 | |
| 435 | Py_DECREF(num); |
| 436 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 437 | if (num == NULL) |
| 438 | return NULL; |
| 439 | |
| 440 | value = PyInt_AsUnsignedLongMask(num); |
| 441 | if (value != (unsigned long)-0x42) |
| 442 | return raiseTestError("test_k_code", |
| 443 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 444 | |
| 445 | PyTuple_SET_ITEM(tuple, 0, num); |
| 446 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 447 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 448 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 449 | return NULL; |
| 450 | if (value != (unsigned long)-0x42) |
| 451 | return raiseTestError("test_k_code", |
| 452 | "k code returned wrong value for long -0xFFF..000042"); |
| 453 | |
| 454 | Py_DECREF(tuple); |
| 455 | Py_INCREF(Py_None); |
| 456 | return Py_None; |
| 457 | } |
| 458 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 459 | #ifdef Py_USING_UNICODE |
| 460 | |
| 461 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 462 | of an error. |
| 463 | */ |
| 464 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 465 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 466 | { |
| 467 | PyObject *tuple, *obj; |
| 468 | Py_UNICODE *value; |
| 469 | int len; |
| 470 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 471 | tuple = PyTuple_New(1); |
| 472 | if (tuple == NULL) |
| 473 | return NULL; |
| 474 | |
| 475 | obj = PyUnicode_Decode("test", strlen("test"), |
| 476 | "ascii", NULL); |
| 477 | if (obj == NULL) |
| 478 | return NULL; |
| 479 | |
| 480 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 481 | |
| 482 | value = 0; |
| 483 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 484 | return NULL; |
| 485 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 486 | return raiseTestError("test_u_code", |
| 487 | "u code returned wrong value for u'test'"); |
| 488 | value = 0; |
| 489 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 490 | return NULL; |
| 491 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 492 | len != PyUnicode_GET_SIZE(obj)) |
| 493 | return raiseTestError("test_u_code", |
| 494 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 495 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 496 | Py_DECREF(tuple); |
| 497 | Py_INCREF(Py_None); |
| 498 | return Py_None; |
| 499 | } |
| 500 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 501 | static PyObject * |
| 502 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 503 | { |
| 504 | const char *encoding, *errors = NULL; |
| 505 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 506 | &encoding, &errors)) |
| 507 | return NULL; |
| 508 | return PyCodec_IncrementalEncoder(encoding, errors); |
| 509 | } |
| 510 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 511 | static PyObject * |
| 512 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 513 | { |
| 514 | const char *encoding, *errors = NULL; |
| 515 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 516 | &encoding, &errors)) |
| 517 | return NULL; |
| 518 | return PyCodec_IncrementalDecoder(encoding, errors); |
| 519 | } |
| 520 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 521 | #endif |
| 522 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 523 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 524 | static PyObject * |
| 525 | test_long_numbits(PyObject *self) |
| 526 | { |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 527 | struct triple { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 528 | long input; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 529 | size_t nbits; |
| 530 | int sign; |
| 531 | } testcases[] = {{0, 0, 0}, |
| 532 | {1L, 1, 1}, |
| 533 | {-1L, 1, -1}, |
| 534 | {2L, 2, 1}, |
| 535 | {-2L, 2, -1}, |
| 536 | {3L, 2, 1}, |
| 537 | {-3L, 2, -1}, |
| 538 | {4L, 3, 1}, |
| 539 | {-4L, 3, -1}, |
| 540 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 541 | {-0x7fffL, 15, -1}, |
| 542 | {0xffffL, 16, 1}, |
| 543 | {-0xffffL, 16, -1}, |
| 544 | {0xfffffffL, 28, 1}, |
| 545 | {-0xfffffffL, 28, -1}}; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 546 | int i; |
| 547 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 548 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 549 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 550 | size_t nbits = _PyLong_NumBits(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 551 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 552 | |
| 553 | Py_DECREF(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 554 | if (nbits != testcases[i].nbits) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 555 | return raiseTestError("test_long_numbits", |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 556 | "wrong result for _PyLong_NumBits"); |
| 557 | if (sign != testcases[i].sign) |
| 558 | return raiseTestError("test_long_numbits", |
| 559 | "wrong result for _PyLong_Sign"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 560 | } |
| 561 | Py_INCREF(Py_None); |
| 562 | return Py_None; |
| 563 | } |
| 564 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 565 | /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */ |
| 566 | |
| 567 | static PyObject * |
| 568 | test_null_strings(PyObject *self) |
| 569 | { |
| 570 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL); |
| 571 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 572 | Py_XDECREF(o1); |
| 573 | Py_XDECREF(o2); |
| 574 | return tuple; |
| 575 | } |
| 576 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 577 | static PyObject * |
| 578 | raise_exception(PyObject *self, PyObject *args) |
| 579 | { |
| 580 | PyObject *exc; |
| 581 | PyObject *exc_args, *v; |
| 582 | int num_args, i; |
| 583 | |
| 584 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 585 | &exc, &num_args)) |
| 586 | return NULL; |
| 587 | |
| 588 | exc_args = PyTuple_New(num_args); |
| 589 | if (exc_args == NULL) |
| 590 | return NULL; |
| 591 | for (i = 0; i < num_args; ++i) { |
| 592 | v = PyInt_FromLong(i); |
| 593 | if (v == NULL) { |
| 594 | Py_DECREF(exc_args); |
| 595 | return NULL; |
| 596 | } |
| 597 | PyTuple_SET_ITEM(exc_args, i, v); |
| 598 | } |
| 599 | PyErr_SetObject(exc, exc_args); |
Michael W. Hudson | f02bcee | 2003-08-15 13:03:30 +0000 | [diff] [blame] | 600 | Py_DECREF(exc_args); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 601 | return NULL; |
| 602 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 603 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 604 | #ifdef WITH_THREAD |
| 605 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 606 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 607 | * `thread_done` when it's finished. The driver code has to know when the |
| 608 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 609 | * may go away when the driver finishes. The former lack of this explicit |
| 610 | * synchronization caused rare segfaults, so rare that they were seen only |
| 611 | * on a Mac buildbot (although they were possible on any box). |
| 612 | */ |
| 613 | static PyThread_type_lock thread_done = NULL; |
| 614 | |
| 615 | static void |
| 616 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 617 | { |
| 618 | PyObject *rc; |
| 619 | PyGILState_STATE s = PyGILState_Ensure(); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 620 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 621 | Py_XDECREF(rc); |
| 622 | PyGILState_Release(s); |
| 623 | } |
| 624 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 625 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 626 | * should be called only from threads spawned by test_thread_state(). |
| 627 | */ |
| 628 | static void |
| 629 | _make_call_from_thread(void *callable) |
| 630 | { |
| 631 | _make_call(callable); |
| 632 | PyThread_release_lock(thread_done); |
| 633 | } |
| 634 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 635 | static PyObject * |
| 636 | test_thread_state(PyObject *self, PyObject *args) |
| 637 | { |
| 638 | PyObject *fn; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 639 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 640 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 641 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 642 | |
| 643 | /* Ensure Python is set up for threading */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 644 | PyEval_InitThreads(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 645 | thread_done = PyThread_allocate_lock(); |
| 646 | if (thread_done == NULL) |
| 647 | return PyErr_NoMemory(); |
| 648 | PyThread_acquire_lock(thread_done, 1); |
| 649 | |
| 650 | /* Start a new thread with our callback. */ |
| 651 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 652 | /* Make the callback with the thread lock held by this thread */ |
| 653 | _make_call(fn); |
| 654 | /* Do it all again, but this time with the thread-lock released */ |
| 655 | Py_BEGIN_ALLOW_THREADS |
| 656 | _make_call(fn); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 657 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 658 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 659 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 660 | /* And once more with and without a thread |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 661 | XXX - should use a lock and work out exactly what we are trying |
| 662 | to test <wink> |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 663 | */ |
| 664 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 665 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 666 | _make_call(fn); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 667 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 668 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 669 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 670 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 671 | PyThread_release_lock(thread_done); |
| 672 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 673 | PyThread_free_lock(thread_done); |
| 674 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 675 | } |
| 676 | #endif |
| 677 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 678 | /* Some tests of PyString_FromFormat(). This needs more tests. */ |
| 679 | static PyObject * |
| 680 | test_string_from_format(PyObject *self, PyObject *args) |
| 681 | { |
| 682 | PyObject *result; |
| 683 | char *msg; |
| 684 | |
| 685 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 686 | result = PyString_FromFormat(FORMAT, (TYPE)1); \ |
| 687 | if (result == NULL) \ |
| 688 | return NULL; \ |
| 689 | if (strcmp(PyString_AsString(result), "1")) { \ |
| 690 | msg = FORMAT " failed at 1"; \ |
| 691 | goto Fail; \ |
| 692 | } \ |
| 693 | Py_DECREF(result) |
| 694 | |
| 695 | CHECK_1_FORMAT("%d", int); |
| 696 | CHECK_1_FORMAT("%ld", long); |
| 697 | /* The z width modifier was added in Python 2.5. */ |
| 698 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
| 699 | |
| 700 | /* The u type code was added in Python 2.5. */ |
| 701 | CHECK_1_FORMAT("%u", unsigned int); |
| 702 | CHECK_1_FORMAT("%lu", unsigned long); |
| 703 | CHECK_1_FORMAT("%zu", size_t); |
| 704 | |
| 705 | Py_RETURN_NONE; |
| 706 | |
| 707 | Fail: |
| 708 | Py_XDECREF(result); |
| 709 | return raiseTestError("test_string_from_format", msg); |
| 710 | |
| 711 | #undef CHECK_1_FORMAT |
| 712 | } |
| 713 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 714 | /* This is here to provide a docstring for test_descr. */ |
| 715 | static PyObject * |
| 716 | test_with_docstring(PyObject *self) |
| 717 | { |
| 718 | Py_RETURN_NONE; |
| 719 | } |
| 720 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 721 | #ifdef HAVE_GETTIMEOFDAY |
| 722 | /* Profiling of integer performance */ |
| 723 | void print_delta(int test, struct timeval *s, struct timeval *e) |
| 724 | { |
| 725 | e->tv_sec -= s->tv_sec; |
| 726 | e->tv_usec -= s->tv_usec; |
| 727 | if (e->tv_usec < 0) { |
| 728 | e->tv_sec -=1; |
| 729 | e->tv_usec += 1000000; |
| 730 | } |
| 731 | printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, e->tv_usec); |
| 732 | } |
| 733 | |
| 734 | static PyObject * |
| 735 | profile_int(PyObject *self, PyObject* args) |
| 736 | { |
| 737 | int i, k; |
| 738 | struct timeval start, stop; |
| 739 | PyObject *single, **multiple, *op1, *result; |
| 740 | |
| 741 | /* Test 1: Allocate and immediately deallocate |
| 742 | many small integers */ |
| 743 | gettimeofday(&start, NULL); |
| 744 | for(k=0; k < 20000; k++) |
| 745 | for(i=0; i < 1000; i++) { |
| 746 | single = PyInt_FromLong(i); |
| 747 | Py_DECREF(single); |
| 748 | } |
| 749 | gettimeofday(&stop, NULL); |
| 750 | print_delta(1, &start, &stop); |
| 751 | |
| 752 | /* Test 2: Allocate and immediately deallocate |
| 753 | many large integers */ |
| 754 | gettimeofday(&start, NULL); |
| 755 | for(k=0; k < 20000; k++) |
| 756 | for(i=0; i < 1000; i++) { |
| 757 | single = PyInt_FromLong(i+1000000); |
| 758 | Py_DECREF(single); |
| 759 | } |
| 760 | gettimeofday(&stop, NULL); |
| 761 | print_delta(2, &start, &stop); |
| 762 | |
| 763 | /* Test 3: Allocate a few integers, then release |
| 764 | them all simultaneously. */ |
| 765 | multiple = malloc(sizeof(PyObject*) * 1000); |
| 766 | gettimeofday(&start, NULL); |
| 767 | for(k=0; k < 20000; k++) { |
| 768 | for(i=0; i < 1000; i++) { |
| 769 | multiple[i] = PyInt_FromLong(i+1000000); |
| 770 | } |
| 771 | for(i=0; i < 1000; i++) { |
| 772 | Py_DECREF(multiple[i]); |
| 773 | } |
| 774 | } |
| 775 | gettimeofday(&stop, NULL); |
| 776 | print_delta(3, &start, &stop); |
| 777 | |
| 778 | /* Test 4: Allocate many integers, then release |
| 779 | them all simultaneously. */ |
| 780 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 781 | gettimeofday(&start, NULL); |
| 782 | for(k=0; k < 20; k++) { |
| 783 | for(i=0; i < 1000000; i++) { |
| 784 | multiple[i] = PyInt_FromLong(i+1000000); |
| 785 | } |
| 786 | for(i=0; i < 1000000; i++) { |
| 787 | Py_DECREF(multiple[i]); |
| 788 | } |
| 789 | } |
| 790 | gettimeofday(&stop, NULL); |
| 791 | print_delta(4, &start, &stop); |
| 792 | |
| 793 | /* Test 5: Allocate many integers < 32000 */ |
| 794 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 795 | gettimeofday(&start, NULL); |
| 796 | for(k=0; k < 10; k++) { |
| 797 | for(i=0; i < 1000000; i++) { |
| 798 | multiple[i] = PyInt_FromLong(i+1000); |
| 799 | } |
| 800 | for(i=0; i < 1000000; i++) { |
| 801 | Py_DECREF(multiple[i]); |
| 802 | } |
| 803 | } |
| 804 | gettimeofday(&stop, NULL); |
| 805 | print_delta(5, &start, &stop); |
| 806 | |
| 807 | /* Test 6: Perform small int addition */ |
| 808 | op1 = PyInt_FromLong(1); |
| 809 | gettimeofday(&start, NULL); |
| 810 | for(i=0; i < 10000000; i++) { |
| 811 | result = PyNumber_Add(op1, op1); |
| 812 | Py_DECREF(result); |
| 813 | } |
| 814 | gettimeofday(&stop, NULL); |
| 815 | Py_DECREF(op1); |
| 816 | print_delta(6, &start, &stop); |
| 817 | |
| 818 | /* Test 7: Perform medium int addition */ |
| 819 | op1 = PyInt_FromLong(1000); |
| 820 | gettimeofday(&start, NULL); |
| 821 | for(i=0; i < 10000000; i++) { |
| 822 | result = PyNumber_Add(op1, op1); |
| 823 | Py_DECREF(result); |
| 824 | } |
| 825 | gettimeofday(&stop, NULL); |
| 826 | Py_DECREF(op1); |
| 827 | print_delta(7, &start, &stop); |
| 828 | |
| 829 | Py_INCREF(Py_None); |
| 830 | return Py_None; |
| 831 | } |
| 832 | #endif |
| 833 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 834 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 835 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 836 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 837 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 838 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 839 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 840 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 841 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 842 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 843 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 844 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 845 | PyDoc_STR("This is a pretty normal docstring.")}, |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 846 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 847 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 848 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 849 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 850 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 851 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 852 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 853 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 854 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 855 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 856 | #ifdef HAVE_LONG_LONG |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 857 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 858 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 859 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 860 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 861 | {"codec_incrementalencoder", |
| 862 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 863 | {"codec_incrementaldecoder", |
| 864 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 865 | #endif |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 866 | #ifdef Py_USING_UNICODE |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 867 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 868 | #endif |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 869 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 870 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 871 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 872 | #ifdef HAVE_GETTIMEOFDAY |
| 873 | {"profile_int", profile_int, METH_NOARGS}, |
| 874 | #endif |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 875 | {NULL, NULL} /* sentinel */ |
| 876 | }; |
| 877 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 878 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 879 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 880 | typedef struct { |
| 881 | char byte_member; |
| 882 | unsigned char ubyte_member; |
| 883 | short short_member; |
| 884 | unsigned short ushort_member; |
| 885 | int int_member; |
| 886 | unsigned int uint_member; |
| 887 | long long_member; |
| 888 | unsigned long ulong_member; |
| 889 | float float_member; |
| 890 | double double_member; |
| 891 | } all_structmembers; |
| 892 | |
| 893 | typedef struct { |
| 894 | PyObject_HEAD |
| 895 | all_structmembers structmembers; |
| 896 | } test_structmembers; |
| 897 | |
| 898 | static struct PyMemberDef test_members[] = { |
| 899 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 900 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 901 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 902 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 903 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 904 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 905 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 906 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 907 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 908 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 909 | {NULL} |
| 910 | }; |
| 911 | |
| 912 | |
| 913 | static PyObject *test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs){ |
| 914 | static char *keywords[]={"T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", "T_INT", "T_UINT", |
| 915 | "T_LONG", "T_ULONG", "T_FLOAT", "T_DOUBLE", NULL}; |
| 916 | test_structmembers *ob=PyObject_New(test_structmembers, type); |
| 917 | if (ob==NULL) |
| 918 | return NULL; |
| 919 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 920 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|bBhHiIlkfd", keywords, |
| 921 | &ob->structmembers.byte_member, &ob->structmembers.ubyte_member, |
| 922 | &ob->structmembers.short_member, &ob->structmembers.ushort_member, |
| 923 | &ob->structmembers.int_member, &ob->structmembers.uint_member, |
| 924 | &ob->structmembers.long_member, &ob->structmembers.ulong_member, |
| 925 | &ob->structmembers.float_member, &ob->structmembers.double_member)){ |
| 926 | Py_DECREF(ob); |
| 927 | return NULL; |
| 928 | } |
| 929 | return (PyObject *)ob; |
| 930 | } |
| 931 | |
| 932 | static void test_structmembers_free(PyObject *ob){ |
| 933 | PyObject_FREE(ob); |
| 934 | } |
| 935 | |
| 936 | static PyTypeObject test_structmembersType = { |
| 937 | PyObject_HEAD_INIT(NULL) |
| 938 | 0, |
| 939 | "test_structmembersType", |
| 940 | sizeof(test_structmembers), /* tp_basicsize */ |
| 941 | 0, /* tp_itemsize */ |
| 942 | test_structmembers_free, /* destructor tp_dealloc */ |
| 943 | 0, /* tp_print */ |
| 944 | 0, /* tp_getattr */ |
| 945 | 0, /* tp_setattr */ |
| 946 | 0, /* tp_compare */ |
| 947 | 0, /* tp_repr */ |
| 948 | 0, /* tp_as_number */ |
| 949 | 0, /* tp_as_sequence */ |
| 950 | 0, /* tp_as_mapping */ |
| 951 | 0, /* tp_hash */ |
| 952 | 0, /* tp_call */ |
| 953 | 0, /* tp_str */ |
| 954 | PyObject_GenericGetAttr, |
| 955 | PyObject_GenericSetAttr, |
| 956 | 0, /* tp_as_buffer */ |
| 957 | 0, /* tp_flags */ |
| 958 | "Type containing all structmember types", |
| 959 | 0, /* traverseproc tp_traverse */ |
| 960 | 0, /* tp_clear */ |
| 961 | 0, /* tp_richcompare */ |
| 962 | 0, /* tp_weaklistoffset */ |
| 963 | 0, /* tp_iter */ |
| 964 | 0, /* tp_iternext */ |
| 965 | 0, /* tp_methods */ |
| 966 | test_members, /* tp_members */ |
| 967 | 0, |
| 968 | 0, |
| 969 | 0, |
| 970 | 0, |
| 971 | 0, |
| 972 | 0, |
| 973 | 0, |
| 974 | 0, |
| 975 | test_structmembers_new, /* tp_new */ |
| 976 | }; |
| 977 | |
| 978 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 979 | PyMODINIT_FUNC |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 980 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 981 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 982 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 983 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 984 | m = Py_InitModule("_testcapi", TestMethods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 985 | if (m == NULL) |
| 986 | return; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 987 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 988 | test_structmembersType.ob_type=&PyType_Type; |
| 989 | Py_INCREF(&test_structmembersType); |
| 990 | PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType); |
| 991 | |
| 992 | PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)); |
| 993 | PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 994 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 995 | PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)); |
| 996 | PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 997 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 998 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 999 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1000 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1001 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1002 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); |
| 1003 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 1004 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 1005 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 1006 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 1007 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1008 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1009 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1010 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1011 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1012 | Py_INCREF(TestError); |
| 1013 | PyModule_AddObject(m, "error", TestError); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1014 | } |