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