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