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