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