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" |
Martin v. Löwis | 0347a9a | 2006-10-27 07:06:52 +0000 | [diff] [blame] | 9 | #include <float.h> |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 10 | #include "structmember.h" |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 11 | #include "datetime.h" |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 12 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 13 | #ifdef WITH_THREAD |
| 14 | #include "pythread.h" |
| 15 | #endif /* WITH_THREAD */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 16 | static PyObject *TestError; /* set to exception object in init */ |
| 17 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 18 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 19 | |
| 20 | static PyObject * |
| 21 | raiseTestError(const char* test_name, const char* msg) |
| 22 | { |
| 23 | char buf[2048]; |
| 24 | |
| 25 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 26 | PyErr_SetString(TestError, "internal error msg too large"); |
| 27 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 28 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 29 | PyErr_SetString(TestError, buf); |
| 30 | } |
| 31 | return NULL; |
| 32 | } |
| 33 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 34 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 35 | |
| 36 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 37 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 38 | platforms have these hardcoded. Better safe than sorry. |
| 39 | */ |
| 40 | static PyObject* |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 41 | sizeof_error(const char* fatname, const char* typname, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 42 | int expected, int got) |
| 43 | { |
| 44 | char buf[1024]; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 45 | PyOS_snprintf(buf, sizeof(buf), |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 46 | "%.200s #define == %d but sizeof(%.200s) == %d", |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 47 | fatname, expected, typname, got); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 48 | PyErr_SetString(TestError, buf); |
| 49 | return (PyObject*)NULL; |
| 50 | } |
| 51 | |
| 52 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 53 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 54 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 55 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
| 56 | if (FATNAME != sizeof(TYPE)) \ |
| 57 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
| 58 | |
Tim Peters | 208efe5 | 2001-06-26 22:40:47 +0000 | [diff] [blame] | 59 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 60 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 61 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 62 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 63 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
| 64 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 65 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | #undef CHECK_SIZEOF |
| 69 | |
| 70 | Py_INCREF(Py_None); |
| 71 | return Py_None; |
| 72 | } |
| 73 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 74 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 75 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 76 | { |
| 77 | PyObject* list; |
| 78 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 79 | |
| 80 | /* SF bug 132008: PyList_Reverse segfaults */ |
| 81 | #define NLIST 30 |
| 82 | list = PyList_New(NLIST); |
| 83 | if (list == (PyObject*)NULL) |
| 84 | return (PyObject*)NULL; |
| 85 | /* list = range(NLIST) */ |
| 86 | for (i = 0; i < NLIST; ++i) { |
| 87 | PyObject* anint = PyInt_FromLong(i); |
| 88 | if (anint == (PyObject*)NULL) { |
| 89 | Py_DECREF(list); |
| 90 | return (PyObject*)NULL; |
| 91 | } |
| 92 | PyList_SET_ITEM(list, i, anint); |
| 93 | } |
| 94 | /* list.reverse(), via PyList_Reverse() */ |
| 95 | i = PyList_Reverse(list); /* should not blow up! */ |
| 96 | if (i != 0) { |
| 97 | Py_DECREF(list); |
| 98 | return (PyObject*)NULL; |
| 99 | } |
| 100 | /* Check that list == range(29, -1, -1) now */ |
| 101 | for (i = 0; i < NLIST; ++i) { |
| 102 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 103 | if (PyInt_AS_LONG(anint) != NLIST-1-i) { |
| 104 | PyErr_SetString(TestError, |
| 105 | "test_list_api: reverse screwed up"); |
| 106 | Py_DECREF(list); |
| 107 | return (PyObject*)NULL; |
| 108 | } |
| 109 | } |
| 110 | Py_DECREF(list); |
| 111 | #undef NLIST |
| 112 | |
| 113 | Py_INCREF(Py_None); |
| 114 | return Py_None; |
| 115 | } |
| 116 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 117 | static int |
| 118 | test_dict_inner(int count) |
| 119 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 120 | Py_ssize_t pos = 0, iterations = 0; |
| 121 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 122 | PyObject *dict = PyDict_New(); |
| 123 | PyObject *v, *k; |
| 124 | |
| 125 | if (dict == NULL) |
| 126 | return -1; |
| 127 | |
| 128 | for (i = 0; i < count; i++) { |
| 129 | v = PyInt_FromLong(i); |
| 130 | PyDict_SetItem(dict, v, v); |
| 131 | Py_DECREF(v); |
| 132 | } |
| 133 | |
| 134 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 135 | PyObject *o; |
| 136 | iterations++; |
| 137 | |
| 138 | i = PyInt_AS_LONG(v) + 1; |
| 139 | o = PyInt_FromLong(i); |
| 140 | if (o == NULL) |
| 141 | return -1; |
| 142 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 143 | Py_DECREF(o); |
| 144 | return -1; |
| 145 | } |
| 146 | Py_DECREF(o); |
| 147 | } |
| 148 | |
| 149 | Py_DECREF(dict); |
| 150 | |
| 151 | if (iterations != count) { |
| 152 | PyErr_SetString( |
| 153 | TestError, |
| 154 | "test_dict_iteration: dict iteration went wrong "); |
| 155 | return -1; |
| 156 | } else { |
| 157 | return 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 162 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 163 | { |
| 164 | int i; |
| 165 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 166 | for (i = 0; i < 200; i++) { |
| 167 | if (test_dict_inner(i) < 0) { |
| 168 | return NULL; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | Py_INCREF(Py_None); |
| 173 | return Py_None; |
| 174 | } |
| 175 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 176 | |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 177 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 178 | * PyType_Ready if it hasn't already been called |
| 179 | */ |
| 180 | static PyTypeObject _HashInheritanceTester_Type = { |
Hirokazu Yamamoto | 52c1e3c | 2008-12-31 05:24:37 +0000 | [diff] [blame] | 181 | PyObject_HEAD_INIT(NULL) |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 182 | 0, /* Number of items for varobject */ |
| 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; |
Benjamin Peterson | a98c8e1 | 2009-05-05 21:09:21 +0000 | [diff] [blame] | 230 | |
Benjamin Peterson | c6c1f96 | 2009-05-05 23:00:48 +0000 | [diff] [blame] | 231 | if (type->tp_dict != NULL) |
| 232 | /* The type has already been initialized. This probably means |
| 233 | -R is being used. */ |
| 234 | Py_RETURN_NONE; |
Benjamin Peterson | a98c8e1 | 2009-05-05 21:09:21 +0000 | [diff] [blame] | 235 | |
| 236 | |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 237 | obj = PyObject_New(PyObject, type); |
| 238 | if (obj == NULL) { |
| 239 | PyErr_Clear(); |
| 240 | PyErr_SetString( |
| 241 | TestError, |
| 242 | "test_lazy_hash_inheritance: failed to create object"); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | if (type->tp_dict != NULL) { |
| 247 | PyErr_SetString( |
| 248 | TestError, |
| 249 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 250 | Py_DECREF(obj); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | hash = PyObject_Hash(obj); |
| 255 | if ((hash == -1) && PyErr_Occurred()) { |
| 256 | PyErr_Clear(); |
| 257 | PyErr_SetString( |
| 258 | TestError, |
| 259 | "test_lazy_hash_inheritance: could not hash object"); |
| 260 | Py_DECREF(obj); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | if (type->tp_dict == NULL) { |
| 265 | PyErr_SetString( |
| 266 | TestError, |
| 267 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 268 | Py_DECREF(obj); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 273 | PyErr_SetString( |
| 274 | TestError, |
| 275 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 276 | Py_DECREF(obj); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
Benjamin Peterson | c6c1f96 | 2009-05-05 23:00:48 +0000 | [diff] [blame] | 280 | Py_DECREF(obj); |
Benjamin Peterson | a98c8e1 | 2009-05-05 21:09:21 +0000 | [diff] [blame] | 281 | |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 282 | Py_RETURN_NONE; |
| 283 | } |
| 284 | |
| 285 | |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 286 | /* Issue #7385: Check that memoryview() does not crash |
| 287 | * when bf_getbuffer returns an error |
| 288 | */ |
| 289 | |
| 290 | static int |
| 291 | broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags) |
| 292 | { |
| 293 | PyErr_SetString( |
| 294 | TestError, |
| 295 | "test_broken_memoryview: expected error in bf_getbuffer"); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | static PyBufferProcs memoryviewtester_as_buffer = { |
| 300 | 0, /* bf_getreadbuffer */ |
| 301 | 0, /* bf_getwritebuffer */ |
| 302 | 0, /* bf_getsegcount */ |
| 303 | 0, /* bf_getcharbuffer */ |
| 304 | (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */ |
| 305 | 0, /* bf_releasebuffer */ |
| 306 | }; |
| 307 | |
| 308 | static PyTypeObject _MemoryViewTester_Type = { |
| 309 | PyObject_HEAD_INIT(NULL) |
| 310 | 0, /* Number of items for varobject */ |
| 311 | "memoryviewtester", /* Name of this type */ |
| 312 | sizeof(PyObject), /* Basic object size */ |
| 313 | 0, /* Item size for varobject */ |
| 314 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 315 | 0, /* tp_print */ |
| 316 | 0, /* tp_getattr */ |
| 317 | 0, /* tp_setattr */ |
| 318 | 0, /* tp_compare */ |
| 319 | 0, /* tp_repr */ |
| 320 | 0, /* tp_as_number */ |
| 321 | 0, /* tp_as_sequence */ |
| 322 | 0, /* tp_as_mapping */ |
| 323 | 0, /* tp_hash */ |
| 324 | 0, /* tp_call */ |
| 325 | 0, /* tp_str */ |
| 326 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 327 | 0, /* tp_setattro */ |
| 328 | &memoryviewtester_as_buffer, /* tp_as_buffer */ |
| 329 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */ |
| 330 | 0, /* tp_doc */ |
| 331 | 0, /* tp_traverse */ |
| 332 | 0, /* tp_clear */ |
| 333 | 0, /* tp_richcompare */ |
| 334 | 0, /* tp_weaklistoffset */ |
| 335 | 0, /* tp_iter */ |
| 336 | 0, /* tp_iternext */ |
| 337 | 0, /* tp_methods */ |
| 338 | 0, /* tp_members */ |
| 339 | 0, /* tp_getset */ |
| 340 | 0, /* tp_base */ |
| 341 | 0, /* tp_dict */ |
| 342 | 0, /* tp_descr_get */ |
| 343 | 0, /* tp_descr_set */ |
| 344 | 0, /* tp_dictoffset */ |
| 345 | 0, /* tp_init */ |
| 346 | 0, /* tp_alloc */ |
| 347 | PyType_GenericNew, /* tp_new */ |
| 348 | }; |
| 349 | |
| 350 | static PyObject* |
| 351 | test_broken_memoryview(PyObject* self) |
| 352 | { |
| 353 | PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type); |
| 354 | PyObject *res; |
| 355 | |
| 356 | if (obj == NULL) { |
| 357 | PyErr_Clear(); |
| 358 | PyErr_SetString( |
| 359 | TestError, |
| 360 | "test_broken_memoryview: failed to create object"); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | res = PyMemoryView_FromObject(obj); |
| 365 | if (res || !PyErr_Occurred()){ |
| 366 | PyErr_SetString( |
| 367 | TestError, |
| 368 | "test_broken_memoryview: memoryview() didn't raise an Exception"); |
| 369 | Py_XDECREF(res); |
| 370 | Py_DECREF(obj); |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | PyErr_Clear(); |
| 375 | Py_DECREF(obj); |
| 376 | Py_RETURN_NONE; |
| 377 | } |
| 378 | |
| 379 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 380 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 381 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 382 | |
| 383 | Note that the meat of the test is contained in testcapi_long.h. |
| 384 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 385 | 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] | 386 | dependence on type names makes it impossible to use a parameterized |
| 387 | function. A giant macro would be even worse than this. A C++ template |
| 388 | would be perfect. |
| 389 | |
| 390 | The "report an error" functions are deliberately not part of the #include |
| 391 | file: if the test fails, you can set a breakpoint in the appropriate |
| 392 | error function directly, and crawl back from there in the debugger. |
| 393 | */ |
| 394 | |
| 395 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 396 | |
| 397 | static PyObject * |
| 398 | raise_test_long_error(const char* msg) |
| 399 | { |
| 400 | return raiseTestError("test_long_api", msg); |
| 401 | } |
| 402 | |
| 403 | #define TESTNAME test_long_api_inner |
| 404 | #define TYPENAME long |
| 405 | #define F_S_TO_PY PyLong_FromLong |
| 406 | #define F_PY_TO_S PyLong_AsLong |
| 407 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 408 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 409 | |
| 410 | #include "testcapi_long.h" |
| 411 | |
| 412 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 413 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 414 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 415 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | #undef TESTNAME |
| 419 | #undef TYPENAME |
| 420 | #undef F_S_TO_PY |
| 421 | #undef F_PY_TO_S |
| 422 | #undef F_U_TO_PY |
| 423 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 424 | |
| 425 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 426 | |
| 427 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 428 | raise_test_longlong_error(const char* msg) |
| 429 | { |
| 430 | return raiseTestError("test_longlong_api", msg); |
| 431 | } |
| 432 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 433 | #define TESTNAME test_longlong_api_inner |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 434 | #define TYPENAME PY_LONG_LONG |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 435 | #define F_S_TO_PY PyLong_FromLongLong |
| 436 | #define F_PY_TO_S PyLong_AsLongLong |
| 437 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 438 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 439 | |
| 440 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 441 | |
| 442 | static PyObject * |
Skip Montanaro | 9582c14 | 2006-04-18 01:01:41 +0000 | [diff] [blame] | 443 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 444 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 445 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 448 | #undef TESTNAME |
| 449 | #undef TYPENAME |
| 450 | #undef F_S_TO_PY |
| 451 | #undef F_PY_TO_S |
| 452 | #undef F_U_TO_PY |
| 453 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 454 | |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 455 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 456 | is tested by test_long_api_inner. This test will concentrate on proper |
| 457 | handling of overflow. |
| 458 | */ |
| 459 | |
| 460 | static PyObject * |
| 461 | test_long_and_overflow(PyObject *self) |
| 462 | { |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 463 | PyObject *num, *one, *temp; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 464 | long value; |
| 465 | int overflow; |
| 466 | |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 467 | /* Test that overflow is set properly for a large value. */ |
| 468 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 469 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 470 | if (num == NULL) |
| 471 | return NULL; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 472 | overflow = 1234; |
| 473 | value = PyLong_AsLongAndOverflow(num, &overflow); |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 474 | Py_DECREF(num); |
| 475 | if (value == -1 && PyErr_Occurred()) |
| 476 | return NULL; |
| 477 | if (value != -1) |
| 478 | return raiseTestError("test_long_and_overflow", |
| 479 | "return value was not set to -1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 480 | if (overflow != 1) |
| 481 | return raiseTestError("test_long_and_overflow", |
| 482 | "overflow was not set to 1"); |
| 483 | |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 484 | /* Same again, with num = LONG_MAX + 1 */ |
| 485 | num = PyLong_FromLong(LONG_MAX); |
| 486 | if (num == NULL) |
| 487 | return NULL; |
| 488 | one = PyLong_FromLong(1L); |
| 489 | if (one == NULL) { |
| 490 | Py_DECREF(num); |
| 491 | return NULL; |
| 492 | } |
| 493 | temp = PyNumber_Add(num, one); |
| 494 | Py_DECREF(one); |
| 495 | Py_DECREF(num); |
| 496 | num = temp; |
| 497 | if (num == NULL) |
| 498 | return NULL; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 499 | overflow = 0; |
| 500 | value = PyLong_AsLongAndOverflow(num, &overflow); |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 501 | Py_DECREF(num); |
| 502 | if (value == -1 && PyErr_Occurred()) |
| 503 | return NULL; |
| 504 | if (value != -1) |
| 505 | return raiseTestError("test_long_and_overflow", |
| 506 | "return value was not set to -1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 507 | if (overflow != 1) |
| 508 | return raiseTestError("test_long_and_overflow", |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 509 | "overflow was not set to 1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 510 | |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 511 | /* Test that overflow is set properly for a large negative value. */ |
| 512 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 513 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 514 | if (num == NULL) |
| 515 | return NULL; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 516 | overflow = 1234; |
| 517 | value = PyLong_AsLongAndOverflow(num, &overflow); |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 518 | Py_DECREF(num); |
| 519 | if (value == -1 && PyErr_Occurred()) |
| 520 | return NULL; |
| 521 | if (value != -1) |
| 522 | return raiseTestError("test_long_and_overflow", |
| 523 | "return value was not set to -1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 524 | if (overflow != -1) |
| 525 | return raiseTestError("test_long_and_overflow", |
| 526 | "overflow was not set to -1"); |
| 527 | |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 528 | /* Same again, with num = LONG_MIN - 1 */ |
| 529 | num = PyLong_FromLong(LONG_MIN); |
| 530 | if (num == NULL) |
| 531 | return NULL; |
| 532 | one = PyLong_FromLong(1L); |
| 533 | if (one == NULL) { |
| 534 | Py_DECREF(num); |
| 535 | return NULL; |
| 536 | } |
| 537 | temp = PyNumber_Subtract(num, one); |
| 538 | Py_DECREF(one); |
| 539 | Py_DECREF(num); |
| 540 | num = temp; |
| 541 | if (num == NULL) |
| 542 | return NULL; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 543 | overflow = 0; |
| 544 | value = PyLong_AsLongAndOverflow(num, &overflow); |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 545 | Py_DECREF(num); |
| 546 | if (value == -1 && PyErr_Occurred()) |
| 547 | return NULL; |
| 548 | if (value != -1) |
| 549 | return raiseTestError("test_long_and_overflow", |
| 550 | "return value was not set to -1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 551 | if (overflow != -1) |
| 552 | return raiseTestError("test_long_and_overflow", |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 553 | "overflow was not set to -1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 554 | |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 555 | /* Test that overflow is cleared properly for small values. */ |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 556 | num = PyLong_FromString("FF", NULL, 16); |
| 557 | if (num == NULL) |
| 558 | return NULL; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 559 | overflow = 1234; |
| 560 | value = PyLong_AsLongAndOverflow(num, &overflow); |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 561 | Py_DECREF(num); |
| 562 | if (value == -1 && PyErr_Occurred()) |
| 563 | return NULL; |
| 564 | if (value != 0xFF) |
| 565 | return raiseTestError("test_long_and_overflow", |
| 566 | "expected return value 0xFF"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 567 | if (overflow != 0) |
| 568 | return raiseTestError("test_long_and_overflow", |
| 569 | "overflow was not cleared"); |
| 570 | |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 571 | num = PyLong_FromString("-FF", NULL, 16); |
| 572 | if (num == NULL) |
| 573 | return NULL; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 574 | overflow = 0; |
| 575 | value = PyLong_AsLongAndOverflow(num, &overflow); |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 576 | Py_DECREF(num); |
| 577 | if (value == -1 && PyErr_Occurred()) |
| 578 | return NULL; |
| 579 | if (value != -0xFF) |
| 580 | return raiseTestError("test_long_and_overflow", |
| 581 | "expected return value 0xFF"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 582 | if (overflow != 0) |
| 583 | return raiseTestError("test_long_and_overflow", |
| 584 | "overflow was set incorrectly"); |
| 585 | |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 586 | num = PyLong_FromLong(LONG_MAX); |
| 587 | if (num == NULL) |
| 588 | return NULL; |
| 589 | overflow = 1234; |
| 590 | value = PyLong_AsLongAndOverflow(num, &overflow); |
Mark Dickinson | ed02b3f | 2009-12-21 11:31:54 +0000 | [diff] [blame] | 591 | Py_DECREF(num); |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 592 | if (value == -1 && PyErr_Occurred()) |
| 593 | return NULL; |
| 594 | if (value != LONG_MAX) |
| 595 | return raiseTestError("test_long_and_overflow", |
| 596 | "expected return value LONG_MAX"); |
| 597 | if (overflow != 0) |
| 598 | return raiseTestError("test_long_and_overflow", |
| 599 | "overflow was not cleared"); |
| 600 | |
| 601 | num = PyLong_FromLong(LONG_MIN); |
| 602 | if (num == NULL) |
| 603 | return NULL; |
| 604 | overflow = 0; |
| 605 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 606 | Py_DECREF(num); |
| 607 | if (value == -1 && PyErr_Occurred()) |
| 608 | return NULL; |
| 609 | if (value != LONG_MIN) |
| 610 | return raiseTestError("test_long_and_overflow", |
| 611 | "expected return value LONG_MIN"); |
| 612 | if (overflow != 0) |
| 613 | return raiseTestError("test_long_and_overflow", |
| 614 | "overflow was not cleared"); |
Mark Dickinson | ed02b3f | 2009-12-21 11:31:54 +0000 | [diff] [blame] | 615 | |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 616 | Py_INCREF(Py_None); |
| 617 | return Py_None; |
| 618 | } |
| 619 | |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 620 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
| 621 | PY_LONG_LONG is tested by test_long_api_inner. This test will |
| 622 | concentrate on proper handling of overflow. |
| 623 | */ |
| 624 | |
| 625 | static PyObject * |
| 626 | test_long_long_and_overflow(PyObject *self) |
| 627 | { |
| 628 | PyObject *num, *one, *temp; |
| 629 | PY_LONG_LONG value; |
| 630 | int overflow; |
| 631 | |
| 632 | /* Test that overflow is set properly for a large value. */ |
| 633 | /* num is a number larger than PY_LLONG_MAX on a typical machine. */ |
| 634 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 635 | if (num == NULL) |
| 636 | return NULL; |
| 637 | overflow = 1234; |
| 638 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 639 | Py_DECREF(num); |
| 640 | if (value == -1 && PyErr_Occurred()) |
| 641 | return NULL; |
| 642 | if (value != -1) |
| 643 | return raiseTestError("test_long_long_and_overflow", |
| 644 | "return value was not set to -1"); |
| 645 | if (overflow != 1) |
| 646 | return raiseTestError("test_long_long_and_overflow", |
| 647 | "overflow was not set to 1"); |
| 648 | |
| 649 | /* Same again, with num = PY_LLONG_MAX + 1 */ |
| 650 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 651 | if (num == NULL) |
| 652 | return NULL; |
| 653 | one = PyLong_FromLong(1L); |
| 654 | if (one == NULL) { |
| 655 | Py_DECREF(num); |
| 656 | return NULL; |
| 657 | } |
| 658 | temp = PyNumber_Add(num, one); |
| 659 | Py_DECREF(one); |
| 660 | Py_DECREF(num); |
| 661 | num = temp; |
| 662 | if (num == NULL) |
| 663 | return NULL; |
| 664 | overflow = 0; |
| 665 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 666 | Py_DECREF(num); |
| 667 | if (value == -1 && PyErr_Occurred()) |
| 668 | return NULL; |
| 669 | if (value != -1) |
| 670 | return raiseTestError("test_long_long_and_overflow", |
| 671 | "return value was not set to -1"); |
| 672 | if (overflow != 1) |
| 673 | return raiseTestError("test_long_long_and_overflow", |
| 674 | "overflow was not set to 1"); |
| 675 | |
| 676 | /* Test that overflow is set properly for a large negative value. */ |
| 677 | /* num is a number smaller than PY_LLONG_MIN on a typical platform */ |
| 678 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 679 | if (num == NULL) |
| 680 | return NULL; |
| 681 | overflow = 1234; |
| 682 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 683 | Py_DECREF(num); |
| 684 | if (value == -1 && PyErr_Occurred()) |
| 685 | return NULL; |
| 686 | if (value != -1) |
| 687 | return raiseTestError("test_long_long_and_overflow", |
| 688 | "return value was not set to -1"); |
| 689 | if (overflow != -1) |
| 690 | return raiseTestError("test_long_long_and_overflow", |
| 691 | "overflow was not set to -1"); |
| 692 | |
| 693 | /* Same again, with num = PY_LLONG_MIN - 1 */ |
| 694 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 695 | if (num == NULL) |
| 696 | return NULL; |
| 697 | one = PyLong_FromLong(1L); |
| 698 | if (one == NULL) { |
| 699 | Py_DECREF(num); |
| 700 | return NULL; |
| 701 | } |
| 702 | temp = PyNumber_Subtract(num, one); |
| 703 | Py_DECREF(one); |
| 704 | Py_DECREF(num); |
| 705 | num = temp; |
| 706 | if (num == NULL) |
| 707 | return NULL; |
| 708 | overflow = 0; |
| 709 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 710 | Py_DECREF(num); |
| 711 | if (value == -1 && PyErr_Occurred()) |
| 712 | return NULL; |
| 713 | if (value != -1) |
| 714 | return raiseTestError("test_long_long_and_overflow", |
| 715 | "return value was not set to -1"); |
| 716 | if (overflow != -1) |
| 717 | return raiseTestError("test_long_long_and_overflow", |
| 718 | "overflow was not set to -1"); |
| 719 | |
| 720 | /* Test that overflow is cleared properly for small values. */ |
| 721 | num = PyLong_FromString("FF", NULL, 16); |
| 722 | if (num == NULL) |
| 723 | return NULL; |
| 724 | overflow = 1234; |
| 725 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 726 | Py_DECREF(num); |
| 727 | if (value == -1 && PyErr_Occurred()) |
| 728 | return NULL; |
| 729 | if (value != 0xFF) |
| 730 | return raiseTestError("test_long_long_and_overflow", |
| 731 | "expected return value 0xFF"); |
| 732 | if (overflow != 0) |
| 733 | return raiseTestError("test_long_long_and_overflow", |
| 734 | "overflow was not cleared"); |
| 735 | |
| 736 | num = PyLong_FromString("-FF", NULL, 16); |
| 737 | if (num == NULL) |
| 738 | return NULL; |
| 739 | overflow = 0; |
| 740 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 741 | Py_DECREF(num); |
| 742 | if (value == -1 && PyErr_Occurred()) |
| 743 | return NULL; |
| 744 | if (value != -0xFF) |
| 745 | return raiseTestError("test_long_long_and_overflow", |
| 746 | "expected return value 0xFF"); |
| 747 | if (overflow != 0) |
| 748 | return raiseTestError("test_long_long_and_overflow", |
| 749 | "overflow was set incorrectly"); |
| 750 | |
| 751 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 752 | if (num == NULL) |
| 753 | return NULL; |
| 754 | overflow = 1234; |
| 755 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 756 | Py_DECREF(num); |
| 757 | if (value == -1 && PyErr_Occurred()) |
| 758 | return NULL; |
| 759 | if (value != PY_LLONG_MAX) |
| 760 | return raiseTestError("test_long_long_and_overflow", |
| 761 | "expected return value PY_LLONG_MAX"); |
| 762 | if (overflow != 0) |
| 763 | return raiseTestError("test_long_long_and_overflow", |
| 764 | "overflow was not cleared"); |
| 765 | |
| 766 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 767 | if (num == NULL) |
| 768 | return NULL; |
| 769 | overflow = 0; |
| 770 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 771 | Py_DECREF(num); |
| 772 | if (value == -1 && PyErr_Occurred()) |
| 773 | return NULL; |
| 774 | if (value != PY_LLONG_MIN) |
| 775 | return raiseTestError("test_long_long_and_overflow", |
| 776 | "expected return value PY_LLONG_MIN"); |
| 777 | if (overflow != 0) |
| 778 | return raiseTestError("test_long_long_and_overflow", |
| 779 | "overflow was not cleared"); |
| 780 | |
| 781 | Py_INCREF(Py_None); |
| 782 | return Py_None; |
| 783 | } |
| 784 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 785 | /* 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] | 786 | for both long and int arguments. The test may leak a little memory if |
| 787 | it fails. |
| 788 | */ |
| 789 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 790 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 791 | { |
| 792 | PyObject *tuple, *num; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 793 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 794 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 795 | tuple = PyTuple_New(1); |
| 796 | if (tuple == NULL) |
| 797 | return NULL; |
| 798 | |
| 799 | num = PyLong_FromLong(42); |
| 800 | if (num == NULL) |
| 801 | return NULL; |
| 802 | |
| 803 | PyTuple_SET_ITEM(tuple, 0, num); |
| 804 | |
| 805 | value = -1; |
| 806 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 807 | return NULL; |
| 808 | if (value != 42) |
| 809 | return raiseTestError("test_L_code", |
| 810 | "L code returned wrong value for long 42"); |
| 811 | |
| 812 | Py_DECREF(num); |
| 813 | num = PyInt_FromLong(42); |
| 814 | if (num == NULL) |
| 815 | return NULL; |
| 816 | |
| 817 | PyTuple_SET_ITEM(tuple, 0, num); |
| 818 | |
| 819 | value = -1; |
| 820 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 821 | return NULL; |
| 822 | if (value != 42) |
| 823 | return raiseTestError("test_L_code", |
| 824 | "L code returned wrong value for int 42"); |
| 825 | |
| 826 | Py_DECREF(tuple); |
| 827 | Py_INCREF(Py_None); |
| 828 | return Py_None; |
| 829 | } |
| 830 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 831 | #endif /* ifdef HAVE_LONG_LONG */ |
| 832 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 833 | /* Test tuple argument processing */ |
| 834 | static PyObject * |
| 835 | getargs_tuple(PyObject *self, PyObject *args) |
| 836 | { |
| 837 | int a, b, c; |
| 838 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 839 | return NULL; |
| 840 | return Py_BuildValue("iii", a, b, c); |
| 841 | } |
| 842 | |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 843 | /* test PyArg_ParseTupleAndKeywords */ |
| 844 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 845 | { |
| 846 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 847 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 848 | int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 849 | |
| 850 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 851 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 852 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 853 | return NULL; |
| 854 | return Py_BuildValue("iiiiiiiiii", |
| 855 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 856 | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
| 857 | } |
| 858 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 859 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 860 | and return the result. |
| 861 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 862 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 863 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 864 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 865 | unsigned char value; |
| 866 | if (!PyArg_ParseTuple(args, "b", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 867 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 868 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 871 | static PyObject * |
| 872 | getargs_B(PyObject *self, PyObject *args) |
| 873 | { |
| 874 | unsigned char value; |
| 875 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 876 | return NULL; |
| 877 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 878 | } |
| 879 | |
| 880 | static PyObject * |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 881 | getargs_h(PyObject *self, PyObject *args) |
| 882 | { |
| 883 | short value; |
| 884 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 885 | return NULL; |
| 886 | return PyLong_FromLong((long)value); |
| 887 | } |
| 888 | |
| 889 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 890 | getargs_H(PyObject *self, PyObject *args) |
| 891 | { |
| 892 | unsigned short value; |
| 893 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 894 | return NULL; |
| 895 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 896 | } |
| 897 | |
| 898 | static PyObject * |
| 899 | getargs_I(PyObject *self, PyObject *args) |
| 900 | { |
| 901 | unsigned int value; |
| 902 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 903 | return NULL; |
| 904 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 905 | } |
| 906 | |
| 907 | static PyObject * |
| 908 | getargs_k(PyObject *self, PyObject *args) |
| 909 | { |
| 910 | unsigned long value; |
| 911 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 912 | return NULL; |
| 913 | return PyLong_FromUnsignedLong(value); |
| 914 | } |
| 915 | |
| 916 | static PyObject * |
| 917 | getargs_i(PyObject *self, PyObject *args) |
| 918 | { |
| 919 | int value; |
| 920 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 921 | return NULL; |
| 922 | return PyLong_FromLong((long)value); |
| 923 | } |
| 924 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 925 | static PyObject * |
| 926 | getargs_l(PyObject *self, PyObject *args) |
| 927 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 928 | long value; |
| 929 | if (!PyArg_ParseTuple(args, "l", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 930 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 931 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 934 | static PyObject * |
| 935 | getargs_n(PyObject *self, PyObject *args) |
| 936 | { |
| 937 | Py_ssize_t value; |
| 938 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 939 | return NULL; |
| 940 | return PyInt_FromSsize_t(value); |
| 941 | } |
| 942 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 943 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 944 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 945 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 946 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 947 | PY_LONG_LONG value; |
| 948 | if (!PyArg_ParseTuple(args, "L", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 949 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 950 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 953 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 954 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 955 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 956 | unsigned PY_LONG_LONG value; |
| 957 | if (!PyArg_ParseTuple(args, "K", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 958 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 959 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 960 | } |
| 961 | #endif |
| 962 | |
| 963 | /* This function not only tests the 'k' getargs code, but also the |
| 964 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ |
| 965 | static PyObject * |
| 966 | test_k_code(PyObject *self) |
| 967 | { |
| 968 | PyObject *tuple, *num; |
| 969 | unsigned long value; |
| 970 | |
| 971 | tuple = PyTuple_New(1); |
| 972 | if (tuple == NULL) |
| 973 | return NULL; |
| 974 | |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 975 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 976 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 977 | if (num == NULL) |
| 978 | return NULL; |
| 979 | |
| 980 | value = PyInt_AsUnsignedLongMask(num); |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 981 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 982 | return raiseTestError("test_k_code", |
| 983 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 984 | |
| 985 | PyTuple_SET_ITEM(tuple, 0, num); |
| 986 | |
Neal Norwitz | b183a25 | 2006-04-10 01:03:32 +0000 | [diff] [blame] | 987 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 988 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 989 | return NULL; |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 990 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 991 | return raiseTestError("test_k_code", |
| 992 | "k code returned wrong value for long 0xFFF...FFF"); |
| 993 | |
| 994 | Py_DECREF(num); |
| 995 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 996 | if (num == NULL) |
| 997 | return NULL; |
| 998 | |
| 999 | value = PyInt_AsUnsignedLongMask(num); |
| 1000 | if (value != (unsigned long)-0x42) |
| 1001 | return raiseTestError("test_k_code", |
| 1002 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 1003 | |
| 1004 | PyTuple_SET_ITEM(tuple, 0, num); |
| 1005 | |
Neal Norwitz | b183a25 | 2006-04-10 01:03:32 +0000 | [diff] [blame] | 1006 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1007 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 1008 | return NULL; |
| 1009 | if (value != (unsigned long)-0x42) |
| 1010 | return raiseTestError("test_k_code", |
| 1011 | "k code returned wrong value for long -0xFFF..000042"); |
| 1012 | |
| 1013 | Py_DECREF(tuple); |
| 1014 | Py_INCREF(Py_None); |
| 1015 | return Py_None; |
| 1016 | } |
| 1017 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1018 | #ifdef Py_USING_UNICODE |
| 1019 | |
Benjamin Peterson | e098c4a | 2008-12-23 20:12:33 +0000 | [diff] [blame] | 1020 | static volatile int x; |
Benjamin Peterson | b6a53b5 | 2008-12-23 20:09:28 +0000 | [diff] [blame] | 1021 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1022 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1023 | of an error. |
| 1024 | */ |
| 1025 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1026 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1027 | { |
| 1028 | PyObject *tuple, *obj; |
| 1029 | Py_UNICODE *value; |
| 1030 | int len; |
| 1031 | |
Amaury Forgeot d'Arc | 07d539d | 2008-10-14 21:47:22 +0000 | [diff] [blame] | 1032 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1033 | /* Just use the macro and check that it compiles */ |
Benjamin Peterson | b6a53b5 | 2008-12-23 20:09:28 +0000 | [diff] [blame] | 1034 | x = Py_UNICODE_ISSPACE(25); |
Amaury Forgeot d'Arc | 07d539d | 2008-10-14 21:47:22 +0000 | [diff] [blame] | 1035 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1036 | tuple = PyTuple_New(1); |
| 1037 | if (tuple == NULL) |
| 1038 | return NULL; |
| 1039 | |
| 1040 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1041 | "ascii", NULL); |
| 1042 | if (obj == NULL) |
| 1043 | return NULL; |
| 1044 | |
| 1045 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1046 | |
| 1047 | value = 0; |
| 1048 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 1049 | return NULL; |
| 1050 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1051 | return raiseTestError("test_u_code", |
| 1052 | "u code returned wrong value for u'test'"); |
| 1053 | value = 0; |
| 1054 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 1055 | return NULL; |
| 1056 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1057 | len != PyUnicode_GET_SIZE(obj)) |
| 1058 | return raiseTestError("test_u_code", |
| 1059 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1060 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1061 | Py_DECREF(tuple); |
| 1062 | Py_INCREF(Py_None); |
| 1063 | return Py_None; |
| 1064 | } |
| 1065 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1066 | static PyObject * |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1067 | test_widechar(PyObject *self) |
| 1068 | { |
| 1069 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1070 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1071 | size_t wtextlen = 1; |
| 1072 | #else |
| 1073 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1074 | size_t wtextlen = 2; |
| 1075 | #endif |
| 1076 | PyObject *wide, *utf8; |
| 1077 | |
| 1078 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1079 | if (wide == NULL) |
| 1080 | return NULL; |
| 1081 | |
| 1082 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1083 | if (utf8 == NULL) { |
| 1084 | Py_DECREF(wide); |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | |
| 1088 | if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) { |
| 1089 | Py_DECREF(wide); |
| 1090 | Py_DECREF(utf8); |
| 1091 | return raiseTestError("test_widechar", |
| 1092 | "wide string and utf8 string have different length"); |
| 1093 | } |
| 1094 | if (PyUnicode_Compare(wide, utf8)) { |
| 1095 | Py_DECREF(wide); |
| 1096 | Py_DECREF(utf8); |
| 1097 | if (PyErr_Occurred()) |
| 1098 | return NULL; |
| 1099 | return raiseTestError("test_widechar", |
| 1100 | "wide string and utf8 string are differents"); |
| 1101 | } |
| 1102 | |
| 1103 | Py_DECREF(wide); |
| 1104 | Py_DECREF(utf8); |
| 1105 | Py_RETURN_NONE; |
| 1106 | } |
| 1107 | |
| 1108 | static PyObject * |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 1109 | test_empty_argparse(PyObject *self) |
| 1110 | { |
| 1111 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 1112 | PyObject *tuple, *dict = NULL; |
| 1113 | static char *kwlist[] = {NULL}; |
| 1114 | int result; |
| 1115 | tuple = PyTuple_New(0); |
| 1116 | if (!tuple) |
| 1117 | return NULL; |
| 1118 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 1119 | goto done; |
| 1120 | dict = PyDict_New(); |
| 1121 | if (!dict) |
| 1122 | goto done; |
| 1123 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
| 1124 | done: |
| 1125 | Py_DECREF(tuple); |
| 1126 | Py_XDECREF(dict); |
| 1127 | if (result < 0) |
| 1128 | return NULL; |
| 1129 | else { |
| 1130 | Py_RETURN_NONE; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | static PyObject * |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1135 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1136 | { |
| 1137 | const char *encoding, *errors = NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1138 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1139 | &encoding, &errors)) |
| 1140 | return NULL; |
| 1141 | return PyCodec_IncrementalEncoder(encoding, errors); |
| 1142 | } |
| 1143 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1144 | static PyObject * |
| 1145 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1146 | { |
| 1147 | const char *encoding, *errors = NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1148 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1149 | &encoding, &errors)) |
| 1150 | return NULL; |
| 1151 | return PyCodec_IncrementalDecoder(encoding, errors); |
| 1152 | } |
| 1153 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1154 | #endif |
| 1155 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1156 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1157 | static PyObject * |
| 1158 | test_long_numbits(PyObject *self) |
| 1159 | { |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1160 | struct triple { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1161 | long input; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1162 | size_t nbits; |
| 1163 | int sign; |
| 1164 | } testcases[] = {{0, 0, 0}, |
| 1165 | {1L, 1, 1}, |
| 1166 | {-1L, 1, -1}, |
| 1167 | {2L, 2, 1}, |
| 1168 | {-2L, 2, -1}, |
| 1169 | {3L, 2, 1}, |
| 1170 | {-3L, 2, -1}, |
| 1171 | {4L, 3, 1}, |
| 1172 | {-4L, 3, -1}, |
| 1173 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 1174 | {-0x7fffL, 15, -1}, |
| 1175 | {0xffffL, 16, 1}, |
| 1176 | {-0xffffL, 16, -1}, |
| 1177 | {0xfffffffL, 28, 1}, |
| 1178 | {-0xfffffffL, 28, -1}}; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1179 | int i; |
| 1180 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1181 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 1182 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1183 | size_t nbits = _PyLong_NumBits(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1184 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1185 | |
| 1186 | Py_DECREF(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1187 | if (nbits != testcases[i].nbits) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1188 | return raiseTestError("test_long_numbits", |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1189 | "wrong result for _PyLong_NumBits"); |
| 1190 | if (sign != testcases[i].sign) |
| 1191 | return raiseTestError("test_long_numbits", |
| 1192 | "wrong result for _PyLong_Sign"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1193 | } |
| 1194 | Py_INCREF(Py_None); |
| 1195 | return Py_None; |
| 1196 | } |
| 1197 | |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 1198 | /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */ |
| 1199 | |
| 1200 | static PyObject * |
| 1201 | test_null_strings(PyObject *self) |
| 1202 | { |
| 1203 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL); |
| 1204 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 1205 | Py_XDECREF(o1); |
| 1206 | Py_XDECREF(o2); |
| 1207 | return tuple; |
| 1208 | } |
| 1209 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1210 | static PyObject * |
| 1211 | raise_exception(PyObject *self, PyObject *args) |
| 1212 | { |
| 1213 | PyObject *exc; |
| 1214 | PyObject *exc_args, *v; |
| 1215 | int num_args, i; |
| 1216 | |
| 1217 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1218 | &exc, &num_args)) |
| 1219 | return NULL; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1220 | if (!PyExceptionClass_Check(exc)) { |
| 1221 | PyErr_Format(PyExc_TypeError, "an exception class is required"); |
| 1222 | return NULL; |
| 1223 | } |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1224 | |
| 1225 | exc_args = PyTuple_New(num_args); |
| 1226 | if (exc_args == NULL) |
| 1227 | return NULL; |
| 1228 | for (i = 0; i < num_args; ++i) { |
| 1229 | v = PyInt_FromLong(i); |
| 1230 | if (v == NULL) { |
| 1231 | Py_DECREF(exc_args); |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | PyTuple_SET_ITEM(exc_args, i, v); |
| 1235 | } |
| 1236 | PyErr_SetObject(exc, exc_args); |
Michael W. Hudson | f02bcee | 2003-08-15 13:03:30 +0000 | [diff] [blame] | 1237 | Py_DECREF(exc_args); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1238 | return NULL; |
| 1239 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1240 | |
Benjamin Peterson | 46bff79 | 2010-01-30 23:28:38 +0000 | [diff] [blame] | 1241 | |
| 1242 | static int test_run_counter = 0; |
| 1243 | |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 1244 | static PyObject * |
| 1245 | test_datetime_capi(PyObject *self, PyObject *args) { |
| 1246 | if (PyDateTimeAPI) { |
Benjamin Peterson | 46bff79 | 2010-01-30 23:28:38 +0000 | [diff] [blame] | 1247 | if (test_run_counter) { |
| 1248 | /* Probably regrtest.py -R */ |
| 1249 | Py_RETURN_NONE; |
| 1250 | } |
| 1251 | else { |
| 1252 | PyErr_SetString(PyExc_AssertionError, |
| 1253 | "PyDateTime_CAPI somehow initialized"); |
| 1254 | return NULL; |
| 1255 | } |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 1256 | } |
Benjamin Peterson | 46bff79 | 2010-01-30 23:28:38 +0000 | [diff] [blame] | 1257 | test_run_counter++; |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 1258 | PyDateTime_IMPORT; |
| 1259 | if (PyDateTimeAPI) |
| 1260 | Py_RETURN_NONE; |
| 1261 | else |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | |
Benjamin Peterson | a04ae01 | 2010-01-30 23:26:05 +0000 | [diff] [blame] | 1265 | |
| 1266 | #ifdef WITH_THREAD |
| 1267 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1268 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 1269 | * `thread_done` when it's finished. The driver code has to know when the |
| 1270 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 1271 | * may go away when the driver finishes. The former lack of this explicit |
| 1272 | * synchronization caused rare segfaults, so rare that they were seen only |
| 1273 | * on a Mac buildbot (although they were possible on any box). |
| 1274 | */ |
| 1275 | static PyThread_type_lock thread_done = NULL; |
| 1276 | |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1277 | static int |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1278 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1279 | { |
| 1280 | PyObject *rc; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1281 | int success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1282 | PyGILState_STATE s = PyGILState_Ensure(); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1283 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1284 | success = (rc != NULL); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1285 | Py_XDECREF(rc); |
| 1286 | PyGILState_Release(s); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1287 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1290 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 1291 | * should be called only from threads spawned by test_thread_state(). |
| 1292 | */ |
| 1293 | static void |
| 1294 | _make_call_from_thread(void *callable) |
| 1295 | { |
| 1296 | _make_call(callable); |
| 1297 | PyThread_release_lock(thread_done); |
| 1298 | } |
| 1299 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1300 | static PyObject * |
| 1301 | test_thread_state(PyObject *self, PyObject *args) |
| 1302 | { |
| 1303 | PyObject *fn; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1304 | int success = 1; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1305 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1306 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 1307 | return NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1308 | |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1309 | if (!PyCallable_Check(fn)) { |
| 1310 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1311 | fn->ob_type->tp_name); |
| 1312 | return NULL; |
| 1313 | } |
| 1314 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1315 | /* Ensure Python is set up for threading */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1316 | PyEval_InitThreads(); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1317 | thread_done = PyThread_allocate_lock(); |
| 1318 | if (thread_done == NULL) |
| 1319 | return PyErr_NoMemory(); |
| 1320 | PyThread_acquire_lock(thread_done, 1); |
| 1321 | |
| 1322 | /* Start a new thread with our callback. */ |
| 1323 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1324 | /* Make the callback with the thread lock held by this thread */ |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1325 | success &= _make_call(fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1326 | /* Do it all again, but this time with the thread-lock released */ |
| 1327 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1328 | success &= _make_call(fn); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1329 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1330 | Py_END_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1331 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1332 | /* And once more with and without a thread |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1333 | XXX - should use a lock and work out exactly what we are trying |
| 1334 | to test <wink> |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1335 | */ |
| 1336 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1337 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1338 | success &= _make_call(fn); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1339 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1340 | Py_END_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1341 | |
Neal Norwitz | 97a5722 | 2006-10-28 21:17:51 +0000 | [diff] [blame] | 1342 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 1343 | PyThread_release_lock(thread_done); |
| 1344 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1345 | PyThread_free_lock(thread_done); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1346 | if (!success) |
| 1347 | return NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1348 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1349 | } |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1350 | |
| 1351 | /* test Py_AddPendingCalls using threads */ |
| 1352 | static int _pending_callback(void *arg) |
| 1353 | { |
| 1354 | /* we assume the argument is callable object to which we own a reference */ |
| 1355 | PyObject *callable = (PyObject *)arg; |
| 1356 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 1357 | Py_DECREF(callable); |
| 1358 | Py_XDECREF(r); |
| 1359 | return r != NULL ? 0 : -1; |
| 1360 | } |
| 1361 | |
| 1362 | /* The following requests n callbacks to _pending_callback. It can be |
| 1363 | * run from any python thread. |
| 1364 | */ |
| 1365 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1366 | { |
| 1367 | PyObject *callable; |
| 1368 | int r; |
| 1369 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1370 | return NULL; |
| 1371 | |
| 1372 | /* create the reference for the callbackwhile we hold the lock */ |
| 1373 | Py_INCREF(callable); |
| 1374 | |
| 1375 | Py_BEGIN_ALLOW_THREADS |
| 1376 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1377 | Py_END_ALLOW_THREADS |
| 1378 | |
| 1379 | if (r<0) { |
| 1380 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1381 | Py_INCREF(Py_False); |
| 1382 | return Py_False; |
| 1383 | } |
| 1384 | Py_INCREF(Py_True); |
| 1385 | return Py_True; |
| 1386 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1387 | #endif |
| 1388 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1389 | /* Some tests of PyString_FromFormat(). This needs more tests. */ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1390 | static PyObject * |
| 1391 | test_string_from_format(PyObject *self, PyObject *args) |
| 1392 | { |
| 1393 | PyObject *result; |
| 1394 | char *msg; |
| 1395 | |
| 1396 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1397 | result = PyString_FromFormat(FORMAT, (TYPE)1); \ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1398 | if (result == NULL) \ |
| 1399 | return NULL; \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1400 | if (strcmp(PyString_AsString(result), "1")) { \ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1401 | msg = FORMAT " failed at 1"; \ |
| 1402 | goto Fail; \ |
| 1403 | } \ |
| 1404 | Py_DECREF(result) |
| 1405 | |
| 1406 | CHECK_1_FORMAT("%d", int); |
| 1407 | CHECK_1_FORMAT("%ld", long); |
| 1408 | /* The z width modifier was added in Python 2.5. */ |
| 1409 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
| 1410 | |
| 1411 | /* The u type code was added in Python 2.5. */ |
| 1412 | CHECK_1_FORMAT("%u", unsigned int); |
| 1413 | CHECK_1_FORMAT("%lu", unsigned long); |
| 1414 | CHECK_1_FORMAT("%zu", size_t); |
| 1415 | |
Mark Dickinson | 82864d1 | 2009-11-15 16:18:58 +0000 | [diff] [blame] | 1416 | /* "%lld" and "%llu" support added in Python 2.7. */ |
| 1417 | #ifdef HAVE_LONG_LONG |
| 1418 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 1419 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
| 1420 | #endif |
| 1421 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1422 | Py_RETURN_NONE; |
| 1423 | |
| 1424 | Fail: |
| 1425 | Py_XDECREF(result); |
| 1426 | return raiseTestError("test_string_from_format", msg); |
| 1427 | |
| 1428 | #undef CHECK_1_FORMAT |
| 1429 | } |
| 1430 | |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame^] | 1431 | /* Coverage testing of capsule objects. */ |
| 1432 | |
| 1433 | static const char *capsule_name = "capsule name"; |
| 1434 | static char *capsule_pointer = "capsule pointer"; |
| 1435 | static char *capsule_context = "capsule context"; |
| 1436 | static const char *capsule_error = NULL; |
| 1437 | static int |
| 1438 | capsule_destructor_call_count = 0; |
| 1439 | |
| 1440 | static void |
| 1441 | capsule_destructor(PyObject *o) { |
| 1442 | capsule_destructor_call_count++; |
| 1443 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 1444 | capsule_error = "context did not match in destructor!"; |
| 1445 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 1446 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 1447 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 1448 | capsule_error = "name did not match in destructor!"; |
| 1449 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 1450 | capsule_error = "pointer did not match in destructor!"; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | typedef struct { |
| 1455 | char *name; |
| 1456 | char *module; |
| 1457 | char *attribute; |
| 1458 | } known_capsule; |
| 1459 | |
| 1460 | static PyObject * |
| 1461 | test_capsule(PyObject *self, PyObject *args) |
| 1462 | { |
| 1463 | PyObject *object; |
| 1464 | const char *error = NULL; |
| 1465 | void *pointer; |
| 1466 | void *pointer2; |
| 1467 | known_capsule known_capsules[] = { |
| 1468 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 1469 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 1470 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 1471 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 1472 | { NULL, NULL }, |
| 1473 | }; |
| 1474 | known_capsule *known = &known_capsules[0]; |
| 1475 | |
| 1476 | #define FAIL(x) { error = (x); goto exit; } |
| 1477 | |
| 1478 | #define CHECK_DESTRUCTOR \ |
| 1479 | if (capsule_error) { \ |
| 1480 | FAIL(capsule_error); \ |
| 1481 | } \ |
| 1482 | else if (!capsule_destructor_call_count) { \ |
| 1483 | FAIL("destructor not called!"); \ |
| 1484 | } \ |
| 1485 | capsule_destructor_call_count = 0; \ |
| 1486 | |
| 1487 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 1488 | PyCapsule_SetContext(object, capsule_context); |
| 1489 | capsule_destructor(object); |
| 1490 | CHECK_DESTRUCTOR; |
| 1491 | Py_DECREF(object); |
| 1492 | CHECK_DESTRUCTOR; |
| 1493 | |
| 1494 | object = PyCapsule_New(known, "ignored", NULL); |
| 1495 | PyCapsule_SetPointer(object, capsule_pointer); |
| 1496 | PyCapsule_SetName(object, capsule_name); |
| 1497 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 1498 | PyCapsule_SetContext(object, capsule_context); |
| 1499 | capsule_destructor(object); |
| 1500 | CHECK_DESTRUCTOR; |
| 1501 | /* intentionally access using the wrong name */ |
| 1502 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 1503 | if (!PyErr_Occurred()) { |
| 1504 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1505 | } |
| 1506 | PyErr_Clear(); |
| 1507 | if (pointer2) { |
| 1508 | if (pointer2 == capsule_pointer) { |
| 1509 | FAIL("PyCapsule_GetPointer should not have" |
| 1510 | " returned the internal pointer!"); |
| 1511 | } else { |
| 1512 | FAIL("PyCapsule_GetPointer should have " |
| 1513 | "returned NULL pointer but did not!"); |
| 1514 | } |
| 1515 | } |
| 1516 | PyCapsule_SetDestructor(object, NULL); |
| 1517 | Py_DECREF(object); |
| 1518 | if (capsule_destructor_call_count) { |
| 1519 | FAIL("destructor called when it should not have been!"); |
| 1520 | } |
| 1521 | |
| 1522 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 1523 | /* yeah, ordinarily I wouldn't do this either, |
| 1524 | but it's fine for this test harness. |
| 1525 | */ |
| 1526 | static char buffer[256]; |
| 1527 | #undef FAIL |
| 1528 | #define FAIL(x) \ |
| 1529 | { \ |
| 1530 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 1531 | x, known->module, known->attribute); \ |
| 1532 | error = buffer; \ |
| 1533 | goto exit; \ |
| 1534 | } \ |
| 1535 | |
| 1536 | PyObject *module = PyImport_ImportModule(known->module); |
| 1537 | if (module) { |
| 1538 | pointer = PyCapsule_Import(known->name, 0); |
| 1539 | if (!pointer) { |
| 1540 | Py_DECREF(module); |
| 1541 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 1542 | } |
| 1543 | object = PyObject_GetAttrString(module, known->attribute); |
| 1544 | if (!object) { |
| 1545 | Py_DECREF(module); |
| 1546 | return NULL; |
| 1547 | } |
| 1548 | pointer2 = PyCapsule_GetPointer(object, |
| 1549 | "weebles wobble but they don't fall down"); |
| 1550 | if (!PyErr_Occurred()) { |
| 1551 | Py_DECREF(object); |
| 1552 | Py_DECREF(module); |
| 1553 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1554 | } |
| 1555 | PyErr_Clear(); |
| 1556 | if (pointer2) { |
| 1557 | Py_DECREF(module); |
| 1558 | Py_DECREF(object); |
| 1559 | if (pointer2 == pointer) { |
| 1560 | FAIL("PyCapsule_GetPointer should not have" |
| 1561 | " returned its internal pointer!"); |
| 1562 | } else { |
| 1563 | FAIL("PyCapsule_GetPointer should have" |
| 1564 | " returned NULL pointer but did not!"); |
| 1565 | } |
| 1566 | } |
| 1567 | Py_DECREF(object); |
| 1568 | Py_DECREF(module); |
| 1569 | } |
| 1570 | else |
| 1571 | PyErr_Clear(); |
| 1572 | } |
| 1573 | |
| 1574 | exit: |
| 1575 | if (error) { |
| 1576 | return raiseTestError("test_capsule", error); |
| 1577 | } |
| 1578 | Py_RETURN_NONE; |
| 1579 | #undef FAIL |
| 1580 | } |
| 1581 | |
Georg Brandl | e9462c7 | 2006-08-04 18:03:37 +0000 | [diff] [blame] | 1582 | /* This is here to provide a docstring for test_descr. */ |
| 1583 | static PyObject * |
| 1584 | test_with_docstring(PyObject *self) |
| 1585 | { |
| 1586 | Py_RETURN_NONE; |
| 1587 | } |
| 1588 | |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 1589 | /* To test the format of tracebacks as printed out. */ |
| 1590 | static PyObject * |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 1591 | traceback_print(PyObject *self, PyObject *args) |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 1592 | { |
| 1593 | PyObject *file; |
| 1594 | PyObject *traceback; |
| 1595 | int result; |
| 1596 | |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 1597 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 1598 | &traceback, &file)) |
| 1599 | return NULL; |
| 1600 | |
| 1601 | result = PyTraceBack_Print(traceback, file); |
| 1602 | if (result < 0) |
| 1603 | return NULL; |
| 1604 | Py_RETURN_NONE; |
| 1605 | } |
| 1606 | |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 1607 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 1608 | static PyObject * |
| 1609 | code_newempty(PyObject *self, PyObject *args) |
| 1610 | { |
| 1611 | const char *filename; |
| 1612 | const char *funcname; |
| 1613 | int firstlineno; |
| 1614 | |
| 1615 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 1616 | &filename, &funcname, &firstlineno)) |
| 1617 | return NULL; |
| 1618 | |
| 1619 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
| 1620 | } |
| 1621 | |
Georg Brandl | 740cdc3 | 2009-12-28 08:34:58 +0000 | [diff] [blame] | 1622 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 1623 | Run via Lib/test/test_exceptions.py */ |
| 1624 | static PyObject * |
| 1625 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1626 | { |
| 1627 | char *name; |
| 1628 | char *doc = NULL; |
| 1629 | PyObject *base = NULL; |
| 1630 | PyObject *dict = NULL; |
| 1631 | |
| 1632 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
| 1633 | |
| 1634 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 1635 | "s|sOO:make_exception_with_doc", kwlist, |
| 1636 | &name, &doc, &base, &dict)) |
| 1637 | return NULL; |
| 1638 | |
| 1639 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
| 1640 | } |
| 1641 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1642 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1643 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 1644 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 1645 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1646 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 1647 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 1648 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 1649 | {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1650 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 1651 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 1652 | METH_NOARGS}, |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1653 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1654 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 1655 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 1656 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1657 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Georg Brandl | e9462c7 | 2006-08-04 18:03:37 +0000 | [diff] [blame] | 1658 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 1659 | PyDoc_STR("This is a pretty normal docstring.")}, |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1660 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 1661 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 1662 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 1663 | METH_VARARGS|METH_KEYWORDS}, |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1664 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 1665 | {"getargs_B", getargs_B, METH_VARARGS}, |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 1666 | {"getargs_h", getargs_h, METH_VARARGS}, |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1667 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 1668 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 1669 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 1670 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 1671 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 1672 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1673 | #ifdef HAVE_LONG_LONG |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1674 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 1675 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 1676 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 1677 | {"test_long_long_and_overflow", |
| 1678 | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1679 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1680 | {"codec_incrementalencoder", |
| 1681 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 1682 | {"codec_incrementaldecoder", |
| 1683 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1684 | #endif |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1685 | #ifdef Py_USING_UNICODE |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1686 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1687 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1688 | #endif |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1689 | #ifdef WITH_THREAD |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1690 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1691 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1692 | #endif |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame^] | 1693 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 1694 | {"traceback_print", traceback_print, METH_VARARGS}, |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 1695 | {"code_newempty", code_newempty, METH_VARARGS}, |
Georg Brandl | 740cdc3 | 2009-12-28 08:34:58 +0000 | [diff] [blame] | 1696 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 1697 | METH_VARARGS | METH_KEYWORDS}, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1698 | {NULL, NULL} /* sentinel */ |
| 1699 | }; |
| 1700 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1701 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 1702 | |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1703 | typedef struct { |
Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 1704 | char bool_member; |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1705 | char byte_member; |
| 1706 | unsigned char ubyte_member; |
| 1707 | short short_member; |
| 1708 | unsigned short ushort_member; |
| 1709 | int int_member; |
| 1710 | unsigned int uint_member; |
| 1711 | long long_member; |
| 1712 | unsigned long ulong_member; |
| 1713 | float float_member; |
| 1714 | double double_member; |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1715 | #ifdef HAVE_LONG_LONG |
| 1716 | PY_LONG_LONG longlong_member; |
| 1717 | unsigned PY_LONG_LONG ulonglong_member; |
| 1718 | #endif |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1719 | } all_structmembers; |
| 1720 | |
| 1721 | typedef struct { |
| 1722 | PyObject_HEAD |
| 1723 | all_structmembers structmembers; |
| 1724 | } test_structmembers; |
| 1725 | |
| 1726 | static struct PyMemberDef test_members[] = { |
Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 1727 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1728 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 1729 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 1730 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 1731 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 1732 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 1733 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 1734 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 1735 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 1736 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 1737 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1738 | #ifdef HAVE_LONG_LONG |
| 1739 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 1740 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
| 1741 | #endif |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1742 | {NULL} |
| 1743 | }; |
| 1744 | |
| 1745 | |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1746 | static PyObject * |
| 1747 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 1748 | { |
| 1749 | static char *keywords[] = { |
| 1750 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 1751 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", |
| 1752 | "T_FLOAT", "T_DOUBLE", |
| 1753 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1754 | "T_LONGLONG", "T_ULONGLONG", |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1755 | #endif |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1756 | NULL}; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1757 | static char *fmt = "|bbBhHiIlkfd" |
| 1758 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1759 | "LK" |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1760 | #endif |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1761 | ; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1762 | test_structmembers *ob; |
| 1763 | ob = PyObject_New(test_structmembers, type); |
| 1764 | if (ob == NULL) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1765 | return NULL; |
| 1766 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1767 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1768 | &ob->structmembers.bool_member, |
| 1769 | &ob->structmembers.byte_member, |
| 1770 | &ob->structmembers.ubyte_member, |
| 1771 | &ob->structmembers.short_member, |
| 1772 | &ob->structmembers.ushort_member, |
| 1773 | &ob->structmembers.int_member, |
| 1774 | &ob->structmembers.uint_member, |
| 1775 | &ob->structmembers.long_member, |
| 1776 | &ob->structmembers.ulong_member, |
| 1777 | &ob->structmembers.float_member, |
| 1778 | &ob->structmembers.double_member |
| 1779 | #ifdef HAVE_LONG_LONG |
| 1780 | , &ob->structmembers.longlong_member, |
| 1781 | &ob->structmembers.ulonglong_member |
| 1782 | #endif |
| 1783 | )) { |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1784 | Py_DECREF(ob); |
| 1785 | return NULL; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1786 | } |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1787 | return (PyObject *)ob; |
| 1788 | } |
| 1789 | |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1790 | static void |
| 1791 | test_structmembers_free(PyObject *ob) |
| 1792 | { |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1793 | PyObject_FREE(ob); |
| 1794 | } |
| 1795 | |
| 1796 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1797 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1798 | "test_structmembersType", |
| 1799 | sizeof(test_structmembers), /* tp_basicsize */ |
| 1800 | 0, /* tp_itemsize */ |
| 1801 | test_structmembers_free, /* destructor tp_dealloc */ |
| 1802 | 0, /* tp_print */ |
| 1803 | 0, /* tp_getattr */ |
| 1804 | 0, /* tp_setattr */ |
| 1805 | 0, /* tp_compare */ |
| 1806 | 0, /* tp_repr */ |
| 1807 | 0, /* tp_as_number */ |
| 1808 | 0, /* tp_as_sequence */ |
| 1809 | 0, /* tp_as_mapping */ |
| 1810 | 0, /* tp_hash */ |
| 1811 | 0, /* tp_call */ |
| 1812 | 0, /* tp_str */ |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1813 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1814 | PyObject_GenericSetAttr, /* tp_setattro */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1815 | 0, /* tp_as_buffer */ |
| 1816 | 0, /* tp_flags */ |
| 1817 | "Type containing all structmember types", |
| 1818 | 0, /* traverseproc tp_traverse */ |
| 1819 | 0, /* tp_clear */ |
| 1820 | 0, /* tp_richcompare */ |
| 1821 | 0, /* tp_weaklistoffset */ |
| 1822 | 0, /* tp_iter */ |
| 1823 | 0, /* tp_iternext */ |
| 1824 | 0, /* tp_methods */ |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1825 | test_members, /* tp_members */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1826 | 0, |
| 1827 | 0, |
| 1828 | 0, |
| 1829 | 0, |
| 1830 | 0, |
| 1831 | 0, |
| 1832 | 0, |
| 1833 | 0, |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1834 | test_structmembers_new, /* tp_new */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1835 | }; |
| 1836 | |
| 1837 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1838 | PyMODINIT_FUNC |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1839 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1840 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1841 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1842 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1843 | m = Py_InitModule("_testcapi", TestMethods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1844 | if (m == NULL) |
| 1845 | return; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1846 | |
Hirokazu Yamamoto | 52c1e3c | 2008-12-31 05:24:37 +0000 | [diff] [blame] | 1847 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 3cda1db | 2008-12-31 05:47:19 +0000 | [diff] [blame] | 1848 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1849 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1850 | Py_INCREF(&test_structmembersType); |
| 1851 | PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType); |
| 1852 | |
| 1853 | PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)); |
| 1854 | PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1855 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1856 | PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)); |
| 1857 | PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1858 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1859 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 1860 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1861 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1862 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1863 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); |
| 1864 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 1865 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 1866 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 1867 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 1868 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1869 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 1870 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 1871 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
Georg Brandl | 635af32 | 2006-04-13 07:29:18 +0000 | [diff] [blame] | 1872 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1873 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN)); |
Robert Schuppenies | 4762902 | 2008-07-10 17:13:55 +0000 | [diff] [blame] | 1874 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head))); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1875 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1876 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1877 | Py_INCREF(TestError); |
| 1878 | PyModule_AddObject(m, "error", TestError); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1879 | } |