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