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" |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 11 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 12 | #ifdef WITH_THREAD |
| 13 | #include "pythread.h" |
| 14 | #endif /* WITH_THREAD */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 15 | static PyObject *TestError; /* set to exception object in init */ |
| 16 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 17 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 18 | |
| 19 | static PyObject * |
| 20 | raiseTestError(const char* test_name, const char* msg) |
| 21 | { |
| 22 | char buf[2048]; |
| 23 | |
| 24 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 25 | PyErr_SetString(TestError, "internal error msg too large"); |
| 26 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 27 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 28 | PyErr_SetString(TestError, buf); |
| 29 | } |
| 30 | return NULL; |
| 31 | } |
| 32 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 33 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 34 | |
| 35 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 36 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 37 | platforms have these hardcoded. Better safe than sorry. |
| 38 | */ |
| 39 | static PyObject* |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 40 | sizeof_error(const char* fatname, const char* typname, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 41 | int expected, int got) |
| 42 | { |
| 43 | char buf[1024]; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 44 | PyOS_snprintf(buf, sizeof(buf), |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 45 | "%.200s #define == %d but sizeof(%.200s) == %d", |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 46 | fatname, expected, typname, got); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 47 | PyErr_SetString(TestError, buf); |
| 48 | return (PyObject*)NULL; |
| 49 | } |
| 50 | |
| 51 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 52 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 53 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 54 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
| 55 | if (FATNAME != sizeof(TYPE)) \ |
| 56 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
| 57 | |
Tim Peters | 208efe5 | 2001-06-26 22:40:47 +0000 | [diff] [blame] | 58 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 59 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 60 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 61 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 62 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
| 63 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 64 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | #undef CHECK_SIZEOF |
| 68 | |
| 69 | Py_INCREF(Py_None); |
| 70 | return Py_None; |
| 71 | } |
| 72 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 73 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 74 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 75 | { |
| 76 | PyObject* list; |
| 77 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 78 | |
| 79 | /* SF bug 132008: PyList_Reverse segfaults */ |
| 80 | #define NLIST 30 |
| 81 | list = PyList_New(NLIST); |
| 82 | if (list == (PyObject*)NULL) |
| 83 | return (PyObject*)NULL; |
| 84 | /* list = range(NLIST) */ |
| 85 | for (i = 0; i < NLIST; ++i) { |
| 86 | PyObject* anint = PyInt_FromLong(i); |
| 87 | if (anint == (PyObject*)NULL) { |
| 88 | Py_DECREF(list); |
| 89 | return (PyObject*)NULL; |
| 90 | } |
| 91 | PyList_SET_ITEM(list, i, anint); |
| 92 | } |
| 93 | /* list.reverse(), via PyList_Reverse() */ |
| 94 | i = PyList_Reverse(list); /* should not blow up! */ |
| 95 | if (i != 0) { |
| 96 | Py_DECREF(list); |
| 97 | return (PyObject*)NULL; |
| 98 | } |
| 99 | /* Check that list == range(29, -1, -1) now */ |
| 100 | for (i = 0; i < NLIST; ++i) { |
| 101 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 102 | if (PyInt_AS_LONG(anint) != NLIST-1-i) { |
| 103 | PyErr_SetString(TestError, |
| 104 | "test_list_api: reverse screwed up"); |
| 105 | Py_DECREF(list); |
| 106 | return (PyObject*)NULL; |
| 107 | } |
| 108 | } |
| 109 | Py_DECREF(list); |
| 110 | #undef NLIST |
| 111 | |
| 112 | Py_INCREF(Py_None); |
| 113 | return Py_None; |
| 114 | } |
| 115 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 116 | static int |
| 117 | test_dict_inner(int count) |
| 118 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 119 | Py_ssize_t pos = 0, iterations = 0; |
| 120 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 121 | PyObject *dict = PyDict_New(); |
| 122 | PyObject *v, *k; |
| 123 | |
| 124 | if (dict == NULL) |
| 125 | return -1; |
| 126 | |
| 127 | for (i = 0; i < count; i++) { |
| 128 | v = PyInt_FromLong(i); |
| 129 | PyDict_SetItem(dict, v, v); |
| 130 | Py_DECREF(v); |
| 131 | } |
| 132 | |
| 133 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 134 | PyObject *o; |
| 135 | iterations++; |
| 136 | |
| 137 | i = PyInt_AS_LONG(v) + 1; |
| 138 | o = PyInt_FromLong(i); |
| 139 | if (o == NULL) |
| 140 | return -1; |
| 141 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 142 | Py_DECREF(o); |
| 143 | return -1; |
| 144 | } |
| 145 | Py_DECREF(o); |
| 146 | } |
| 147 | |
| 148 | Py_DECREF(dict); |
| 149 | |
| 150 | if (iterations != count) { |
| 151 | PyErr_SetString( |
| 152 | TestError, |
| 153 | "test_dict_iteration: dict iteration went wrong "); |
| 154 | return -1; |
| 155 | } else { |
| 156 | return 0; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 161 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 162 | { |
| 163 | int i; |
| 164 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 165 | for (i = 0; i < 200; i++) { |
| 166 | if (test_dict_inner(i) < 0) { |
| 167 | return NULL; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | Py_INCREF(Py_None); |
| 172 | return Py_None; |
| 173 | } |
| 174 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 175 | |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 176 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 177 | * PyType_Ready if it hasn't already been called |
| 178 | */ |
| 179 | static PyTypeObject _HashInheritanceTester_Type = { |
Hirokazu Yamamoto | 52c1e3c | 2008-12-31 05:24:37 +0000 | [diff] [blame] | 180 | PyObject_HEAD_INIT(NULL) |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 181 | 0, /* Number of items for varobject */ |
| 182 | "hashinheritancetester", /* Name of this type */ |
| 183 | sizeof(PyObject), /* Basic object size */ |
| 184 | 0, /* Item size for varobject */ |
| 185 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 186 | 0, /* tp_print */ |
| 187 | 0, /* tp_getattr */ |
| 188 | 0, /* tp_setattr */ |
| 189 | 0, /* tp_compare */ |
| 190 | 0, /* tp_repr */ |
| 191 | 0, /* tp_as_number */ |
| 192 | 0, /* tp_as_sequence */ |
| 193 | 0, /* tp_as_mapping */ |
| 194 | 0, /* tp_hash */ |
| 195 | 0, /* tp_call */ |
| 196 | 0, /* tp_str */ |
| 197 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 198 | 0, /* tp_setattro */ |
| 199 | 0, /* tp_as_buffer */ |
| 200 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 201 | 0, /* tp_doc */ |
| 202 | 0, /* tp_traverse */ |
| 203 | 0, /* tp_clear */ |
| 204 | 0, /* tp_richcompare */ |
| 205 | 0, /* tp_weaklistoffset */ |
| 206 | 0, /* tp_iter */ |
| 207 | 0, /* tp_iternext */ |
| 208 | 0, /* tp_methods */ |
| 209 | 0, /* tp_members */ |
| 210 | 0, /* tp_getset */ |
| 211 | 0, /* tp_base */ |
| 212 | 0, /* tp_dict */ |
| 213 | 0, /* tp_descr_get */ |
| 214 | 0, /* tp_descr_set */ |
| 215 | 0, /* tp_dictoffset */ |
| 216 | 0, /* tp_init */ |
| 217 | 0, /* tp_alloc */ |
| 218 | PyType_GenericNew, /* tp_new */ |
| 219 | }; |
| 220 | |
| 221 | static PyObject* |
| 222 | test_lazy_hash_inheritance(PyObject* self) |
| 223 | { |
| 224 | PyTypeObject *type; |
| 225 | PyObject *obj; |
| 226 | long hash; |
| 227 | |
| 228 | type = &_HashInheritanceTester_Type; |
| 229 | obj = PyObject_New(PyObject, type); |
| 230 | if (obj == NULL) { |
| 231 | PyErr_Clear(); |
| 232 | PyErr_SetString( |
| 233 | TestError, |
| 234 | "test_lazy_hash_inheritance: failed to create object"); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | if (type->tp_dict != NULL) { |
| 239 | PyErr_SetString( |
| 240 | TestError, |
| 241 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 242 | Py_DECREF(obj); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | hash = PyObject_Hash(obj); |
| 247 | if ((hash == -1) && PyErr_Occurred()) { |
| 248 | PyErr_Clear(); |
| 249 | PyErr_SetString( |
| 250 | TestError, |
| 251 | "test_lazy_hash_inheritance: could not hash object"); |
| 252 | Py_DECREF(obj); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | if (type->tp_dict == NULL) { |
| 257 | PyErr_SetString( |
| 258 | TestError, |
| 259 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 260 | Py_DECREF(obj); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 265 | PyErr_SetString( |
| 266 | TestError, |
| 267 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 268 | Py_DECREF(obj); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | Py_RETURN_NONE; |
| 273 | } |
| 274 | |
| 275 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 276 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 277 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 278 | |
| 279 | Note that the meat of the test is contained in testcapi_long.h. |
| 280 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 281 | 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] | 282 | dependence on type names makes it impossible to use a parameterized |
| 283 | function. A giant macro would be even worse than this. A C++ template |
| 284 | would be perfect. |
| 285 | |
| 286 | The "report an error" functions are deliberately not part of the #include |
| 287 | file: if the test fails, you can set a breakpoint in the appropriate |
| 288 | error function directly, and crawl back from there in the debugger. |
| 289 | */ |
| 290 | |
| 291 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 292 | |
| 293 | static PyObject * |
| 294 | raise_test_long_error(const char* msg) |
| 295 | { |
| 296 | return raiseTestError("test_long_api", msg); |
| 297 | } |
| 298 | |
| 299 | #define TESTNAME test_long_api_inner |
| 300 | #define TYPENAME long |
| 301 | #define F_S_TO_PY PyLong_FromLong |
| 302 | #define F_PY_TO_S PyLong_AsLong |
| 303 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 304 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 305 | |
| 306 | #include "testcapi_long.h" |
| 307 | |
| 308 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 309 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 310 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 311 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | #undef TESTNAME |
| 315 | #undef TYPENAME |
| 316 | #undef F_S_TO_PY |
| 317 | #undef F_PY_TO_S |
| 318 | #undef F_U_TO_PY |
| 319 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 320 | |
| 321 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 322 | |
| 323 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 324 | raise_test_longlong_error(const char* msg) |
| 325 | { |
| 326 | return raiseTestError("test_longlong_api", msg); |
| 327 | } |
| 328 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 329 | #define TESTNAME test_longlong_api_inner |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 330 | #define TYPENAME PY_LONG_LONG |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 331 | #define F_S_TO_PY PyLong_FromLongLong |
| 332 | #define F_PY_TO_S PyLong_AsLongLong |
| 333 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 334 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 335 | |
| 336 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 337 | |
| 338 | static PyObject * |
Skip Montanaro | 9582c14 | 2006-04-18 01:01:41 +0000 | [diff] [blame] | 339 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 340 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 341 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 344 | #undef TESTNAME |
| 345 | #undef TYPENAME |
| 346 | #undef F_S_TO_PY |
| 347 | #undef F_PY_TO_S |
| 348 | #undef F_U_TO_PY |
| 349 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 350 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 351 | /* 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] | 352 | for both long and int arguments. The test may leak a little memory if |
| 353 | it fails. |
| 354 | */ |
| 355 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 356 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 357 | { |
| 358 | PyObject *tuple, *num; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 359 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 360 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 361 | tuple = PyTuple_New(1); |
| 362 | if (tuple == NULL) |
| 363 | return NULL; |
| 364 | |
| 365 | num = PyLong_FromLong(42); |
| 366 | if (num == NULL) |
| 367 | return NULL; |
| 368 | |
| 369 | PyTuple_SET_ITEM(tuple, 0, num); |
| 370 | |
| 371 | value = -1; |
| 372 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 373 | return NULL; |
| 374 | if (value != 42) |
| 375 | return raiseTestError("test_L_code", |
| 376 | "L code returned wrong value for long 42"); |
| 377 | |
| 378 | Py_DECREF(num); |
| 379 | num = PyInt_FromLong(42); |
| 380 | if (num == NULL) |
| 381 | return NULL; |
| 382 | |
| 383 | PyTuple_SET_ITEM(tuple, 0, num); |
| 384 | |
| 385 | value = -1; |
| 386 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 387 | return NULL; |
| 388 | if (value != 42) |
| 389 | return raiseTestError("test_L_code", |
| 390 | "L code returned wrong value for int 42"); |
| 391 | |
| 392 | Py_DECREF(tuple); |
| 393 | Py_INCREF(Py_None); |
| 394 | return Py_None; |
| 395 | } |
| 396 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 397 | #endif /* ifdef HAVE_LONG_LONG */ |
| 398 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 399 | /* Test tuple argument processing */ |
| 400 | static PyObject * |
| 401 | getargs_tuple(PyObject *self, PyObject *args) |
| 402 | { |
| 403 | int a, b, c; |
| 404 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 405 | return NULL; |
| 406 | return Py_BuildValue("iii", a, b, c); |
| 407 | } |
| 408 | |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 409 | /* test PyArg_ParseTupleAndKeywords */ |
| 410 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 411 | { |
| 412 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 413 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 414 | int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 415 | |
| 416 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 417 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 418 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 419 | return NULL; |
| 420 | return Py_BuildValue("iiiiiiiiii", |
| 421 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 422 | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
| 423 | } |
| 424 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 425 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 426 | and return the result. |
| 427 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 428 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 429 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 430 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 431 | unsigned char value; |
| 432 | if (!PyArg_ParseTuple(args, "b", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 433 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 434 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 437 | static PyObject * |
| 438 | getargs_B(PyObject *self, PyObject *args) |
| 439 | { |
| 440 | unsigned char value; |
| 441 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 442 | return NULL; |
| 443 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 444 | } |
| 445 | |
| 446 | static PyObject * |
| 447 | getargs_H(PyObject *self, PyObject *args) |
| 448 | { |
| 449 | unsigned short value; |
| 450 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 451 | return NULL; |
| 452 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 453 | } |
| 454 | |
| 455 | static PyObject * |
| 456 | getargs_I(PyObject *self, PyObject *args) |
| 457 | { |
| 458 | unsigned int value; |
| 459 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 460 | return NULL; |
| 461 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 462 | } |
| 463 | |
| 464 | static PyObject * |
| 465 | getargs_k(PyObject *self, PyObject *args) |
| 466 | { |
| 467 | unsigned long value; |
| 468 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 469 | return NULL; |
| 470 | return PyLong_FromUnsignedLong(value); |
| 471 | } |
| 472 | |
| 473 | static PyObject * |
| 474 | getargs_i(PyObject *self, PyObject *args) |
| 475 | { |
| 476 | int value; |
| 477 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 478 | return NULL; |
| 479 | return PyLong_FromLong((long)value); |
| 480 | } |
| 481 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 482 | static PyObject * |
| 483 | getargs_l(PyObject *self, PyObject *args) |
| 484 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 485 | long value; |
| 486 | if (!PyArg_ParseTuple(args, "l", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 487 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 488 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 491 | static PyObject * |
| 492 | getargs_n(PyObject *self, PyObject *args) |
| 493 | { |
| 494 | Py_ssize_t value; |
| 495 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 496 | return NULL; |
| 497 | return PyInt_FromSsize_t(value); |
| 498 | } |
| 499 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 500 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 501 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 502 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 503 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 504 | PY_LONG_LONG value; |
| 505 | if (!PyArg_ParseTuple(args, "L", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 506 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 507 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 510 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 511 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 512 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 513 | unsigned PY_LONG_LONG value; |
| 514 | if (!PyArg_ParseTuple(args, "K", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 515 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 516 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 517 | } |
| 518 | #endif |
| 519 | |
| 520 | /* This function not only tests the 'k' getargs code, but also the |
| 521 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ |
| 522 | static PyObject * |
| 523 | test_k_code(PyObject *self) |
| 524 | { |
| 525 | PyObject *tuple, *num; |
| 526 | unsigned long value; |
| 527 | |
| 528 | tuple = PyTuple_New(1); |
| 529 | if (tuple == NULL) |
| 530 | return NULL; |
| 531 | |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 532 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 533 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 534 | if (num == NULL) |
| 535 | return NULL; |
| 536 | |
| 537 | value = PyInt_AsUnsignedLongMask(num); |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 538 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 539 | return raiseTestError("test_k_code", |
| 540 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 541 | |
| 542 | PyTuple_SET_ITEM(tuple, 0, num); |
| 543 | |
Neal Norwitz | b183a25 | 2006-04-10 01:03:32 +0000 | [diff] [blame] | 544 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 545 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 546 | return NULL; |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 547 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 548 | return raiseTestError("test_k_code", |
| 549 | "k code returned wrong value for long 0xFFF...FFF"); |
| 550 | |
| 551 | Py_DECREF(num); |
| 552 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 553 | if (num == NULL) |
| 554 | return NULL; |
| 555 | |
| 556 | value = PyInt_AsUnsignedLongMask(num); |
| 557 | if (value != (unsigned long)-0x42) |
| 558 | return raiseTestError("test_k_code", |
| 559 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
| 560 | |
| 561 | PyTuple_SET_ITEM(tuple, 0, num); |
| 562 | |
Neal Norwitz | b183a25 | 2006-04-10 01:03:32 +0000 | [diff] [blame] | 563 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 564 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 565 | return NULL; |
| 566 | if (value != (unsigned long)-0x42) |
| 567 | return raiseTestError("test_k_code", |
| 568 | "k code returned wrong value for long -0xFFF..000042"); |
| 569 | |
| 570 | Py_DECREF(tuple); |
| 571 | Py_INCREF(Py_None); |
| 572 | return Py_None; |
| 573 | } |
| 574 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 575 | #ifdef Py_USING_UNICODE |
| 576 | |
Benjamin Peterson | e098c4a | 2008-12-23 20:12:33 +0000 | [diff] [blame] | 577 | static volatile int x; |
Benjamin Peterson | b6a53b5 | 2008-12-23 20:09:28 +0000 | [diff] [blame] | 578 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 579 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 580 | of an error. |
| 581 | */ |
| 582 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 583 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 584 | { |
| 585 | PyObject *tuple, *obj; |
| 586 | Py_UNICODE *value; |
| 587 | int len; |
| 588 | |
Amaury Forgeot d'Arc | 07d539d | 2008-10-14 21:47:22 +0000 | [diff] [blame] | 589 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 590 | /* Just use the macro and check that it compiles */ |
Benjamin Peterson | b6a53b5 | 2008-12-23 20:09:28 +0000 | [diff] [blame] | 591 | x = Py_UNICODE_ISSPACE(25); |
Amaury Forgeot d'Arc | 07d539d | 2008-10-14 21:47:22 +0000 | [diff] [blame] | 592 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 593 | tuple = PyTuple_New(1); |
| 594 | if (tuple == NULL) |
| 595 | return NULL; |
| 596 | |
| 597 | obj = PyUnicode_Decode("test", strlen("test"), |
| 598 | "ascii", NULL); |
| 599 | if (obj == NULL) |
| 600 | return NULL; |
| 601 | |
| 602 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 603 | |
| 604 | value = 0; |
| 605 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 606 | return NULL; |
| 607 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 608 | return raiseTestError("test_u_code", |
| 609 | "u code returned wrong value for u'test'"); |
| 610 | value = 0; |
| 611 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 612 | return NULL; |
| 613 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 614 | len != PyUnicode_GET_SIZE(obj)) |
| 615 | return raiseTestError("test_u_code", |
| 616 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 617 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 618 | Py_DECREF(tuple); |
| 619 | Py_INCREF(Py_None); |
| 620 | return Py_None; |
| 621 | } |
| 622 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 623 | static PyObject * |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 624 | test_empty_argparse(PyObject *self) |
| 625 | { |
| 626 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 627 | PyObject *tuple, *dict = NULL; |
| 628 | static char *kwlist[] = {NULL}; |
| 629 | int result; |
| 630 | tuple = PyTuple_New(0); |
| 631 | if (!tuple) |
| 632 | return NULL; |
| 633 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 634 | goto done; |
| 635 | dict = PyDict_New(); |
| 636 | if (!dict) |
| 637 | goto done; |
| 638 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
| 639 | done: |
| 640 | Py_DECREF(tuple); |
| 641 | Py_XDECREF(dict); |
| 642 | if (result < 0) |
| 643 | return NULL; |
| 644 | else { |
| 645 | Py_RETURN_NONE; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | static PyObject * |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 650 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 651 | { |
| 652 | const char *encoding, *errors = NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 653 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 654 | &encoding, &errors)) |
| 655 | return NULL; |
| 656 | return PyCodec_IncrementalEncoder(encoding, errors); |
| 657 | } |
| 658 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 659 | static PyObject * |
| 660 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 661 | { |
| 662 | const char *encoding, *errors = NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 663 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 664 | &encoding, &errors)) |
| 665 | return NULL; |
| 666 | return PyCodec_IncrementalDecoder(encoding, errors); |
| 667 | } |
| 668 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 669 | #endif |
| 670 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 671 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 672 | static PyObject * |
| 673 | test_long_numbits(PyObject *self) |
| 674 | { |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 675 | struct triple { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 676 | long input; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 677 | size_t nbits; |
| 678 | int sign; |
| 679 | } testcases[] = {{0, 0, 0}, |
| 680 | {1L, 1, 1}, |
| 681 | {-1L, 1, -1}, |
| 682 | {2L, 2, 1}, |
| 683 | {-2L, 2, -1}, |
| 684 | {3L, 2, 1}, |
| 685 | {-3L, 2, -1}, |
| 686 | {4L, 3, 1}, |
| 687 | {-4L, 3, -1}, |
| 688 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 689 | {-0x7fffL, 15, -1}, |
| 690 | {0xffffL, 16, 1}, |
| 691 | {-0xffffL, 16, -1}, |
| 692 | {0xfffffffL, 28, 1}, |
| 693 | {-0xfffffffL, 28, -1}}; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 694 | int i; |
| 695 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 696 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 697 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 698 | size_t nbits = _PyLong_NumBits(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 699 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 700 | |
| 701 | Py_DECREF(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 702 | if (nbits != testcases[i].nbits) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 703 | return raiseTestError("test_long_numbits", |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 704 | "wrong result for _PyLong_NumBits"); |
| 705 | if (sign != testcases[i].sign) |
| 706 | return raiseTestError("test_long_numbits", |
| 707 | "wrong result for _PyLong_Sign"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 708 | } |
| 709 | Py_INCREF(Py_None); |
| 710 | return Py_None; |
| 711 | } |
| 712 | |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 713 | /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */ |
| 714 | |
| 715 | static PyObject * |
| 716 | test_null_strings(PyObject *self) |
| 717 | { |
| 718 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL); |
| 719 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 720 | Py_XDECREF(o1); |
| 721 | Py_XDECREF(o2); |
| 722 | return tuple; |
| 723 | } |
| 724 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 725 | static PyObject * |
| 726 | raise_exception(PyObject *self, PyObject *args) |
| 727 | { |
| 728 | PyObject *exc; |
| 729 | PyObject *exc_args, *v; |
| 730 | int num_args, i; |
| 731 | |
| 732 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 733 | &exc, &num_args)) |
| 734 | return NULL; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 735 | if (!PyExceptionClass_Check(exc)) { |
| 736 | PyErr_Format(PyExc_TypeError, "an exception class is required"); |
| 737 | return NULL; |
| 738 | } |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 739 | |
| 740 | exc_args = PyTuple_New(num_args); |
| 741 | if (exc_args == NULL) |
| 742 | return NULL; |
| 743 | for (i = 0; i < num_args; ++i) { |
| 744 | v = PyInt_FromLong(i); |
| 745 | if (v == NULL) { |
| 746 | Py_DECREF(exc_args); |
| 747 | return NULL; |
| 748 | } |
| 749 | PyTuple_SET_ITEM(exc_args, i, v); |
| 750 | } |
| 751 | PyErr_SetObject(exc, exc_args); |
Michael W. Hudson | f02bcee | 2003-08-15 13:03:30 +0000 | [diff] [blame] | 752 | Py_DECREF(exc_args); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 753 | return NULL; |
| 754 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 755 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 756 | #ifdef WITH_THREAD |
| 757 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 758 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 759 | * `thread_done` when it's finished. The driver code has to know when the |
| 760 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 761 | * may go away when the driver finishes. The former lack of this explicit |
| 762 | * synchronization caused rare segfaults, so rare that they were seen only |
| 763 | * on a Mac buildbot (although they were possible on any box). |
| 764 | */ |
| 765 | static PyThread_type_lock thread_done = NULL; |
| 766 | |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 767 | static int |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 768 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 769 | { |
| 770 | PyObject *rc; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 771 | int success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 772 | PyGILState_STATE s = PyGILState_Ensure(); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 773 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 774 | success = (rc != NULL); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 775 | Py_XDECREF(rc); |
| 776 | PyGILState_Release(s); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 777 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 780 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 781 | * should be called only from threads spawned by test_thread_state(). |
| 782 | */ |
| 783 | static void |
| 784 | _make_call_from_thread(void *callable) |
| 785 | { |
| 786 | _make_call(callable); |
| 787 | PyThread_release_lock(thread_done); |
| 788 | } |
| 789 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 790 | static PyObject * |
| 791 | test_thread_state(PyObject *self, PyObject *args) |
| 792 | { |
| 793 | PyObject *fn; |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 794 | int success = 1; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 795 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 796 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 797 | return NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 798 | |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 799 | if (!PyCallable_Check(fn)) { |
| 800 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 801 | fn->ob_type->tp_name); |
| 802 | return NULL; |
| 803 | } |
| 804 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 805 | /* Ensure Python is set up for threading */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 806 | PyEval_InitThreads(); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 807 | thread_done = PyThread_allocate_lock(); |
| 808 | if (thread_done == NULL) |
| 809 | return PyErr_NoMemory(); |
| 810 | PyThread_acquire_lock(thread_done, 1); |
| 811 | |
| 812 | /* Start a new thread with our callback. */ |
| 813 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 814 | /* Make the callback with the thread lock held by this thread */ |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 815 | success &= _make_call(fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 816 | /* Do it all again, but this time with the thread-lock released */ |
| 817 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 818 | success &= _make_call(fn); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 819 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 820 | Py_END_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 821 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 822 | /* And once more with and without a thread |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 823 | XXX - should use a lock and work out exactly what we are trying |
| 824 | to test <wink> |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 825 | */ |
| 826 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 827 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 828 | success &= _make_call(fn); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 829 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 830 | Py_END_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 831 | |
Neal Norwitz | 97a5722 | 2006-10-28 21:17:51 +0000 | [diff] [blame] | 832 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 833 | PyThread_release_lock(thread_done); |
| 834 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 835 | PyThread_free_lock(thread_done); |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 836 | if (!success) |
| 837 | return NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 838 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 839 | } |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 840 | |
| 841 | /* test Py_AddPendingCalls using threads */ |
| 842 | static int _pending_callback(void *arg) |
| 843 | { |
| 844 | /* we assume the argument is callable object to which we own a reference */ |
| 845 | PyObject *callable = (PyObject *)arg; |
| 846 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 847 | Py_DECREF(callable); |
| 848 | Py_XDECREF(r); |
| 849 | return r != NULL ? 0 : -1; |
| 850 | } |
| 851 | |
| 852 | /* The following requests n callbacks to _pending_callback. It can be |
| 853 | * run from any python thread. |
| 854 | */ |
| 855 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 856 | { |
| 857 | PyObject *callable; |
| 858 | int r; |
| 859 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 860 | return NULL; |
| 861 | |
| 862 | /* create the reference for the callbackwhile we hold the lock */ |
| 863 | Py_INCREF(callable); |
| 864 | |
| 865 | Py_BEGIN_ALLOW_THREADS |
| 866 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 867 | Py_END_ALLOW_THREADS |
| 868 | |
| 869 | if (r<0) { |
| 870 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 871 | Py_INCREF(Py_False); |
| 872 | return Py_False; |
| 873 | } |
| 874 | Py_INCREF(Py_True); |
| 875 | return Py_True; |
| 876 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 877 | #endif |
| 878 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 879 | /* Some tests of PyString_FromFormat(). This needs more tests. */ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 880 | static PyObject * |
| 881 | test_string_from_format(PyObject *self, PyObject *args) |
| 882 | { |
| 883 | PyObject *result; |
| 884 | char *msg; |
| 885 | |
| 886 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 887 | result = PyString_FromFormat(FORMAT, (TYPE)1); \ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 888 | if (result == NULL) \ |
| 889 | return NULL; \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 890 | if (strcmp(PyString_AsString(result), "1")) { \ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 891 | msg = FORMAT " failed at 1"; \ |
| 892 | goto Fail; \ |
| 893 | } \ |
| 894 | Py_DECREF(result) |
| 895 | |
| 896 | CHECK_1_FORMAT("%d", int); |
| 897 | CHECK_1_FORMAT("%ld", long); |
| 898 | /* The z width modifier was added in Python 2.5. */ |
| 899 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
| 900 | |
| 901 | /* The u type code was added in Python 2.5. */ |
| 902 | CHECK_1_FORMAT("%u", unsigned int); |
| 903 | CHECK_1_FORMAT("%lu", unsigned long); |
| 904 | CHECK_1_FORMAT("%zu", size_t); |
| 905 | |
| 906 | Py_RETURN_NONE; |
| 907 | |
| 908 | Fail: |
| 909 | Py_XDECREF(result); |
| 910 | return raiseTestError("test_string_from_format", msg); |
| 911 | |
| 912 | #undef CHECK_1_FORMAT |
| 913 | } |
| 914 | |
Georg Brandl | e9462c7 | 2006-08-04 18:03:37 +0000 | [diff] [blame] | 915 | /* This is here to provide a docstring for test_descr. */ |
| 916 | static PyObject * |
| 917 | test_with_docstring(PyObject *self) |
| 918 | { |
| 919 | Py_RETURN_NONE; |
| 920 | } |
| 921 | |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 922 | /* To test the format of tracebacks as printed out. */ |
| 923 | static PyObject * |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 924 | traceback_print(PyObject *self, PyObject *args) |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 925 | { |
| 926 | PyObject *file; |
| 927 | PyObject *traceback; |
| 928 | int result; |
| 929 | |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 930 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 931 | &traceback, &file)) |
| 932 | return NULL; |
| 933 | |
| 934 | result = PyTraceBack_Print(traceback, file); |
| 935 | if (result < 0) |
| 936 | return NULL; |
| 937 | Py_RETURN_NONE; |
| 938 | } |
| 939 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 940 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 941 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 942 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 943 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 944 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 945 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 946 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 947 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 948 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 949 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 950 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 951 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Georg Brandl | e9462c7 | 2006-08-04 18:03:37 +0000 | [diff] [blame] | 952 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 953 | PyDoc_STR("This is a pretty normal docstring.")}, |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 954 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 955 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 956 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 957 | METH_VARARGS|METH_KEYWORDS}, |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 958 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 959 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 960 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 961 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 962 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 963 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 964 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 965 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 966 | #ifdef HAVE_LONG_LONG |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 967 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 968 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 969 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 970 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 971 | {"codec_incrementalencoder", |
| 972 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 973 | {"codec_incrementaldecoder", |
| 974 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 975 | #endif |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 976 | #ifdef Py_USING_UNICODE |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 977 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 978 | #endif |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 979 | #ifdef WITH_THREAD |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 980 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 981 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 982 | #endif |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 983 | {"traceback_print", traceback_print, METH_VARARGS}, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 984 | {NULL, NULL} /* sentinel */ |
| 985 | }; |
| 986 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 987 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 988 | |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 989 | typedef struct { |
Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 990 | char bool_member; |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 991 | char byte_member; |
| 992 | unsigned char ubyte_member; |
| 993 | short short_member; |
| 994 | unsigned short ushort_member; |
| 995 | int int_member; |
| 996 | unsigned int uint_member; |
| 997 | long long_member; |
| 998 | unsigned long ulong_member; |
| 999 | float float_member; |
| 1000 | double double_member; |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1001 | #ifdef HAVE_LONG_LONG |
| 1002 | PY_LONG_LONG longlong_member; |
| 1003 | unsigned PY_LONG_LONG ulonglong_member; |
| 1004 | #endif |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1005 | } all_structmembers; |
| 1006 | |
| 1007 | typedef struct { |
| 1008 | PyObject_HEAD |
| 1009 | all_structmembers structmembers; |
| 1010 | } test_structmembers; |
| 1011 | |
| 1012 | static struct PyMemberDef test_members[] = { |
Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 1013 | {"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] | 1014 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 1015 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 1016 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 1017 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 1018 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 1019 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 1020 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 1021 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 1022 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 1023 | {"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] | 1024 | #ifdef HAVE_LONG_LONG |
| 1025 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 1026 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
| 1027 | #endif |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1028 | {NULL} |
| 1029 | }; |
| 1030 | |
| 1031 | |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1032 | static PyObject * |
| 1033 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 1034 | { |
| 1035 | static char *keywords[] = { |
| 1036 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 1037 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", |
| 1038 | "T_FLOAT", "T_DOUBLE", |
| 1039 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1040 | "T_LONGLONG", "T_ULONGLONG", |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1041 | #endif |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1042 | NULL}; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1043 | static char *fmt = "|bbBhHiIlkfd" |
| 1044 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1045 | "LK" |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1046 | #endif |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1047 | ; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1048 | test_structmembers *ob; |
| 1049 | ob = PyObject_New(test_structmembers, type); |
| 1050 | if (ob == NULL) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1051 | return NULL; |
| 1052 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1053 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1054 | &ob->structmembers.bool_member, |
| 1055 | &ob->structmembers.byte_member, |
| 1056 | &ob->structmembers.ubyte_member, |
| 1057 | &ob->structmembers.short_member, |
| 1058 | &ob->structmembers.ushort_member, |
| 1059 | &ob->structmembers.int_member, |
| 1060 | &ob->structmembers.uint_member, |
| 1061 | &ob->structmembers.long_member, |
| 1062 | &ob->structmembers.ulong_member, |
| 1063 | &ob->structmembers.float_member, |
| 1064 | &ob->structmembers.double_member |
| 1065 | #ifdef HAVE_LONG_LONG |
| 1066 | , &ob->structmembers.longlong_member, |
| 1067 | &ob->structmembers.ulonglong_member |
| 1068 | #endif |
| 1069 | )) { |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1070 | Py_DECREF(ob); |
| 1071 | return NULL; |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1072 | } |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1073 | return (PyObject *)ob; |
| 1074 | } |
| 1075 | |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1076 | static void |
| 1077 | test_structmembers_free(PyObject *ob) |
| 1078 | { |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1079 | PyObject_FREE(ob); |
| 1080 | } |
| 1081 | |
| 1082 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1083 | PyVarObject_HEAD_INIT(NULL, 0) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1084 | "test_structmembersType", |
| 1085 | sizeof(test_structmembers), /* tp_basicsize */ |
| 1086 | 0, /* tp_itemsize */ |
| 1087 | test_structmembers_free, /* destructor tp_dealloc */ |
| 1088 | 0, /* tp_print */ |
| 1089 | 0, /* tp_getattr */ |
| 1090 | 0, /* tp_setattr */ |
| 1091 | 0, /* tp_compare */ |
| 1092 | 0, /* tp_repr */ |
| 1093 | 0, /* tp_as_number */ |
| 1094 | 0, /* tp_as_sequence */ |
| 1095 | 0, /* tp_as_mapping */ |
| 1096 | 0, /* tp_hash */ |
| 1097 | 0, /* tp_call */ |
| 1098 | 0, /* tp_str */ |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1099 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1100 | PyObject_GenericSetAttr, /* tp_setattro */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1101 | 0, /* tp_as_buffer */ |
| 1102 | 0, /* tp_flags */ |
| 1103 | "Type containing all structmember types", |
| 1104 | 0, /* traverseproc tp_traverse */ |
| 1105 | 0, /* tp_clear */ |
| 1106 | 0, /* tp_richcompare */ |
| 1107 | 0, /* tp_weaklistoffset */ |
| 1108 | 0, /* tp_iter */ |
| 1109 | 0, /* tp_iternext */ |
| 1110 | 0, /* tp_methods */ |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1111 | test_members, /* tp_members */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1112 | 0, |
| 1113 | 0, |
| 1114 | 0, |
| 1115 | 0, |
| 1116 | 0, |
| 1117 | 0, |
| 1118 | 0, |
| 1119 | 0, |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 1120 | test_structmembers_new, /* tp_new */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1121 | }; |
| 1122 | |
| 1123 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1124 | PyMODINIT_FUNC |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1125 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1126 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1127 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1128 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1129 | m = Py_InitModule("_testcapi", TestMethods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1130 | if (m == NULL) |
| 1131 | return; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1132 | |
Hirokazu Yamamoto | 52c1e3c | 2008-12-31 05:24:37 +0000 | [diff] [blame] | 1133 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 3cda1db | 2008-12-31 05:47:19 +0000 | [diff] [blame] | 1134 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1135 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1136 | Py_INCREF(&test_structmembersType); |
| 1137 | PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType); |
| 1138 | |
| 1139 | PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)); |
| 1140 | PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1141 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1142 | PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)); |
| 1143 | PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1144 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1145 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 1146 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1147 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1148 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1149 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); |
| 1150 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 1151 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 1152 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 1153 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 1154 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 1155 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 1156 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 1157 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
Georg Brandl | 635af32 | 2006-04-13 07:29:18 +0000 | [diff] [blame] | 1158 | 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] | 1159 | 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] | 1160 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head))); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1161 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1162 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1163 | Py_INCREF(TestError); |
| 1164 | PyModule_AddObject(m, "error", TestError); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1165 | } |