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