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 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 286 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 287 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 288 | |
| 289 | Note that the meat of the test is contained in testcapi_long.h. |
| 290 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 291 | 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] | 292 | dependence on type names makes it impossible to use a parameterized |
| 293 | function. A giant macro would be even worse than this. A C++ template |
| 294 | would be perfect. |
| 295 | |
| 296 | The "report an error" functions are deliberately not part of the #include |
| 297 | file: if the test fails, you can set a breakpoint in the appropriate |
| 298 | error function directly, and crawl back from there in the debugger. |
| 299 | */ |
| 300 | |
| 301 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 302 | |
| 303 | static PyObject * |
| 304 | raise_test_long_error(const char* msg) |
| 305 | { |
| 306 | return raiseTestError("test_long_api", msg); |
| 307 | } |
| 308 | |
| 309 | #define TESTNAME test_long_api_inner |
| 310 | #define TYPENAME long |
| 311 | #define F_S_TO_PY PyLong_FromLong |
| 312 | #define F_PY_TO_S PyLong_AsLong |
| 313 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 314 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 315 | |
| 316 | #include "testcapi_long.h" |
| 317 | |
| 318 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 319 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 320 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 321 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | #undef TESTNAME |
| 325 | #undef TYPENAME |
| 326 | #undef F_S_TO_PY |
| 327 | #undef F_PY_TO_S |
| 328 | #undef F_U_TO_PY |
| 329 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 330 | |
| 331 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 332 | |
| 333 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 334 | raise_test_longlong_error(const char* msg) |
| 335 | { |
| 336 | return raiseTestError("test_longlong_api", msg); |
| 337 | } |
| 338 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 339 | #define TESTNAME test_longlong_api_inner |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 340 | #define TYPENAME PY_LONG_LONG |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 341 | #define F_S_TO_PY PyLong_FromLongLong |
| 342 | #define F_PY_TO_S PyLong_AsLongLong |
| 343 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 344 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 345 | |
| 346 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 347 | |
| 348 | static PyObject * |
Skip Montanaro | 9582c14 | 2006-04-18 01:01:41 +0000 | [diff] [blame] | 349 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 350 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 351 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 354 | #undef TESTNAME |
| 355 | #undef TYPENAME |
| 356 | #undef F_S_TO_PY |
| 357 | #undef F_PY_TO_S |
| 358 | #undef F_U_TO_PY |
| 359 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 360 | |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 361 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 362 | is tested by test_long_api_inner. This test will concentrate on proper |
| 363 | handling of overflow. |
| 364 | */ |
| 365 | |
| 366 | static PyObject * |
| 367 | test_long_and_overflow(PyObject *self) |
| 368 | { |
| 369 | PyObject *num; |
| 370 | long value; |
| 371 | int overflow; |
| 372 | |
| 373 | /* a number larger than LONG_MAX even on 64-bit platforms */ |
| 374 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 375 | if (num == NULL) |
| 376 | return NULL; |
| 377 | |
| 378 | /* Test that overflow is set properly for a large value. */ |
| 379 | overflow = 1234; |
| 380 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 381 | if (overflow != 1) |
| 382 | return raiseTestError("test_long_and_overflow", |
| 383 | "overflow was not set to 1"); |
| 384 | |
| 385 | overflow = 0; |
| 386 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 387 | if (overflow != 1) |
| 388 | return raiseTestError("test_long_and_overflow", |
| 389 | "overflow was not set to 0"); |
| 390 | |
Mark Dickinson | ed02b3f | 2009-12-21 11:31:54 +0000 | [diff] [blame^] | 391 | Py_DECREF(num); |
| 392 | |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 393 | /* a number smaller than LONG_MIN even on 64-bit platforms */ |
| 394 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 395 | if (num == NULL) |
| 396 | return NULL; |
| 397 | |
| 398 | /* Test that overflow is set properly for a large negative value. */ |
| 399 | overflow = 1234; |
| 400 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 401 | if (overflow != -1) |
| 402 | return raiseTestError("test_long_and_overflow", |
| 403 | "overflow was not set to -1"); |
| 404 | |
| 405 | overflow = 0; |
| 406 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 407 | if (overflow != -1) |
| 408 | return raiseTestError("test_long_and_overflow", |
| 409 | "overflow was not set to 0"); |
| 410 | |
Mark Dickinson | ed02b3f | 2009-12-21 11:31:54 +0000 | [diff] [blame^] | 411 | Py_DECREF(num); |
| 412 | |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 413 | num = PyLong_FromString("FF", NULL, 16); |
| 414 | if (num == NULL) |
| 415 | return NULL; |
| 416 | |
| 417 | /* Test that overflow is cleared properly for a small value. */ |
| 418 | overflow = 1234; |
| 419 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 420 | if (overflow != 0) |
| 421 | return raiseTestError("test_long_and_overflow", |
| 422 | "overflow was not cleared"); |
| 423 | |
| 424 | overflow = 0; |
| 425 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 426 | if (overflow != 0) |
| 427 | return raiseTestError("test_long_and_overflow", |
| 428 | "overflow was set incorrectly"); |
| 429 | |
Mark Dickinson | ed02b3f | 2009-12-21 11:31:54 +0000 | [diff] [blame^] | 430 | Py_DECREF(num); |
| 431 | |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 432 | Py_INCREF(Py_None); |
| 433 | return Py_None; |
| 434 | } |
| 435 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 436 | /* 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] | 437 | for both long and int arguments. The test may leak a little memory if |
| 438 | it fails. |
| 439 | */ |
| 440 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 441 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 442 | { |
| 443 | PyObject *tuple, *num; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 444 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 445 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 446 | tuple = PyTuple_New(1); |
| 447 | if (tuple == NULL) |
| 448 | return NULL; |
| 449 | |
| 450 | num = PyLong_FromLong(42); |
| 451 | if (num == NULL) |
| 452 | return NULL; |
| 453 | |
| 454 | PyTuple_SET_ITEM(tuple, 0, num); |
| 455 | |
| 456 | value = -1; |
| 457 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 458 | return NULL; |
| 459 | if (value != 42) |
| 460 | return raiseTestError("test_L_code", |
| 461 | "L code returned wrong value for long 42"); |
| 462 | |
| 463 | Py_DECREF(num); |
| 464 | num = PyInt_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 int 42"); |
| 476 | |
| 477 | Py_DECREF(tuple); |
| 478 | Py_INCREF(Py_None); |
| 479 | return Py_None; |
| 480 | } |
| 481 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 482 | #endif /* ifdef HAVE_LONG_LONG */ |
| 483 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 484 | /* Test tuple argument processing */ |
| 485 | static PyObject * |
| 486 | getargs_tuple(PyObject *self, PyObject *args) |
| 487 | { |
| 488 | int a, b, c; |
| 489 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 490 | return NULL; |
| 491 | return Py_BuildValue("iii", a, b, c); |
| 492 | } |
| 493 | |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 494 | /* test PyArg_ParseTupleAndKeywords */ |
| 495 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 496 | { |
| 497 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 498 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 499 | int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 500 | |
| 501 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 502 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 503 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 504 | return NULL; |
| 505 | return Py_BuildValue("iiiiiiiiii", |
| 506 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 507 | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
| 508 | } |
| 509 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 510 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 511 | and return the result. |
| 512 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 513 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 514 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 515 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 516 | unsigned char value; |
| 517 | if (!PyArg_ParseTuple(args, "b", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 518 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 519 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 522 | static PyObject * |
| 523 | getargs_B(PyObject *self, PyObject *args) |
| 524 | { |
| 525 | unsigned char value; |
| 526 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 527 | return NULL; |
| 528 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 529 | } |
| 530 | |
| 531 | static PyObject * |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 532 | getargs_h(PyObject *self, PyObject *args) |
| 533 | { |
| 534 | short value; |
| 535 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 536 | return NULL; |
| 537 | return PyLong_FromLong((long)value); |
| 538 | } |
| 539 | |
| 540 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 541 | getargs_H(PyObject *self, PyObject *args) |
| 542 | { |
| 543 | unsigned short value; |
| 544 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 545 | return NULL; |
| 546 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 547 | } |
| 548 | |
| 549 | static PyObject * |
| 550 | getargs_I(PyObject *self, PyObject *args) |
| 551 | { |
| 552 | unsigned int value; |
| 553 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 554 | return NULL; |
| 555 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 556 | } |
| 557 | |
| 558 | static PyObject * |
| 559 | getargs_k(PyObject *self, PyObject *args) |
| 560 | { |
| 561 | unsigned long value; |
| 562 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 563 | return NULL; |
| 564 | return PyLong_FromUnsignedLong(value); |
| 565 | } |
| 566 | |
| 567 | static PyObject * |
| 568 | getargs_i(PyObject *self, PyObject *args) |
| 569 | { |
| 570 | int value; |
| 571 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 572 | return NULL; |
| 573 | return PyLong_FromLong((long)value); |
| 574 | } |
| 575 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 576 | static PyObject * |
| 577 | getargs_l(PyObject *self, PyObject *args) |
| 578 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 579 | long value; |
| 580 | if (!PyArg_ParseTuple(args, "l", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 581 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 582 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 585 | static PyObject * |
| 586 | getargs_n(PyObject *self, PyObject *args) |
| 587 | { |
| 588 | Py_ssize_t value; |
| 589 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 590 | return NULL; |
| 591 | return PyInt_FromSsize_t(value); |
| 592 | } |
| 593 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 594 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 595 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 596 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 597 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 598 | PY_LONG_LONG value; |
| 599 | if (!PyArg_ParseTuple(args, "L", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 600 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 601 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 604 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 605 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 606 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 607 | unsigned PY_LONG_LONG value; |
| 608 | if (!PyArg_ParseTuple(args, "K", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 609 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 610 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 611 | } |
| 612 | #endif |
| 613 | |
| 614 | /* This function not only tests the 'k' getargs code, but also the |
| 615 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ |
| 616 | static PyObject * |
| 617 | test_k_code(PyObject *self) |
| 618 | { |
| 619 | PyObject *tuple, *num; |
| 620 | unsigned long value; |
| 621 | |
| 622 | tuple = PyTuple_New(1); |
| 623 | if (tuple == NULL) |
| 624 | return NULL; |
| 625 | |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 626 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 627 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 628 | if (num == NULL) |
| 629 | return NULL; |
| 630 | |
| 631 | value = PyInt_AsUnsignedLongMask(num); |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 632 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 633 | return raiseTestError("test_k_code", |
| 634 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 635 | |
| 636 | PyTuple_SET_ITEM(tuple, 0, num); |
| 637 | |
Neal Norwitz | b183a25 | 2006-04-10 01:03:32 +0000 | [diff] [blame] | 638 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 639 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 640 | return NULL; |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 641 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 642 | return raiseTestError("test_k_code", |
| 643 | "k code returned wrong value for long 0xFFF...FFF"); |
| 644 | |
| 645 | Py_DECREF(num); |
| 646 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 647 | if (num == NULL) |
| 648 | return NULL; |
| 649 | |
| 650 | value = PyInt_AsUnsignedLongMask(num); |
| 651 | if (value != (unsigned long)-0x42) |
| 652 | return raiseTestError("test_k_code", |
| 653 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 654 | |
| 655 | PyTuple_SET_ITEM(tuple, 0, num); |
| 656 | |
Neal Norwitz | b183a25 | 2006-04-10 01:03:32 +0000 | [diff] [blame] | 657 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 658 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 659 | return NULL; |
| 660 | if (value != (unsigned long)-0x42) |
| 661 | return raiseTestError("test_k_code", |
| 662 | "k code returned wrong value for long -0xFFF..000042"); |
| 663 | |
| 664 | Py_DECREF(tuple); |
| 665 | Py_INCREF(Py_None); |
| 666 | return Py_None; |
| 667 | } |
| 668 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 669 | #ifdef Py_USING_UNICODE |
| 670 | |
Benjamin Peterson | e098c4a | 2008-12-23 20:12:33 +0000 | [diff] [blame] | 671 | static volatile int x; |
Benjamin Peterson | b6a53b5 | 2008-12-23 20:09:28 +0000 | [diff] [blame] | 672 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 673 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 674 | of an error. |
| 675 | */ |
| 676 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 677 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 678 | { |
| 679 | PyObject *tuple, *obj; |
| 680 | Py_UNICODE *value; |
| 681 | int len; |
| 682 | |
Amaury Forgeot d'Arc | 07d539d | 2008-10-14 21:47:22 +0000 | [diff] [blame] | 683 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 684 | /* Just use the macro and check that it compiles */ |
Benjamin Peterson | b6a53b5 | 2008-12-23 20:09:28 +0000 | [diff] [blame] | 685 | x = Py_UNICODE_ISSPACE(25); |
Amaury Forgeot d'Arc | 07d539d | 2008-10-14 21:47:22 +0000 | [diff] [blame] | 686 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 687 | tuple = PyTuple_New(1); |
| 688 | if (tuple == NULL) |
| 689 | return NULL; |
| 690 | |
| 691 | obj = PyUnicode_Decode("test", strlen("test"), |
| 692 | "ascii", NULL); |
| 693 | if (obj == NULL) |
| 694 | return NULL; |
| 695 | |
| 696 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 697 | |
| 698 | value = 0; |
| 699 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 700 | return NULL; |
| 701 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 702 | return raiseTestError("test_u_code", |
| 703 | "u code returned wrong value for u'test'"); |
| 704 | value = 0; |
| 705 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 706 | return NULL; |
| 707 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 708 | len != PyUnicode_GET_SIZE(obj)) |
| 709 | return raiseTestError("test_u_code", |
| 710 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 711 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 712 | Py_DECREF(tuple); |
| 713 | Py_INCREF(Py_None); |
| 714 | return Py_None; |
| 715 | } |
| 716 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 717 | static PyObject * |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 718 | test_widechar(PyObject *self) |
| 719 | { |
| 720 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 721 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 722 | size_t wtextlen = 1; |
| 723 | #else |
| 724 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 725 | size_t wtextlen = 2; |
| 726 | #endif |
| 727 | PyObject *wide, *utf8; |
| 728 | |
| 729 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 730 | if (wide == NULL) |
| 731 | return NULL; |
| 732 | |
| 733 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 734 | if (utf8 == NULL) { |
| 735 | Py_DECREF(wide); |
| 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) { |
| 740 | Py_DECREF(wide); |
| 741 | Py_DECREF(utf8); |
| 742 | return raiseTestError("test_widechar", |
| 743 | "wide string and utf8 string have different length"); |
| 744 | } |
| 745 | if (PyUnicode_Compare(wide, utf8)) { |
| 746 | Py_DECREF(wide); |
| 747 | Py_DECREF(utf8); |
| 748 | if (PyErr_Occurred()) |
| 749 | return NULL; |
| 750 | return raiseTestError("test_widechar", |
| 751 | "wide string and utf8 string are differents"); |
| 752 | } |
| 753 | |
| 754 | Py_DECREF(wide); |
| 755 | Py_DECREF(utf8); |
| 756 | Py_RETURN_NONE; |
| 757 | } |
| 758 | |
| 759 | static PyObject * |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 760 | test_empty_argparse(PyObject *self) |
| 761 | { |
| 762 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 763 | PyObject *tuple, *dict = NULL; |
| 764 | static char *kwlist[] = {NULL}; |
| 765 | int result; |
| 766 | tuple = PyTuple_New(0); |
| 767 | if (!tuple) |
| 768 | return NULL; |
| 769 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 770 | goto done; |
| 771 | dict = PyDict_New(); |
| 772 | if (!dict) |
| 773 | goto done; |
| 774 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
| 775 | done: |
| 776 | Py_DECREF(tuple); |
| 777 | Py_XDECREF(dict); |
| 778 | if (result < 0) |
| 779 | return NULL; |
| 780 | else { |
| 781 | Py_RETURN_NONE; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | static PyObject * |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 786 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 787 | { |
| 788 | const char *encoding, *errors = NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 789 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 790 | &encoding, &errors)) |
| 791 | return NULL; |
| 792 | return PyCodec_IncrementalEncoder(encoding, errors); |
| 793 | } |
| 794 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 795 | static PyObject * |
| 796 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 797 | { |
| 798 | const char *encoding, *errors = NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 799 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 800 | &encoding, &errors)) |
| 801 | return NULL; |
| 802 | return PyCodec_IncrementalDecoder(encoding, errors); |
| 803 | } |
| 804 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 805 | #endif |
| 806 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 807 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 808 | static PyObject * |
| 809 | test_long_numbits(PyObject *self) |
| 810 | { |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 811 | struct triple { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 812 | long input; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 813 | size_t nbits; |
| 814 | int sign; |
| 815 | } testcases[] = {{0, 0, 0}, |
| 816 | {1L, 1, 1}, |
| 817 | {-1L, 1, -1}, |
| 818 | {2L, 2, 1}, |
| 819 | {-2L, 2, -1}, |
| 820 | {3L, 2, 1}, |
| 821 | {-3L, 2, -1}, |
| 822 | {4L, 3, 1}, |
| 823 | {-4L, 3, -1}, |
| 824 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 825 | {-0x7fffL, 15, -1}, |
| 826 | {0xffffL, 16, 1}, |
| 827 | {-0xffffL, 16, -1}, |
| 828 | {0xfffffffL, 28, 1}, |
| 829 | {-0xfffffffL, 28, -1}}; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 830 | int i; |
| 831 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 832 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 833 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 834 | size_t nbits = _PyLong_NumBits(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 835 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 836 | |
| 837 | Py_DECREF(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 838 | if (nbits != testcases[i].nbits) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 839 | return raiseTestError("test_long_numbits", |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 840 | "wrong result for _PyLong_NumBits"); |
| 841 | if (sign != testcases[i].sign) |
| 842 | return raiseTestError("test_long_numbits", |
| 843 | "wrong result for _PyLong_Sign"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 844 | } |
| 845 | Py_INCREF(Py_None); |
| 846 | return Py_None; |
| 847 | } |
| 848 | |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 849 | /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */ |
| 850 | |
| 851 | static PyObject * |
| 852 | test_null_strings(PyObject *self) |
| 853 | { |
| 854 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL); |
| 855 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 856 | Py_XDECREF(o1); |
| 857 | Py_XDECREF(o2); |
| 858 | return tuple; |
| 859 | } |
| 860 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 861 | static PyObject * |
| 862 | raise_exception(PyObject *self, PyObject *args) |
| 863 | { |
| 864 | PyObject *exc; |
| 865 | PyObject *exc_args, *v; |
| 866 | int num_args, i; |
| 867 | |
| 868 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 869 | &exc, &num_args)) |
| 870 | return NULL; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 871 | if (!PyExceptionClass_Check(exc)) { |
| 872 | PyErr_Format(PyExc_TypeError, "an exception class is required"); |
| 873 | return NULL; |
| 874 | } |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 875 | |
| 876 | exc_args = PyTuple_New(num_args); |
| 877 | if (exc_args == NULL) |
| 878 | return NULL; |
| 879 | for (i = 0; i < num_args; ++i) { |
| 880 | v = PyInt_FromLong(i); |
| 881 | if (v == NULL) { |
| 882 | Py_DECREF(exc_args); |
| 883 | return NULL; |
| 884 | } |
| 885 | PyTuple_SET_ITEM(exc_args, i, v); |
| 886 | } |
| 887 | PyErr_SetObject(exc, exc_args); |
Michael W. Hudson | f02bcee | 2003-08-15 13:03:30 +0000 | [diff] [blame] | 888 | Py_DECREF(exc_args); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 889 | return NULL; |
| 890 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 891 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 892 | #ifdef WITH_THREAD |
| 893 | |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 894 | static PyObject * |
| 895 | test_datetime_capi(PyObject *self, PyObject *args) { |
| 896 | if (PyDateTimeAPI) { |
| 897 | PyErr_SetString(PyExc_AssertionError, |
| 898 | "PyDateTime_CAPI somehow initialized"); |
| 899 | return NULL; |
| 900 | } |
| 901 | PyDateTime_IMPORT; |
| 902 | if (PyDateTimeAPI) |
| 903 | Py_RETURN_NONE; |
| 904 | else |
| 905 | return NULL; |
| 906 | } |
| 907 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 908 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 909 | * `thread_done` when it's finished. The driver code has to know when the |
| 910 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 911 | * may go away when the driver finishes. The former lack of this explicit |
| 912 | * synchronization caused rare segfaults, so rare that they were seen only |
| 913 | * on a Mac buildbot (although they were possible on any box). |
| 914 | */ |
| 915 | static PyThread_type_lock thread_done = NULL; |
| 916 | |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 917 | static int |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 918 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 919 | { |
| 920 | PyObject *rc; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 921 | int success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 922 | PyGILState_STATE s = PyGILState_Ensure(); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 923 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 924 | success = (rc != NULL); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 925 | Py_XDECREF(rc); |
| 926 | PyGILState_Release(s); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 927 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 930 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 931 | * should be called only from threads spawned by test_thread_state(). |
| 932 | */ |
| 933 | static void |
| 934 | _make_call_from_thread(void *callable) |
| 935 | { |
| 936 | _make_call(callable); |
| 937 | PyThread_release_lock(thread_done); |
| 938 | } |
| 939 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 940 | static PyObject * |
| 941 | test_thread_state(PyObject *self, PyObject *args) |
| 942 | { |
| 943 | PyObject *fn; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 944 | int success = 1; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 945 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 946 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 947 | return NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 948 | |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 949 | if (!PyCallable_Check(fn)) { |
| 950 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 951 | fn->ob_type->tp_name); |
| 952 | return NULL; |
| 953 | } |
| 954 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 955 | /* Ensure Python is set up for threading */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 956 | PyEval_InitThreads(); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 957 | thread_done = PyThread_allocate_lock(); |
| 958 | if (thread_done == NULL) |
| 959 | return PyErr_NoMemory(); |
| 960 | PyThread_acquire_lock(thread_done, 1); |
| 961 | |
| 962 | /* Start a new thread with our callback. */ |
| 963 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 964 | /* Make the callback with the thread lock held by this thread */ |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 965 | success &= _make_call(fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 966 | /* Do it all again, but this time with the thread-lock released */ |
| 967 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 968 | success &= _make_call(fn); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 969 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 970 | Py_END_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 971 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 972 | /* And once more with and without a thread |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 973 | XXX - should use a lock and work out exactly what we are trying |
| 974 | to test <wink> |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 975 | */ |
| 976 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 977 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 978 | success &= _make_call(fn); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 979 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 980 | Py_END_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 981 | |
Neal Norwitz | 97a5722 | 2006-10-28 21:17:51 +0000 | [diff] [blame] | 982 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 983 | PyThread_release_lock(thread_done); |
| 984 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 985 | PyThread_free_lock(thread_done); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 986 | if (!success) |
| 987 | return NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 988 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 989 | } |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 990 | |
| 991 | /* test Py_AddPendingCalls using threads */ |
| 992 | static int _pending_callback(void *arg) |
| 993 | { |
| 994 | /* we assume the argument is callable object to which we own a reference */ |
| 995 | PyObject *callable = (PyObject *)arg; |
| 996 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 997 | Py_DECREF(callable); |
| 998 | Py_XDECREF(r); |
| 999 | return r != NULL ? 0 : -1; |
| 1000 | } |
| 1001 | |
| 1002 | /* The following requests n callbacks to _pending_callback. It can be |
| 1003 | * run from any python thread. |
| 1004 | */ |
| 1005 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1006 | { |
| 1007 | PyObject *callable; |
| 1008 | int r; |
| 1009 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1010 | return NULL; |
| 1011 | |
| 1012 | /* create the reference for the callbackwhile we hold the lock */ |
| 1013 | Py_INCREF(callable); |
| 1014 | |
| 1015 | Py_BEGIN_ALLOW_THREADS |
| 1016 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1017 | Py_END_ALLOW_THREADS |
| 1018 | |
| 1019 | if (r<0) { |
| 1020 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1021 | Py_INCREF(Py_False); |
| 1022 | return Py_False; |
| 1023 | } |
| 1024 | Py_INCREF(Py_True); |
| 1025 | return Py_True; |
| 1026 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1027 | #endif |
| 1028 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1029 | /* Some tests of PyString_FromFormat(). This needs more tests. */ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1030 | static PyObject * |
| 1031 | test_string_from_format(PyObject *self, PyObject *args) |
| 1032 | { |
| 1033 | PyObject *result; |
| 1034 | char *msg; |
| 1035 | |
| 1036 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1037 | result = PyString_FromFormat(FORMAT, (TYPE)1); \ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1038 | if (result == NULL) \ |
| 1039 | return NULL; \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1040 | if (strcmp(PyString_AsString(result), "1")) { \ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1041 | msg = FORMAT " failed at 1"; \ |
| 1042 | goto Fail; \ |
| 1043 | } \ |
| 1044 | Py_DECREF(result) |
| 1045 | |
| 1046 | CHECK_1_FORMAT("%d", int); |
| 1047 | CHECK_1_FORMAT("%ld", long); |
| 1048 | /* The z width modifier was added in Python 2.5. */ |
| 1049 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
| 1050 | |
| 1051 | /* The u type code was added in Python 2.5. */ |
| 1052 | CHECK_1_FORMAT("%u", unsigned int); |
| 1053 | CHECK_1_FORMAT("%lu", unsigned long); |
| 1054 | CHECK_1_FORMAT("%zu", size_t); |
| 1055 | |
Mark Dickinson | 82864d1 | 2009-11-15 16:18:58 +0000 | [diff] [blame] | 1056 | /* "%lld" and "%llu" support added in Python 2.7. */ |
| 1057 | #ifdef HAVE_LONG_LONG |
| 1058 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 1059 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
| 1060 | #endif |
| 1061 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1062 | Py_RETURN_NONE; |
| 1063 | |
| 1064 | Fail: |
| 1065 | Py_XDECREF(result); |
| 1066 | return raiseTestError("test_string_from_format", msg); |
| 1067 | |
| 1068 | #undef CHECK_1_FORMAT |
| 1069 | } |
| 1070 | |
Georg Brandl | e9462c7 | 2006-08-04 18:03:37 +0000 | [diff] [blame] | 1071 | /* This is here to provide a docstring for test_descr. */ |
| 1072 | static PyObject * |
| 1073 | test_with_docstring(PyObject *self) |
| 1074 | { |
| 1075 | Py_RETURN_NONE; |
| 1076 | } |
| 1077 | |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 1078 | /* To test the format of tracebacks as printed out. */ |
| 1079 | static PyObject * |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 1080 | traceback_print(PyObject *self, PyObject *args) |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 1081 | { |
| 1082 | PyObject *file; |
| 1083 | PyObject *traceback; |
| 1084 | int result; |
| 1085 | |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 1086 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 1087 | &traceback, &file)) |
| 1088 | return NULL; |
| 1089 | |
| 1090 | result = PyTraceBack_Print(traceback, file); |
| 1091 | if (result < 0) |
| 1092 | return NULL; |
| 1093 | Py_RETURN_NONE; |
| 1094 | } |
| 1095 | |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 1096 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 1097 | static PyObject * |
| 1098 | code_newempty(PyObject *self, PyObject *args) |
| 1099 | { |
| 1100 | const char *filename; |
| 1101 | const char *funcname; |
| 1102 | int firstlineno; |
| 1103 | |
| 1104 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 1105 | &filename, &funcname, &firstlineno)) |
| 1106 | return NULL; |
| 1107 | |
| 1108 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
| 1109 | } |
| 1110 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1111 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1112 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 1113 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 1114 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1115 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 1116 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 1117 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1118 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 1119 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, METH_NOARGS}, |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1120 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1121 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 1122 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 1123 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1124 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Georg Brandl | e9462c7 | 2006-08-04 18:03:37 +0000 | [diff] [blame] | 1125 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 1126 | PyDoc_STR("This is a pretty normal docstring.")}, |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1127 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 1128 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 1129 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 1130 | METH_VARARGS|METH_KEYWORDS}, |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1131 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 1132 | {"getargs_B", getargs_B, METH_VARARGS}, |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 1133 | {"getargs_h", getargs_h, METH_VARARGS}, |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1134 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 1135 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 1136 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 1137 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 1138 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 1139 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1140 | #ifdef HAVE_LONG_LONG |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1141 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 1142 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 1143 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1144 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1145 | {"codec_incrementalencoder", |
| 1146 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 1147 | {"codec_incrementaldecoder", |
| 1148 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1149 | #endif |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1150 | #ifdef Py_USING_UNICODE |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1151 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1152 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1153 | #endif |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1154 | #ifdef WITH_THREAD |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1155 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1156 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1157 | #endif |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 1158 | {"traceback_print", traceback_print, METH_VARARGS}, |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 1159 | {"code_newempty", code_newempty, METH_VARARGS}, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1160 | {NULL, NULL} /* sentinel */ |
| 1161 | }; |
| 1162 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1163 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 1164 | |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1165 | typedef struct { |
Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 1166 | char bool_member; |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1167 | char byte_member; |
| 1168 | unsigned char ubyte_member; |
| 1169 | short short_member; |
| 1170 | unsigned short ushort_member; |
| 1171 | int int_member; |
| 1172 | unsigned int uint_member; |
| 1173 | long long_member; |
| 1174 | unsigned long ulong_member; |
| 1175 | float float_member; |
| 1176 | double double_member; |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1177 | #ifdef HAVE_LONG_LONG |
| 1178 | PY_LONG_LONG longlong_member; |
| 1179 | unsigned PY_LONG_LONG ulonglong_member; |
| 1180 | #endif |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1181 | } all_structmembers; |
| 1182 | |
| 1183 | typedef struct { |
| 1184 | PyObject_HEAD |
| 1185 | all_structmembers structmembers; |
| 1186 | } test_structmembers; |
| 1187 | |
| 1188 | static struct PyMemberDef test_members[] = { |
Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 1189 | {"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] | 1190 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 1191 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 1192 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 1193 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 1194 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 1195 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 1196 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 1197 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 1198 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 1199 | {"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] | 1200 | #ifdef HAVE_LONG_LONG |
| 1201 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 1202 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
| 1203 | #endif |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1204 | {NULL} |
| 1205 | }; |
| 1206 | |
| 1207 | |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1208 | static PyObject * |
| 1209 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 1210 | { |
| 1211 | static char *keywords[] = { |
| 1212 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 1213 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", |
| 1214 | "T_FLOAT", "T_DOUBLE", |
| 1215 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1216 | "T_LONGLONG", "T_ULONGLONG", |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1217 | #endif |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1218 | NULL}; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1219 | static char *fmt = "|bbBhHiIlkfd" |
| 1220 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1221 | "LK" |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1222 | #endif |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1223 | ; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1224 | test_structmembers *ob; |
| 1225 | ob = PyObject_New(test_structmembers, type); |
| 1226 | if (ob == NULL) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1227 | return NULL; |
| 1228 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1229 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1230 | &ob->structmembers.bool_member, |
| 1231 | &ob->structmembers.byte_member, |
| 1232 | &ob->structmembers.ubyte_member, |
| 1233 | &ob->structmembers.short_member, |
| 1234 | &ob->structmembers.ushort_member, |
| 1235 | &ob->structmembers.int_member, |
| 1236 | &ob->structmembers.uint_member, |
| 1237 | &ob->structmembers.long_member, |
| 1238 | &ob->structmembers.ulong_member, |
| 1239 | &ob->structmembers.float_member, |
| 1240 | &ob->structmembers.double_member |
| 1241 | #ifdef HAVE_LONG_LONG |
| 1242 | , &ob->structmembers.longlong_member, |
| 1243 | &ob->structmembers.ulonglong_member |
| 1244 | #endif |
| 1245 | )) { |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1246 | Py_DECREF(ob); |
| 1247 | return NULL; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1248 | } |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1249 | return (PyObject *)ob; |
| 1250 | } |
| 1251 | |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1252 | static void |
| 1253 | test_structmembers_free(PyObject *ob) |
| 1254 | { |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1255 | PyObject_FREE(ob); |
| 1256 | } |
| 1257 | |
| 1258 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1259 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1260 | "test_structmembersType", |
| 1261 | sizeof(test_structmembers), /* tp_basicsize */ |
| 1262 | 0, /* tp_itemsize */ |
| 1263 | test_structmembers_free, /* destructor tp_dealloc */ |
| 1264 | 0, /* tp_print */ |
| 1265 | 0, /* tp_getattr */ |
| 1266 | 0, /* tp_setattr */ |
| 1267 | 0, /* tp_compare */ |
| 1268 | 0, /* tp_repr */ |
| 1269 | 0, /* tp_as_number */ |
| 1270 | 0, /* tp_as_sequence */ |
| 1271 | 0, /* tp_as_mapping */ |
| 1272 | 0, /* tp_hash */ |
| 1273 | 0, /* tp_call */ |
| 1274 | 0, /* tp_str */ |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1275 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1276 | PyObject_GenericSetAttr, /* tp_setattro */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1277 | 0, /* tp_as_buffer */ |
| 1278 | 0, /* tp_flags */ |
| 1279 | "Type containing all structmember types", |
| 1280 | 0, /* traverseproc tp_traverse */ |
| 1281 | 0, /* tp_clear */ |
| 1282 | 0, /* tp_richcompare */ |
| 1283 | 0, /* tp_weaklistoffset */ |
| 1284 | 0, /* tp_iter */ |
| 1285 | 0, /* tp_iternext */ |
| 1286 | 0, /* tp_methods */ |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1287 | test_members, /* tp_members */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1288 | 0, |
| 1289 | 0, |
| 1290 | 0, |
| 1291 | 0, |
| 1292 | 0, |
| 1293 | 0, |
| 1294 | 0, |
| 1295 | 0, |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1296 | test_structmembers_new, /* tp_new */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1297 | }; |
| 1298 | |
| 1299 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1300 | PyMODINIT_FUNC |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1301 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1302 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1303 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1304 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1305 | m = Py_InitModule("_testcapi", TestMethods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1306 | if (m == NULL) |
| 1307 | return; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1308 | |
Hirokazu Yamamoto | 52c1e3c | 2008-12-31 05:24:37 +0000 | [diff] [blame] | 1309 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 3cda1db | 2008-12-31 05:47:19 +0000 | [diff] [blame] | 1310 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1311 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1312 | Py_INCREF(&test_structmembersType); |
| 1313 | PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType); |
| 1314 | |
| 1315 | PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)); |
| 1316 | PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1317 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1318 | PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)); |
| 1319 | PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1320 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1321 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 1322 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1323 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1324 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1325 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); |
| 1326 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 1327 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 1328 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 1329 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 1330 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1331 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 1332 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 1333 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
Georg Brandl | 635af32 | 2006-04-13 07:29:18 +0000 | [diff] [blame] | 1334 | 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] | 1335 | 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] | 1336 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head))); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1337 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1338 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1339 | Py_INCREF(TestError); |
| 1340 | PyModule_AddObject(m, "error", TestError); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1341 | } |