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 | |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 8 | #define PY_SSIZE_T_CLEAN |
| 9 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | #include <float.h> |
| 12 | #include "structmember.h" |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 13 | #include "datetime.h" |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 14 | #include "marshal.h" |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 15 | #include <signal.h> |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 16 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 17 | #ifdef MS_WINDOWS |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 18 | # include <winsock2.h> /* struct timeval */ |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 19 | #endif |
| 20 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 21 | #ifdef HAVE_SYS_WAIT_H |
| 22 | #include <sys/wait.h> /* For W_STOPCODE */ |
| 23 | #endif |
| 24 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 25 | #ifdef WITH_THREAD |
| 26 | #include "pythread.h" |
| 27 | #endif /* WITH_THREAD */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | static PyObject *TestError; /* set to exception object in init */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 29 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 30 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 31 | |
| 32 | static PyObject * |
| 33 | raiseTestError(const char* test_name, const char* msg) |
| 34 | { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 35 | PyErr_Format(TestError, "%s: %s", test_name, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 39 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 40 | |
| 41 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 42 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 43 | platforms have these hardcoded. Better safe than sorry. |
| 44 | */ |
| 45 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 46 | sizeof_error(const char* fatname, const char* typname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | int expected, int got) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 48 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 49 | PyErr_Format(TestError, |
| 50 | "%s #define == %d but sizeof(%s) == %d", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | fatname, expected, typname, got); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | return (PyObject*)NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 56 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 57 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 58 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | if (FATNAME != sizeof(TYPE)) \ |
| 60 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 63 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 64 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 65 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 66 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 67 | CHECK_SIZEOF(SIZEOF_LONG_LONG, long long); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 68 | |
| 69 | #undef CHECK_SIZEOF |
| 70 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 71 | Py_RETURN_NONE; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 74 | static PyObject* |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 75 | test_sizeof_c_types(PyObject *self) |
| 76 | { |
Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 77 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 78 | #pragma GCC diagnostic push |
| 79 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 80 | #endif |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 81 | #define CHECK_SIZEOF(TYPE, EXPECTED) \ |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 82 | if (EXPECTED != sizeof(TYPE)) { \ |
| 83 | PyErr_Format(TestError, \ |
| 84 | "sizeof(%s) = %u instead of %u", \ |
| 85 | #TYPE, sizeof(TYPE), EXPECTED); \ |
| 86 | return (PyObject*)NULL; \ |
| 87 | } |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 88 | #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0) |
| 89 | #define CHECK_SIGNNESS(TYPE, SIGNED) \ |
| 90 | if (IS_SIGNED(TYPE) != SIGNED) { \ |
| 91 | PyErr_Format(TestError, \ |
| 92 | "%s signness is, instead of %i", \ |
| 93 | #TYPE, IS_SIGNED(TYPE), SIGNED); \ |
| 94 | return (PyObject*)NULL; \ |
| 95 | } |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 96 | |
| 97 | /* integer types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 98 | CHECK_SIZEOF(Py_UCS1, 1); |
| 99 | CHECK_SIZEOF(Py_UCS2, 2); |
| 100 | CHECK_SIZEOF(Py_UCS4, 4); |
| 101 | CHECK_SIGNNESS(Py_UCS1, 0); |
| 102 | CHECK_SIGNNESS(Py_UCS2, 0); |
| 103 | CHECK_SIGNNESS(Py_UCS4, 0); |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 104 | CHECK_SIZEOF(int32_t, 4); |
| 105 | CHECK_SIGNNESS(int32_t, 1); |
| 106 | CHECK_SIZEOF(uint32_t, 4); |
| 107 | CHECK_SIGNNESS(uint32_t, 0); |
| 108 | CHECK_SIZEOF(int64_t, 8); |
| 109 | CHECK_SIGNNESS(int64_t, 1); |
| 110 | CHECK_SIZEOF(uint64_t, 8); |
| 111 | CHECK_SIGNNESS(uint64_t, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 112 | |
| 113 | /* pointer/size types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 114 | CHECK_SIZEOF(size_t, sizeof(void *)); |
| 115 | CHECK_SIGNNESS(size_t, 0); |
| 116 | CHECK_SIZEOF(Py_ssize_t, sizeof(void *)); |
| 117 | CHECK_SIGNNESS(Py_ssize_t, 1); |
| 118 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 119 | CHECK_SIZEOF(uintptr_t, sizeof(void *)); |
| 120 | CHECK_SIGNNESS(uintptr_t, 0); |
| 121 | CHECK_SIZEOF(intptr_t, sizeof(void *)); |
| 122 | CHECK_SIGNNESS(intptr_t, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 123 | |
Serhiy Storchaka | d1302c0 | 2017-01-23 10:23:58 +0200 | [diff] [blame] | 124 | Py_RETURN_NONE; |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 125 | |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 126 | #undef IS_SIGNED |
| 127 | #undef CHECK_SIGNESS |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 128 | #undef CHECK_SIZEOF |
Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 129 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 130 | #pragma GCC diagnostic pop |
| 131 | #endif |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 136 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | PyObject* list; |
| 139 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 142 | #define NLIST 30 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | list = PyList_New(NLIST); |
| 144 | if (list == (PyObject*)NULL) |
| 145 | return (PyObject*)NULL; |
| 146 | /* list = range(NLIST) */ |
| 147 | for (i = 0; i < NLIST; ++i) { |
| 148 | PyObject* anint = PyLong_FromLong(i); |
| 149 | if (anint == (PyObject*)NULL) { |
| 150 | Py_DECREF(list); |
| 151 | return (PyObject*)NULL; |
| 152 | } |
| 153 | PyList_SET_ITEM(list, i, anint); |
| 154 | } |
| 155 | /* list.reverse(), via PyList_Reverse() */ |
| 156 | i = PyList_Reverse(list); /* should not blow up! */ |
| 157 | if (i != 0) { |
| 158 | Py_DECREF(list); |
| 159 | return (PyObject*)NULL; |
| 160 | } |
| 161 | /* Check that list == range(29, -1, -1) now */ |
| 162 | for (i = 0; i < NLIST; ++i) { |
| 163 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 164 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 165 | PyErr_SetString(TestError, |
| 166 | "test_list_api: reverse screwed up"); |
| 167 | Py_DECREF(list); |
| 168 | return (PyObject*)NULL; |
| 169 | } |
| 170 | } |
| 171 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 172 | #undef NLIST |
| 173 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 174 | Py_RETURN_NONE; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 177 | static int |
| 178 | test_dict_inner(int count) |
| 179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | Py_ssize_t pos = 0, iterations = 0; |
| 181 | int i; |
| 182 | PyObject *dict = PyDict_New(); |
| 183 | PyObject *v, *k; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | if (dict == NULL) |
| 186 | return -1; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | for (i = 0; i < count; i++) { |
| 189 | v = PyLong_FromLong(i); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 190 | if (v == NULL) { |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 191 | return -1; |
| 192 | } |
Christian Heimes | 97cb67b | 2013-07-20 15:01:26 +0200 | [diff] [blame] | 193 | if (PyDict_SetItem(dict, v, v) < 0) { |
| 194 | Py_DECREF(v); |
| 195 | return -1; |
| 196 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | Py_DECREF(v); |
| 198 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 201 | PyObject *o; |
| 202 | iterations++; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | i = PyLong_AS_LONG(v) + 1; |
| 205 | o = PyLong_FromLong(i); |
| 206 | if (o == NULL) |
| 207 | return -1; |
| 208 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 209 | Py_DECREF(o); |
| 210 | return -1; |
| 211 | } |
| 212 | Py_DECREF(o); |
| 213 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | Py_DECREF(dict); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | if (iterations != count) { |
| 218 | PyErr_SetString( |
| 219 | TestError, |
| 220 | "test_dict_iteration: dict iteration went wrong "); |
| 221 | return -1; |
| 222 | } else { |
| 223 | return 0; |
| 224 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 228 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | for (i = 0; i < 200; i++) { |
| 233 | if (test_dict_inner(i) < 0) { |
| 234 | return NULL; |
| 235 | } |
| 236 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 237 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 238 | Py_RETURN_NONE; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 241 | static PyObject* |
| 242 | dict_getitem_knownhash(PyObject *self, PyObject *args) |
| 243 | { |
| 244 | PyObject *mp, *key, *result; |
| 245 | Py_ssize_t hash; |
| 246 | |
| 247 | if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash", |
| 248 | &mp, &key, &hash)) { |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash); |
| 253 | if (result == NULL && !PyErr_Occurred()) { |
| 254 | _PyErr_SetKeyError(key); |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | Py_XINCREF(result); |
| 259 | return result; |
| 260 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 261 | |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 262 | static PyObject* |
| 263 | dict_hassplittable(PyObject *self, PyObject *arg) |
| 264 | { |
| 265 | if (!PyDict_Check(arg)) { |
| 266 | PyErr_Format(PyExc_TypeError, |
| 267 | "dict_hassplittable() argument must be dict, not '%s'", |
| 268 | arg->ob_type->tp_name); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg)); |
| 273 | } |
| 274 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 275 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 276 | * PyType_Ready if it hasn't already been called |
| 277 | */ |
| 278 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | PyVarObject_HEAD_INIT(NULL, 0) |
| 280 | "hashinheritancetester", /* Name of this type */ |
| 281 | sizeof(PyObject), /* Basic object size */ |
| 282 | 0, /* Item size for varobject */ |
| 283 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 284 | 0, /* tp_print */ |
| 285 | 0, /* tp_getattr */ |
| 286 | 0, /* tp_setattr */ |
| 287 | 0, /* tp_reserved */ |
| 288 | 0, /* tp_repr */ |
| 289 | 0, /* tp_as_number */ |
| 290 | 0, /* tp_as_sequence */ |
| 291 | 0, /* tp_as_mapping */ |
| 292 | 0, /* tp_hash */ |
| 293 | 0, /* tp_call */ |
| 294 | 0, /* tp_str */ |
| 295 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 296 | 0, /* tp_setattro */ |
| 297 | 0, /* tp_as_buffer */ |
| 298 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 299 | 0, /* tp_doc */ |
| 300 | 0, /* tp_traverse */ |
| 301 | 0, /* tp_clear */ |
| 302 | 0, /* tp_richcompare */ |
| 303 | 0, /* tp_weaklistoffset */ |
| 304 | 0, /* tp_iter */ |
| 305 | 0, /* tp_iternext */ |
| 306 | 0, /* tp_methods */ |
| 307 | 0, /* tp_members */ |
| 308 | 0, /* tp_getset */ |
| 309 | 0, /* tp_base */ |
| 310 | 0, /* tp_dict */ |
| 311 | 0, /* tp_descr_get */ |
| 312 | 0, /* tp_descr_set */ |
| 313 | 0, /* tp_dictoffset */ |
| 314 | 0, /* tp_init */ |
| 315 | 0, /* tp_alloc */ |
| 316 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | static PyObject* |
| 320 | test_lazy_hash_inheritance(PyObject* self) |
| 321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | PyTypeObject *type; |
| 323 | PyObject *obj; |
Antoine Pitrou | 29aad00 | 2010-10-23 19:42:38 +0000 | [diff] [blame] | 324 | Py_hash_t hash; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | if (type->tp_dict != NULL) |
| 329 | /* The type has already been initialized. This probably means |
| 330 | -R is being used. */ |
| 331 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 332 | |
| 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | obj = PyObject_New(PyObject, type); |
| 335 | if (obj == NULL) { |
| 336 | PyErr_Clear(); |
| 337 | PyErr_SetString( |
| 338 | TestError, |
| 339 | "test_lazy_hash_inheritance: failed to create object"); |
| 340 | return NULL; |
| 341 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | if (type->tp_dict != NULL) { |
| 344 | PyErr_SetString( |
| 345 | TestError, |
| 346 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 347 | Py_DECREF(obj); |
| 348 | return NULL; |
| 349 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | hash = PyObject_Hash(obj); |
| 352 | if ((hash == -1) && PyErr_Occurred()) { |
| 353 | PyErr_Clear(); |
| 354 | PyErr_SetString( |
| 355 | TestError, |
| 356 | "test_lazy_hash_inheritance: could not hash object"); |
| 357 | Py_DECREF(obj); |
| 358 | return NULL; |
| 359 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | if (type->tp_dict == NULL) { |
| 362 | PyErr_SetString( |
| 363 | TestError, |
| 364 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 365 | Py_DECREF(obj); |
| 366 | return NULL; |
| 367 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 370 | PyErr_SetString( |
| 371 | TestError, |
| 372 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 373 | Py_DECREF(obj); |
| 374 | return NULL; |
| 375 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | Py_RETURN_NONE; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 383 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 384 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 385 | |
| 386 | Note that the meat of the test is contained in testcapi_long.h. |
| 387 | This is revolting, but delicate code duplication is worse: "almost |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 388 | exactly the same" code is needed to test long long, but the ubiquitous |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 389 | dependence on type names makes it impossible to use a parameterized |
| 390 | function. A giant macro would be even worse than this. A C++ template |
| 391 | would be perfect. |
| 392 | |
| 393 | The "report an error" functions are deliberately not part of the #include |
| 394 | file: if the test fails, you can set a breakpoint in the appropriate |
| 395 | error function directly, and crawl back from there in the debugger. |
| 396 | */ |
| 397 | |
| 398 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 399 | |
| 400 | static PyObject * |
| 401 | raise_test_long_error(const char* msg) |
| 402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | #define TESTNAME test_long_api_inner |
| 407 | #define TYPENAME long |
| 408 | #define F_S_TO_PY PyLong_FromLong |
| 409 | #define F_PY_TO_S PyLong_AsLong |
| 410 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 411 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 412 | |
| 413 | #include "testcapi_long.h" |
| 414 | |
| 415 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 416 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | #undef TESTNAME |
| 422 | #undef TYPENAME |
| 423 | #undef F_S_TO_PY |
| 424 | #undef F_PY_TO_S |
| 425 | #undef F_U_TO_PY |
| 426 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 427 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 428 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 429 | raise_test_longlong_error(const char* msg) |
| 430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | #define TESTNAME test_longlong_api_inner |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 435 | #define TYPENAME long long |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | #define F_S_TO_PY PyLong_FromLongLong |
| 437 | #define F_PY_TO_S PyLong_AsLongLong |
| 438 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 439 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 440 | |
| 441 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 442 | |
| 443 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 444 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 449 | #undef TESTNAME |
| 450 | #undef TYPENAME |
| 451 | #undef F_S_TO_PY |
| 452 | #undef F_PY_TO_S |
| 453 | #undef F_U_TO_PY |
| 454 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 455 | |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 456 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 457 | is tested by test_long_api_inner. This test will concentrate on proper |
| 458 | handling of overflow. |
| 459 | */ |
| 460 | |
| 461 | static PyObject * |
| 462 | test_long_and_overflow(PyObject *self) |
| 463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | PyObject *num, *one, *temp; |
| 465 | long value; |
| 466 | int overflow; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | /* Test that overflow is set properly for a large value. */ |
| 469 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 470 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 471 | if (num == NULL) |
| 472 | return NULL; |
| 473 | overflow = 1234; |
| 474 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 475 | Py_DECREF(num); |
| 476 | if (value == -1 && PyErr_Occurred()) |
| 477 | return NULL; |
| 478 | if (value != -1) |
| 479 | return raiseTestError("test_long_and_overflow", |
| 480 | "return value was not set to -1"); |
| 481 | if (overflow != 1) |
| 482 | return raiseTestError("test_long_and_overflow", |
| 483 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | /* Same again, with num = LONG_MAX + 1 */ |
| 486 | num = PyLong_FromLong(LONG_MAX); |
| 487 | if (num == NULL) |
| 488 | return NULL; |
| 489 | one = PyLong_FromLong(1L); |
| 490 | if (one == NULL) { |
| 491 | Py_DECREF(num); |
| 492 | return NULL; |
| 493 | } |
| 494 | temp = PyNumber_Add(num, one); |
| 495 | Py_DECREF(one); |
| 496 | Py_DECREF(num); |
| 497 | num = temp; |
| 498 | if (num == NULL) |
| 499 | return NULL; |
| 500 | overflow = 0; |
| 501 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 502 | Py_DECREF(num); |
| 503 | if (value == -1 && PyErr_Occurred()) |
| 504 | return NULL; |
| 505 | if (value != -1) |
| 506 | return raiseTestError("test_long_and_overflow", |
| 507 | "return value was not set to -1"); |
| 508 | if (overflow != 1) |
| 509 | return raiseTestError("test_long_and_overflow", |
| 510 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | /* Test that overflow is set properly for a large negative value. */ |
| 513 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 514 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 515 | if (num == NULL) |
| 516 | return NULL; |
| 517 | overflow = 1234; |
| 518 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 519 | Py_DECREF(num); |
| 520 | if (value == -1 && PyErr_Occurred()) |
| 521 | return NULL; |
| 522 | if (value != -1) |
| 523 | return raiseTestError("test_long_and_overflow", |
| 524 | "return value was not set to -1"); |
| 525 | if (overflow != -1) |
| 526 | return raiseTestError("test_long_and_overflow", |
| 527 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | /* Same again, with num = LONG_MIN - 1 */ |
| 530 | num = PyLong_FromLong(LONG_MIN); |
| 531 | if (num == NULL) |
| 532 | return NULL; |
| 533 | one = PyLong_FromLong(1L); |
| 534 | if (one == NULL) { |
| 535 | Py_DECREF(num); |
| 536 | return NULL; |
| 537 | } |
| 538 | temp = PyNumber_Subtract(num, one); |
| 539 | Py_DECREF(one); |
| 540 | Py_DECREF(num); |
| 541 | num = temp; |
| 542 | if (num == NULL) |
| 543 | return NULL; |
| 544 | overflow = 0; |
| 545 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 546 | Py_DECREF(num); |
| 547 | if (value == -1 && PyErr_Occurred()) |
| 548 | return NULL; |
| 549 | if (value != -1) |
| 550 | return raiseTestError("test_long_and_overflow", |
| 551 | "return value was not set to -1"); |
| 552 | if (overflow != -1) |
| 553 | return raiseTestError("test_long_and_overflow", |
| 554 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 556 | /* Test that overflow is cleared properly for small values. */ |
| 557 | num = PyLong_FromString("FF", NULL, 16); |
| 558 | if (num == NULL) |
| 559 | return NULL; |
| 560 | overflow = 1234; |
| 561 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 562 | Py_DECREF(num); |
| 563 | if (value == -1 && PyErr_Occurred()) |
| 564 | return NULL; |
| 565 | if (value != 0xFF) |
| 566 | return raiseTestError("test_long_and_overflow", |
| 567 | "expected return value 0xFF"); |
| 568 | if (overflow != 0) |
| 569 | return raiseTestError("test_long_and_overflow", |
| 570 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | num = PyLong_FromString("-FF", NULL, 16); |
| 573 | if (num == NULL) |
| 574 | return NULL; |
| 575 | overflow = 0; |
| 576 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 577 | Py_DECREF(num); |
| 578 | if (value == -1 && PyErr_Occurred()) |
| 579 | return NULL; |
| 580 | if (value != -0xFF) |
| 581 | return raiseTestError("test_long_and_overflow", |
| 582 | "expected return value 0xFF"); |
| 583 | if (overflow != 0) |
| 584 | return raiseTestError("test_long_and_overflow", |
| 585 | "overflow was set incorrectly"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | num = PyLong_FromLong(LONG_MAX); |
| 588 | if (num == NULL) |
| 589 | return NULL; |
| 590 | overflow = 1234; |
| 591 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 592 | Py_DECREF(num); |
| 593 | if (value == -1 && PyErr_Occurred()) |
| 594 | return NULL; |
| 595 | if (value != LONG_MAX) |
| 596 | return raiseTestError("test_long_and_overflow", |
| 597 | "expected return value LONG_MAX"); |
| 598 | if (overflow != 0) |
| 599 | return raiseTestError("test_long_and_overflow", |
| 600 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | num = PyLong_FromLong(LONG_MIN); |
| 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 != LONG_MIN) |
| 611 | return raiseTestError("test_long_and_overflow", |
| 612 | "expected return value LONG_MIN"); |
| 613 | if (overflow != 0) |
| 614 | return raiseTestError("test_long_and_overflow", |
| 615 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 616 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 617 | Py_RETURN_NONE; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 620 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 621 | long long is tested by test_long_api_inner. This test will |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 622 | concentrate on proper handling of overflow. |
| 623 | */ |
| 624 | |
| 625 | static PyObject * |
| 626 | test_long_long_and_overflow(PyObject *self) |
| 627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | PyObject *num, *one, *temp; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 629 | long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | int overflow; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | /* Test that overflow is set properly for a large value. */ |
| 633 | /* num is a number larger than PY_LLONG_MAX on a typical machine. */ |
| 634 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 635 | if (num == NULL) |
| 636 | return NULL; |
| 637 | overflow = 1234; |
| 638 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 639 | Py_DECREF(num); |
| 640 | if (value == -1 && PyErr_Occurred()) |
| 641 | return NULL; |
| 642 | if (value != -1) |
| 643 | return raiseTestError("test_long_long_and_overflow", |
| 644 | "return value was not set to -1"); |
| 645 | if (overflow != 1) |
| 646 | return raiseTestError("test_long_long_and_overflow", |
| 647 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | /* Same again, with num = PY_LLONG_MAX + 1 */ |
| 650 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 651 | if (num == NULL) |
| 652 | return NULL; |
| 653 | one = PyLong_FromLong(1L); |
| 654 | if (one == NULL) { |
| 655 | Py_DECREF(num); |
| 656 | return NULL; |
| 657 | } |
| 658 | temp = PyNumber_Add(num, one); |
| 659 | Py_DECREF(one); |
| 660 | Py_DECREF(num); |
| 661 | num = temp; |
| 662 | if (num == NULL) |
| 663 | return NULL; |
| 664 | overflow = 0; |
| 665 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 666 | Py_DECREF(num); |
| 667 | if (value == -1 && PyErr_Occurred()) |
| 668 | return NULL; |
| 669 | if (value != -1) |
| 670 | return raiseTestError("test_long_long_and_overflow", |
| 671 | "return value was not set to -1"); |
| 672 | if (overflow != 1) |
| 673 | return raiseTestError("test_long_long_and_overflow", |
| 674 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | /* Test that overflow is set properly for a large negative value. */ |
| 677 | /* num is a number smaller than PY_LLONG_MIN on a typical platform */ |
| 678 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 679 | if (num == NULL) |
| 680 | return NULL; |
| 681 | overflow = 1234; |
| 682 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 683 | Py_DECREF(num); |
| 684 | if (value == -1 && PyErr_Occurred()) |
| 685 | return NULL; |
| 686 | if (value != -1) |
| 687 | return raiseTestError("test_long_long_and_overflow", |
| 688 | "return value was not set to -1"); |
| 689 | if (overflow != -1) |
| 690 | return raiseTestError("test_long_long_and_overflow", |
| 691 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | /* Same again, with num = PY_LLONG_MIN - 1 */ |
| 694 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 695 | if (num == NULL) |
| 696 | return NULL; |
| 697 | one = PyLong_FromLong(1L); |
| 698 | if (one == NULL) { |
| 699 | Py_DECREF(num); |
| 700 | return NULL; |
| 701 | } |
| 702 | temp = PyNumber_Subtract(num, one); |
| 703 | Py_DECREF(one); |
| 704 | Py_DECREF(num); |
| 705 | num = temp; |
| 706 | if (num == NULL) |
| 707 | return NULL; |
| 708 | overflow = 0; |
| 709 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 710 | Py_DECREF(num); |
| 711 | if (value == -1 && PyErr_Occurred()) |
| 712 | return NULL; |
| 713 | if (value != -1) |
| 714 | return raiseTestError("test_long_long_and_overflow", |
| 715 | "return value was not set to -1"); |
| 716 | if (overflow != -1) |
| 717 | return raiseTestError("test_long_long_and_overflow", |
| 718 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | /* Test that overflow is cleared properly for small values. */ |
| 721 | num = PyLong_FromString("FF", NULL, 16); |
| 722 | if (num == NULL) |
| 723 | return NULL; |
| 724 | overflow = 1234; |
| 725 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 726 | Py_DECREF(num); |
| 727 | if (value == -1 && PyErr_Occurred()) |
| 728 | return NULL; |
| 729 | if (value != 0xFF) |
| 730 | return raiseTestError("test_long_long_and_overflow", |
| 731 | "expected return value 0xFF"); |
| 732 | if (overflow != 0) |
| 733 | return raiseTestError("test_long_long_and_overflow", |
| 734 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | num = PyLong_FromString("-FF", NULL, 16); |
| 737 | if (num == NULL) |
| 738 | return NULL; |
| 739 | overflow = 0; |
| 740 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 741 | Py_DECREF(num); |
| 742 | if (value == -1 && PyErr_Occurred()) |
| 743 | return NULL; |
| 744 | if (value != -0xFF) |
| 745 | return raiseTestError("test_long_long_and_overflow", |
| 746 | "expected return value 0xFF"); |
| 747 | if (overflow != 0) |
| 748 | return raiseTestError("test_long_long_and_overflow", |
| 749 | "overflow was set incorrectly"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 752 | if (num == NULL) |
| 753 | return NULL; |
| 754 | overflow = 1234; |
| 755 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 756 | Py_DECREF(num); |
| 757 | if (value == -1 && PyErr_Occurred()) |
| 758 | return NULL; |
| 759 | if (value != PY_LLONG_MAX) |
| 760 | return raiseTestError("test_long_long_and_overflow", |
| 761 | "expected return value PY_LLONG_MAX"); |
| 762 | if (overflow != 0) |
| 763 | return raiseTestError("test_long_long_and_overflow", |
| 764 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 767 | if (num == NULL) |
| 768 | return NULL; |
| 769 | overflow = 0; |
| 770 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 771 | Py_DECREF(num); |
| 772 | if (value == -1 && PyErr_Occurred()) |
| 773 | return NULL; |
| 774 | if (value != PY_LLONG_MIN) |
| 775 | return raiseTestError("test_long_long_and_overflow", |
| 776 | "expected return value PY_LLONG_MIN"); |
| 777 | if (overflow != 0) |
| 778 | return raiseTestError("test_long_long_and_overflow", |
| 779 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 780 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 781 | Py_RETURN_NONE; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 784 | /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that |
| 785 | non-integer arguments are handled correctly. It should be extended to |
| 786 | test overflow handling. |
| 787 | */ |
| 788 | |
| 789 | static PyObject * |
| 790 | test_long_as_size_t(PyObject *self) |
| 791 | { |
| 792 | size_t out_u; |
| 793 | Py_ssize_t out_s; |
| 794 | |
| 795 | Py_INCREF(Py_None); |
| 796 | |
| 797 | out_u = PyLong_AsSize_t(Py_None); |
| 798 | if (out_u != (size_t)-1 || !PyErr_Occurred()) |
| 799 | return raiseTestError("test_long_as_size_t", |
| 800 | "PyLong_AsSize_t(None) didn't complain"); |
| 801 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 802 | return raiseTestError("test_long_as_size_t", |
| 803 | "PyLong_AsSize_t(None) raised " |
| 804 | "something other than TypeError"); |
| 805 | PyErr_Clear(); |
| 806 | |
| 807 | out_s = PyLong_AsSsize_t(Py_None); |
| 808 | if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) |
| 809 | return raiseTestError("test_long_as_size_t", |
| 810 | "PyLong_AsSsize_t(None) didn't complain"); |
| 811 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 812 | return raiseTestError("test_long_as_size_t", |
| 813 | "PyLong_AsSsize_t(None) raised " |
| 814 | "something other than TypeError"); |
| 815 | PyErr_Clear(); |
| 816 | |
| 817 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 818 | return Py_None; |
| 819 | } |
| 820 | |
| 821 | /* Test the PyLong_AsDouble API. At present this just tests that |
| 822 | non-integer arguments are handled correctly. |
| 823 | */ |
| 824 | |
| 825 | static PyObject * |
| 826 | test_long_as_double(PyObject *self) |
| 827 | { |
| 828 | double out; |
| 829 | |
| 830 | Py_INCREF(Py_None); |
| 831 | |
| 832 | out = PyLong_AsDouble(Py_None); |
| 833 | if (out != -1.0 || !PyErr_Occurred()) |
| 834 | return raiseTestError("test_long_as_double", |
| 835 | "PyLong_AsDouble(None) didn't complain"); |
| 836 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 837 | return raiseTestError("test_long_as_double", |
| 838 | "PyLong_AsDouble(None) raised " |
| 839 | "something other than TypeError"); |
| 840 | PyErr_Clear(); |
| 841 | |
| 842 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 843 | return Py_None; |
| 844 | } |
| 845 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 846 | /* Test the L code for PyArg_ParseTuple. This should deliver a long long |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 847 | for both long and int arguments. The test may leak a little memory if |
| 848 | it fails. |
| 849 | */ |
| 850 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 851 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | PyObject *tuple, *num; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 854 | long long value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | tuple = PyTuple_New(1); |
| 857 | if (tuple == NULL) |
| 858 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | num = PyLong_FromLong(42); |
| 861 | if (num == NULL) |
| 862 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | value = -1; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 867 | if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 869 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | if (value != 42) |
| 871 | return raiseTestError("test_L_code", |
| 872 | "L code returned wrong value for long 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | Py_DECREF(num); |
| 875 | num = PyLong_FromLong(42); |
| 876 | if (num == NULL) |
| 877 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 880 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 881 | value = -1; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 882 | if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 884 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | if (value != 42) |
| 886 | return raiseTestError("test_L_code", |
| 887 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 890 | Py_RETURN_NONE; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 893 | static PyObject * |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 894 | return_none(void *unused) |
| 895 | { |
| 896 | Py_RETURN_NONE; |
| 897 | } |
| 898 | |
| 899 | static PyObject * |
| 900 | raise_error(void *unused) |
| 901 | { |
| 902 | PyErr_SetNone(PyExc_ValueError); |
| 903 | return NULL; |
| 904 | } |
| 905 | |
| 906 | static int |
| 907 | test_buildvalue_N_error(const char *fmt) |
| 908 | { |
| 909 | PyObject *arg, *res; |
| 910 | |
| 911 | arg = PyList_New(0); |
| 912 | if (arg == NULL) { |
| 913 | return -1; |
| 914 | } |
| 915 | |
| 916 | Py_INCREF(arg); |
| 917 | res = Py_BuildValue(fmt, return_none, NULL, arg); |
| 918 | if (res == NULL) { |
| 919 | return -1; |
| 920 | } |
| 921 | Py_DECREF(res); |
| 922 | if (Py_REFCNT(arg) != 1) { |
| 923 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 924 | "arg was not decrefed in successful " |
| 925 | "Py_BuildValue(\"%s\")", fmt); |
| 926 | return -1; |
| 927 | } |
| 928 | |
| 929 | Py_INCREF(arg); |
| 930 | res = Py_BuildValue(fmt, raise_error, NULL, arg); |
| 931 | if (res != NULL || !PyErr_Occurred()) { |
| 932 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 933 | "Py_BuildValue(\"%s\") didn't complain", fmt); |
| 934 | return -1; |
| 935 | } |
| 936 | PyErr_Clear(); |
| 937 | if (Py_REFCNT(arg) != 1) { |
| 938 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 939 | "arg was not decrefed in failed " |
| 940 | "Py_BuildValue(\"%s\")", fmt); |
| 941 | return -1; |
| 942 | } |
| 943 | Py_DECREF(arg); |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static PyObject * |
| 948 | test_buildvalue_N(PyObject *self, PyObject *noargs) |
| 949 | { |
| 950 | PyObject *arg, *res; |
| 951 | |
| 952 | arg = PyList_New(0); |
| 953 | if (arg == NULL) { |
| 954 | return NULL; |
| 955 | } |
| 956 | Py_INCREF(arg); |
| 957 | res = Py_BuildValue("N", arg); |
| 958 | if (res == NULL) { |
| 959 | return NULL; |
| 960 | } |
| 961 | if (res != arg) { |
| 962 | return raiseTestError("test_buildvalue_N", |
| 963 | "Py_BuildValue(\"N\") returned wrong result"); |
| 964 | } |
| 965 | if (Py_REFCNT(arg) != 2) { |
| 966 | return raiseTestError("test_buildvalue_N", |
| 967 | "arg was not decrefed in Py_BuildValue(\"N\")"); |
| 968 | } |
| 969 | Py_DECREF(res); |
| 970 | Py_DECREF(arg); |
| 971 | |
| 972 | if (test_buildvalue_N_error("O&N") < 0) |
| 973 | return NULL; |
| 974 | if (test_buildvalue_N_error("(O&N)") < 0) |
| 975 | return NULL; |
| 976 | if (test_buildvalue_N_error("[O&N]") < 0) |
| 977 | return NULL; |
| 978 | if (test_buildvalue_N_error("{O&N}") < 0) |
| 979 | return NULL; |
| 980 | if (test_buildvalue_N_error("{()O&(())N}") < 0) |
| 981 | return NULL; |
| 982 | |
| 983 | Py_RETURN_NONE; |
| 984 | } |
| 985 | |
| 986 | |
| 987 | static PyObject * |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 988 | get_args(PyObject *self, PyObject *args) |
| 989 | { |
| 990 | if (args == NULL) { |
| 991 | args = Py_None; |
| 992 | } |
| 993 | Py_INCREF(args); |
| 994 | return args; |
| 995 | } |
| 996 | |
| 997 | static PyObject * |
| 998 | get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
| 999 | { |
| 1000 | if (kwargs == NULL) { |
| 1001 | kwargs = Py_None; |
| 1002 | } |
| 1003 | Py_INCREF(kwargs); |
| 1004 | return kwargs; |
| 1005 | } |
| 1006 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1007 | /* Test tuple argument processing */ |
| 1008 | static PyObject * |
| 1009 | getargs_tuple(PyObject *self, PyObject *args) |
| 1010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | int a, b, c; |
| 1012 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 1013 | return NULL; |
| 1014 | return Py_BuildValue("iii", a, b, c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1017 | /* test PyArg_ParseTupleAndKeywords */ |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1018 | static PyObject * |
| 1019 | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1020 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1022 | static const char fmt[] = "(ii)i|(i(ii))(iii)i"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 1026 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 1027 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 1028 | return NULL; |
| 1029 | return Py_BuildValue("iiiiiiiiii", |
| 1030 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 1031 | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1034 | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
| 1035 | static PyObject * |
| 1036 | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1037 | { |
| 1038 | static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
| 1039 | int required = -1; |
| 1040 | int optional = -1; |
| 1041 | int keyword_only = -1; |
| 1042 | |
| 1043 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
| 1044 | &required, &optional, &keyword_only)) |
| 1045 | return NULL; |
| 1046 | return Py_BuildValue("iii", required, optional, keyword_only); |
| 1047 | } |
| 1048 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1049 | /* test PyArg_ParseTupleAndKeywords positional-only arguments */ |
| 1050 | static PyObject * |
| 1051 | getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1052 | { |
| 1053 | static char *keywords[] = {"", "", "keyword", NULL}; |
| 1054 | int required = -1; |
| 1055 | int optional = -1; |
| 1056 | int keyword = -1; |
| 1057 | |
| 1058 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords, |
| 1059 | &required, &optional, &keyword)) |
| 1060 | return NULL; |
| 1061 | return Py_BuildValue("iii", required, optional, keyword); |
| 1062 | } |
| 1063 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1064 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 1065 | and return the result. |
| 1066 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1067 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1068 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | unsigned char value; |
| 1071 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 1072 | return NULL; |
| 1073 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1076 | static PyObject * |
| 1077 | getargs_B(PyObject *self, PyObject *args) |
| 1078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | unsigned char value; |
| 1080 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 1081 | return NULL; |
| 1082 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1086 | getargs_h(PyObject *self, PyObject *args) |
| 1087 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | short value; |
| 1089 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 1090 | return NULL; |
| 1091 | return PyLong_FromLong((long)value); |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1095 | getargs_H(PyObject *self, PyObject *args) |
| 1096 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | unsigned short value; |
| 1098 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 1099 | return NULL; |
| 1100 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static PyObject * |
| 1104 | getargs_I(PyObject *self, PyObject *args) |
| 1105 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | unsigned int value; |
| 1107 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 1108 | return NULL; |
| 1109 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | static PyObject * |
| 1113 | getargs_k(PyObject *self, PyObject *args) |
| 1114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | unsigned long value; |
| 1116 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 1117 | return NULL; |
| 1118 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | static PyObject * |
| 1122 | getargs_i(PyObject *self, PyObject *args) |
| 1123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | int value; |
| 1125 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 1126 | return NULL; |
| 1127 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1130 | static PyObject * |
| 1131 | getargs_l(PyObject *self, PyObject *args) |
| 1132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | long value; |
| 1134 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 1135 | return NULL; |
| 1136 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1139 | static PyObject * |
| 1140 | getargs_n(PyObject *self, PyObject *args) |
| 1141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | Py_ssize_t value; |
| 1143 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 1144 | return NULL; |
| 1145 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 1148 | static PyObject * |
| 1149 | getargs_p(PyObject *self, PyObject *args) |
| 1150 | { |
| 1151 | int value; |
| 1152 | if (!PyArg_ParseTuple(args, "p", &value)) |
| 1153 | return NULL; |
| 1154 | return PyLong_FromLong(value); |
| 1155 | } |
| 1156 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1157 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1158 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1159 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1160 | long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 1162 | return NULL; |
| 1163 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
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_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1168 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1169 | unsigned long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 1171 | return NULL; |
| 1172 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1173 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1174 | |
| 1175 | /* This function not only tests the 'k' getargs code, but also the |
orenmn | 698845e | 2017-03-02 13:29:20 +0200 | [diff] [blame] | 1176 | PyLong_AsUnsignedLongMask() function. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1177 | static PyObject * |
| 1178 | test_k_code(PyObject *self) |
| 1179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | PyObject *tuple, *num; |
| 1181 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | tuple = PyTuple_New(1); |
| 1184 | if (tuple == NULL) |
| 1185 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 1188 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 1189 | if (num == NULL) |
| 1190 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | value = PyLong_AsUnsignedLongMask(num); |
| 1193 | if (value != ULONG_MAX) |
| 1194 | return raiseTestError("test_k_code", |
Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 1195 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1200 | if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1202 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | if (value != ULONG_MAX) |
| 1204 | return raiseTestError("test_k_code", |
| 1205 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | Py_DECREF(num); |
| 1208 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 1209 | if (num == NULL) |
| 1210 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | value = PyLong_AsUnsignedLongMask(num); |
| 1213 | if (value != (unsigned long)-0x42) |
| 1214 | return raiseTestError("test_k_code", |
orenmn | 698845e | 2017-03-02 13:29:20 +0200 | [diff] [blame] | 1215 | "PyLong_AsUnsignedLongMask() returned wrong " |
| 1216 | "value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1221 | if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1223 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | if (value != (unsigned long)-0x42) |
| 1225 | return raiseTestError("test_k_code", |
| 1226 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1229 | Py_RETURN_NONE; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1232 | static PyObject * |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 1233 | getargs_f(PyObject *self, PyObject *args) |
| 1234 | { |
| 1235 | float f; |
| 1236 | if (!PyArg_ParseTuple(args, "f", &f)) |
| 1237 | return NULL; |
| 1238 | return PyFloat_FromDouble(f); |
| 1239 | } |
| 1240 | |
| 1241 | static PyObject * |
| 1242 | getargs_d(PyObject *self, PyObject *args) |
| 1243 | { |
| 1244 | double d; |
| 1245 | if (!PyArg_ParseTuple(args, "d", &d)) |
| 1246 | return NULL; |
| 1247 | return PyFloat_FromDouble(d); |
| 1248 | } |
| 1249 | |
| 1250 | static PyObject * |
| 1251 | getargs_D(PyObject *self, PyObject *args) |
| 1252 | { |
| 1253 | Py_complex cval; |
| 1254 | if (!PyArg_ParseTuple(args, "D", &cval)) |
| 1255 | return NULL; |
| 1256 | return PyComplex_FromCComplex(cval); |
| 1257 | } |
| 1258 | |
| 1259 | static PyObject * |
| 1260 | getargs_S(PyObject *self, PyObject *args) |
| 1261 | { |
| 1262 | PyObject *obj; |
| 1263 | if (!PyArg_ParseTuple(args, "S", &obj)) |
| 1264 | return NULL; |
| 1265 | Py_INCREF(obj); |
| 1266 | return obj; |
| 1267 | } |
| 1268 | |
| 1269 | static PyObject * |
| 1270 | getargs_Y(PyObject *self, PyObject *args) |
| 1271 | { |
| 1272 | PyObject *obj; |
| 1273 | if (!PyArg_ParseTuple(args, "Y", &obj)) |
| 1274 | return NULL; |
| 1275 | Py_INCREF(obj); |
| 1276 | return obj; |
| 1277 | } |
| 1278 | |
| 1279 | static PyObject * |
| 1280 | getargs_U(PyObject *self, PyObject *args) |
| 1281 | { |
| 1282 | PyObject *obj; |
| 1283 | if (!PyArg_ParseTuple(args, "U", &obj)) |
| 1284 | return NULL; |
| 1285 | Py_INCREF(obj); |
| 1286 | return obj; |
| 1287 | } |
| 1288 | |
| 1289 | static PyObject * |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1290 | getargs_c(PyObject *self, PyObject *args) |
| 1291 | { |
| 1292 | char c; |
| 1293 | if (!PyArg_ParseTuple(args, "c", &c)) |
| 1294 | return NULL; |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1295 | return PyLong_FromLong((unsigned char)c); |
| 1296 | } |
| 1297 | |
| 1298 | static PyObject * |
| 1299 | getargs_C(PyObject *self, PyObject *args) |
| 1300 | { |
| 1301 | int c; |
| 1302 | if (!PyArg_ParseTuple(args, "C", &c)) |
| 1303 | return NULL; |
| 1304 | return PyLong_FromLong(c); |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | static PyObject * |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1308 | getargs_s(PyObject *self, PyObject *args) |
| 1309 | { |
| 1310 | char *str; |
| 1311 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1312 | return NULL; |
| 1313 | return PyBytes_FromString(str); |
| 1314 | } |
| 1315 | |
| 1316 | static PyObject * |
| 1317 | getargs_s_star(PyObject *self, PyObject *args) |
| 1318 | { |
| 1319 | Py_buffer buffer; |
| 1320 | PyObject *bytes; |
| 1321 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1322 | return NULL; |
| 1323 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1324 | PyBuffer_Release(&buffer); |
| 1325 | return bytes; |
| 1326 | } |
| 1327 | |
| 1328 | static PyObject * |
| 1329 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1330 | { |
| 1331 | char *str; |
| 1332 | Py_ssize_t size; |
| 1333 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1334 | return NULL; |
| 1335 | return PyBytes_FromStringAndSize(str, size); |
| 1336 | } |
| 1337 | |
| 1338 | static PyObject * |
| 1339 | getargs_z(PyObject *self, PyObject *args) |
| 1340 | { |
| 1341 | char *str; |
| 1342 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1343 | return NULL; |
| 1344 | if (str != NULL) |
| 1345 | return PyBytes_FromString(str); |
| 1346 | else |
| 1347 | Py_RETURN_NONE; |
| 1348 | } |
| 1349 | |
| 1350 | static PyObject * |
| 1351 | getargs_z_star(PyObject *self, PyObject *args) |
| 1352 | { |
| 1353 | Py_buffer buffer; |
| 1354 | PyObject *bytes; |
| 1355 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1356 | return NULL; |
| 1357 | if (buffer.buf != NULL) |
| 1358 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1359 | else { |
| 1360 | Py_INCREF(Py_None); |
| 1361 | bytes = Py_None; |
| 1362 | } |
| 1363 | PyBuffer_Release(&buffer); |
| 1364 | return bytes; |
| 1365 | } |
| 1366 | |
| 1367 | static PyObject * |
| 1368 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1369 | { |
| 1370 | char *str; |
| 1371 | Py_ssize_t size; |
| 1372 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1373 | return NULL; |
| 1374 | if (str != NULL) |
| 1375 | return PyBytes_FromStringAndSize(str, size); |
| 1376 | else |
| 1377 | Py_RETURN_NONE; |
| 1378 | } |
| 1379 | |
| 1380 | static PyObject * |
| 1381 | getargs_y(PyObject *self, PyObject *args) |
| 1382 | { |
| 1383 | char *str; |
| 1384 | if (!PyArg_ParseTuple(args, "y", &str)) |
| 1385 | return NULL; |
| 1386 | return PyBytes_FromString(str); |
| 1387 | } |
| 1388 | |
| 1389 | static PyObject * |
| 1390 | getargs_y_star(PyObject *self, PyObject *args) |
| 1391 | { |
| 1392 | Py_buffer buffer; |
| 1393 | PyObject *bytes; |
| 1394 | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 1395 | return NULL; |
| 1396 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1397 | PyBuffer_Release(&buffer); |
| 1398 | return bytes; |
| 1399 | } |
| 1400 | |
| 1401 | static PyObject * |
| 1402 | getargs_y_hash(PyObject *self, PyObject *args) |
| 1403 | { |
| 1404 | char *str; |
| 1405 | Py_ssize_t size; |
| 1406 | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
| 1407 | return NULL; |
| 1408 | return PyBytes_FromStringAndSize(str, size); |
| 1409 | } |
| 1410 | |
| 1411 | static PyObject * |
| 1412 | getargs_u(PyObject *self, PyObject *args) |
| 1413 | { |
| 1414 | Py_UNICODE *str; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1415 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1416 | return NULL; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1417 | return PyUnicode_FromWideChar(str, -1); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | static PyObject * |
| 1421 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1422 | { |
| 1423 | Py_UNICODE *str; |
| 1424 | Py_ssize_t size; |
| 1425 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1426 | return NULL; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1427 | return PyUnicode_FromWideChar(str, size); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | static PyObject * |
| 1431 | getargs_Z(PyObject *self, PyObject *args) |
| 1432 | { |
| 1433 | Py_UNICODE *str; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1434 | if (!PyArg_ParseTuple(args, "Z", &str)) |
| 1435 | return NULL; |
| 1436 | if (str != NULL) { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1437 | return PyUnicode_FromWideChar(str, -1); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1438 | } else |
| 1439 | Py_RETURN_NONE; |
| 1440 | } |
| 1441 | |
| 1442 | static PyObject * |
| 1443 | getargs_Z_hash(PyObject *self, PyObject *args) |
| 1444 | { |
| 1445 | Py_UNICODE *str; |
| 1446 | Py_ssize_t size; |
| 1447 | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
| 1448 | return NULL; |
| 1449 | if (str != NULL) |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1450 | return PyUnicode_FromWideChar(str, size); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1451 | else |
| 1452 | Py_RETURN_NONE; |
| 1453 | } |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1454 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1455 | static PyObject * |
| 1456 | getargs_es(PyObject *self, PyObject *args) |
| 1457 | { |
| 1458 | PyObject *arg, *result; |
| 1459 | const char *encoding = NULL; |
| 1460 | char *str; |
| 1461 | |
| 1462 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1463 | return NULL; |
| 1464 | if (!PyArg_Parse(arg, "es", encoding, &str)) |
| 1465 | return NULL; |
| 1466 | result = PyBytes_FromString(str); |
| 1467 | PyMem_Free(str); |
| 1468 | return result; |
| 1469 | } |
| 1470 | |
| 1471 | static PyObject * |
| 1472 | getargs_et(PyObject *self, PyObject *args) |
| 1473 | { |
| 1474 | PyObject *arg, *result; |
| 1475 | const char *encoding = NULL; |
| 1476 | char *str; |
| 1477 | |
| 1478 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1479 | return NULL; |
| 1480 | if (!PyArg_Parse(arg, "et", encoding, &str)) |
| 1481 | return NULL; |
| 1482 | result = PyBytes_FromString(str); |
| 1483 | PyMem_Free(str); |
| 1484 | return result; |
| 1485 | } |
| 1486 | |
| 1487 | static PyObject * |
| 1488 | getargs_es_hash(PyObject *self, PyObject *args) |
| 1489 | { |
| 1490 | PyObject *arg, *result; |
| 1491 | const char *encoding = NULL; |
| 1492 | PyByteArrayObject *buffer = NULL; |
| 1493 | char *str = NULL; |
| 1494 | Py_ssize_t size; |
| 1495 | |
| 1496 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1497 | return NULL; |
| 1498 | if (buffer != NULL) { |
| 1499 | str = PyByteArray_AS_STRING(buffer); |
| 1500 | size = PyByteArray_GET_SIZE(buffer); |
| 1501 | } |
| 1502 | if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) |
| 1503 | return NULL; |
| 1504 | result = PyBytes_FromStringAndSize(str, size); |
| 1505 | if (buffer == NULL) |
| 1506 | PyMem_Free(str); |
| 1507 | return result; |
| 1508 | } |
| 1509 | |
| 1510 | static PyObject * |
| 1511 | getargs_et_hash(PyObject *self, PyObject *args) |
| 1512 | { |
| 1513 | PyObject *arg, *result; |
| 1514 | const char *encoding = NULL; |
| 1515 | PyByteArrayObject *buffer = NULL; |
| 1516 | char *str = NULL; |
| 1517 | Py_ssize_t size; |
| 1518 | |
| 1519 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1520 | return NULL; |
| 1521 | if (buffer != NULL) { |
| 1522 | str = PyByteArray_AS_STRING(buffer); |
| 1523 | size = PyByteArray_GET_SIZE(buffer); |
| 1524 | } |
| 1525 | if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) |
| 1526 | return NULL; |
| 1527 | result = PyBytes_FromStringAndSize(str, size); |
| 1528 | if (buffer == NULL) |
| 1529 | PyMem_Free(str); |
| 1530 | return result; |
| 1531 | } |
| 1532 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1533 | /* Test the s and z codes for PyArg_ParseTuple. |
| 1534 | */ |
| 1535 | static PyObject * |
| 1536 | test_s_code(PyObject *self) |
| 1537 | { |
| 1538 | /* Unicode strings should be accepted */ |
| 1539 | PyObject *tuple, *obj; |
| 1540 | char *value; |
| 1541 | |
| 1542 | tuple = PyTuple_New(1); |
| 1543 | if (tuple == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1545 | |
| 1546 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1548 | if (obj == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1550 | |
| 1551 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1552 | |
| 1553 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1554 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1555 | */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1556 | if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) { |
| 1557 | return NULL; |
| 1558 | } |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1559 | |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1560 | if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) { |
| 1561 | return NULL; |
| 1562 | } |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1563 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1564 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1565 | Py_RETURN_NONE; |
| 1566 | } |
| 1567 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1568 | static PyObject * |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1569 | parse_tuple_and_keywords(PyObject *self, PyObject *args) |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1570 | { |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1571 | PyObject *sub_args; |
| 1572 | PyObject *sub_kwargs; |
Serhiy Storchaka | 5f161fd | 2017-05-04 00:03:23 +0300 | [diff] [blame] | 1573 | const char *sub_format; |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1574 | PyObject *sub_keywords; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1575 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1576 | Py_ssize_t i, size; |
| 1577 | char *keywords[8 + 1]; /* space for NULL at end */ |
| 1578 | PyObject *o; |
| 1579 | PyObject *converted[8]; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1580 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1581 | int result; |
| 1582 | PyObject *return_value = NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1583 | |
Larry Hastings | 22701e8 | 2012-08-08 14:52:22 -0700 | [diff] [blame] | 1584 | double buffers[8][4]; /* double ensures alignment where necessary */ |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1585 | |
Serhiy Storchaka | 5f161fd | 2017-05-04 00:03:23 +0300 | [diff] [blame] | 1586 | if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords", |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1587 | &sub_args, &sub_kwargs, |
| 1588 | &sub_format, &sub_keywords)) |
| 1589 | return NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1590 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1591 | if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
| 1592 | PyErr_SetString(PyExc_ValueError, |
| 1593 | "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
| 1594 | return NULL; |
| 1595 | } |
| 1596 | |
| 1597 | memset(buffers, 0, sizeof(buffers)); |
| 1598 | memset(converted, 0, sizeof(converted)); |
| 1599 | memset(keywords, 0, sizeof(keywords)); |
| 1600 | |
| 1601 | size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 1602 | if (size > 8) { |
| 1603 | PyErr_SetString(PyExc_ValueError, |
| 1604 | "parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 1605 | goto exit; |
| 1606 | } |
| 1607 | |
| 1608 | for (i = 0; i < size; i++) { |
| 1609 | o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 1610 | if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { |
| 1611 | PyErr_Format(PyExc_ValueError, |
Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 1612 | "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i); |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1613 | goto exit; |
| 1614 | } |
| 1615 | keywords[i] = PyBytes_AS_STRING(converted[i]); |
| 1616 | } |
| 1617 | |
| 1618 | result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 1619 | sub_format, keywords, |
| 1620 | buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 1621 | buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 1622 | |
| 1623 | if (result) { |
| 1624 | return_value = Py_None; |
| 1625 | Py_INCREF(Py_None); |
| 1626 | } |
| 1627 | |
| 1628 | exit: |
| 1629 | size = sizeof(converted) / sizeof(converted[0]); |
| 1630 | for (i = 0; i < size; i++) { |
| 1631 | Py_XDECREF(converted[i]); |
| 1632 | } |
| 1633 | return return_value; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1636 | static volatile int x; |
| 1637 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1638 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1639 | of an error. |
| 1640 | */ |
| 1641 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1642 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | PyObject *tuple, *obj; |
| 1645 | Py_UNICODE *value; |
| 1646 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1648 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1649 | /* Just use the macro and check that it compiles */ |
| 1650 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | tuple = PyTuple_New(1); |
| 1653 | if (tuple == NULL) |
| 1654 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1657 | "ascii", NULL); |
| 1658 | if (obj == NULL) |
| 1659 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1661 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1664 | if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1665 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1666 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1668 | return raiseTestError("test_u_code", |
| 1669 | "u code returned wrong value for u'test'"); |
| 1670 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1671 | if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1673 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1675 | len != PyUnicode_GET_SIZE(obj)) |
| 1676 | return raiseTestError("test_u_code", |
| 1677 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1680 | Py_RETURN_NONE; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1683 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1684 | static PyObject * |
| 1685 | test_Z_code(PyObject *self) |
| 1686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | PyObject *tuple, *obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1688 | const Py_UNICODE *value1, *value2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1691 | tuple = PyTuple_New(2); |
| 1692 | if (tuple == NULL) |
| 1693 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1695 | obj = PyUnicode_FromString("test"); |
| 1696 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1697 | Py_INCREF(Py_None); |
| 1698 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | /* swap values on purpose */ |
| 1701 | value1 = NULL; |
| 1702 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | /* Test Z for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1705 | if (!PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1707 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1708 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1709 | return raiseTestError("test_Z_code", |
| 1710 | "Z code returned wrong value for 'test'"); |
| 1711 | if (value2 != NULL) |
| 1712 | return raiseTestError("test_Z_code", |
| 1713 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | value1 = NULL; |
| 1716 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1717 | len1 = -1; |
| 1718 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | /* Test Z# for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1721 | if (!PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1722 | &value2, &len2)) |
| 1723 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1725 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1727 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1728 | return raiseTestError("test_Z_code", |
| 1729 | "Z# code returned wrong values for 'test'"); |
| 1730 | if (value2 != NULL || |
| 1731 | len2 != 0) |
| 1732 | return raiseTestError("test_Z_code", |
| 1733 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | Py_DECREF(tuple); |
| 1736 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1739 | static PyObject * |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1740 | test_widechar(PyObject *self) |
| 1741 | { |
| 1742 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1744 | size_t wtextlen = 1; |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1745 | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1746 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1748 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1749 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1753 | if (wide == NULL) |
| 1754 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1756 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1757 | if (utf8 == NULL) { |
| 1758 | Py_DECREF(wide); |
| 1759 | return NULL; |
| 1760 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1761 | |
Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1762 | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | Py_DECREF(wide); |
| 1764 | Py_DECREF(utf8); |
| 1765 | return raiseTestError("test_widechar", |
| 1766 | "wide string and utf8 string " |
| 1767 | "have different length"); |
| 1768 | } |
| 1769 | if (PyUnicode_Compare(wide, utf8)) { |
| 1770 | Py_DECREF(wide); |
| 1771 | Py_DECREF(utf8); |
| 1772 | if (PyErr_Occurred()) |
| 1773 | return NULL; |
| 1774 | return raiseTestError("test_widechar", |
| 1775 | "wide string and utf8 string " |
| 1776 | "are different"); |
| 1777 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | Py_DECREF(wide); |
| 1780 | Py_DECREF(utf8); |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1781 | |
| 1782 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1783 | wide = PyUnicode_FromWideChar(invalid, 1); |
| 1784 | if (wide == NULL) |
| 1785 | PyErr_Clear(); |
| 1786 | else |
| 1787 | return raiseTestError("test_widechar", |
| 1788 | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
| 1789 | |
| 1790 | wide = PyUnicode_FromUnicode(invalid, 1); |
| 1791 | if (wide == NULL) |
| 1792 | PyErr_Clear(); |
| 1793 | else |
| 1794 | return raiseTestError("test_widechar", |
| 1795 | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1796 | |
| 1797 | wide = PyUnicode_FromUnicode(NULL, 1); |
| 1798 | if (wide == NULL) |
| 1799 | return NULL; |
| 1800 | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1801 | if (_PyUnicode_Ready(wide) < 0) { |
| 1802 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1803 | PyErr_Clear(); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1804 | } |
| 1805 | else { |
| 1806 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1807 | return raiseTestError("test_widechar", |
| 1808 | "PyUnicode_Ready() didn't fail"); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1809 | } |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1810 | #endif |
| 1811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1812 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1816 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1817 | { |
| 1818 | PyObject *unicode, *result; |
| 1819 | Py_ssize_t buflen, size; |
| 1820 | wchar_t *buffer; |
| 1821 | |
| 1822 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1823 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1824 | buffer = PyMem_New(wchar_t, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1825 | if (buffer == NULL) |
| 1826 | return PyErr_NoMemory(); |
| 1827 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1828 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1829 | if (size == -1) { |
| 1830 | PyMem_Free(buffer); |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | |
| 1834 | if (size < buflen) |
| 1835 | buflen = size + 1; |
| 1836 | else |
| 1837 | buflen = size; |
| 1838 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 1839 | PyMem_Free(buffer); |
| 1840 | if (result == NULL) |
| 1841 | return NULL; |
| 1842 | |
| 1843 | return Py_BuildValue("(Nn)", result, size); |
| 1844 | } |
| 1845 | |
| 1846 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1847 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1848 | { |
| 1849 | PyObject *unicode, *result; |
| 1850 | Py_ssize_t size; |
| 1851 | wchar_t *buffer; |
| 1852 | |
| 1853 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 1854 | return NULL; |
| 1855 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1856 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1857 | if (buffer == NULL) |
| 1858 | return NULL; |
| 1859 | |
| 1860 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 1861 | PyMem_Free(buffer); |
| 1862 | if (result == NULL) |
| 1863 | return NULL; |
| 1864 | return Py_BuildValue("(Nn)", result, size); |
| 1865 | } |
| 1866 | |
| 1867 | static PyObject * |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 1868 | unicode_asucs4(PyObject *self, PyObject *args) |
| 1869 | { |
| 1870 | PyObject *unicode, *result; |
| 1871 | Py_UCS4 *buffer; |
| 1872 | int copy_null; |
| 1873 | Py_ssize_t str_len, buf_len; |
| 1874 | |
| 1875 | if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, ©_null)) { |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | |
| 1879 | buf_len = str_len + 1; |
| 1880 | buffer = PyMem_NEW(Py_UCS4, buf_len); |
| 1881 | if (buffer == NULL) { |
| 1882 | return PyErr_NoMemory(); |
| 1883 | } |
| 1884 | memset(buffer, 0, sizeof(Py_UCS4)*buf_len); |
| 1885 | buffer[str_len] = 0xffffU; |
| 1886 | |
| 1887 | if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) { |
| 1888 | PyMem_FREE(buffer); |
| 1889 | return NULL; |
| 1890 | } |
| 1891 | |
| 1892 | result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len); |
| 1893 | PyMem_FREE(buffer); |
| 1894 | return result; |
| 1895 | } |
| 1896 | |
| 1897 | static PyObject * |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 1898 | unicode_findchar(PyObject *self, PyObject *args) |
| 1899 | { |
| 1900 | PyObject *str; |
| 1901 | int direction; |
| 1902 | unsigned int ch; |
| 1903 | Py_ssize_t result; |
| 1904 | Py_ssize_t start, end; |
| 1905 | |
| 1906 | if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch, |
| 1907 | &start, &end, &direction)) { |
| 1908 | return NULL; |
| 1909 | } |
| 1910 | |
| 1911 | result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction); |
| 1912 | if (result == -2) |
| 1913 | return NULL; |
| 1914 | else |
| 1915 | return PyLong_FromSsize_t(result); |
| 1916 | } |
| 1917 | |
| 1918 | static PyObject * |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 1919 | unicode_copycharacters(PyObject *self, PyObject *args) |
| 1920 | { |
| 1921 | PyObject *from, *to, *to_copy; |
| 1922 | Py_ssize_t from_start, to_start, how_many, copied; |
| 1923 | |
| 1924 | if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start, |
| 1925 | &from, &from_start, &how_many)) { |
| 1926 | return NULL; |
| 1927 | } |
| 1928 | |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 1929 | if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to), |
| 1930 | PyUnicode_MAX_CHAR_VALUE(to)))) { |
| 1931 | return NULL; |
| 1932 | } |
| 1933 | if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) { |
| 1934 | Py_DECREF(to_copy); |
| 1935 | return NULL; |
| 1936 | } |
| 1937 | |
| 1938 | if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from, |
| 1939 | from_start, how_many)) < 0) { |
| 1940 | Py_DECREF(to_copy); |
| 1941 | return NULL; |
| 1942 | } |
| 1943 | |
| 1944 | return Py_BuildValue("(Nn)", to_copy, copied); |
| 1945 | } |
| 1946 | |
| 1947 | static PyObject * |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 1948 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 1949 | { |
| 1950 | Py_UNICODE *unicode; |
| 1951 | Py_ssize_t length; |
| 1952 | char *errors = NULL; |
| 1953 | PyObject *decimal; |
| 1954 | Py_ssize_t decimal_length, new_length; |
| 1955 | int res; |
| 1956 | |
| 1957 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 1958 | return NULL; |
| 1959 | |
| 1960 | decimal_length = length * 7; /* len('€') */ |
| 1961 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 1962 | if (decimal == NULL) |
| 1963 | return NULL; |
| 1964 | |
| 1965 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 1966 | PyBytes_AS_STRING(decimal), |
| 1967 | errors); |
| 1968 | if (res < 0) { |
| 1969 | Py_DECREF(decimal); |
| 1970 | return NULL; |
| 1971 | } |
| 1972 | |
| 1973 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 1974 | assert(new_length <= decimal_length); |
| 1975 | res = _PyBytes_Resize(&decimal, new_length); |
| 1976 | if (res < 0) |
| 1977 | return NULL; |
| 1978 | |
| 1979 | return decimal; |
| 1980 | } |
| 1981 | |
| 1982 | static PyObject * |
| 1983 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 1984 | { |
| 1985 | Py_UNICODE *unicode; |
| 1986 | Py_ssize_t length; |
| 1987 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 1988 | return NULL; |
| 1989 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 1990 | } |
| 1991 | |
| 1992 | static PyObject * |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 1993 | unicode_legacy_string(PyObject *self, PyObject *args) |
| 1994 | { |
| 1995 | Py_UNICODE *data; |
| 1996 | Py_ssize_t len; |
| 1997 | PyObject *u; |
| 1998 | |
| 1999 | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
| 2000 | return NULL; |
| 2001 | |
| 2002 | u = PyUnicode_FromUnicode(NULL, len); |
| 2003 | if (u == NULL) |
| 2004 | return NULL; |
| 2005 | |
| 2006 | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
| 2007 | |
| 2008 | if (len > 0) { /* The empty string is always ready. */ |
| 2009 | assert(!PyUnicode_IS_READY(u)); |
| 2010 | } |
| 2011 | |
| 2012 | return u; |
| 2013 | } |
| 2014 | |
| 2015 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2016 | getargs_w_star(PyObject *self, PyObject *args) |
| 2017 | { |
| 2018 | Py_buffer buffer; |
| 2019 | PyObject *result; |
| 2020 | char *str; |
| 2021 | |
| 2022 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 2023 | return NULL; |
| 2024 | |
| 2025 | if (2 <= buffer.len) { |
| 2026 | str = buffer.buf; |
| 2027 | str[0] = '['; |
| 2028 | str[buffer.len-1] = ']'; |
| 2029 | } |
| 2030 | |
| 2031 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 2032 | PyBuffer_Release(&buffer); |
| 2033 | return result; |
| 2034 | } |
| 2035 | |
| 2036 | |
| 2037 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2038 | test_empty_argparse(PyObject *self) |
| 2039 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 2041 | PyObject *tuple, *dict = NULL; |
| 2042 | static char *kwlist[] = {NULL}; |
| 2043 | int result; |
| 2044 | tuple = PyTuple_New(0); |
| 2045 | if (!tuple) |
| 2046 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2047 | if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | goto done; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2049 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | dict = PyDict_New(); |
| 2051 | if (!dict) |
| 2052 | goto done; |
| 2053 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2054 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | Py_DECREF(tuple); |
| 2056 | Py_XDECREF(dict); |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2057 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2059 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | else { |
| 2061 | Py_RETURN_NONE; |
| 2062 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2066 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | const char *encoding, *errors = NULL; |
| 2069 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 2070 | &encoding, &errors)) |
| 2071 | return NULL; |
| 2072 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2075 | static PyObject * |
| 2076 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2077 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | const char *encoding, *errors = NULL; |
| 2079 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 2080 | &encoding, &errors)) |
| 2081 | return NULL; |
| 2082 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 2085 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 2086 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2087 | static PyObject * |
| 2088 | test_long_numbits(PyObject *self) |
| 2089 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | struct triple { |
| 2091 | long input; |
| 2092 | size_t nbits; |
| 2093 | int sign; |
| 2094 | } testcases[] = {{0, 0, 0}, |
| 2095 | {1L, 1, 1}, |
| 2096 | {-1L, 1, -1}, |
| 2097 | {2L, 2, 1}, |
| 2098 | {-2L, 2, -1}, |
| 2099 | {3L, 2, 1}, |
| 2100 | {-3L, 2, -1}, |
| 2101 | {4L, 3, 1}, |
| 2102 | {-4L, 3, -1}, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2103 | {0x7fffL, 15, 1}, /* one Python int digit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | {-0x7fffL, 15, -1}, |
| 2105 | {0xffffL, 16, 1}, |
| 2106 | {-0xffffL, 16, -1}, |
| 2107 | {0xfffffffL, 28, 1}, |
| 2108 | {-0xfffffffL, 28, -1}}; |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 2109 | size_t i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2110 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2111 | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2112 | size_t nbits; |
| 2113 | int sign; |
| 2114 | PyObject *plong; |
| 2115 | |
| 2116 | plong = PyLong_FromLong(testcases[i].input); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2117 | if (plong == NULL) |
| 2118 | return NULL; |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2119 | nbits = _PyLong_NumBits(plong); |
| 2120 | sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2122 | Py_DECREF(plong); |
| 2123 | if (nbits != testcases[i].nbits) |
| 2124 | return raiseTestError("test_long_numbits", |
| 2125 | "wrong result for _PyLong_NumBits"); |
| 2126 | if (sign != testcases[i].sign) |
| 2127 | return raiseTestError("test_long_numbits", |
| 2128 | "wrong result for _PyLong_Sign"); |
| 2129 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2130 | Py_RETURN_NONE; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 2133 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2134 | |
| 2135 | static PyObject * |
| 2136 | test_null_strings(PyObject *self) |
| 2137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2138 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 2139 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 2140 | Py_XDECREF(o1); |
| 2141 | Py_XDECREF(o2); |
| 2142 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2145 | static PyObject * |
| 2146 | raise_exception(PyObject *self, PyObject *args) |
| 2147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2148 | PyObject *exc; |
| 2149 | PyObject *exc_args, *v; |
| 2150 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 2153 | &exc, &num_args)) |
| 2154 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | exc_args = PyTuple_New(num_args); |
| 2157 | if (exc_args == NULL) |
| 2158 | return NULL; |
| 2159 | for (i = 0; i < num_args; ++i) { |
| 2160 | v = PyLong_FromLong(i); |
| 2161 | if (v == NULL) { |
| 2162 | Py_DECREF(exc_args); |
| 2163 | return NULL; |
| 2164 | } |
| 2165 | PyTuple_SET_ITEM(exc_args, i, v); |
| 2166 | } |
| 2167 | PyErr_SetObject(exc, exc_args); |
| 2168 | Py_DECREF(exc_args); |
| 2169 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2170 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2171 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2172 | static PyObject * |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 2173 | set_errno(PyObject *self, PyObject *args) |
| 2174 | { |
| 2175 | int new_errno; |
| 2176 | |
| 2177 | if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
| 2178 | return NULL; |
| 2179 | |
| 2180 | errno = new_errno; |
| 2181 | Py_RETURN_NONE; |
| 2182 | } |
| 2183 | |
| 2184 | static PyObject * |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2185 | test_set_exc_info(PyObject *self, PyObject *args) |
| 2186 | { |
| 2187 | PyObject *orig_exc; |
| 2188 | PyObject *new_type, *new_value, *new_tb; |
| 2189 | PyObject *type, *value, *tb; |
| 2190 | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
| 2191 | &new_type, &new_value, &new_tb)) |
| 2192 | return NULL; |
| 2193 | |
| 2194 | PyErr_GetExcInfo(&type, &value, &tb); |
| 2195 | |
| 2196 | Py_INCREF(new_type); |
| 2197 | Py_INCREF(new_value); |
| 2198 | Py_INCREF(new_tb); |
| 2199 | PyErr_SetExcInfo(new_type, new_value, new_tb); |
| 2200 | |
| 2201 | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
| 2202 | Py_XDECREF(type); |
| 2203 | Py_XDECREF(value); |
| 2204 | Py_XDECREF(tb); |
| 2205 | return orig_exc; |
| 2206 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2207 | |
| 2208 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2209 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2210 | static PyObject * |
| 2211 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | if (PyDateTimeAPI) { |
| 2213 | if (test_run_counter) { |
| 2214 | /* Probably regrtest.py -R */ |
| 2215 | Py_RETURN_NONE; |
| 2216 | } |
| 2217 | else { |
| 2218 | PyErr_SetString(PyExc_AssertionError, |
| 2219 | "PyDateTime_CAPI somehow initialized"); |
| 2220 | return NULL; |
| 2221 | } |
| 2222 | } |
| 2223 | test_run_counter++; |
| 2224 | PyDateTime_IMPORT; |
| 2225 | if (PyDateTimeAPI) |
| 2226 | Py_RETURN_NONE; |
| 2227 | else |
| 2228 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2231 | |
| 2232 | #ifdef WITH_THREAD |
| 2233 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2234 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 2235 | * `thread_done` when it's finished. The driver code has to know when the |
| 2236 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 2237 | * may go away when the driver finishes. The former lack of this explicit |
| 2238 | * synchronization caused rare segfaults, so rare that they were seen only |
| 2239 | * on a Mac buildbot (although they were possible on any box). |
| 2240 | */ |
| 2241 | static PyThread_type_lock thread_done = NULL; |
| 2242 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2243 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2244 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | PyObject *rc; |
| 2247 | int success; |
| 2248 | PyGILState_STATE s = PyGILState_Ensure(); |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2249 | rc = _PyObject_CallNoArg((PyObject *)callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | success = (rc != NULL); |
| 2251 | Py_XDECREF(rc); |
| 2252 | PyGILState_Release(s); |
| 2253 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2256 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 2257 | * should be called only from threads spawned by test_thread_state(). |
| 2258 | */ |
| 2259 | static void |
| 2260 | _make_call_from_thread(void *callable) |
| 2261 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | _make_call(callable); |
| 2263 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2266 | static PyObject * |
| 2267 | test_thread_state(PyObject *self, PyObject *args) |
| 2268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | PyObject *fn; |
| 2270 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 2273 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | if (!PyCallable_Check(fn)) { |
| 2276 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 2277 | fn->ob_type->tp_name); |
| 2278 | return NULL; |
| 2279 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | /* Ensure Python is set up for threading */ |
| 2282 | PyEval_InitThreads(); |
| 2283 | thread_done = PyThread_allocate_lock(); |
| 2284 | if (thread_done == NULL) |
| 2285 | return PyErr_NoMemory(); |
| 2286 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | /* Start a new thread with our callback. */ |
| 2289 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2290 | /* Make the callback with the thread lock held by this thread */ |
| 2291 | success &= _make_call(fn); |
| 2292 | /* Do it all again, but this time with the thread-lock released */ |
| 2293 | Py_BEGIN_ALLOW_THREADS |
| 2294 | success &= _make_call(fn); |
| 2295 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2296 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | /* And once more with and without a thread |
| 2299 | XXX - should use a lock and work out exactly what we are trying |
| 2300 | to test <wink> |
| 2301 | */ |
| 2302 | Py_BEGIN_ALLOW_THREADS |
| 2303 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2304 | success &= _make_call(fn); |
| 2305 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2306 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 2309 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | PyThread_free_lock(thread_done); |
| 2312 | if (!success) |
| 2313 | return NULL; |
| 2314 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2315 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2316 | |
| 2317 | /* test Py_AddPendingCalls using threads */ |
| 2318 | static int _pending_callback(void *arg) |
| 2319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | /* we assume the argument is callable object to which we own a reference */ |
| 2321 | PyObject *callable = (PyObject *)arg; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 2322 | PyObject *r = _PyObject_CallNoArg(callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2323 | Py_DECREF(callable); |
| 2324 | Py_XDECREF(r); |
| 2325 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | /* The following requests n callbacks to _pending_callback. It can be |
| 2329 | * run from any python thread. |
| 2330 | */ |
| 2331 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 2332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | PyObject *callable; |
| 2334 | int r; |
| 2335 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 2336 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | /* create the reference for the callbackwhile we hold the lock */ |
| 2339 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | Py_BEGIN_ALLOW_THREADS |
| 2342 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 2343 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2345 | if (r<0) { |
| 2346 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2347 | Py_RETURN_FALSE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2349 | Py_RETURN_TRUE; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2350 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2351 | #endif |
| 2352 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 2353 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2354 | static PyObject * |
| 2355 | test_string_from_format(PyObject *self, PyObject *args) |
| 2356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | PyObject *result; |
| 2358 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2359 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2360 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 2361 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 2362 | if (result == NULL) \ |
| 2363 | return NULL; \ |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 2364 | if (!_PyUnicode_EqualToASCIIString(result, "1")) { \ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2365 | msg = FORMAT " failed at 1"; \ |
| 2366 | goto Fail; \ |
| 2367 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | CHECK_1_FORMAT("%d", int); |
| 2371 | CHECK_1_FORMAT("%ld", long); |
| 2372 | /* The z width modifier was added in Python 2.5. */ |
| 2373 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | /* The u type code was added in Python 2.5. */ |
| 2376 | CHECK_1_FORMAT("%u", unsigned int); |
| 2377 | CHECK_1_FORMAT("%lu", unsigned long); |
| 2378 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2380 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 2381 | CHECK_1_FORMAT("%llu", unsigned long long); |
| 2382 | CHECK_1_FORMAT("%lld", long long); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2384 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2385 | |
| 2386 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | Py_XDECREF(result); |
| 2388 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2389 | |
| 2390 | #undef CHECK_1_FORMAT |
| 2391 | } |
| 2392 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2393 | |
| 2394 | static PyObject * |
| 2395 | test_unicode_compare_with_ascii(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 2397 | int result; |
| 2398 | if (py_s == NULL) |
| 2399 | return NULL; |
| 2400 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 2401 | Py_DECREF(py_s); |
| 2402 | if (!result) { |
| 2403 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 2404 | "should not compare equal to c string."); |
| 2405 | return NULL; |
| 2406 | } |
| 2407 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2408 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2409 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2410 | /* This is here to provide a docstring for test_descr. */ |
| 2411 | static PyObject * |
| 2412 | test_with_docstring(PyObject *self) |
| 2413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2414 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2417 | /* Test PyOS_string_to_double. */ |
| 2418 | static PyObject * |
| 2419 | test_string_to_double(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | double result; |
| 2421 | char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2423 | #define CHECK_STRING(STR, expected) \ |
| 2424 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2425 | if (result == -1.0 && PyErr_Occurred()) \ |
| 2426 | return NULL; \ |
Benjamin Peterson | 8f4b247 | 2016-09-07 18:09:22 -0700 | [diff] [blame] | 2427 | if (result != (double)expected) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2428 | msg = "conversion of " STR " to float failed"; \ |
| 2429 | goto fail; \ |
| 2430 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2432 | #define CHECK_INVALID(STR) \ |
| 2433 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2434 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 2435 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 2436 | PyErr_Clear(); \ |
| 2437 | else \ |
| 2438 | return NULL; \ |
| 2439 | } \ |
| 2440 | else { \ |
| 2441 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 2442 | goto fail; \ |
| 2443 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2445 | CHECK_STRING("0.1", 0.1); |
| 2446 | CHECK_STRING("1.234", 1.234); |
| 2447 | CHECK_STRING("-1.35", -1.35); |
| 2448 | CHECK_STRING(".1e01", 1.0); |
| 2449 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2451 | CHECK_INVALID(" 0.1"); |
| 2452 | CHECK_INVALID("\t\n-3"); |
| 2453 | CHECK_INVALID(".123 "); |
| 2454 | CHECK_INVALID("3\n"); |
| 2455 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2458 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2460 | #undef CHECK_STRING |
| 2461 | #undef CHECK_INVALID |
| 2462 | } |
| 2463 | |
| 2464 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2465 | /* Coverage testing of capsule objects. */ |
| 2466 | |
| 2467 | static const char *capsule_name = "capsule name"; |
| 2468 | static char *capsule_pointer = "capsule pointer"; |
| 2469 | static char *capsule_context = "capsule context"; |
| 2470 | static const char *capsule_error = NULL; |
| 2471 | static int |
| 2472 | capsule_destructor_call_count = 0; |
| 2473 | |
| 2474 | static void |
| 2475 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | capsule_destructor_call_count++; |
| 2477 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 2478 | capsule_error = "context did not match in destructor!"; |
| 2479 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2480 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2481 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 2482 | capsule_error = "name did not match in destructor!"; |
| 2483 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2484 | capsule_error = "pointer did not match in destructor!"; |
| 2485 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2489 | char *name; |
| 2490 | char *module; |
| 2491 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2492 | } known_capsule; |
| 2493 | |
| 2494 | static PyObject * |
| 2495 | test_capsule(PyObject *self, PyObject *args) |
| 2496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2497 | PyObject *object; |
| 2498 | const char *error = NULL; |
| 2499 | void *pointer; |
| 2500 | void *pointer2; |
| 2501 | known_capsule known_capsules[] = { |
| 2502 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2503 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 2504 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 2505 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2506 | { NULL, NULL }, |
| 2507 | }; |
| 2508 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2509 | |
| 2510 | #define FAIL(x) { error = (x); goto exit; } |
| 2511 | |
| 2512 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2513 | if (capsule_error) { \ |
| 2514 | FAIL(capsule_error); \ |
| 2515 | } \ |
| 2516 | else if (!capsule_destructor_call_count) { \ |
| 2517 | FAIL("destructor not called!"); \ |
| 2518 | } \ |
| 2519 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2522 | PyCapsule_SetContext(object, capsule_context); |
| 2523 | capsule_destructor(object); |
| 2524 | CHECK_DESTRUCTOR; |
| 2525 | Py_DECREF(object); |
| 2526 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | object = PyCapsule_New(known, "ignored", NULL); |
| 2529 | PyCapsule_SetPointer(object, capsule_pointer); |
| 2530 | PyCapsule_SetName(object, capsule_name); |
| 2531 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 2532 | PyCapsule_SetContext(object, capsule_context); |
| 2533 | capsule_destructor(object); |
| 2534 | CHECK_DESTRUCTOR; |
| 2535 | /* intentionally access using the wrong name */ |
| 2536 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2537 | if (!PyErr_Occurred()) { |
| 2538 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2539 | } |
| 2540 | PyErr_Clear(); |
| 2541 | if (pointer2) { |
| 2542 | if (pointer2 == capsule_pointer) { |
| 2543 | FAIL("PyCapsule_GetPointer should not have" |
| 2544 | " returned the internal pointer!"); |
| 2545 | } else { |
| 2546 | FAIL("PyCapsule_GetPointer should have " |
| 2547 | "returned NULL pointer but did not!"); |
| 2548 | } |
| 2549 | } |
| 2550 | PyCapsule_SetDestructor(object, NULL); |
| 2551 | Py_DECREF(object); |
| 2552 | if (capsule_destructor_call_count) { |
| 2553 | FAIL("destructor called when it should not have been!"); |
| 2554 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2556 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 2557 | /* yeah, ordinarily I wouldn't do this either, |
| 2558 | but it's fine for this test harness. |
| 2559 | */ |
| 2560 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2561 | #undef FAIL |
| 2562 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | { \ |
| 2564 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 2565 | x, known->module, known->attribute); \ |
| 2566 | error = buffer; \ |
| 2567 | goto exit; \ |
| 2568 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | PyObject *module = PyImport_ImportModule(known->module); |
| 2571 | if (module) { |
| 2572 | pointer = PyCapsule_Import(known->name, 0); |
| 2573 | if (!pointer) { |
| 2574 | Py_DECREF(module); |
| 2575 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 2576 | } |
| 2577 | object = PyObject_GetAttrString(module, known->attribute); |
| 2578 | if (!object) { |
| 2579 | Py_DECREF(module); |
| 2580 | return NULL; |
| 2581 | } |
| 2582 | pointer2 = PyCapsule_GetPointer(object, |
| 2583 | "weebles wobble but they don't fall down"); |
| 2584 | if (!PyErr_Occurred()) { |
| 2585 | Py_DECREF(object); |
| 2586 | Py_DECREF(module); |
| 2587 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2588 | } |
| 2589 | PyErr_Clear(); |
| 2590 | if (pointer2) { |
| 2591 | Py_DECREF(module); |
| 2592 | Py_DECREF(object); |
| 2593 | if (pointer2 == pointer) { |
| 2594 | FAIL("PyCapsule_GetPointer should not have" |
| 2595 | " returned its internal pointer!"); |
| 2596 | } else { |
| 2597 | FAIL("PyCapsule_GetPointer should have" |
| 2598 | " returned NULL pointer but did not!"); |
| 2599 | } |
| 2600 | } |
| 2601 | Py_DECREF(object); |
| 2602 | Py_DECREF(module); |
| 2603 | } |
| 2604 | else |
| 2605 | PyErr_Clear(); |
| 2606 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2607 | |
| 2608 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2609 | if (error) { |
| 2610 | return raiseTestError("test_capsule", error); |
| 2611 | } |
| 2612 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2613 | #undef FAIL |
| 2614 | } |
| 2615 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2616 | #ifdef HAVE_GETTIMEOFDAY |
| 2617 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 2618 | static void print_delta(int test, struct timeval *s, struct timeval *e) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2619 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | e->tv_sec -= s->tv_sec; |
| 2621 | e->tv_usec -= s->tv_usec; |
| 2622 | if (e->tv_usec < 0) { |
| 2623 | e->tv_sec -=1; |
| 2624 | e->tv_usec += 1000000; |
| 2625 | } |
| 2626 | printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | static PyObject * |
| 2630 | profile_int(PyObject *self, PyObject* args) |
| 2631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | int i, k; |
| 2633 | struct timeval start, stop; |
| 2634 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | /* Test 1: Allocate and immediately deallocate |
| 2637 | many small integers */ |
| 2638 | gettimeofday(&start, NULL); |
| 2639 | for(k=0; k < 20000; k++) |
| 2640 | for(i=0; i < 1000; i++) { |
| 2641 | single = PyLong_FromLong(i); |
| 2642 | Py_DECREF(single); |
| 2643 | } |
| 2644 | gettimeofday(&stop, NULL); |
| 2645 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | /* Test 2: Allocate and immediately deallocate |
| 2648 | many large integers */ |
| 2649 | gettimeofday(&start, NULL); |
| 2650 | for(k=0; k < 20000; k++) |
| 2651 | for(i=0; i < 1000; i++) { |
| 2652 | single = PyLong_FromLong(i+1000000); |
| 2653 | Py_DECREF(single); |
| 2654 | } |
| 2655 | gettimeofday(&stop, NULL); |
| 2656 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2658 | /* Test 3: Allocate a few integers, then release |
| 2659 | them all simultaneously. */ |
| 2660 | multiple = malloc(sizeof(PyObject*) * 1000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2661 | if (multiple == NULL) |
| 2662 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2663 | gettimeofday(&start, NULL); |
| 2664 | for(k=0; k < 20000; k++) { |
| 2665 | for(i=0; i < 1000; i++) { |
| 2666 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2667 | } |
| 2668 | for(i=0; i < 1000; i++) { |
| 2669 | Py_DECREF(multiple[i]); |
| 2670 | } |
| 2671 | } |
| 2672 | gettimeofday(&stop, NULL); |
| 2673 | print_delta(3, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2674 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | /* Test 4: Allocate many integers, then release |
| 2677 | them all simultaneously. */ |
| 2678 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2679 | if (multiple == NULL) |
| 2680 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | gettimeofday(&start, NULL); |
| 2682 | for(k=0; k < 20; k++) { |
| 2683 | for(i=0; i < 1000000; i++) { |
| 2684 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2685 | } |
| 2686 | for(i=0; i < 1000000; i++) { |
| 2687 | Py_DECREF(multiple[i]); |
| 2688 | } |
| 2689 | } |
| 2690 | gettimeofday(&stop, NULL); |
| 2691 | print_delta(4, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2692 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | /* Test 5: Allocate many integers < 32000 */ |
| 2695 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2696 | if (multiple == NULL) |
| 2697 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | gettimeofday(&start, NULL); |
| 2699 | for(k=0; k < 10; k++) { |
| 2700 | for(i=0; i < 1000000; i++) { |
| 2701 | multiple[i] = PyLong_FromLong(i+1000); |
| 2702 | } |
| 2703 | for(i=0; i < 1000000; i++) { |
| 2704 | Py_DECREF(multiple[i]); |
| 2705 | } |
| 2706 | } |
| 2707 | gettimeofday(&stop, NULL); |
| 2708 | print_delta(5, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2709 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | /* Test 6: Perform small int addition */ |
| 2712 | op1 = PyLong_FromLong(1); |
| 2713 | gettimeofday(&start, NULL); |
| 2714 | for(i=0; i < 10000000; i++) { |
| 2715 | result = PyNumber_Add(op1, op1); |
| 2716 | Py_DECREF(result); |
| 2717 | } |
| 2718 | gettimeofday(&stop, NULL); |
| 2719 | Py_DECREF(op1); |
| 2720 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2722 | /* Test 7: Perform medium int addition */ |
| 2723 | op1 = PyLong_FromLong(1000); |
Christian Heimes | 66eda26 | 2013-07-26 15:54:07 +0200 | [diff] [blame] | 2724 | if (op1 == NULL) |
| 2725 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | gettimeofday(&start, NULL); |
| 2727 | for(i=0; i < 10000000; i++) { |
| 2728 | result = PyNumber_Add(op1, op1); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2729 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | } |
| 2731 | gettimeofday(&stop, NULL); |
| 2732 | Py_DECREF(op1); |
| 2733 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2734 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2735 | Py_RETURN_NONE; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2736 | } |
| 2737 | #endif |
| 2738 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2739 | /* To test the format of tracebacks as printed out. */ |
| 2740 | static PyObject * |
| 2741 | traceback_print(PyObject *self, PyObject *args) |
| 2742 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | PyObject *file; |
| 2744 | PyObject *traceback; |
| 2745 | int result; |
| 2746 | |
| 2747 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 2748 | &traceback, &file)) |
| 2749 | return NULL; |
| 2750 | |
| 2751 | result = PyTraceBack_Print(traceback, file); |
| 2752 | if (result < 0) |
| 2753 | return NULL; |
| 2754 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2757 | /* To test the format of exceptions as printed out. */ |
| 2758 | static PyObject * |
| 2759 | exception_print(PyObject *self, PyObject *args) |
| 2760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | PyObject *value; |
| 2762 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 2765 | &value)) |
| 2766 | return NULL; |
| 2767 | if (!PyExceptionInstance_Check(value)) { |
| 2768 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 2769 | return NULL; |
| 2770 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | tb = PyException_GetTraceback(value); |
| 2773 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 2774 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | |
| 2780 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2781 | |
| 2782 | /* reliably raise a MemoryError */ |
| 2783 | static PyObject * |
| 2784 | raise_memoryerror(PyObject *self) |
| 2785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | PyErr_NoMemory(); |
| 2787 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2790 | /* Issue 6012 */ |
| 2791 | static PyObject *str1, *str2; |
| 2792 | static int |
| 2793 | failing_converter(PyObject *obj, void *arg) |
| 2794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | /* Clone str1, then let the conversion fail. */ |
| 2796 | assert(str1); |
| 2797 | str2 = str1; |
| 2798 | Py_INCREF(str2); |
| 2799 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2800 | } |
| 2801 | static PyObject* |
| 2802 | argparsing(PyObject *o, PyObject *args) |
| 2803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | PyObject *res; |
| 2805 | str1 = str2 = NULL; |
| 2806 | if (!PyArg_ParseTuple(args, "O&O&", |
| 2807 | PyUnicode_FSConverter, &str1, |
| 2808 | failing_converter, &str2)) { |
| 2809 | if (!str2) |
| 2810 | /* argument converter not called? */ |
| 2811 | return NULL; |
| 2812 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2813 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | Py_DECREF(str2); |
| 2815 | PyErr_Clear(); |
| 2816 | return res; |
| 2817 | } |
| 2818 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2819 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2820 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2821 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 2822 | static PyObject * |
| 2823 | code_newempty(PyObject *self, PyObject *args) |
| 2824 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | const char *filename; |
| 2826 | const char *funcname; |
| 2827 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2829 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 2830 | &filename, &funcname, &firstlineno)) |
| 2831 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2836 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 2837 | Run via Lib/test/test_exceptions.py */ |
| 2838 | static PyObject * |
| 2839 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | const char *name; |
| 2842 | const char *doc = NULL; |
| 2843 | PyObject *base = NULL; |
| 2844 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2849 | "s|sOO:make_exception_with_doc", kwlist, |
| 2850 | &name, &doc, &base, &dict)) |
| 2851 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2856 | static PyObject * |
| 2857 | make_memoryview_from_NULL_pointer(PyObject *self) |
| 2858 | { |
| 2859 | Py_buffer info; |
| 2860 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 2861 | return NULL; |
| 2862 | return PyMemoryView_FromBuffer(&info); |
| 2863 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 2864 | |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 2865 | static PyObject * |
| 2866 | test_from_contiguous(PyObject* self, PyObject *noargs) |
| 2867 | { |
| 2868 | int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 2869 | int init[5] = {0, 1, 2, 3, 4}; |
| 2870 | Py_ssize_t itemsize = sizeof(int); |
| 2871 | Py_ssize_t shape = 5; |
| 2872 | Py_ssize_t strides = 2 * itemsize; |
| 2873 | Py_buffer view = { |
| 2874 | data, |
| 2875 | NULL, |
| 2876 | 5 * itemsize, |
| 2877 | itemsize, |
| 2878 | 1, |
| 2879 | 1, |
| 2880 | NULL, |
| 2881 | &shape, |
| 2882 | &strides, |
| 2883 | NULL, |
| 2884 | NULL |
| 2885 | }; |
| 2886 | int *ptr; |
| 2887 | int i; |
| 2888 | |
| 2889 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 2890 | ptr = view.buf; |
| 2891 | for (i = 0; i < 5; i++) { |
| 2892 | if (ptr[2*i] != i) { |
| 2893 | PyErr_SetString(TestError, |
| 2894 | "test_from_contiguous: incorrect result"); |
| 2895 | return NULL; |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | view.buf = &data[8]; |
| 2900 | view.strides[0] = -2 * itemsize; |
| 2901 | |
| 2902 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 2903 | ptr = view.buf; |
| 2904 | for (i = 0; i < 5; i++) { |
| 2905 | if (*(ptr-2*i) != i) { |
| 2906 | PyErr_SetString(TestError, |
| 2907 | "test_from_contiguous: incorrect result"); |
| 2908 | return NULL; |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | Py_RETURN_NONE; |
| 2913 | } |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2914 | |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 2915 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2916 | extern PyTypeObject _PyBytesIOBuffer_Type; |
| 2917 | |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2918 | static PyObject * |
| 2919 | test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs) |
| 2920 | { |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2921 | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2922 | PyObject *b; |
| 2923 | char *dummy[1]; |
| 2924 | int ret, match; |
| 2925 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2926 | /* PyBuffer_FillInfo() */ |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2927 | ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE); |
| 2928 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 2929 | PyErr_Clear(); |
| 2930 | if (ret != -1 || match == 0) |
| 2931 | goto error; |
| 2932 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2933 | /* bytesiobuf_getbuffer() */ |
| 2934 | b = type->tp_alloc(type, 0); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2935 | if (b == NULL) { |
| 2936 | return NULL; |
| 2937 | } |
| 2938 | |
| 2939 | ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE); |
| 2940 | Py_DECREF(b); |
| 2941 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 2942 | PyErr_Clear(); |
| 2943 | if (ret != -1 || match == 0) |
| 2944 | goto error; |
| 2945 | |
| 2946 | Py_RETURN_NONE; |
| 2947 | |
| 2948 | error: |
| 2949 | PyErr_SetString(TestError, |
| 2950 | "test_pep3118_obsolete_write_locks: failure"); |
| 2951 | return NULL; |
| 2952 | } |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 2953 | #endif |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2954 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2955 | /* This tests functions that historically supported write locks. It is |
| 2956 | wrong to call getbuffer() with view==NULL and a compliant getbufferproc |
| 2957 | is entitled to segfault in that case. */ |
| 2958 | static PyObject * |
| 2959 | getbuffer_with_null_view(PyObject* self, PyObject *obj) |
| 2960 | { |
| 2961 | if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0) |
| 2962 | return NULL; |
| 2963 | |
| 2964 | Py_RETURN_NONE; |
| 2965 | } |
| 2966 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2967 | /* Test that the fatal error from not having a current thread doesn't |
| 2968 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 2969 | static PyObject * |
| 2970 | crash_no_current_thread(PyObject *self) |
| 2971 | { |
| 2972 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 2973 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 2974 | !pydebug mode. However, the test only actually tests anything |
| 2975 | in pydebug mode, since that's where the infinite loop was in |
| 2976 | the first place. */ |
| 2977 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2978 | Py_END_ALLOW_THREADS |
| 2979 | return NULL; |
| 2980 | } |
| 2981 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2982 | /* To run some code in a sub-interpreter. */ |
| 2983 | static PyObject * |
| 2984 | run_in_subinterp(PyObject *self, PyObject *args) |
| 2985 | { |
| 2986 | const char *code; |
| 2987 | int r; |
| 2988 | PyThreadState *substate, *mainstate; |
| 2989 | |
| 2990 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 2991 | &code)) |
| 2992 | return NULL; |
| 2993 | |
| 2994 | mainstate = PyThreadState_Get(); |
| 2995 | |
| 2996 | PyThreadState_Swap(NULL); |
| 2997 | |
| 2998 | substate = Py_NewInterpreter(); |
Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 2999 | if (substate == NULL) { |
| 3000 | /* Since no new thread state was created, there is no exception to |
| 3001 | propagate; raise a fresh one after swapping in the old thread |
| 3002 | state. */ |
| 3003 | PyThreadState_Swap(mainstate); |
| 3004 | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
| 3005 | return NULL; |
| 3006 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3007 | r = PyRun_SimpleString(code); |
| 3008 | Py_EndInterpreter(substate); |
| 3009 | |
| 3010 | PyThreadState_Swap(mainstate); |
| 3011 | |
| 3012 | return PyLong_FromLong(r); |
| 3013 | } |
| 3014 | |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3015 | static int |
| 3016 | check_time_rounding(int round) |
| 3017 | { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 3018 | if (round != _PyTime_ROUND_FLOOR |
| 3019 | && round != _PyTime_ROUND_CEILING |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 3020 | && round != _PyTime_ROUND_HALF_EVEN) { |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3021 | PyErr_SetString(PyExc_ValueError, "invalid rounding"); |
| 3022 | return -1; |
| 3023 | } |
| 3024 | return 0; |
| 3025 | } |
| 3026 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3027 | static PyObject * |
| 3028 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
| 3029 | { |
| 3030 | PyObject *obj; |
| 3031 | time_t sec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3032 | int round; |
| 3033 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3034 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3035 | if (check_time_rounding(round) < 0) |
| 3036 | return NULL; |
| 3037 | if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3038 | return NULL; |
| 3039 | return _PyLong_FromTime_t(sec); |
| 3040 | } |
| 3041 | |
| 3042 | static PyObject * |
| 3043 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
| 3044 | { |
| 3045 | PyObject *obj; |
| 3046 | time_t sec; |
| 3047 | long usec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3048 | int round; |
| 3049 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3050 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3051 | if (check_time_rounding(round) < 0) |
| 3052 | return NULL; |
| 3053 | if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3054 | return NULL; |
| 3055 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
| 3056 | } |
| 3057 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3058 | static PyObject * |
| 3059 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
| 3060 | { |
| 3061 | PyObject *obj; |
| 3062 | time_t sec; |
| 3063 | long nsec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3064 | int round; |
| 3065 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3066 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3067 | if (check_time_rounding(round) < 0) |
| 3068 | return NULL; |
| 3069 | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3070 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3071 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3072 | } |
| 3073 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3074 | static void |
| 3075 | slot_tp_del(PyObject *self) |
| 3076 | { |
| 3077 | _Py_IDENTIFIER(__tp_del__); |
| 3078 | PyObject *del, *res; |
| 3079 | PyObject *error_type, *error_value, *error_traceback; |
| 3080 | |
| 3081 | /* Temporarily resurrect the object. */ |
| 3082 | assert(self->ob_refcnt == 0); |
| 3083 | self->ob_refcnt = 1; |
| 3084 | |
| 3085 | /* Save the current exception, if any. */ |
| 3086 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 3087 | |
| 3088 | /* Execute __del__ method, if any. */ |
| 3089 | del = _PyObject_LookupSpecial(self, &PyId___tp_del__); |
| 3090 | if (del != NULL) { |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 3091 | res = _PyObject_CallNoArg(del); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3092 | if (res == NULL) |
| 3093 | PyErr_WriteUnraisable(del); |
| 3094 | else |
| 3095 | Py_DECREF(res); |
| 3096 | Py_DECREF(del); |
| 3097 | } |
| 3098 | |
| 3099 | /* Restore the saved exception. */ |
| 3100 | PyErr_Restore(error_type, error_value, error_traceback); |
| 3101 | |
| 3102 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 3103 | * cause a recursive call. |
| 3104 | */ |
| 3105 | assert(self->ob_refcnt > 0); |
| 3106 | if (--self->ob_refcnt == 0) |
| 3107 | return; /* this is the normal path out */ |
| 3108 | |
| 3109 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 3110 | * never happened. |
| 3111 | */ |
| 3112 | { |
| 3113 | Py_ssize_t refcnt = self->ob_refcnt; |
| 3114 | _Py_NewReference(self); |
| 3115 | self->ob_refcnt = refcnt; |
| 3116 | } |
| 3117 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 3118 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 3119 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 3120 | * we need to undo that. */ |
| 3121 | _Py_DEC_REFTOTAL; |
| 3122 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 3123 | * chain, so no more to do there. |
| 3124 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 3125 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 3126 | * undone. |
| 3127 | */ |
| 3128 | #ifdef COUNT_ALLOCS |
| 3129 | --Py_TYPE(self)->tp_frees; |
| 3130 | --Py_TYPE(self)->tp_allocs; |
| 3131 | #endif |
| 3132 | } |
| 3133 | |
| 3134 | static PyObject * |
| 3135 | with_tp_del(PyObject *self, PyObject *args) |
| 3136 | { |
| 3137 | PyObject *obj; |
| 3138 | PyTypeObject *tp; |
| 3139 | |
| 3140 | if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) |
| 3141 | return NULL; |
| 3142 | tp = (PyTypeObject *) obj; |
| 3143 | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
| 3144 | PyErr_Format(PyExc_TypeError, |
| 3145 | "heap type expected, got %R", obj); |
| 3146 | return NULL; |
| 3147 | } |
| 3148 | tp->tp_del = slot_tp_del; |
| 3149 | Py_INCREF(obj); |
| 3150 | return obj; |
| 3151 | } |
| 3152 | |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 3153 | static PyMethodDef ml; |
| 3154 | |
| 3155 | static PyObject * |
| 3156 | create_cfunction(PyObject *self, PyObject *args) |
| 3157 | { |
| 3158 | return PyCFunction_NewEx(&ml, self, NULL); |
| 3159 | } |
| 3160 | |
| 3161 | static PyMethodDef ml = { |
| 3162 | "create_cfunction", |
| 3163 | create_cfunction, |
| 3164 | METH_NOARGS, |
| 3165 | NULL |
| 3166 | }; |
| 3167 | |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3168 | static PyObject * |
| 3169 | _test_incref(PyObject *ob) |
| 3170 | { |
| 3171 | Py_INCREF(ob); |
| 3172 | return ob; |
| 3173 | } |
| 3174 | |
| 3175 | static PyObject * |
| 3176 | test_xincref_doesnt_leak(PyObject *ob) |
| 3177 | { |
| 3178 | PyObject *obj = PyLong_FromLong(0); |
| 3179 | Py_XINCREF(_test_incref(obj)); |
| 3180 | Py_DECREF(obj); |
| 3181 | Py_DECREF(obj); |
| 3182 | Py_DECREF(obj); |
| 3183 | Py_RETURN_NONE; |
| 3184 | } |
| 3185 | |
| 3186 | static PyObject * |
| 3187 | test_incref_doesnt_leak(PyObject *ob) |
| 3188 | { |
| 3189 | PyObject *obj = PyLong_FromLong(0); |
| 3190 | Py_INCREF(_test_incref(obj)); |
| 3191 | Py_DECREF(obj); |
| 3192 | Py_DECREF(obj); |
| 3193 | Py_DECREF(obj); |
| 3194 | Py_RETURN_NONE; |
| 3195 | } |
| 3196 | |
| 3197 | static PyObject * |
| 3198 | test_xdecref_doesnt_leak(PyObject *ob) |
| 3199 | { |
| 3200 | Py_XDECREF(PyLong_FromLong(0)); |
| 3201 | Py_RETURN_NONE; |
| 3202 | } |
| 3203 | |
| 3204 | static PyObject * |
| 3205 | test_decref_doesnt_leak(PyObject *ob) |
| 3206 | { |
| 3207 | Py_DECREF(PyLong_FromLong(0)); |
| 3208 | Py_RETURN_NONE; |
| 3209 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3210 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3211 | static PyObject * |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3212 | test_incref_decref_API(PyObject *ob) |
| 3213 | { |
| 3214 | PyObject *obj = PyLong_FromLong(0); |
Victor Stinner | fc6a90a | 2014-10-09 22:15:41 +0200 | [diff] [blame] | 3215 | Py_IncRef(obj); |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3216 | Py_DecRef(obj); |
| 3217 | Py_DecRef(obj); |
| 3218 | Py_RETURN_NONE; |
| 3219 | } |
| 3220 | |
| 3221 | static PyObject * |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3222 | test_pymem_alloc0(PyObject *self) |
| 3223 | { |
| 3224 | void *ptr; |
| 3225 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3226 | ptr = PyMem_RawMalloc(0); |
| 3227 | if (ptr == NULL) { |
| 3228 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); |
| 3229 | return NULL; |
| 3230 | } |
| 3231 | PyMem_RawFree(ptr); |
| 3232 | |
| 3233 | ptr = PyMem_RawCalloc(0, 0); |
| 3234 | if (ptr == NULL) { |
| 3235 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); |
| 3236 | return NULL; |
| 3237 | } |
| 3238 | PyMem_RawFree(ptr); |
| 3239 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3240 | ptr = PyMem_Malloc(0); |
| 3241 | if (ptr == NULL) { |
| 3242 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); |
| 3243 | return NULL; |
| 3244 | } |
| 3245 | PyMem_Free(ptr); |
| 3246 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3247 | ptr = PyMem_Calloc(0, 0); |
| 3248 | if (ptr == NULL) { |
| 3249 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); |
| 3250 | return NULL; |
| 3251 | } |
| 3252 | PyMem_Free(ptr); |
| 3253 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3254 | ptr = PyObject_Malloc(0); |
| 3255 | if (ptr == NULL) { |
| 3256 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); |
| 3257 | return NULL; |
| 3258 | } |
| 3259 | PyObject_Free(ptr); |
| 3260 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3261 | ptr = PyObject_Calloc(0, 0); |
| 3262 | if (ptr == NULL) { |
| 3263 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); |
| 3264 | return NULL; |
| 3265 | } |
| 3266 | PyObject_Free(ptr); |
| 3267 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3268 | Py_RETURN_NONE; |
| 3269 | } |
| 3270 | |
| 3271 | typedef struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3272 | PyMemAllocatorEx alloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3273 | |
| 3274 | size_t malloc_size; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3275 | size_t calloc_nelem; |
| 3276 | size_t calloc_elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3277 | void *realloc_ptr; |
| 3278 | size_t realloc_new_size; |
| 3279 | void *free_ptr; |
| 3280 | } alloc_hook_t; |
| 3281 | |
| 3282 | static void* hook_malloc (void* ctx, size_t size) |
| 3283 | { |
| 3284 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 3285 | hook->malloc_size = size; |
| 3286 | return hook->alloc.malloc(hook->alloc.ctx, size); |
| 3287 | } |
| 3288 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3289 | static void* hook_calloc (void* ctx, size_t nelem, size_t elsize) |
| 3290 | { |
| 3291 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 3292 | hook->calloc_nelem = nelem; |
| 3293 | hook->calloc_elsize = elsize; |
| 3294 | return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); |
| 3295 | } |
| 3296 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3297 | static void* hook_realloc (void* ctx, void* ptr, size_t new_size) |
| 3298 | { |
| 3299 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 3300 | hook->realloc_ptr = ptr; |
| 3301 | hook->realloc_new_size = new_size; |
| 3302 | return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); |
| 3303 | } |
| 3304 | |
| 3305 | static void hook_free (void *ctx, void *ptr) |
| 3306 | { |
| 3307 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 3308 | hook->free_ptr = ptr; |
| 3309 | hook->alloc.free(hook->alloc.ctx, ptr); |
| 3310 | } |
| 3311 | |
| 3312 | static PyObject * |
| 3313 | test_setallocators(PyMemAllocatorDomain domain) |
| 3314 | { |
| 3315 | PyObject *res = NULL; |
| 3316 | const char *error_msg; |
| 3317 | alloc_hook_t hook; |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3318 | PyMemAllocatorEx alloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3319 | size_t size, size2, nelem, elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3320 | void *ptr, *ptr2; |
| 3321 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3322 | memset(&hook, 0, sizeof(hook)); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3323 | |
| 3324 | alloc.ctx = &hook; |
| 3325 | alloc.malloc = &hook_malloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3326 | alloc.calloc = &hook_calloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3327 | alloc.realloc = &hook_realloc; |
| 3328 | alloc.free = &hook_free; |
| 3329 | PyMem_GetAllocator(domain, &hook.alloc); |
| 3330 | PyMem_SetAllocator(domain, &alloc); |
| 3331 | |
| 3332 | size = 42; |
| 3333 | switch(domain) |
| 3334 | { |
| 3335 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; |
| 3336 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; |
| 3337 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; |
| 3338 | default: ptr = NULL; break; |
| 3339 | } |
| 3340 | |
| 3341 | if (ptr == NULL) { |
| 3342 | error_msg = "malloc failed"; |
| 3343 | goto fail; |
| 3344 | } |
| 3345 | |
| 3346 | if (hook.malloc_size != size) { |
| 3347 | error_msg = "malloc invalid size"; |
| 3348 | goto fail; |
| 3349 | } |
| 3350 | |
| 3351 | size2 = 200; |
| 3352 | switch(domain) |
| 3353 | { |
| 3354 | case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; |
| 3355 | case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; |
| 3356 | case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; |
Christian Heimes | 865d12a | 2013-08-02 11:10:51 +0200 | [diff] [blame] | 3357 | default: ptr2 = NULL; break; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | if (ptr2 == NULL) { |
| 3361 | error_msg = "realloc failed"; |
| 3362 | goto fail; |
| 3363 | } |
| 3364 | |
| 3365 | if (hook.realloc_ptr != ptr |
| 3366 | || hook.realloc_new_size != size2) { |
| 3367 | error_msg = "realloc invalid parameters"; |
| 3368 | goto fail; |
| 3369 | } |
| 3370 | |
| 3371 | switch(domain) |
| 3372 | { |
| 3373 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; |
| 3374 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; |
| 3375 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; |
| 3376 | } |
| 3377 | |
| 3378 | if (hook.free_ptr != ptr2) { |
| 3379 | error_msg = "free invalid pointer"; |
| 3380 | goto fail; |
| 3381 | } |
| 3382 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3383 | nelem = 2; |
| 3384 | elsize = 5; |
| 3385 | switch(domain) |
| 3386 | { |
| 3387 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; |
| 3388 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; |
| 3389 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; |
| 3390 | default: ptr = NULL; break; |
| 3391 | } |
| 3392 | |
| 3393 | if (ptr == NULL) { |
| 3394 | error_msg = "calloc failed"; |
| 3395 | goto fail; |
| 3396 | } |
| 3397 | |
| 3398 | if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { |
| 3399 | error_msg = "calloc invalid nelem or elsize"; |
| 3400 | goto fail; |
| 3401 | } |
| 3402 | |
| 3403 | switch(domain) |
| 3404 | { |
| 3405 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; |
| 3406 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; |
| 3407 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; |
| 3408 | } |
| 3409 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3410 | Py_INCREF(Py_None); |
| 3411 | res = Py_None; |
| 3412 | goto finally; |
| 3413 | |
| 3414 | fail: |
| 3415 | PyErr_SetString(PyExc_RuntimeError, error_msg); |
| 3416 | |
| 3417 | finally: |
| 3418 | PyMem_SetAllocator(domain, &hook.alloc); |
| 3419 | return res; |
| 3420 | } |
| 3421 | |
| 3422 | static PyObject * |
| 3423 | test_pymem_setrawallocators(PyObject *self) |
| 3424 | { |
| 3425 | return test_setallocators(PYMEM_DOMAIN_RAW); |
| 3426 | } |
| 3427 | |
| 3428 | static PyObject * |
| 3429 | test_pymem_setallocators(PyObject *self) |
| 3430 | { |
| 3431 | return test_setallocators(PYMEM_DOMAIN_MEM); |
| 3432 | } |
| 3433 | |
| 3434 | static PyObject * |
| 3435 | test_pyobject_setallocators(PyObject *self) |
| 3436 | { |
| 3437 | return test_setallocators(PYMEM_DOMAIN_OBJ); |
| 3438 | } |
| 3439 | |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 3440 | /* Most part of the following code is inherited from the pyfailmalloc project |
| 3441 | * written by Victor Stinner. */ |
| 3442 | static struct { |
| 3443 | int installed; |
| 3444 | PyMemAllocatorEx raw; |
| 3445 | PyMemAllocatorEx mem; |
| 3446 | PyMemAllocatorEx obj; |
| 3447 | } FmHook; |
| 3448 | |
| 3449 | static struct { |
| 3450 | int start; |
| 3451 | int stop; |
| 3452 | Py_ssize_t count; |
| 3453 | } FmData; |
| 3454 | |
| 3455 | static int |
| 3456 | fm_nomemory(void) |
| 3457 | { |
| 3458 | FmData.count++; |
| 3459 | if (FmData.count > FmData.start && |
| 3460 | (FmData.stop <= 0 || FmData.count <= FmData.stop)) { |
| 3461 | return 1; |
| 3462 | } |
| 3463 | return 0; |
| 3464 | } |
| 3465 | |
| 3466 | static void * |
| 3467 | hook_fmalloc(void *ctx, size_t size) |
| 3468 | { |
| 3469 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3470 | if (fm_nomemory()) { |
| 3471 | return NULL; |
| 3472 | } |
| 3473 | return alloc->malloc(alloc->ctx, size); |
| 3474 | } |
| 3475 | |
| 3476 | static void * |
| 3477 | hook_fcalloc(void *ctx, size_t nelem, size_t elsize) |
| 3478 | { |
| 3479 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3480 | if (fm_nomemory()) { |
| 3481 | return NULL; |
| 3482 | } |
| 3483 | return alloc->calloc(alloc->ctx, nelem, elsize); |
| 3484 | } |
| 3485 | |
| 3486 | static void * |
| 3487 | hook_frealloc(void *ctx, void *ptr, size_t new_size) |
| 3488 | { |
| 3489 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3490 | if (fm_nomemory()) { |
| 3491 | return NULL; |
| 3492 | } |
| 3493 | return alloc->realloc(alloc->ctx, ptr, new_size); |
| 3494 | } |
| 3495 | |
| 3496 | static void |
| 3497 | hook_ffree(void *ctx, void *ptr) |
| 3498 | { |
| 3499 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3500 | alloc->free(alloc->ctx, ptr); |
| 3501 | } |
| 3502 | |
| 3503 | static void |
| 3504 | fm_setup_hooks(void) |
| 3505 | { |
| 3506 | PyMemAllocatorEx alloc; |
| 3507 | |
| 3508 | if (FmHook.installed) { |
| 3509 | return; |
| 3510 | } |
| 3511 | FmHook.installed = 1; |
| 3512 | |
| 3513 | alloc.malloc = hook_fmalloc; |
| 3514 | alloc.calloc = hook_fcalloc; |
| 3515 | alloc.realloc = hook_frealloc; |
| 3516 | alloc.free = hook_ffree; |
| 3517 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 3518 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 3519 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 3520 | |
| 3521 | alloc.ctx = &FmHook.raw; |
| 3522 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 3523 | |
| 3524 | alloc.ctx = &FmHook.mem; |
| 3525 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 3526 | |
| 3527 | alloc.ctx = &FmHook.obj; |
| 3528 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 3529 | } |
| 3530 | |
| 3531 | static void |
| 3532 | fm_remove_hooks(void) |
| 3533 | { |
| 3534 | if (FmHook.installed) { |
| 3535 | FmHook.installed = 0; |
| 3536 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 3537 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 3538 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | static PyObject* |
| 3543 | set_nomemory(PyObject *self, PyObject *args) |
| 3544 | { |
| 3545 | /* Memory allocation fails after 'start' allocation requests, and until |
| 3546 | * 'stop' allocation requests except when 'stop' is negative or equal |
| 3547 | * to 0 (default) in which case allocation failures never stop. */ |
| 3548 | FmData.count = 0; |
| 3549 | FmData.stop = 0; |
| 3550 | if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) { |
| 3551 | return NULL; |
| 3552 | } |
| 3553 | fm_setup_hooks(); |
| 3554 | Py_RETURN_NONE; |
| 3555 | } |
| 3556 | |
| 3557 | static PyObject* |
| 3558 | remove_mem_hooks(PyObject *self) |
| 3559 | { |
| 3560 | fm_remove_hooks(); |
| 3561 | Py_RETURN_NONE; |
| 3562 | } |
| 3563 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3564 | PyDoc_STRVAR(docstring_empty, |
| 3565 | "" |
| 3566 | ); |
| 3567 | |
| 3568 | PyDoc_STRVAR(docstring_no_signature, |
| 3569 | "This docstring has no signature." |
| 3570 | ); |
| 3571 | |
| 3572 | PyDoc_STRVAR(docstring_with_invalid_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3573 | "docstring_with_invalid_signature($module, /, boo)\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3574 | "\n" |
| 3575 | "This docstring has an invalid signature." |
| 3576 | ); |
| 3577 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3578 | PyDoc_STRVAR(docstring_with_invalid_signature2, |
| 3579 | "docstring_with_invalid_signature2($module, /, boo)\n" |
| 3580 | "\n" |
| 3581 | "--\n" |
| 3582 | "\n" |
| 3583 | "This docstring also has an invalid signature." |
| 3584 | ); |
| 3585 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3586 | PyDoc_STRVAR(docstring_with_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3587 | "docstring_with_signature($module, /, sig)\n" |
| 3588 | "--\n" |
| 3589 | "\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3590 | "This docstring has a valid signature." |
| 3591 | ); |
| 3592 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 3593 | PyDoc_STRVAR(docstring_with_signature_but_no_doc, |
| 3594 | "docstring_with_signature_but_no_doc($module, /, sig)\n" |
| 3595 | "--\n" |
| 3596 | "\n" |
| 3597 | ); |
| 3598 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3599 | PyDoc_STRVAR(docstring_with_signature_and_extra_newlines, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3600 | "docstring_with_signature_and_extra_newlines($module, /, parameter)\n" |
| 3601 | "--\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3602 | "\n" |
| 3603 | "\n" |
| 3604 | "This docstring has a valid signature and some extra newlines." |
| 3605 | ); |
| 3606 | |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 3607 | PyDoc_STRVAR(docstring_with_signature_with_defaults, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3608 | "docstring_with_signature_with_defaults(module, s='avocado',\n" |
| 3609 | " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n" |
| 3610 | " local=the_number_three, sys=sys.maxsize,\n" |
| 3611 | " exp=sys.maxsize - 1)\n" |
| 3612 | "--\n" |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 3613 | "\n" |
| 3614 | "\n" |
| 3615 | "\n" |
| 3616 | "This docstring has a valid signature with parameters,\n" |
| 3617 | "and the parameters take defaults of varying types." |
| 3618 | ); |
| 3619 | |
Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 3620 | #ifdef WITH_THREAD |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3621 | typedef struct { |
| 3622 | PyThread_type_lock start_event; |
| 3623 | PyThread_type_lock exit_event; |
| 3624 | PyObject *callback; |
| 3625 | } test_c_thread_t; |
| 3626 | |
| 3627 | static void |
| 3628 | temporary_c_thread(void *data) |
| 3629 | { |
| 3630 | test_c_thread_t *test_c_thread = data; |
| 3631 | PyGILState_STATE state; |
| 3632 | PyObject *res; |
| 3633 | |
| 3634 | PyThread_release_lock(test_c_thread->start_event); |
| 3635 | |
| 3636 | /* Allocate a Python thread state for this thread */ |
| 3637 | state = PyGILState_Ensure(); |
| 3638 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 3639 | res = _PyObject_CallNoArg(test_c_thread->callback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3640 | Py_CLEAR(test_c_thread->callback); |
| 3641 | |
| 3642 | if (res == NULL) { |
| 3643 | PyErr_Print(); |
| 3644 | } |
| 3645 | else { |
| 3646 | Py_DECREF(res); |
| 3647 | } |
| 3648 | |
| 3649 | /* Destroy the Python thread state for this thread */ |
| 3650 | PyGILState_Release(state); |
| 3651 | |
| 3652 | PyThread_release_lock(test_c_thread->exit_event); |
| 3653 | |
| 3654 | PyThread_exit_thread(); |
| 3655 | } |
| 3656 | |
| 3657 | static PyObject * |
| 3658 | call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
| 3659 | { |
| 3660 | PyObject *res = NULL; |
| 3661 | test_c_thread_t test_c_thread; |
| 3662 | long thread; |
| 3663 | |
| 3664 | PyEval_InitThreads(); |
| 3665 | |
| 3666 | test_c_thread.start_event = PyThread_allocate_lock(); |
| 3667 | test_c_thread.exit_event = PyThread_allocate_lock(); |
| 3668 | test_c_thread.callback = NULL; |
| 3669 | if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
| 3670 | PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
| 3671 | goto exit; |
| 3672 | } |
| 3673 | |
| 3674 | Py_INCREF(callback); |
| 3675 | test_c_thread.callback = callback; |
| 3676 | |
| 3677 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 3678 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 3679 | |
| 3680 | thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
| 3681 | if (thread == -1) { |
| 3682 | PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
| 3683 | PyThread_release_lock(test_c_thread.start_event); |
| 3684 | PyThread_release_lock(test_c_thread.exit_event); |
| 3685 | goto exit; |
| 3686 | } |
| 3687 | |
| 3688 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 3689 | PyThread_release_lock(test_c_thread.start_event); |
| 3690 | |
| 3691 | Py_BEGIN_ALLOW_THREADS |
| 3692 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 3693 | PyThread_release_lock(test_c_thread.exit_event); |
| 3694 | Py_END_ALLOW_THREADS |
| 3695 | |
| 3696 | Py_INCREF(Py_None); |
| 3697 | res = Py_None; |
| 3698 | |
| 3699 | exit: |
| 3700 | Py_CLEAR(test_c_thread.callback); |
| 3701 | if (test_c_thread.start_event) |
| 3702 | PyThread_free_lock(test_c_thread.start_event); |
| 3703 | if (test_c_thread.exit_event) |
| 3704 | PyThread_free_lock(test_c_thread.exit_event); |
| 3705 | return res; |
| 3706 | } |
Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 3707 | #endif /* WITH_THREAD */ |
Victor Stinner | 1310510 | 2013-12-13 02:17:29 +0100 | [diff] [blame] | 3708 | |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 3709 | static PyObject* |
| 3710 | test_raise_signal(PyObject* self, PyObject *args) |
| 3711 | { |
| 3712 | int signum, err; |
| 3713 | |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 3714 | if (!PyArg_ParseTuple(args, "i:raise_signal", &signum)) { |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 3715 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 3716 | } |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 3717 | |
| 3718 | err = raise(signum); |
| 3719 | if (err) |
| 3720 | return PyErr_SetFromErrno(PyExc_OSError); |
| 3721 | |
| 3722 | if (PyErr_CheckSignals() < 0) |
| 3723 | return NULL; |
| 3724 | |
| 3725 | Py_RETURN_NONE; |
| 3726 | } |
| 3727 | |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 3728 | /* marshal */ |
| 3729 | |
| 3730 | static PyObject* |
| 3731 | pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
| 3732 | { |
| 3733 | long value; |
| 3734 | char *filename; |
| 3735 | int version; |
| 3736 | FILE *fp; |
| 3737 | |
| 3738 | if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", |
| 3739 | &value, &filename, &version)) |
| 3740 | return NULL; |
| 3741 | |
| 3742 | fp = fopen(filename, "wb"); |
| 3743 | if (fp == NULL) { |
| 3744 | PyErr_SetFromErrno(PyExc_OSError); |
| 3745 | return NULL; |
| 3746 | } |
| 3747 | |
| 3748 | PyMarshal_WriteLongToFile(value, fp, version); |
| 3749 | |
| 3750 | fclose(fp); |
| 3751 | if (PyErr_Occurred()) |
| 3752 | return NULL; |
| 3753 | Py_RETURN_NONE; |
| 3754 | } |
| 3755 | |
| 3756 | static PyObject* |
| 3757 | pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
| 3758 | { |
| 3759 | PyObject *obj; |
| 3760 | char *filename; |
| 3761 | int version; |
| 3762 | FILE *fp; |
| 3763 | |
| 3764 | if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", |
| 3765 | &obj, &filename, &version)) |
| 3766 | return NULL; |
| 3767 | |
| 3768 | fp = fopen(filename, "wb"); |
| 3769 | if (fp == NULL) { |
| 3770 | PyErr_SetFromErrno(PyExc_OSError); |
| 3771 | return NULL; |
| 3772 | } |
| 3773 | |
| 3774 | PyMarshal_WriteObjectToFile(obj, fp, version); |
| 3775 | |
| 3776 | fclose(fp); |
| 3777 | if (PyErr_Occurred()) |
| 3778 | return NULL; |
| 3779 | Py_RETURN_NONE; |
| 3780 | } |
| 3781 | |
| 3782 | static PyObject* |
| 3783 | pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
| 3784 | { |
| 3785 | int value; |
| 3786 | long pos; |
| 3787 | char *filename; |
| 3788 | FILE *fp; |
| 3789 | |
| 3790 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) |
| 3791 | return NULL; |
| 3792 | |
| 3793 | fp = fopen(filename, "rb"); |
| 3794 | if (fp == NULL) { |
| 3795 | PyErr_SetFromErrno(PyExc_OSError); |
| 3796 | return NULL; |
| 3797 | } |
| 3798 | |
| 3799 | value = PyMarshal_ReadShortFromFile(fp); |
| 3800 | pos = ftell(fp); |
| 3801 | |
| 3802 | fclose(fp); |
| 3803 | if (PyErr_Occurred()) |
| 3804 | return NULL; |
| 3805 | return Py_BuildValue("il", value, pos); |
| 3806 | } |
| 3807 | |
| 3808 | static PyObject* |
| 3809 | pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
| 3810 | { |
| 3811 | long value, pos; |
| 3812 | char *filename; |
| 3813 | FILE *fp; |
| 3814 | |
| 3815 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) |
| 3816 | return NULL; |
| 3817 | |
| 3818 | fp = fopen(filename, "rb"); |
| 3819 | if (fp == NULL) { |
| 3820 | PyErr_SetFromErrno(PyExc_OSError); |
| 3821 | return NULL; |
| 3822 | } |
| 3823 | |
| 3824 | value = PyMarshal_ReadLongFromFile(fp); |
| 3825 | pos = ftell(fp); |
| 3826 | |
| 3827 | fclose(fp); |
| 3828 | if (PyErr_Occurred()) |
| 3829 | return NULL; |
| 3830 | return Py_BuildValue("ll", value, pos); |
| 3831 | } |
| 3832 | |
| 3833 | static PyObject* |
| 3834 | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
| 3835 | { |
| 3836 | PyObject *obj; |
| 3837 | long pos; |
| 3838 | char *filename; |
| 3839 | FILE *fp; |
| 3840 | |
| 3841 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) |
| 3842 | return NULL; |
| 3843 | |
| 3844 | fp = fopen(filename, "rb"); |
| 3845 | if (fp == NULL) { |
| 3846 | PyErr_SetFromErrno(PyExc_OSError); |
| 3847 | return NULL; |
| 3848 | } |
| 3849 | |
| 3850 | obj = PyMarshal_ReadLastObjectFromFile(fp); |
| 3851 | pos = ftell(fp); |
| 3852 | |
| 3853 | fclose(fp); |
| 3854 | return Py_BuildValue("Nl", obj, pos); |
| 3855 | } |
| 3856 | |
| 3857 | static PyObject* |
| 3858 | pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
| 3859 | { |
| 3860 | PyObject *obj; |
| 3861 | long pos; |
| 3862 | char *filename; |
| 3863 | FILE *fp; |
| 3864 | |
| 3865 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) |
| 3866 | return NULL; |
| 3867 | |
| 3868 | fp = fopen(filename, "rb"); |
| 3869 | if (fp == NULL) { |
| 3870 | PyErr_SetFromErrno(PyExc_OSError); |
| 3871 | return NULL; |
| 3872 | } |
| 3873 | |
| 3874 | obj = PyMarshal_ReadObjectFromFile(fp); |
| 3875 | pos = ftell(fp); |
| 3876 | |
| 3877 | fclose(fp); |
| 3878 | return Py_BuildValue("Nl", obj, pos); |
| 3879 | } |
| 3880 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3881 | static PyObject* |
| 3882 | return_null_without_error(PyObject *self, PyObject *args) |
| 3883 | { |
| 3884 | /* invalid call: return NULL without setting an error, |
| 3885 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 3886 | PyErr_Clear(); |
| 3887 | return NULL; |
| 3888 | } |
| 3889 | |
| 3890 | static PyObject* |
| 3891 | return_result_with_error(PyObject *self, PyObject *args) |
| 3892 | { |
| 3893 | /* invalid call: return a result with an error set, |
| 3894 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 3895 | PyErr_SetNone(PyExc_ValueError); |
| 3896 | Py_RETURN_NONE; |
| 3897 | } |
| 3898 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 3899 | static PyObject * |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 3900 | test_pytime_fromseconds(PyObject *self, PyObject *args) |
| 3901 | { |
| 3902 | int seconds; |
| 3903 | _PyTime_t ts; |
| 3904 | |
| 3905 | if (!PyArg_ParseTuple(args, "i", &seconds)) |
| 3906 | return NULL; |
| 3907 | ts = _PyTime_FromSeconds(seconds); |
| 3908 | return _PyTime_AsNanosecondsObject(ts); |
| 3909 | } |
| 3910 | |
| 3911 | static PyObject * |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 3912 | test_pytime_fromsecondsobject(PyObject *self, PyObject *args) |
| 3913 | { |
| 3914 | PyObject *obj; |
| 3915 | int round; |
| 3916 | _PyTime_t ts; |
| 3917 | |
| 3918 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
| 3919 | return NULL; |
| 3920 | if (check_time_rounding(round) < 0) |
| 3921 | return NULL; |
| 3922 | if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) |
| 3923 | return NULL; |
| 3924 | return _PyTime_AsNanosecondsObject(ts); |
| 3925 | } |
| 3926 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 3927 | static PyObject * |
| 3928 | test_pytime_assecondsdouble(PyObject *self, PyObject *args) |
| 3929 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3930 | long long ns; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 3931 | _PyTime_t ts; |
| 3932 | double d; |
| 3933 | |
| 3934 | if (!PyArg_ParseTuple(args, "L", &ns)) |
| 3935 | return NULL; |
| 3936 | ts = _PyTime_FromNanoseconds(ns); |
| 3937 | d = _PyTime_AsSecondsDouble(ts); |
| 3938 | return PyFloat_FromDouble(d); |
| 3939 | } |
| 3940 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3941 | static PyObject * |
| 3942 | test_PyTime_AsTimeval(PyObject *self, PyObject *args) |
| 3943 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3944 | long long ns; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3945 | int round; |
| 3946 | _PyTime_t t; |
| 3947 | struct timeval tv; |
| 3948 | PyObject *seconds; |
| 3949 | |
| 3950 | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
| 3951 | return NULL; |
| 3952 | if (check_time_rounding(round) < 0) |
| 3953 | return NULL; |
| 3954 | t = _PyTime_FromNanoseconds(ns); |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 3955 | if (_PyTime_AsTimeval(t, &tv, round) < 0) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3956 | return NULL; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3957 | |
Benjamin Peterson | 2c134c3 | 2017-04-13 01:44:54 -0700 | [diff] [blame] | 3958 | seconds = PyLong_FromLongLong(tv.tv_sec); |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3959 | if (seconds == NULL) |
| 3960 | return NULL; |
| 3961 | return Py_BuildValue("Nl", seconds, tv.tv_usec); |
| 3962 | } |
| 3963 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 3964 | #ifdef HAVE_CLOCK_GETTIME |
| 3965 | static PyObject * |
| 3966 | test_PyTime_AsTimespec(PyObject *self, PyObject *args) |
| 3967 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3968 | long long ns; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 3969 | _PyTime_t t; |
| 3970 | struct timespec ts; |
| 3971 | |
| 3972 | if (!PyArg_ParseTuple(args, "L", &ns)) |
| 3973 | return NULL; |
| 3974 | t = _PyTime_FromNanoseconds(ns); |
| 3975 | if (_PyTime_AsTimespec(t, &ts) == -1) |
| 3976 | return NULL; |
| 3977 | return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); |
| 3978 | } |
| 3979 | #endif |
| 3980 | |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 3981 | static PyObject * |
| 3982 | test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) |
| 3983 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3984 | long long ns; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 3985 | int round; |
| 3986 | _PyTime_t t, ms; |
| 3987 | |
| 3988 | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
| 3989 | return NULL; |
| 3990 | if (check_time_rounding(round) < 0) |
| 3991 | return NULL; |
| 3992 | t = _PyTime_FromNanoseconds(ns); |
| 3993 | ms = _PyTime_AsMilliseconds(t, round); |
| 3994 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 3995 | nanoseconds */ |
| 3996 | return _PyTime_AsNanosecondsObject(ms); |
| 3997 | } |
| 3998 | |
| 3999 | static PyObject * |
| 4000 | test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) |
| 4001 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4002 | long long ns; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4003 | int round; |
| 4004 | _PyTime_t t, ms; |
| 4005 | |
| 4006 | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
| 4007 | return NULL; |
| 4008 | if (check_time_rounding(round) < 0) |
| 4009 | return NULL; |
| 4010 | t = _PyTime_FromNanoseconds(ns); |
| 4011 | ms = _PyTime_AsMicroseconds(t, round); |
| 4012 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4013 | nanoseconds */ |
| 4014 | return _PyTime_AsNanosecondsObject(ms); |
| 4015 | } |
| 4016 | |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4017 | static PyObject* |
| 4018 | get_recursion_depth(PyObject *self, PyObject *args) |
| 4019 | { |
| 4020 | PyThreadState *tstate = PyThreadState_GET(); |
| 4021 | |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 4022 | /* subtract one to ignore the frame of the get_recursion_depth() call */ |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4023 | return PyLong_FromLong(tstate->recursion_depth - 1); |
| 4024 | } |
| 4025 | |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4026 | static PyObject* |
| 4027 | pymem_buffer_overflow(PyObject *self, PyObject *args) |
| 4028 | { |
| 4029 | char *buffer; |
| 4030 | |
| 4031 | /* Deliberate buffer overflow to check that PyMem_Free() detects |
| 4032 | the overflow when debug hooks are installed. */ |
| 4033 | buffer = PyMem_Malloc(16); |
| 4034 | buffer[16] = 'x'; |
| 4035 | PyMem_Free(buffer); |
| 4036 | |
| 4037 | Py_RETURN_NONE; |
| 4038 | } |
| 4039 | |
| 4040 | static PyObject* |
| 4041 | pymem_api_misuse(PyObject *self, PyObject *args) |
| 4042 | { |
| 4043 | char *buffer; |
| 4044 | |
| 4045 | /* Deliberate misusage of Python allocators: |
| 4046 | allococate with PyMem but release with PyMem_Raw. */ |
| 4047 | buffer = PyMem_Malloc(16); |
| 4048 | PyMem_RawFree(buffer); |
| 4049 | |
| 4050 | Py_RETURN_NONE; |
| 4051 | } |
| 4052 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4053 | static PyObject* |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4054 | pymem_malloc_without_gil(PyObject *self, PyObject *args) |
| 4055 | { |
| 4056 | char *buffer; |
| 4057 | |
| 4058 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4059 | call PyMem_Malloc() without holding the GIL */ |
| 4060 | Py_BEGIN_ALLOW_THREADS |
| 4061 | buffer = PyMem_Malloc(10); |
| 4062 | Py_END_ALLOW_THREADS |
| 4063 | |
| 4064 | PyMem_Free(buffer); |
| 4065 | |
| 4066 | Py_RETURN_NONE; |
| 4067 | } |
| 4068 | |
| 4069 | static PyObject* |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4070 | pyobject_malloc_without_gil(PyObject *self, PyObject *args) |
| 4071 | { |
| 4072 | char *buffer; |
| 4073 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4074 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4075 | call PyObject_Malloc() without holding the GIL */ |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4076 | Py_BEGIN_ALLOW_THREADS |
| 4077 | buffer = PyObject_Malloc(10); |
| 4078 | Py_END_ALLOW_THREADS |
| 4079 | |
| 4080 | PyObject_Free(buffer); |
| 4081 | |
| 4082 | Py_RETURN_NONE; |
| 4083 | } |
| 4084 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4085 | static PyObject * |
| 4086 | tracemalloc_track(PyObject *self, PyObject *args) |
| 4087 | { |
| 4088 | unsigned int domain; |
| 4089 | PyObject *ptr_obj; |
| 4090 | void *ptr; |
| 4091 | Py_ssize_t size; |
| 4092 | int release_gil = 0; |
| 4093 | int res; |
| 4094 | |
| 4095 | if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil)) |
| 4096 | return NULL; |
| 4097 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4098 | if (PyErr_Occurred()) |
| 4099 | return NULL; |
| 4100 | |
| 4101 | if (release_gil) { |
| 4102 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4103 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4104 | Py_END_ALLOW_THREADS |
| 4105 | } |
| 4106 | else { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4107 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4111 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4112 | return NULL; |
| 4113 | } |
| 4114 | |
| 4115 | Py_RETURN_NONE; |
| 4116 | } |
| 4117 | |
| 4118 | static PyObject * |
| 4119 | tracemalloc_untrack(PyObject *self, PyObject *args) |
| 4120 | { |
| 4121 | unsigned int domain; |
| 4122 | PyObject *ptr_obj; |
| 4123 | void *ptr; |
| 4124 | int res; |
| 4125 | |
| 4126 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4127 | return NULL; |
| 4128 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4129 | if (PyErr_Occurred()) |
| 4130 | return NULL; |
| 4131 | |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4132 | res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4133 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4134 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4135 | return NULL; |
| 4136 | } |
| 4137 | |
| 4138 | Py_RETURN_NONE; |
| 4139 | } |
| 4140 | |
| 4141 | static PyObject * |
| 4142 | tracemalloc_get_traceback(PyObject *self, PyObject *args) |
| 4143 | { |
| 4144 | unsigned int domain; |
| 4145 | PyObject *ptr_obj; |
| 4146 | void *ptr; |
| 4147 | |
| 4148 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4149 | return NULL; |
| 4150 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4151 | if (PyErr_Occurred()) |
| 4152 | return NULL; |
| 4153 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 4154 | return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4155 | } |
| 4156 | |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4157 | static PyObject * |
| 4158 | dict_get_version(PyObject *self, PyObject *args) |
| 4159 | { |
| 4160 | PyDictObject *dict; |
| 4161 | uint64_t version; |
| 4162 | |
| 4163 | if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict)) |
| 4164 | return NULL; |
| 4165 | |
| 4166 | version = dict->ma_version_tag; |
| 4167 | |
| 4168 | Py_BUILD_ASSERT(sizeof(unsigned PY_LONG_LONG) >= sizeof(version)); |
| 4169 | return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)version); |
| 4170 | } |
| 4171 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4172 | |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 4173 | static PyObject * |
| 4174 | raise_SIGINT_then_send_None(PyObject *self, PyObject *args) |
| 4175 | { |
| 4176 | PyGenObject *gen; |
| 4177 | |
| 4178 | if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen)) |
| 4179 | return NULL; |
| 4180 | |
| 4181 | /* This is used in a test to check what happens if a signal arrives just |
| 4182 | as we're in the process of entering a yield from chain (see |
| 4183 | bpo-30039). |
| 4184 | |
| 4185 | Needs to be done in C, because: |
| 4186 | - we don't have a Python wrapper for raise() |
| 4187 | - we need to make sure that the Python-level signal handler doesn't run |
| 4188 | *before* we enter the generator frame, which is impossible in Python |
| 4189 | because we check for signals before every bytecode operation. |
| 4190 | */ |
| 4191 | raise(SIGINT); |
| 4192 | return _PyGen_Send(gen, Py_None); |
| 4193 | } |
| 4194 | |
| 4195 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4196 | static int |
| 4197 | fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs) |
| 4198 | { |
| 4199 | if (args == Py_None) { |
| 4200 | *stack = NULL; |
| 4201 | *nargs = 0; |
| 4202 | } |
| 4203 | else if (PyTuple_Check(args)) { |
| 4204 | *stack = &PyTuple_GET_ITEM(args, 0); |
| 4205 | *nargs = PyTuple_GET_SIZE(args); |
| 4206 | } |
| 4207 | else { |
| 4208 | PyErr_SetString(PyExc_TypeError, "args must be None or a tuple"); |
| 4209 | return -1; |
| 4210 | } |
| 4211 | return 0; |
| 4212 | } |
| 4213 | |
| 4214 | |
| 4215 | static PyObject * |
| 4216 | test_pyobject_fastcall(PyObject *self, PyObject *args) |
| 4217 | { |
| 4218 | PyObject *func, *func_args; |
| 4219 | PyObject **stack; |
| 4220 | Py_ssize_t nargs; |
| 4221 | |
| 4222 | if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) { |
| 4223 | return NULL; |
| 4224 | } |
| 4225 | |
| 4226 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4227 | return NULL; |
| 4228 | } |
| 4229 | return _PyObject_FastCall(func, stack, nargs); |
| 4230 | } |
| 4231 | |
| 4232 | |
| 4233 | static PyObject * |
| 4234 | test_pyobject_fastcalldict(PyObject *self, PyObject *args) |
| 4235 | { |
| 4236 | PyObject *func, *func_args, *kwargs; |
| 4237 | PyObject **stack; |
| 4238 | Py_ssize_t nargs; |
| 4239 | |
| 4240 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) { |
| 4241 | return NULL; |
| 4242 | } |
| 4243 | |
| 4244 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4245 | return NULL; |
| 4246 | } |
| 4247 | |
| 4248 | if (kwargs == Py_None) { |
| 4249 | kwargs = NULL; |
| 4250 | } |
| 4251 | else if (!PyDict_Check(kwargs)) { |
| 4252 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict"); |
| 4253 | return NULL; |
| 4254 | } |
| 4255 | |
| 4256 | return _PyObject_FastCallDict(func, stack, nargs, kwargs); |
| 4257 | } |
| 4258 | |
| 4259 | |
| 4260 | static PyObject * |
| 4261 | test_pyobject_fastcallkeywords(PyObject *self, PyObject *args) |
| 4262 | { |
| 4263 | PyObject *func, *func_args, *kwnames = NULL; |
| 4264 | PyObject **stack; |
| 4265 | Py_ssize_t nargs, nkw; |
| 4266 | |
| 4267 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) { |
| 4268 | return NULL; |
| 4269 | } |
| 4270 | |
| 4271 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4272 | return NULL; |
| 4273 | } |
| 4274 | |
| 4275 | if (kwnames == Py_None) { |
| 4276 | kwnames = NULL; |
| 4277 | } |
| 4278 | else if (PyTuple_Check(kwnames)) { |
| 4279 | nkw = PyTuple_GET_SIZE(kwnames); |
| 4280 | if (nargs < nkw) { |
| 4281 | PyErr_SetString(PyExc_ValueError, "kwnames longer than args"); |
| 4282 | return NULL; |
| 4283 | } |
| 4284 | nargs -= nkw; |
| 4285 | } |
| 4286 | else { |
| 4287 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple"); |
| 4288 | return NULL; |
| 4289 | } |
| 4290 | return _PyObject_FastCallKeywords(func, stack, nargs, kwnames); |
| 4291 | } |
| 4292 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4293 | |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 4294 | static PyObject* |
| 4295 | stack_pointer(PyObject *self, PyObject *args) |
| 4296 | { |
| 4297 | int v = 5; |
| 4298 | return PyLong_FromVoidPtr(&v); |
| 4299 | } |
| 4300 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4301 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4302 | #ifdef W_STOPCODE |
| 4303 | static PyObject* |
| 4304 | py_w_stopcode(PyObject *self, PyObject *args) |
| 4305 | { |
| 4306 | int sig, status; |
| 4307 | if (!PyArg_ParseTuple(args, "i", &sig)) { |
| 4308 | return NULL; |
| 4309 | } |
| 4310 | status = W_STOPCODE(sig); |
| 4311 | return PyLong_FromLong(status); |
| 4312 | } |
| 4313 | #endif |
| 4314 | |
| 4315 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4316 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 4318 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 4319 | {"set_errno", set_errno, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 4321 | {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4322 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
| 4323 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 4324 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 4325 | {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 4326 | {"dict_hassplittable", dict_hassplittable, METH_O}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4327 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 4329 | {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS}, |
| 4330 | {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak, METH_NOARGS}, |
| 4331 | {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak, METH_NOARGS}, |
| 4332 | {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak, METH_NOARGS}, |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 4333 | {"test_incref_decref_API", (PyCFunction)test_incref_decref_API, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4334 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 4335 | METH_NOARGS}, |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 4336 | {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS}, |
| 4337 | {"test_long_as_size_t", (PyCFunction)test_long_as_size_t,METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4338 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 4339 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 4340 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 4341 | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4342 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 4343 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 4344 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4345 | PyDoc_STR("This is a pretty normal docstring.")}, |
| 4346 | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
| 4347 | {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, |
| 4348 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 4349 | {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 4350 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 4351 | {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS}, |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 4352 | #endif |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 4353 | {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 4354 | {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 4355 | {"get_args", get_args, METH_VARARGS}, |
| 4356 | {"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 4358 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 4359 | METH_VARARGS|METH_KEYWORDS}, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 4360 | {"getargs_keyword_only", (PyCFunction)getargs_keyword_only, |
| 4361 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 4362 | {"getargs_positional_only_and_keywords", |
| 4363 | (PyCFunction)getargs_positional_only_and_keywords, |
| 4364 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4365 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 4366 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 4367 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 4368 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 4369 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 4370 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 4371 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 4372 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 4373 | {"getargs_n", getargs_n, METH_VARARGS}, |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 4374 | {"getargs_p", getargs_p, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 4376 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 4377 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 4378 | {"test_long_long_and_overflow", |
| 4379 | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
| 4380 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 4381 | {"getargs_f", getargs_f, METH_VARARGS}, |
| 4382 | {"getargs_d", getargs_d, METH_VARARGS}, |
| 4383 | {"getargs_D", getargs_D, METH_VARARGS}, |
| 4384 | {"getargs_S", getargs_S, METH_VARARGS}, |
| 4385 | {"getargs_Y", getargs_Y, METH_VARARGS}, |
| 4386 | {"getargs_U", getargs_U, METH_VARARGS}, |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 4387 | {"getargs_c", getargs_c, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 4388 | {"getargs_C", getargs_C, METH_VARARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 4389 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 4390 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 4391 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 4392 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 4393 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 4394 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 4395 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 4396 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 4397 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 4398 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 4399 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 4400 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 4401 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 4402 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 4403 | {"getargs_es", getargs_es, METH_VARARGS}, |
| 4404 | {"getargs_et", getargs_et, METH_VARARGS}, |
| 4405 | {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 4406 | {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4408 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4409 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4410 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
| 4412 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 4413 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
| 4414 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 4415 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 4416 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 4417 | {"unicode_asucs4", unicode_asucs4, METH_VARARGS}, |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 4418 | {"unicode_findchar", unicode_findchar, METH_VARARGS}, |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 4419 | {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 4420 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 4421 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 4422 | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4423 | #ifdef WITH_THREAD |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4424 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4426 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4427 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4428 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4429 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4430 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 4431 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 4432 | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4433 | {"argparsing", argparsing, METH_VARARGS}, |
| 4434 | {"code_newempty", code_newempty, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 4436 | METH_VARARGS | METH_KEYWORDS}, |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 4437 | {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer, |
| 4438 | METH_NOARGS}, |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 4439 | {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 4440 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4441 | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
| 4442 | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 4443 | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 4444 | {"with_tp_del", with_tp_del, METH_VARARGS}, |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 4445 | {"create_cfunction", create_cfunction, METH_NOARGS}, |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4446 | {"test_pymem_alloc0", |
Victor Stinner | 797bcb5 | 2014-06-02 21:29:07 +0200 | [diff] [blame] | 4447 | (PyCFunction)test_pymem_alloc0, METH_NOARGS}, |
| 4448 | {"test_pymem_setrawallocators", |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4449 | (PyCFunction)test_pymem_setrawallocators, METH_NOARGS}, |
| 4450 | {"test_pymem_setallocators", |
| 4451 | (PyCFunction)test_pymem_setallocators, METH_NOARGS}, |
| 4452 | {"test_pyobject_setallocators", |
| 4453 | (PyCFunction)test_pyobject_setallocators, METH_NOARGS}, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 4454 | {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS, |
| 4455 | PyDoc_STR("set_nomemory(start:int, stop:int = 0)")}, |
| 4456 | {"remove_mem_hooks", (PyCFunction)remove_mem_hooks, METH_NOARGS, |
| 4457 | PyDoc_STR("Remove memory hooks.")}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4458 | {"no_docstring", |
| 4459 | (PyCFunction)test_with_docstring, METH_NOARGS}, |
| 4460 | {"docstring_empty", |
| 4461 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4462 | docstring_empty}, |
| 4463 | {"docstring_no_signature", |
| 4464 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4465 | docstring_no_signature}, |
| 4466 | {"docstring_with_invalid_signature", |
| 4467 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4468 | docstring_with_invalid_signature}, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4469 | {"docstring_with_invalid_signature2", |
| 4470 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4471 | docstring_with_invalid_signature2}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4472 | {"docstring_with_signature", |
| 4473 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4474 | docstring_with_signature}, |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 4475 | {"docstring_with_signature_but_no_doc", |
| 4476 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4477 | docstring_with_signature_but_no_doc}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4478 | {"docstring_with_signature_and_extra_newlines", |
| 4479 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4480 | docstring_with_signature_and_extra_newlines}, |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4481 | {"docstring_with_signature_with_defaults", |
| 4482 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4483 | docstring_with_signature_with_defaults}, |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 4484 | {"raise_signal", |
| 4485 | (PyCFunction)test_raise_signal, METH_VARARGS}, |
Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 4486 | #ifdef WITH_THREAD |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4487 | {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
| 4488 | PyDoc_STR("set_error_class(error_class) -> None")}, |
Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 4489 | #endif |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4490 | {"pymarshal_write_long_to_file", |
| 4491 | pymarshal_write_long_to_file, METH_VARARGS}, |
| 4492 | {"pymarshal_write_object_to_file", |
| 4493 | pymarshal_write_object_to_file, METH_VARARGS}, |
| 4494 | {"pymarshal_read_short_from_file", |
| 4495 | pymarshal_read_short_from_file, METH_VARARGS}, |
| 4496 | {"pymarshal_read_long_from_file", |
| 4497 | pymarshal_read_long_from_file, METH_VARARGS}, |
| 4498 | {"pymarshal_read_last_object_from_file", |
| 4499 | pymarshal_read_last_object_from_file, METH_VARARGS}, |
| 4500 | {"pymarshal_read_object_from_file", |
| 4501 | pymarshal_read_object_from_file, METH_VARARGS}, |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4502 | {"return_null_without_error", |
| 4503 | return_null_without_error, METH_NOARGS}, |
| 4504 | {"return_result_with_error", |
| 4505 | return_result_with_error, METH_NOARGS}, |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 4506 | {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS}, |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4507 | {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS}, |
| 4508 | {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4509 | {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4510 | #ifdef HAVE_CLOCK_GETTIME |
| 4511 | {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, |
| 4512 | #endif |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4513 | {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS}, |
| 4514 | {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS}, |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4515 | {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4516 | {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS}, |
| 4517 | {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4518 | {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4519 | {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4520 | {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, |
| 4521 | {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, |
| 4522 | {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4523 | {"dict_get_version", dict_get_version, METH_VARARGS}, |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 4524 | {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS}, |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4525 | {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS}, |
| 4526 | {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS}, |
| 4527 | {"pyobject_fastcallkeywords", test_pyobject_fastcallkeywords, METH_VARARGS}, |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 4528 | {"stack_pointer", stack_pointer, METH_NOARGS}, |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4529 | #ifdef W_STOPCODE |
| 4530 | {"W_STOPCODE", py_w_stopcode, METH_VARARGS}, |
| 4531 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4532 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4533 | }; |
| 4534 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 4535 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 4536 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4537 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | char bool_member; |
| 4539 | char byte_member; |
| 4540 | unsigned char ubyte_member; |
| 4541 | short short_member; |
| 4542 | unsigned short ushort_member; |
| 4543 | int int_member; |
| 4544 | unsigned int uint_member; |
| 4545 | long long_member; |
| 4546 | unsigned long ulong_member; |
| 4547 | Py_ssize_t pyssizet_member; |
| 4548 | float float_member; |
| 4549 | double double_member; |
| 4550 | char inplace_member[6]; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4551 | long long longlong_member; |
| 4552 | unsigned long long ulonglong_member; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4553 | } all_structmembers; |
| 4554 | |
| 4555 | typedef struct { |
| 4556 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4557 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4558 | } test_structmembers; |
| 4559 | |
| 4560 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 4562 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 4563 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 4564 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 4565 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 4566 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 4567 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 4568 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 4569 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 4570 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 4571 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 4572 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 4573 | {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4574 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 4575 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4576 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4577 | }; |
| 4578 | |
| 4579 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4580 | static PyObject * |
| 4581 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4583 | static char *keywords[] = { |
| 4584 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 4585 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 4586 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4587 | "T_LONGLONG", "T_ULONGLONG", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4588 | NULL}; |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 4589 | static const char fmt[] = "|bbBhHiIlknfds#LK"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | test_structmembers *ob; |
| 4591 | const char *s = NULL; |
| 4592 | Py_ssize_t string_len = 0; |
| 4593 | ob = PyObject_New(test_structmembers, type); |
| 4594 | if (ob == NULL) |
| 4595 | return NULL; |
| 4596 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 4597 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 4598 | &ob->structmembers.bool_member, |
| 4599 | &ob->structmembers.byte_member, |
| 4600 | &ob->structmembers.ubyte_member, |
| 4601 | &ob->structmembers.short_member, |
| 4602 | &ob->structmembers.ushort_member, |
| 4603 | &ob->structmembers.int_member, |
| 4604 | &ob->structmembers.uint_member, |
| 4605 | &ob->structmembers.long_member, |
| 4606 | &ob->structmembers.ulong_member, |
| 4607 | &ob->structmembers.pyssizet_member, |
| 4608 | &ob->structmembers.float_member, |
| 4609 | &ob->structmembers.double_member, |
| 4610 | &s, &string_len |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4611 | , &ob->structmembers.longlong_member, |
| 4612 | &ob->structmembers.ulonglong_member |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4613 | )) { |
| 4614 | Py_DECREF(ob); |
| 4615 | return NULL; |
| 4616 | } |
| 4617 | if (s != NULL) { |
| 4618 | if (string_len > 5) { |
| 4619 | Py_DECREF(ob); |
| 4620 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 4621 | return NULL; |
| 4622 | } |
| 4623 | strcpy(ob->structmembers.inplace_member, s); |
| 4624 | } |
| 4625 | else { |
| 4626 | strcpy(ob->structmembers.inplace_member, ""); |
| 4627 | } |
| 4628 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4631 | static void |
| 4632 | test_structmembers_free(PyObject *ob) |
| 4633 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4638 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4639 | "test_structmembersType", |
| 4640 | sizeof(test_structmembers), /* tp_basicsize */ |
| 4641 | 0, /* tp_itemsize */ |
| 4642 | test_structmembers_free, /* destructor tp_dealloc */ |
| 4643 | 0, /* tp_print */ |
| 4644 | 0, /* tp_getattr */ |
| 4645 | 0, /* tp_setattr */ |
| 4646 | 0, /* tp_reserved */ |
| 4647 | 0, /* tp_repr */ |
| 4648 | 0, /* tp_as_number */ |
| 4649 | 0, /* tp_as_sequence */ |
| 4650 | 0, /* tp_as_mapping */ |
| 4651 | 0, /* tp_hash */ |
| 4652 | 0, /* tp_call */ |
| 4653 | 0, /* tp_str */ |
| 4654 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4655 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4656 | 0, /* tp_as_buffer */ |
| 4657 | 0, /* tp_flags */ |
| 4658 | "Type containing all structmember types", |
| 4659 | 0, /* traverseproc tp_traverse */ |
| 4660 | 0, /* tp_clear */ |
| 4661 | 0, /* tp_richcompare */ |
| 4662 | 0, /* tp_weaklistoffset */ |
| 4663 | 0, /* tp_iter */ |
| 4664 | 0, /* tp_iternext */ |
| 4665 | 0, /* tp_methods */ |
| 4666 | test_members, /* tp_members */ |
| 4667 | 0, |
| 4668 | 0, |
| 4669 | 0, |
| 4670 | 0, |
| 4671 | 0, |
| 4672 | 0, |
| 4673 | 0, |
| 4674 | 0, |
| 4675 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4676 | }; |
| 4677 | |
| 4678 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4679 | typedef struct { |
| 4680 | PyObject_HEAD |
| 4681 | } matmulObject; |
| 4682 | |
| 4683 | static PyObject * |
| 4684 | matmulType_matmul(PyObject *self, PyObject *other) |
| 4685 | { |
| 4686 | return Py_BuildValue("(sOO)", "matmul", self, other); |
| 4687 | } |
| 4688 | |
| 4689 | static PyObject * |
| 4690 | matmulType_imatmul(PyObject *self, PyObject *other) |
| 4691 | { |
| 4692 | return Py_BuildValue("(sOO)", "imatmul", self, other); |
| 4693 | } |
| 4694 | |
| 4695 | static void |
| 4696 | matmulType_dealloc(PyObject *self) |
| 4697 | { |
Zachary Ware | 420dc56 | 2014-04-23 13:51:27 -0500 | [diff] [blame] | 4698 | Py_TYPE(self)->tp_free(self); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4699 | } |
| 4700 | |
| 4701 | static PyNumberMethods matmulType_as_number = { |
| 4702 | 0, /* nb_add */ |
| 4703 | 0, /* nb_subtract */ |
| 4704 | 0, /* nb_multiply */ |
| 4705 | 0, /* nb_remainde r*/ |
| 4706 | 0, /* nb_divmod */ |
| 4707 | 0, /* nb_power */ |
| 4708 | 0, /* nb_negative */ |
| 4709 | 0, /* tp_positive */ |
| 4710 | 0, /* tp_absolute */ |
| 4711 | 0, /* tp_bool */ |
| 4712 | 0, /* nb_invert */ |
| 4713 | 0, /* nb_lshift */ |
| 4714 | 0, /* nb_rshift */ |
| 4715 | 0, /* nb_and */ |
| 4716 | 0, /* nb_xor */ |
| 4717 | 0, /* nb_or */ |
| 4718 | 0, /* nb_int */ |
| 4719 | 0, /* nb_reserved */ |
| 4720 | 0, /* nb_float */ |
| 4721 | 0, /* nb_inplace_add */ |
| 4722 | 0, /* nb_inplace_subtract */ |
| 4723 | 0, /* nb_inplace_multiply */ |
| 4724 | 0, /* nb_inplace_remainder */ |
| 4725 | 0, /* nb_inplace_power */ |
| 4726 | 0, /* nb_inplace_lshift */ |
| 4727 | 0, /* nb_inplace_rshift */ |
| 4728 | 0, /* nb_inplace_and */ |
| 4729 | 0, /* nb_inplace_xor */ |
| 4730 | 0, /* nb_inplace_or */ |
| 4731 | 0, /* nb_floor_divide */ |
| 4732 | 0, /* nb_true_divide */ |
| 4733 | 0, /* nb_inplace_floor_divide */ |
| 4734 | 0, /* nb_inplace_true_divide */ |
| 4735 | 0, /* nb_index */ |
| 4736 | matmulType_matmul, /* nb_matrix_multiply */ |
| 4737 | matmulType_imatmul /* nb_matrix_inplace_multiply */ |
| 4738 | }; |
| 4739 | |
| 4740 | static PyTypeObject matmulType = { |
| 4741 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4742 | "matmulType", |
| 4743 | sizeof(matmulObject), /* tp_basicsize */ |
| 4744 | 0, /* tp_itemsize */ |
| 4745 | matmulType_dealloc, /* destructor tp_dealloc */ |
| 4746 | 0, /* tp_print */ |
| 4747 | 0, /* tp_getattr */ |
| 4748 | 0, /* tp_setattr */ |
| 4749 | 0, /* tp_reserved */ |
| 4750 | 0, /* tp_repr */ |
| 4751 | &matmulType_as_number, /* tp_as_number */ |
| 4752 | 0, /* tp_as_sequence */ |
| 4753 | 0, /* tp_as_mapping */ |
| 4754 | 0, /* tp_hash */ |
| 4755 | 0, /* tp_call */ |
| 4756 | 0, /* tp_str */ |
| 4757 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4758 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4759 | 0, /* tp_as_buffer */ |
| 4760 | 0, /* tp_flags */ |
| 4761 | "C level type with matrix operations defined", |
| 4762 | 0, /* traverseproc tp_traverse */ |
| 4763 | 0, /* tp_clear */ |
| 4764 | 0, /* tp_richcompare */ |
| 4765 | 0, /* tp_weaklistoffset */ |
| 4766 | 0, /* tp_iter */ |
| 4767 | 0, /* tp_iternext */ |
| 4768 | 0, /* tp_methods */ |
| 4769 | 0, /* tp_members */ |
| 4770 | 0, |
| 4771 | 0, |
| 4772 | 0, |
| 4773 | 0, |
| 4774 | 0, |
| 4775 | 0, |
| 4776 | 0, |
| 4777 | 0, |
| 4778 | PyType_GenericNew, /* tp_new */ |
| 4779 | PyObject_Del, /* tp_free */ |
| 4780 | }; |
| 4781 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4782 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4783 | typedef struct { |
| 4784 | PyObject_HEAD |
| 4785 | PyObject *ao_iterator; |
| 4786 | } awaitObject; |
| 4787 | |
| 4788 | |
| 4789 | static PyObject * |
| 4790 | awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4791 | { |
| 4792 | PyObject *v; |
| 4793 | awaitObject *ao; |
| 4794 | |
| 4795 | if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v)) |
| 4796 | return NULL; |
| 4797 | |
| 4798 | ao = (awaitObject *)type->tp_alloc(type, 0); |
| 4799 | if (ao == NULL) { |
| 4800 | return NULL; |
| 4801 | } |
| 4802 | |
| 4803 | Py_INCREF(v); |
| 4804 | ao->ao_iterator = v; |
| 4805 | |
| 4806 | return (PyObject *)ao; |
| 4807 | } |
| 4808 | |
| 4809 | |
| 4810 | static void |
| 4811 | awaitObject_dealloc(awaitObject *ao) |
| 4812 | { |
| 4813 | Py_CLEAR(ao->ao_iterator); |
| 4814 | Py_TYPE(ao)->tp_free(ao); |
| 4815 | } |
| 4816 | |
| 4817 | |
| 4818 | static PyObject * |
| 4819 | awaitObject_await(awaitObject *ao) |
| 4820 | { |
| 4821 | Py_INCREF(ao->ao_iterator); |
| 4822 | return ao->ao_iterator; |
| 4823 | } |
| 4824 | |
| 4825 | static PyAsyncMethods awaitType_as_async = { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 4826 | (unaryfunc)awaitObject_await, /* am_await */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4827 | 0, /* am_aiter */ |
| 4828 | 0 /* am_anext */ |
| 4829 | }; |
| 4830 | |
| 4831 | |
| 4832 | static PyTypeObject awaitType = { |
| 4833 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4834 | "awaitType", |
| 4835 | sizeof(awaitObject), /* tp_basicsize */ |
| 4836 | 0, /* tp_itemsize */ |
| 4837 | (destructor)awaitObject_dealloc, /* destructor tp_dealloc */ |
| 4838 | 0, /* tp_print */ |
| 4839 | 0, /* tp_getattr */ |
| 4840 | 0, /* tp_setattr */ |
| 4841 | &awaitType_as_async, /* tp_as_async */ |
| 4842 | 0, /* tp_repr */ |
| 4843 | 0, /* tp_as_number */ |
| 4844 | 0, /* tp_as_sequence */ |
| 4845 | 0, /* tp_as_mapping */ |
| 4846 | 0, /* tp_hash */ |
| 4847 | 0, /* tp_call */ |
| 4848 | 0, /* tp_str */ |
| 4849 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4850 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4851 | 0, /* tp_as_buffer */ |
| 4852 | 0, /* tp_flags */ |
| 4853 | "C level type with tp_as_async", |
| 4854 | 0, /* traverseproc tp_traverse */ |
| 4855 | 0, /* tp_clear */ |
| 4856 | 0, /* tp_richcompare */ |
| 4857 | 0, /* tp_weaklistoffset */ |
| 4858 | 0, /* tp_iter */ |
| 4859 | 0, /* tp_iternext */ |
| 4860 | 0, /* tp_methods */ |
| 4861 | 0, /* tp_members */ |
| 4862 | 0, |
| 4863 | 0, |
| 4864 | 0, |
| 4865 | 0, |
| 4866 | 0, |
| 4867 | 0, |
| 4868 | 0, |
| 4869 | 0, |
| 4870 | awaitObject_new, /* tp_new */ |
| 4871 | PyObject_Del, /* tp_free */ |
| 4872 | }; |
| 4873 | |
| 4874 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4875 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4876 | PyModuleDef_HEAD_INIT, |
| 4877 | "_testcapi", |
| 4878 | NULL, |
| 4879 | -1, |
| 4880 | TestMethods, |
| 4881 | NULL, |
| 4882 | NULL, |
| 4883 | NULL, |
| 4884 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4885 | }; |
| 4886 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 4887 | /* Per PEP 489, this module will not be converted to multi-phase initialization |
| 4888 | */ |
| 4889 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 4890 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4891 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4892 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4893 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4895 | m = PyModule_Create(&_testcapimodule); |
| 4896 | if (m == NULL) |
| 4897 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4899 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 4900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4901 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 4902 | Py_INCREF(&test_structmembersType); |
| 4903 | /* don't use a name starting with "test", since we don't want |
| 4904 | test_capi to automatically call this */ |
| 4905 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4906 | if (PyType_Ready(&matmulType) < 0) |
| 4907 | return NULL; |
| 4908 | Py_INCREF(&matmulType); |
| 4909 | PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4910 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4911 | if (PyType_Ready(&awaitType) < 0) |
| 4912 | return NULL; |
| 4913 | Py_INCREF(&awaitType); |
| 4914 | PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType); |
| 4915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4916 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 4917 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 4918 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 4919 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 4920 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 4921 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 4922 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 4923 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 4924 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 4925 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 4926 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 4927 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 4928 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 4929 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 4930 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 4931 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 4932 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 4933 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 4934 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 4935 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 4936 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
| 4937 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 4938 | PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4939 | Py_INCREF(&PyInstanceMethod_Type); |
| 4940 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 4941 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 4942 | PyModule_AddIntConstant(m, "the_number_three", 3); |
| 4943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4944 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 4945 | Py_INCREF(TestError); |
| 4946 | PyModule_AddObject(m, "error", TestError); |
| 4947 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4948 | } |