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 | |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 8 | #define PY_SSIZE_T_CLEAN |
| 9 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | #include <float.h> |
| 12 | #include "structmember.h" |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 13 | #include "datetime.h" |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 14 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 15 | #ifdef WITH_THREAD |
| 16 | #include "pythread.h" |
| 17 | #endif /* WITH_THREAD */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 18 | static PyObject *TestError; /* set to exception object in init */ |
| 19 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 20 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 21 | |
| 22 | static PyObject * |
| 23 | raiseTestError(const char* test_name, const char* msg) |
| 24 | { |
| 25 | char buf[2048]; |
| 26 | |
| 27 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 28 | PyErr_SetString(TestError, "internal error msg too large"); |
| 29 | else { |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 30 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 31 | PyErr_SetString(TestError, buf); |
| 32 | } |
| 33 | return NULL; |
| 34 | } |
| 35 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 36 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 37 | |
| 38 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 39 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 40 | platforms have these hardcoded. Better safe than sorry. |
| 41 | */ |
| 42 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 43 | sizeof_error(const char* fatname, const char* typname, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 44 | int expected, int got) |
| 45 | { |
| 46 | char buf[1024]; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 47 | PyOS_snprintf(buf, sizeof(buf), |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 48 | "%.200s #define == %d but sizeof(%.200s) == %d", |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 49 | fatname, expected, typname, got); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 50 | PyErr_SetString(TestError, buf); |
| 51 | return (PyObject*)NULL; |
| 52 | } |
| 53 | |
| 54 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 55 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 56 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 57 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
| 58 | if (FATNAME != sizeof(TYPE)) \ |
| 59 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
| 60 | |
Tim Peters | 208efe5 | 2001-06-26 22:40:47 +0000 | [diff] [blame] | 61 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 62 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 63 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 64 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 65 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
| 66 | #ifdef HAVE_LONG_LONG |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 67 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | #undef CHECK_SIZEOF |
| 71 | |
| 72 | Py_INCREF(Py_None); |
| 73 | return Py_None; |
| 74 | } |
| 75 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 76 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 77 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 78 | { |
| 79 | PyObject* list; |
| 80 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 81 | |
| 82 | /* SF bug 132008: PyList_Reverse segfaults */ |
| 83 | #define NLIST 30 |
| 84 | list = PyList_New(NLIST); |
| 85 | if (list == (PyObject*)NULL) |
| 86 | return (PyObject*)NULL; |
| 87 | /* list = range(NLIST) */ |
| 88 | for (i = 0; i < NLIST; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 89 | PyObject* anint = PyLong_FromLong(i); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 90 | if (anint == (PyObject*)NULL) { |
| 91 | Py_DECREF(list); |
| 92 | return (PyObject*)NULL; |
| 93 | } |
| 94 | PyList_SET_ITEM(list, i, anint); |
| 95 | } |
| 96 | /* list.reverse(), via PyList_Reverse() */ |
| 97 | i = PyList_Reverse(list); /* should not blow up! */ |
| 98 | if (i != 0) { |
| 99 | Py_DECREF(list); |
| 100 | return (PyObject*)NULL; |
| 101 | } |
| 102 | /* Check that list == range(29, -1, -1) now */ |
| 103 | for (i = 0; i < NLIST; ++i) { |
| 104 | PyObject* anint = PyList_GET_ITEM(list, i); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 105 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 106 | PyErr_SetString(TestError, |
| 107 | "test_list_api: reverse screwed up"); |
| 108 | Py_DECREF(list); |
| 109 | return (PyObject*)NULL; |
| 110 | } |
| 111 | } |
| 112 | Py_DECREF(list); |
| 113 | #undef NLIST |
| 114 | |
| 115 | Py_INCREF(Py_None); |
| 116 | return Py_None; |
| 117 | } |
| 118 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 119 | static int |
| 120 | test_dict_inner(int count) |
| 121 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 122 | Py_ssize_t pos = 0, iterations = 0; |
| 123 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 124 | PyObject *dict = PyDict_New(); |
| 125 | PyObject *v, *k; |
| 126 | |
| 127 | if (dict == NULL) |
| 128 | return -1; |
| 129 | |
| 130 | for (i = 0; i < count; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 131 | v = PyLong_FromLong(i); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 132 | PyDict_SetItem(dict, v, v); |
| 133 | Py_DECREF(v); |
| 134 | } |
| 135 | |
| 136 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 137 | PyObject *o; |
| 138 | iterations++; |
| 139 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 140 | i = PyLong_AS_LONG(v) + 1; |
| 141 | o = PyLong_FromLong(i); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 142 | if (o == NULL) |
| 143 | return -1; |
| 144 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 145 | Py_DECREF(o); |
| 146 | return -1; |
| 147 | } |
| 148 | Py_DECREF(o); |
| 149 | } |
| 150 | |
| 151 | Py_DECREF(dict); |
| 152 | |
| 153 | if (iterations != count) { |
| 154 | PyErr_SetString( |
| 155 | TestError, |
| 156 | "test_dict_iteration: dict iteration went wrong "); |
| 157 | return -1; |
| 158 | } else { |
| 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 164 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 165 | { |
| 166 | int i; |
| 167 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 168 | for (i = 0; i < 200; i++) { |
| 169 | if (test_dict_inner(i) < 0) { |
| 170 | return NULL; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | Py_INCREF(Py_None); |
| 175 | return Py_None; |
| 176 | } |
| 177 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 178 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 179 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 180 | * PyType_Ready if it hasn't already been called |
| 181 | */ |
| 182 | static PyTypeObject _HashInheritanceTester_Type = { |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 183 | PyVarObject_HEAD_INIT(NULL, 0) |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 184 | "hashinheritancetester", /* Name of this type */ |
| 185 | sizeof(PyObject), /* Basic object size */ |
| 186 | 0, /* Item size for varobject */ |
| 187 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 188 | 0, /* tp_print */ |
| 189 | 0, /* tp_getattr */ |
| 190 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 191 | 0, /* tp_reserved */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 192 | 0, /* tp_repr */ |
| 193 | 0, /* tp_as_number */ |
| 194 | 0, /* tp_as_sequence */ |
| 195 | 0, /* tp_as_mapping */ |
| 196 | 0, /* tp_hash */ |
| 197 | 0, /* tp_call */ |
| 198 | 0, /* tp_str */ |
| 199 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 200 | 0, /* tp_setattro */ |
| 201 | 0, /* tp_as_buffer */ |
| 202 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 203 | 0, /* tp_doc */ |
| 204 | 0, /* tp_traverse */ |
| 205 | 0, /* tp_clear */ |
| 206 | 0, /* tp_richcompare */ |
| 207 | 0, /* tp_weaklistoffset */ |
| 208 | 0, /* tp_iter */ |
| 209 | 0, /* tp_iternext */ |
| 210 | 0, /* tp_methods */ |
| 211 | 0, /* tp_members */ |
| 212 | 0, /* tp_getset */ |
| 213 | 0, /* tp_base */ |
| 214 | 0, /* tp_dict */ |
| 215 | 0, /* tp_descr_get */ |
| 216 | 0, /* tp_descr_set */ |
| 217 | 0, /* tp_dictoffset */ |
| 218 | 0, /* tp_init */ |
| 219 | 0, /* tp_alloc */ |
| 220 | PyType_GenericNew, /* tp_new */ |
| 221 | }; |
| 222 | |
| 223 | static PyObject* |
| 224 | test_lazy_hash_inheritance(PyObject* self) |
| 225 | { |
| 226 | PyTypeObject *type; |
| 227 | PyObject *obj; |
| 228 | long hash; |
| 229 | |
| 230 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 231 | |
Benjamin Peterson | b43d325 | 2009-05-05 23:14:15 +0000 | [diff] [blame] | 232 | if (type->tp_dict != NULL) |
| 233 | /* The type has already been initialized. This probably means |
| 234 | -R is being used. */ |
| 235 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 236 | |
| 237 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 238 | obj = PyObject_New(PyObject, type); |
| 239 | if (obj == NULL) { |
| 240 | PyErr_Clear(); |
| 241 | PyErr_SetString( |
| 242 | TestError, |
| 243 | "test_lazy_hash_inheritance: failed to create object"); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | if (type->tp_dict != NULL) { |
| 248 | PyErr_SetString( |
| 249 | TestError, |
| 250 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 251 | Py_DECREF(obj); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | hash = PyObject_Hash(obj); |
| 256 | if ((hash == -1) && PyErr_Occurred()) { |
| 257 | PyErr_Clear(); |
| 258 | PyErr_SetString( |
| 259 | TestError, |
| 260 | "test_lazy_hash_inheritance: could not hash object"); |
| 261 | Py_DECREF(obj); |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | if (type->tp_dict == NULL) { |
| 266 | PyErr_SetString( |
| 267 | TestError, |
| 268 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 269 | Py_DECREF(obj); |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 274 | PyErr_SetString( |
| 275 | TestError, |
| 276 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 277 | Py_DECREF(obj); |
| 278 | return NULL; |
| 279 | } |
| 280 | |
Benjamin Peterson | b43d325 | 2009-05-05 23:14:15 +0000 | [diff] [blame] | 281 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 282 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 283 | Py_RETURN_NONE; |
| 284 | } |
| 285 | |
| 286 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 287 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 288 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 289 | |
| 290 | Note that the meat of the test is contained in testcapi_long.h. |
| 291 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 292 | 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] | 293 | dependence on type names makes it impossible to use a parameterized |
| 294 | function. A giant macro would be even worse than this. A C++ template |
| 295 | would be perfect. |
| 296 | |
| 297 | The "report an error" functions are deliberately not part of the #include |
| 298 | file: if the test fails, you can set a breakpoint in the appropriate |
| 299 | error function directly, and crawl back from there in the debugger. |
| 300 | */ |
| 301 | |
| 302 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 303 | |
| 304 | static PyObject * |
| 305 | raise_test_long_error(const char* msg) |
| 306 | { |
| 307 | return raiseTestError("test_long_api", msg); |
| 308 | } |
| 309 | |
| 310 | #define TESTNAME test_long_api_inner |
| 311 | #define TYPENAME long |
| 312 | #define F_S_TO_PY PyLong_FromLong |
| 313 | #define F_PY_TO_S PyLong_AsLong |
| 314 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 315 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 316 | |
| 317 | #include "testcapi_long.h" |
| 318 | |
| 319 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 320 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 321 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 322 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | #undef TESTNAME |
| 326 | #undef TYPENAME |
| 327 | #undef F_S_TO_PY |
| 328 | #undef F_PY_TO_S |
| 329 | #undef F_U_TO_PY |
| 330 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 331 | |
| 332 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 333 | |
| 334 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 335 | raise_test_longlong_error(const char* msg) |
| 336 | { |
| 337 | return raiseTestError("test_longlong_api", msg); |
| 338 | } |
| 339 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 340 | #define TESTNAME test_longlong_api_inner |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 341 | #define TYPENAME PY_LONG_LONG |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 342 | #define F_S_TO_PY PyLong_FromLongLong |
| 343 | #define F_PY_TO_S PyLong_AsLongLong |
| 344 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 345 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 346 | |
| 347 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 348 | |
| 349 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 350 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 351 | { |
Tim Peters | 83c9edc | 2001-06-16 08:10:13 +0000 | [diff] [blame] | 352 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 355 | #undef TESTNAME |
| 356 | #undef TYPENAME |
| 357 | #undef F_S_TO_PY |
| 358 | #undef F_PY_TO_S |
| 359 | #undef F_U_TO_PY |
| 360 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 361 | |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 362 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 363 | is tested by test_long_api_inner. This test will concentrate on proper |
| 364 | handling of overflow. |
| 365 | */ |
| 366 | |
| 367 | static PyObject * |
| 368 | test_long_and_overflow(PyObject *self) |
| 369 | { |
| 370 | PyObject *num, *one, *temp; |
| 371 | long value; |
| 372 | int overflow; |
| 373 | |
| 374 | /* Test that overflow is set properly for a large value. */ |
| 375 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 376 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 377 | if (num == NULL) |
| 378 | return NULL; |
| 379 | overflow = 1234; |
| 380 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 381 | Py_DECREF(num); |
| 382 | if (value == -1 && PyErr_Occurred()) |
| 383 | return NULL; |
| 384 | if (value != -1) |
| 385 | return raiseTestError("test_long_and_overflow", |
| 386 | "return value was not set to -1"); |
| 387 | if (overflow != 1) |
| 388 | return raiseTestError("test_long_and_overflow", |
| 389 | "overflow was not set to 1"); |
| 390 | |
| 391 | /* Same again, with num = LONG_MAX + 1 */ |
| 392 | num = PyLong_FromLong(LONG_MAX); |
| 393 | if (num == NULL) |
| 394 | return NULL; |
| 395 | one = PyLong_FromLong(1L); |
| 396 | if (one == NULL) { |
| 397 | Py_DECREF(num); |
| 398 | return NULL; |
| 399 | } |
| 400 | temp = PyNumber_Add(num, one); |
| 401 | Py_DECREF(one); |
| 402 | Py_DECREF(num); |
| 403 | num = temp; |
| 404 | if (num == NULL) |
| 405 | return NULL; |
| 406 | overflow = 0; |
| 407 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 408 | Py_DECREF(num); |
| 409 | if (value == -1 && PyErr_Occurred()) |
| 410 | return NULL; |
| 411 | if (value != -1) |
| 412 | return raiseTestError("test_long_and_overflow", |
| 413 | "return value was not set to -1"); |
| 414 | if (overflow != 1) |
| 415 | return raiseTestError("test_long_and_overflow", |
| 416 | "overflow was not set to 1"); |
| 417 | |
| 418 | /* Test that overflow is set properly for a large negative value. */ |
| 419 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 420 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 421 | if (num == NULL) |
| 422 | return NULL; |
| 423 | overflow = 1234; |
| 424 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 425 | Py_DECREF(num); |
| 426 | if (value == -1 && PyErr_Occurred()) |
| 427 | return NULL; |
| 428 | if (value != -1) |
| 429 | return raiseTestError("test_long_and_overflow", |
| 430 | "return value was not set to -1"); |
| 431 | if (overflow != -1) |
| 432 | return raiseTestError("test_long_and_overflow", |
| 433 | "overflow was not set to -1"); |
| 434 | |
| 435 | /* Same again, with num = LONG_MIN - 1 */ |
| 436 | num = PyLong_FromLong(LONG_MIN); |
| 437 | if (num == NULL) |
| 438 | return NULL; |
| 439 | one = PyLong_FromLong(1L); |
| 440 | if (one == NULL) { |
| 441 | Py_DECREF(num); |
| 442 | return NULL; |
| 443 | } |
| 444 | temp = PyNumber_Subtract(num, one); |
| 445 | Py_DECREF(one); |
| 446 | Py_DECREF(num); |
| 447 | num = temp; |
| 448 | if (num == NULL) |
| 449 | return NULL; |
| 450 | overflow = 0; |
| 451 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 452 | Py_DECREF(num); |
| 453 | if (value == -1 && PyErr_Occurred()) |
| 454 | return NULL; |
| 455 | if (value != -1) |
| 456 | return raiseTestError("test_long_and_overflow", |
| 457 | "return value was not set to -1"); |
| 458 | if (overflow != -1) |
| 459 | return raiseTestError("test_long_and_overflow", |
| 460 | "overflow was not set to -1"); |
| 461 | |
| 462 | /* Test that overflow is cleared properly for small values. */ |
| 463 | num = PyLong_FromString("FF", NULL, 16); |
| 464 | if (num == NULL) |
| 465 | return NULL; |
| 466 | overflow = 1234; |
| 467 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 468 | Py_DECREF(num); |
| 469 | if (value == -1 && PyErr_Occurred()) |
| 470 | return NULL; |
| 471 | if (value != 0xFF) |
| 472 | return raiseTestError("test_long_and_overflow", |
| 473 | "expected return value 0xFF"); |
| 474 | if (overflow != 0) |
| 475 | return raiseTestError("test_long_and_overflow", |
| 476 | "overflow was not cleared"); |
| 477 | |
| 478 | num = PyLong_FromString("-FF", NULL, 16); |
| 479 | if (num == NULL) |
| 480 | return NULL; |
| 481 | overflow = 0; |
| 482 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 483 | Py_DECREF(num); |
| 484 | if (value == -1 && PyErr_Occurred()) |
| 485 | return NULL; |
| 486 | if (value != -0xFF) |
| 487 | return raiseTestError("test_long_and_overflow", |
| 488 | "expected return value 0xFF"); |
| 489 | if (overflow != 0) |
| 490 | return raiseTestError("test_long_and_overflow", |
| 491 | "overflow was set incorrectly"); |
| 492 | |
| 493 | num = PyLong_FromLong(LONG_MAX); |
| 494 | if (num == NULL) |
| 495 | return NULL; |
| 496 | overflow = 1234; |
| 497 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 498 | Py_DECREF(num); |
| 499 | if (value == -1 && PyErr_Occurred()) |
| 500 | return NULL; |
| 501 | if (value != LONG_MAX) |
| 502 | return raiseTestError("test_long_and_overflow", |
| 503 | "expected return value LONG_MAX"); |
| 504 | if (overflow != 0) |
| 505 | return raiseTestError("test_long_and_overflow", |
| 506 | "overflow was not cleared"); |
| 507 | |
| 508 | num = PyLong_FromLong(LONG_MIN); |
| 509 | if (num == NULL) |
| 510 | return NULL; |
| 511 | overflow = 0; |
| 512 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 513 | Py_DECREF(num); |
| 514 | if (value == -1 && PyErr_Occurred()) |
| 515 | return NULL; |
| 516 | if (value != LONG_MIN) |
| 517 | return raiseTestError("test_long_and_overflow", |
| 518 | "expected return value LONG_MIN"); |
| 519 | if (overflow != 0) |
| 520 | return raiseTestError("test_long_and_overflow", |
| 521 | "overflow was not cleared"); |
| 522 | |
| 523 | Py_INCREF(Py_None); |
| 524 | return Py_None; |
| 525 | } |
| 526 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 527 | /* 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] | 528 | for both long and int arguments. The test may leak a little memory if |
| 529 | it fails. |
| 530 | */ |
| 531 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 532 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 533 | { |
| 534 | PyObject *tuple, *num; |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 535 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 536 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 537 | tuple = PyTuple_New(1); |
| 538 | if (tuple == NULL) |
| 539 | return NULL; |
| 540 | |
| 541 | num = PyLong_FromLong(42); |
| 542 | if (num == NULL) |
| 543 | return NULL; |
| 544 | |
| 545 | PyTuple_SET_ITEM(tuple, 0, num); |
| 546 | |
| 547 | value = -1; |
| 548 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 549 | return NULL; |
| 550 | if (value != 42) |
| 551 | return raiseTestError("test_L_code", |
| 552 | "L code returned wrong value for long 42"); |
| 553 | |
| 554 | Py_DECREF(num); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 555 | num = PyLong_FromLong(42); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 556 | if (num == NULL) |
| 557 | return NULL; |
| 558 | |
| 559 | PyTuple_SET_ITEM(tuple, 0, num); |
| 560 | |
| 561 | value = -1; |
| 562 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 563 | return NULL; |
| 564 | if (value != 42) |
| 565 | return raiseTestError("test_L_code", |
| 566 | "L code returned wrong value for int 42"); |
| 567 | |
| 568 | Py_DECREF(tuple); |
| 569 | Py_INCREF(Py_None); |
| 570 | return Py_None; |
| 571 | } |
| 572 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 573 | #endif /* ifdef HAVE_LONG_LONG */ |
| 574 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 575 | /* Test tuple argument processing */ |
| 576 | static PyObject * |
| 577 | getargs_tuple(PyObject *self, PyObject *args) |
| 578 | { |
| 579 | int a, b, c; |
| 580 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 581 | return NULL; |
| 582 | return Py_BuildValue("iii", a, b, c); |
| 583 | } |
| 584 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 585 | /* test PyArg_ParseTupleAndKeywords */ |
| 586 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 587 | { |
| 588 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 589 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 590 | int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 591 | |
| 592 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 593 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 594 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 595 | return NULL; |
| 596 | return Py_BuildValue("iiiiiiiiii", |
| 597 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 598 | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
| 599 | } |
| 600 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 601 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 602 | and return the result. |
| 603 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 604 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 605 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 606 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 607 | unsigned char value; |
| 608 | if (!PyArg_ParseTuple(args, "b", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 609 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 610 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 613 | static PyObject * |
| 614 | getargs_B(PyObject *self, PyObject *args) |
| 615 | { |
| 616 | unsigned char value; |
| 617 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 618 | return NULL; |
| 619 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 620 | } |
| 621 | |
| 622 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 623 | getargs_h(PyObject *self, PyObject *args) |
| 624 | { |
| 625 | short value; |
| 626 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 627 | return NULL; |
| 628 | return PyLong_FromLong((long)value); |
| 629 | } |
| 630 | |
| 631 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 632 | getargs_H(PyObject *self, PyObject *args) |
| 633 | { |
| 634 | unsigned short value; |
| 635 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 636 | return NULL; |
| 637 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 638 | } |
| 639 | |
| 640 | static PyObject * |
| 641 | getargs_I(PyObject *self, PyObject *args) |
| 642 | { |
| 643 | unsigned int value; |
| 644 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 645 | return NULL; |
| 646 | return PyLong_FromUnsignedLong((unsigned long)value); |
| 647 | } |
| 648 | |
| 649 | static PyObject * |
| 650 | getargs_k(PyObject *self, PyObject *args) |
| 651 | { |
| 652 | unsigned long value; |
| 653 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 654 | return NULL; |
| 655 | return PyLong_FromUnsignedLong(value); |
| 656 | } |
| 657 | |
| 658 | static PyObject * |
| 659 | getargs_i(PyObject *self, PyObject *args) |
| 660 | { |
| 661 | int value; |
| 662 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 663 | return NULL; |
| 664 | return PyLong_FromLong((long)value); |
| 665 | } |
| 666 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 667 | static PyObject * |
| 668 | getargs_l(PyObject *self, PyObject *args) |
| 669 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 670 | long value; |
| 671 | if (!PyArg_ParseTuple(args, "l", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 672 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 673 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 676 | static PyObject * |
| 677 | getargs_n(PyObject *self, PyObject *args) |
| 678 | { |
| 679 | Py_ssize_t value; |
| 680 | if (!PyArg_ParseTuple(args, "n", &value)) |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 681 | return NULL; |
| 682 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 685 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 686 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 687 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 688 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 689 | PY_LONG_LONG value; |
| 690 | if (!PyArg_ParseTuple(args, "L", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 691 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 692 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 695 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 696 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 697 | { |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 698 | unsigned PY_LONG_LONG value; |
| 699 | if (!PyArg_ParseTuple(args, "K", &value)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 700 | return NULL; |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 701 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 702 | } |
| 703 | #endif |
| 704 | |
| 705 | /* This function not only tests the 'k' getargs code, but also the |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 706 | PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 707 | static PyObject * |
| 708 | test_k_code(PyObject *self) |
| 709 | { |
| 710 | PyObject *tuple, *num; |
| 711 | unsigned long value; |
| 712 | |
| 713 | tuple = PyTuple_New(1); |
| 714 | if (tuple == NULL) |
| 715 | return NULL; |
| 716 | |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 717 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 718 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 719 | if (num == NULL) |
| 720 | return NULL; |
| 721 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 722 | value = PyLong_AsUnsignedLongMask(num); |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 723 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 724 | return raiseTestError("test_k_code", |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 725 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 726 | |
| 727 | PyTuple_SET_ITEM(tuple, 0, num); |
| 728 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 729 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 730 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 731 | return NULL; |
Neal Norwitz | 699cbb7 | 2003-04-22 01:28:57 +0000 | [diff] [blame] | 732 | if (value != ULONG_MAX) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 733 | return raiseTestError("test_k_code", |
| 734 | "k code returned wrong value for long 0xFFF...FFF"); |
| 735 | |
| 736 | Py_DECREF(num); |
| 737 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 738 | if (num == NULL) |
| 739 | return NULL; |
| 740 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 741 | value = PyLong_AsUnsignedLongMask(num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 742 | if (value != (unsigned long)-0x42) |
| 743 | return raiseTestError("test_k_code", |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 744 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 745 | |
| 746 | PyTuple_SET_ITEM(tuple, 0, num); |
| 747 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 748 | value = 0; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 749 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 750 | return NULL; |
| 751 | if (value != (unsigned long)-0x42) |
| 752 | return raiseTestError("test_k_code", |
| 753 | "k code returned wrong value for long -0xFFF..000042"); |
| 754 | |
| 755 | Py_DECREF(tuple); |
| 756 | Py_INCREF(Py_None); |
| 757 | return Py_None; |
| 758 | } |
| 759 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 760 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 761 | /* Test the s and z codes for PyArg_ParseTuple. |
| 762 | */ |
| 763 | static PyObject * |
| 764 | test_s_code(PyObject *self) |
| 765 | { |
| 766 | /* Unicode strings should be accepted */ |
| 767 | PyObject *tuple, *obj; |
| 768 | char *value; |
| 769 | |
| 770 | tuple = PyTuple_New(1); |
| 771 | if (tuple == NULL) |
| 772 | return NULL; |
| 773 | |
| 774 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
| 775 | "latin-1", NULL); |
| 776 | if (obj == NULL) |
| 777 | return NULL; |
| 778 | |
| 779 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 780 | |
| 781 | /* These two blocks used to raise a TypeError: |
| 782 | * "argument must be string without null bytes, not str" |
| 783 | */ |
| 784 | if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) |
| 785 | return NULL; |
| 786 | |
| 787 | if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) |
| 788 | return NULL; |
| 789 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 790 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 791 | Py_RETURN_NONE; |
| 792 | } |
| 793 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 794 | static PyObject * |
| 795 | test_bug_7414(PyObject *self) |
| 796 | { |
| 797 | /* Issue #7414: for PyArg_ParseTupleAndKeywords, 'C' code wasn't being |
| 798 | skipped properly in skipitem() */ |
| 799 | int a = 0, b = 0, result; |
| 800 | char *kwlist[] = {"a", "b", NULL}; |
| 801 | PyObject *tuple = NULL, *dict = NULL, *b_str; |
| 802 | |
| 803 | tuple = PyTuple_New(0); |
| 804 | if (tuple == NULL) |
| 805 | goto failure; |
| 806 | dict = PyDict_New(); |
| 807 | if (dict == NULL) |
| 808 | goto failure; |
| 809 | b_str = PyUnicode_FromString("b"); |
| 810 | if (b_str == NULL) |
| 811 | goto failure; |
| 812 | result = PyDict_SetItemString(dict, "b", b_str); |
| 813 | Py_DECREF(b_str); |
| 814 | if (result < 0) |
| 815 | goto failure; |
| 816 | |
| 817 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|CC", |
| 818 | kwlist, &a, &b); |
| 819 | if (!result) |
| 820 | goto failure; |
| 821 | |
| 822 | if (a != 0) |
| 823 | return raiseTestError("test_bug_7414", |
| 824 | "C format code not skipped properly"); |
| 825 | if (b != 'b') |
| 826 | return raiseTestError("test_bug_7414", |
| 827 | "C format code returned wrong value"); |
| 828 | |
| 829 | Py_DECREF(dict); |
| 830 | Py_DECREF(tuple); |
| 831 | Py_RETURN_NONE; |
| 832 | |
| 833 | failure: |
| 834 | Py_XDECREF(dict); |
| 835 | Py_XDECREF(tuple); |
| 836 | return NULL; |
| 837 | } |
| 838 | |
| 839 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 840 | static volatile int x; |
| 841 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 842 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 843 | of an error. |
| 844 | */ |
| 845 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 846 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 847 | { |
| 848 | PyObject *tuple, *obj; |
| 849 | Py_UNICODE *value; |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 850 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 851 | |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 852 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 853 | /* Just use the macro and check that it compiles */ |
Christian Heimes | 727c2c5 | 2008-11-12 09:04:04 +0000 | [diff] [blame] | 854 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 855 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 856 | tuple = PyTuple_New(1); |
| 857 | if (tuple == NULL) |
| 858 | return NULL; |
| 859 | |
| 860 | obj = PyUnicode_Decode("test", strlen("test"), |
| 861 | "ascii", NULL); |
| 862 | if (obj == NULL) |
| 863 | return NULL; |
| 864 | |
| 865 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 866 | |
| 867 | value = 0; |
| 868 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 869 | return NULL; |
| 870 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 871 | return raiseTestError("test_u_code", |
| 872 | "u code returned wrong value for u'test'"); |
| 873 | value = 0; |
| 874 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 875 | return NULL; |
| 876 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 877 | len != PyUnicode_GET_SIZE(obj)) |
| 878 | return raiseTestError("test_u_code", |
| 879 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 880 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 881 | Py_DECREF(tuple); |
| 882 | Py_INCREF(Py_None); |
| 883 | return Py_None; |
| 884 | } |
| 885 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 886 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 887 | static PyObject * |
| 888 | test_Z_code(PyObject *self) |
| 889 | { |
| 890 | PyObject *tuple, *obj; |
| 891 | Py_UNICODE *value1, *value2; |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 892 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 893 | |
| 894 | tuple = PyTuple_New(2); |
| 895 | if (tuple == NULL) |
| 896 | return NULL; |
| 897 | |
| 898 | obj = PyUnicode_FromString("test"); |
| 899 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 900 | Py_INCREF(Py_None); |
| 901 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
| 902 | |
| 903 | /* swap values on purpose */ |
| 904 | value1 = NULL; |
| 905 | value2 = PyUnicode_AS_UNICODE(obj); |
| 906 | |
| 907 | /* Test Z for both values */ |
| 908 | if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) |
| 909 | return NULL; |
| 910 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 911 | return raiseTestError("test_Z_code", |
| 912 | "Z code returned wrong value for 'test'"); |
| 913 | if (value2 != NULL) |
| 914 | return raiseTestError("test_Z_code", |
| 915 | "Z code returned wrong value for None"); |
| 916 | |
| 917 | value1 = NULL; |
| 918 | value2 = PyUnicode_AS_UNICODE(obj); |
| 919 | len1 = -1; |
| 920 | len2 = -1; |
| 921 | |
| 922 | /* Test Z# for both values */ |
| 923 | if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 924 | &value2, &len2) < 0) |
| 925 | return NULL; |
| 926 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 927 | len1 != PyUnicode_GET_SIZE(obj)) |
| 928 | return raiseTestError("test_Z_code", |
| 929 | "Z# code returned wrong values for 'test'"); |
| 930 | if (value2 != NULL || |
| 931 | len2 != 0) |
| 932 | return raiseTestError("test_Z_code", |
| 933 | "Z# code returned wrong values for None'"); |
| 934 | |
| 935 | Py_DECREF(tuple); |
| 936 | Py_RETURN_NONE; |
| 937 | } |
| 938 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 939 | static PyObject * |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 940 | test_widechar(PyObject *self) |
| 941 | { |
| 942 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 943 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 944 | size_t wtextlen = 1; |
| 945 | #else |
| 946 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 947 | size_t wtextlen = 2; |
| 948 | #endif |
| 949 | PyObject *wide, *utf8; |
| 950 | |
| 951 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 952 | if (wide == NULL) |
| 953 | return NULL; |
| 954 | |
| 955 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 956 | if (utf8 == NULL) { |
| 957 | Py_DECREF(wide); |
| 958 | return NULL; |
| 959 | } |
| 960 | |
| 961 | if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) { |
| 962 | Py_DECREF(wide); |
| 963 | Py_DECREF(utf8); |
| 964 | return raiseTestError("test_widechar", |
| 965 | "wide string and utf8 string " |
| 966 | "have different length"); |
| 967 | } |
| 968 | if (PyUnicode_Compare(wide, utf8)) { |
| 969 | Py_DECREF(wide); |
| 970 | Py_DECREF(utf8); |
| 971 | if (PyErr_Occurred()) |
| 972 | return NULL; |
| 973 | return raiseTestError("test_widechar", |
| 974 | "wide string and utf8 string " |
| 975 | "are different"); |
| 976 | } |
| 977 | |
| 978 | Py_DECREF(wide); |
| 979 | Py_DECREF(utf8); |
| 980 | Py_RETURN_NONE; |
| 981 | } |
| 982 | |
| 983 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 984 | test_empty_argparse(PyObject *self) |
| 985 | { |
| 986 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 987 | PyObject *tuple, *dict = NULL; |
| 988 | static char *kwlist[] = {NULL}; |
| 989 | int result; |
| 990 | tuple = PyTuple_New(0); |
| 991 | if (!tuple) |
| 992 | return NULL; |
| 993 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 994 | goto done; |
| 995 | dict = PyDict_New(); |
| 996 | if (!dict) |
| 997 | goto done; |
| 998 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
| 999 | done: |
| 1000 | Py_DECREF(tuple); |
| 1001 | Py_XDECREF(dict); |
| 1002 | if (result < 0) |
| 1003 | return NULL; |
| 1004 | else { |
| 1005 | Py_RETURN_NONE; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1010 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1011 | { |
| 1012 | const char *encoding, *errors = NULL; |
| 1013 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 1014 | &encoding, &errors)) |
| 1015 | return NULL; |
| 1016 | return PyCodec_IncrementalEncoder(encoding, errors); |
| 1017 | } |
| 1018 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1019 | static PyObject * |
| 1020 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1021 | { |
| 1022 | const char *encoding, *errors = NULL; |
| 1023 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 1024 | &encoding, &errors)) |
| 1025 | return NULL; |
| 1026 | return PyCodec_IncrementalDecoder(encoding, errors); |
| 1027 | } |
| 1028 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1029 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1030 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1031 | static PyObject * |
| 1032 | test_long_numbits(PyObject *self) |
| 1033 | { |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1034 | struct triple { |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1035 | long input; |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1036 | size_t nbits; |
| 1037 | int sign; |
| 1038 | } testcases[] = {{0, 0, 0}, |
| 1039 | {1L, 1, 1}, |
| 1040 | {-1L, 1, -1}, |
| 1041 | {2L, 2, 1}, |
| 1042 | {-2L, 2, -1}, |
| 1043 | {3L, 2, 1}, |
| 1044 | {-3L, 2, -1}, |
| 1045 | {4L, 3, 1}, |
| 1046 | {-4L, 3, -1}, |
| 1047 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 1048 | {-0x7fffL, 15, -1}, |
| 1049 | {0xffffL, 16, 1}, |
| 1050 | {-0xffffL, 16, -1}, |
| 1051 | {0xfffffffL, 28, 1}, |
| 1052 | {-0xfffffffL, 28, -1}}; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1053 | int i; |
| 1054 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1055 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 1056 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1057 | size_t nbits = _PyLong_NumBits(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1058 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1059 | |
| 1060 | Py_DECREF(plong); |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1061 | if (nbits != testcases[i].nbits) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1062 | return raiseTestError("test_long_numbits", |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1063 | "wrong result for _PyLong_NumBits"); |
| 1064 | if (sign != testcases[i].sign) |
| 1065 | return raiseTestError("test_long_numbits", |
| 1066 | "wrong result for _PyLong_Sign"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1067 | } |
| 1068 | Py_INCREF(Py_None); |
| 1069 | return Py_None; |
| 1070 | } |
| 1071 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1072 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1073 | |
| 1074 | static PyObject * |
| 1075 | test_null_strings(PyObject *self) |
| 1076 | { |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1077 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1078 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 1079 | Py_XDECREF(o1); |
| 1080 | Py_XDECREF(o2); |
| 1081 | return tuple; |
| 1082 | } |
| 1083 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1084 | static PyObject * |
| 1085 | raise_exception(PyObject *self, PyObject *args) |
| 1086 | { |
| 1087 | PyObject *exc; |
| 1088 | PyObject *exc_args, *v; |
| 1089 | int num_args, i; |
| 1090 | |
| 1091 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1092 | &exc, &num_args)) |
| 1093 | return NULL; |
| 1094 | |
| 1095 | exc_args = PyTuple_New(num_args); |
| 1096 | if (exc_args == NULL) |
| 1097 | return NULL; |
| 1098 | for (i = 0; i < num_args; ++i) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1099 | v = PyLong_FromLong(i); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1100 | if (v == NULL) { |
| 1101 | Py_DECREF(exc_args); |
| 1102 | return NULL; |
| 1103 | } |
| 1104 | PyTuple_SET_ITEM(exc_args, i, v); |
| 1105 | } |
| 1106 | PyErr_SetObject(exc, exc_args); |
Michael W. Hudson | f02bcee | 2003-08-15 13:03:30 +0000 | [diff] [blame] | 1107 | Py_DECREF(exc_args); |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1108 | return NULL; |
| 1109 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1110 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1111 | #ifdef WITH_THREAD |
| 1112 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1113 | static PyObject * |
| 1114 | test_datetime_capi(PyObject *self, PyObject *args) { |
| 1115 | if (PyDateTimeAPI) { |
| 1116 | PyErr_SetString(PyExc_AssertionError, |
| 1117 | "PyDateTime_CAPI somehow initialized"); |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | PyDateTime_IMPORT; |
| 1121 | if (PyDateTimeAPI) |
| 1122 | Py_RETURN_NONE; |
| 1123 | else |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1127 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 1128 | * `thread_done` when it's finished. The driver code has to know when the |
| 1129 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 1130 | * may go away when the driver finishes. The former lack of this explicit |
| 1131 | * synchronization caused rare segfaults, so rare that they were seen only |
| 1132 | * on a Mac buildbot (although they were possible on any box). |
| 1133 | */ |
| 1134 | static PyThread_type_lock thread_done = NULL; |
| 1135 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1136 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1137 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1138 | { |
| 1139 | PyObject *rc; |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1140 | int success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1141 | PyGILState_STATE s = PyGILState_Ensure(); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1142 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1143 | success = (rc != NULL); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1144 | Py_XDECREF(rc); |
| 1145 | PyGILState_Release(s); |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1146 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1149 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 1150 | * should be called only from threads spawned by test_thread_state(). |
| 1151 | */ |
| 1152 | static void |
| 1153 | _make_call_from_thread(void *callable) |
| 1154 | { |
| 1155 | _make_call(callable); |
| 1156 | PyThread_release_lock(thread_done); |
| 1157 | } |
| 1158 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1159 | static PyObject * |
| 1160 | test_thread_state(PyObject *self, PyObject *args) |
| 1161 | { |
| 1162 | PyObject *fn; |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1163 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1164 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1165 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 1166 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1167 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1168 | if (!PyCallable_Check(fn)) { |
| 1169 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1170 | fn->ob_type->tp_name); |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1174 | /* Ensure Python is set up for threading */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1175 | PyEval_InitThreads(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1176 | thread_done = PyThread_allocate_lock(); |
| 1177 | if (thread_done == NULL) |
| 1178 | return PyErr_NoMemory(); |
| 1179 | PyThread_acquire_lock(thread_done, 1); |
| 1180 | |
| 1181 | /* Start a new thread with our callback. */ |
| 1182 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1183 | /* Make the callback with the thread lock held by this thread */ |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1184 | success &= _make_call(fn); |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1185 | /* Do it all again, but this time with the thread-lock released */ |
| 1186 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1187 | success &= _make_call(fn); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1188 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1189 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1190 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1191 | /* And once more with and without a thread |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1192 | XXX - should use a lock and work out exactly what we are trying |
| 1193 | to test <wink> |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1194 | */ |
| 1195 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1196 | PyThread_start_new_thread(_make_call_from_thread, fn); |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1197 | success &= _make_call(fn); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1198 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1199 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1200 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1201 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 1202 | PyThread_release_lock(thread_done); |
| 1203 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1204 | PyThread_free_lock(thread_done); |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1205 | if (!success) |
| 1206 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1207 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1208 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1209 | |
| 1210 | /* test Py_AddPendingCalls using threads */ |
| 1211 | static int _pending_callback(void *arg) |
| 1212 | { |
| 1213 | /* we assume the argument is callable object to which we own a reference */ |
| 1214 | PyObject *callable = (PyObject *)arg; |
| 1215 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 1216 | Py_DECREF(callable); |
| 1217 | Py_XDECREF(r); |
| 1218 | return r != NULL ? 0 : -1; |
| 1219 | } |
| 1220 | |
| 1221 | /* The following requests n callbacks to _pending_callback. It can be |
| 1222 | * run from any python thread. |
| 1223 | */ |
| 1224 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1225 | { |
| 1226 | PyObject *callable; |
| 1227 | int r; |
| 1228 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1229 | return NULL; |
| 1230 | |
| 1231 | /* create the reference for the callbackwhile we hold the lock */ |
| 1232 | Py_INCREF(callable); |
| 1233 | |
| 1234 | Py_BEGIN_ALLOW_THREADS |
| 1235 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1236 | Py_END_ALLOW_THREADS |
| 1237 | |
| 1238 | if (r<0) { |
| 1239 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1240 | Py_INCREF(Py_False); |
| 1241 | return Py_False; |
| 1242 | } |
| 1243 | Py_INCREF(Py_True); |
| 1244 | return Py_True; |
| 1245 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1246 | #endif |
| 1247 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 1248 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1249 | static PyObject * |
| 1250 | test_string_from_format(PyObject *self, PyObject *args) |
| 1251 | { |
| 1252 | PyObject *result; |
| 1253 | char *msg; |
| 1254 | |
| 1255 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 1256 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1257 | if (result == NULL) \ |
| 1258 | return NULL; \ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1259 | if (strcmp(_PyUnicode_AsString(result), "1")) { \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1260 | msg = FORMAT " failed at 1"; \ |
| 1261 | goto Fail; \ |
| 1262 | } \ |
| 1263 | Py_DECREF(result) |
| 1264 | |
| 1265 | CHECK_1_FORMAT("%d", int); |
| 1266 | CHECK_1_FORMAT("%ld", long); |
| 1267 | /* The z width modifier was added in Python 2.5. */ |
| 1268 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
| 1269 | |
| 1270 | /* The u type code was added in Python 2.5. */ |
| 1271 | CHECK_1_FORMAT("%u", unsigned int); |
| 1272 | CHECK_1_FORMAT("%lu", unsigned long); |
| 1273 | CHECK_1_FORMAT("%zu", size_t); |
| 1274 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1275 | /* "%lld" and "%llu" support added in Python 2.7. */ |
| 1276 | #ifdef HAVE_LONG_LONG |
| 1277 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 1278 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
| 1279 | #endif |
| 1280 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1281 | Py_RETURN_NONE; |
| 1282 | |
| 1283 | Fail: |
| 1284 | Py_XDECREF(result); |
| 1285 | return raiseTestError("test_string_from_format", msg); |
| 1286 | |
| 1287 | #undef CHECK_1_FORMAT |
| 1288 | } |
| 1289 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1290 | /* This is here to provide a docstring for test_descr. */ |
| 1291 | static PyObject * |
| 1292 | test_with_docstring(PyObject *self) |
| 1293 | { |
| 1294 | Py_RETURN_NONE; |
| 1295 | } |
| 1296 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1297 | /* Test PyOS_string_to_double. */ |
| 1298 | static PyObject * |
| 1299 | test_string_to_double(PyObject *self) { |
| 1300 | double result; |
| 1301 | char *msg; |
| 1302 | |
| 1303 | #define CHECK_STRING(STR, expected) \ |
| 1304 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1305 | if (result == -1.0 && PyErr_Occurred()) \ |
| 1306 | return NULL; \ |
| 1307 | if (result != expected) { \ |
| 1308 | msg = "conversion of " STR " to float failed"; \ |
| 1309 | goto fail; \ |
| 1310 | } |
| 1311 | |
| 1312 | #define CHECK_INVALID(STR) \ |
| 1313 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1314 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 1315 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 1316 | PyErr_Clear(); \ |
| 1317 | else \ |
| 1318 | return NULL; \ |
| 1319 | } \ |
| 1320 | else { \ |
| 1321 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 1322 | goto fail; \ |
| 1323 | } |
| 1324 | |
| 1325 | CHECK_STRING("0.1", 0.1); |
| 1326 | CHECK_STRING("1.234", 1.234); |
| 1327 | CHECK_STRING("-1.35", -1.35); |
| 1328 | CHECK_STRING(".1e01", 1.0); |
| 1329 | CHECK_STRING("2.e-2", 0.02); |
| 1330 | |
| 1331 | CHECK_INVALID(" 0.1"); |
| 1332 | CHECK_INVALID("\t\n-3"); |
| 1333 | CHECK_INVALID(".123 "); |
| 1334 | CHECK_INVALID("3\n"); |
| 1335 | CHECK_INVALID("123abc"); |
| 1336 | |
| 1337 | Py_RETURN_NONE; |
| 1338 | fail: |
| 1339 | return raiseTestError("test_string_to_double", msg); |
| 1340 | #undef CHECK_STRING |
| 1341 | #undef CHECK_INVALID |
| 1342 | } |
| 1343 | |
| 1344 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1345 | /* Coverage testing of capsule objects. */ |
| 1346 | |
| 1347 | static const char *capsule_name = "capsule name"; |
| 1348 | static char *capsule_pointer = "capsule pointer"; |
| 1349 | static char *capsule_context = "capsule context"; |
| 1350 | static const char *capsule_error = NULL; |
| 1351 | static int |
| 1352 | capsule_destructor_call_count = 0; |
| 1353 | |
| 1354 | static void |
| 1355 | capsule_destructor(PyObject *o) { |
| 1356 | capsule_destructor_call_count++; |
| 1357 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 1358 | capsule_error = "context did not match in destructor!"; |
| 1359 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 1360 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 1361 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 1362 | capsule_error = "name did not match in destructor!"; |
| 1363 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 1364 | capsule_error = "pointer did not match in destructor!"; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | typedef struct { |
| 1369 | char *name; |
| 1370 | char *module; |
| 1371 | char *attribute; |
| 1372 | } known_capsule; |
| 1373 | |
| 1374 | static PyObject * |
| 1375 | test_capsule(PyObject *self, PyObject *args) |
| 1376 | { |
| 1377 | PyObject *object; |
| 1378 | const char *error = NULL; |
| 1379 | void *pointer; |
| 1380 | void *pointer2; |
| 1381 | known_capsule known_capsules[] = { |
| 1382 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 1383 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 1384 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 1385 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 1386 | { NULL, NULL }, |
| 1387 | }; |
| 1388 | known_capsule *known = &known_capsules[0]; |
| 1389 | |
| 1390 | #define FAIL(x) { error = (x); goto exit; } |
| 1391 | |
| 1392 | #define CHECK_DESTRUCTOR \ |
| 1393 | if (capsule_error) { \ |
| 1394 | FAIL(capsule_error); \ |
| 1395 | } \ |
| 1396 | else if (!capsule_destructor_call_count) { \ |
| 1397 | FAIL("destructor not called!"); \ |
| 1398 | } \ |
| 1399 | capsule_destructor_call_count = 0; \ |
| 1400 | |
| 1401 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 1402 | PyCapsule_SetContext(object, capsule_context); |
| 1403 | capsule_destructor(object); |
| 1404 | CHECK_DESTRUCTOR; |
| 1405 | Py_DECREF(object); |
| 1406 | CHECK_DESTRUCTOR; |
| 1407 | |
| 1408 | object = PyCapsule_New(known, "ignored", NULL); |
| 1409 | PyCapsule_SetPointer(object, capsule_pointer); |
| 1410 | PyCapsule_SetName(object, capsule_name); |
| 1411 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 1412 | PyCapsule_SetContext(object, capsule_context); |
| 1413 | capsule_destructor(object); |
| 1414 | CHECK_DESTRUCTOR; |
| 1415 | /* intentionally access using the wrong name */ |
| 1416 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 1417 | if (!PyErr_Occurred()) { |
| 1418 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1419 | } |
| 1420 | PyErr_Clear(); |
| 1421 | if (pointer2) { |
| 1422 | if (pointer2 == capsule_pointer) { |
| 1423 | FAIL("PyCapsule_GetPointer should not have" |
| 1424 | " returned the internal pointer!"); |
| 1425 | } else { |
| 1426 | FAIL("PyCapsule_GetPointer should have " |
| 1427 | "returned NULL pointer but did not!"); |
| 1428 | } |
| 1429 | } |
| 1430 | PyCapsule_SetDestructor(object, NULL); |
| 1431 | Py_DECREF(object); |
| 1432 | if (capsule_destructor_call_count) { |
| 1433 | FAIL("destructor called when it should not have been!"); |
| 1434 | } |
| 1435 | |
| 1436 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 1437 | /* yeah, ordinarily I wouldn't do this either, |
| 1438 | but it's fine for this test harness. |
| 1439 | */ |
| 1440 | static char buffer[256]; |
| 1441 | #undef FAIL |
| 1442 | #define FAIL(x) \ |
| 1443 | { \ |
| 1444 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 1445 | x, known->module, known->attribute); \ |
| 1446 | error = buffer; \ |
| 1447 | goto exit; \ |
| 1448 | } \ |
| 1449 | |
| 1450 | PyObject *module = PyImport_ImportModule(known->module); |
| 1451 | if (module) { |
| 1452 | pointer = PyCapsule_Import(known->name, 0); |
| 1453 | if (!pointer) { |
| 1454 | Py_DECREF(module); |
| 1455 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 1456 | } |
| 1457 | object = PyObject_GetAttrString(module, known->attribute); |
| 1458 | if (!object) { |
| 1459 | Py_DECREF(module); |
| 1460 | return NULL; |
| 1461 | } |
| 1462 | pointer2 = PyCapsule_GetPointer(object, |
| 1463 | "weebles wobble but they don't fall down"); |
| 1464 | if (!PyErr_Occurred()) { |
| 1465 | Py_DECREF(object); |
| 1466 | Py_DECREF(module); |
| 1467 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1468 | } |
| 1469 | PyErr_Clear(); |
| 1470 | if (pointer2) { |
| 1471 | Py_DECREF(module); |
| 1472 | Py_DECREF(object); |
| 1473 | if (pointer2 == pointer) { |
| 1474 | FAIL("PyCapsule_GetPointer should not have" |
| 1475 | " returned its internal pointer!"); |
| 1476 | } else { |
| 1477 | FAIL("PyCapsule_GetPointer should have" |
| 1478 | " returned NULL pointer but did not!"); |
| 1479 | } |
| 1480 | } |
| 1481 | Py_DECREF(object); |
| 1482 | Py_DECREF(module); |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | exit: |
| 1487 | if (error) { |
| 1488 | return raiseTestError("test_capsule", error); |
| 1489 | } |
Mark Dickinson | 6d138f1 | 2009-12-13 20:03:21 +0000 | [diff] [blame] | 1490 | /* 13/12/2009: something is causing test_capi to fail occasionally on |
| 1491 | the Solaris buildbot, with the output: |
| 1492 | |
| 1493 | internal test_L_code |
| 1494 | internal test_Z_code |
| 1495 | internal test_bug_7414 |
| 1496 | internal test_capsule |
| 1497 | XXX undetected error |
| 1498 | internaltest test_capi crashed -- <class 'ImportError'>: No module named datetime |
| 1499 | |
| 1500 | It seems possible that test_capsule is raising an exception but |
| 1501 | failing to return NULL. Do a PyErr_Occurred check to find out. |
| 1502 | */ |
| 1503 | if (PyErr_Occurred()) |
| 1504 | return NULL; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1505 | Py_RETURN_NONE; |
| 1506 | #undef FAIL |
| 1507 | } |
| 1508 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1509 | #ifdef HAVE_GETTIMEOFDAY |
| 1510 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 1511 | static void print_delta(int test, struct timeval *s, struct timeval *e) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1512 | { |
| 1513 | e->tv_sec -= s->tv_sec; |
| 1514 | e->tv_usec -= s->tv_usec; |
| 1515 | if (e->tv_usec < 0) { |
| 1516 | e->tv_sec -=1; |
| 1517 | e->tv_usec += 1000000; |
| 1518 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1519 | printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | static PyObject * |
| 1523 | profile_int(PyObject *self, PyObject* args) |
| 1524 | { |
| 1525 | int i, k; |
| 1526 | struct timeval start, stop; |
| 1527 | PyObject *single, **multiple, *op1, *result; |
| 1528 | |
| 1529 | /* Test 1: Allocate and immediately deallocate |
| 1530 | many small integers */ |
| 1531 | gettimeofday(&start, NULL); |
| 1532 | for(k=0; k < 20000; k++) |
| 1533 | for(i=0; i < 1000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1534 | single = PyLong_FromLong(i); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1535 | Py_DECREF(single); |
| 1536 | } |
| 1537 | gettimeofday(&stop, NULL); |
| 1538 | print_delta(1, &start, &stop); |
| 1539 | |
| 1540 | /* Test 2: Allocate and immediately deallocate |
| 1541 | many large integers */ |
| 1542 | gettimeofday(&start, NULL); |
| 1543 | for(k=0; k < 20000; k++) |
| 1544 | for(i=0; i < 1000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1545 | single = PyLong_FromLong(i+1000000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1546 | Py_DECREF(single); |
| 1547 | } |
| 1548 | gettimeofday(&stop, NULL); |
| 1549 | print_delta(2, &start, &stop); |
| 1550 | |
| 1551 | /* Test 3: Allocate a few integers, then release |
| 1552 | them all simultaneously. */ |
| 1553 | multiple = malloc(sizeof(PyObject*) * 1000); |
| 1554 | gettimeofday(&start, NULL); |
| 1555 | for(k=0; k < 20000; k++) { |
| 1556 | for(i=0; i < 1000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1557 | multiple[i] = PyLong_FromLong(i+1000000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1558 | } |
| 1559 | for(i=0; i < 1000; i++) { |
| 1560 | Py_DECREF(multiple[i]); |
| 1561 | } |
| 1562 | } |
| 1563 | gettimeofday(&stop, NULL); |
| 1564 | print_delta(3, &start, &stop); |
| 1565 | |
| 1566 | /* Test 4: Allocate many integers, then release |
| 1567 | them all simultaneously. */ |
| 1568 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 1569 | gettimeofday(&start, NULL); |
| 1570 | for(k=0; k < 20; k++) { |
| 1571 | for(i=0; i < 1000000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1572 | multiple[i] = PyLong_FromLong(i+1000000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1573 | } |
| 1574 | for(i=0; i < 1000000; i++) { |
| 1575 | Py_DECREF(multiple[i]); |
| 1576 | } |
| 1577 | } |
| 1578 | gettimeofday(&stop, NULL); |
| 1579 | print_delta(4, &start, &stop); |
| 1580 | |
| 1581 | /* Test 5: Allocate many integers < 32000 */ |
| 1582 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 1583 | gettimeofday(&start, NULL); |
| 1584 | for(k=0; k < 10; k++) { |
| 1585 | for(i=0; i < 1000000; i++) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1586 | multiple[i] = PyLong_FromLong(i+1000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1587 | } |
| 1588 | for(i=0; i < 1000000; i++) { |
| 1589 | Py_DECREF(multiple[i]); |
| 1590 | } |
| 1591 | } |
| 1592 | gettimeofday(&stop, NULL); |
| 1593 | print_delta(5, &start, &stop); |
| 1594 | |
| 1595 | /* Test 6: Perform small int addition */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1596 | op1 = PyLong_FromLong(1); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1597 | gettimeofday(&start, NULL); |
| 1598 | for(i=0; i < 10000000; i++) { |
| 1599 | result = PyNumber_Add(op1, op1); |
| 1600 | Py_DECREF(result); |
| 1601 | } |
| 1602 | gettimeofday(&stop, NULL); |
| 1603 | Py_DECREF(op1); |
| 1604 | print_delta(6, &start, &stop); |
| 1605 | |
| 1606 | /* Test 7: Perform medium int addition */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1607 | op1 = PyLong_FromLong(1000); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1608 | gettimeofday(&start, NULL); |
| 1609 | for(i=0; i < 10000000; i++) { |
| 1610 | result = PyNumber_Add(op1, op1); |
| 1611 | Py_DECREF(result); |
| 1612 | } |
| 1613 | gettimeofday(&stop, NULL); |
| 1614 | Py_DECREF(op1); |
| 1615 | print_delta(7, &start, &stop); |
| 1616 | |
| 1617 | Py_INCREF(Py_None); |
| 1618 | return Py_None; |
| 1619 | } |
| 1620 | #endif |
| 1621 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1622 | /* To test the format of tracebacks as printed out. */ |
| 1623 | static PyObject * |
| 1624 | traceback_print(PyObject *self, PyObject *args) |
| 1625 | { |
| 1626 | PyObject *file; |
| 1627 | PyObject *traceback; |
| 1628 | int result; |
| 1629 | |
| 1630 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 1631 | &traceback, &file)) |
| 1632 | return NULL; |
| 1633 | |
| 1634 | result = PyTraceBack_Print(traceback, file); |
| 1635 | if (result < 0) |
| 1636 | return NULL; |
| 1637 | Py_RETURN_NONE; |
| 1638 | } |
| 1639 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1640 | /* To test the format of exceptions as printed out. */ |
| 1641 | static PyObject * |
| 1642 | exception_print(PyObject *self, PyObject *args) |
| 1643 | { |
| 1644 | PyObject *value; |
| 1645 | PyObject *tb; |
| 1646 | |
| 1647 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 1648 | &value)) |
| 1649 | return NULL; |
Benjamin Peterson | 6784eb7 | 2008-08-23 20:32:27 +0000 | [diff] [blame] | 1650 | if (!PyExceptionInstance_Check(value)) { |
| 1651 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 1652 | return NULL; |
| 1653 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1654 | |
| 1655 | tb = PyException_GetTraceback(value); |
| 1656 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 1657 | Py_XDECREF(tb); |
| 1658 | |
| 1659 | Py_RETURN_NONE; |
| 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 1664 | |
| 1665 | /* reliably raise a MemoryError */ |
| 1666 | static PyObject * |
| 1667 | raise_memoryerror(PyObject *self) |
| 1668 | { |
| 1669 | PyErr_NoMemory(); |
| 1670 | return NULL; |
| 1671 | } |
| 1672 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1673 | /* Issue 6012 */ |
| 1674 | static PyObject *str1, *str2; |
| 1675 | static int |
| 1676 | failing_converter(PyObject *obj, void *arg) |
| 1677 | { |
| 1678 | /* Clone str1, then let the conversion fail. */ |
| 1679 | assert(str1); |
| 1680 | str2 = str1; |
| 1681 | Py_INCREF(str2); |
| 1682 | return 0; |
| 1683 | } |
| 1684 | static PyObject* |
| 1685 | argparsing(PyObject *o, PyObject *args) |
| 1686 | { |
| 1687 | PyObject *res; |
| 1688 | str1 = str2 = NULL; |
| 1689 | if (!PyArg_ParseTuple(args, "O&O&", |
| 1690 | PyUnicode_FSConverter, &str1, |
| 1691 | failing_converter, &str2)) { |
| 1692 | if (!str2) |
| 1693 | /* argument converter not called? */ |
| 1694 | return NULL; |
| 1695 | /* Should be 1 */ |
| 1696 | res = PyLong_FromLong(Py_REFCNT(str2)); |
| 1697 | Py_DECREF(str2); |
| 1698 | PyErr_Clear(); |
| 1699 | return res; |
| 1700 | } |
| 1701 | Py_RETURN_NONE; |
| 1702 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 1703 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 1704 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 1705 | static PyObject * |
| 1706 | code_newempty(PyObject *self, PyObject *args) |
| 1707 | { |
| 1708 | const char *filename; |
| 1709 | const char *funcname; |
| 1710 | int firstlineno; |
| 1711 | |
| 1712 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 1713 | &filename, &funcname, &firstlineno)) |
| 1714 | return NULL; |
| 1715 | |
| 1716 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
| 1717 | } |
| 1718 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1719 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 1720 | Run via Lib/test/test_exceptions.py */ |
| 1721 | static PyObject * |
| 1722 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1723 | { |
| 1724 | const char *name; |
| 1725 | const char *doc = NULL; |
| 1726 | PyObject *base = NULL; |
| 1727 | PyObject *dict = NULL; |
| 1728 | |
| 1729 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
| 1730 | |
| 1731 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 1732 | "s|sOO:make_exception_with_doc", kwlist, |
| 1733 | &name, &doc, &base, &dict)) |
| 1734 | return NULL; |
| 1735 | |
| 1736 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
| 1737 | } |
| 1738 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1739 | static PyMethodDef TestMethods[] = { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1740 | {"raise_exception", raise_exception, METH_VARARGS}, |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 1741 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1742 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1743 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1744 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 1745 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 1746 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1747 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 1748 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 1749 | METH_NOARGS}, |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1750 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1751 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1752 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1753 | {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS}, |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1754 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1755 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1756 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 1757 | PyDoc_STR("This is a pretty normal docstring.")}, |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1758 | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
| 1759 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1760 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1761 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 1762 | METH_VARARGS|METH_KEYWORDS}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1763 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 1764 | {"getargs_B", getargs_B, METH_VARARGS}, |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1765 | {"getargs_h", getargs_h, METH_VARARGS}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1766 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 1767 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 1768 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 1769 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 1770 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 1771 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1772 | #ifdef HAVE_LONG_LONG |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1773 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 1774 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 1775 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1776 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1777 | {"codec_incrementalencoder", |
| 1778 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 1779 | {"codec_incrementaldecoder", |
| 1780 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1781 | #endif |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1782 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1783 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1784 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1785 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1786 | #ifdef WITH_THREAD |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1787 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1788 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1789 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1790 | #ifdef HAVE_GETTIMEOFDAY |
| 1791 | {"profile_int", profile_int, METH_NOARGS}, |
| 1792 | #endif |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1793 | {"traceback_print", traceback_print, METH_VARARGS}, |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1794 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1795 | {"argparsing", argparsing, METH_VARARGS}, |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 1796 | {"code_newempty", code_newempty, METH_VARARGS}, |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 1797 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 1798 | METH_VARARGS | METH_KEYWORDS}, |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1799 | {NULL, NULL} /* sentinel */ |
| 1800 | }; |
| 1801 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1802 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 1803 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1804 | typedef struct { |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1805 | char bool_member; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1806 | char byte_member; |
| 1807 | unsigned char ubyte_member; |
| 1808 | short short_member; |
| 1809 | unsigned short ushort_member; |
| 1810 | int int_member; |
| 1811 | unsigned int uint_member; |
| 1812 | long long_member; |
| 1813 | unsigned long ulong_member; |
| 1814 | float float_member; |
| 1815 | double double_member; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1816 | #ifdef HAVE_LONG_LONG |
| 1817 | PY_LONG_LONG longlong_member; |
| 1818 | unsigned PY_LONG_LONG ulonglong_member; |
| 1819 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1820 | } all_structmembers; |
| 1821 | |
| 1822 | typedef struct { |
| 1823 | PyObject_HEAD |
| 1824 | all_structmembers structmembers; |
| 1825 | } test_structmembers; |
| 1826 | |
| 1827 | static struct PyMemberDef test_members[] = { |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1828 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1829 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 1830 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 1831 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 1832 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 1833 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 1834 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 1835 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 1836 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 1837 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 1838 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1839 | #ifdef HAVE_LONG_LONG |
| 1840 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 1841 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
| 1842 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1843 | {NULL} |
| 1844 | }; |
| 1845 | |
| 1846 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1847 | static PyObject * |
| 1848 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 1849 | { |
| 1850 | static char *keywords[] = { |
| 1851 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 1852 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", |
| 1853 | "T_FLOAT", "T_DOUBLE", |
| 1854 | #ifdef HAVE_LONG_LONG |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1855 | "T_LONGLONG", "T_ULONGLONG", |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1856 | #endif |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1857 | NULL}; |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1858 | static char *fmt = "|bbBhHiIlkfd" |
| 1859 | #ifdef HAVE_LONG_LONG |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1860 | "LK" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1861 | #endif |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1862 | ; |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1863 | test_structmembers *ob; |
| 1864 | ob = PyObject_New(test_structmembers, type); |
| 1865 | if (ob == NULL) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1866 | return NULL; |
| 1867 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1868 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1869 | &ob->structmembers.bool_member, |
| 1870 | &ob->structmembers.byte_member, |
| 1871 | &ob->structmembers.ubyte_member, |
| 1872 | &ob->structmembers.short_member, |
| 1873 | &ob->structmembers.ushort_member, |
| 1874 | &ob->structmembers.int_member, |
| 1875 | &ob->structmembers.uint_member, |
| 1876 | &ob->structmembers.long_member, |
| 1877 | &ob->structmembers.ulong_member, |
| 1878 | &ob->structmembers.float_member, |
| 1879 | &ob->structmembers.double_member |
| 1880 | #ifdef HAVE_LONG_LONG |
| 1881 | , &ob->structmembers.longlong_member, |
| 1882 | &ob->structmembers.ulonglong_member |
| 1883 | #endif |
| 1884 | )) { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1885 | Py_DECREF(ob); |
| 1886 | return NULL; |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1887 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1888 | return (PyObject *)ob; |
| 1889 | } |
| 1890 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1891 | static void |
| 1892 | test_structmembers_free(PyObject *ob) |
| 1893 | { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1894 | PyObject_FREE(ob); |
| 1895 | } |
| 1896 | |
| 1897 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1898 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1899 | "test_structmembersType", |
| 1900 | sizeof(test_structmembers), /* tp_basicsize */ |
| 1901 | 0, /* tp_itemsize */ |
| 1902 | test_structmembers_free, /* destructor tp_dealloc */ |
| 1903 | 0, /* tp_print */ |
| 1904 | 0, /* tp_getattr */ |
| 1905 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1906 | 0, /* tp_reserved */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1907 | 0, /* tp_repr */ |
| 1908 | 0, /* tp_as_number */ |
| 1909 | 0, /* tp_as_sequence */ |
| 1910 | 0, /* tp_as_mapping */ |
| 1911 | 0, /* tp_hash */ |
| 1912 | 0, /* tp_call */ |
| 1913 | 0, /* tp_str */ |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1914 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1915 | PyObject_GenericSetAttr, /* tp_setattro */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1916 | 0, /* tp_as_buffer */ |
| 1917 | 0, /* tp_flags */ |
| 1918 | "Type containing all structmember types", |
| 1919 | 0, /* traverseproc tp_traverse */ |
| 1920 | 0, /* tp_clear */ |
| 1921 | 0, /* tp_richcompare */ |
| 1922 | 0, /* tp_weaklistoffset */ |
| 1923 | 0, /* tp_iter */ |
| 1924 | 0, /* tp_iternext */ |
| 1925 | 0, /* tp_methods */ |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1926 | test_members, /* tp_members */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1927 | 0, |
| 1928 | 0, |
| 1929 | 0, |
| 1930 | 0, |
| 1931 | 0, |
| 1932 | 0, |
| 1933 | 0, |
| 1934 | 0, |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1935 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1936 | }; |
| 1937 | |
| 1938 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1939 | |
| 1940 | static struct PyModuleDef _testcapimodule = { |
| 1941 | PyModuleDef_HEAD_INIT, |
| 1942 | "_testcapi", |
| 1943 | NULL, |
| 1944 | -1, |
| 1945 | TestMethods, |
| 1946 | NULL, |
| 1947 | NULL, |
| 1948 | NULL, |
| 1949 | NULL |
| 1950 | }; |
| 1951 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1952 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1953 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1954 | { |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1955 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1956 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1957 | m = PyModule_Create(&_testcapimodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1958 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1959 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1960 | |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 1961 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
| 1962 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1963 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1964 | Py_INCREF(&test_structmembersType); |
| 1965 | PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType); |
| 1966 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1967 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 1968 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 1969 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 1970 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 1971 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 1972 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1973 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 1974 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1975 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1976 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 1977 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1978 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 1979 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 1980 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 1981 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 1982 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1983 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 1984 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 1985 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1986 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 1987 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 1988 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
Benjamin Peterson | 9b6df6a | 2008-10-16 23:56:29 +0000 | [diff] [blame] | 1989 | Py_INCREF(&PyInstanceMethod_Type); |
| 1990 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1991 | |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 1992 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1993 | Py_INCREF(TestError); |
| 1994 | PyModule_AddObject(m, "error", TestError); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1995 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1996 | } |