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" |
Serhiy Storchaka | 4068b01 | 2015-02-06 08:56:33 +0200 | [diff] [blame] | 12 | #include "marshal.h" |
Victor Stinner | d2aff60 | 2017-05-09 13:57:20 +0200 | [diff] [blame^] | 13 | #include <signal.h> |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 14 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 15 | #ifdef WITH_THREAD |
| 16 | #include "pythread.h" |
| 17 | #endif /* WITH_THREAD */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 18 | static PyObject *TestError; /* set to exception object in init */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 19 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 20 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 21 | |
| 22 | static PyObject * |
| 23 | raiseTestError(const char* test_name, const char* msg) |
| 24 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 25 | char buf[2048]; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 26 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 27 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 28 | PyErr_SetString(TestError, "internal error msg too large"); |
| 29 | else { |
| 30 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
| 31 | PyErr_SetString(TestError, buf); |
| 32 | } |
| 33 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 36 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 37 | |
| 38 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 39 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 40 | platforms have these hardcoded. Better safe than sorry. |
| 41 | */ |
| 42 | static PyObject* |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 43 | sizeof_error(const char* fatname, const char* typname, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 44 | int expected, int got) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 45 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 46 | char buf[1024]; |
| 47 | PyOS_snprintf(buf, sizeof(buf), |
| 48 | "%.200s #define == %d but sizeof(%.200s) == %d", |
| 49 | fatname, expected, typname, got); |
| 50 | PyErr_SetString(TestError, buf); |
| 51 | return (PyObject*)NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 55 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 56 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 57 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 58 | if (FATNAME != sizeof(TYPE)) \ |
| 59 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 61 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 62 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 63 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 64 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 65 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 66 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 67 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | #undef CHECK_SIZEOF |
| 71 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 72 | Py_INCREF(Py_None); |
| 73 | return Py_None; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 76 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 77 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 78 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 79 | PyObject* list; |
| 80 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 81 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 82 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 83 | #define NLIST 30 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 84 | list = PyList_New(NLIST); |
| 85 | if (list == (PyObject*)NULL) |
| 86 | return (PyObject*)NULL; |
| 87 | /* list = range(NLIST) */ |
| 88 | for (i = 0; i < NLIST; ++i) { |
| 89 | PyObject* anint = PyInt_FromLong(i); |
| 90 | if (anint == (PyObject*)NULL) { |
| 91 | Py_DECREF(list); |
| 92 | return (PyObject*)NULL; |
| 93 | } |
| 94 | PyList_SET_ITEM(list, i, anint); |
| 95 | } |
| 96 | /* list.reverse(), via PyList_Reverse() */ |
| 97 | i = PyList_Reverse(list); /* should not blow up! */ |
| 98 | if (i != 0) { |
| 99 | Py_DECREF(list); |
| 100 | return (PyObject*)NULL; |
| 101 | } |
| 102 | /* Check that list == range(29, -1, -1) now */ |
| 103 | for (i = 0; i < NLIST; ++i) { |
| 104 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 105 | if (PyInt_AS_LONG(anint) != NLIST-1-i) { |
| 106 | PyErr_SetString(TestError, |
| 107 | "test_list_api: reverse screwed up"); |
| 108 | Py_DECREF(list); |
| 109 | return (PyObject*)NULL; |
| 110 | } |
| 111 | } |
| 112 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 113 | #undef NLIST |
| 114 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 115 | Py_INCREF(Py_None); |
| 116 | return Py_None; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 119 | static int |
| 120 | test_dict_inner(int count) |
| 121 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 122 | Py_ssize_t pos = 0, iterations = 0; |
| 123 | int i; |
| 124 | PyObject *dict = PyDict_New(); |
| 125 | PyObject *v, *k; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 126 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 127 | if (dict == NULL) |
| 128 | return -1; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 130 | for (i = 0; i < count; i++) { |
| 131 | v = PyInt_FromLong(i); |
| 132 | PyDict_SetItem(dict, v, v); |
| 133 | Py_DECREF(v); |
| 134 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 136 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 137 | PyObject *o; |
| 138 | iterations++; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 140 | i = PyInt_AS_LONG(v) + 1; |
| 141 | o = PyInt_FromLong(i); |
| 142 | if (o == NULL) |
| 143 | return -1; |
| 144 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 145 | Py_DECREF(o); |
| 146 | return -1; |
| 147 | } |
| 148 | Py_DECREF(o); |
| 149 | } |
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 | Py_DECREF(dict); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 153 | if (iterations != count) { |
| 154 | PyErr_SetString( |
| 155 | TestError, |
| 156 | "test_dict_iteration: dict iteration went wrong "); |
| 157 | return -1; |
| 158 | } else { |
| 159 | return 0; |
| 160 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 164 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 165 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 166 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 168 | for (i = 0; i < 200; i++) { |
| 169 | if (test_dict_inner(i) < 0) { |
| 170 | return NULL; |
| 171 | } |
| 172 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 174 | Py_INCREF(Py_None); |
| 175 | return Py_None; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 178 | |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 179 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 180 | * PyType_Ready if it hasn't already been called |
| 181 | */ |
| 182 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 183 | PyObject_HEAD_INIT(NULL) |
| 184 | 0, /* Number of items for varobject */ |
| 185 | "hashinheritancetester", /* Name of this type */ |
| 186 | sizeof(PyObject), /* Basic object size */ |
| 187 | 0, /* Item size for varobject */ |
| 188 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 189 | 0, /* tp_print */ |
| 190 | 0, /* tp_getattr */ |
| 191 | 0, /* tp_setattr */ |
| 192 | 0, /* tp_compare */ |
| 193 | 0, /* tp_repr */ |
| 194 | 0, /* tp_as_number */ |
| 195 | 0, /* tp_as_sequence */ |
| 196 | 0, /* tp_as_mapping */ |
| 197 | 0, /* tp_hash */ |
| 198 | 0, /* tp_call */ |
| 199 | 0, /* tp_str */ |
| 200 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 201 | 0, /* tp_setattro */ |
| 202 | 0, /* tp_as_buffer */ |
| 203 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 204 | 0, /* tp_doc */ |
| 205 | 0, /* tp_traverse */ |
| 206 | 0, /* tp_clear */ |
| 207 | 0, /* tp_richcompare */ |
| 208 | 0, /* tp_weaklistoffset */ |
| 209 | 0, /* tp_iter */ |
| 210 | 0, /* tp_iternext */ |
| 211 | 0, /* tp_methods */ |
| 212 | 0, /* tp_members */ |
| 213 | 0, /* tp_getset */ |
| 214 | 0, /* tp_base */ |
| 215 | 0, /* tp_dict */ |
| 216 | 0, /* tp_descr_get */ |
| 217 | 0, /* tp_descr_set */ |
| 218 | 0, /* tp_dictoffset */ |
| 219 | 0, /* tp_init */ |
| 220 | 0, /* tp_alloc */ |
| 221 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | static PyObject* |
| 225 | test_lazy_hash_inheritance(PyObject* self) |
| 226 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 227 | PyTypeObject *type; |
| 228 | PyObject *obj; |
| 229 | long hash; |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 230 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 231 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | a98c8e1 | 2009-05-05 21:09:21 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 233 | if (type->tp_dict != NULL) |
| 234 | /* The type has already been initialized. This probably means |
| 235 | -R is being used. */ |
| 236 | Py_RETURN_NONE; |
Benjamin Peterson | a98c8e1 | 2009-05-05 21:09:21 +0000 | [diff] [blame] | 237 | |
| 238 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 239 | obj = PyObject_New(PyObject, type); |
| 240 | if (obj == NULL) { |
| 241 | PyErr_Clear(); |
| 242 | PyErr_SetString( |
| 243 | TestError, |
| 244 | "test_lazy_hash_inheritance: failed to create object"); |
| 245 | return NULL; |
| 246 | } |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 248 | if (type->tp_dict != NULL) { |
| 249 | PyErr_SetString( |
| 250 | TestError, |
| 251 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 252 | Py_DECREF(obj); |
| 253 | return NULL; |
| 254 | } |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 256 | hash = PyObject_Hash(obj); |
| 257 | if ((hash == -1) && PyErr_Occurred()) { |
| 258 | PyErr_Clear(); |
| 259 | PyErr_SetString( |
| 260 | TestError, |
| 261 | "test_lazy_hash_inheritance: could not hash object"); |
| 262 | Py_DECREF(obj); |
| 263 | return NULL; |
| 264 | } |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 265 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 266 | if (type->tp_dict == NULL) { |
| 267 | PyErr_SetString( |
| 268 | TestError, |
| 269 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 270 | Py_DECREF(obj); |
| 271 | return NULL; |
| 272 | } |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 274 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 275 | PyErr_SetString( |
| 276 | TestError, |
| 277 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 278 | Py_DECREF(obj); |
| 279 | return NULL; |
| 280 | } |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 282 | Py_DECREF(obj); |
Benjamin Peterson | a98c8e1 | 2009-05-05 21:09:21 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 284 | Py_RETURN_NONE; |
Nick Coghlan | 180e400 | 2008-12-30 01:18:48 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 288 | /* Issue #7385: Check that memoryview() does not crash |
| 289 | * when bf_getbuffer returns an error |
| 290 | */ |
| 291 | |
| 292 | static int |
| 293 | broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags) |
| 294 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 295 | PyErr_SetString( |
| 296 | TestError, |
| 297 | "test_broken_memoryview: expected error in bf_getbuffer"); |
| 298 | return -1; |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static PyBufferProcs memoryviewtester_as_buffer = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 302 | 0, /* bf_getreadbuffer */ |
| 303 | 0, /* bf_getwritebuffer */ |
| 304 | 0, /* bf_getsegcount */ |
| 305 | 0, /* bf_getcharbuffer */ |
| 306 | (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */ |
| 307 | 0, /* bf_releasebuffer */ |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | static PyTypeObject _MemoryViewTester_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 311 | PyObject_HEAD_INIT(NULL) |
| 312 | 0, /* Number of items for varobject */ |
| 313 | "memoryviewtester", /* Name of this type */ |
| 314 | sizeof(PyObject), /* Basic object size */ |
| 315 | 0, /* Item size for varobject */ |
| 316 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 317 | 0, /* tp_print */ |
| 318 | 0, /* tp_getattr */ |
| 319 | 0, /* tp_setattr */ |
| 320 | 0, /* tp_compare */ |
| 321 | 0, /* tp_repr */ |
| 322 | 0, /* tp_as_number */ |
| 323 | 0, /* tp_as_sequence */ |
| 324 | 0, /* tp_as_mapping */ |
| 325 | 0, /* tp_hash */ |
| 326 | 0, /* tp_call */ |
| 327 | 0, /* tp_str */ |
| 328 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 329 | 0, /* tp_setattro */ |
| 330 | &memoryviewtester_as_buffer, /* tp_as_buffer */ |
| 331 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */ |
| 332 | 0, /* tp_doc */ |
| 333 | 0, /* tp_traverse */ |
| 334 | 0, /* tp_clear */ |
| 335 | 0, /* tp_richcompare */ |
| 336 | 0, /* tp_weaklistoffset */ |
| 337 | 0, /* tp_iter */ |
| 338 | 0, /* tp_iternext */ |
| 339 | 0, /* tp_methods */ |
| 340 | 0, /* tp_members */ |
| 341 | 0, /* tp_getset */ |
| 342 | 0, /* tp_base */ |
| 343 | 0, /* tp_dict */ |
| 344 | 0, /* tp_descr_get */ |
| 345 | 0, /* tp_descr_set */ |
| 346 | 0, /* tp_dictoffset */ |
| 347 | 0, /* tp_init */ |
| 348 | 0, /* tp_alloc */ |
| 349 | PyType_GenericNew, /* tp_new */ |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | static PyObject* |
| 353 | test_broken_memoryview(PyObject* self) |
| 354 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 355 | PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type); |
| 356 | PyObject *res; |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 357 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 358 | if (obj == NULL) { |
| 359 | PyErr_Clear(); |
| 360 | PyErr_SetString( |
| 361 | TestError, |
| 362 | "test_broken_memoryview: failed to create object"); |
| 363 | return NULL; |
| 364 | } |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 366 | res = PyMemoryView_FromObject(obj); |
| 367 | if (res || !PyErr_Occurred()){ |
| 368 | PyErr_SetString( |
| 369 | TestError, |
| 370 | "test_broken_memoryview: memoryview() didn't raise an Exception"); |
| 371 | Py_XDECREF(res); |
| 372 | Py_DECREF(obj); |
| 373 | return NULL; |
| 374 | } |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 375 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 376 | PyErr_Clear(); |
| 377 | Py_DECREF(obj); |
| 378 | Py_RETURN_NONE; |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Stefan Krah | 5606cd9 | 2015-01-30 20:11:10 +0100 | [diff] [blame] | 381 | static PyObject * |
| 382 | test_to_contiguous(PyObject* self, PyObject *noargs) |
| 383 | { |
| 384 | int data[9] = {0, -1, 1, -1, 2, -1, 3, -1, 4}; |
| 385 | int result[5]; |
| 386 | Py_ssize_t itemsize = sizeof(int); |
| 387 | Py_ssize_t shape = 5; |
| 388 | Py_ssize_t strides = 2 * itemsize; |
| 389 | Py_buffer view = { |
| 390 | data, |
| 391 | NULL, |
| 392 | 5 * itemsize, |
| 393 | itemsize, |
| 394 | 1, |
| 395 | 1, |
| 396 | NULL, |
| 397 | &shape, |
| 398 | &strides, |
| 399 | NULL, |
| 400 | {0, 0}, |
| 401 | NULL |
| 402 | }; |
| 403 | int i; |
| 404 | |
| 405 | PyBuffer_ToContiguous(result, &view, view.len, 'C'); |
| 406 | for (i = 0; i < 5; i++) { |
| 407 | if (result[i] != i) { |
| 408 | PyErr_SetString(TestError, |
| 409 | "test_to_contiguous: incorrect result"); |
| 410 | return NULL; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | view.buf = &data[8]; |
| 415 | view.strides[0] = -2 * itemsize; |
| 416 | |
| 417 | PyBuffer_ToContiguous(result, &view, view.len, 'C'); |
| 418 | for (i = 0; i < 5; i++) { |
| 419 | if (result[i] != 4-i) { |
| 420 | PyErr_SetString(TestError, |
| 421 | "test_to_contiguous: incorrect result"); |
| 422 | return NULL; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | Py_RETURN_NONE; |
| 427 | } |
Antoine Pitrou | 526e421 | 2010-02-02 22:36:17 +0000 | [diff] [blame] | 428 | |
Stefan Krah | 0a7fc53 | 2015-02-01 16:10:35 +0100 | [diff] [blame] | 429 | static PyObject * |
| 430 | test_from_contiguous(PyObject* self, PyObject *noargs) |
| 431 | { |
| 432 | int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 433 | int init[5] = {0, 1, 2, 3, 4}; |
| 434 | Py_ssize_t itemsize = sizeof(int); |
| 435 | Py_ssize_t shape = 5; |
| 436 | Py_ssize_t strides = 2 * itemsize; |
| 437 | Py_buffer view = { |
| 438 | data, |
| 439 | NULL, |
| 440 | 5 * itemsize, |
| 441 | itemsize, |
| 442 | 1, |
| 443 | 1, |
| 444 | NULL, |
| 445 | &shape, |
| 446 | &strides, |
| 447 | NULL, |
| 448 | {0, 0}, |
| 449 | NULL |
| 450 | }; |
| 451 | int *ptr; |
| 452 | int i; |
| 453 | |
| 454 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 455 | ptr = view.buf; |
| 456 | for (i = 0; i < 5; i++) { |
| 457 | if (ptr[2*i] != i) { |
| 458 | PyErr_SetString(TestError, |
| 459 | "test_from_contiguous: incorrect result"); |
| 460 | return NULL; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | view.buf = &data[8]; |
| 465 | view.strides[0] = -2 * itemsize; |
| 466 | |
| 467 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 468 | ptr = view.buf; |
| 469 | for (i = 0; i < 5; i++) { |
| 470 | if (*(ptr-2*i) != i) { |
| 471 | PyErr_SetString(TestError, |
| 472 | "test_from_contiguous: incorrect result"); |
| 473 | return NULL; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | Py_RETURN_NONE; |
| 478 | } |
| 479 | |
| 480 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 481 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 482 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 483 | |
| 484 | Note that the meat of the test is contained in testcapi_long.h. |
| 485 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 486 | 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] | 487 | dependence on type names makes it impossible to use a parameterized |
| 488 | function. A giant macro would be even worse than this. A C++ template |
| 489 | would be perfect. |
| 490 | |
| 491 | The "report an error" functions are deliberately not part of the #include |
| 492 | file: if the test fails, you can set a breakpoint in the appropriate |
| 493 | error function directly, and crawl back from there in the debugger. |
| 494 | */ |
| 495 | |
| 496 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 497 | |
| 498 | static PyObject * |
| 499 | raise_test_long_error(const char* msg) |
| 500 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 501 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 504 | #define TESTNAME test_long_api_inner |
| 505 | #define TYPENAME long |
| 506 | #define F_S_TO_PY PyLong_FromLong |
| 507 | #define F_PY_TO_S PyLong_AsLong |
| 508 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 509 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 510 | |
| 511 | #include "testcapi_long.h" |
| 512 | |
| 513 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 514 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 515 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 516 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | #undef TESTNAME |
| 520 | #undef TYPENAME |
| 521 | #undef F_S_TO_PY |
| 522 | #undef F_PY_TO_S |
| 523 | #undef F_U_TO_PY |
| 524 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 525 | |
| 526 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 527 | |
| 528 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 529 | raise_test_longlong_error(const char* msg) |
| 530 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 531 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 534 | #define TESTNAME test_longlong_api_inner |
| 535 | #define TYPENAME PY_LONG_LONG |
| 536 | #define F_S_TO_PY PyLong_FromLongLong |
| 537 | #define F_PY_TO_S PyLong_AsLongLong |
| 538 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 539 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 540 | |
| 541 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 542 | |
| 543 | static PyObject * |
Skip Montanaro | 9582c14 | 2006-04-18 01:01:41 +0000 | [diff] [blame] | 544 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 545 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 546 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 549 | #undef TESTNAME |
| 550 | #undef TYPENAME |
| 551 | #undef F_S_TO_PY |
| 552 | #undef F_PY_TO_S |
| 553 | #undef F_U_TO_PY |
| 554 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 555 | |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 556 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 557 | is tested by test_long_api_inner. This test will concentrate on proper |
| 558 | handling of overflow. |
| 559 | */ |
| 560 | |
| 561 | static PyObject * |
| 562 | test_long_and_overflow(PyObject *self) |
| 563 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 564 | PyObject *num, *one, *temp; |
| 565 | long value; |
| 566 | int overflow; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 568 | /* Test that overflow is set properly for a large value. */ |
| 569 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 570 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 571 | if (num == NULL) |
| 572 | return NULL; |
| 573 | overflow = 1234; |
| 574 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 575 | Py_DECREF(num); |
| 576 | if (value == -1 && PyErr_Occurred()) |
| 577 | return NULL; |
| 578 | if (value != -1) |
| 579 | return raiseTestError("test_long_and_overflow", |
| 580 | "return value was not set to -1"); |
| 581 | if (overflow != 1) |
| 582 | return raiseTestError("test_long_and_overflow", |
| 583 | "overflow was not set to 1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 585 | /* Same again, with num = LONG_MAX + 1 */ |
| 586 | num = PyLong_FromLong(LONG_MAX); |
| 587 | if (num == NULL) |
| 588 | return NULL; |
| 589 | one = PyLong_FromLong(1L); |
| 590 | if (one == NULL) { |
| 591 | Py_DECREF(num); |
| 592 | return NULL; |
| 593 | } |
| 594 | temp = PyNumber_Add(num, one); |
| 595 | Py_DECREF(one); |
| 596 | Py_DECREF(num); |
| 597 | num = temp; |
| 598 | if (num == NULL) |
| 599 | return NULL; |
| 600 | overflow = 0; |
| 601 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 602 | Py_DECREF(num); |
| 603 | if (value == -1 && PyErr_Occurred()) |
| 604 | return NULL; |
| 605 | if (value != -1) |
| 606 | return raiseTestError("test_long_and_overflow", |
| 607 | "return value was not set to -1"); |
| 608 | if (overflow != 1) |
| 609 | return raiseTestError("test_long_and_overflow", |
| 610 | "overflow was not set to 1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 612 | /* Test that overflow is set properly for a large negative value. */ |
| 613 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 614 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 615 | if (num == NULL) |
| 616 | return NULL; |
| 617 | overflow = 1234; |
| 618 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 619 | Py_DECREF(num); |
| 620 | if (value == -1 && PyErr_Occurred()) |
| 621 | return NULL; |
| 622 | if (value != -1) |
| 623 | return raiseTestError("test_long_and_overflow", |
| 624 | "return value was not set to -1"); |
| 625 | if (overflow != -1) |
| 626 | return raiseTestError("test_long_and_overflow", |
| 627 | "overflow was not set to -1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 629 | /* Same again, with num = LONG_MIN - 1 */ |
| 630 | num = PyLong_FromLong(LONG_MIN); |
| 631 | if (num == NULL) |
| 632 | return NULL; |
| 633 | one = PyLong_FromLong(1L); |
| 634 | if (one == NULL) { |
| 635 | Py_DECREF(num); |
| 636 | return NULL; |
| 637 | } |
| 638 | temp = PyNumber_Subtract(num, one); |
| 639 | Py_DECREF(one); |
| 640 | Py_DECREF(num); |
| 641 | num = temp; |
| 642 | if (num == NULL) |
| 643 | return NULL; |
| 644 | overflow = 0; |
| 645 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 646 | Py_DECREF(num); |
| 647 | if (value == -1 && PyErr_Occurred()) |
| 648 | return NULL; |
| 649 | if (value != -1) |
| 650 | return raiseTestError("test_long_and_overflow", |
| 651 | "return value was not set to -1"); |
| 652 | if (overflow != -1) |
| 653 | return raiseTestError("test_long_and_overflow", |
| 654 | "overflow was not set to -1"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 656 | /* Test that overflow is cleared properly for small values. */ |
| 657 | num = PyLong_FromString("FF", NULL, 16); |
| 658 | if (num == NULL) |
| 659 | return NULL; |
| 660 | overflow = 1234; |
| 661 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 662 | Py_DECREF(num); |
| 663 | if (value == -1 && PyErr_Occurred()) |
| 664 | return NULL; |
| 665 | if (value != 0xFF) |
| 666 | return raiseTestError("test_long_and_overflow", |
| 667 | "expected return value 0xFF"); |
| 668 | if (overflow != 0) |
| 669 | return raiseTestError("test_long_and_overflow", |
| 670 | "overflow was not cleared"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 672 | num = PyLong_FromString("-FF", NULL, 16); |
| 673 | if (num == NULL) |
| 674 | return NULL; |
| 675 | overflow = 0; |
| 676 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 677 | Py_DECREF(num); |
| 678 | if (value == -1 && PyErr_Occurred()) |
| 679 | return NULL; |
| 680 | if (value != -0xFF) |
| 681 | return raiseTestError("test_long_and_overflow", |
| 682 | "expected return value 0xFF"); |
| 683 | if (overflow != 0) |
| 684 | return raiseTestError("test_long_and_overflow", |
| 685 | "overflow was set incorrectly"); |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 686 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 687 | num = PyLong_FromLong(LONG_MAX); |
| 688 | if (num == NULL) |
| 689 | return NULL; |
| 690 | overflow = 1234; |
| 691 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 692 | Py_DECREF(num); |
| 693 | if (value == -1 && PyErr_Occurred()) |
| 694 | return NULL; |
| 695 | if (value != LONG_MAX) |
| 696 | return raiseTestError("test_long_and_overflow", |
| 697 | "expected return value LONG_MAX"); |
| 698 | if (overflow != 0) |
| 699 | return raiseTestError("test_long_and_overflow", |
| 700 | "overflow was not cleared"); |
Mark Dickinson | bd335bf | 2009-12-21 12:15:48 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 702 | num = PyLong_FromLong(LONG_MIN); |
| 703 | if (num == NULL) |
| 704 | return NULL; |
| 705 | overflow = 0; |
| 706 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 707 | Py_DECREF(num); |
| 708 | if (value == -1 && PyErr_Occurred()) |
| 709 | return NULL; |
| 710 | if (value != LONG_MIN) |
| 711 | return raiseTestError("test_long_and_overflow", |
| 712 | "expected return value LONG_MIN"); |
| 713 | if (overflow != 0) |
| 714 | return raiseTestError("test_long_and_overflow", |
| 715 | "overflow was not cleared"); |
Mark Dickinson | ed02b3f | 2009-12-21 11:31:54 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 717 | Py_INCREF(Py_None); |
| 718 | return Py_None; |
Mark Dickinson | e31d300 | 2009-12-21 11:21:25 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 721 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
| 722 | PY_LONG_LONG is tested by test_long_api_inner. This test will |
| 723 | concentrate on proper handling of overflow. |
| 724 | */ |
| 725 | |
| 726 | static PyObject * |
| 727 | test_long_long_and_overflow(PyObject *self) |
| 728 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 729 | PyObject *num, *one, *temp; |
| 730 | PY_LONG_LONG value; |
| 731 | int overflow; |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 732 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 733 | /* Test that overflow is set properly for a large value. */ |
| 734 | /* num is a number larger than PY_LLONG_MAX on a typical machine. */ |
| 735 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 736 | if (num == NULL) |
| 737 | return NULL; |
| 738 | overflow = 1234; |
| 739 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 740 | Py_DECREF(num); |
| 741 | if (value == -1 && PyErr_Occurred()) |
| 742 | return NULL; |
| 743 | if (value != -1) |
| 744 | return raiseTestError("test_long_long_and_overflow", |
| 745 | "return value was not set to -1"); |
| 746 | if (overflow != 1) |
| 747 | return raiseTestError("test_long_long_and_overflow", |
| 748 | "overflow was not set to 1"); |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 749 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 750 | /* Same again, with num = PY_LLONG_MAX + 1 */ |
| 751 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 752 | if (num == NULL) |
| 753 | return NULL; |
| 754 | one = PyLong_FromLong(1L); |
| 755 | if (one == NULL) { |
| 756 | Py_DECREF(num); |
| 757 | return NULL; |
| 758 | } |
| 759 | temp = PyNumber_Add(num, one); |
| 760 | Py_DECREF(one); |
| 761 | Py_DECREF(num); |
| 762 | num = temp; |
| 763 | if (num == NULL) |
| 764 | return NULL; |
| 765 | overflow = 0; |
| 766 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 767 | Py_DECREF(num); |
| 768 | if (value == -1 && PyErr_Occurred()) |
| 769 | return NULL; |
| 770 | if (value != -1) |
| 771 | return raiseTestError("test_long_long_and_overflow", |
| 772 | "return value was not set to -1"); |
| 773 | if (overflow != 1) |
| 774 | return raiseTestError("test_long_long_and_overflow", |
| 775 | "overflow was not set to 1"); |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 777 | /* Test that overflow is set properly for a large negative value. */ |
| 778 | /* num is a number smaller than PY_LLONG_MIN on a typical platform */ |
| 779 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 780 | if (num == NULL) |
| 781 | return NULL; |
| 782 | overflow = 1234; |
| 783 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 784 | Py_DECREF(num); |
| 785 | if (value == -1 && PyErr_Occurred()) |
| 786 | return NULL; |
| 787 | if (value != -1) |
| 788 | return raiseTestError("test_long_long_and_overflow", |
| 789 | "return value was not set to -1"); |
| 790 | if (overflow != -1) |
| 791 | return raiseTestError("test_long_long_and_overflow", |
| 792 | "overflow was not set to -1"); |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 794 | /* Same again, with num = PY_LLONG_MIN - 1 */ |
| 795 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 796 | if (num == NULL) |
| 797 | return NULL; |
| 798 | one = PyLong_FromLong(1L); |
| 799 | if (one == NULL) { |
| 800 | Py_DECREF(num); |
| 801 | return NULL; |
| 802 | } |
| 803 | temp = PyNumber_Subtract(num, one); |
| 804 | Py_DECREF(one); |
| 805 | Py_DECREF(num); |
| 806 | num = temp; |
| 807 | if (num == NULL) |
| 808 | return NULL; |
| 809 | overflow = 0; |
| 810 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 811 | Py_DECREF(num); |
| 812 | if (value == -1 && PyErr_Occurred()) |
| 813 | return NULL; |
| 814 | if (value != -1) |
| 815 | return raiseTestError("test_long_long_and_overflow", |
| 816 | "return value was not set to -1"); |
| 817 | if (overflow != -1) |
| 818 | return raiseTestError("test_long_long_and_overflow", |
| 819 | "overflow was not set to -1"); |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 820 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 821 | /* Test that overflow is cleared properly for small values. */ |
| 822 | num = PyLong_FromString("FF", NULL, 16); |
| 823 | if (num == NULL) |
| 824 | return NULL; |
| 825 | overflow = 1234; |
| 826 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 827 | Py_DECREF(num); |
| 828 | if (value == -1 && PyErr_Occurred()) |
| 829 | return NULL; |
| 830 | if (value != 0xFF) |
| 831 | return raiseTestError("test_long_long_and_overflow", |
| 832 | "expected return value 0xFF"); |
| 833 | if (overflow != 0) |
| 834 | return raiseTestError("test_long_long_and_overflow", |
| 835 | "overflow was not cleared"); |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 836 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 837 | num = PyLong_FromString("-FF", NULL, 16); |
| 838 | if (num == NULL) |
| 839 | return NULL; |
| 840 | overflow = 0; |
| 841 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 842 | Py_DECREF(num); |
| 843 | if (value == -1 && PyErr_Occurred()) |
| 844 | return NULL; |
| 845 | if (value != -0xFF) |
| 846 | return raiseTestError("test_long_long_and_overflow", |
| 847 | "expected return value 0xFF"); |
| 848 | if (overflow != 0) |
| 849 | return raiseTestError("test_long_long_and_overflow", |
| 850 | "overflow was set incorrectly"); |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 851 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 852 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 853 | if (num == NULL) |
| 854 | return NULL; |
| 855 | overflow = 1234; |
| 856 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 857 | Py_DECREF(num); |
| 858 | if (value == -1 && PyErr_Occurred()) |
| 859 | return NULL; |
| 860 | if (value != PY_LLONG_MAX) |
| 861 | return raiseTestError("test_long_long_and_overflow", |
| 862 | "expected return value PY_LLONG_MAX"); |
| 863 | if (overflow != 0) |
| 864 | return raiseTestError("test_long_long_and_overflow", |
| 865 | "overflow was not cleared"); |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 866 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 867 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 868 | if (num == NULL) |
| 869 | return NULL; |
| 870 | overflow = 0; |
| 871 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 872 | Py_DECREF(num); |
| 873 | if (value == -1 && PyErr_Occurred()) |
| 874 | return NULL; |
| 875 | if (value != PY_LLONG_MIN) |
| 876 | return raiseTestError("test_long_long_and_overflow", |
| 877 | "expected return value PY_LLONG_MIN"); |
| 878 | if (overflow != 0) |
| 879 | return raiseTestError("test_long_long_and_overflow", |
| 880 | "overflow was not cleared"); |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 882 | Py_INCREF(Py_None); |
| 883 | return Py_None; |
Mark Dickinson | a36507c | 2010-01-30 10:08:33 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 886 | /* 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] | 887 | for both long and int arguments. The test may leak a little memory if |
| 888 | it fails. |
| 889 | */ |
| 890 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 891 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 892 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 893 | PyObject *tuple, *num; |
| 894 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 896 | tuple = PyTuple_New(1); |
| 897 | if (tuple == NULL) |
| 898 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 900 | num = PyLong_FromLong(42); |
| 901 | if (num == NULL) |
| 902 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 904 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 906 | value = -1; |
| 907 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 908 | return NULL; |
| 909 | if (value != 42) |
| 910 | return raiseTestError("test_L_code", |
| 911 | "L code returned wrong value for long 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 912 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 913 | Py_DECREF(num); |
| 914 | num = PyInt_FromLong(42); |
| 915 | if (num == NULL) |
| 916 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 918 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 920 | value = -1; |
| 921 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 922 | return NULL; |
| 923 | if (value != 42) |
| 924 | return raiseTestError("test_L_code", |
| 925 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 927 | Py_DECREF(tuple); |
| 928 | Py_INCREF(Py_None); |
| 929 | return Py_None; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 932 | #endif /* ifdef HAVE_LONG_LONG */ |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 933 | |
Serhiy Storchaka | c6a7c96 | 2016-05-16 09:55:32 +0300 | [diff] [blame] | 934 | static PyObject * |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 935 | return_none(void *unused) |
| 936 | { |
| 937 | Py_RETURN_NONE; |
| 938 | } |
| 939 | |
| 940 | static PyObject * |
| 941 | raise_error(void *unused) |
| 942 | { |
| 943 | PyErr_SetNone(PyExc_ValueError); |
| 944 | return NULL; |
| 945 | } |
| 946 | |
| 947 | static int |
| 948 | test_buildvalue_N_error(const char *fmt) |
| 949 | { |
| 950 | PyObject *arg, *res; |
| 951 | |
| 952 | arg = PyList_New(0); |
| 953 | if (arg == NULL) { |
| 954 | return -1; |
| 955 | } |
| 956 | |
| 957 | Py_INCREF(arg); |
| 958 | res = Py_BuildValue(fmt, return_none, NULL, arg); |
| 959 | if (res == NULL) { |
| 960 | return -1; |
| 961 | } |
| 962 | Py_DECREF(res); |
| 963 | if (Py_REFCNT(arg) != 1) { |
| 964 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 965 | "arg was not decrefed in successful " |
| 966 | "Py_BuildValue(\"%s\")", fmt); |
| 967 | return -1; |
| 968 | } |
| 969 | |
| 970 | Py_INCREF(arg); |
| 971 | res = Py_BuildValue(fmt, raise_error, NULL, arg); |
| 972 | if (res != NULL || !PyErr_Occurred()) { |
| 973 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 974 | "Py_BuildValue(\"%s\") didn't complain", fmt); |
| 975 | return -1; |
| 976 | } |
| 977 | PyErr_Clear(); |
| 978 | if (Py_REFCNT(arg) != 1) { |
| 979 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 980 | "arg was not decrefed in failed " |
| 981 | "Py_BuildValue(\"%s\")", fmt); |
| 982 | return -1; |
| 983 | } |
| 984 | Py_DECREF(arg); |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static PyObject * |
| 989 | test_buildvalue_N(PyObject *self, PyObject *noargs) |
| 990 | { |
| 991 | PyObject *arg, *res; |
| 992 | |
| 993 | arg = PyList_New(0); |
| 994 | if (arg == NULL) { |
| 995 | return NULL; |
| 996 | } |
| 997 | Py_INCREF(arg); |
| 998 | res = Py_BuildValue("N", arg); |
| 999 | if (res == NULL) { |
| 1000 | return NULL; |
| 1001 | } |
| 1002 | if (res != arg) { |
| 1003 | return raiseTestError("test_buildvalue_N", |
| 1004 | "Py_BuildValue(\"N\") returned wrong result"); |
| 1005 | } |
| 1006 | if (Py_REFCNT(arg) != 2) { |
| 1007 | return raiseTestError("test_buildvalue_N", |
| 1008 | "arg was not decrefed in Py_BuildValue(\"N\")"); |
| 1009 | } |
| 1010 | Py_DECREF(res); |
| 1011 | Py_DECREF(arg); |
| 1012 | |
| 1013 | if (test_buildvalue_N_error("O&N") < 0) |
| 1014 | return NULL; |
| 1015 | if (test_buildvalue_N_error("(O&N)") < 0) |
| 1016 | return NULL; |
| 1017 | if (test_buildvalue_N_error("[O&N]") < 0) |
| 1018 | return NULL; |
| 1019 | if (test_buildvalue_N_error("{O&N}") < 0) |
| 1020 | return NULL; |
| 1021 | if (test_buildvalue_N_error("{()O&(())N}") < 0) |
| 1022 | return NULL; |
| 1023 | |
| 1024 | Py_RETURN_NONE; |
| 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | static PyObject * |
Serhiy Storchaka | c6a7c96 | 2016-05-16 09:55:32 +0300 | [diff] [blame] | 1029 | get_args(PyObject *self, PyObject *args) |
| 1030 | { |
| 1031 | if (args == NULL) { |
| 1032 | args = Py_None; |
| 1033 | } |
| 1034 | Py_INCREF(args); |
| 1035 | return args; |
| 1036 | } |
| 1037 | |
| 1038 | static PyObject * |
| 1039 | get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1040 | { |
| 1041 | if (kwargs == NULL) { |
| 1042 | kwargs = Py_None; |
| 1043 | } |
| 1044 | Py_INCREF(kwargs); |
| 1045 | return kwargs; |
| 1046 | } |
| 1047 | |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 1048 | /* Test tuple argument processing */ |
| 1049 | static PyObject * |
| 1050 | getargs_tuple(PyObject *self, PyObject *args) |
| 1051 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1052 | int a, b, c; |
| 1053 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 1054 | return NULL; |
| 1055 | return Py_BuildValue("iii", a, b, c); |
Georg Brandl | 5f13578 | 2006-07-26 08:03:10 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Christian Heimes | ea83793 | 2008-02-26 17:23:51 +0000 | [diff] [blame] | 1058 | /* test PyArg_ParseTupleAndKeywords */ |
| 1059 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1060 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1061 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 1062 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 1063 | 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] | 1064 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1065 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 1066 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 1067 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 1068 | return NULL; |
| 1069 | return Py_BuildValue("iiiiiiiiii", |
| 1070 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 1071 | 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] | 1072 | } |
| 1073 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1074 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 1075 | and return the result. |
| 1076 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1077 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1078 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1079 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1080 | unsigned char value; |
| 1081 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 1082 | return NULL; |
| 1083 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1086 | static PyObject * |
| 1087 | getargs_B(PyObject *self, PyObject *args) |
| 1088 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1089 | unsigned char value; |
| 1090 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 1091 | return NULL; |
| 1092 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static PyObject * |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 1096 | getargs_h(PyObject *self, PyObject *args) |
| 1097 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1098 | short value; |
| 1099 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 1100 | return NULL; |
| 1101 | return PyLong_FromLong((long)value); |
Mark Dickinson | b5e348b | 2009-12-20 15:57:56 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1105 | getargs_H(PyObject *self, PyObject *args) |
| 1106 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1107 | unsigned short value; |
| 1108 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 1109 | return NULL; |
| 1110 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static PyObject * |
| 1114 | getargs_I(PyObject *self, PyObject *args) |
| 1115 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1116 | unsigned int value; |
| 1117 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 1118 | return NULL; |
| 1119 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | static PyObject * |
| 1123 | getargs_k(PyObject *self, PyObject *args) |
| 1124 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1125 | unsigned long value; |
| 1126 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 1127 | return NULL; |
| 1128 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static PyObject * |
| 1132 | getargs_i(PyObject *self, PyObject *args) |
| 1133 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1134 | int value; |
| 1135 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 1136 | return NULL; |
| 1137 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1140 | static PyObject * |
| 1141 | getargs_l(PyObject *self, PyObject *args) |
| 1142 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1143 | long value; |
| 1144 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 1145 | return NULL; |
| 1146 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1149 | static PyObject * |
| 1150 | getargs_n(PyObject *self, PyObject *args) |
| 1151 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1152 | Py_ssize_t value; |
| 1153 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 1154 | return NULL; |
| 1155 | return PyInt_FromSsize_t(value); |
Georg Brandl | 7f573f7 | 2006-04-13 07:59:30 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1158 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1159 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1160 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1161 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1162 | PY_LONG_LONG value; |
| 1163 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 1164 | return NULL; |
| 1165 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1168 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1169 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1170 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1171 | unsigned PY_LONG_LONG value; |
| 1172 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 1173 | return NULL; |
| 1174 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1175 | } |
| 1176 | #endif |
| 1177 | |
| 1178 | /* This function not only tests the 'k' getargs code, but also the |
| 1179 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */ |
| 1180 | static PyObject * |
| 1181 | test_k_code(PyObject *self) |
| 1182 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1183 | PyObject *tuple, *num; |
| 1184 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1186 | tuple = PyTuple_New(1); |
| 1187 | if (tuple == NULL) |
| 1188 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1190 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 1191 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 1192 | if (num == NULL) |
| 1193 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1194 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1195 | value = PyInt_AsUnsignedLongMask(num); |
| 1196 | if (value != ULONG_MAX) |
| 1197 | return raiseTestError("test_k_code", |
| 1198 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1199 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1200 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1201 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1202 | value = 0; |
| 1203 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 1204 | return NULL; |
| 1205 | if (value != ULONG_MAX) |
| 1206 | return raiseTestError("test_k_code", |
| 1207 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1209 | Py_DECREF(num); |
| 1210 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 1211 | if (num == NULL) |
| 1212 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1214 | value = PyInt_AsUnsignedLongMask(num); |
| 1215 | if (value != (unsigned long)-0x42) |
| 1216 | return raiseTestError("test_k_code", |
| 1217 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1218 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1219 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1220 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1221 | value = 0; |
| 1222 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 1223 | return NULL; |
| 1224 | if (value != (unsigned long)-0x42) |
| 1225 | return raiseTestError("test_k_code", |
| 1226 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1228 | Py_DECREF(tuple); |
| 1229 | Py_INCREF(Py_None); |
| 1230 | return Py_None; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1233 | static PyObject * |
Serhiy Storchaka | d0dc72b | 2016-05-16 10:12:02 +0300 | [diff] [blame] | 1234 | getargs_f(PyObject *self, PyObject *args) |
| 1235 | { |
| 1236 | float f; |
| 1237 | if (!PyArg_ParseTuple(args, "f", &f)) |
| 1238 | return NULL; |
| 1239 | return PyFloat_FromDouble(f); |
| 1240 | } |
| 1241 | |
| 1242 | static PyObject * |
| 1243 | getargs_d(PyObject *self, PyObject *args) |
| 1244 | { |
| 1245 | double d; |
| 1246 | if (!PyArg_ParseTuple(args, "d", &d)) |
| 1247 | return NULL; |
| 1248 | return PyFloat_FromDouble(d); |
| 1249 | } |
| 1250 | |
| 1251 | static PyObject * |
| 1252 | getargs_D(PyObject *self, PyObject *args) |
| 1253 | { |
| 1254 | Py_complex cval; |
| 1255 | if (!PyArg_ParseTuple(args, "D", &cval)) |
| 1256 | return NULL; |
| 1257 | return PyComplex_FromCComplex(cval); |
| 1258 | } |
| 1259 | |
| 1260 | static PyObject * |
| 1261 | getargs_S(PyObject *self, PyObject *args) |
| 1262 | { |
| 1263 | PyObject *obj; |
| 1264 | if (!PyArg_ParseTuple(args, "S", &obj)) |
| 1265 | return NULL; |
| 1266 | Py_INCREF(obj); |
| 1267 | return obj; |
| 1268 | } |
| 1269 | |
| 1270 | static PyObject * |
| 1271 | getargs_Y(PyObject *self, PyObject *args) |
| 1272 | { |
| 1273 | PyObject *obj; |
| 1274 | if (!PyArg_ParseTuple(args, "Y", &obj)) |
| 1275 | return NULL; |
| 1276 | Py_INCREF(obj); |
| 1277 | return obj; |
| 1278 | } |
| 1279 | |
| 1280 | #ifdef Py_USING_UNICODE |
| 1281 | static PyObject * |
| 1282 | getargs_U(PyObject *self, PyObject *args) |
| 1283 | { |
| 1284 | PyObject *obj; |
| 1285 | if (!PyArg_ParseTuple(args, "U", &obj)) |
| 1286 | return NULL; |
| 1287 | Py_INCREF(obj); |
| 1288 | return obj; |
| 1289 | } |
| 1290 | #endif |
| 1291 | |
| 1292 | static PyObject * |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1293 | getargs_c(PyObject *self, PyObject *args) |
| 1294 | { |
| 1295 | char c; |
| 1296 | if (!PyArg_ParseTuple(args, "c", &c)) |
| 1297 | return NULL; |
| 1298 | return PyInt_FromLong((unsigned char)c); |
| 1299 | } |
| 1300 | |
| 1301 | static PyObject * |
| 1302 | getargs_s(PyObject *self, PyObject *args) |
| 1303 | { |
| 1304 | const char *str; |
| 1305 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1306 | return NULL; |
| 1307 | return PyBytes_FromString(str); |
| 1308 | } |
| 1309 | |
| 1310 | static PyObject * |
| 1311 | getargs_s_star(PyObject *self, PyObject *args) |
| 1312 | { |
| 1313 | Py_buffer buffer; |
| 1314 | PyObject *bytes; |
| 1315 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1316 | return NULL; |
| 1317 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1318 | PyBuffer_Release(&buffer); |
| 1319 | return bytes; |
| 1320 | } |
| 1321 | |
| 1322 | static PyObject * |
| 1323 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1324 | { |
| 1325 | const char *str; |
Serhiy Storchaka | 4b40c3e | 2016-01-29 00:55:37 +0200 | [diff] [blame] | 1326 | int size; |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1327 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1328 | return NULL; |
| 1329 | return PyBytes_FromStringAndSize(str, size); |
| 1330 | } |
| 1331 | |
| 1332 | static PyObject * |
| 1333 | getargs_t_hash(PyObject *self, PyObject *args) |
| 1334 | { |
| 1335 | const char *str; |
Serhiy Storchaka | 4b40c3e | 2016-01-29 00:55:37 +0200 | [diff] [blame] | 1336 | int size; |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1337 | if (!PyArg_ParseTuple(args, "t#", &str, &size)) |
| 1338 | return NULL; |
| 1339 | return PyBytes_FromStringAndSize(str, size); |
| 1340 | } |
| 1341 | |
| 1342 | static PyObject * |
| 1343 | getargs_z(PyObject *self, PyObject *args) |
| 1344 | { |
| 1345 | const char *str; |
| 1346 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1347 | return NULL; |
| 1348 | if (str != NULL) |
| 1349 | return PyBytes_FromString(str); |
| 1350 | else |
| 1351 | Py_RETURN_NONE; |
| 1352 | } |
| 1353 | |
| 1354 | static PyObject * |
| 1355 | getargs_z_star(PyObject *self, PyObject *args) |
| 1356 | { |
| 1357 | Py_buffer buffer; |
| 1358 | PyObject *bytes; |
| 1359 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1360 | return NULL; |
| 1361 | if (buffer.buf != NULL) |
| 1362 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1363 | else { |
| 1364 | Py_INCREF(Py_None); |
| 1365 | bytes = Py_None; |
| 1366 | } |
| 1367 | PyBuffer_Release(&buffer); |
| 1368 | return bytes; |
| 1369 | } |
| 1370 | |
| 1371 | static PyObject * |
| 1372 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1373 | { |
| 1374 | const char *str; |
Serhiy Storchaka | 4b40c3e | 2016-01-29 00:55:37 +0200 | [diff] [blame] | 1375 | int size; |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1376 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1377 | return NULL; |
| 1378 | if (str != NULL) |
| 1379 | return PyBytes_FromStringAndSize(str, size); |
| 1380 | else |
| 1381 | Py_RETURN_NONE; |
| 1382 | } |
| 1383 | |
| 1384 | static PyObject * |
| 1385 | getargs_w(PyObject *self, PyObject *args) |
| 1386 | { |
| 1387 | char *str; |
| 1388 | Py_ssize_t size; |
| 1389 | |
| 1390 | if (!PyArg_ParseTuple(args, "wn", &str, &size)) |
| 1391 | return NULL; |
| 1392 | |
| 1393 | if (2 <= size) { |
| 1394 | str[0] = '['; |
| 1395 | str[size-1] = ']'; |
| 1396 | } |
| 1397 | |
| 1398 | return PyBytes_FromStringAndSize(str, size); |
| 1399 | } |
| 1400 | |
| 1401 | static PyObject * |
| 1402 | getargs_w_hash(PyObject *self, PyObject *args) |
| 1403 | { |
| 1404 | char *str; |
Serhiy Storchaka | 4b40c3e | 2016-01-29 00:55:37 +0200 | [diff] [blame] | 1405 | int size; |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1406 | |
| 1407 | if (!PyArg_ParseTuple(args, "w#", &str, &size)) |
| 1408 | return NULL; |
| 1409 | |
| 1410 | if (2 <= size) { |
| 1411 | str[0] = '['; |
| 1412 | str[size-1] = ']'; |
| 1413 | } |
| 1414 | |
| 1415 | return PyBytes_FromStringAndSize(str, size); |
| 1416 | } |
| 1417 | |
| 1418 | static PyObject * |
| 1419 | getargs_w_star(PyObject *self, PyObject *args) |
| 1420 | { |
| 1421 | Py_buffer buffer; |
| 1422 | PyObject *result; |
| 1423 | char *str; |
| 1424 | |
| 1425 | if (!PyArg_ParseTuple(args, "w*", &buffer)) |
| 1426 | return NULL; |
| 1427 | |
| 1428 | if (2 <= buffer.len) { |
| 1429 | str = buffer.buf; |
| 1430 | str[0] = '['; |
| 1431 | str[buffer.len-1] = ']'; |
| 1432 | } |
| 1433 | |
| 1434 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1435 | PyBuffer_Release(&buffer); |
| 1436 | return result; |
| 1437 | } |
| 1438 | |
| 1439 | #ifdef Py_USING_UNICODE |
| 1440 | |
| 1441 | static int |
| 1442 | _ustrlen(const Py_UNICODE *u) |
| 1443 | { |
| 1444 | int i = 0; |
| 1445 | const Py_UNICODE *v = u; |
| 1446 | while (*v != 0) { i++; v++; } |
| 1447 | return i; |
| 1448 | } |
| 1449 | |
| 1450 | static PyObject * |
| 1451 | getargs_u(PyObject *self, PyObject *args) |
| 1452 | { |
| 1453 | const Py_UNICODE *str; |
Serhiy Storchaka | 4b40c3e | 2016-01-29 00:55:37 +0200 | [diff] [blame] | 1454 | int size; |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1455 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1456 | return NULL; |
| 1457 | size = _ustrlen(str); |
| 1458 | return PyUnicode_FromUnicode(str, size); |
| 1459 | } |
| 1460 | |
| 1461 | static PyObject * |
| 1462 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1463 | { |
| 1464 | const Py_UNICODE *str; |
Serhiy Storchaka | 4b40c3e | 2016-01-29 00:55:37 +0200 | [diff] [blame] | 1465 | int size; |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1466 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1467 | return NULL; |
| 1468 | return PyUnicode_FromUnicode(str, size); |
| 1469 | } |
| 1470 | |
| 1471 | #endif |
| 1472 | |
| 1473 | static PyObject * |
| 1474 | getargs_es(PyObject *self, PyObject *args) |
| 1475 | { |
| 1476 | PyObject *arg, *result; |
| 1477 | const char *encoding = NULL; |
| 1478 | char *str; |
| 1479 | |
| 1480 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1481 | return NULL; |
| 1482 | if (!PyArg_Parse(arg, "es", encoding, &str)) |
| 1483 | return NULL; |
| 1484 | result = PyBytes_FromString(str); |
| 1485 | PyMem_Free(str); |
| 1486 | return result; |
| 1487 | } |
| 1488 | |
| 1489 | static PyObject * |
| 1490 | getargs_et(PyObject *self, PyObject *args) |
| 1491 | { |
| 1492 | PyObject *arg, *result; |
| 1493 | const char *encoding = NULL; |
| 1494 | char *str; |
| 1495 | |
| 1496 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1497 | return NULL; |
| 1498 | if (!PyArg_Parse(arg, "et", encoding, &str)) |
| 1499 | return NULL; |
| 1500 | result = PyBytes_FromString(str); |
| 1501 | PyMem_Free(str); |
| 1502 | return result; |
| 1503 | } |
| 1504 | |
| 1505 | static PyObject * |
| 1506 | getargs_es_hash(PyObject *self, PyObject *args) |
| 1507 | { |
| 1508 | PyObject *arg, *result; |
| 1509 | const char *encoding = NULL; |
| 1510 | PyByteArrayObject *buffer = NULL; |
| 1511 | char *str = NULL; |
Serhiy Storchaka | 4b40c3e | 2016-01-29 00:55:37 +0200 | [diff] [blame] | 1512 | int size; |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1513 | |
| 1514 | if (!PyArg_ParseTuple(args, "O|sO!", |
| 1515 | &arg, &encoding, &PyByteArray_Type, &buffer)) |
| 1516 | return NULL; |
| 1517 | if (buffer != NULL) { |
| 1518 | str = PyByteArray_AS_STRING(buffer); |
| 1519 | size = PyByteArray_GET_SIZE(buffer); |
| 1520 | } |
| 1521 | if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) |
| 1522 | return NULL; |
| 1523 | result = PyBytes_FromStringAndSize(str, size); |
| 1524 | if (buffer == NULL) |
| 1525 | PyMem_Free(str); |
| 1526 | return result; |
| 1527 | } |
| 1528 | |
| 1529 | static PyObject * |
| 1530 | getargs_et_hash(PyObject *self, PyObject *args) |
| 1531 | { |
| 1532 | PyObject *arg, *result; |
| 1533 | const char *encoding = NULL; |
| 1534 | PyByteArrayObject *buffer = NULL; |
| 1535 | char *str = NULL; |
Serhiy Storchaka | 4b40c3e | 2016-01-29 00:55:37 +0200 | [diff] [blame] | 1536 | int size; |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1537 | |
| 1538 | if (!PyArg_ParseTuple(args, "O|sO!", |
| 1539 | &arg, &encoding, &PyByteArray_Type, &buffer)) |
| 1540 | return NULL; |
| 1541 | if (buffer != NULL) { |
| 1542 | str = PyByteArray_AS_STRING(buffer); |
| 1543 | size = PyByteArray_GET_SIZE(buffer); |
| 1544 | } |
| 1545 | if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) |
| 1546 | return NULL; |
| 1547 | result = PyBytes_FromStringAndSize(str, size); |
| 1548 | if (buffer == NULL) |
| 1549 | PyMem_Free(str); |
| 1550 | return result; |
| 1551 | } |
| 1552 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1553 | #ifdef Py_USING_UNICODE |
| 1554 | |
Benjamin Peterson | e098c4a | 2008-12-23 20:12:33 +0000 | [diff] [blame] | 1555 | static volatile int x; |
Benjamin Peterson | b6a53b5 | 2008-12-23 20:09:28 +0000 | [diff] [blame] | 1556 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1557 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1558 | of an error. |
| 1559 | */ |
| 1560 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1561 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1562 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1563 | PyObject *tuple, *obj; |
| 1564 | Py_UNICODE *value; |
| 1565 | int len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1567 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1568 | /* Just use the macro and check that it compiles */ |
| 1569 | x = Py_UNICODE_ISSPACE(25); |
Amaury Forgeot d'Arc | 07d539d | 2008-10-14 21:47:22 +0000 | [diff] [blame] | 1570 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1571 | tuple = PyTuple_New(1); |
| 1572 | if (tuple == NULL) |
| 1573 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1574 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1575 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1576 | "ascii", NULL); |
| 1577 | if (obj == NULL) |
| 1578 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1579 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1580 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1581 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1582 | value = 0; |
| 1583 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 1584 | return NULL; |
| 1585 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1586 | return raiseTestError("test_u_code", |
| 1587 | "u code returned wrong value for u'test'"); |
| 1588 | value = 0; |
| 1589 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 1590 | return NULL; |
| 1591 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1592 | len != PyUnicode_GET_SIZE(obj)) |
| 1593 | return raiseTestError("test_u_code", |
| 1594 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1595 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1596 | Py_DECREF(tuple); |
| 1597 | Py_INCREF(Py_None); |
| 1598 | return Py_None; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1601 | static PyObject * |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1602 | test_widechar(PyObject *self) |
| 1603 | { |
| 1604 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1605 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1606 | size_t wtextlen = 1; |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1607 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1608 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1609 | size_t wtextlen = 2; |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1610 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1611 | PyObject *wide, *utf8; |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1613 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1614 | if (wide == NULL) |
| 1615 | return NULL; |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1616 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1617 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1618 | if (utf8 == NULL) { |
| 1619 | Py_DECREF(wide); |
| 1620 | return NULL; |
| 1621 | } |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1622 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1623 | if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) { |
| 1624 | Py_DECREF(wide); |
| 1625 | Py_DECREF(utf8); |
| 1626 | return raiseTestError("test_widechar", |
| 1627 | "wide string and utf8 string have different length"); |
| 1628 | } |
| 1629 | if (PyUnicode_Compare(wide, utf8)) { |
| 1630 | Py_DECREF(wide); |
| 1631 | Py_DECREF(utf8); |
| 1632 | if (PyErr_Occurred()) |
| 1633 | return NULL; |
| 1634 | return raiseTestError("test_widechar", |
| 1635 | "wide string and utf8 string are differents"); |
| 1636 | } |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1637 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1638 | Py_DECREF(wide); |
| 1639 | Py_DECREF(utf8); |
| 1640 | Py_RETURN_NONE; |
Mark Dickinson | 6b265f1 | 2009-03-18 16:07:26 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | static PyObject * |
Victor Stinner | 975134e | 2011-11-22 01:54:19 +0100 | [diff] [blame] | 1644 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 1645 | { |
| 1646 | Py_UNICODE *unicode; |
Victor Stinner | 9f915d9 | 2011-11-29 00:53:09 +0100 | [diff] [blame] | 1647 | int length; |
Victor Stinner | 975134e | 2011-11-22 01:54:19 +0100 | [diff] [blame] | 1648 | char *errors = NULL; |
| 1649 | PyObject *decimal; |
| 1650 | Py_ssize_t decimal_length, new_length; |
| 1651 | int res; |
| 1652 | |
| 1653 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 1654 | return NULL; |
| 1655 | |
Serhiy Storchaka | e822b03 | 2013-08-06 16:56:26 +0300 | [diff] [blame] | 1656 | decimal_length = length * 10; /* len('') */ |
Victor Stinner | 975134e | 2011-11-22 01:54:19 +0100 | [diff] [blame] | 1657 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 1658 | if (decimal == NULL) |
| 1659 | return NULL; |
| 1660 | |
| 1661 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 1662 | PyBytes_AS_STRING(decimal), |
| 1663 | errors); |
| 1664 | if (res < 0) { |
| 1665 | Py_DECREF(decimal); |
| 1666 | return NULL; |
| 1667 | } |
| 1668 | |
| 1669 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 1670 | assert(new_length <= decimal_length); |
| 1671 | res = _PyBytes_Resize(&decimal, new_length); |
| 1672 | if (res < 0) |
| 1673 | return NULL; |
| 1674 | |
| 1675 | return decimal; |
| 1676 | } |
| 1677 | |
| 1678 | static PyObject * |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 1679 | test_empty_argparse(PyObject *self) |
| 1680 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1681 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 1682 | PyObject *tuple, *dict = NULL; |
| 1683 | static char *kwlist[] = {NULL}; |
| 1684 | int result; |
| 1685 | tuple = PyTuple_New(0); |
| 1686 | if (!tuple) |
| 1687 | return NULL; |
| 1688 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 1689 | goto done; |
| 1690 | dict = PyDict_New(); |
| 1691 | if (!dict) |
| 1692 | goto done; |
| 1693 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 1694 | done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1695 | Py_DECREF(tuple); |
| 1696 | Py_XDECREF(dict); |
| 1697 | if (result < 0) |
| 1698 | return NULL; |
| 1699 | else { |
| 1700 | Py_RETURN_NONE; |
| 1701 | } |
Benjamin Peterson | 4caef5c | 2008-12-22 20:51:15 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | static PyObject * |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1705 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1706 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1707 | const char *encoding, *errors = NULL; |
| 1708 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 1709 | &encoding, &errors)) |
| 1710 | return NULL; |
| 1711 | return PyCodec_IncrementalEncoder(encoding, errors); |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1714 | static PyObject * |
| 1715 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1716 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1717 | const char *encoding, *errors = NULL; |
| 1718 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 1719 | &encoding, &errors)) |
| 1720 | return NULL; |
| 1721 | return PyCodec_IncrementalDecoder(encoding, errors); |
Walter Dörwald | 9ae019b | 2006-03-18 14:22:26 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1724 | #endif |
| 1725 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1726 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1727 | static PyObject * |
| 1728 | test_long_numbits(PyObject *self) |
| 1729 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1730 | struct triple { |
| 1731 | long input; |
| 1732 | size_t nbits; |
| 1733 | int sign; |
| 1734 | } testcases[] = {{0, 0, 0}, |
| 1735 | {1L, 1, 1}, |
| 1736 | {-1L, 1, -1}, |
| 1737 | {2L, 2, 1}, |
| 1738 | {-2L, 2, -1}, |
| 1739 | {3L, 2, 1}, |
| 1740 | {-3L, 2, -1}, |
| 1741 | {4L, 3, 1}, |
| 1742 | {-4L, 3, -1}, |
| 1743 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 1744 | {-0x7fffL, 15, -1}, |
| 1745 | {0xffffL, 16, 1}, |
| 1746 | {-0xffffL, 16, -1}, |
| 1747 | {0xfffffffL, 28, 1}, |
| 1748 | {-0xfffffffL, 28, -1}}; |
| 1749 | int i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1751 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 1752 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
| 1753 | size_t nbits = _PyLong_NumBits(plong); |
| 1754 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1755 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1756 | Py_DECREF(plong); |
| 1757 | if (nbits != testcases[i].nbits) |
| 1758 | return raiseTestError("test_long_numbits", |
| 1759 | "wrong result for _PyLong_NumBits"); |
| 1760 | if (sign != testcases[i].sign) |
| 1761 | return raiseTestError("test_long_numbits", |
| 1762 | "wrong result for _PyLong_Sign"); |
| 1763 | } |
| 1764 | Py_INCREF(Py_None); |
| 1765 | return Py_None; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 1768 | /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */ |
| 1769 | |
| 1770 | static PyObject * |
| 1771 | test_null_strings(PyObject *self) |
| 1772 | { |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1773 | PyObject *o1 = PyObject_Str(NULL); |
| 1774 | #ifdef Py_USING_UNICODE |
| 1775 | PyObject *o2 = PyObject_Unicode(NULL); |
| 1776 | #else |
| 1777 | PyObject *o2 = PyObject_Str(NULL); |
| 1778 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1779 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 1780 | Py_XDECREF(o1); |
| 1781 | Py_XDECREF(o2); |
| 1782 | return tuple; |
Neal Norwitz | 4a53dad | 2006-03-15 05:43:10 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1785 | static PyObject * |
| 1786 | raise_exception(PyObject *self, PyObject *args) |
| 1787 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1788 | PyObject *exc; |
| 1789 | PyObject *exc_args, *v; |
| 1790 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1791 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1792 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1793 | &exc, &num_args)) |
| 1794 | return NULL; |
| 1795 | if (!PyExceptionClass_Check(exc)) { |
| 1796 | PyErr_Format(PyExc_TypeError, "an exception class is required"); |
| 1797 | return NULL; |
| 1798 | } |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1799 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1800 | exc_args = PyTuple_New(num_args); |
| 1801 | if (exc_args == NULL) |
| 1802 | return NULL; |
| 1803 | for (i = 0; i < num_args; ++i) { |
| 1804 | v = PyInt_FromLong(i); |
| 1805 | if (v == NULL) { |
| 1806 | Py_DECREF(exc_args); |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | PyTuple_SET_ITEM(exc_args, i, v); |
| 1810 | } |
| 1811 | PyErr_SetObject(exc, exc_args); |
| 1812 | Py_DECREF(exc_args); |
| 1813 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1814 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1815 | |
Antoine Pitrou | 0d2fac1 | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 1816 | static PyObject * |
| 1817 | set_errno(PyObject *self, PyObject *args) |
| 1818 | { |
| 1819 | int new_errno; |
| 1820 | |
| 1821 | if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
| 1822 | return NULL; |
| 1823 | |
| 1824 | errno = new_errno; |
| 1825 | Py_RETURN_NONE; |
| 1826 | } |
Benjamin Peterson | 46bff79 | 2010-01-30 23:28:38 +0000 | [diff] [blame] | 1827 | |
Benjamin Peterson | 888a29f | 2017-01-15 23:57:56 -0800 | [diff] [blame] | 1828 | #if defined(Py_USING_UNICODE) && !defined(Py_BUILD_CORE) |
Benjamin Peterson | 46bff79 | 2010-01-30 23:28:38 +0000 | [diff] [blame] | 1829 | static int test_run_counter = 0; |
| 1830 | |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 1831 | static PyObject * |
| 1832 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1833 | if (PyDateTimeAPI) { |
| 1834 | if (test_run_counter) { |
| 1835 | /* Probably regrtest.py -R */ |
| 1836 | Py_RETURN_NONE; |
| 1837 | } |
| 1838 | else { |
| 1839 | PyErr_SetString(PyExc_AssertionError, |
| 1840 | "PyDateTime_CAPI somehow initialized"); |
| 1841 | return NULL; |
| 1842 | } |
| 1843 | } |
| 1844 | test_run_counter++; |
| 1845 | PyDateTime_IMPORT; |
| 1846 | if (PyDateTimeAPI) |
| 1847 | Py_RETURN_NONE; |
| 1848 | else |
| 1849 | return NULL; |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 1850 | } |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 1851 | #endif |
Benjamin Peterson | 001e4a6 | 2009-12-13 21:27:53 +0000 | [diff] [blame] | 1852 | |
Benjamin Peterson | a04ae01 | 2010-01-30 23:26:05 +0000 | [diff] [blame] | 1853 | |
| 1854 | #ifdef WITH_THREAD |
| 1855 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1856 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 1857 | * `thread_done` when it's finished. The driver code has to know when the |
| 1858 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 1859 | * may go away when the driver finishes. The former lack of this explicit |
| 1860 | * synchronization caused rare segfaults, so rare that they were seen only |
| 1861 | * on a Mac buildbot (although they were possible on any box). |
| 1862 | */ |
| 1863 | static PyThread_type_lock thread_done = NULL; |
| 1864 | |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1865 | static int |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1866 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1867 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1868 | PyObject *rc; |
| 1869 | int success; |
| 1870 | PyGILState_STATE s = PyGILState_Ensure(); |
| 1871 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
| 1872 | success = (rc != NULL); |
| 1873 | Py_XDECREF(rc); |
| 1874 | PyGILState_Release(s); |
| 1875 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1878 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 1879 | * should be called only from threads spawned by test_thread_state(). |
| 1880 | */ |
| 1881 | static void |
| 1882 | _make_call_from_thread(void *callable) |
| 1883 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1884 | _make_call(callable); |
| 1885 | PyThread_release_lock(thread_done); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1888 | static PyObject * |
| 1889 | test_thread_state(PyObject *self, PyObject *args) |
| 1890 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1891 | PyObject *fn; |
| 1892 | int success = 1; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1893 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1894 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 1895 | return NULL; |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1896 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1897 | if (!PyCallable_Check(fn)) { |
| 1898 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1899 | fn->ob_type->tp_name); |
| 1900 | return NULL; |
| 1901 | } |
Benjamin Peterson | 37346b2 | 2008-08-23 20:27:43 +0000 | [diff] [blame] | 1902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1903 | /* Ensure Python is set up for threading */ |
| 1904 | PyEval_InitThreads(); |
| 1905 | thread_done = PyThread_allocate_lock(); |
| 1906 | if (thread_done == NULL) |
| 1907 | return PyErr_NoMemory(); |
| 1908 | PyThread_acquire_lock(thread_done, 1); |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1910 | /* Start a new thread with our callback. */ |
| 1911 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1912 | /* Make the callback with the thread lock held by this thread */ |
| 1913 | success &= _make_call(fn); |
| 1914 | /* Do it all again, but this time with the thread-lock released */ |
| 1915 | Py_BEGIN_ALLOW_THREADS |
| 1916 | success &= _make_call(fn); |
| 1917 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1918 | Py_END_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1919 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1920 | /* And once more with and without a thread |
| 1921 | XXX - should use a lock and work out exactly what we are trying |
| 1922 | to test <wink> |
| 1923 | */ |
| 1924 | Py_BEGIN_ALLOW_THREADS |
| 1925 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1926 | success &= _make_call(fn); |
| 1927 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1928 | Py_END_ALLOW_THREADS |
Tim Peters | 59b96c1 | 2006-03-21 03:58:41 +0000 | [diff] [blame] | 1929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1930 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 1931 | PyThread_release_lock(thread_done); |
Neal Norwitz | 97a5722 | 2006-10-28 21:17:51 +0000 | [diff] [blame] | 1932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1933 | PyThread_free_lock(thread_done); |
| 1934 | if (!success) |
| 1935 | return NULL; |
| 1936 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1937 | } |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1938 | |
| 1939 | /* test Py_AddPendingCalls using threads */ |
| 1940 | static int _pending_callback(void *arg) |
| 1941 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1942 | /* we assume the argument is callable object to which we own a reference */ |
| 1943 | PyObject *callable = (PyObject *)arg; |
| 1944 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 1945 | Py_DECREF(callable); |
| 1946 | Py_XDECREF(r); |
| 1947 | return r != NULL ? 0 : -1; |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /* The following requests n callbacks to _pending_callback. It can be |
| 1951 | * run from any python thread. |
| 1952 | */ |
| 1953 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1954 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1955 | PyObject *callable; |
| 1956 | int r; |
| 1957 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1958 | return NULL; |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1960 | /* create the reference for the callbackwhile we hold the lock */ |
| 1961 | Py_INCREF(callable); |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1962 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1963 | Py_BEGIN_ALLOW_THREADS |
| 1964 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1965 | Py_END_ALLOW_THREADS |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1966 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1967 | if (r<0) { |
| 1968 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1969 | Py_INCREF(Py_False); |
| 1970 | return Py_False; |
| 1971 | } |
| 1972 | Py_INCREF(Py_True); |
| 1973 | return Py_True; |
Kristján Valur Jónsson | 0e2d8c3 | 2009-01-09 21:35:16 +0000 | [diff] [blame] | 1974 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1975 | #endif |
| 1976 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1977 | /* Some tests of PyString_FromFormat(). This needs more tests. */ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1978 | static PyObject * |
| 1979 | test_string_from_format(PyObject *self, PyObject *args) |
| 1980 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1981 | PyObject *result; |
| 1982 | char *msg; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1983 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1984 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 1985 | result = PyString_FromFormat(FORMAT, (TYPE)1); \ |
| 1986 | if (result == NULL) \ |
| 1987 | return NULL; \ |
| 1988 | if (strcmp(PyString_AsString(result), "1")) { \ |
| 1989 | msg = FORMAT " failed at 1"; \ |
| 1990 | goto Fail; \ |
| 1991 | } \ |
| 1992 | Py_DECREF(result) |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1994 | CHECK_1_FORMAT("%d", int); |
| 1995 | CHECK_1_FORMAT("%ld", long); |
| 1996 | /* The z width modifier was added in Python 2.5. */ |
| 1997 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 1998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1999 | /* The u type code was added in Python 2.5. */ |
| 2000 | CHECK_1_FORMAT("%u", unsigned int); |
| 2001 | CHECK_1_FORMAT("%lu", unsigned long); |
| 2002 | CHECK_1_FORMAT("%zu", size_t); |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 2003 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2004 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Mark Dickinson | 82864d1 | 2009-11-15 16:18:58 +0000 | [diff] [blame] | 2005 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2006 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 2007 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
Mark Dickinson | 82864d1 | 2009-11-15 16:18:58 +0000 | [diff] [blame] | 2008 | #endif |
| 2009 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2010 | Py_RETURN_NONE; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 2011 | |
| 2012 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2013 | Py_XDECREF(result); |
| 2014 | return raiseTestError("test_string_from_format", msg); |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 2015 | |
| 2016 | #undef CHECK_1_FORMAT |
| 2017 | } |
| 2018 | |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2019 | /* Coverage testing of capsule objects. */ |
| 2020 | |
| 2021 | static const char *capsule_name = "capsule name"; |
| 2022 | static char *capsule_pointer = "capsule pointer"; |
| 2023 | static char *capsule_context = "capsule context"; |
| 2024 | static const char *capsule_error = NULL; |
| 2025 | static int |
| 2026 | capsule_destructor_call_count = 0; |
| 2027 | |
| 2028 | static void |
| 2029 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2030 | capsule_destructor_call_count++; |
| 2031 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 2032 | capsule_error = "context did not match in destructor!"; |
| 2033 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2034 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2035 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 2036 | capsule_error = "name did not match in destructor!"; |
| 2037 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2038 | capsule_error = "pointer did not match in destructor!"; |
| 2039 | } |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2043 | char *name; |
| 2044 | char *module; |
| 2045 | char *attribute; |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2046 | } known_capsule; |
| 2047 | |
| 2048 | static PyObject * |
| 2049 | test_capsule(PyObject *self, PyObject *args) |
| 2050 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2051 | PyObject *object; |
| 2052 | const char *error = NULL; |
| 2053 | void *pointer; |
| 2054 | void *pointer2; |
| 2055 | known_capsule known_capsules[] = { |
| 2056 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2057 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 2058 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 2059 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2060 | { NULL, NULL }, |
| 2061 | }; |
| 2062 | known_capsule *known = &known_capsules[0]; |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2063 | |
| 2064 | #define FAIL(x) { error = (x); goto exit; } |
| 2065 | |
| 2066 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2067 | if (capsule_error) { \ |
| 2068 | FAIL(capsule_error); \ |
| 2069 | } \ |
| 2070 | else if (!capsule_destructor_call_count) { \ |
| 2071 | FAIL("destructor not called!"); \ |
| 2072 | } \ |
| 2073 | capsule_destructor_call_count = 0; \ |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2074 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2075 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2076 | PyCapsule_SetContext(object, capsule_context); |
| 2077 | capsule_destructor(object); |
| 2078 | CHECK_DESTRUCTOR; |
| 2079 | Py_DECREF(object); |
| 2080 | CHECK_DESTRUCTOR; |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2081 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2082 | object = PyCapsule_New(known, "ignored", NULL); |
| 2083 | PyCapsule_SetPointer(object, capsule_pointer); |
| 2084 | PyCapsule_SetName(object, capsule_name); |
| 2085 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 2086 | PyCapsule_SetContext(object, capsule_context); |
| 2087 | capsule_destructor(object); |
| 2088 | CHECK_DESTRUCTOR; |
| 2089 | /* intentionally access using the wrong name */ |
| 2090 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2091 | if (!PyErr_Occurred()) { |
| 2092 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2093 | } |
| 2094 | PyErr_Clear(); |
| 2095 | if (pointer2) { |
| 2096 | if (pointer2 == capsule_pointer) { |
| 2097 | FAIL("PyCapsule_GetPointer should not have" |
| 2098 | " returned the internal pointer!"); |
| 2099 | } else { |
| 2100 | FAIL("PyCapsule_GetPointer should have " |
| 2101 | "returned NULL pointer but did not!"); |
| 2102 | } |
| 2103 | } |
| 2104 | PyCapsule_SetDestructor(object, NULL); |
| 2105 | Py_DECREF(object); |
| 2106 | if (capsule_destructor_call_count) { |
| 2107 | FAIL("destructor called when it should not have been!"); |
| 2108 | } |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2109 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2110 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 2111 | /* yeah, ordinarily I wouldn't do this either, |
| 2112 | but it's fine for this test harness. |
| 2113 | */ |
| 2114 | static char buffer[256]; |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2115 | #undef FAIL |
| 2116 | #define FAIL(x) \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2117 | { \ |
| 2118 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 2119 | x, known->module, known->attribute); \ |
| 2120 | error = buffer; \ |
| 2121 | goto exit; \ |
| 2122 | } \ |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2123 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2124 | PyObject *module = PyImport_ImportModule(known->module); |
| 2125 | if (module) { |
| 2126 | pointer = PyCapsule_Import(known->name, 0); |
| 2127 | if (!pointer) { |
| 2128 | Py_DECREF(module); |
| 2129 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 2130 | } |
| 2131 | object = PyObject_GetAttrString(module, known->attribute); |
| 2132 | if (!object) { |
| 2133 | Py_DECREF(module); |
| 2134 | return NULL; |
| 2135 | } |
| 2136 | pointer2 = PyCapsule_GetPointer(object, |
| 2137 | "weebles wobble but they don't fall down"); |
| 2138 | if (!PyErr_Occurred()) { |
| 2139 | Py_DECREF(object); |
| 2140 | Py_DECREF(module); |
| 2141 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2142 | } |
| 2143 | PyErr_Clear(); |
| 2144 | if (pointer2) { |
| 2145 | Py_DECREF(module); |
| 2146 | Py_DECREF(object); |
| 2147 | if (pointer2 == pointer) { |
| 2148 | FAIL("PyCapsule_GetPointer should not have" |
| 2149 | " returned its internal pointer!"); |
| 2150 | } else { |
| 2151 | FAIL("PyCapsule_GetPointer should have" |
| 2152 | " returned NULL pointer but did not!"); |
| 2153 | } |
| 2154 | } |
| 2155 | Py_DECREF(object); |
| 2156 | Py_DECREF(module); |
| 2157 | } |
| 2158 | else |
| 2159 | PyErr_Clear(); |
| 2160 | } |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2161 | |
| 2162 | exit: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2163 | if (error) { |
| 2164 | return raiseTestError("test_capsule", error); |
| 2165 | } |
| 2166 | Py_RETURN_NONE; |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 2167 | #undef FAIL |
| 2168 | } |
| 2169 | |
Georg Brandl | e9462c7 | 2006-08-04 18:03:37 +0000 | [diff] [blame] | 2170 | /* This is here to provide a docstring for test_descr. */ |
| 2171 | static PyObject * |
| 2172 | test_with_docstring(PyObject *self) |
| 2173 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2174 | Py_RETURN_NONE; |
Georg Brandl | e9462c7 | 2006-08-04 18:03:37 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 2177 | /* To test the format of tracebacks as printed out. */ |
| 2178 | static PyObject * |
Brett Cannon | 8dc4303 | 2008-04-28 04:50:06 +0000 | [diff] [blame] | 2179 | traceback_print(PyObject *self, PyObject *args) |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 2180 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2181 | PyObject *file; |
| 2182 | PyObject *traceback; |
| 2183 | int result; |
| 2184 | |
| 2185 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 2186 | &traceback, &file)) |
| 2187 | return NULL; |
| 2188 | |
| 2189 | result = PyTraceBack_Print(traceback, file); |
| 2190 | if (result < 0) |
| 2191 | return NULL; |
| 2192 | Py_RETURN_NONE; |
Brett Cannon | 141534e | 2008-04-28 03:23:50 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 2195 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 2196 | static PyObject * |
| 2197 | code_newempty(PyObject *self, PyObject *args) |
| 2198 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2199 | const char *filename; |
| 2200 | const char *funcname; |
| 2201 | int firstlineno; |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 2202 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2203 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 2204 | &filename, &funcname, &firstlineno)) |
| 2205 | return NULL; |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2207 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Jeffrey Yasskin | 1aa4700 | 2009-05-08 21:51:06 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Georg Brandl | 740cdc3 | 2009-12-28 08:34:58 +0000 | [diff] [blame] | 2210 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 2211 | Run via Lib/test/test_exceptions.py */ |
| 2212 | static PyObject * |
| 2213 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2214 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2215 | char *name; |
| 2216 | char *doc = NULL; |
| 2217 | PyObject *base = NULL; |
| 2218 | PyObject *dict = NULL; |
Georg Brandl | 740cdc3 | 2009-12-28 08:34:58 +0000 | [diff] [blame] | 2219 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2220 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 740cdc3 | 2009-12-28 08:34:58 +0000 | [diff] [blame] | 2221 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2222 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2223 | "s|sOO:make_exception_with_doc", kwlist, |
| 2224 | &name, &doc, &base, &dict)) |
| 2225 | return NULL; |
Georg Brandl | 740cdc3 | 2009-12-28 08:34:58 +0000 | [diff] [blame] | 2226 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2227 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 740cdc3 | 2009-12-28 08:34:58 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Benjamin Peterson | a7b0976 | 2011-10-15 13:43:21 -0400 | [diff] [blame] | 2230 | static PyObject * |
| 2231 | sequence_delitem(PyObject *self, PyObject *args) |
| 2232 | { |
| 2233 | PyObject *seq; |
| 2234 | Py_ssize_t i; |
| 2235 | |
| 2236 | if (!PyArg_ParseTuple(args, "On", &seq, &i)) |
| 2237 | return NULL; |
| 2238 | if (PySequence_DelItem(seq, i) < 0) |
| 2239 | return NULL; |
| 2240 | Py_RETURN_NONE; |
| 2241 | } |
| 2242 | |
Victor Stinner | 66c6e9d | 2013-12-13 02:37:09 +0100 | [diff] [blame] | 2243 | #ifdef WITH_THREAD |
| 2244 | typedef struct { |
| 2245 | PyThread_type_lock start_event; |
| 2246 | PyThread_type_lock exit_event; |
| 2247 | PyObject *callback; |
| 2248 | } test_c_thread_t; |
| 2249 | |
| 2250 | static void |
| 2251 | temporary_c_thread(void *data) |
| 2252 | { |
| 2253 | test_c_thread_t *test_c_thread = data; |
| 2254 | PyGILState_STATE state; |
| 2255 | PyObject *res; |
| 2256 | |
| 2257 | PyThread_release_lock(test_c_thread->start_event); |
| 2258 | |
| 2259 | /* Allocate a Python thread state for this thread */ |
| 2260 | state = PyGILState_Ensure(); |
| 2261 | |
| 2262 | res = PyObject_CallFunction(test_c_thread->callback, "", NULL); |
| 2263 | Py_CLEAR(test_c_thread->callback); |
| 2264 | |
| 2265 | if (res == NULL) { |
| 2266 | PyErr_Print(); |
| 2267 | } |
| 2268 | else { |
| 2269 | Py_DECREF(res); |
| 2270 | } |
| 2271 | |
| 2272 | /* Destroy the Python thread state for this thread */ |
| 2273 | PyGILState_Release(state); |
| 2274 | |
| 2275 | PyThread_release_lock(test_c_thread->exit_event); |
| 2276 | |
| 2277 | PyThread_exit_thread(); |
| 2278 | } |
| 2279 | |
| 2280 | static PyObject * |
| 2281 | call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
| 2282 | { |
| 2283 | PyObject *res = NULL; |
| 2284 | test_c_thread_t test_c_thread; |
| 2285 | long thread; |
| 2286 | |
| 2287 | PyEval_InitThreads(); |
| 2288 | |
| 2289 | test_c_thread.start_event = PyThread_allocate_lock(); |
| 2290 | test_c_thread.exit_event = PyThread_allocate_lock(); |
| 2291 | test_c_thread.callback = NULL; |
| 2292 | if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
| 2293 | PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
| 2294 | goto exit; |
| 2295 | } |
| 2296 | |
| 2297 | Py_INCREF(callback); |
| 2298 | test_c_thread.callback = callback; |
| 2299 | |
| 2300 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 2301 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 2302 | |
| 2303 | thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
| 2304 | if (thread == -1) { |
| 2305 | PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
| 2306 | PyThread_release_lock(test_c_thread.start_event); |
| 2307 | PyThread_release_lock(test_c_thread.exit_event); |
| 2308 | goto exit; |
| 2309 | } |
| 2310 | |
| 2311 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 2312 | PyThread_release_lock(test_c_thread.start_event); |
| 2313 | |
| 2314 | Py_BEGIN_ALLOW_THREADS |
| 2315 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 2316 | PyThread_release_lock(test_c_thread.exit_event); |
| 2317 | Py_END_ALLOW_THREADS |
| 2318 | |
| 2319 | Py_INCREF(Py_None); |
| 2320 | res = Py_None; |
| 2321 | |
| 2322 | exit: |
| 2323 | Py_CLEAR(test_c_thread.callback); |
| 2324 | if (test_c_thread.start_event) |
| 2325 | PyThread_free_lock(test_c_thread.start_event); |
| 2326 | if (test_c_thread.exit_event) |
| 2327 | PyThread_free_lock(test_c_thread.exit_event); |
| 2328 | return res; |
| 2329 | } |
| 2330 | #endif /* WITH_THREAD */ |
| 2331 | |
Serhiy Storchaka | 4068b01 | 2015-02-06 08:56:33 +0200 | [diff] [blame] | 2332 | /* marshal */ |
| 2333 | |
| 2334 | static PyObject* |
| 2335 | pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
| 2336 | { |
| 2337 | long value; |
| 2338 | char *filename; |
| 2339 | int version; |
| 2340 | FILE *fp; |
| 2341 | |
| 2342 | if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", |
| 2343 | &value, &filename, &version)) |
| 2344 | return NULL; |
| 2345 | |
| 2346 | fp = fopen(filename, "wb"); |
| 2347 | if (fp == NULL) { |
| 2348 | PyErr_SetFromErrno(PyExc_OSError); |
| 2349 | return NULL; |
| 2350 | } |
| 2351 | |
| 2352 | PyMarshal_WriteLongToFile(value, fp, version); |
| 2353 | |
| 2354 | fclose(fp); |
| 2355 | if (PyErr_Occurred()) |
| 2356 | return NULL; |
| 2357 | Py_RETURN_NONE; |
| 2358 | } |
| 2359 | |
| 2360 | static PyObject* |
| 2361 | pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
| 2362 | { |
| 2363 | PyObject *obj; |
| 2364 | char *filename; |
| 2365 | int version; |
| 2366 | FILE *fp; |
| 2367 | |
| 2368 | if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", |
| 2369 | &obj, &filename, &version)) |
| 2370 | return NULL; |
| 2371 | |
| 2372 | fp = fopen(filename, "wb"); |
| 2373 | if (fp == NULL) { |
| 2374 | PyErr_SetFromErrno(PyExc_OSError); |
| 2375 | return NULL; |
| 2376 | } |
| 2377 | |
| 2378 | PyMarshal_WriteObjectToFile(obj, fp, version); |
| 2379 | |
| 2380 | fclose(fp); |
| 2381 | if (PyErr_Occurred()) |
| 2382 | return NULL; |
| 2383 | Py_RETURN_NONE; |
| 2384 | } |
| 2385 | |
| 2386 | static PyObject* |
| 2387 | pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
| 2388 | { |
| 2389 | int value; |
| 2390 | long pos; |
| 2391 | char *filename; |
| 2392 | FILE *fp; |
| 2393 | |
| 2394 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) |
| 2395 | return NULL; |
| 2396 | |
| 2397 | fp = fopen(filename, "rb"); |
| 2398 | if (fp == NULL) { |
| 2399 | PyErr_SetFromErrno(PyExc_OSError); |
| 2400 | return NULL; |
| 2401 | } |
| 2402 | |
| 2403 | value = PyMarshal_ReadShortFromFile(fp); |
| 2404 | pos = ftell(fp); |
| 2405 | |
| 2406 | fclose(fp); |
| 2407 | if (PyErr_Occurred()) |
| 2408 | return NULL; |
| 2409 | return Py_BuildValue("il", value, pos); |
| 2410 | } |
| 2411 | |
| 2412 | static PyObject* |
| 2413 | pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
| 2414 | { |
| 2415 | long value, pos; |
| 2416 | char *filename; |
| 2417 | FILE *fp; |
| 2418 | |
| 2419 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) |
| 2420 | return NULL; |
| 2421 | |
| 2422 | fp = fopen(filename, "rb"); |
| 2423 | if (fp == NULL) { |
| 2424 | PyErr_SetFromErrno(PyExc_OSError); |
| 2425 | return NULL; |
| 2426 | } |
| 2427 | |
| 2428 | value = PyMarshal_ReadLongFromFile(fp); |
| 2429 | pos = ftell(fp); |
| 2430 | |
| 2431 | fclose(fp); |
| 2432 | if (PyErr_Occurred()) |
| 2433 | return NULL; |
| 2434 | return Py_BuildValue("ll", value, pos); |
| 2435 | } |
| 2436 | |
| 2437 | static PyObject* |
| 2438 | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
| 2439 | { |
| 2440 | PyObject *obj; |
| 2441 | long pos; |
| 2442 | char *filename; |
| 2443 | FILE *fp; |
| 2444 | |
| 2445 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) |
| 2446 | return NULL; |
| 2447 | |
| 2448 | fp = fopen(filename, "rb"); |
| 2449 | if (fp == NULL) { |
| 2450 | PyErr_SetFromErrno(PyExc_OSError); |
| 2451 | return NULL; |
| 2452 | } |
| 2453 | |
| 2454 | obj = PyMarshal_ReadLastObjectFromFile(fp); |
| 2455 | pos = ftell(fp); |
| 2456 | |
| 2457 | fclose(fp); |
| 2458 | return Py_BuildValue("Nl", obj, pos); |
| 2459 | } |
| 2460 | |
| 2461 | static PyObject* |
| 2462 | pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
| 2463 | { |
| 2464 | PyObject *obj; |
| 2465 | long pos; |
| 2466 | char *filename; |
| 2467 | FILE *fp; |
| 2468 | |
| 2469 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) |
| 2470 | return NULL; |
| 2471 | |
| 2472 | fp = fopen(filename, "rb"); |
| 2473 | if (fp == NULL) { |
| 2474 | PyErr_SetFromErrno(PyExc_OSError); |
| 2475 | return NULL; |
| 2476 | } |
| 2477 | |
| 2478 | obj = PyMarshal_ReadObjectFromFile(fp); |
| 2479 | pos = ftell(fp); |
| 2480 | |
| 2481 | fclose(fp); |
| 2482 | return Py_BuildValue("Nl", obj, pos); |
| 2483 | } |
| 2484 | |
Victor Stinner | d2aff60 | 2017-05-09 13:57:20 +0200 | [diff] [blame^] | 2485 | static PyObject* |
| 2486 | test_raise_signal(PyObject* self, PyObject *args) |
| 2487 | { |
| 2488 | int signum, err; |
| 2489 | |
| 2490 | if (PyArg_ParseTuple(args, "i:raise_signal", &signum) < 0) |
| 2491 | return NULL; |
| 2492 | |
| 2493 | err = raise(signum); |
| 2494 | if (err) |
| 2495 | return PyErr_SetFromErrno(PyExc_OSError); |
| 2496 | |
| 2497 | if (PyErr_CheckSignals() < 0) |
| 2498 | return NULL; |
| 2499 | |
| 2500 | Py_RETURN_NONE; |
| 2501 | } |
| 2502 | |
Victor Stinner | 66c6e9d | 2013-12-13 02:37:09 +0100 | [diff] [blame] | 2503 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2504 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2505 | {"raise_exception", raise_exception, METH_VARARGS}, |
Antoine Pitrou | 0d2fac1 | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 2506 | {"set_errno", set_errno, METH_VARARGS}, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2507 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
Benjamin Peterson | 888a29f | 2017-01-15 23:57:56 -0800 | [diff] [blame] | 2508 | #if defined(Py_USING_UNICODE) && !defined(Py_BUILD_CORE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2509 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 2510 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2511 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 2512 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 2513 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
| 2514 | {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, |
Stefan Krah | 5606cd9 | 2015-01-30 20:11:10 +0100 | [diff] [blame] | 2515 | {"test_to_contiguous", (PyCFunction)test_to_contiguous, METH_NOARGS}, |
Stefan Krah | 0a7fc53 | 2015-02-01 16:10:35 +0100 | [diff] [blame] | 2516 | {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2517 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
| 2518 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 2519 | METH_NOARGS}, |
| 2520 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 2521 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 2522 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2523 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 2524 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2525 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 2526 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 2527 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 2528 | PyDoc_STR("This is a pretty normal docstring.")}, |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 2529 | |
Serhiy Storchaka | 12cf60c | 2016-05-20 22:31:24 +0300 | [diff] [blame] | 2530 | {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
Serhiy Storchaka | c6a7c96 | 2016-05-16 09:55:32 +0300 | [diff] [blame] | 2531 | {"get_args", get_args, METH_VARARGS}, |
| 2532 | {"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2533 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 2534 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 2535 | METH_VARARGS|METH_KEYWORDS}, |
| 2536 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 2537 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 2538 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 2539 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 2540 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 2541 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 2542 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 2543 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 2544 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2545 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2546 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 2547 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 2548 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 2549 | {"test_long_long_and_overflow", |
| 2550 | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
| 2551 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 2552 | #endif |
Serhiy Storchaka | d0dc72b | 2016-05-16 10:12:02 +0300 | [diff] [blame] | 2553 | {"getargs_f", getargs_f, METH_VARARGS}, |
| 2554 | {"getargs_d", getargs_d, METH_VARARGS}, |
| 2555 | {"getargs_D", getargs_D, METH_VARARGS}, |
| 2556 | {"getargs_S", getargs_S, METH_VARARGS}, |
| 2557 | {"getargs_Y", getargs_Y, METH_VARARGS}, |
| 2558 | #ifdef Py_USING_UNICODE |
| 2559 | {"getargs_U", getargs_U, METH_VARARGS}, |
| 2560 | #endif |
Serhiy Storchaka | 3ecb6ab | 2016-01-28 19:56:04 +0200 | [diff] [blame] | 2561 | {"getargs_c", getargs_c, METH_VARARGS}, |
| 2562 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 2563 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 2564 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 2565 | {"getargs_t_hash", getargs_t_hash, METH_VARARGS}, |
| 2566 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 2567 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 2568 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 2569 | {"getargs_w", getargs_w, METH_VARARGS}, |
| 2570 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
| 2571 | {"getargs_w_hash", getargs_w_hash, METH_VARARGS}, |
| 2572 | #ifdef Py_USING_UNICODE |
| 2573 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 2574 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 2575 | #endif |
| 2576 | {"getargs_es", getargs_es, METH_VARARGS}, |
| 2577 | {"getargs_et", getargs_et, METH_VARARGS}, |
| 2578 | {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 2579 | {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
| 2580 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2581 | {"codec_incrementalencoder", |
| 2582 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 2583 | {"codec_incrementaldecoder", |
| 2584 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2585 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 2586 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Victor Stinner | 975134e | 2011-11-22 01:54:19 +0100 | [diff] [blame] | 2587 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 2588 | #endif |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2589 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2590 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
| 2591 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2592 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2593 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
| 2594 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 2595 | {"code_newempty", code_newempty, METH_VARARGS}, |
| 2596 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 2597 | METH_VARARGS | METH_KEYWORDS}, |
Benjamin Peterson | a7b0976 | 2011-10-15 13:43:21 -0400 | [diff] [blame] | 2598 | {"sequence_delitem", (PyCFunction)sequence_delitem, METH_VARARGS}, |
Victor Stinner | 66c6e9d | 2013-12-13 02:37:09 +0100 | [diff] [blame] | 2599 | #ifdef WITH_THREAD |
| 2600 | {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
| 2601 | PyDoc_STR("set_error_class(error_class) -> None")}, |
| 2602 | #endif |
Serhiy Storchaka | 4068b01 | 2015-02-06 08:56:33 +0200 | [diff] [blame] | 2603 | {"pymarshal_write_long_to_file", |
| 2604 | pymarshal_write_long_to_file, METH_VARARGS}, |
| 2605 | {"pymarshal_write_object_to_file", |
| 2606 | pymarshal_write_object_to_file, METH_VARARGS}, |
| 2607 | {"pymarshal_read_short_from_file", |
| 2608 | pymarshal_read_short_from_file, METH_VARARGS}, |
| 2609 | {"pymarshal_read_long_from_file", |
| 2610 | pymarshal_read_long_from_file, METH_VARARGS}, |
| 2611 | {"pymarshal_read_last_object_from_file", |
| 2612 | pymarshal_read_last_object_from_file, METH_VARARGS}, |
| 2613 | {"pymarshal_read_object_from_file", |
| 2614 | pymarshal_read_object_from_file, METH_VARARGS}, |
Victor Stinner | d2aff60 | 2017-05-09 13:57:20 +0200 | [diff] [blame^] | 2615 | {"raise_signal", (PyCFunction)test_raise_signal, METH_VARARGS}, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2616 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2617 | }; |
| 2618 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2619 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 2620 | |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 2621 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2622 | char bool_member; |
| 2623 | char byte_member; |
| 2624 | unsigned char ubyte_member; |
| 2625 | short short_member; |
| 2626 | unsigned short ushort_member; |
| 2627 | int int_member; |
| 2628 | unsigned int uint_member; |
| 2629 | long long_member; |
| 2630 | unsigned long ulong_member; |
| 2631 | float float_member; |
| 2632 | double double_member; |
| 2633 | char inplace_member[6]; |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 2634 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2635 | PY_LONG_LONG longlong_member; |
| 2636 | unsigned PY_LONG_LONG ulonglong_member; |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 2637 | #endif |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 2638 | } all_structmembers; |
| 2639 | |
| 2640 | typedef struct { |
| 2641 | PyObject_HEAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2642 | all_structmembers structmembers; |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 2643 | } test_structmembers; |
| 2644 | |
| 2645 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2646 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 2647 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 2648 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 2649 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 2650 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 2651 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 2652 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 2653 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 2654 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 2655 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 2656 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 2657 | {"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] | 2658 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2659 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 2660 | {"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] | 2661 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2662 | {NULL} |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 2663 | }; |
| 2664 | |
| 2665 | |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 2666 | static PyObject * |
| 2667 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 2668 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2669 | static char *keywords[] = { |
| 2670 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 2671 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", |
| 2672 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 2673 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2674 | "T_LONGLONG", "T_ULONGLONG", |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 2675 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2676 | NULL}; |
| 2677 | static char *fmt = "|bbBhHiIlkfds#" |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 2678 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2679 | "LK" |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 2680 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2681 | ; |
| 2682 | test_structmembers *ob; |
| 2683 | const char *s = NULL; |
Antoine Pitrou | 1e18102 | 2013-05-08 02:07:13 +0200 | [diff] [blame] | 2684 | int string_len = 0; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2685 | ob = PyObject_New(test_structmembers, type); |
| 2686 | if (ob == NULL) |
| 2687 | return NULL; |
| 2688 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 2689 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 2690 | &ob->structmembers.bool_member, |
| 2691 | &ob->structmembers.byte_member, |
| 2692 | &ob->structmembers.ubyte_member, |
| 2693 | &ob->structmembers.short_member, |
| 2694 | &ob->structmembers.ushort_member, |
| 2695 | &ob->structmembers.int_member, |
| 2696 | &ob->structmembers.uint_member, |
| 2697 | &ob->structmembers.long_member, |
| 2698 | &ob->structmembers.ulong_member, |
| 2699 | &ob->structmembers.float_member, |
| 2700 | &ob->structmembers.double_member, |
| 2701 | &s, &string_len |
| 2702 | #ifdef HAVE_LONG_LONG |
| 2703 | , &ob->structmembers.longlong_member, |
| 2704 | &ob->structmembers.ulonglong_member |
| 2705 | #endif |
| 2706 | )) { |
| 2707 | Py_DECREF(ob); |
| 2708 | return NULL; |
| 2709 | } |
| 2710 | if (s != NULL) { |
| 2711 | if (string_len > 5) { |
| 2712 | Py_DECREF(ob); |
| 2713 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 2714 | return NULL; |
| 2715 | } |
| 2716 | strcpy(ob->structmembers.inplace_member, s); |
| 2717 | } |
| 2718 | else { |
| 2719 | strcpy(ob->structmembers.inplace_member, ""); |
| 2720 | } |
| 2721 | return (PyObject *)ob; |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 2724 | static void |
| 2725 | test_structmembers_free(PyObject *ob) |
| 2726 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2727 | PyObject_FREE(ob); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2731 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2732 | "test_structmembersType", |
| 2733 | sizeof(test_structmembers), /* tp_basicsize */ |
| 2734 | 0, /* tp_itemsize */ |
| 2735 | test_structmembers_free, /* destructor tp_dealloc */ |
| 2736 | 0, /* tp_print */ |
| 2737 | 0, /* tp_getattr */ |
| 2738 | 0, /* tp_setattr */ |
| 2739 | 0, /* tp_compare */ |
| 2740 | 0, /* tp_repr */ |
| 2741 | 0, /* tp_as_number */ |
| 2742 | 0, /* tp_as_sequence */ |
| 2743 | 0, /* tp_as_mapping */ |
| 2744 | 0, /* tp_hash */ |
| 2745 | 0, /* tp_call */ |
| 2746 | 0, /* tp_str */ |
| 2747 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2748 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 2749 | 0, /* tp_as_buffer */ |
| 2750 | 0, /* tp_flags */ |
| 2751 | "Type containing all structmember types", |
| 2752 | 0, /* traverseproc tp_traverse */ |
| 2753 | 0, /* tp_clear */ |
| 2754 | 0, /* tp_richcompare */ |
| 2755 | 0, /* tp_weaklistoffset */ |
| 2756 | 0, /* tp_iter */ |
| 2757 | 0, /* tp_iternext */ |
| 2758 | 0, /* tp_methods */ |
| 2759 | test_members, /* tp_members */ |
| 2760 | 0, |
| 2761 | 0, |
| 2762 | 0, |
| 2763 | 0, |
| 2764 | 0, |
| 2765 | 0, |
| 2766 | 0, |
| 2767 | 0, |
| 2768 | test_structmembers_new, /* tp_new */ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 2769 | }; |
| 2770 | |
| 2771 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 2772 | PyMODINIT_FUNC |
Tim Peters | d66595f | 2001-02-04 03:09:53 +0000 | [diff] [blame] | 2773 | init_testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2774 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2775 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2776 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2777 | m = Py_InitModule("_testcapi", TestMethods); |
| 2778 | if (m == NULL) |
| 2779 | return; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2781 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 3cda1db | 2008-12-31 05:47:19 +0000 | [diff] [blame] | 2782 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2783 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 2784 | Py_INCREF(&test_structmembersType); |
| 2785 | /* don't use a name starting with "test", since we don't want |
| 2786 | test_capi to automatically call this */ |
| 2787 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 2788 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2789 | PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX)); |
| 2790 | PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN)); |
| 2791 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX)); |
| 2792 | PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX)); |
| 2793 | PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN)); |
| 2794 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX)); |
| 2795 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 2796 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 2797 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 2798 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX)); |
| 2799 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN)); |
| 2800 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 2801 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 2802 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 2803 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 2804 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 2805 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 2806 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 2807 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 2808 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX)); |
| 2809 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN)); |
| 2810 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head))); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2811 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2812 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 2813 | Py_INCREF(TestError); |
| 2814 | PyModule_AddObject(m, "error", TestError); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2815 | } |