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