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) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 88 | PyObject* anint = PyLong_FromLong(i); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 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); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 104 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 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++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 130 | v = PyLong_FromLong(i); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 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 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 139 | i = PyLong_AS_LONG(v) + 1; |
| 140 | o = PyLong_FromLong(i); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 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 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 178 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 179 | * PyType_Ready if it hasn't already been called |
| 180 | */ |
| 181 | static PyTypeObject _HashInheritanceTester_Type = { |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 182 | PyVarObject_HEAD_INIT(NULL, 0) |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 183 | "hashinheritancetester", /* Name of this type */ |
| 184 | sizeof(PyObject), /* Basic object size */ |
| 185 | 0, /* Item size for varobject */ |
| 186 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 187 | 0, /* tp_print */ |
| 188 | 0, /* tp_getattr */ |
| 189 | 0, /* tp_setattr */ |
| 190 | 0, /* tp_compare */ |
| 191 | 0, /* tp_repr */ |
| 192 | 0, /* tp_as_number */ |
| 193 | 0, /* tp_as_sequence */ |
| 194 | 0, /* tp_as_mapping */ |
| 195 | 0, /* tp_hash */ |
| 196 | 0, /* tp_call */ |
| 197 | 0, /* tp_str */ |
| 198 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 199 | 0, /* tp_setattro */ |
| 200 | 0, /* tp_as_buffer */ |
| 201 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 202 | 0, /* tp_doc */ |
| 203 | 0, /* tp_traverse */ |
| 204 | 0, /* tp_clear */ |
| 205 | 0, /* tp_richcompare */ |
| 206 | 0, /* tp_weaklistoffset */ |
| 207 | 0, /* tp_iter */ |
| 208 | 0, /* tp_iternext */ |
| 209 | 0, /* tp_methods */ |
| 210 | 0, /* tp_members */ |
| 211 | 0, /* tp_getset */ |
| 212 | 0, /* tp_base */ |
| 213 | 0, /* tp_dict */ |
| 214 | 0, /* tp_descr_get */ |
| 215 | 0, /* tp_descr_set */ |
| 216 | 0, /* tp_dictoffset */ |
| 217 | 0, /* tp_init */ |
| 218 | 0, /* tp_alloc */ |
| 219 | PyType_GenericNew, /* tp_new */ |
| 220 | }; |
| 221 | |
| 222 | static PyObject* |
| 223 | test_lazy_hash_inheritance(PyObject* self) |
| 224 | { |
| 225 | PyTypeObject *type; |
| 226 | PyObject *obj; |
| 227 | long hash; |
| 228 | |
| 229 | type = &_HashInheritanceTester_Type; |
| 230 | obj = PyObject_New(PyObject, type); |
| 231 | if (obj == NULL) { |
| 232 | PyErr_Clear(); |
| 233 | PyErr_SetString( |
| 234 | TestError, |
| 235 | "test_lazy_hash_inheritance: failed to create object"); |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | if (type->tp_dict != NULL) { |
| 240 | PyErr_SetString( |
| 241 | TestError, |
| 242 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 243 | Py_DECREF(obj); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | hash = PyObject_Hash(obj); |
| 248 | if ((hash == -1) && PyErr_Occurred()) { |
| 249 | PyErr_Clear(); |
| 250 | PyErr_SetString( |
| 251 | TestError, |
| 252 | "test_lazy_hash_inheritance: could not hash object"); |
| 253 | Py_DECREF(obj); |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | if (type->tp_dict == NULL) { |
| 258 | PyErr_SetString( |
| 259 | TestError, |
| 260 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 261 | Py_DECREF(obj); |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 266 | PyErr_SetString( |
| 267 | TestError, |
| 268 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 269 | Py_DECREF(obj); |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | Py_RETURN_NONE; |
| 274 | } |
| 275 | |
| 276 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 277 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 278 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 279 | |
| 280 | Note that the meat of the test is contained in testcapi_long.h. |
| 281 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 282 | 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] | 283 | dependence on type names makes it impossible to use a parameterized |
| 284 | function. A giant macro would be even worse than this. A C++ template |
| 285 | would be perfect. |
| 286 | |
| 287 | The "report an error" functions are deliberately not part of the #include |
| 288 | file: if the test fails, you can set a breakpoint in the appropriate |
| 289 | error function directly, and crawl back from there in the debugger. |
| 290 | */ |
| 291 | |
| 292 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 293 | |
| 294 | static PyObject * |
| 295 | raise_test_long_error(const char* msg) |
| 296 | { |
| 297 | return raiseTestError("test_long_api", msg); |
| 298 | } |
| 299 | |
| 300 | #define TESTNAME test_long_api_inner |
| 301 | #define TYPENAME long |
| 302 | #define F_S_TO_PY PyLong_FromLong |
| 303 | #define F_PY_TO_S PyLong_AsLong |
| 304 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 305 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 306 | |
| 307 | #include "testcapi_long.h" |
| 308 | |
| 309 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 310 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 311 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 312 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | #undef TESTNAME |
| 316 | #undef TYPENAME |
| 317 | #undef F_S_TO_PY |
| 318 | #undef F_PY_TO_S |
| 319 | #undef F_U_TO_PY |
| 320 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 321 | |
| 322 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 323 | |
| 324 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 325 | raise_test_longlong_error(const char* msg) |
| 326 | { |
| 327 | return raiseTestError("test_longlong_api", msg); |
| 328 | } |
| 329 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 330 | #define TESTNAME test_longlong_api_inner |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 331 | #define TYPENAME PY_LONG_LONG |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 332 | #define F_S_TO_PY PyLong_FromLongLong |
| 333 | #define F_PY_TO_S PyLong_AsLongLong |
| 334 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 335 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 336 | |
| 337 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 338 | |
| 339 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 340 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 341 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 342 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 345 | #undef TESTNAME |
| 346 | #undef TYPENAME |
| 347 | #undef F_S_TO_PY |
| 348 | #undef F_PY_TO_S |
| 349 | #undef F_U_TO_PY |
| 350 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 351 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 352 | /* 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] | 353 | for both long and int arguments. The test may leak a little memory if |
| 354 | it fails. |
| 355 | */ |
| 356 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 357 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 358 | { |
| 359 | PyObject *tuple, *num; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 360 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 361 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 362 | tuple = PyTuple_New(1); |
| 363 | if (tuple == NULL) |
| 364 | return NULL; |
| 365 | |
| 366 | num = PyLong_FromLong(42); |
| 367 | if (num == NULL) |
| 368 | return NULL; |
| 369 | |
| 370 | PyTuple_SET_ITEM(tuple, 0, num); |
| 371 | |
| 372 | value = -1; |
| 373 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 374 | return NULL; |
| 375 | if (value != 42) |
| 376 | return raiseTestError("test_L_code", |
| 377 | "L code returned wrong value for long 42"); |
| 378 | |
| 379 | Py_DECREF(num); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 380 | num = PyLong_FromLong(42); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 381 | if (num == NULL) |
| 382 | return NULL; |
| 383 | |
| 384 | PyTuple_SET_ITEM(tuple, 0, num); |
| 385 | |
| 386 | value = -1; |
| 387 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 388 | return NULL; |
| 389 | if (value != 42) |
| 390 | return raiseTestError("test_L_code", |
| 391 | "L code returned wrong value for int 42"); |
| 392 | |
| 393 | Py_DECREF(tuple); |
| 394 | Py_INCREF(Py_None); |
| 395 | return Py_None; |
| 396 | } |
| 397 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 398 | #endif /* ifdef HAVE_LONG_LONG */ |
| 399 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 400 | /* Test tuple argument processing */ |
| 401 | static PyObject * |
| 402 | getargs_tuple(PyObject *self, PyObject *args) |
| 403 | { |
| 404 | int a, b, c; |
| 405 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 406 | return NULL; |
| 407 | return Py_BuildValue("iii", a, b, c); |
| 408 | } |
| 409 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 410 | /* test PyArg_ParseTupleAndKeywords */ |
| 411 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 412 | { |
| 413 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 414 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 415 | int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 416 | |
| 417 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 418 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 419 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 420 | return NULL; |
| 421 | return Py_BuildValue("iiiiiiiiii", |
| 422 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 423 | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
| 424 | } |
| 425 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 426 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 427 | and return the result. |
| 428 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 429 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 430 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 431 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 432 | unsigned char value; |
| 433 | if (!PyArg_ParseTuple(args, "b", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 434 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 435 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 438 | static PyObject * |
| 439 | getargs_B(PyObject *self, PyObject *args) |
| 440 | { |
| 441 | unsigned char value; |
| 442 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 443 | return NULL; |
| 444 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 445 | } |
| 446 | |
| 447 | static PyObject * |
| 448 | getargs_H(PyObject *self, PyObject *args) |
| 449 | { |
| 450 | unsigned short value; |
| 451 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 452 | return NULL; |
| 453 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 454 | } |
| 455 | |
| 456 | static PyObject * |
| 457 | getargs_I(PyObject *self, PyObject *args) |
| 458 | { |
| 459 | unsigned int value; |
| 460 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 461 | return NULL; |
| 462 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 463 | } |
| 464 | |
| 465 | static PyObject * |
| 466 | getargs_k(PyObject *self, PyObject *args) |
| 467 | { |
| 468 | unsigned long value; |
| 469 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 470 | return NULL; |
| 471 | return PyLong_FromUnsignedLong(value); |
| 472 | } |
| 473 | |
| 474 | static PyObject * |
| 475 | getargs_i(PyObject *self, PyObject *args) |
| 476 | { |
| 477 | int value; |
| 478 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 479 | return NULL; |
| 480 | return PyLong_FromLong((long)value); |
| 481 | } |
| 482 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 483 | static PyObject * |
| 484 | getargs_l(PyObject *self, PyObject *args) |
| 485 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 486 | long value; |
| 487 | if (!PyArg_ParseTuple(args, "l", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 488 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 489 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 492 | static PyObject * |
| 493 | getargs_n(PyObject *self, PyObject *args) |
| 494 | { |
| 495 | Py_ssize_t value; |
| 496 | if (!PyArg_ParseTuple(args, "n", &value)) |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 497 | return NULL; |
| 498 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 501 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 502 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 503 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 504 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 505 | PY_LONG_LONG value; |
| 506 | if (!PyArg_ParseTuple(args, "L", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 507 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 508 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 511 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 512 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 513 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 514 | unsigned PY_LONG_LONG value; |
| 515 | if (!PyArg_ParseTuple(args, "K", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 516 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 517 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 518 | } |
| 519 | #endif |
| 520 | |
| 521 | /* This function not only tests the 'k' getargs code, but also the |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 522 | PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 523 | static PyObject * |
| 524 | test_k_code(PyObject *self) |
| 525 | { |
| 526 | PyObject *tuple, *num; |
| 527 | unsigned long value; |
| 528 | |
| 529 | tuple = PyTuple_New(1); |
| 530 | if (tuple == NULL) |
| 531 | return NULL; |
| 532 | |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 533 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 534 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 535 | if (num == NULL) |
| 536 | return NULL; |
| 537 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 538 | value = PyLong_AsUnsignedLongMask(num); |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 539 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 540 | return raiseTestError("test_k_code", |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 541 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 542 | |
| 543 | PyTuple_SET_ITEM(tuple, 0, num); |
| 544 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 545 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 546 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 547 | return NULL; |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 548 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 549 | return raiseTestError("test_k_code", |
| 550 | "k code returned wrong value for long 0xFFF...FFF"); |
| 551 | |
| 552 | Py_DECREF(num); |
| 553 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 554 | if (num == NULL) |
| 555 | return NULL; |
| 556 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 557 | value = PyLong_AsUnsignedLongMask(num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 558 | if (value != (unsigned long)-0x42) |
| 559 | return raiseTestError("test_k_code", |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 560 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 561 | |
| 562 | PyTuple_SET_ITEM(tuple, 0, num); |
| 563 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 564 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 565 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 566 | return NULL; |
| 567 | if (value != (unsigned long)-0x42) |
| 568 | return raiseTestError("test_k_code", |
| 569 | "k code returned wrong value for long -0xFFF..000042"); |
| 570 | |
| 571 | Py_DECREF(tuple); |
| 572 | Py_INCREF(Py_None); |
| 573 | return Py_None; |
| 574 | } |
| 575 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 576 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 577 | /* Test the s and z codes for PyArg_ParseTuple. |
| 578 | */ |
| 579 | static PyObject * |
| 580 | test_s_code(PyObject *self) |
| 581 | { |
| 582 | /* Unicode strings should be accepted */ |
| 583 | PyObject *tuple, *obj; |
| 584 | char *value; |
| 585 | |
| 586 | tuple = PyTuple_New(1); |
| 587 | if (tuple == NULL) |
| 588 | return NULL; |
| 589 | |
| 590 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
| 591 | "latin-1", NULL); |
| 592 | if (obj == NULL) |
| 593 | return NULL; |
| 594 | |
| 595 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 596 | |
| 597 | /* These two blocks used to raise a TypeError: |
| 598 | * "argument must be string without null bytes, not str" |
| 599 | */ |
| 600 | if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) |
| 601 | return NULL; |
| 602 | |
| 603 | if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) |
| 604 | return NULL; |
| 605 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 606 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 607 | Py_RETURN_NONE; |
| 608 | } |
| 609 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 610 | static volatile int x; |
| 611 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 612 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 613 | of an error. |
| 614 | */ |
| 615 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 616 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 617 | { |
| 618 | PyObject *tuple, *obj; |
| 619 | Py_UNICODE *value; |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 620 | Py_ssize_t len; |
Christian Heimes | 3bae65b | 2008-11-11 12:53:39 +0000 | [diff] [blame] | 621 | int x; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 622 | |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 623 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 624 | /* Just use the macro and check that it compiles */ |
Christian Heimes | 727c2c5 | 2008-11-12 09:04:04 +0000 | [diff] [blame] | 625 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 626 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 627 | tuple = PyTuple_New(1); |
| 628 | if (tuple == NULL) |
| 629 | return NULL; |
| 630 | |
| 631 | obj = PyUnicode_Decode("test", strlen("test"), |
| 632 | "ascii", NULL); |
| 633 | if (obj == NULL) |
| 634 | return NULL; |
| 635 | |
| 636 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 637 | |
| 638 | value = 0; |
| 639 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 640 | return NULL; |
| 641 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 642 | return raiseTestError("test_u_code", |
| 643 | "u code returned wrong value for u'test'"); |
| 644 | value = 0; |
| 645 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 646 | return NULL; |
| 647 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 648 | len != PyUnicode_GET_SIZE(obj)) |
| 649 | return raiseTestError("test_u_code", |
| 650 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 651 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 652 | Py_DECREF(tuple); |
| 653 | Py_INCREF(Py_None); |
| 654 | return Py_None; |
| 655 | } |
| 656 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 657 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 658 | static PyObject * |
| 659 | test_Z_code(PyObject *self) |
| 660 | { |
| 661 | PyObject *tuple, *obj; |
| 662 | Py_UNICODE *value1, *value2; |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 663 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 664 | |
| 665 | tuple = PyTuple_New(2); |
| 666 | if (tuple == NULL) |
| 667 | return NULL; |
| 668 | |
| 669 | obj = PyUnicode_FromString("test"); |
| 670 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 671 | Py_INCREF(Py_None); |
| 672 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
| 673 | |
| 674 | /* swap values on purpose */ |
| 675 | value1 = NULL; |
| 676 | value2 = PyUnicode_AS_UNICODE(obj); |
| 677 | |
| 678 | /* Test Z for both values */ |
| 679 | if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) |
| 680 | return NULL; |
| 681 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 682 | return raiseTestError("test_Z_code", |
| 683 | "Z code returned wrong value for 'test'"); |
| 684 | if (value2 != NULL) |
| 685 | return raiseTestError("test_Z_code", |
| 686 | "Z code returned wrong value for None"); |
| 687 | |
| 688 | value1 = NULL; |
| 689 | value2 = PyUnicode_AS_UNICODE(obj); |
| 690 | len1 = -1; |
| 691 | len2 = -1; |
| 692 | |
| 693 | /* Test Z# for both values */ |
| 694 | if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 695 | &value2, &len2) < 0) |
| 696 | return NULL; |
| 697 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 698 | len1 != PyUnicode_GET_SIZE(obj)) |
| 699 | return raiseTestError("test_Z_code", |
| 700 | "Z# code returned wrong values for 'test'"); |
| 701 | if (value2 != NULL || |
| 702 | len2 != 0) |
| 703 | return raiseTestError("test_Z_code", |
| 704 | "Z# code returned wrong values for None'"); |
| 705 | |
| 706 | Py_DECREF(tuple); |
| 707 | Py_RETURN_NONE; |
| 708 | } |
| 709 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 710 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 711 | test_empty_argparse(PyObject *self) |
| 712 | { |
| 713 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 714 | PyObject *tuple, *dict = NULL; |
| 715 | static char *kwlist[] = {NULL}; |
| 716 | int result; |
| 717 | tuple = PyTuple_New(0); |
| 718 | if (!tuple) |
| 719 | return NULL; |
| 720 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 721 | goto done; |
| 722 | dict = PyDict_New(); |
| 723 | if (!dict) |
| 724 | goto done; |
| 725 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
| 726 | done: |
| 727 | Py_DECREF(tuple); |
| 728 | Py_XDECREF(dict); |
| 729 | if (result < 0) |
| 730 | return NULL; |
| 731 | else { |
| 732 | Py_RETURN_NONE; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 737 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 738 | { |
| 739 | const char *encoding, *errors = NULL; |
| 740 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 741 | &encoding, &errors)) |
| 742 | return NULL; |
| 743 | return PyCodec_IncrementalEncoder(encoding, errors); |
| 744 | } |
| 745 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 746 | static PyObject * |
| 747 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 748 | { |
| 749 | const char *encoding, *errors = NULL; |
| 750 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 751 | &encoding, &errors)) |
| 752 | return NULL; |
| 753 | return PyCodec_IncrementalDecoder(encoding, errors); |
| 754 | } |
| 755 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 756 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 757 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 758 | static PyObject * |
| 759 | test_long_numbits(PyObject *self) |
| 760 | { |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 761 | struct triple { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 762 | long input; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 763 | size_t nbits; |
| 764 | int sign; |
| 765 | } testcases[] = {{0, 0, 0}, |
| 766 | {1L, 1, 1}, |
| 767 | {-1L, 1, -1}, |
| 768 | {2L, 2, 1}, |
| 769 | {-2L, 2, -1}, |
| 770 | {3L, 2, 1}, |
| 771 | {-3L, 2, -1}, |
| 772 | {4L, 3, 1}, |
| 773 | {-4L, 3, -1}, |
| 774 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 775 | {-0x7fffL, 15, -1}, |
| 776 | {0xffffL, 16, 1}, |
| 777 | {-0xffffL, 16, -1}, |
| 778 | {0xfffffffL, 28, 1}, |
| 779 | {-0xfffffffL, 28, -1}}; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 780 | int i; |
| 781 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 782 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 783 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 784 | size_t nbits = _PyLong_NumBits(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 785 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 786 | |
| 787 | Py_DECREF(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 788 | if (nbits != testcases[i].nbits) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 789 | return raiseTestError("test_long_numbits", |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 790 | "wrong result for _PyLong_NumBits"); |
| 791 | if (sign != testcases[i].sign) |
| 792 | return raiseTestError("test_long_numbits", |
| 793 | "wrong result for _PyLong_Sign"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 794 | } |
| 795 | Py_INCREF(Py_None); |
| 796 | return Py_None; |
| 797 | } |
| 798 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 799 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 800 | |
| 801 | static PyObject * |
| 802 | test_null_strings(PyObject *self) |
| 803 | { |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 804 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 805 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 806 | Py_XDECREF(o1); |
| 807 | Py_XDECREF(o2); |
| 808 | return tuple; |
| 809 | } |
| 810 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 811 | static PyObject * |
| 812 | raise_exception(PyObject *self, PyObject *args) |
| 813 | { |
| 814 | PyObject *exc; |
| 815 | PyObject *exc_args, *v; |
| 816 | int num_args, i; |
| 817 | |
| 818 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 819 | &exc, &num_args)) |
| 820 | return NULL; |
| 821 | |
| 822 | exc_args = PyTuple_New(num_args); |
| 823 | if (exc_args == NULL) |
| 824 | return NULL; |
| 825 | for (i = 0; i < num_args; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 826 | v = PyLong_FromLong(i); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 827 | if (v == NULL) { |
| 828 | Py_DECREF(exc_args); |
| 829 | return NULL; |
| 830 | } |
| 831 | PyTuple_SET_ITEM(exc_args, i, v); |
| 832 | } |
| 833 | PyErr_SetObject(exc, exc_args); |
Michael W. Hudson | f02bcee | 2003-08-15 13:03:30 +0000 | [diff] [blame] | 834 | Py_DECREF(exc_args); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 835 | return NULL; |
| 836 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 837 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 838 | #ifdef WITH_THREAD |
| 839 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 840 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 841 | * `thread_done` when it's finished. The driver code has to know when the |
| 842 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 843 | * may go away when the driver finishes. The former lack of this explicit |
| 844 | * synchronization caused rare segfaults, so rare that they were seen only |
| 845 | * on a Mac buildbot (although they were possible on any box). |
| 846 | */ |
| 847 | static PyThread_type_lock thread_done = NULL; |
| 848 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 849 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 850 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 851 | { |
| 852 | PyObject *rc; |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 853 | int success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 854 | PyGILState_STATE s = PyGILState_Ensure(); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 855 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 856 | success = (rc != NULL); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 857 | Py_XDECREF(rc); |
| 858 | PyGILState_Release(s); |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 859 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 862 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 863 | * should be called only from threads spawned by test_thread_state(). |
| 864 | */ |
| 865 | static void |
| 866 | _make_call_from_thread(void *callable) |
| 867 | { |
| 868 | _make_call(callable); |
| 869 | PyThread_release_lock(thread_done); |
| 870 | } |
| 871 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 872 | static PyObject * |
| 873 | test_thread_state(PyObject *self, PyObject *args) |
| 874 | { |
| 875 | PyObject *fn; |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 876 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 877 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 878 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 879 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 880 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 881 | if (!PyCallable_Check(fn)) { |
| 882 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 883 | fn->ob_type->tp_name); |
| 884 | return NULL; |
| 885 | } |
| 886 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 887 | /* Ensure Python is set up for threading */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 888 | PyEval_InitThreads(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 889 | thread_done = PyThread_allocate_lock(); |
| 890 | if (thread_done == NULL) |
| 891 | return PyErr_NoMemory(); |
| 892 | PyThread_acquire_lock(thread_done, 1); |
| 893 | |
| 894 | /* Start a new thread with our callback. */ |
| 895 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 896 | /* Make the callback with the thread lock held by this thread */ |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 897 | success &= _make_call(fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 898 | /* Do it all again, but this time with the thread-lock released */ |
| 899 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 900 | success &= _make_call(fn); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 901 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 902 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 903 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 904 | /* And once more with and without a thread |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 905 | XXX - should use a lock and work out exactly what we are trying |
| 906 | to test <wink> |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 907 | */ |
| 908 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 909 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 910 | success &= _make_call(fn); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 911 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 912 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 913 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 914 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 915 | PyThread_release_lock(thread_done); |
| 916 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 917 | PyThread_free_lock(thread_done); |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 918 | if (!success) |
| 919 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 920 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 921 | } |
| 922 | #endif |
| 923 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 924 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 925 | static PyObject * |
| 926 | test_string_from_format(PyObject *self, PyObject *args) |
| 927 | { |
| 928 | PyObject *result; |
| 929 | char *msg; |
| 930 | |
| 931 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 932 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 933 | if (result == NULL) \ |
| 934 | return NULL; \ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 935 | if (strcmp(_PyUnicode_AsString(result), "1")) { \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 936 | msg = FORMAT " failed at 1"; \ |
| 937 | goto Fail; \ |
| 938 | } \ |
| 939 | Py_DECREF(result) |
| 940 | |
| 941 | CHECK_1_FORMAT("%d", int); |
| 942 | CHECK_1_FORMAT("%ld", long); |
| 943 | /* The z width modifier was added in Python 2.5. */ |
| 944 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
| 945 | |
| 946 | /* The u type code was added in Python 2.5. */ |
| 947 | CHECK_1_FORMAT("%u", unsigned int); |
| 948 | CHECK_1_FORMAT("%lu", unsigned long); |
| 949 | CHECK_1_FORMAT("%zu", size_t); |
| 950 | |
| 951 | Py_RETURN_NONE; |
| 952 | |
| 953 | Fail: |
| 954 | Py_XDECREF(result); |
| 955 | return raiseTestError("test_string_from_format", msg); |
| 956 | |
| 957 | #undef CHECK_1_FORMAT |
| 958 | } |
| 959 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 960 | /* This is here to provide a docstring for test_descr. */ |
| 961 | static PyObject * |
| 962 | test_with_docstring(PyObject *self) |
| 963 | { |
| 964 | Py_RETURN_NONE; |
| 965 | } |
| 966 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 967 | #ifdef HAVE_GETTIMEOFDAY |
| 968 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 969 | static void print_delta(int test, struct timeval *s, struct timeval *e) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 970 | { |
| 971 | e->tv_sec -= s->tv_sec; |
| 972 | e->tv_usec -= s->tv_usec; |
| 973 | if (e->tv_usec < 0) { |
| 974 | e->tv_sec -=1; |
| 975 | e->tv_usec += 1000000; |
| 976 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 977 | 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] | 978 | } |
| 979 | |
| 980 | static PyObject * |
| 981 | profile_int(PyObject *self, PyObject* args) |
| 982 | { |
| 983 | int i, k; |
| 984 | struct timeval start, stop; |
| 985 | PyObject *single, **multiple, *op1, *result; |
| 986 | |
| 987 | /* Test 1: Allocate and immediately deallocate |
| 988 | many small integers */ |
| 989 | gettimeofday(&start, NULL); |
| 990 | for(k=0; k < 20000; k++) |
| 991 | for(i=0; i < 1000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 992 | single = PyLong_FromLong(i); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 993 | Py_DECREF(single); |
| 994 | } |
| 995 | gettimeofday(&stop, NULL); |
| 996 | print_delta(1, &start, &stop); |
| 997 | |
| 998 | /* Test 2: Allocate and immediately deallocate |
| 999 | many large integers */ |
| 1000 | gettimeofday(&start, NULL); |
| 1001 | for(k=0; k < 20000; k++) |
| 1002 | for(i=0; i < 1000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1003 | single = PyLong_FromLong(i+1000000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1004 | Py_DECREF(single); |
| 1005 | } |
| 1006 | gettimeofday(&stop, NULL); |
| 1007 | print_delta(2, &start, &stop); |
| 1008 | |
| 1009 | /* Test 3: Allocate a few integers, then release |
| 1010 | them all simultaneously. */ |
| 1011 | multiple = malloc(sizeof(PyObject*) * 1000); |
| 1012 | gettimeofday(&start, NULL); |
| 1013 | for(k=0; k < 20000; k++) { |
| 1014 | for(i=0; i < 1000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1015 | multiple[i] = PyLong_FromLong(i+1000000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1016 | } |
| 1017 | for(i=0; i < 1000; i++) { |
| 1018 | Py_DECREF(multiple[i]); |
| 1019 | } |
| 1020 | } |
| 1021 | gettimeofday(&stop, NULL); |
| 1022 | print_delta(3, &start, &stop); |
| 1023 | |
| 1024 | /* Test 4: Allocate many integers, then release |
| 1025 | them all simultaneously. */ |
| 1026 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 1027 | gettimeofday(&start, NULL); |
| 1028 | for(k=0; k < 20; k++) { |
| 1029 | for(i=0; i < 1000000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1030 | multiple[i] = PyLong_FromLong(i+1000000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1031 | } |
| 1032 | for(i=0; i < 1000000; i++) { |
| 1033 | Py_DECREF(multiple[i]); |
| 1034 | } |
| 1035 | } |
| 1036 | gettimeofday(&stop, NULL); |
| 1037 | print_delta(4, &start, &stop); |
| 1038 | |
| 1039 | /* Test 5: Allocate many integers < 32000 */ |
| 1040 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 1041 | gettimeofday(&start, NULL); |
| 1042 | for(k=0; k < 10; k++) { |
| 1043 | for(i=0; i < 1000000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1044 | multiple[i] = PyLong_FromLong(i+1000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1045 | } |
| 1046 | for(i=0; i < 1000000; i++) { |
| 1047 | Py_DECREF(multiple[i]); |
| 1048 | } |
| 1049 | } |
| 1050 | gettimeofday(&stop, NULL); |
| 1051 | print_delta(5, &start, &stop); |
| 1052 | |
| 1053 | /* Test 6: Perform small int addition */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1054 | op1 = PyLong_FromLong(1); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1055 | gettimeofday(&start, NULL); |
| 1056 | for(i=0; i < 10000000; i++) { |
| 1057 | result = PyNumber_Add(op1, op1); |
| 1058 | Py_DECREF(result); |
| 1059 | } |
| 1060 | gettimeofday(&stop, NULL); |
| 1061 | Py_DECREF(op1); |
| 1062 | print_delta(6, &start, &stop); |
| 1063 | |
| 1064 | /* Test 7: Perform medium int addition */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1065 | op1 = PyLong_FromLong(1000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1066 | gettimeofday(&start, NULL); |
| 1067 | for(i=0; i < 10000000; i++) { |
| 1068 | result = PyNumber_Add(op1, op1); |
| 1069 | Py_DECREF(result); |
| 1070 | } |
| 1071 | gettimeofday(&stop, NULL); |
| 1072 | Py_DECREF(op1); |
| 1073 | print_delta(7, &start, &stop); |
| 1074 | |
| 1075 | Py_INCREF(Py_None); |
| 1076 | return Py_None; |
| 1077 | } |
| 1078 | #endif |
| 1079 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1080 | /* To test the format of tracebacks as printed out. */ |
| 1081 | static PyObject * |
| 1082 | traceback_print(PyObject *self, PyObject *args) |
| 1083 | { |
| 1084 | PyObject *file; |
| 1085 | PyObject *traceback; |
| 1086 | int result; |
| 1087 | |
| 1088 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 1089 | &traceback, &file)) |
| 1090 | return NULL; |
| 1091 | |
| 1092 | result = PyTraceBack_Print(traceback, file); |
| 1093 | if (result < 0) |
| 1094 | return NULL; |
| 1095 | Py_RETURN_NONE; |
| 1096 | } |
| 1097 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1098 | /* To test the format of exceptions as printed out. */ |
| 1099 | static PyObject * |
| 1100 | exception_print(PyObject *self, PyObject *args) |
| 1101 | { |
| 1102 | PyObject *value; |
| 1103 | PyObject *tb; |
| 1104 | |
| 1105 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 1106 | &value)) |
| 1107 | return NULL; |
Benjamin Peterson | 6784eb7 | 2008-08-23 20:32:27 +0000 | [diff] [blame] | 1108 | if (!PyExceptionInstance_Check(value)) { |
| 1109 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 1110 | return NULL; |
| 1111 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1112 | |
| 1113 | tb = PyException_GetTraceback(value); |
| 1114 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 1115 | Py_XDECREF(tb); |
| 1116 | |
| 1117 | Py_RETURN_NONE; |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* reliably raise a MemoryError */ |
| 1124 | static PyObject * |
| 1125 | raise_memoryerror(PyObject *self) |
| 1126 | { |
| 1127 | PyErr_NoMemory(); |
| 1128 | return NULL; |
| 1129 | } |
| 1130 | |
| 1131 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1132 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1133 | {"raise_exception", raise_exception, METH_VARARGS}, |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 1134 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1135 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 1136 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 1137 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 1138 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1139 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1140 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1141 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1142 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1143 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1144 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1145 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 1146 | PyDoc_STR("This is a pretty normal docstring.")}, |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1147 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1148 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1149 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 1150 | METH_VARARGS|METH_KEYWORDS}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1151 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 1152 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 1153 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 1154 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 1155 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 1156 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 1157 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 1158 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1159 | #ifdef HAVE_LONG_LONG |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1160 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 1161 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 1162 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1163 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1164 | {"codec_incrementalencoder", |
| 1165 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 1166 | {"codec_incrementaldecoder", |
| 1167 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1168 | #endif |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1169 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1170 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1171 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1172 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1173 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1174 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1175 | #ifdef HAVE_GETTIMEOFDAY |
| 1176 | {"profile_int", profile_int, METH_NOARGS}, |
| 1177 | #endif |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1178 | {"traceback_print", traceback_print, METH_VARARGS}, |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1179 | {"exception_print", exception_print, METH_VARARGS}, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1180 | {NULL, NULL} /* sentinel */ |
| 1181 | }; |
| 1182 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1183 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 1184 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1185 | typedef struct { |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1186 | char bool_member; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1187 | char byte_member; |
| 1188 | unsigned char ubyte_member; |
| 1189 | short short_member; |
| 1190 | unsigned short ushort_member; |
| 1191 | int int_member; |
| 1192 | unsigned int uint_member; |
| 1193 | long long_member; |
| 1194 | unsigned long ulong_member; |
| 1195 | float float_member; |
| 1196 | double double_member; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1197 | #ifdef HAVE_LONG_LONG |
| 1198 | PY_LONG_LONG longlong_member; |
| 1199 | unsigned PY_LONG_LONG ulonglong_member; |
| 1200 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1201 | } all_structmembers; |
| 1202 | |
| 1203 | typedef struct { |
| 1204 | PyObject_HEAD |
| 1205 | all_structmembers structmembers; |
| 1206 | } test_structmembers; |
| 1207 | |
| 1208 | static struct PyMemberDef test_members[] = { |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1209 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1210 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 1211 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 1212 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 1213 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 1214 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 1215 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 1216 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 1217 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 1218 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 1219 | {"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] | 1220 | #ifdef HAVE_LONG_LONG |
| 1221 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 1222 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
| 1223 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1224 | {NULL} |
| 1225 | }; |
| 1226 | |
| 1227 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1228 | static PyObject * |
| 1229 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 1230 | { |
| 1231 | static char *keywords[] = { |
| 1232 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 1233 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", |
| 1234 | "T_FLOAT", "T_DOUBLE", |
| 1235 | #ifdef HAVE_LONG_LONG |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1236 | "T_LONGLONG", "T_ULONGLONG", |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1237 | #endif |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1238 | NULL}; |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1239 | static char *fmt = "|bbBhHiIlkfd" |
| 1240 | #ifdef HAVE_LONG_LONG |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1241 | "LK" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1242 | #endif |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1243 | ; |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1244 | test_structmembers *ob; |
| 1245 | ob = PyObject_New(test_structmembers, type); |
| 1246 | if (ob == NULL) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1247 | return NULL; |
| 1248 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1249 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1250 | &ob->structmembers.bool_member, |
| 1251 | &ob->structmembers.byte_member, |
| 1252 | &ob->structmembers.ubyte_member, |
| 1253 | &ob->structmembers.short_member, |
| 1254 | &ob->structmembers.ushort_member, |
| 1255 | &ob->structmembers.int_member, |
| 1256 | &ob->structmembers.uint_member, |
| 1257 | &ob->structmembers.long_member, |
| 1258 | &ob->structmembers.ulong_member, |
| 1259 | &ob->structmembers.float_member, |
| 1260 | &ob->structmembers.double_member |
| 1261 | #ifdef HAVE_LONG_LONG |
| 1262 | , &ob->structmembers.longlong_member, |
| 1263 | &ob->structmembers.ulonglong_member |
| 1264 | #endif |
| 1265 | )) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1266 | Py_DECREF(ob); |
| 1267 | return NULL; |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1268 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1269 | return (PyObject *)ob; |
| 1270 | } |
| 1271 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1272 | static void |
| 1273 | test_structmembers_free(PyObject *ob) |
| 1274 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1275 | PyObject_FREE(ob); |
| 1276 | } |
| 1277 | |
| 1278 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1279 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1280 | "test_structmembersType", |
| 1281 | sizeof(test_structmembers), /* tp_basicsize */ |
| 1282 | 0, /* tp_itemsize */ |
| 1283 | test_structmembers_free, /* destructor tp_dealloc */ |
| 1284 | 0, /* tp_print */ |
| 1285 | 0, /* tp_getattr */ |
| 1286 | 0, /* tp_setattr */ |
| 1287 | 0, /* tp_compare */ |
| 1288 | 0, /* tp_repr */ |
| 1289 | 0, /* tp_as_number */ |
| 1290 | 0, /* tp_as_sequence */ |
| 1291 | 0, /* tp_as_mapping */ |
| 1292 | 0, /* tp_hash */ |
| 1293 | 0, /* tp_call */ |
| 1294 | 0, /* tp_str */ |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1295 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1296 | PyObject_GenericSetAttr, /* tp_setattro */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1297 | 0, /* tp_as_buffer */ |
| 1298 | 0, /* tp_flags */ |
| 1299 | "Type containing all structmember types", |
| 1300 | 0, /* traverseproc tp_traverse */ |
| 1301 | 0, /* tp_clear */ |
| 1302 | 0, /* tp_richcompare */ |
| 1303 | 0, /* tp_weaklistoffset */ |
| 1304 | 0, /* tp_iter */ |
| 1305 | 0, /* tp_iternext */ |
| 1306 | 0, /* tp_methods */ |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1307 | test_members, /* tp_members */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1308 | 0, |
| 1309 | 0, |
| 1310 | 0, |
| 1311 | 0, |
| 1312 | 0, |
| 1313 | 0, |
| 1314 | 0, |
| 1315 | 0, |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1316 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1317 | }; |
| 1318 | |
| 1319 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1320 | |
| 1321 | static struct PyModuleDef _testcapimodule = { |
| 1322 | PyModuleDef_HEAD_INIT, |
| 1323 | "_testcapi", |
| 1324 | NULL, |
| 1325 | -1, |
| 1326 | TestMethods, |
| 1327 | NULL, |
| 1328 | NULL, |
| 1329 | NULL, |
| 1330 | NULL |
| 1331 | }; |
| 1332 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1333 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1334 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1335 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1336 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1337 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1338 | m = PyModule_Create(&_testcapimodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1339 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1340 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1341 | |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 1342 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
| 1343 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1344 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1345 | Py_INCREF(&test_structmembersType); |
| 1346 | PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType); |
| 1347 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1348 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 1349 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 1350 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 1351 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 1352 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 1353 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1354 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 1355 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1356 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1357 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 1358 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1359 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 1360 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 1361 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 1362 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 1363 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1364 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 1365 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 1366 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1367 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 1368 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1369 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 1370 | Py_INCREF(&PyInstanceMethod_Type); |
| 1371 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1372 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1373 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1374 | Py_INCREF(TestError); |
| 1375 | PyModule_AddObject(m, "error", TestError); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1376 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1377 | } |