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