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