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