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