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 | 3f2f4fe | 2020-03-13 13:07:31 +0100 | [diff] [blame] | 8 | /* This module tests the public (Include/ and Include/cpython/) C API. |
| 9 | The internal C API must not be used here: use _testinternalcapi for that. |
| 10 | |
| 11 | The Visual Studio projects builds _testcapi with Py_BUILD_CORE_MODULE |
| 12 | macro defined, but only the public C API must be tested here. */ |
Hai Shi | 5dd21f5 | 2020-04-21 00:49:13 +0800 | [diff] [blame] | 13 | |
Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 14 | #undef Py_BUILD_CORE_MODULE |
Hai Shi | 5dd21f5 | 2020-04-21 00:49:13 +0800 | [diff] [blame] | 15 | /* Always enable assertions */ |
| 16 | #undef NDEBUG |
Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 17 | |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 18 | #define PY_SSIZE_T_CLEAN |
| 19 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 20 | #include "Python.h" |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 21 | #include "datetime.h" |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 22 | #include "marshal.h" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 23 | #include "structmember.h" // PyMemberDef |
Victor Stinner | a1c249c | 2018-11-01 03:15:58 +0100 | [diff] [blame] | 24 | #include <float.h> |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 25 | #include <signal.h> |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 26 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 27 | #ifdef MS_WINDOWS |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 28 | # include <winsock2.h> /* struct timeval */ |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 29 | #endif |
| 30 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 31 | #ifdef HAVE_SYS_WAIT_H |
| 32 | #include <sys/wait.h> /* For W_STOPCODE */ |
| 33 | #endif |
| 34 | |
Victor Stinner | 5ed6995 | 2018-11-06 15:59:52 +0100 | [diff] [blame] | 35 | #ifdef Py_BUILD_CORE |
| 36 | # error "_testcapi must test the public Python C API, not CPython internal C API" |
| 37 | #endif |
| 38 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 39 | static struct PyModuleDef _testcapimodule; |
| 40 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | static PyObject *TestError; /* set to exception object in init */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 42 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 43 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 44 | |
| 45 | static PyObject * |
| 46 | raiseTestError(const char* test_name, const char* msg) |
| 47 | { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 48 | PyErr_Format(TestError, "%s: %s", test_name, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 52 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 53 | |
| 54 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 55 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 56 | platforms have these hardcoded. Better safe than sorry. |
| 57 | */ |
| 58 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 59 | sizeof_error(const char* fatname, const char* typname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | int expected, int got) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 61 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 62 | PyErr_Format(TestError, |
| 63 | "%s #define == %d but sizeof(%s) == %d", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | fatname, expected, typname, got); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | return (PyObject*)NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 69 | test_config(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 70 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 71 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | if (FATNAME != sizeof(TYPE)) \ |
| 73 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 74 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 76 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 77 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 78 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 79 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 80 | CHECK_SIZEOF(SIZEOF_LONG_LONG, long long); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 81 | |
| 82 | #undef CHECK_SIZEOF |
| 83 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 84 | Py_RETURN_NONE; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 87 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 88 | test_sizeof_c_types(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 89 | { |
Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 90 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 91 | #pragma GCC diagnostic push |
| 92 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 93 | #endif |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 94 | #define CHECK_SIZEOF(TYPE, EXPECTED) \ |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 95 | if (EXPECTED != sizeof(TYPE)) { \ |
| 96 | PyErr_Format(TestError, \ |
| 97 | "sizeof(%s) = %u instead of %u", \ |
| 98 | #TYPE, sizeof(TYPE), EXPECTED); \ |
| 99 | return (PyObject*)NULL; \ |
| 100 | } |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 101 | #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0) |
| 102 | #define CHECK_SIGNNESS(TYPE, SIGNED) \ |
| 103 | if (IS_SIGNED(TYPE) != SIGNED) { \ |
| 104 | PyErr_Format(TestError, \ |
| 105 | "%s signness is, instead of %i", \ |
| 106 | #TYPE, IS_SIGNED(TYPE), SIGNED); \ |
| 107 | return (PyObject*)NULL; \ |
| 108 | } |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 109 | |
| 110 | /* integer types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 111 | CHECK_SIZEOF(Py_UCS1, 1); |
| 112 | CHECK_SIZEOF(Py_UCS2, 2); |
| 113 | CHECK_SIZEOF(Py_UCS4, 4); |
| 114 | CHECK_SIGNNESS(Py_UCS1, 0); |
| 115 | CHECK_SIGNNESS(Py_UCS2, 0); |
| 116 | CHECK_SIGNNESS(Py_UCS4, 0); |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 117 | CHECK_SIZEOF(int32_t, 4); |
| 118 | CHECK_SIGNNESS(int32_t, 1); |
| 119 | CHECK_SIZEOF(uint32_t, 4); |
| 120 | CHECK_SIGNNESS(uint32_t, 0); |
| 121 | CHECK_SIZEOF(int64_t, 8); |
| 122 | CHECK_SIGNNESS(int64_t, 1); |
| 123 | CHECK_SIZEOF(uint64_t, 8); |
| 124 | CHECK_SIGNNESS(uint64_t, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 125 | |
| 126 | /* pointer/size types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 127 | CHECK_SIZEOF(size_t, sizeof(void *)); |
| 128 | CHECK_SIGNNESS(size_t, 0); |
| 129 | CHECK_SIZEOF(Py_ssize_t, sizeof(void *)); |
| 130 | CHECK_SIGNNESS(Py_ssize_t, 1); |
| 131 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 132 | CHECK_SIZEOF(uintptr_t, sizeof(void *)); |
| 133 | CHECK_SIGNNESS(uintptr_t, 0); |
| 134 | CHECK_SIZEOF(intptr_t, sizeof(void *)); |
| 135 | CHECK_SIGNNESS(intptr_t, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 136 | |
Serhiy Storchaka | d1302c0 | 2017-01-23 10:23:58 +0200 | [diff] [blame] | 137 | Py_RETURN_NONE; |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 138 | |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 139 | #undef IS_SIGNED |
| 140 | #undef CHECK_SIGNESS |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 141 | #undef CHECK_SIZEOF |
Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 142 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 143 | #pragma GCC diagnostic pop |
| 144 | #endif |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
| 148 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 149 | test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | PyObject* list; |
| 152 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 155 | #define NLIST 30 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | list = PyList_New(NLIST); |
| 157 | if (list == (PyObject*)NULL) |
| 158 | return (PyObject*)NULL; |
| 159 | /* list = range(NLIST) */ |
| 160 | for (i = 0; i < NLIST; ++i) { |
| 161 | PyObject* anint = PyLong_FromLong(i); |
| 162 | if (anint == (PyObject*)NULL) { |
| 163 | Py_DECREF(list); |
| 164 | return (PyObject*)NULL; |
| 165 | } |
| 166 | PyList_SET_ITEM(list, i, anint); |
| 167 | } |
| 168 | /* list.reverse(), via PyList_Reverse() */ |
| 169 | i = PyList_Reverse(list); /* should not blow up! */ |
| 170 | if (i != 0) { |
| 171 | Py_DECREF(list); |
| 172 | return (PyObject*)NULL; |
| 173 | } |
| 174 | /* Check that list == range(29, -1, -1) now */ |
| 175 | for (i = 0; i < NLIST; ++i) { |
| 176 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 177 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 178 | PyErr_SetString(TestError, |
| 179 | "test_list_api: reverse screwed up"); |
| 180 | Py_DECREF(list); |
| 181 | return (PyObject*)NULL; |
| 182 | } |
| 183 | } |
| 184 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 185 | #undef NLIST |
| 186 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 187 | Py_RETURN_NONE; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 190 | static int |
| 191 | test_dict_inner(int count) |
| 192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | Py_ssize_t pos = 0, iterations = 0; |
| 194 | int i; |
| 195 | PyObject *dict = PyDict_New(); |
| 196 | PyObject *v, *k; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | if (dict == NULL) |
| 199 | return -1; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | for (i = 0; i < count; i++) { |
| 202 | v = PyLong_FromLong(i); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 203 | if (v == NULL) { |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 204 | return -1; |
| 205 | } |
Christian Heimes | 97cb67b | 2013-07-20 15:01:26 +0200 | [diff] [blame] | 206 | if (PyDict_SetItem(dict, v, v) < 0) { |
| 207 | Py_DECREF(v); |
| 208 | return -1; |
| 209 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | Py_DECREF(v); |
| 211 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 214 | PyObject *o; |
| 215 | iterations++; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | i = PyLong_AS_LONG(v) + 1; |
| 218 | o = PyLong_FromLong(i); |
| 219 | if (o == NULL) |
| 220 | return -1; |
| 221 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 222 | Py_DECREF(o); |
| 223 | return -1; |
| 224 | } |
| 225 | Py_DECREF(o); |
| 226 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | Py_DECREF(dict); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | if (iterations != count) { |
| 231 | PyErr_SetString( |
| 232 | TestError, |
| 233 | "test_dict_iteration: dict iteration went wrong "); |
| 234 | return -1; |
| 235 | } else { |
| 236 | return 0; |
| 237 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 241 | test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | for (i = 0; i < 200; i++) { |
| 246 | if (test_dict_inner(i) < 0) { |
| 247 | return NULL; |
| 248 | } |
| 249 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 250 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 251 | Py_RETURN_NONE; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 254 | static PyObject* |
| 255 | dict_getitem_knownhash(PyObject *self, PyObject *args) |
| 256 | { |
| 257 | PyObject *mp, *key, *result; |
| 258 | Py_ssize_t hash; |
| 259 | |
| 260 | if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash", |
| 261 | &mp, &key, &hash)) { |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash); |
| 266 | if (result == NULL && !PyErr_Occurred()) { |
| 267 | _PyErr_SetKeyError(key); |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | Py_XINCREF(result); |
| 272 | return result; |
| 273 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 274 | |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 275 | static PyObject* |
| 276 | dict_hassplittable(PyObject *self, PyObject *arg) |
| 277 | { |
| 278 | if (!PyDict_Check(arg)) { |
| 279 | PyErr_Format(PyExc_TypeError, |
| 280 | "dict_hassplittable() argument must be dict, not '%s'", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 281 | Py_TYPE(arg)->tp_name); |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg)); |
| 286 | } |
| 287 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 288 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 289 | * PyType_Ready if it hasn't already been called |
| 290 | */ |
| 291 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | PyVarObject_HEAD_INIT(NULL, 0) |
| 293 | "hashinheritancetester", /* Name of this type */ |
| 294 | sizeof(PyObject), /* Basic object size */ |
| 295 | 0, /* Item size for varobject */ |
| 296 | (destructor)PyObject_Del, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 297 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | 0, /* tp_getattr */ |
| 299 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 300 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | 0, /* tp_repr */ |
| 302 | 0, /* tp_as_number */ |
| 303 | 0, /* tp_as_sequence */ |
| 304 | 0, /* tp_as_mapping */ |
| 305 | 0, /* tp_hash */ |
| 306 | 0, /* tp_call */ |
| 307 | 0, /* tp_str */ |
| 308 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 309 | 0, /* tp_setattro */ |
| 310 | 0, /* tp_as_buffer */ |
| 311 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 312 | 0, /* tp_doc */ |
| 313 | 0, /* tp_traverse */ |
| 314 | 0, /* tp_clear */ |
| 315 | 0, /* tp_richcompare */ |
| 316 | 0, /* tp_weaklistoffset */ |
| 317 | 0, /* tp_iter */ |
| 318 | 0, /* tp_iternext */ |
| 319 | 0, /* tp_methods */ |
| 320 | 0, /* tp_members */ |
| 321 | 0, /* tp_getset */ |
| 322 | 0, /* tp_base */ |
| 323 | 0, /* tp_dict */ |
| 324 | 0, /* tp_descr_get */ |
| 325 | 0, /* tp_descr_set */ |
| 326 | 0, /* tp_dictoffset */ |
| 327 | 0, /* tp_init */ |
| 328 | 0, /* tp_alloc */ |
| 329 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 333 | test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | PyTypeObject *type; |
| 336 | PyObject *obj; |
Antoine Pitrou | 29aad00 | 2010-10-23 19:42:38 +0000 | [diff] [blame] | 337 | Py_hash_t hash; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | if (type->tp_dict != NULL) |
| 342 | /* The type has already been initialized. This probably means |
| 343 | -R is being used. */ |
| 344 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 345 | |
| 346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | obj = PyObject_New(PyObject, type); |
| 348 | if (obj == NULL) { |
| 349 | PyErr_Clear(); |
| 350 | PyErr_SetString( |
| 351 | TestError, |
| 352 | "test_lazy_hash_inheritance: failed to create object"); |
| 353 | return NULL; |
| 354 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | if (type->tp_dict != NULL) { |
| 357 | PyErr_SetString( |
| 358 | TestError, |
| 359 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 360 | Py_DECREF(obj); |
| 361 | return NULL; |
| 362 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | hash = PyObject_Hash(obj); |
| 365 | if ((hash == -1) && PyErr_Occurred()) { |
| 366 | PyErr_Clear(); |
| 367 | PyErr_SetString( |
| 368 | TestError, |
| 369 | "test_lazy_hash_inheritance: could not hash object"); |
| 370 | Py_DECREF(obj); |
| 371 | return NULL; |
| 372 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | if (type->tp_dict == NULL) { |
| 375 | PyErr_SetString( |
| 376 | TestError, |
| 377 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 378 | Py_DECREF(obj); |
| 379 | return NULL; |
| 380 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 383 | PyErr_SetString( |
| 384 | TestError, |
| 385 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 386 | Py_DECREF(obj); |
| 387 | return NULL; |
| 388 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | Py_RETURN_NONE; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 396 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 397 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 398 | |
| 399 | Note that the meat of the test is contained in testcapi_long.h. |
| 400 | This is revolting, but delicate code duplication is worse: "almost |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 401 | 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] | 402 | dependence on type names makes it impossible to use a parameterized |
| 403 | function. A giant macro would be even worse than this. A C++ template |
| 404 | would be perfect. |
| 405 | |
| 406 | The "report an error" functions are deliberately not part of the #include |
| 407 | file: if the test fails, you can set a breakpoint in the appropriate |
| 408 | error function directly, and crawl back from there in the debugger. |
| 409 | */ |
| 410 | |
| 411 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 412 | |
| 413 | static PyObject * |
| 414 | raise_test_long_error(const char* msg) |
| 415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | #define TESTNAME test_long_api_inner |
| 420 | #define TYPENAME long |
| 421 | #define F_S_TO_PY PyLong_FromLong |
| 422 | #define F_PY_TO_S PyLong_AsLong |
| 423 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 424 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 425 | |
| 426 | #include "testcapi_long.h" |
| 427 | |
| 428 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 429 | test_long_api(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | #undef TESTNAME |
| 435 | #undef TYPENAME |
| 436 | #undef F_S_TO_PY |
| 437 | #undef F_PY_TO_S |
| 438 | #undef F_U_TO_PY |
| 439 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 440 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 441 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 442 | raise_test_longlong_error(const char* msg) |
| 443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | #define TESTNAME test_longlong_api_inner |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 448 | #define TYPENAME long long |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | #define F_S_TO_PY PyLong_FromLongLong |
| 450 | #define F_PY_TO_S PyLong_AsLongLong |
| 451 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 452 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 453 | |
| 454 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 455 | |
| 456 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 457 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 462 | #undef TESTNAME |
| 463 | #undef TYPENAME |
| 464 | #undef F_S_TO_PY |
| 465 | #undef F_PY_TO_S |
| 466 | #undef F_U_TO_PY |
| 467 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 468 | |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 469 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 470 | is tested by test_long_api_inner. This test will concentrate on proper |
| 471 | handling of overflow. |
| 472 | */ |
| 473 | |
| 474 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 475 | test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | PyObject *num, *one, *temp; |
| 478 | long value; |
| 479 | int overflow; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | /* Test that overflow is set properly for a large value. */ |
| 482 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 483 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 484 | if (num == NULL) |
| 485 | return NULL; |
| 486 | overflow = 1234; |
| 487 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 488 | Py_DECREF(num); |
| 489 | if (value == -1 && PyErr_Occurred()) |
| 490 | return NULL; |
| 491 | if (value != -1) |
| 492 | return raiseTestError("test_long_and_overflow", |
| 493 | "return value was not set to -1"); |
| 494 | if (overflow != 1) |
| 495 | return raiseTestError("test_long_and_overflow", |
| 496 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | /* Same again, with num = LONG_MAX + 1 */ |
| 499 | num = PyLong_FromLong(LONG_MAX); |
| 500 | if (num == NULL) |
| 501 | return NULL; |
| 502 | one = PyLong_FromLong(1L); |
| 503 | if (one == NULL) { |
| 504 | Py_DECREF(num); |
| 505 | return NULL; |
| 506 | } |
| 507 | temp = PyNumber_Add(num, one); |
| 508 | Py_DECREF(one); |
| 509 | Py_DECREF(num); |
| 510 | num = temp; |
| 511 | if (num == NULL) |
| 512 | return NULL; |
| 513 | overflow = 0; |
| 514 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 515 | Py_DECREF(num); |
| 516 | if (value == -1 && PyErr_Occurred()) |
| 517 | return NULL; |
| 518 | if (value != -1) |
| 519 | return raiseTestError("test_long_and_overflow", |
| 520 | "return value was not set to -1"); |
| 521 | if (overflow != 1) |
| 522 | return raiseTestError("test_long_and_overflow", |
| 523 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | /* Test that overflow is set properly for a large negative value. */ |
| 526 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 527 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 528 | if (num == NULL) |
| 529 | return NULL; |
| 530 | overflow = 1234; |
| 531 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 532 | Py_DECREF(num); |
| 533 | if (value == -1 && PyErr_Occurred()) |
| 534 | return NULL; |
| 535 | if (value != -1) |
| 536 | return raiseTestError("test_long_and_overflow", |
| 537 | "return value was not set to -1"); |
| 538 | if (overflow != -1) |
| 539 | return raiseTestError("test_long_and_overflow", |
| 540 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | /* Same again, with num = LONG_MIN - 1 */ |
| 543 | num = PyLong_FromLong(LONG_MIN); |
| 544 | if (num == NULL) |
| 545 | return NULL; |
| 546 | one = PyLong_FromLong(1L); |
| 547 | if (one == NULL) { |
| 548 | Py_DECREF(num); |
| 549 | return NULL; |
| 550 | } |
| 551 | temp = PyNumber_Subtract(num, one); |
| 552 | Py_DECREF(one); |
| 553 | Py_DECREF(num); |
| 554 | num = temp; |
| 555 | if (num == NULL) |
| 556 | return NULL; |
| 557 | overflow = 0; |
| 558 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 559 | Py_DECREF(num); |
| 560 | if (value == -1 && PyErr_Occurred()) |
| 561 | return NULL; |
| 562 | if (value != -1) |
| 563 | return raiseTestError("test_long_and_overflow", |
| 564 | "return value was not set to -1"); |
| 565 | if (overflow != -1) |
| 566 | return raiseTestError("test_long_and_overflow", |
| 567 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | /* Test that overflow is cleared properly for small values. */ |
| 570 | num = PyLong_FromString("FF", NULL, 16); |
| 571 | if (num == NULL) |
| 572 | return NULL; |
| 573 | overflow = 1234; |
| 574 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 575 | Py_DECREF(num); |
| 576 | if (value == -1 && PyErr_Occurred()) |
| 577 | return NULL; |
| 578 | if (value != 0xFF) |
| 579 | return raiseTestError("test_long_and_overflow", |
| 580 | "expected return value 0xFF"); |
| 581 | if (overflow != 0) |
| 582 | return raiseTestError("test_long_and_overflow", |
| 583 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | num = PyLong_FromString("-FF", NULL, 16); |
| 586 | if (num == NULL) |
| 587 | return NULL; |
| 588 | overflow = 0; |
| 589 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 590 | Py_DECREF(num); |
| 591 | if (value == -1 && PyErr_Occurred()) |
| 592 | return NULL; |
| 593 | if (value != -0xFF) |
| 594 | return raiseTestError("test_long_and_overflow", |
| 595 | "expected return value 0xFF"); |
| 596 | if (overflow != 0) |
| 597 | return raiseTestError("test_long_and_overflow", |
| 598 | "overflow was set incorrectly"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | num = PyLong_FromLong(LONG_MAX); |
| 601 | if (num == NULL) |
| 602 | return NULL; |
| 603 | overflow = 1234; |
| 604 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 605 | Py_DECREF(num); |
| 606 | if (value == -1 && PyErr_Occurred()) |
| 607 | return NULL; |
| 608 | if (value != LONG_MAX) |
| 609 | return raiseTestError("test_long_and_overflow", |
| 610 | "expected return value LONG_MAX"); |
| 611 | if (overflow != 0) |
| 612 | return raiseTestError("test_long_and_overflow", |
| 613 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | num = PyLong_FromLong(LONG_MIN); |
| 616 | if (num == NULL) |
| 617 | return NULL; |
| 618 | overflow = 0; |
| 619 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 620 | Py_DECREF(num); |
| 621 | if (value == -1 && PyErr_Occurred()) |
| 622 | return NULL; |
| 623 | if (value != LONG_MIN) |
| 624 | return raiseTestError("test_long_and_overflow", |
| 625 | "expected return value LONG_MIN"); |
| 626 | if (overflow != 0) |
| 627 | return raiseTestError("test_long_and_overflow", |
| 628 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 629 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 630 | Py_RETURN_NONE; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 633 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 634 | long long is tested by test_long_api_inner. This test will |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 635 | concentrate on proper handling of overflow. |
| 636 | */ |
| 637 | |
| 638 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 639 | test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | PyObject *num, *one, *temp; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 642 | long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | int overflow; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | /* Test that overflow is set properly for a large value. */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 646 | /* num is a number larger than LLONG_MAX on a typical machine. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 648 | if (num == NULL) |
| 649 | return NULL; |
| 650 | overflow = 1234; |
| 651 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 652 | Py_DECREF(num); |
| 653 | if (value == -1 && PyErr_Occurred()) |
| 654 | return NULL; |
| 655 | if (value != -1) |
| 656 | return raiseTestError("test_long_long_and_overflow", |
| 657 | "return value was not set to -1"); |
| 658 | if (overflow != 1) |
| 659 | return raiseTestError("test_long_long_and_overflow", |
| 660 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 661 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 662 | /* Same again, with num = LLONG_MAX + 1 */ |
| 663 | num = PyLong_FromLongLong(LLONG_MAX); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | if (num == NULL) |
| 665 | return NULL; |
| 666 | one = PyLong_FromLong(1L); |
| 667 | if (one == NULL) { |
| 668 | Py_DECREF(num); |
| 669 | return NULL; |
| 670 | } |
| 671 | temp = PyNumber_Add(num, one); |
| 672 | Py_DECREF(one); |
| 673 | Py_DECREF(num); |
| 674 | num = temp; |
| 675 | if (num == NULL) |
| 676 | return NULL; |
| 677 | overflow = 0; |
| 678 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 679 | Py_DECREF(num); |
| 680 | if (value == -1 && PyErr_Occurred()) |
| 681 | return NULL; |
| 682 | if (value != -1) |
| 683 | return raiseTestError("test_long_long_and_overflow", |
| 684 | "return value was not set to -1"); |
| 685 | if (overflow != 1) |
| 686 | return raiseTestError("test_long_long_and_overflow", |
| 687 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | /* Test that overflow is set properly for a large negative value. */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 690 | /* num is a number smaller than LLONG_MIN on a typical platform */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 692 | if (num == NULL) |
| 693 | return NULL; |
| 694 | overflow = 1234; |
| 695 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 696 | Py_DECREF(num); |
| 697 | if (value == -1 && PyErr_Occurred()) |
| 698 | return NULL; |
| 699 | if (value != -1) |
| 700 | return raiseTestError("test_long_long_and_overflow", |
| 701 | "return value was not set to -1"); |
| 702 | if (overflow != -1) |
| 703 | return raiseTestError("test_long_long_and_overflow", |
| 704 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 705 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 706 | /* Same again, with num = LLONG_MIN - 1 */ |
| 707 | num = PyLong_FromLongLong(LLONG_MIN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | if (num == NULL) |
| 709 | return NULL; |
| 710 | one = PyLong_FromLong(1L); |
| 711 | if (one == NULL) { |
| 712 | Py_DECREF(num); |
| 713 | return NULL; |
| 714 | } |
| 715 | temp = PyNumber_Subtract(num, one); |
| 716 | Py_DECREF(one); |
| 717 | Py_DECREF(num); |
| 718 | num = temp; |
| 719 | if (num == NULL) |
| 720 | return NULL; |
| 721 | overflow = 0; |
| 722 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 723 | Py_DECREF(num); |
| 724 | if (value == -1 && PyErr_Occurred()) |
| 725 | return NULL; |
| 726 | if (value != -1) |
| 727 | return raiseTestError("test_long_long_and_overflow", |
| 728 | "return value was not set to -1"); |
| 729 | if (overflow != -1) |
| 730 | return raiseTestError("test_long_long_and_overflow", |
| 731 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | /* Test that overflow is cleared properly for small values. */ |
| 734 | num = PyLong_FromString("FF", NULL, 16); |
| 735 | if (num == NULL) |
| 736 | return NULL; |
| 737 | overflow = 1234; |
| 738 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 739 | Py_DECREF(num); |
| 740 | if (value == -1 && PyErr_Occurred()) |
| 741 | return NULL; |
| 742 | if (value != 0xFF) |
| 743 | return raiseTestError("test_long_long_and_overflow", |
| 744 | "expected return value 0xFF"); |
| 745 | if (overflow != 0) |
| 746 | return raiseTestError("test_long_long_and_overflow", |
| 747 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | num = PyLong_FromString("-FF", NULL, 16); |
| 750 | if (num == NULL) |
| 751 | return NULL; |
| 752 | overflow = 0; |
| 753 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 754 | Py_DECREF(num); |
| 755 | if (value == -1 && PyErr_Occurred()) |
| 756 | return NULL; |
| 757 | if (value != -0xFF) |
| 758 | return raiseTestError("test_long_long_and_overflow", |
| 759 | "expected return value 0xFF"); |
| 760 | if (overflow != 0) |
| 761 | return raiseTestError("test_long_long_and_overflow", |
| 762 | "overflow was set incorrectly"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 763 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 764 | num = PyLong_FromLongLong(LLONG_MAX); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | if (num == NULL) |
| 766 | return NULL; |
| 767 | overflow = 1234; |
| 768 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 769 | Py_DECREF(num); |
| 770 | if (value == -1 && PyErr_Occurred()) |
| 771 | return NULL; |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 772 | if (value != LLONG_MAX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | return raiseTestError("test_long_long_and_overflow", |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 774 | "expected return value LLONG_MAX"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | if (overflow != 0) |
| 776 | return raiseTestError("test_long_long_and_overflow", |
| 777 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 778 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 779 | num = PyLong_FromLongLong(LLONG_MIN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | if (num == NULL) |
| 781 | return NULL; |
| 782 | overflow = 0; |
| 783 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 784 | Py_DECREF(num); |
| 785 | if (value == -1 && PyErr_Occurred()) |
| 786 | return NULL; |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 787 | if (value != LLONG_MIN) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | return raiseTestError("test_long_long_and_overflow", |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 789 | "expected return value LLONG_MIN"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | if (overflow != 0) |
| 791 | return raiseTestError("test_long_long_and_overflow", |
| 792 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 793 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 794 | Py_RETURN_NONE; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 797 | /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that |
| 798 | non-integer arguments are handled correctly. It should be extended to |
| 799 | test overflow handling. |
| 800 | */ |
| 801 | |
| 802 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 803 | test_long_as_size_t(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 804 | { |
| 805 | size_t out_u; |
| 806 | Py_ssize_t out_s; |
| 807 | |
| 808 | Py_INCREF(Py_None); |
| 809 | |
| 810 | out_u = PyLong_AsSize_t(Py_None); |
| 811 | if (out_u != (size_t)-1 || !PyErr_Occurred()) |
| 812 | return raiseTestError("test_long_as_size_t", |
| 813 | "PyLong_AsSize_t(None) didn't complain"); |
| 814 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 815 | return raiseTestError("test_long_as_size_t", |
| 816 | "PyLong_AsSize_t(None) raised " |
| 817 | "something other than TypeError"); |
| 818 | PyErr_Clear(); |
| 819 | |
| 820 | out_s = PyLong_AsSsize_t(Py_None); |
| 821 | if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) |
| 822 | return raiseTestError("test_long_as_size_t", |
| 823 | "PyLong_AsSsize_t(None) didn't complain"); |
| 824 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 825 | return raiseTestError("test_long_as_size_t", |
| 826 | "PyLong_AsSsize_t(None) raised " |
| 827 | "something other than TypeError"); |
| 828 | PyErr_Clear(); |
| 829 | |
| 830 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 831 | return Py_None; |
| 832 | } |
| 833 | |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 834 | static PyObject * |
| 835 | test_long_as_unsigned_long_long_mask(PyObject *self, |
| 836 | PyObject *Py_UNUSED(ignored)) |
| 837 | { |
| 838 | unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL); |
| 839 | |
| 840 | if (res != (unsigned long long)-1 || !PyErr_Occurred()) { |
| 841 | return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 842 | "PyLong_AsUnsignedLongLongMask(NULL) didn't " |
| 843 | "complain"); |
| 844 | } |
| 845 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 846 | return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 847 | "PyLong_AsUnsignedLongLongMask(NULL) raised " |
| 848 | "something other than SystemError"); |
| 849 | } |
| 850 | PyErr_Clear(); |
| 851 | Py_RETURN_NONE; |
| 852 | } |
| 853 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 854 | /* Test the PyLong_AsDouble API. At present this just tests that |
| 855 | non-integer arguments are handled correctly. |
| 856 | */ |
| 857 | |
| 858 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 859 | test_long_as_double(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 860 | { |
| 861 | double out; |
| 862 | |
| 863 | Py_INCREF(Py_None); |
| 864 | |
| 865 | out = PyLong_AsDouble(Py_None); |
| 866 | if (out != -1.0 || !PyErr_Occurred()) |
| 867 | return raiseTestError("test_long_as_double", |
| 868 | "PyLong_AsDouble(None) didn't complain"); |
| 869 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 870 | return raiseTestError("test_long_as_double", |
| 871 | "PyLong_AsDouble(None) raised " |
| 872 | "something other than TypeError"); |
| 873 | PyErr_Clear(); |
| 874 | |
| 875 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 876 | return Py_None; |
| 877 | } |
| 878 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 879 | /* 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] | 880 | for both long and int arguments. The test may leak a little memory if |
| 881 | it fails. |
| 882 | */ |
| 883 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 884 | test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 885 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | PyObject *tuple, *num; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 887 | long long value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | tuple = PyTuple_New(1); |
| 890 | if (tuple == NULL) |
| 891 | return NULL; |
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 | num = PyLong_FromLong(42); |
| 894 | if (num == NULL) |
| 895 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | value = -1; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 900 | if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 902 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | if (value != 42) |
| 904 | return raiseTestError("test_L_code", |
| 905 | "L code returned wrong value for long 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | Py_DECREF(num); |
| 908 | num = PyLong_FromLong(42); |
| 909 | if (num == NULL) |
| 910 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | value = -1; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 915 | if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 917 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | if (value != 42) |
| 919 | return raiseTestError("test_L_code", |
| 920 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 923 | Py_RETURN_NONE; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 926 | static PyObject * |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 927 | return_none(void *unused) |
| 928 | { |
| 929 | Py_RETURN_NONE; |
| 930 | } |
| 931 | |
| 932 | static PyObject * |
| 933 | raise_error(void *unused) |
| 934 | { |
| 935 | PyErr_SetNone(PyExc_ValueError); |
| 936 | return NULL; |
| 937 | } |
| 938 | |
| 939 | static int |
| 940 | test_buildvalue_N_error(const char *fmt) |
| 941 | { |
| 942 | PyObject *arg, *res; |
| 943 | |
| 944 | arg = PyList_New(0); |
| 945 | if (arg == NULL) { |
| 946 | return -1; |
| 947 | } |
| 948 | |
| 949 | Py_INCREF(arg); |
| 950 | res = Py_BuildValue(fmt, return_none, NULL, arg); |
| 951 | if (res == NULL) { |
| 952 | return -1; |
| 953 | } |
| 954 | Py_DECREF(res); |
| 955 | if (Py_REFCNT(arg) != 1) { |
| 956 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 957 | "arg was not decrefed in successful " |
| 958 | "Py_BuildValue(\"%s\")", fmt); |
| 959 | return -1; |
| 960 | } |
| 961 | |
| 962 | Py_INCREF(arg); |
| 963 | res = Py_BuildValue(fmt, raise_error, NULL, arg); |
| 964 | if (res != NULL || !PyErr_Occurred()) { |
| 965 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 966 | "Py_BuildValue(\"%s\") didn't complain", fmt); |
| 967 | return -1; |
| 968 | } |
| 969 | PyErr_Clear(); |
| 970 | if (Py_REFCNT(arg) != 1) { |
| 971 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 972 | "arg was not decrefed in failed " |
| 973 | "Py_BuildValue(\"%s\")", fmt); |
| 974 | return -1; |
| 975 | } |
| 976 | Py_DECREF(arg); |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 981 | test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 982 | { |
| 983 | PyObject *arg, *res; |
| 984 | |
| 985 | arg = PyList_New(0); |
| 986 | if (arg == NULL) { |
| 987 | return NULL; |
| 988 | } |
| 989 | Py_INCREF(arg); |
| 990 | res = Py_BuildValue("N", arg); |
| 991 | if (res == NULL) { |
| 992 | return NULL; |
| 993 | } |
| 994 | if (res != arg) { |
| 995 | return raiseTestError("test_buildvalue_N", |
| 996 | "Py_BuildValue(\"N\") returned wrong result"); |
| 997 | } |
| 998 | if (Py_REFCNT(arg) != 2) { |
| 999 | return raiseTestError("test_buildvalue_N", |
| 1000 | "arg was not decrefed in Py_BuildValue(\"N\")"); |
| 1001 | } |
| 1002 | Py_DECREF(res); |
| 1003 | Py_DECREF(arg); |
| 1004 | |
| 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 | if (test_buildvalue_N_error("[O&N]") < 0) |
| 1010 | return NULL; |
| 1011 | if (test_buildvalue_N_error("{O&N}") < 0) |
| 1012 | return NULL; |
| 1013 | if (test_buildvalue_N_error("{()O&(())N}") < 0) |
| 1014 | return NULL; |
| 1015 | |
| 1016 | Py_RETURN_NONE; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | static PyObject * |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 1021 | get_args(PyObject *self, PyObject *args) |
| 1022 | { |
| 1023 | if (args == NULL) { |
| 1024 | args = Py_None; |
| 1025 | } |
| 1026 | Py_INCREF(args); |
| 1027 | return args; |
| 1028 | } |
| 1029 | |
| 1030 | static PyObject * |
| 1031 | get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1032 | { |
| 1033 | if (kwargs == NULL) { |
| 1034 | kwargs = Py_None; |
| 1035 | } |
| 1036 | Py_INCREF(kwargs); |
| 1037 | return kwargs; |
| 1038 | } |
| 1039 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1040 | /* Test tuple argument processing */ |
| 1041 | static PyObject * |
| 1042 | getargs_tuple(PyObject *self, PyObject *args) |
| 1043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | int a, b, c; |
| 1045 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 1046 | return NULL; |
| 1047 | return Py_BuildValue("iii", a, b, c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1050 | /* test PyArg_ParseTupleAndKeywords */ |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1051 | static PyObject * |
| 1052 | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1055 | static const char fmt[] = "(ii)i|(i(ii))(iii)i"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | 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] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 1059 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 1060 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 1061 | return NULL; |
| 1062 | return Py_BuildValue("iiiiiiiiii", |
| 1063 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 1064 | 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] | 1065 | } |
| 1066 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1067 | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
| 1068 | static PyObject * |
| 1069 | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1070 | { |
| 1071 | static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
| 1072 | int required = -1; |
| 1073 | int optional = -1; |
| 1074 | int keyword_only = -1; |
| 1075 | |
| 1076 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
| 1077 | &required, &optional, &keyword_only)) |
| 1078 | return NULL; |
| 1079 | return Py_BuildValue("iii", required, optional, keyword_only); |
| 1080 | } |
| 1081 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1082 | /* test PyArg_ParseTupleAndKeywords positional-only arguments */ |
| 1083 | static PyObject * |
| 1084 | getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1085 | { |
| 1086 | static char *keywords[] = {"", "", "keyword", NULL}; |
| 1087 | int required = -1; |
| 1088 | int optional = -1; |
| 1089 | int keyword = -1; |
| 1090 | |
| 1091 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords, |
| 1092 | &required, &optional, &keyword)) |
| 1093 | return NULL; |
| 1094 | return Py_BuildValue("iii", required, optional, keyword); |
| 1095 | } |
| 1096 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1097 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 1098 | and return the result. |
| 1099 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1100 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1101 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | unsigned char value; |
| 1104 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 1105 | return NULL; |
| 1106 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1109 | static PyObject * |
| 1110 | getargs_B(PyObject *self, PyObject *args) |
| 1111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | unsigned char value; |
| 1113 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 1114 | return NULL; |
| 1115 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1119 | getargs_h(PyObject *self, PyObject *args) |
| 1120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | short value; |
| 1122 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 1123 | return NULL; |
| 1124 | return PyLong_FromLong((long)value); |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1128 | getargs_H(PyObject *self, PyObject *args) |
| 1129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | unsigned short value; |
| 1131 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 1132 | return NULL; |
| 1133 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | static PyObject * |
| 1137 | getargs_I(PyObject *self, PyObject *args) |
| 1138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | unsigned int value; |
| 1140 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 1141 | return NULL; |
| 1142 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | static PyObject * |
| 1146 | getargs_k(PyObject *self, PyObject *args) |
| 1147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | unsigned long value; |
| 1149 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 1150 | return NULL; |
| 1151 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | static PyObject * |
| 1155 | getargs_i(PyObject *self, PyObject *args) |
| 1156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | int value; |
| 1158 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 1159 | return NULL; |
| 1160 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1163 | static PyObject * |
| 1164 | getargs_l(PyObject *self, PyObject *args) |
| 1165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | long value; |
| 1167 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 1168 | return NULL; |
| 1169 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1172 | static PyObject * |
| 1173 | getargs_n(PyObject *self, PyObject *args) |
| 1174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | Py_ssize_t value; |
| 1176 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 1177 | return NULL; |
| 1178 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 1181 | static PyObject * |
| 1182 | getargs_p(PyObject *self, PyObject *args) |
| 1183 | { |
| 1184 | int value; |
| 1185 | if (!PyArg_ParseTuple(args, "p", &value)) |
| 1186 | return NULL; |
| 1187 | return PyLong_FromLong(value); |
| 1188 | } |
| 1189 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1190 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1191 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1192 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1193 | long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 1195 | return NULL; |
| 1196 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1199 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1200 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1201 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1202 | unsigned long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 1204 | return NULL; |
| 1205 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1206 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1207 | |
| 1208 | /* This function not only tests the 'k' getargs code, but also the |
orenmn | 698845e | 2017-03-02 13:29:20 +0200 | [diff] [blame] | 1209 | PyLong_AsUnsignedLongMask() function. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1210 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1211 | test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | PyObject *tuple, *num; |
| 1214 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | tuple = PyTuple_New(1); |
| 1217 | if (tuple == NULL) |
| 1218 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 1221 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 1222 | if (num == NULL) |
| 1223 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | value = PyLong_AsUnsignedLongMask(num); |
| 1226 | if (value != ULONG_MAX) |
| 1227 | return raiseTestError("test_k_code", |
Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 1228 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1233 | if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1235 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | if (value != ULONG_MAX) |
| 1237 | return raiseTestError("test_k_code", |
| 1238 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | Py_DECREF(num); |
| 1241 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 1242 | if (num == NULL) |
| 1243 | return NULL; |
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 | value = PyLong_AsUnsignedLongMask(num); |
| 1246 | if (value != (unsigned long)-0x42) |
| 1247 | return raiseTestError("test_k_code", |
orenmn | 698845e | 2017-03-02 13:29:20 +0200 | [diff] [blame] | 1248 | "PyLong_AsUnsignedLongMask() returned wrong " |
| 1249 | "value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1254 | if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1256 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | if (value != (unsigned long)-0x42) |
| 1258 | return raiseTestError("test_k_code", |
| 1259 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1262 | Py_RETURN_NONE; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1265 | static PyObject * |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 1266 | getargs_f(PyObject *self, PyObject *args) |
| 1267 | { |
| 1268 | float f; |
| 1269 | if (!PyArg_ParseTuple(args, "f", &f)) |
| 1270 | return NULL; |
| 1271 | return PyFloat_FromDouble(f); |
| 1272 | } |
| 1273 | |
| 1274 | static PyObject * |
| 1275 | getargs_d(PyObject *self, PyObject *args) |
| 1276 | { |
| 1277 | double d; |
| 1278 | if (!PyArg_ParseTuple(args, "d", &d)) |
| 1279 | return NULL; |
| 1280 | return PyFloat_FromDouble(d); |
| 1281 | } |
| 1282 | |
| 1283 | static PyObject * |
| 1284 | getargs_D(PyObject *self, PyObject *args) |
| 1285 | { |
| 1286 | Py_complex cval; |
| 1287 | if (!PyArg_ParseTuple(args, "D", &cval)) |
| 1288 | return NULL; |
| 1289 | return PyComplex_FromCComplex(cval); |
| 1290 | } |
| 1291 | |
| 1292 | static PyObject * |
| 1293 | getargs_S(PyObject *self, PyObject *args) |
| 1294 | { |
| 1295 | PyObject *obj; |
| 1296 | if (!PyArg_ParseTuple(args, "S", &obj)) |
| 1297 | return NULL; |
| 1298 | Py_INCREF(obj); |
| 1299 | return obj; |
| 1300 | } |
| 1301 | |
| 1302 | static PyObject * |
| 1303 | getargs_Y(PyObject *self, PyObject *args) |
| 1304 | { |
| 1305 | PyObject *obj; |
| 1306 | if (!PyArg_ParseTuple(args, "Y", &obj)) |
| 1307 | return NULL; |
| 1308 | Py_INCREF(obj); |
| 1309 | return obj; |
| 1310 | } |
| 1311 | |
| 1312 | static PyObject * |
| 1313 | getargs_U(PyObject *self, PyObject *args) |
| 1314 | { |
| 1315 | PyObject *obj; |
| 1316 | if (!PyArg_ParseTuple(args, "U", &obj)) |
| 1317 | return NULL; |
| 1318 | Py_INCREF(obj); |
| 1319 | return obj; |
| 1320 | } |
| 1321 | |
| 1322 | static PyObject * |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1323 | getargs_c(PyObject *self, PyObject *args) |
| 1324 | { |
| 1325 | char c; |
| 1326 | if (!PyArg_ParseTuple(args, "c", &c)) |
| 1327 | return NULL; |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1328 | return PyLong_FromLong((unsigned char)c); |
| 1329 | } |
| 1330 | |
| 1331 | static PyObject * |
| 1332 | getargs_C(PyObject *self, PyObject *args) |
| 1333 | { |
| 1334 | int c; |
| 1335 | if (!PyArg_ParseTuple(args, "C", &c)) |
| 1336 | return NULL; |
| 1337 | return PyLong_FromLong(c); |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | static PyObject * |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1341 | getargs_s(PyObject *self, PyObject *args) |
| 1342 | { |
| 1343 | char *str; |
| 1344 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1345 | return NULL; |
| 1346 | return PyBytes_FromString(str); |
| 1347 | } |
| 1348 | |
| 1349 | static PyObject * |
| 1350 | getargs_s_star(PyObject *self, PyObject *args) |
| 1351 | { |
| 1352 | Py_buffer buffer; |
| 1353 | PyObject *bytes; |
| 1354 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1355 | return NULL; |
| 1356 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1357 | PyBuffer_Release(&buffer); |
| 1358 | return bytes; |
| 1359 | } |
| 1360 | |
| 1361 | static PyObject * |
| 1362 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1363 | { |
| 1364 | char *str; |
| 1365 | Py_ssize_t size; |
| 1366 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1367 | return NULL; |
| 1368 | return PyBytes_FromStringAndSize(str, size); |
| 1369 | } |
| 1370 | |
| 1371 | static PyObject * |
| 1372 | getargs_z(PyObject *self, PyObject *args) |
| 1373 | { |
| 1374 | char *str; |
| 1375 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1376 | return NULL; |
| 1377 | if (str != NULL) |
| 1378 | return PyBytes_FromString(str); |
| 1379 | else |
| 1380 | Py_RETURN_NONE; |
| 1381 | } |
| 1382 | |
| 1383 | static PyObject * |
| 1384 | getargs_z_star(PyObject *self, PyObject *args) |
| 1385 | { |
| 1386 | Py_buffer buffer; |
| 1387 | PyObject *bytes; |
| 1388 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1389 | return NULL; |
| 1390 | if (buffer.buf != NULL) |
| 1391 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1392 | else { |
| 1393 | Py_INCREF(Py_None); |
| 1394 | bytes = Py_None; |
| 1395 | } |
| 1396 | PyBuffer_Release(&buffer); |
| 1397 | return bytes; |
| 1398 | } |
| 1399 | |
| 1400 | static PyObject * |
| 1401 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1402 | { |
| 1403 | char *str; |
| 1404 | Py_ssize_t size; |
| 1405 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1406 | return NULL; |
| 1407 | if (str != NULL) |
| 1408 | return PyBytes_FromStringAndSize(str, size); |
| 1409 | else |
| 1410 | Py_RETURN_NONE; |
| 1411 | } |
| 1412 | |
| 1413 | static PyObject * |
| 1414 | getargs_y(PyObject *self, PyObject *args) |
| 1415 | { |
| 1416 | char *str; |
| 1417 | if (!PyArg_ParseTuple(args, "y", &str)) |
| 1418 | return NULL; |
| 1419 | return PyBytes_FromString(str); |
| 1420 | } |
| 1421 | |
| 1422 | static PyObject * |
| 1423 | getargs_y_star(PyObject *self, PyObject *args) |
| 1424 | { |
| 1425 | Py_buffer buffer; |
| 1426 | PyObject *bytes; |
| 1427 | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 1428 | return NULL; |
| 1429 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1430 | PyBuffer_Release(&buffer); |
| 1431 | return bytes; |
| 1432 | } |
| 1433 | |
| 1434 | static PyObject * |
| 1435 | getargs_y_hash(PyObject *self, PyObject *args) |
| 1436 | { |
| 1437 | char *str; |
| 1438 | Py_ssize_t size; |
| 1439 | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
| 1440 | return NULL; |
| 1441 | return PyBytes_FromStringAndSize(str, size); |
| 1442 | } |
| 1443 | |
| 1444 | static PyObject * |
| 1445 | getargs_u(PyObject *self, PyObject *args) |
| 1446 | { |
| 1447 | Py_UNICODE *str; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1448 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1449 | return NULL; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1450 | return PyUnicode_FromWideChar(str, -1); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | static PyObject * |
| 1454 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1455 | { |
| 1456 | Py_UNICODE *str; |
| 1457 | Py_ssize_t size; |
| 1458 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1459 | return NULL; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1460 | return PyUnicode_FromWideChar(str, size); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static PyObject * |
| 1464 | getargs_Z(PyObject *self, PyObject *args) |
| 1465 | { |
| 1466 | Py_UNICODE *str; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1467 | if (!PyArg_ParseTuple(args, "Z", &str)) |
| 1468 | return NULL; |
| 1469 | if (str != NULL) { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1470 | return PyUnicode_FromWideChar(str, -1); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1471 | } else |
| 1472 | Py_RETURN_NONE; |
| 1473 | } |
| 1474 | |
| 1475 | static PyObject * |
| 1476 | getargs_Z_hash(PyObject *self, PyObject *args) |
| 1477 | { |
| 1478 | Py_UNICODE *str; |
| 1479 | Py_ssize_t size; |
| 1480 | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
| 1481 | return NULL; |
| 1482 | if (str != NULL) |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1483 | return PyUnicode_FromWideChar(str, size); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1484 | else |
| 1485 | Py_RETURN_NONE; |
| 1486 | } |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1487 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1488 | static PyObject * |
| 1489 | getargs_es(PyObject *self, PyObject *args) |
| 1490 | { |
| 1491 | PyObject *arg, *result; |
| 1492 | const char *encoding = NULL; |
| 1493 | char *str; |
| 1494 | |
| 1495 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1496 | return NULL; |
| 1497 | if (!PyArg_Parse(arg, "es", encoding, &str)) |
| 1498 | return NULL; |
| 1499 | result = PyBytes_FromString(str); |
| 1500 | PyMem_Free(str); |
| 1501 | return result; |
| 1502 | } |
| 1503 | |
| 1504 | static PyObject * |
| 1505 | getargs_et(PyObject *self, PyObject *args) |
| 1506 | { |
| 1507 | PyObject *arg, *result; |
| 1508 | const char *encoding = NULL; |
| 1509 | char *str; |
| 1510 | |
| 1511 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1512 | return NULL; |
| 1513 | if (!PyArg_Parse(arg, "et", encoding, &str)) |
| 1514 | return NULL; |
| 1515 | result = PyBytes_FromString(str); |
| 1516 | PyMem_Free(str); |
| 1517 | return result; |
| 1518 | } |
| 1519 | |
| 1520 | static PyObject * |
| 1521 | getargs_es_hash(PyObject *self, PyObject *args) |
| 1522 | { |
| 1523 | PyObject *arg, *result; |
| 1524 | const char *encoding = NULL; |
| 1525 | PyByteArrayObject *buffer = NULL; |
| 1526 | char *str = NULL; |
| 1527 | Py_ssize_t size; |
| 1528 | |
| 1529 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1530 | return NULL; |
| 1531 | if (buffer != NULL) { |
| 1532 | str = PyByteArray_AS_STRING(buffer); |
| 1533 | size = PyByteArray_GET_SIZE(buffer); |
| 1534 | } |
| 1535 | if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) |
| 1536 | return NULL; |
| 1537 | result = PyBytes_FromStringAndSize(str, size); |
| 1538 | if (buffer == NULL) |
| 1539 | PyMem_Free(str); |
| 1540 | return result; |
| 1541 | } |
| 1542 | |
| 1543 | static PyObject * |
| 1544 | getargs_et_hash(PyObject *self, PyObject *args) |
| 1545 | { |
| 1546 | PyObject *arg, *result; |
| 1547 | const char *encoding = NULL; |
| 1548 | PyByteArrayObject *buffer = NULL; |
| 1549 | char *str = NULL; |
| 1550 | Py_ssize_t size; |
| 1551 | |
| 1552 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1553 | return NULL; |
| 1554 | if (buffer != NULL) { |
| 1555 | str = PyByteArray_AS_STRING(buffer); |
| 1556 | size = PyByteArray_GET_SIZE(buffer); |
| 1557 | } |
| 1558 | if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) |
| 1559 | return NULL; |
| 1560 | result = PyBytes_FromStringAndSize(str, size); |
| 1561 | if (buffer == NULL) |
| 1562 | PyMem_Free(str); |
| 1563 | return result; |
| 1564 | } |
| 1565 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1566 | /* Test the s and z codes for PyArg_ParseTuple. |
| 1567 | */ |
| 1568 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1569 | test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1570 | { |
| 1571 | /* Unicode strings should be accepted */ |
| 1572 | PyObject *tuple, *obj; |
| 1573 | char *value; |
| 1574 | |
| 1575 | tuple = PyTuple_New(1); |
| 1576 | if (tuple == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1578 | |
| 1579 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1581 | if (obj == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1583 | |
| 1584 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1585 | |
| 1586 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1588 | */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1589 | if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) { |
| 1590 | return NULL; |
| 1591 | } |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1592 | |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1593 | if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) { |
| 1594 | return NULL; |
| 1595 | } |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1596 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1597 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1598 | Py_RETURN_NONE; |
| 1599 | } |
| 1600 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1601 | static PyObject * |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1602 | parse_tuple_and_keywords(PyObject *self, PyObject *args) |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1603 | { |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1604 | PyObject *sub_args; |
| 1605 | PyObject *sub_kwargs; |
Serhiy Storchaka | 5f161fd | 2017-05-04 00:03:23 +0300 | [diff] [blame] | 1606 | const char *sub_format; |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1607 | PyObject *sub_keywords; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1608 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1609 | Py_ssize_t i, size; |
| 1610 | char *keywords[8 + 1]; /* space for NULL at end */ |
| 1611 | PyObject *o; |
| 1612 | PyObject *converted[8]; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1613 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1614 | int result; |
| 1615 | PyObject *return_value = NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1616 | |
Larry Hastings | 22701e8 | 2012-08-08 14:52:22 -0700 | [diff] [blame] | 1617 | double buffers[8][4]; /* double ensures alignment where necessary */ |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1618 | |
Serhiy Storchaka | 5f161fd | 2017-05-04 00:03:23 +0300 | [diff] [blame] | 1619 | if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords", |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1620 | &sub_args, &sub_kwargs, |
| 1621 | &sub_format, &sub_keywords)) |
| 1622 | return NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1623 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1624 | if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
| 1625 | PyErr_SetString(PyExc_ValueError, |
| 1626 | "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
| 1627 | return NULL; |
| 1628 | } |
| 1629 | |
| 1630 | memset(buffers, 0, sizeof(buffers)); |
| 1631 | memset(converted, 0, sizeof(converted)); |
| 1632 | memset(keywords, 0, sizeof(keywords)); |
| 1633 | |
| 1634 | size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 1635 | if (size > 8) { |
| 1636 | PyErr_SetString(PyExc_ValueError, |
| 1637 | "parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 1638 | goto exit; |
| 1639 | } |
| 1640 | |
| 1641 | for (i = 0; i < size; i++) { |
| 1642 | o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 1643 | if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { |
| 1644 | PyErr_Format(PyExc_ValueError, |
Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 1645 | "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] | 1646 | goto exit; |
| 1647 | } |
| 1648 | keywords[i] = PyBytes_AS_STRING(converted[i]); |
| 1649 | } |
| 1650 | |
| 1651 | result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 1652 | sub_format, keywords, |
| 1653 | buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 1654 | buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 1655 | |
| 1656 | if (result) { |
| 1657 | return_value = Py_None; |
| 1658 | Py_INCREF(Py_None); |
| 1659 | } |
| 1660 | |
| 1661 | exit: |
| 1662 | size = sizeof(converted) / sizeof(converted[0]); |
| 1663 | for (i = 0; i < size; i++) { |
| 1664 | Py_XDECREF(converted[i]); |
| 1665 | } |
| 1666 | return return_value; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1669 | static volatile int x; |
| 1670 | |
Inada Naoki | 2c4928d | 2020-06-17 20:09:44 +0900 | [diff] [blame] | 1671 | /* Ignore use of deprecated APIs */ |
| 1672 | _Py_COMP_DIAG_PUSH |
| 1673 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 1674 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1675 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1676 | of an error. |
| 1677 | */ |
| 1678 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1679 | test_u_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1680 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1681 | PyObject *tuple, *obj; |
| 1682 | Py_UNICODE *value; |
| 1683 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1686 | /* Just use the macro and check that it compiles */ |
| 1687 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | tuple = PyTuple_New(1); |
| 1690 | if (tuple == NULL) |
| 1691 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1694 | "ascii", NULL); |
| 1695 | if (obj == NULL) |
| 1696 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1698 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1701 | if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1703 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1705 | return raiseTestError("test_u_code", |
| 1706 | "u code returned wrong value for u'test'"); |
| 1707 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1708 | if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1710 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1711 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1712 | len != PyUnicode_GET_SIZE(obj)) |
| 1713 | return raiseTestError("test_u_code", |
| 1714 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1717 | Py_RETURN_NONE; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1720 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1721 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1722 | test_Z_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1723 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | PyObject *tuple, *obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1725 | const Py_UNICODE *value1, *value2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | tuple = PyTuple_New(2); |
| 1729 | if (tuple == NULL) |
| 1730 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1732 | obj = PyUnicode_FromString("test"); |
| 1733 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1734 | Py_INCREF(Py_None); |
| 1735 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | /* swap values on purpose */ |
| 1738 | value1 = NULL; |
| 1739 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | /* Test Z for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1742 | if (!PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1744 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1746 | return raiseTestError("test_Z_code", |
| 1747 | "Z code returned wrong value for 'test'"); |
| 1748 | if (value2 != NULL) |
| 1749 | return raiseTestError("test_Z_code", |
| 1750 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | value1 = NULL; |
| 1753 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1754 | len1 = -1; |
| 1755 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | /* Test Z# for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1758 | if (!PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1759 | &value2, &len2)) |
| 1760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1762 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1764 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1765 | return raiseTestError("test_Z_code", |
| 1766 | "Z# code returned wrong values for 'test'"); |
| 1767 | if (value2 != NULL || |
| 1768 | len2 != 0) |
| 1769 | return raiseTestError("test_Z_code", |
| 1770 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | Py_DECREF(tuple); |
| 1773 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1776 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1777 | test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1778 | { |
| 1779 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1781 | size_t wtextlen = 1; |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1782 | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1783 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1785 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1786 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1790 | if (wide == NULL) |
| 1791 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1794 | if (utf8 == NULL) { |
| 1795 | Py_DECREF(wide); |
| 1796 | return NULL; |
| 1797 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1798 | |
Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1799 | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1800 | Py_DECREF(wide); |
| 1801 | Py_DECREF(utf8); |
| 1802 | return raiseTestError("test_widechar", |
| 1803 | "wide string and utf8 string " |
| 1804 | "have different length"); |
| 1805 | } |
| 1806 | if (PyUnicode_Compare(wide, utf8)) { |
| 1807 | Py_DECREF(wide); |
| 1808 | Py_DECREF(utf8); |
| 1809 | if (PyErr_Occurred()) |
| 1810 | return NULL; |
| 1811 | return raiseTestError("test_widechar", |
| 1812 | "wide string and utf8 string " |
| 1813 | "are different"); |
| 1814 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | Py_DECREF(wide); |
| 1817 | Py_DECREF(utf8); |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1818 | |
| 1819 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1820 | wide = PyUnicode_FromWideChar(invalid, 1); |
| 1821 | if (wide == NULL) |
| 1822 | PyErr_Clear(); |
| 1823 | else |
| 1824 | return raiseTestError("test_widechar", |
| 1825 | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
| 1826 | |
| 1827 | wide = PyUnicode_FromUnicode(invalid, 1); |
| 1828 | if (wide == NULL) |
| 1829 | PyErr_Clear(); |
| 1830 | else |
| 1831 | return raiseTestError("test_widechar", |
| 1832 | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1833 | |
| 1834 | wide = PyUnicode_FromUnicode(NULL, 1); |
| 1835 | if (wide == NULL) |
| 1836 | return NULL; |
| 1837 | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1838 | if (_PyUnicode_Ready(wide) < 0) { |
| 1839 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1840 | PyErr_Clear(); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1841 | } |
| 1842 | else { |
| 1843 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1844 | return raiseTestError("test_widechar", |
| 1845 | "PyUnicode_Ready() didn't fail"); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1846 | } |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1847 | #endif |
| 1848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1850 | } |
Inada Naoki | 2c4928d | 2020-06-17 20:09:44 +0900 | [diff] [blame] | 1851 | _Py_COMP_DIAG_POP |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1852 | |
| 1853 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1854 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1855 | { |
| 1856 | PyObject *unicode, *result; |
| 1857 | Py_ssize_t buflen, size; |
| 1858 | wchar_t *buffer; |
| 1859 | |
| 1860 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1861 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1862 | buffer = PyMem_New(wchar_t, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1863 | if (buffer == NULL) |
| 1864 | return PyErr_NoMemory(); |
| 1865 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1866 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1867 | if (size == -1) { |
| 1868 | PyMem_Free(buffer); |
| 1869 | return NULL; |
| 1870 | } |
| 1871 | |
| 1872 | if (size < buflen) |
| 1873 | buflen = size + 1; |
| 1874 | else |
| 1875 | buflen = size; |
| 1876 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 1877 | PyMem_Free(buffer); |
| 1878 | if (result == NULL) |
| 1879 | return NULL; |
| 1880 | |
| 1881 | return Py_BuildValue("(Nn)", result, size); |
| 1882 | } |
| 1883 | |
| 1884 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1885 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1886 | { |
| 1887 | PyObject *unicode, *result; |
| 1888 | Py_ssize_t size; |
| 1889 | wchar_t *buffer; |
| 1890 | |
| 1891 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 1892 | return NULL; |
| 1893 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1894 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1895 | if (buffer == NULL) |
| 1896 | return NULL; |
| 1897 | |
| 1898 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 1899 | PyMem_Free(buffer); |
| 1900 | if (result == NULL) |
| 1901 | return NULL; |
| 1902 | return Py_BuildValue("(Nn)", result, size); |
| 1903 | } |
| 1904 | |
| 1905 | static PyObject * |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 1906 | unicode_asucs4(PyObject *self, PyObject *args) |
| 1907 | { |
| 1908 | PyObject *unicode, *result; |
| 1909 | Py_UCS4 *buffer; |
| 1910 | int copy_null; |
| 1911 | Py_ssize_t str_len, buf_len; |
| 1912 | |
| 1913 | if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, ©_null)) { |
| 1914 | return NULL; |
| 1915 | } |
| 1916 | |
| 1917 | buf_len = str_len + 1; |
| 1918 | buffer = PyMem_NEW(Py_UCS4, buf_len); |
| 1919 | if (buffer == NULL) { |
| 1920 | return PyErr_NoMemory(); |
| 1921 | } |
| 1922 | memset(buffer, 0, sizeof(Py_UCS4)*buf_len); |
| 1923 | buffer[str_len] = 0xffffU; |
| 1924 | |
| 1925 | if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) { |
| 1926 | PyMem_FREE(buffer); |
| 1927 | return NULL; |
| 1928 | } |
| 1929 | |
| 1930 | result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len); |
| 1931 | PyMem_FREE(buffer); |
| 1932 | return result; |
| 1933 | } |
| 1934 | |
| 1935 | static PyObject * |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 1936 | unicode_asutf8(PyObject *self, PyObject *args) |
| 1937 | { |
| 1938 | PyObject *unicode; |
| 1939 | const char *buffer; |
| 1940 | |
| 1941 | if (!PyArg_ParseTuple(args, "U", &unicode)) { |
| 1942 | return NULL; |
| 1943 | } |
| 1944 | |
| 1945 | buffer = PyUnicode_AsUTF8(unicode); |
| 1946 | if (buffer == NULL) { |
| 1947 | return NULL; |
| 1948 | } |
| 1949 | |
| 1950 | return PyBytes_FromString(buffer); |
| 1951 | } |
| 1952 | |
| 1953 | static PyObject * |
| 1954 | unicode_asutf8andsize(PyObject *self, PyObject *args) |
| 1955 | { |
| 1956 | PyObject *unicode, *result; |
| 1957 | const char *buffer; |
| 1958 | Py_ssize_t utf8_len; |
| 1959 | |
| 1960 | if(!PyArg_ParseTuple(args, "U", &unicode)) { |
| 1961 | return NULL; |
| 1962 | } |
| 1963 | |
Victor Stinner | aca8c40 | 2019-09-30 21:14:26 +0200 | [diff] [blame] | 1964 | buffer = PyUnicode_AsUTF8AndSize(unicode, &utf8_len); |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 1965 | if (buffer == NULL) { |
| 1966 | return NULL; |
| 1967 | } |
| 1968 | |
| 1969 | result = PyBytes_FromString(buffer); |
| 1970 | if (result == NULL) { |
| 1971 | return NULL; |
| 1972 | } |
| 1973 | |
| 1974 | return Py_BuildValue("(Nn)", result, utf8_len); |
| 1975 | } |
| 1976 | |
| 1977 | static PyObject * |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 1978 | unicode_findchar(PyObject *self, PyObject *args) |
| 1979 | { |
| 1980 | PyObject *str; |
| 1981 | int direction; |
| 1982 | unsigned int ch; |
| 1983 | Py_ssize_t result; |
| 1984 | Py_ssize_t start, end; |
| 1985 | |
| 1986 | if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch, |
| 1987 | &start, &end, &direction)) { |
| 1988 | return NULL; |
| 1989 | } |
| 1990 | |
| 1991 | result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction); |
| 1992 | if (result == -2) |
| 1993 | return NULL; |
| 1994 | else |
| 1995 | return PyLong_FromSsize_t(result); |
| 1996 | } |
| 1997 | |
| 1998 | static PyObject * |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 1999 | unicode_copycharacters(PyObject *self, PyObject *args) |
| 2000 | { |
| 2001 | PyObject *from, *to, *to_copy; |
| 2002 | Py_ssize_t from_start, to_start, how_many, copied; |
| 2003 | |
| 2004 | if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start, |
| 2005 | &from, &from_start, &how_many)) { |
| 2006 | return NULL; |
| 2007 | } |
| 2008 | |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 2009 | if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to), |
| 2010 | PyUnicode_MAX_CHAR_VALUE(to)))) { |
| 2011 | return NULL; |
| 2012 | } |
| 2013 | if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) { |
| 2014 | Py_DECREF(to_copy); |
| 2015 | return NULL; |
| 2016 | } |
| 2017 | |
| 2018 | if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from, |
| 2019 | from_start, how_many)) < 0) { |
| 2020 | Py_DECREF(to_copy); |
| 2021 | return NULL; |
| 2022 | } |
| 2023 | |
| 2024 | return Py_BuildValue("(Nn)", to_copy, copied); |
| 2025 | } |
| 2026 | |
| 2027 | static PyObject * |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2028 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 2029 | { |
| 2030 | Py_UNICODE *unicode; |
| 2031 | Py_ssize_t length; |
| 2032 | char *errors = NULL; |
| 2033 | PyObject *decimal; |
| 2034 | Py_ssize_t decimal_length, new_length; |
| 2035 | int res; |
| 2036 | |
| 2037 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 2038 | return NULL; |
| 2039 | |
| 2040 | decimal_length = length * 7; /* len('€') */ |
| 2041 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 2042 | if (decimal == NULL) |
| 2043 | return NULL; |
| 2044 | |
| 2045 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 2046 | PyBytes_AS_STRING(decimal), |
| 2047 | errors); |
| 2048 | if (res < 0) { |
| 2049 | Py_DECREF(decimal); |
| 2050 | return NULL; |
| 2051 | } |
| 2052 | |
| 2053 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 2054 | assert(new_length <= decimal_length); |
| 2055 | res = _PyBytes_Resize(&decimal, new_length); |
| 2056 | if (res < 0) |
| 2057 | return NULL; |
| 2058 | |
| 2059 | return decimal; |
| 2060 | } |
| 2061 | |
| 2062 | static PyObject * |
| 2063 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 2064 | { |
| 2065 | Py_UNICODE *unicode; |
| 2066 | Py_ssize_t length; |
| 2067 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 2068 | return NULL; |
| 2069 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 2070 | } |
| 2071 | |
Inada Naoki | 2c4928d | 2020-06-17 20:09:44 +0900 | [diff] [blame] | 2072 | /* Ignore use of deprecated APIs */ |
| 2073 | _Py_COMP_DIAG_PUSH |
| 2074 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 2075 | |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2076 | static PyObject * |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 2077 | unicode_legacy_string(PyObject *self, PyObject *args) |
| 2078 | { |
| 2079 | Py_UNICODE *data; |
| 2080 | Py_ssize_t len; |
| 2081 | PyObject *u; |
| 2082 | |
| 2083 | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
| 2084 | return NULL; |
| 2085 | |
| 2086 | u = PyUnicode_FromUnicode(NULL, len); |
| 2087 | if (u == NULL) |
| 2088 | return NULL; |
| 2089 | |
| 2090 | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
| 2091 | |
| 2092 | if (len > 0) { /* The empty string is always ready. */ |
| 2093 | assert(!PyUnicode_IS_READY(u)); |
| 2094 | } |
| 2095 | |
| 2096 | return u; |
| 2097 | } |
Inada Naoki | 2c4928d | 2020-06-17 20:09:44 +0900 | [diff] [blame] | 2098 | _Py_COMP_DIAG_POP |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 2099 | |
| 2100 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2101 | getargs_w_star(PyObject *self, PyObject *args) |
| 2102 | { |
| 2103 | Py_buffer buffer; |
| 2104 | PyObject *result; |
| 2105 | char *str; |
| 2106 | |
| 2107 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 2108 | return NULL; |
| 2109 | |
| 2110 | if (2 <= buffer.len) { |
| 2111 | str = buffer.buf; |
| 2112 | str[0] = '['; |
| 2113 | str[buffer.len-1] = ']'; |
| 2114 | } |
| 2115 | |
| 2116 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 2117 | PyBuffer_Release(&buffer); |
| 2118 | return result; |
| 2119 | } |
| 2120 | |
| 2121 | |
| 2122 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2123 | test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 2126 | PyObject *tuple, *dict = NULL; |
| 2127 | static char *kwlist[] = {NULL}; |
| 2128 | int result; |
| 2129 | tuple = PyTuple_New(0); |
| 2130 | if (!tuple) |
| 2131 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2132 | if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | goto done; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2134 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | dict = PyDict_New(); |
| 2136 | if (!dict) |
| 2137 | goto done; |
| 2138 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2139 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | Py_DECREF(tuple); |
| 2141 | Py_XDECREF(dict); |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2142 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2144 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | else { |
| 2146 | Py_RETURN_NONE; |
| 2147 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2151 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2153 | const char *encoding, *errors = NULL; |
| 2154 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 2155 | &encoding, &errors)) |
| 2156 | return NULL; |
| 2157 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2160 | static PyObject * |
| 2161 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2162 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2163 | const char *encoding, *errors = NULL; |
| 2164 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 2165 | &encoding, &errors)) |
| 2166 | return NULL; |
| 2167 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 2170 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 2171 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2172 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2173 | test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | struct triple { |
| 2176 | long input; |
| 2177 | size_t nbits; |
| 2178 | int sign; |
| 2179 | } testcases[] = {{0, 0, 0}, |
| 2180 | {1L, 1, 1}, |
| 2181 | {-1L, 1, -1}, |
| 2182 | {2L, 2, 1}, |
| 2183 | {-2L, 2, -1}, |
| 2184 | {3L, 2, 1}, |
| 2185 | {-3L, 2, -1}, |
| 2186 | {4L, 3, 1}, |
| 2187 | {-4L, 3, -1}, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2188 | {0x7fffL, 15, 1}, /* one Python int digit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | {-0x7fffL, 15, -1}, |
| 2190 | {0xffffL, 16, 1}, |
| 2191 | {-0xffffL, 16, -1}, |
| 2192 | {0xfffffffL, 28, 1}, |
| 2193 | {-0xfffffffL, 28, -1}}; |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 2194 | size_t i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2195 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2196 | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2197 | size_t nbits; |
| 2198 | int sign; |
| 2199 | PyObject *plong; |
| 2200 | |
| 2201 | plong = PyLong_FromLong(testcases[i].input); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2202 | if (plong == NULL) |
| 2203 | return NULL; |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2204 | nbits = _PyLong_NumBits(plong); |
| 2205 | sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | Py_DECREF(plong); |
| 2208 | if (nbits != testcases[i].nbits) |
| 2209 | return raiseTestError("test_long_numbits", |
| 2210 | "wrong result for _PyLong_NumBits"); |
| 2211 | if (sign != testcases[i].sign) |
| 2212 | return raiseTestError("test_long_numbits", |
| 2213 | "wrong result for _PyLong_Sign"); |
| 2214 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2215 | Py_RETURN_NONE; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 2218 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2219 | |
| 2220 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2221 | test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 2224 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 2225 | Py_XDECREF(o1); |
| 2226 | Py_XDECREF(o2); |
| 2227 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2230 | static PyObject * |
| 2231 | raise_exception(PyObject *self, PyObject *args) |
| 2232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2233 | PyObject *exc; |
| 2234 | PyObject *exc_args, *v; |
| 2235 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 2238 | &exc, &num_args)) |
| 2239 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2241 | exc_args = PyTuple_New(num_args); |
| 2242 | if (exc_args == NULL) |
| 2243 | return NULL; |
| 2244 | for (i = 0; i < num_args; ++i) { |
| 2245 | v = PyLong_FromLong(i); |
| 2246 | if (v == NULL) { |
| 2247 | Py_DECREF(exc_args); |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | PyTuple_SET_ITEM(exc_args, i, v); |
| 2251 | } |
| 2252 | PyErr_SetObject(exc, exc_args); |
| 2253 | Py_DECREF(exc_args); |
| 2254 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2255 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2256 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2257 | static PyObject * |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 2258 | set_errno(PyObject *self, PyObject *args) |
| 2259 | { |
| 2260 | int new_errno; |
| 2261 | |
| 2262 | if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
| 2263 | return NULL; |
| 2264 | |
| 2265 | errno = new_errno; |
| 2266 | Py_RETURN_NONE; |
| 2267 | } |
| 2268 | |
| 2269 | static PyObject * |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2270 | test_set_exc_info(PyObject *self, PyObject *args) |
| 2271 | { |
| 2272 | PyObject *orig_exc; |
| 2273 | PyObject *new_type, *new_value, *new_tb; |
| 2274 | PyObject *type, *value, *tb; |
| 2275 | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
| 2276 | &new_type, &new_value, &new_tb)) |
| 2277 | return NULL; |
| 2278 | |
| 2279 | PyErr_GetExcInfo(&type, &value, &tb); |
| 2280 | |
| 2281 | Py_INCREF(new_type); |
| 2282 | Py_INCREF(new_value); |
| 2283 | Py_INCREF(new_tb); |
| 2284 | PyErr_SetExcInfo(new_type, new_value, new_tb); |
| 2285 | |
| 2286 | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
| 2287 | Py_XDECREF(type); |
| 2288 | Py_XDECREF(value); |
| 2289 | Py_XDECREF(tb); |
| 2290 | return orig_exc; |
| 2291 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2292 | |
| 2293 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2294 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2295 | static PyObject * |
| 2296 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2297 | if (PyDateTimeAPI) { |
| 2298 | if (test_run_counter) { |
| 2299 | /* Probably regrtest.py -R */ |
| 2300 | Py_RETURN_NONE; |
| 2301 | } |
| 2302 | else { |
| 2303 | PyErr_SetString(PyExc_AssertionError, |
| 2304 | "PyDateTime_CAPI somehow initialized"); |
| 2305 | return NULL; |
| 2306 | } |
| 2307 | } |
| 2308 | test_run_counter++; |
| 2309 | PyDateTime_IMPORT; |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | if (PyDateTimeAPI) |
| 2312 | Py_RETURN_NONE; |
| 2313 | else |
| 2314 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2317 | /* Functions exposing the C API type checking for testing */ |
| 2318 | #define MAKE_DATETIME_CHECK_FUNC(check_method, exact_method) \ |
| 2319 | PyObject *obj; \ |
| 2320 | int exact = 0; \ |
| 2321 | if (!PyArg_ParseTuple(args, "O|p", &obj, &exact)) { \ |
| 2322 | return NULL; \ |
| 2323 | } \ |
| 2324 | int rv = exact?exact_method(obj):check_method(obj); \ |
| 2325 | if (rv) { \ |
| 2326 | Py_RETURN_TRUE; \ |
| 2327 | } else { \ |
| 2328 | Py_RETURN_FALSE; \ |
| 2329 | } |
| 2330 | |
| 2331 | static PyObject * |
| 2332 | datetime_check_date(PyObject *self, PyObject *args) { |
| 2333 | MAKE_DATETIME_CHECK_FUNC(PyDate_Check, PyDate_CheckExact) |
| 2334 | } |
| 2335 | |
| 2336 | static PyObject * |
| 2337 | datetime_check_time(PyObject *self, PyObject *args) { |
| 2338 | MAKE_DATETIME_CHECK_FUNC(PyTime_Check, PyTime_CheckExact) |
| 2339 | } |
| 2340 | |
| 2341 | static PyObject * |
| 2342 | datetime_check_datetime(PyObject *self, PyObject *args) { |
| 2343 | MAKE_DATETIME_CHECK_FUNC(PyDateTime_Check, PyDateTime_CheckExact) |
| 2344 | } |
| 2345 | |
| 2346 | static PyObject * |
| 2347 | datetime_check_delta(PyObject *self, PyObject *args) { |
| 2348 | MAKE_DATETIME_CHECK_FUNC(PyDelta_Check, PyDelta_CheckExact) |
| 2349 | } |
| 2350 | |
| 2351 | static PyObject * |
| 2352 | datetime_check_tzinfo(PyObject *self, PyObject *args) { |
| 2353 | MAKE_DATETIME_CHECK_FUNC(PyTZInfo_Check, PyTZInfo_CheckExact) |
| 2354 | } |
| 2355 | |
| 2356 | |
| 2357 | /* Makes three variations on timezone representing UTC-5: |
| 2358 | 1. timezone with offset and name from PyDateTimeAPI |
| 2359 | 2. timezone with offset and name from PyTimeZone_FromOffsetAndName |
| 2360 | 3. timezone with offset (no name) from PyTimeZone_FromOffset |
| 2361 | */ |
| 2362 | static PyObject * |
| 2363 | make_timezones_capi(PyObject *self, PyObject *args) { |
| 2364 | PyObject *offset = PyDelta_FromDSU(0, -18000, 0); |
| 2365 | PyObject *name = PyUnicode_FromString("EST"); |
| 2366 | |
| 2367 | PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name); |
| 2368 | PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name); |
| 2369 | PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset); |
| 2370 | |
| 2371 | Py_DecRef(offset); |
| 2372 | Py_DecRef(name); |
| 2373 | |
| 2374 | PyObject *rv = PyTuple_New(3); |
| 2375 | |
| 2376 | PyTuple_SET_ITEM(rv, 0, est_zone_capi); |
| 2377 | PyTuple_SET_ITEM(rv, 1, est_zone_macro); |
| 2378 | PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname); |
| 2379 | |
| 2380 | return rv; |
| 2381 | } |
| 2382 | |
| 2383 | static PyObject * |
Paul Ganssle | a049f57 | 2018-02-22 15:15:32 -0500 | [diff] [blame] | 2384 | get_timezones_offset_zero(PyObject *self, PyObject *args) { |
| 2385 | PyObject *offset = PyDelta_FromDSU(0, 0, 0); |
| 2386 | PyObject *name = PyUnicode_FromString(""); |
| 2387 | |
| 2388 | // These two should return the UTC singleton |
| 2389 | PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset); |
| 2390 | PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL); |
| 2391 | |
| 2392 | // This one will return +00:00 zone, but not the UTC singleton |
| 2393 | PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name); |
| 2394 | |
| 2395 | Py_DecRef(offset); |
| 2396 | Py_DecRef(name); |
| 2397 | |
| 2398 | PyObject *rv = PyTuple_New(3); |
| 2399 | PyTuple_SET_ITEM(rv, 0, utc_singleton_0); |
| 2400 | PyTuple_SET_ITEM(rv, 1, utc_singleton_1); |
| 2401 | PyTuple_SET_ITEM(rv, 2, non_utc_zone); |
| 2402 | |
| 2403 | return rv; |
| 2404 | } |
| 2405 | |
| 2406 | static PyObject * |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2407 | get_timezone_utc_capi(PyObject* self, PyObject *args) { |
| 2408 | int macro = 0; |
| 2409 | if (!PyArg_ParseTuple(args, "|p", ¯o)) { |
| 2410 | return NULL; |
| 2411 | } |
| 2412 | if (macro) { |
Paul Ganssle | 58dc03c | 2018-01-25 08:58:07 -0500 | [diff] [blame] | 2413 | Py_INCREF(PyDateTime_TimeZone_UTC); |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2414 | return PyDateTime_TimeZone_UTC; |
| 2415 | } else { |
Paul Ganssle | 58dc03c | 2018-01-25 08:58:07 -0500 | [diff] [blame] | 2416 | Py_INCREF(PyDateTimeAPI->TimeZone_UTC); |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2417 | return PyDateTimeAPI->TimeZone_UTC; |
| 2418 | } |
| 2419 | } |
| 2420 | |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2421 | static PyObject * |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 2422 | get_date_fromdate(PyObject *self, PyObject *args) |
| 2423 | { |
| 2424 | PyObject *rv = NULL; |
| 2425 | int macro; |
| 2426 | int year, month, day; |
| 2427 | |
| 2428 | if (!PyArg_ParseTuple(args, "piii", ¯o, &year, &month, &day)) { |
| 2429 | return NULL; |
| 2430 | } |
| 2431 | |
| 2432 | if (macro) { |
| 2433 | rv = PyDate_FromDate(year, month, day); |
| 2434 | } |
| 2435 | else { |
| 2436 | rv = PyDateTimeAPI->Date_FromDate( |
| 2437 | year, month, day, |
| 2438 | PyDateTimeAPI->DateType); |
| 2439 | } |
| 2440 | return rv; |
| 2441 | } |
| 2442 | |
| 2443 | static PyObject * |
| 2444 | get_datetime_fromdateandtime(PyObject *self, PyObject *args) |
| 2445 | { |
| 2446 | PyObject *rv = NULL; |
| 2447 | int macro; |
| 2448 | int year, month, day; |
| 2449 | int hour, minute, second, microsecond; |
| 2450 | |
| 2451 | if (!PyArg_ParseTuple(args, "piiiiiii", |
| 2452 | ¯o, |
| 2453 | &year, &month, &day, |
| 2454 | &hour, &minute, &second, µsecond)) { |
| 2455 | return NULL; |
| 2456 | } |
| 2457 | |
| 2458 | if (macro) { |
| 2459 | rv = PyDateTime_FromDateAndTime( |
| 2460 | year, month, day, |
| 2461 | hour, minute, second, microsecond); |
| 2462 | } |
| 2463 | else { |
| 2464 | rv = PyDateTimeAPI->DateTime_FromDateAndTime( |
| 2465 | year, month, day, |
| 2466 | hour, minute, second, microsecond, |
| 2467 | Py_None, |
| 2468 | PyDateTimeAPI->DateTimeType); |
| 2469 | } |
| 2470 | return rv; |
| 2471 | } |
| 2472 | |
| 2473 | static PyObject * |
| 2474 | get_datetime_fromdateandtimeandfold(PyObject *self, PyObject *args) |
| 2475 | { |
| 2476 | PyObject *rv = NULL; |
| 2477 | int macro; |
| 2478 | int year, month, day; |
| 2479 | int hour, minute, second, microsecond, fold; |
| 2480 | |
| 2481 | if (!PyArg_ParseTuple(args, "piiiiiiii", |
| 2482 | ¯o, |
| 2483 | &year, &month, &day, |
| 2484 | &hour, &minute, &second, µsecond, |
| 2485 | &fold)) { |
| 2486 | return NULL; |
| 2487 | } |
| 2488 | |
| 2489 | if (macro) { |
| 2490 | rv = PyDateTime_FromDateAndTimeAndFold( |
| 2491 | year, month, day, |
| 2492 | hour, minute, second, microsecond, |
| 2493 | fold); |
| 2494 | } |
| 2495 | else { |
| 2496 | rv = PyDateTimeAPI->DateTime_FromDateAndTimeAndFold( |
| 2497 | year, month, day, |
| 2498 | hour, minute, second, microsecond, |
| 2499 | Py_None, |
| 2500 | fold, |
| 2501 | PyDateTimeAPI->DateTimeType); |
| 2502 | } |
| 2503 | return rv; |
| 2504 | } |
| 2505 | |
| 2506 | static PyObject * |
| 2507 | get_time_fromtime(PyObject *self, PyObject *args) |
| 2508 | { |
| 2509 | PyObject *rv = NULL; |
| 2510 | int macro; |
| 2511 | int hour, minute, second, microsecond; |
| 2512 | |
| 2513 | if (!PyArg_ParseTuple(args, "piiii", |
| 2514 | ¯o, |
| 2515 | &hour, &minute, &second, µsecond)) { |
| 2516 | return NULL; |
| 2517 | } |
| 2518 | |
| 2519 | if (macro) { |
| 2520 | rv = PyTime_FromTime(hour, minute, second, microsecond); |
| 2521 | } |
| 2522 | else { |
| 2523 | rv = PyDateTimeAPI->Time_FromTime( |
| 2524 | hour, minute, second, microsecond, |
| 2525 | Py_None, |
| 2526 | PyDateTimeAPI->TimeType); |
| 2527 | } |
| 2528 | return rv; |
| 2529 | } |
| 2530 | |
| 2531 | static PyObject * |
| 2532 | get_time_fromtimeandfold(PyObject *self, PyObject *args) |
| 2533 | { |
| 2534 | PyObject *rv = NULL; |
| 2535 | int macro; |
| 2536 | int hour, minute, second, microsecond, fold; |
| 2537 | |
| 2538 | if (!PyArg_ParseTuple(args, "piiiii", |
| 2539 | ¯o, |
| 2540 | &hour, &minute, &second, µsecond, |
| 2541 | &fold)) { |
| 2542 | return NULL; |
| 2543 | } |
| 2544 | |
| 2545 | if (macro) { |
| 2546 | rv = PyTime_FromTimeAndFold(hour, minute, second, microsecond, fold); |
| 2547 | } |
| 2548 | else { |
| 2549 | rv = PyDateTimeAPI->Time_FromTimeAndFold( |
| 2550 | hour, minute, second, microsecond, |
| 2551 | Py_None, |
| 2552 | fold, |
| 2553 | PyDateTimeAPI->TimeType); |
| 2554 | } |
| 2555 | return rv; |
| 2556 | } |
| 2557 | |
| 2558 | static PyObject * |
| 2559 | get_delta_fromdsu(PyObject *self, PyObject *args) |
| 2560 | { |
| 2561 | PyObject *rv = NULL; |
| 2562 | int macro; |
| 2563 | int days, seconds, microseconds; |
| 2564 | |
| 2565 | if (!PyArg_ParseTuple(args, "piii", |
| 2566 | ¯o, |
| 2567 | &days, &seconds, µseconds)) { |
| 2568 | return NULL; |
| 2569 | } |
| 2570 | |
| 2571 | if (macro) { |
| 2572 | rv = PyDelta_FromDSU(days, seconds, microseconds); |
| 2573 | } |
| 2574 | else { |
| 2575 | rv = PyDateTimeAPI->Delta_FromDelta( |
| 2576 | days, seconds, microseconds, 1, |
| 2577 | PyDateTimeAPI->DeltaType); |
| 2578 | } |
| 2579 | |
| 2580 | return rv; |
| 2581 | } |
| 2582 | |
| 2583 | static PyObject * |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2584 | get_date_fromtimestamp(PyObject* self, PyObject *args) |
| 2585 | { |
| 2586 | PyObject *tsargs = NULL, *ts = NULL, *rv = NULL; |
| 2587 | int macro = 0; |
| 2588 | |
| 2589 | if (!PyArg_ParseTuple(args, "O|p", &ts, ¯o)) { |
| 2590 | return NULL; |
| 2591 | } |
| 2592 | |
| 2593 | // Construct the argument tuple |
| 2594 | if ((tsargs = PyTuple_Pack(1, ts)) == NULL) { |
| 2595 | return NULL; |
| 2596 | } |
| 2597 | |
| 2598 | // Pass along to the API function |
| 2599 | if (macro) { |
| 2600 | rv = PyDate_FromTimestamp(tsargs); |
| 2601 | } |
| 2602 | else { |
| 2603 | rv = PyDateTimeAPI->Date_FromTimestamp( |
| 2604 | (PyObject *)PyDateTimeAPI->DateType, tsargs |
| 2605 | ); |
| 2606 | } |
| 2607 | |
| 2608 | Py_DECREF(tsargs); |
| 2609 | return rv; |
| 2610 | } |
| 2611 | |
| 2612 | static PyObject * |
| 2613 | get_datetime_fromtimestamp(PyObject* self, PyObject *args) |
| 2614 | { |
| 2615 | int macro = 0; |
| 2616 | int usetz = 0; |
| 2617 | PyObject *tsargs = NULL, *ts = NULL, *tzinfo = Py_None, *rv = NULL; |
| 2618 | if (!PyArg_ParseTuple(args, "OO|pp", &ts, &tzinfo, &usetz, ¯o)) { |
| 2619 | return NULL; |
| 2620 | } |
| 2621 | |
| 2622 | // Construct the argument tuple |
| 2623 | if (usetz) { |
| 2624 | tsargs = PyTuple_Pack(2, ts, tzinfo); |
| 2625 | } |
| 2626 | else { |
| 2627 | tsargs = PyTuple_Pack(1, ts); |
| 2628 | } |
| 2629 | |
| 2630 | if (tsargs == NULL) { |
| 2631 | return NULL; |
| 2632 | } |
| 2633 | |
| 2634 | // Pass along to the API function |
| 2635 | if (macro) { |
| 2636 | rv = PyDateTime_FromTimestamp(tsargs); |
| 2637 | } |
| 2638 | else { |
| 2639 | rv = PyDateTimeAPI->DateTime_FromTimestamp( |
| 2640 | (PyObject *)PyDateTimeAPI->DateTimeType, tsargs, NULL |
| 2641 | ); |
| 2642 | } |
| 2643 | |
| 2644 | Py_DECREF(tsargs); |
| 2645 | return rv; |
| 2646 | } |
| 2647 | |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 2648 | static PyObject * |
| 2649 | test_PyDateTime_GET(PyObject *self, PyObject *obj) |
| 2650 | { |
| 2651 | int year, month, day; |
| 2652 | |
| 2653 | year = PyDateTime_GET_YEAR(obj); |
| 2654 | month = PyDateTime_GET_MONTH(obj); |
| 2655 | day = PyDateTime_GET_DAY(obj); |
| 2656 | |
| 2657 | return Py_BuildValue("(lll)", year, month, day); |
| 2658 | } |
| 2659 | |
| 2660 | static PyObject * |
| 2661 | test_PyDateTime_DATE_GET(PyObject *self, PyObject *obj) |
| 2662 | { |
| 2663 | int hour, minute, second, microsecond; |
| 2664 | |
| 2665 | hour = PyDateTime_DATE_GET_HOUR(obj); |
| 2666 | minute = PyDateTime_DATE_GET_MINUTE(obj); |
| 2667 | second = PyDateTime_DATE_GET_SECOND(obj); |
| 2668 | microsecond = PyDateTime_DATE_GET_MICROSECOND(obj); |
| 2669 | |
| 2670 | return Py_BuildValue("(llll)", hour, minute, second, microsecond); |
| 2671 | } |
| 2672 | |
| 2673 | static PyObject * |
| 2674 | test_PyDateTime_TIME_GET(PyObject *self, PyObject *obj) |
| 2675 | { |
| 2676 | int hour, minute, second, microsecond; |
| 2677 | |
| 2678 | hour = PyDateTime_TIME_GET_HOUR(obj); |
| 2679 | minute = PyDateTime_TIME_GET_MINUTE(obj); |
| 2680 | second = PyDateTime_TIME_GET_SECOND(obj); |
| 2681 | microsecond = PyDateTime_TIME_GET_MICROSECOND(obj); |
| 2682 | |
| 2683 | return Py_BuildValue("(llll)", hour, minute, second, microsecond); |
| 2684 | } |
| 2685 | |
| 2686 | static PyObject * |
| 2687 | test_PyDateTime_DELTA_GET(PyObject *self, PyObject *obj) |
| 2688 | { |
| 2689 | int days, seconds, microseconds; |
| 2690 | |
| 2691 | days = PyDateTime_DELTA_GET_DAYS(obj); |
| 2692 | seconds = PyDateTime_DELTA_GET_SECONDS(obj); |
| 2693 | microseconds = PyDateTime_DELTA_GET_MICROSECONDS(obj); |
| 2694 | |
| 2695 | return Py_BuildValue("(lll)", days, seconds, microseconds); |
| 2696 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2697 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2698 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 2699 | * `thread_done` when it's finished. The driver code has to know when the |
| 2700 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 2701 | * may go away when the driver finishes. The former lack of this explicit |
| 2702 | * synchronization caused rare segfaults, so rare that they were seen only |
| 2703 | * on a Mac buildbot (although they were possible on any box). |
| 2704 | */ |
| 2705 | static PyThread_type_lock thread_done = NULL; |
| 2706 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2707 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2708 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2709 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2710 | PyObject *rc; |
| 2711 | int success; |
| 2712 | PyGILState_STATE s = PyGILState_Ensure(); |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2713 | rc = _PyObject_CallNoArg((PyObject *)callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2714 | success = (rc != NULL); |
| 2715 | Py_XDECREF(rc); |
| 2716 | PyGILState_Release(s); |
| 2717 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2720 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 2721 | * should be called only from threads spawned by test_thread_state(). |
| 2722 | */ |
| 2723 | static void |
| 2724 | _make_call_from_thread(void *callable) |
| 2725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | _make_call(callable); |
| 2727 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2730 | static PyObject * |
| 2731 | test_thread_state(PyObject *self, PyObject *args) |
| 2732 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | PyObject *fn; |
| 2734 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 2737 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 | if (!PyCallable_Check(fn)) { |
| 2740 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 2741 | Py_TYPE(fn)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | return NULL; |
| 2743 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | thread_done = PyThread_allocate_lock(); |
| 2746 | if (thread_done == NULL) |
| 2747 | return PyErr_NoMemory(); |
| 2748 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | /* Start a new thread with our callback. */ |
| 2751 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2752 | /* Make the callback with the thread lock held by this thread */ |
| 2753 | success &= _make_call(fn); |
| 2754 | /* Do it all again, but this time with the thread-lock released */ |
| 2755 | Py_BEGIN_ALLOW_THREADS |
| 2756 | success &= _make_call(fn); |
| 2757 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2758 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | /* And once more with and without a thread |
| 2761 | XXX - should use a lock and work out exactly what we are trying |
| 2762 | to test <wink> |
| 2763 | */ |
| 2764 | Py_BEGIN_ALLOW_THREADS |
| 2765 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2766 | success &= _make_call(fn); |
| 2767 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2768 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 2771 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | PyThread_free_lock(thread_done); |
| 2774 | if (!success) |
| 2775 | return NULL; |
| 2776 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2777 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2778 | |
| 2779 | /* test Py_AddPendingCalls using threads */ |
| 2780 | static int _pending_callback(void *arg) |
| 2781 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | /* we assume the argument is callable object to which we own a reference */ |
| 2783 | PyObject *callable = (PyObject *)arg; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 2784 | PyObject *r = _PyObject_CallNoArg(callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | Py_DECREF(callable); |
| 2786 | Py_XDECREF(r); |
| 2787 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | /* The following requests n callbacks to _pending_callback. It can be |
| 2791 | * run from any python thread. |
| 2792 | */ |
Benjamin Peterson | b4588c2 | 2018-07-03 22:30:56 -0700 | [diff] [blame] | 2793 | static PyObject * |
| 2794 | pending_threadfunc(PyObject *self, PyObject *arg) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2795 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | PyObject *callable; |
| 2797 | int r; |
| 2798 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 2799 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2801 | /* create the reference for the callbackwhile we hold the lock */ |
| 2802 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | Py_BEGIN_ALLOW_THREADS |
| 2805 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 2806 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | if (r<0) { |
| 2809 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2810 | Py_RETURN_FALSE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2812 | Py_RETURN_TRUE; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2813 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2814 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 2815 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2816 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 2817 | test_string_from_format(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2818 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | PyObject *result; |
| 2820 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2821 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2822 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 2823 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 2824 | if (result == NULL) \ |
| 2825 | return NULL; \ |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 2826 | if (!_PyUnicode_EqualToASCIIString(result, "1")) { \ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2827 | msg = FORMAT " failed at 1"; \ |
| 2828 | goto Fail; \ |
| 2829 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | CHECK_1_FORMAT("%d", int); |
| 2833 | CHECK_1_FORMAT("%ld", long); |
| 2834 | /* The z width modifier was added in Python 2.5. */ |
| 2835 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | /* The u type code was added in Python 2.5. */ |
| 2838 | CHECK_1_FORMAT("%u", unsigned int); |
| 2839 | CHECK_1_FORMAT("%lu", unsigned long); |
| 2840 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2842 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 2843 | CHECK_1_FORMAT("%llu", unsigned long long); |
| 2844 | CHECK_1_FORMAT("%lld", long long); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2847 | |
| 2848 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | Py_XDECREF(result); |
| 2850 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2851 | |
| 2852 | #undef CHECK_1_FORMAT |
| 2853 | } |
| 2854 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2855 | |
| 2856 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2857 | test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2858 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 2859 | int result; |
| 2860 | if (py_s == NULL) |
| 2861 | return NULL; |
| 2862 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 2863 | Py_DECREF(py_s); |
| 2864 | if (!result) { |
| 2865 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 2866 | "should not compare equal to c string."); |
| 2867 | return NULL; |
| 2868 | } |
| 2869 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2870 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2871 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2872 | /* This is here to provide a docstring for test_descr. */ |
| 2873 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2874 | test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2875 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2879 | /* Test PyOS_string_to_double. */ |
| 2880 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2881 | test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | double result; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 2883 | const char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | #define CHECK_STRING(STR, expected) \ |
| 2886 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2887 | if (result == -1.0 && PyErr_Occurred()) \ |
| 2888 | return NULL; \ |
Benjamin Peterson | 8f4b247 | 2016-09-07 18:09:22 -0700 | [diff] [blame] | 2889 | if (result != (double)expected) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | msg = "conversion of " STR " to float failed"; \ |
| 2891 | goto fail; \ |
| 2892 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | #define CHECK_INVALID(STR) \ |
| 2895 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2896 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 2897 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 2898 | PyErr_Clear(); \ |
| 2899 | else \ |
| 2900 | return NULL; \ |
| 2901 | } \ |
| 2902 | else { \ |
| 2903 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 2904 | goto fail; \ |
| 2905 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | CHECK_STRING("0.1", 0.1); |
| 2908 | CHECK_STRING("1.234", 1.234); |
| 2909 | CHECK_STRING("-1.35", -1.35); |
| 2910 | CHECK_STRING(".1e01", 1.0); |
| 2911 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | CHECK_INVALID(" 0.1"); |
| 2914 | CHECK_INVALID("\t\n-3"); |
| 2915 | CHECK_INVALID(".123 "); |
| 2916 | CHECK_INVALID("3\n"); |
| 2917 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2919 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2920 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2922 | #undef CHECK_STRING |
| 2923 | #undef CHECK_INVALID |
| 2924 | } |
| 2925 | |
| 2926 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2927 | /* Coverage testing of capsule objects. */ |
| 2928 | |
| 2929 | static const char *capsule_name = "capsule name"; |
| 2930 | static char *capsule_pointer = "capsule pointer"; |
| 2931 | static char *capsule_context = "capsule context"; |
| 2932 | static const char *capsule_error = NULL; |
| 2933 | static int |
| 2934 | capsule_destructor_call_count = 0; |
| 2935 | |
| 2936 | static void |
| 2937 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | capsule_destructor_call_count++; |
| 2939 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 2940 | capsule_error = "context did not match in destructor!"; |
| 2941 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2942 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2943 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 2944 | capsule_error = "name did not match in destructor!"; |
| 2945 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2946 | capsule_error = "pointer did not match in destructor!"; |
| 2947 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2951 | char *name; |
| 2952 | char *module; |
| 2953 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2954 | } known_capsule; |
| 2955 | |
| 2956 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 2957 | test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2958 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | PyObject *object; |
| 2960 | const char *error = NULL; |
| 2961 | void *pointer; |
| 2962 | void *pointer2; |
| 2963 | known_capsule known_capsules[] = { |
| 2964 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2965 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 2966 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 2967 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2968 | { NULL, NULL }, |
| 2969 | }; |
| 2970 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2971 | |
| 2972 | #define FAIL(x) { error = (x); goto exit; } |
| 2973 | |
| 2974 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | if (capsule_error) { \ |
| 2976 | FAIL(capsule_error); \ |
| 2977 | } \ |
| 2978 | else if (!capsule_destructor_call_count) { \ |
| 2979 | FAIL("destructor not called!"); \ |
| 2980 | } \ |
| 2981 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2984 | PyCapsule_SetContext(object, capsule_context); |
| 2985 | capsule_destructor(object); |
| 2986 | CHECK_DESTRUCTOR; |
| 2987 | Py_DECREF(object); |
| 2988 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | object = PyCapsule_New(known, "ignored", NULL); |
| 2991 | PyCapsule_SetPointer(object, capsule_pointer); |
| 2992 | PyCapsule_SetName(object, capsule_name); |
| 2993 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 2994 | PyCapsule_SetContext(object, capsule_context); |
| 2995 | capsule_destructor(object); |
| 2996 | CHECK_DESTRUCTOR; |
| 2997 | /* intentionally access using the wrong name */ |
| 2998 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2999 | if (!PyErr_Occurred()) { |
| 3000 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 3001 | } |
| 3002 | PyErr_Clear(); |
| 3003 | if (pointer2) { |
| 3004 | if (pointer2 == capsule_pointer) { |
| 3005 | FAIL("PyCapsule_GetPointer should not have" |
| 3006 | " returned the internal pointer!"); |
| 3007 | } else { |
| 3008 | FAIL("PyCapsule_GetPointer should have " |
| 3009 | "returned NULL pointer but did not!"); |
| 3010 | } |
| 3011 | } |
| 3012 | PyCapsule_SetDestructor(object, NULL); |
| 3013 | Py_DECREF(object); |
| 3014 | if (capsule_destructor_call_count) { |
| 3015 | FAIL("destructor called when it should not have been!"); |
| 3016 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 3019 | /* yeah, ordinarily I wouldn't do this either, |
| 3020 | but it's fine for this test harness. |
| 3021 | */ |
| 3022 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3023 | #undef FAIL |
| 3024 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | { \ |
| 3026 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 3027 | x, known->module, known->attribute); \ |
| 3028 | error = buffer; \ |
| 3029 | goto exit; \ |
| 3030 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | PyObject *module = PyImport_ImportModule(known->module); |
| 3033 | if (module) { |
| 3034 | pointer = PyCapsule_Import(known->name, 0); |
| 3035 | if (!pointer) { |
| 3036 | Py_DECREF(module); |
| 3037 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 3038 | } |
| 3039 | object = PyObject_GetAttrString(module, known->attribute); |
| 3040 | if (!object) { |
| 3041 | Py_DECREF(module); |
| 3042 | return NULL; |
| 3043 | } |
| 3044 | pointer2 = PyCapsule_GetPointer(object, |
| 3045 | "weebles wobble but they don't fall down"); |
| 3046 | if (!PyErr_Occurred()) { |
| 3047 | Py_DECREF(object); |
| 3048 | Py_DECREF(module); |
| 3049 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 3050 | } |
| 3051 | PyErr_Clear(); |
| 3052 | if (pointer2) { |
| 3053 | Py_DECREF(module); |
| 3054 | Py_DECREF(object); |
| 3055 | if (pointer2 == pointer) { |
| 3056 | FAIL("PyCapsule_GetPointer should not have" |
| 3057 | " returned its internal pointer!"); |
| 3058 | } else { |
| 3059 | FAIL("PyCapsule_GetPointer should have" |
| 3060 | " returned NULL pointer but did not!"); |
| 3061 | } |
| 3062 | } |
| 3063 | Py_DECREF(object); |
| 3064 | Py_DECREF(module); |
| 3065 | } |
| 3066 | else |
| 3067 | PyErr_Clear(); |
| 3068 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3069 | |
| 3070 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | if (error) { |
| 3072 | return raiseTestError("test_capsule", error); |
| 3073 | } |
| 3074 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3075 | #undef FAIL |
| 3076 | } |
| 3077 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3078 | #ifdef HAVE_GETTIMEOFDAY |
| 3079 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 3080 | 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] | 3081 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | e->tv_sec -= s->tv_sec; |
| 3083 | e->tv_usec -= s->tv_usec; |
| 3084 | if (e->tv_usec < 0) { |
| 3085 | e->tv_sec -=1; |
| 3086 | e->tv_usec += 1000000; |
| 3087 | } |
| 3088 | 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] | 3089 | } |
| 3090 | |
| 3091 | static PyObject * |
| 3092 | profile_int(PyObject *self, PyObject* args) |
| 3093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | int i, k; |
| 3095 | struct timeval start, stop; |
| 3096 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3098 | /* Test 1: Allocate and immediately deallocate |
| 3099 | many small integers */ |
| 3100 | gettimeofday(&start, NULL); |
| 3101 | for(k=0; k < 20000; k++) |
| 3102 | for(i=0; i < 1000; i++) { |
| 3103 | single = PyLong_FromLong(i); |
| 3104 | Py_DECREF(single); |
| 3105 | } |
| 3106 | gettimeofday(&stop, NULL); |
| 3107 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | /* Test 2: Allocate and immediately deallocate |
| 3110 | many large integers */ |
| 3111 | gettimeofday(&start, NULL); |
| 3112 | for(k=0; k < 20000; k++) |
| 3113 | for(i=0; i < 1000; i++) { |
| 3114 | single = PyLong_FromLong(i+1000000); |
| 3115 | Py_DECREF(single); |
| 3116 | } |
| 3117 | gettimeofday(&stop, NULL); |
| 3118 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | /* Test 3: Allocate a few integers, then release |
| 3121 | them all simultaneously. */ |
| 3122 | multiple = malloc(sizeof(PyObject*) * 1000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3123 | if (multiple == NULL) |
| 3124 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | gettimeofday(&start, NULL); |
| 3126 | for(k=0; k < 20000; k++) { |
| 3127 | for(i=0; i < 1000; i++) { |
| 3128 | multiple[i] = PyLong_FromLong(i+1000000); |
| 3129 | } |
| 3130 | for(i=0; i < 1000; i++) { |
| 3131 | Py_DECREF(multiple[i]); |
| 3132 | } |
| 3133 | } |
| 3134 | gettimeofday(&stop, NULL); |
| 3135 | print_delta(3, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3136 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | /* Test 4: Allocate many integers, then release |
| 3139 | them all simultaneously. */ |
| 3140 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3141 | if (multiple == NULL) |
| 3142 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | gettimeofday(&start, NULL); |
| 3144 | for(k=0; k < 20; k++) { |
| 3145 | for(i=0; i < 1000000; i++) { |
| 3146 | multiple[i] = PyLong_FromLong(i+1000000); |
| 3147 | } |
| 3148 | for(i=0; i < 1000000; i++) { |
| 3149 | Py_DECREF(multiple[i]); |
| 3150 | } |
| 3151 | } |
| 3152 | gettimeofday(&stop, NULL); |
| 3153 | print_delta(4, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3154 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | /* Test 5: Allocate many integers < 32000 */ |
| 3157 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3158 | if (multiple == NULL) |
| 3159 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | gettimeofday(&start, NULL); |
| 3161 | for(k=0; k < 10; k++) { |
| 3162 | for(i=0; i < 1000000; i++) { |
| 3163 | multiple[i] = PyLong_FromLong(i+1000); |
| 3164 | } |
| 3165 | for(i=0; i < 1000000; i++) { |
| 3166 | Py_DECREF(multiple[i]); |
| 3167 | } |
| 3168 | } |
| 3169 | gettimeofday(&stop, NULL); |
| 3170 | print_delta(5, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3171 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | /* Test 6: Perform small int addition */ |
| 3174 | op1 = PyLong_FromLong(1); |
| 3175 | gettimeofday(&start, NULL); |
| 3176 | for(i=0; i < 10000000; i++) { |
| 3177 | result = PyNumber_Add(op1, op1); |
| 3178 | Py_DECREF(result); |
| 3179 | } |
| 3180 | gettimeofday(&stop, NULL); |
| 3181 | Py_DECREF(op1); |
| 3182 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3184 | /* Test 7: Perform medium int addition */ |
| 3185 | op1 = PyLong_FromLong(1000); |
Christian Heimes | 66eda26 | 2013-07-26 15:54:07 +0200 | [diff] [blame] | 3186 | if (op1 == NULL) |
| 3187 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | gettimeofday(&start, NULL); |
| 3189 | for(i=0; i < 10000000; i++) { |
| 3190 | result = PyNumber_Add(op1, op1); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 3191 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | } |
| 3193 | gettimeofday(&stop, NULL); |
| 3194 | Py_DECREF(op1); |
| 3195 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3196 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 3197 | Py_RETURN_NONE; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3198 | } |
| 3199 | #endif |
| 3200 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3201 | /* To test the format of tracebacks as printed out. */ |
| 3202 | static PyObject * |
| 3203 | traceback_print(PyObject *self, PyObject *args) |
| 3204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | PyObject *file; |
| 3206 | PyObject *traceback; |
| 3207 | int result; |
| 3208 | |
| 3209 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 3210 | &traceback, &file)) |
| 3211 | return NULL; |
| 3212 | |
| 3213 | result = PyTraceBack_Print(traceback, file); |
| 3214 | if (result < 0) |
| 3215 | return NULL; |
| 3216 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3219 | /* To test the format of exceptions as printed out. */ |
| 3220 | static PyObject * |
| 3221 | exception_print(PyObject *self, PyObject *args) |
| 3222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | PyObject *value; |
| 3224 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 3227 | &value)) |
| 3228 | return NULL; |
| 3229 | if (!PyExceptionInstance_Check(value)) { |
| 3230 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 3231 | return NULL; |
| 3232 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | tb = PyException_GetTraceback(value); |
| 3235 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 3236 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | |
| 3242 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3243 | |
| 3244 | /* reliably raise a MemoryError */ |
| 3245 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3246 | raise_memoryerror(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | PyErr_NoMemory(); |
| 3249 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3252 | /* Issue 6012 */ |
| 3253 | static PyObject *str1, *str2; |
| 3254 | static int |
| 3255 | failing_converter(PyObject *obj, void *arg) |
| 3256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | /* Clone str1, then let the conversion fail. */ |
| 3258 | assert(str1); |
| 3259 | str2 = str1; |
| 3260 | Py_INCREF(str2); |
| 3261 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3262 | } |
| 3263 | static PyObject* |
| 3264 | argparsing(PyObject *o, PyObject *args) |
| 3265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3266 | PyObject *res; |
| 3267 | str1 = str2 = NULL; |
| 3268 | if (!PyArg_ParseTuple(args, "O&O&", |
| 3269 | PyUnicode_FSConverter, &str1, |
| 3270 | failing_converter, &str2)) { |
| 3271 | if (!str2) |
| 3272 | /* argument converter not called? */ |
| 3273 | return NULL; |
| 3274 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 3275 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3276 | Py_DECREF(str2); |
| 3277 | PyErr_Clear(); |
| 3278 | return res; |
| 3279 | } |
| 3280 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3281 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3282 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3283 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 3284 | static PyObject * |
| 3285 | code_newempty(PyObject *self, PyObject *args) |
| 3286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3287 | const char *filename; |
| 3288 | const char *funcname; |
| 3289 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 3292 | &filename, &funcname, &firstlineno)) |
| 3293 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3298 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 3299 | Run via Lib/test/test_exceptions.py */ |
| 3300 | static PyObject * |
| 3301 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3302 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3303 | const char *name; |
| 3304 | const char *doc = NULL; |
| 3305 | PyObject *base = NULL; |
| 3306 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3308 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3311 | "s|sOO:make_exception_with_doc", kwlist, |
| 3312 | &name, &doc, &base, &dict)) |
| 3313 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3318 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3319 | make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3320 | { |
| 3321 | Py_buffer info; |
| 3322 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 3323 | return NULL; |
| 3324 | return PyMemoryView_FromBuffer(&info); |
| 3325 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 3326 | |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3327 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3328 | test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3329 | { |
| 3330 | int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 3331 | int init[5] = {0, 1, 2, 3, 4}; |
| 3332 | Py_ssize_t itemsize = sizeof(int); |
| 3333 | Py_ssize_t shape = 5; |
| 3334 | Py_ssize_t strides = 2 * itemsize; |
| 3335 | Py_buffer view = { |
| 3336 | data, |
| 3337 | NULL, |
| 3338 | 5 * itemsize, |
| 3339 | itemsize, |
| 3340 | 1, |
| 3341 | 1, |
| 3342 | NULL, |
| 3343 | &shape, |
| 3344 | &strides, |
| 3345 | NULL, |
| 3346 | NULL |
| 3347 | }; |
| 3348 | int *ptr; |
| 3349 | int i; |
| 3350 | |
| 3351 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 3352 | ptr = view.buf; |
| 3353 | for (i = 0; i < 5; i++) { |
| 3354 | if (ptr[2*i] != i) { |
| 3355 | PyErr_SetString(TestError, |
| 3356 | "test_from_contiguous: incorrect result"); |
| 3357 | return NULL; |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | view.buf = &data[8]; |
| 3362 | view.strides[0] = -2 * itemsize; |
| 3363 | |
| 3364 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 3365 | ptr = view.buf; |
| 3366 | for (i = 0; i < 5; i++) { |
| 3367 | if (*(ptr-2*i) != i) { |
| 3368 | PyErr_SetString(TestError, |
| 3369 | "test_from_contiguous: incorrect result"); |
| 3370 | return NULL; |
| 3371 | } |
| 3372 | } |
| 3373 | |
| 3374 | Py_RETURN_NONE; |
| 3375 | } |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3376 | |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3377 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3378 | extern PyTypeObject _PyBytesIOBuffer_Type; |
| 3379 | |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3380 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3381 | test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3382 | { |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3383 | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3384 | PyObject *b; |
| 3385 | char *dummy[1]; |
| 3386 | int ret, match; |
| 3387 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3388 | /* PyBuffer_FillInfo() */ |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3389 | ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE); |
| 3390 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 3391 | PyErr_Clear(); |
| 3392 | if (ret != -1 || match == 0) |
| 3393 | goto error; |
| 3394 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3395 | /* bytesiobuf_getbuffer() */ |
| 3396 | b = type->tp_alloc(type, 0); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3397 | if (b == NULL) { |
| 3398 | return NULL; |
| 3399 | } |
| 3400 | |
| 3401 | ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE); |
| 3402 | Py_DECREF(b); |
| 3403 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 3404 | PyErr_Clear(); |
| 3405 | if (ret != -1 || match == 0) |
| 3406 | goto error; |
| 3407 | |
| 3408 | Py_RETURN_NONE; |
| 3409 | |
| 3410 | error: |
| 3411 | PyErr_SetString(TestError, |
| 3412 | "test_pep3118_obsolete_write_locks: failure"); |
| 3413 | return NULL; |
| 3414 | } |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3415 | #endif |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3416 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3417 | /* This tests functions that historically supported write locks. It is |
| 3418 | wrong to call getbuffer() with view==NULL and a compliant getbufferproc |
| 3419 | is entitled to segfault in that case. */ |
| 3420 | static PyObject * |
| 3421 | getbuffer_with_null_view(PyObject* self, PyObject *obj) |
| 3422 | { |
| 3423 | if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0) |
| 3424 | return NULL; |
| 3425 | |
| 3426 | Py_RETURN_NONE; |
| 3427 | } |
| 3428 | |
Joannah Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 3429 | /* PyBuffer_SizeFromFormat() */ |
| 3430 | static PyObject * |
| 3431 | test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args) |
| 3432 | { |
| 3433 | const char *format; |
| 3434 | Py_ssize_t result; |
| 3435 | |
| 3436 | if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat", |
| 3437 | &format)) { |
| 3438 | return NULL; |
| 3439 | } |
| 3440 | |
| 3441 | result = PyBuffer_SizeFromFormat(format); |
| 3442 | if (result == -1) { |
| 3443 | return NULL; |
| 3444 | } |
| 3445 | |
| 3446 | return PyLong_FromSsize_t(result); |
| 3447 | } |
| 3448 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3449 | /* Test that the fatal error from not having a current thread doesn't |
| 3450 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 3451 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3452 | crash_no_current_thread(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3453 | { |
| 3454 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 3455 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 3456 | !pydebug mode. However, the test only actually tests anything |
| 3457 | in pydebug mode, since that's where the infinite loop was in |
| 3458 | the first place. */ |
| 3459 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3460 | Py_END_ALLOW_THREADS |
| 3461 | return NULL; |
| 3462 | } |
| 3463 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3464 | /* To run some code in a sub-interpreter. */ |
| 3465 | static PyObject * |
| 3466 | run_in_subinterp(PyObject *self, PyObject *args) |
| 3467 | { |
| 3468 | const char *code; |
| 3469 | int r; |
| 3470 | PyThreadState *substate, *mainstate; |
| 3471 | |
| 3472 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 3473 | &code)) |
| 3474 | return NULL; |
| 3475 | |
| 3476 | mainstate = PyThreadState_Get(); |
| 3477 | |
| 3478 | PyThreadState_Swap(NULL); |
| 3479 | |
| 3480 | substate = Py_NewInterpreter(); |
Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 3481 | if (substate == NULL) { |
| 3482 | /* Since no new thread state was created, there is no exception to |
| 3483 | propagate; raise a fresh one after swapping in the old thread |
| 3484 | state. */ |
| 3485 | PyThreadState_Swap(mainstate); |
| 3486 | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
| 3487 | return NULL; |
| 3488 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3489 | r = PyRun_SimpleString(code); |
| 3490 | Py_EndInterpreter(substate); |
| 3491 | |
| 3492 | PyThreadState_Swap(mainstate); |
| 3493 | |
| 3494 | return PyLong_FromLong(r); |
| 3495 | } |
| 3496 | |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3497 | static int |
| 3498 | check_time_rounding(int round) |
| 3499 | { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 3500 | if (round != _PyTime_ROUND_FLOOR |
| 3501 | && round != _PyTime_ROUND_CEILING |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 3502 | && round != _PyTime_ROUND_HALF_EVEN |
| 3503 | && round != _PyTime_ROUND_UP) { |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3504 | PyErr_SetString(PyExc_ValueError, "invalid rounding"); |
| 3505 | return -1; |
| 3506 | } |
| 3507 | return 0; |
| 3508 | } |
| 3509 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3510 | static PyObject * |
| 3511 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
| 3512 | { |
| 3513 | PyObject *obj; |
| 3514 | time_t sec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3515 | int round; |
| 3516 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3517 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3518 | if (check_time_rounding(round) < 0) |
| 3519 | return NULL; |
| 3520 | if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3521 | return NULL; |
| 3522 | return _PyLong_FromTime_t(sec); |
| 3523 | } |
| 3524 | |
| 3525 | static PyObject * |
| 3526 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
| 3527 | { |
| 3528 | PyObject *obj; |
| 3529 | time_t sec; |
| 3530 | long usec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3531 | int round; |
| 3532 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3533 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3534 | if (check_time_rounding(round) < 0) |
| 3535 | return NULL; |
| 3536 | if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3537 | return NULL; |
| 3538 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
| 3539 | } |
| 3540 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3541 | static PyObject * |
| 3542 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
| 3543 | { |
| 3544 | PyObject *obj; |
| 3545 | time_t sec; |
| 3546 | long nsec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3547 | int round; |
| 3548 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3549 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3550 | if (check_time_rounding(round) < 0) |
| 3551 | return NULL; |
| 3552 | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3553 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3554 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3555 | } |
| 3556 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3557 | static void |
| 3558 | slot_tp_del(PyObject *self) |
| 3559 | { |
| 3560 | _Py_IDENTIFIER(__tp_del__); |
| 3561 | PyObject *del, *res; |
| 3562 | PyObject *error_type, *error_value, *error_traceback; |
| 3563 | |
| 3564 | /* Temporarily resurrect the object. */ |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3565 | assert(Py_REFCNT(self) == 0); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3566 | Py_SET_REFCNT(self, 1); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3567 | |
| 3568 | /* Save the current exception, if any. */ |
| 3569 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 3570 | |
| 3571 | /* Execute __del__ method, if any. */ |
| 3572 | del = _PyObject_LookupSpecial(self, &PyId___tp_del__); |
| 3573 | if (del != NULL) { |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 3574 | res = _PyObject_CallNoArg(del); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3575 | if (res == NULL) |
| 3576 | PyErr_WriteUnraisable(del); |
| 3577 | else |
| 3578 | Py_DECREF(res); |
| 3579 | Py_DECREF(del); |
| 3580 | } |
| 3581 | |
| 3582 | /* Restore the saved exception. */ |
| 3583 | PyErr_Restore(error_type, error_value, error_traceback); |
| 3584 | |
| 3585 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 3586 | * cause a recursive call. |
| 3587 | */ |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3588 | assert(Py_REFCNT(self) > 0); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3589 | Py_SET_REFCNT(self, Py_REFCNT(self) - 1); |
| 3590 | if (Py_REFCNT(self) == 0) { |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3591 | /* this is the normal path out */ |
| 3592 | return; |
| 3593 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3594 | |
| 3595 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 3596 | * never happened. |
| 3597 | */ |
| 3598 | { |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3599 | Py_ssize_t refcnt = Py_REFCNT(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3600 | _Py_NewReference(self); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3601 | Py_SET_REFCNT(self, refcnt); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3602 | } |
Pablo Galindo | f13072b | 2020-04-11 01:21:54 +0100 | [diff] [blame] | 3603 | assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self)); |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 3604 | /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased |
| 3605 | _Py_RefTotal, so we need to undo that. */ |
| 3606 | #ifdef Py_REF_DEBUG |
| 3607 | _Py_RefTotal--; |
| 3608 | #endif |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | static PyObject * |
| 3612 | with_tp_del(PyObject *self, PyObject *args) |
| 3613 | { |
| 3614 | PyObject *obj; |
| 3615 | PyTypeObject *tp; |
| 3616 | |
| 3617 | if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) |
| 3618 | return NULL; |
| 3619 | tp = (PyTypeObject *) obj; |
| 3620 | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
| 3621 | PyErr_Format(PyExc_TypeError, |
| 3622 | "heap type expected, got %R", obj); |
| 3623 | return NULL; |
| 3624 | } |
| 3625 | tp->tp_del = slot_tp_del; |
| 3626 | Py_INCREF(obj); |
| 3627 | return obj; |
| 3628 | } |
| 3629 | |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 3630 | static PyMethodDef ml; |
| 3631 | |
| 3632 | static PyObject * |
| 3633 | create_cfunction(PyObject *self, PyObject *args) |
| 3634 | { |
| 3635 | return PyCFunction_NewEx(&ml, self, NULL); |
| 3636 | } |
| 3637 | |
| 3638 | static PyMethodDef ml = { |
| 3639 | "create_cfunction", |
| 3640 | create_cfunction, |
| 3641 | METH_NOARGS, |
| 3642 | NULL |
| 3643 | }; |
| 3644 | |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3645 | static PyObject * |
| 3646 | _test_incref(PyObject *ob) |
| 3647 | { |
| 3648 | Py_INCREF(ob); |
| 3649 | return ob; |
| 3650 | } |
| 3651 | |
| 3652 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3653 | test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3654 | { |
| 3655 | PyObject *obj = PyLong_FromLong(0); |
| 3656 | Py_XINCREF(_test_incref(obj)); |
| 3657 | Py_DECREF(obj); |
| 3658 | Py_DECREF(obj); |
| 3659 | Py_DECREF(obj); |
| 3660 | Py_RETURN_NONE; |
| 3661 | } |
| 3662 | |
| 3663 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3664 | test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3665 | { |
| 3666 | PyObject *obj = PyLong_FromLong(0); |
| 3667 | Py_INCREF(_test_incref(obj)); |
| 3668 | Py_DECREF(obj); |
| 3669 | Py_DECREF(obj); |
| 3670 | Py_DECREF(obj); |
| 3671 | Py_RETURN_NONE; |
| 3672 | } |
| 3673 | |
| 3674 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3675 | test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3676 | { |
| 3677 | Py_XDECREF(PyLong_FromLong(0)); |
| 3678 | Py_RETURN_NONE; |
| 3679 | } |
| 3680 | |
| 3681 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3682 | test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3683 | { |
| 3684 | Py_DECREF(PyLong_FromLong(0)); |
| 3685 | Py_RETURN_NONE; |
| 3686 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3687 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3688 | static PyObject * |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 3689 | test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self), |
| 3690 | PyObject *Py_UNUSED(args)) |
| 3691 | { |
| 3692 | PyStructSequence_Desc descr; |
| 3693 | PyStructSequence_Field descr_fields[3]; |
| 3694 | |
| 3695 | descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"}; |
| 3696 | descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"}; |
| 3697 | descr_fields[2] = (PyStructSequence_Field){0, NULL}; |
| 3698 | |
| 3699 | descr.name = "_testcapi.test_descr"; |
| 3700 | descr.doc = "This is used to test for memory leaks in NewType"; |
| 3701 | descr.fields = descr_fields; |
| 3702 | descr.n_in_sequence = 1; |
| 3703 | |
| 3704 | PyTypeObject* structseq_type = PyStructSequence_NewType(&descr); |
| 3705 | assert(structseq_type != NULL); |
| 3706 | assert(PyType_Check(structseq_type)); |
| 3707 | assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS)); |
| 3708 | Py_DECREF(structseq_type); |
| 3709 | |
| 3710 | Py_RETURN_NONE; |
| 3711 | } |
| 3712 | |
| 3713 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3714 | test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3715 | { |
| 3716 | PyObject *obj = PyLong_FromLong(0); |
Victor Stinner | fc6a90a | 2014-10-09 22:15:41 +0200 | [diff] [blame] | 3717 | Py_IncRef(obj); |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3718 | Py_DecRef(obj); |
| 3719 | Py_DecRef(obj); |
| 3720 | Py_RETURN_NONE; |
| 3721 | } |
| 3722 | |
| 3723 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3724 | test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3725 | { |
| 3726 | void *ptr; |
| 3727 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3728 | ptr = PyMem_RawMalloc(0); |
| 3729 | if (ptr == NULL) { |
| 3730 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); |
| 3731 | return NULL; |
| 3732 | } |
| 3733 | PyMem_RawFree(ptr); |
| 3734 | |
| 3735 | ptr = PyMem_RawCalloc(0, 0); |
| 3736 | if (ptr == NULL) { |
| 3737 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); |
| 3738 | return NULL; |
| 3739 | } |
| 3740 | PyMem_RawFree(ptr); |
| 3741 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3742 | ptr = PyMem_Malloc(0); |
| 3743 | if (ptr == NULL) { |
| 3744 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); |
| 3745 | return NULL; |
| 3746 | } |
| 3747 | PyMem_Free(ptr); |
| 3748 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3749 | ptr = PyMem_Calloc(0, 0); |
| 3750 | if (ptr == NULL) { |
| 3751 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); |
| 3752 | return NULL; |
| 3753 | } |
| 3754 | PyMem_Free(ptr); |
| 3755 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3756 | ptr = PyObject_Malloc(0); |
| 3757 | if (ptr == NULL) { |
| 3758 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); |
| 3759 | return NULL; |
| 3760 | } |
| 3761 | PyObject_Free(ptr); |
| 3762 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3763 | ptr = PyObject_Calloc(0, 0); |
| 3764 | if (ptr == NULL) { |
| 3765 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); |
| 3766 | return NULL; |
| 3767 | } |
| 3768 | PyObject_Free(ptr); |
| 3769 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3770 | Py_RETURN_NONE; |
| 3771 | } |
| 3772 | |
| 3773 | typedef struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3774 | PyMemAllocatorEx alloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3775 | |
| 3776 | size_t malloc_size; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3777 | size_t calloc_nelem; |
| 3778 | size_t calloc_elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3779 | void *realloc_ptr; |
| 3780 | size_t realloc_new_size; |
| 3781 | void *free_ptr; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3782 | void *ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3783 | } alloc_hook_t; |
| 3784 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3785 | static void* hook_malloc(void* ctx, size_t size) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3786 | { |
| 3787 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3788 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3789 | hook->malloc_size = size; |
| 3790 | return hook->alloc.malloc(hook->alloc.ctx, size); |
| 3791 | } |
| 3792 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3793 | static void* hook_calloc(void* ctx, size_t nelem, size_t elsize) |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3794 | { |
| 3795 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3796 | hook->ctx = ctx; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3797 | hook->calloc_nelem = nelem; |
| 3798 | hook->calloc_elsize = elsize; |
| 3799 | return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); |
| 3800 | } |
| 3801 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3802 | static void* hook_realloc(void* ctx, void* ptr, size_t new_size) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3803 | { |
| 3804 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3805 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3806 | hook->realloc_ptr = ptr; |
| 3807 | hook->realloc_new_size = new_size; |
| 3808 | return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); |
| 3809 | } |
| 3810 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3811 | static void hook_free(void *ctx, void *ptr) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3812 | { |
| 3813 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3814 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3815 | hook->free_ptr = ptr; |
| 3816 | hook->alloc.free(hook->alloc.ctx, ptr); |
| 3817 | } |
| 3818 | |
| 3819 | static PyObject * |
| 3820 | test_setallocators(PyMemAllocatorDomain domain) |
| 3821 | { |
| 3822 | PyObject *res = NULL; |
| 3823 | const char *error_msg; |
| 3824 | alloc_hook_t hook; |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3825 | PyMemAllocatorEx alloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3826 | size_t size, size2, nelem, elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3827 | void *ptr, *ptr2; |
| 3828 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3829 | memset(&hook, 0, sizeof(hook)); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3830 | |
| 3831 | alloc.ctx = &hook; |
| 3832 | alloc.malloc = &hook_malloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3833 | alloc.calloc = &hook_calloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3834 | alloc.realloc = &hook_realloc; |
| 3835 | alloc.free = &hook_free; |
| 3836 | PyMem_GetAllocator(domain, &hook.alloc); |
| 3837 | PyMem_SetAllocator(domain, &alloc); |
| 3838 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3839 | /* malloc, realloc, free */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3840 | size = 42; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3841 | hook.ctx = NULL; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3842 | switch(domain) |
| 3843 | { |
| 3844 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; |
| 3845 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; |
| 3846 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; |
| 3847 | default: ptr = NULL; break; |
| 3848 | } |
| 3849 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3850 | #define CHECK_CTX(FUNC) \ |
| 3851 | if (hook.ctx != &hook) { \ |
| 3852 | error_msg = FUNC " wrong context"; \ |
| 3853 | goto fail; \ |
| 3854 | } \ |
| 3855 | hook.ctx = NULL; /* reset for next check */ |
| 3856 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3857 | if (ptr == NULL) { |
| 3858 | error_msg = "malloc failed"; |
| 3859 | goto fail; |
| 3860 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3861 | CHECK_CTX("malloc"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3862 | if (hook.malloc_size != size) { |
| 3863 | error_msg = "malloc invalid size"; |
| 3864 | goto fail; |
| 3865 | } |
| 3866 | |
| 3867 | size2 = 200; |
| 3868 | switch(domain) |
| 3869 | { |
| 3870 | case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; |
| 3871 | case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; |
| 3872 | case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; |
Christian Heimes | 865d12a | 2013-08-02 11:10:51 +0200 | [diff] [blame] | 3873 | default: ptr2 = NULL; break; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | if (ptr2 == NULL) { |
| 3877 | error_msg = "realloc failed"; |
| 3878 | goto fail; |
| 3879 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3880 | CHECK_CTX("realloc"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3881 | if (hook.realloc_ptr != ptr |
| 3882 | || hook.realloc_new_size != size2) { |
| 3883 | error_msg = "realloc invalid parameters"; |
| 3884 | goto fail; |
| 3885 | } |
| 3886 | |
| 3887 | switch(domain) |
| 3888 | { |
| 3889 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; |
| 3890 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; |
| 3891 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; |
| 3892 | } |
| 3893 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3894 | CHECK_CTX("free"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3895 | if (hook.free_ptr != ptr2) { |
| 3896 | error_msg = "free invalid pointer"; |
| 3897 | goto fail; |
| 3898 | } |
| 3899 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3900 | /* calloc, free */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3901 | nelem = 2; |
| 3902 | elsize = 5; |
| 3903 | switch(domain) |
| 3904 | { |
| 3905 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; |
| 3906 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; |
| 3907 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; |
| 3908 | default: ptr = NULL; break; |
| 3909 | } |
| 3910 | |
| 3911 | if (ptr == NULL) { |
| 3912 | error_msg = "calloc failed"; |
| 3913 | goto fail; |
| 3914 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3915 | CHECK_CTX("calloc"); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3916 | if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { |
| 3917 | error_msg = "calloc invalid nelem or elsize"; |
| 3918 | goto fail; |
| 3919 | } |
| 3920 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3921 | hook.free_ptr = NULL; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3922 | switch(domain) |
| 3923 | { |
| 3924 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; |
| 3925 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; |
| 3926 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; |
| 3927 | } |
| 3928 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3929 | CHECK_CTX("calloc free"); |
| 3930 | if (hook.free_ptr != ptr) { |
| 3931 | error_msg = "calloc free invalid pointer"; |
| 3932 | goto fail; |
| 3933 | } |
| 3934 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3935 | Py_INCREF(Py_None); |
| 3936 | res = Py_None; |
| 3937 | goto finally; |
| 3938 | |
| 3939 | fail: |
| 3940 | PyErr_SetString(PyExc_RuntimeError, error_msg); |
| 3941 | |
| 3942 | finally: |
| 3943 | PyMem_SetAllocator(domain, &hook.alloc); |
| 3944 | return res; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3945 | |
| 3946 | #undef CHECK_CTX |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3950 | test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3951 | { |
| 3952 | return test_setallocators(PYMEM_DOMAIN_RAW); |
| 3953 | } |
| 3954 | |
| 3955 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3956 | test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3957 | { |
| 3958 | return test_setallocators(PYMEM_DOMAIN_MEM); |
| 3959 | } |
| 3960 | |
| 3961 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3962 | test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3963 | { |
| 3964 | return test_setallocators(PYMEM_DOMAIN_OBJ); |
| 3965 | } |
| 3966 | |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 3967 | /* Most part of the following code is inherited from the pyfailmalloc project |
| 3968 | * written by Victor Stinner. */ |
| 3969 | static struct { |
| 3970 | int installed; |
| 3971 | PyMemAllocatorEx raw; |
| 3972 | PyMemAllocatorEx mem; |
| 3973 | PyMemAllocatorEx obj; |
| 3974 | } FmHook; |
| 3975 | |
| 3976 | static struct { |
| 3977 | int start; |
| 3978 | int stop; |
| 3979 | Py_ssize_t count; |
| 3980 | } FmData; |
| 3981 | |
| 3982 | static int |
| 3983 | fm_nomemory(void) |
| 3984 | { |
| 3985 | FmData.count++; |
| 3986 | if (FmData.count > FmData.start && |
| 3987 | (FmData.stop <= 0 || FmData.count <= FmData.stop)) { |
| 3988 | return 1; |
| 3989 | } |
| 3990 | return 0; |
| 3991 | } |
| 3992 | |
| 3993 | static void * |
| 3994 | hook_fmalloc(void *ctx, size_t size) |
| 3995 | { |
| 3996 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3997 | if (fm_nomemory()) { |
| 3998 | return NULL; |
| 3999 | } |
| 4000 | return alloc->malloc(alloc->ctx, size); |
| 4001 | } |
| 4002 | |
| 4003 | static void * |
| 4004 | hook_fcalloc(void *ctx, size_t nelem, size_t elsize) |
| 4005 | { |
| 4006 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4007 | if (fm_nomemory()) { |
| 4008 | return NULL; |
| 4009 | } |
| 4010 | return alloc->calloc(alloc->ctx, nelem, elsize); |
| 4011 | } |
| 4012 | |
| 4013 | static void * |
| 4014 | hook_frealloc(void *ctx, void *ptr, size_t new_size) |
| 4015 | { |
| 4016 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4017 | if (fm_nomemory()) { |
| 4018 | return NULL; |
| 4019 | } |
| 4020 | return alloc->realloc(alloc->ctx, ptr, new_size); |
| 4021 | } |
| 4022 | |
| 4023 | static void |
| 4024 | hook_ffree(void *ctx, void *ptr) |
| 4025 | { |
| 4026 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4027 | alloc->free(alloc->ctx, ptr); |
| 4028 | } |
| 4029 | |
| 4030 | static void |
| 4031 | fm_setup_hooks(void) |
| 4032 | { |
| 4033 | PyMemAllocatorEx alloc; |
| 4034 | |
| 4035 | if (FmHook.installed) { |
| 4036 | return; |
| 4037 | } |
| 4038 | FmHook.installed = 1; |
| 4039 | |
| 4040 | alloc.malloc = hook_fmalloc; |
| 4041 | alloc.calloc = hook_fcalloc; |
| 4042 | alloc.realloc = hook_frealloc; |
| 4043 | alloc.free = hook_ffree; |
| 4044 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 4045 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 4046 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 4047 | |
| 4048 | alloc.ctx = &FmHook.raw; |
| 4049 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 4050 | |
| 4051 | alloc.ctx = &FmHook.mem; |
| 4052 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 4053 | |
| 4054 | alloc.ctx = &FmHook.obj; |
| 4055 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 4056 | } |
| 4057 | |
| 4058 | static void |
| 4059 | fm_remove_hooks(void) |
| 4060 | { |
| 4061 | if (FmHook.installed) { |
| 4062 | FmHook.installed = 0; |
| 4063 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 4064 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 4065 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | static PyObject* |
| 4070 | set_nomemory(PyObject *self, PyObject *args) |
| 4071 | { |
| 4072 | /* Memory allocation fails after 'start' allocation requests, and until |
| 4073 | * 'stop' allocation requests except when 'stop' is negative or equal |
| 4074 | * to 0 (default) in which case allocation failures never stop. */ |
| 4075 | FmData.count = 0; |
| 4076 | FmData.stop = 0; |
| 4077 | if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) { |
| 4078 | return NULL; |
| 4079 | } |
| 4080 | fm_setup_hooks(); |
| 4081 | Py_RETURN_NONE; |
| 4082 | } |
| 4083 | |
| 4084 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4085 | remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored)) |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 4086 | { |
| 4087 | fm_remove_hooks(); |
| 4088 | Py_RETURN_NONE; |
| 4089 | } |
| 4090 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4091 | PyDoc_STRVAR(docstring_empty, |
| 4092 | "" |
| 4093 | ); |
| 4094 | |
| 4095 | PyDoc_STRVAR(docstring_no_signature, |
| 4096 | "This docstring has no signature." |
| 4097 | ); |
| 4098 | |
| 4099 | PyDoc_STRVAR(docstring_with_invalid_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4100 | "docstring_with_invalid_signature($module, /, boo)\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4101 | "\n" |
| 4102 | "This docstring has an invalid signature." |
| 4103 | ); |
| 4104 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4105 | PyDoc_STRVAR(docstring_with_invalid_signature2, |
| 4106 | "docstring_with_invalid_signature2($module, /, boo)\n" |
| 4107 | "\n" |
| 4108 | "--\n" |
| 4109 | "\n" |
| 4110 | "This docstring also has an invalid signature." |
| 4111 | ); |
| 4112 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4113 | PyDoc_STRVAR(docstring_with_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4114 | "docstring_with_signature($module, /, sig)\n" |
| 4115 | "--\n" |
| 4116 | "\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4117 | "This docstring has a valid signature." |
| 4118 | ); |
| 4119 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 4120 | PyDoc_STRVAR(docstring_with_signature_but_no_doc, |
| 4121 | "docstring_with_signature_but_no_doc($module, /, sig)\n" |
| 4122 | "--\n" |
| 4123 | "\n" |
| 4124 | ); |
| 4125 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4126 | PyDoc_STRVAR(docstring_with_signature_and_extra_newlines, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4127 | "docstring_with_signature_and_extra_newlines($module, /, parameter)\n" |
| 4128 | "--\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4129 | "\n" |
| 4130 | "\n" |
| 4131 | "This docstring has a valid signature and some extra newlines." |
| 4132 | ); |
| 4133 | |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4134 | PyDoc_STRVAR(docstring_with_signature_with_defaults, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4135 | "docstring_with_signature_with_defaults(module, s='avocado',\n" |
| 4136 | " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n" |
| 4137 | " local=the_number_three, sys=sys.maxsize,\n" |
| 4138 | " exp=sys.maxsize - 1)\n" |
| 4139 | "--\n" |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4140 | "\n" |
| 4141 | "\n" |
| 4142 | "\n" |
| 4143 | "This docstring has a valid signature with parameters,\n" |
| 4144 | "and the parameters take defaults of varying types." |
| 4145 | ); |
| 4146 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4147 | typedef struct { |
| 4148 | PyThread_type_lock start_event; |
| 4149 | PyThread_type_lock exit_event; |
| 4150 | PyObject *callback; |
| 4151 | } test_c_thread_t; |
| 4152 | |
| 4153 | static void |
| 4154 | temporary_c_thread(void *data) |
| 4155 | { |
| 4156 | test_c_thread_t *test_c_thread = data; |
| 4157 | PyGILState_STATE state; |
| 4158 | PyObject *res; |
| 4159 | |
| 4160 | PyThread_release_lock(test_c_thread->start_event); |
| 4161 | |
| 4162 | /* Allocate a Python thread state for this thread */ |
| 4163 | state = PyGILState_Ensure(); |
| 4164 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 4165 | res = _PyObject_CallNoArg(test_c_thread->callback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4166 | Py_CLEAR(test_c_thread->callback); |
| 4167 | |
| 4168 | if (res == NULL) { |
| 4169 | PyErr_Print(); |
| 4170 | } |
| 4171 | else { |
| 4172 | Py_DECREF(res); |
| 4173 | } |
| 4174 | |
| 4175 | /* Destroy the Python thread state for this thread */ |
| 4176 | PyGILState_Release(state); |
| 4177 | |
| 4178 | PyThread_release_lock(test_c_thread->exit_event); |
| 4179 | |
| 4180 | PyThread_exit_thread(); |
| 4181 | } |
| 4182 | |
| 4183 | static PyObject * |
| 4184 | call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
| 4185 | { |
| 4186 | PyObject *res = NULL; |
| 4187 | test_c_thread_t test_c_thread; |
| 4188 | long thread; |
| 4189 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4190 | test_c_thread.start_event = PyThread_allocate_lock(); |
| 4191 | test_c_thread.exit_event = PyThread_allocate_lock(); |
| 4192 | test_c_thread.callback = NULL; |
| 4193 | if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
| 4194 | PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
| 4195 | goto exit; |
| 4196 | } |
| 4197 | |
| 4198 | Py_INCREF(callback); |
| 4199 | test_c_thread.callback = callback; |
| 4200 | |
| 4201 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 4202 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 4203 | |
| 4204 | thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
| 4205 | if (thread == -1) { |
| 4206 | PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
| 4207 | PyThread_release_lock(test_c_thread.start_event); |
| 4208 | PyThread_release_lock(test_c_thread.exit_event); |
| 4209 | goto exit; |
| 4210 | } |
| 4211 | |
| 4212 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 4213 | PyThread_release_lock(test_c_thread.start_event); |
| 4214 | |
| 4215 | Py_BEGIN_ALLOW_THREADS |
| 4216 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 4217 | PyThread_release_lock(test_c_thread.exit_event); |
| 4218 | Py_END_ALLOW_THREADS |
| 4219 | |
| 4220 | Py_INCREF(Py_None); |
| 4221 | res = Py_None; |
| 4222 | |
| 4223 | exit: |
| 4224 | Py_CLEAR(test_c_thread.callback); |
| 4225 | if (test_c_thread.start_event) |
| 4226 | PyThread_free_lock(test_c_thread.start_event); |
| 4227 | if (test_c_thread.exit_event) |
| 4228 | PyThread_free_lock(test_c_thread.exit_event); |
| 4229 | return res; |
| 4230 | } |
Victor Stinner | 1310510 | 2013-12-13 02:17:29 +0100 | [diff] [blame] | 4231 | |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4232 | /* marshal */ |
| 4233 | |
| 4234 | static PyObject* |
| 4235 | pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
| 4236 | { |
| 4237 | long value; |
| 4238 | char *filename; |
| 4239 | int version; |
| 4240 | FILE *fp; |
| 4241 | |
| 4242 | if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", |
| 4243 | &value, &filename, &version)) |
| 4244 | return NULL; |
| 4245 | |
| 4246 | fp = fopen(filename, "wb"); |
| 4247 | if (fp == NULL) { |
| 4248 | PyErr_SetFromErrno(PyExc_OSError); |
| 4249 | return NULL; |
| 4250 | } |
| 4251 | |
| 4252 | PyMarshal_WriteLongToFile(value, fp, version); |
| 4253 | |
| 4254 | fclose(fp); |
| 4255 | if (PyErr_Occurred()) |
| 4256 | return NULL; |
| 4257 | Py_RETURN_NONE; |
| 4258 | } |
| 4259 | |
| 4260 | static PyObject* |
| 4261 | pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
| 4262 | { |
| 4263 | PyObject *obj; |
| 4264 | char *filename; |
| 4265 | int version; |
| 4266 | FILE *fp; |
| 4267 | |
| 4268 | if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", |
| 4269 | &obj, &filename, &version)) |
| 4270 | return NULL; |
| 4271 | |
| 4272 | fp = fopen(filename, "wb"); |
| 4273 | if (fp == NULL) { |
| 4274 | PyErr_SetFromErrno(PyExc_OSError); |
| 4275 | return NULL; |
| 4276 | } |
| 4277 | |
| 4278 | PyMarshal_WriteObjectToFile(obj, fp, version); |
| 4279 | |
| 4280 | fclose(fp); |
| 4281 | if (PyErr_Occurred()) |
| 4282 | return NULL; |
| 4283 | Py_RETURN_NONE; |
| 4284 | } |
| 4285 | |
| 4286 | static PyObject* |
| 4287 | pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
| 4288 | { |
| 4289 | int value; |
| 4290 | long pos; |
| 4291 | char *filename; |
| 4292 | FILE *fp; |
| 4293 | |
| 4294 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) |
| 4295 | return NULL; |
| 4296 | |
| 4297 | fp = fopen(filename, "rb"); |
| 4298 | if (fp == NULL) { |
| 4299 | PyErr_SetFromErrno(PyExc_OSError); |
| 4300 | return NULL; |
| 4301 | } |
| 4302 | |
| 4303 | value = PyMarshal_ReadShortFromFile(fp); |
| 4304 | pos = ftell(fp); |
| 4305 | |
| 4306 | fclose(fp); |
| 4307 | if (PyErr_Occurred()) |
| 4308 | return NULL; |
| 4309 | return Py_BuildValue("il", value, pos); |
| 4310 | } |
| 4311 | |
| 4312 | static PyObject* |
| 4313 | pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
| 4314 | { |
| 4315 | long value, pos; |
| 4316 | char *filename; |
| 4317 | FILE *fp; |
| 4318 | |
| 4319 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) |
| 4320 | return NULL; |
| 4321 | |
| 4322 | fp = fopen(filename, "rb"); |
| 4323 | if (fp == NULL) { |
| 4324 | PyErr_SetFromErrno(PyExc_OSError); |
| 4325 | return NULL; |
| 4326 | } |
| 4327 | |
| 4328 | value = PyMarshal_ReadLongFromFile(fp); |
| 4329 | pos = ftell(fp); |
| 4330 | |
| 4331 | fclose(fp); |
| 4332 | if (PyErr_Occurred()) |
| 4333 | return NULL; |
| 4334 | return Py_BuildValue("ll", value, pos); |
| 4335 | } |
| 4336 | |
| 4337 | static PyObject* |
| 4338 | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
| 4339 | { |
| 4340 | PyObject *obj; |
| 4341 | long pos; |
| 4342 | char *filename; |
| 4343 | FILE *fp; |
| 4344 | |
| 4345 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) |
| 4346 | return NULL; |
| 4347 | |
| 4348 | fp = fopen(filename, "rb"); |
| 4349 | if (fp == NULL) { |
| 4350 | PyErr_SetFromErrno(PyExc_OSError); |
| 4351 | return NULL; |
| 4352 | } |
| 4353 | |
| 4354 | obj = PyMarshal_ReadLastObjectFromFile(fp); |
| 4355 | pos = ftell(fp); |
| 4356 | |
| 4357 | fclose(fp); |
| 4358 | return Py_BuildValue("Nl", obj, pos); |
| 4359 | } |
| 4360 | |
| 4361 | static PyObject* |
| 4362 | pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
| 4363 | { |
| 4364 | PyObject *obj; |
| 4365 | long pos; |
| 4366 | char *filename; |
| 4367 | FILE *fp; |
| 4368 | |
| 4369 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) |
| 4370 | return NULL; |
| 4371 | |
| 4372 | fp = fopen(filename, "rb"); |
| 4373 | if (fp == NULL) { |
| 4374 | PyErr_SetFromErrno(PyExc_OSError); |
| 4375 | return NULL; |
| 4376 | } |
| 4377 | |
| 4378 | obj = PyMarshal_ReadObjectFromFile(fp); |
| 4379 | pos = ftell(fp); |
| 4380 | |
| 4381 | fclose(fp); |
| 4382 | return Py_BuildValue("Nl", obj, pos); |
| 4383 | } |
| 4384 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4385 | static PyObject* |
| 4386 | return_null_without_error(PyObject *self, PyObject *args) |
| 4387 | { |
| 4388 | /* invalid call: return NULL without setting an error, |
| 4389 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 4390 | PyErr_Clear(); |
| 4391 | return NULL; |
| 4392 | } |
| 4393 | |
| 4394 | static PyObject* |
| 4395 | return_result_with_error(PyObject *self, PyObject *args) |
| 4396 | { |
| 4397 | /* invalid call: return a result with an error set, |
| 4398 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 4399 | PyErr_SetNone(PyExc_ValueError); |
| 4400 | Py_RETURN_NONE; |
| 4401 | } |
| 4402 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 4403 | static PyObject * |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 4404 | test_pytime_fromseconds(PyObject *self, PyObject *args) |
| 4405 | { |
| 4406 | int seconds; |
| 4407 | _PyTime_t ts; |
| 4408 | |
| 4409 | if (!PyArg_ParseTuple(args, "i", &seconds)) |
| 4410 | return NULL; |
| 4411 | ts = _PyTime_FromSeconds(seconds); |
| 4412 | return _PyTime_AsNanosecondsObject(ts); |
| 4413 | } |
| 4414 | |
| 4415 | static PyObject * |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 4416 | test_pytime_fromsecondsobject(PyObject *self, PyObject *args) |
| 4417 | { |
| 4418 | PyObject *obj; |
| 4419 | int round; |
| 4420 | _PyTime_t ts; |
| 4421 | |
| 4422 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
| 4423 | return NULL; |
| 4424 | if (check_time_rounding(round) < 0) |
| 4425 | return NULL; |
| 4426 | if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) |
| 4427 | return NULL; |
| 4428 | return _PyTime_AsNanosecondsObject(ts); |
| 4429 | } |
| 4430 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4431 | static PyObject * |
| 4432 | test_pytime_assecondsdouble(PyObject *self, PyObject *args) |
| 4433 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4434 | PyObject *obj; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4435 | _PyTime_t ts; |
| 4436 | double d; |
| 4437 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4438 | if (!PyArg_ParseTuple(args, "O", &obj)) { |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4439 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4440 | } |
| 4441 | if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) { |
| 4442 | return NULL; |
| 4443 | } |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4444 | d = _PyTime_AsSecondsDouble(ts); |
| 4445 | return PyFloat_FromDouble(d); |
| 4446 | } |
| 4447 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4448 | static PyObject * |
| 4449 | test_PyTime_AsTimeval(PyObject *self, PyObject *args) |
| 4450 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4451 | PyObject *obj; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4452 | int round; |
| 4453 | _PyTime_t t; |
| 4454 | struct timeval tv; |
| 4455 | PyObject *seconds; |
| 4456 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4457 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4458 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4459 | if (check_time_rounding(round) < 0) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4460 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4461 | } |
| 4462 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4463 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4464 | } |
| 4465 | if (_PyTime_AsTimeval(t, &tv, round) < 0) { |
| 4466 | return NULL; |
| 4467 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4468 | |
Benjamin Peterson | 2c134c3 | 2017-04-13 01:44:54 -0700 | [diff] [blame] | 4469 | seconds = PyLong_FromLongLong(tv.tv_sec); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4470 | if (seconds == NULL) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4471 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4472 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4473 | return Py_BuildValue("Nl", seconds, tv.tv_usec); |
| 4474 | } |
| 4475 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4476 | #ifdef HAVE_CLOCK_GETTIME |
| 4477 | static PyObject * |
| 4478 | test_PyTime_AsTimespec(PyObject *self, PyObject *args) |
| 4479 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4480 | PyObject *obj; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4481 | _PyTime_t t; |
| 4482 | struct timespec ts; |
| 4483 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4484 | if (!PyArg_ParseTuple(args, "O", &obj)) { |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4485 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4486 | } |
| 4487 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4488 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4489 | } |
| 4490 | if (_PyTime_AsTimespec(t, &ts) == -1) { |
| 4491 | return NULL; |
| 4492 | } |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4493 | return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); |
| 4494 | } |
| 4495 | #endif |
| 4496 | |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4497 | static PyObject * |
| 4498 | test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) |
| 4499 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4500 | PyObject *obj; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4501 | int round; |
| 4502 | _PyTime_t t, ms; |
| 4503 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4504 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4505 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4506 | } |
| 4507 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4508 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4509 | } |
| 4510 | if (check_time_rounding(round) < 0) { |
| 4511 | return NULL; |
| 4512 | } |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4513 | ms = _PyTime_AsMilliseconds(t, round); |
| 4514 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4515 | nanoseconds */ |
| 4516 | return _PyTime_AsNanosecondsObject(ms); |
| 4517 | } |
| 4518 | |
| 4519 | static PyObject * |
| 4520 | test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) |
| 4521 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4522 | PyObject *obj; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4523 | int round; |
| 4524 | _PyTime_t t, ms; |
| 4525 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4526 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4527 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4528 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4529 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4530 | } |
| 4531 | if (check_time_rounding(round) < 0) { |
| 4532 | return NULL; |
| 4533 | } |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4534 | ms = _PyTime_AsMicroseconds(t, round); |
| 4535 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4536 | nanoseconds */ |
| 4537 | return _PyTime_AsNanosecondsObject(ms); |
| 4538 | } |
| 4539 | |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4540 | static PyObject* |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4541 | pymem_buffer_overflow(PyObject *self, PyObject *args) |
| 4542 | { |
| 4543 | char *buffer; |
| 4544 | |
| 4545 | /* Deliberate buffer overflow to check that PyMem_Free() detects |
| 4546 | the overflow when debug hooks are installed. */ |
| 4547 | buffer = PyMem_Malloc(16); |
Victor Stinner | 414b1cd | 2019-03-26 14:35:30 +0100 | [diff] [blame] | 4548 | if (buffer == NULL) { |
| 4549 | PyErr_NoMemory(); |
| 4550 | return NULL; |
| 4551 | } |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4552 | buffer[16] = 'x'; |
| 4553 | PyMem_Free(buffer); |
| 4554 | |
| 4555 | Py_RETURN_NONE; |
| 4556 | } |
| 4557 | |
| 4558 | static PyObject* |
| 4559 | pymem_api_misuse(PyObject *self, PyObject *args) |
| 4560 | { |
| 4561 | char *buffer; |
| 4562 | |
| 4563 | /* Deliberate misusage of Python allocators: |
| 4564 | allococate with PyMem but release with PyMem_Raw. */ |
| 4565 | buffer = PyMem_Malloc(16); |
| 4566 | PyMem_RawFree(buffer); |
| 4567 | |
| 4568 | Py_RETURN_NONE; |
| 4569 | } |
| 4570 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4571 | static PyObject* |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4572 | pymem_malloc_without_gil(PyObject *self, PyObject *args) |
| 4573 | { |
| 4574 | char *buffer; |
| 4575 | |
| 4576 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4577 | call PyMem_Malloc() without holding the GIL */ |
| 4578 | Py_BEGIN_ALLOW_THREADS |
| 4579 | buffer = PyMem_Malloc(10); |
| 4580 | Py_END_ALLOW_THREADS |
| 4581 | |
| 4582 | PyMem_Free(buffer); |
| 4583 | |
| 4584 | Py_RETURN_NONE; |
| 4585 | } |
| 4586 | |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 4587 | |
| 4588 | static PyObject* |
| 4589 | test_pymem_getallocatorsname(PyObject *self, PyObject *args) |
| 4590 | { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 4591 | const char *name = _PyMem_GetCurrentAllocatorName(); |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 4592 | if (name == NULL) { |
| 4593 | PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name"); |
| 4594 | return NULL; |
| 4595 | } |
| 4596 | return PyUnicode_FromString(name); |
| 4597 | } |
| 4598 | |
| 4599 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4600 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4601 | test_pyobject_is_freed(const char *test_name, PyObject *op) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4602 | { |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4603 | if (!_PyObject_IsFreed(op)) { |
| 4604 | return raiseTestError(test_name, "object is not seen as freed"); |
| 4605 | } |
| 4606 | Py_RETURN_NONE; |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | |
| 4610 | static PyObject* |
Victor Stinner | 6876257 | 2019-10-07 18:42:01 +0200 | [diff] [blame] | 4611 | check_pyobject_null_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
| 4612 | { |
| 4613 | PyObject *op = NULL; |
| 4614 | return test_pyobject_is_freed("check_pyobject_null_is_freed", op); |
| 4615 | } |
| 4616 | |
| 4617 | |
| 4618 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4619 | check_pyobject_uninitialized_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4620 | { |
| 4621 | PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject)); |
| 4622 | if (op == NULL) { |
| 4623 | return NULL; |
| 4624 | } |
| 4625 | /* Initialize reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4626 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4627 | /* object fields like ob_type are uninitialized! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4628 | return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | |
| 4632 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4633 | check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4634 | { |
| 4635 | /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */ |
| 4636 | PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type)); |
| 4637 | if (op == NULL) { |
| 4638 | return NULL; |
| 4639 | } |
| 4640 | /* Initialize reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4641 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4642 | /* ob_type field is after the memory block: part of "forbidden bytes" |
Min ho Kim | 39d87b5 | 2019-08-31 06:21:19 +1000 | [diff] [blame] | 4643 | when using debug hooks on memory allocators! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4644 | return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | |
| 4648 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4649 | check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4650 | { |
| 4651 | PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); |
| 4652 | if (op == NULL) { |
| 4653 | return NULL; |
| 4654 | } |
| 4655 | Py_TYPE(op)->tp_dealloc(op); |
| 4656 | /* Reset reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4657 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4658 | /* object memory is freed! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4659 | return test_pyobject_is_freed("check_pyobject_freed_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | |
| 4663 | static PyObject* |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4664 | pyobject_malloc_without_gil(PyObject *self, PyObject *args) |
| 4665 | { |
| 4666 | char *buffer; |
| 4667 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4668 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4669 | call PyObject_Malloc() without holding the GIL */ |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4670 | Py_BEGIN_ALLOW_THREADS |
| 4671 | buffer = PyObject_Malloc(10); |
| 4672 | Py_END_ALLOW_THREADS |
| 4673 | |
| 4674 | PyObject_Free(buffer); |
| 4675 | |
| 4676 | Py_RETURN_NONE; |
| 4677 | } |
| 4678 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4679 | static PyObject * |
| 4680 | tracemalloc_track(PyObject *self, PyObject *args) |
| 4681 | { |
| 4682 | unsigned int domain; |
| 4683 | PyObject *ptr_obj; |
| 4684 | void *ptr; |
| 4685 | Py_ssize_t size; |
| 4686 | int release_gil = 0; |
| 4687 | int res; |
| 4688 | |
| 4689 | if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil)) |
| 4690 | return NULL; |
| 4691 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4692 | if (PyErr_Occurred()) |
| 4693 | return NULL; |
| 4694 | |
| 4695 | if (release_gil) { |
| 4696 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4697 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4698 | Py_END_ALLOW_THREADS |
| 4699 | } |
| 4700 | else { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4701 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4705 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4706 | return NULL; |
| 4707 | } |
| 4708 | |
| 4709 | Py_RETURN_NONE; |
| 4710 | } |
| 4711 | |
| 4712 | static PyObject * |
| 4713 | tracemalloc_untrack(PyObject *self, PyObject *args) |
| 4714 | { |
| 4715 | unsigned int domain; |
| 4716 | PyObject *ptr_obj; |
| 4717 | void *ptr; |
| 4718 | int res; |
| 4719 | |
| 4720 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4721 | return NULL; |
| 4722 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4723 | if (PyErr_Occurred()) |
| 4724 | return NULL; |
| 4725 | |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4726 | res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4727 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4728 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4729 | return NULL; |
| 4730 | } |
| 4731 | |
| 4732 | Py_RETURN_NONE; |
| 4733 | } |
| 4734 | |
| 4735 | static PyObject * |
| 4736 | tracemalloc_get_traceback(PyObject *self, PyObject *args) |
| 4737 | { |
| 4738 | unsigned int domain; |
| 4739 | PyObject *ptr_obj; |
| 4740 | void *ptr; |
| 4741 | |
| 4742 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4743 | return NULL; |
| 4744 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4745 | if (PyErr_Occurred()) |
| 4746 | return NULL; |
| 4747 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 4748 | return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4749 | } |
| 4750 | |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4751 | static PyObject * |
| 4752 | dict_get_version(PyObject *self, PyObject *args) |
| 4753 | { |
| 4754 | PyDictObject *dict; |
| 4755 | uint64_t version; |
| 4756 | |
| 4757 | if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict)) |
| 4758 | return NULL; |
| 4759 | |
| 4760 | version = dict->ma_version_tag; |
| 4761 | |
Sergey Fedoseev | a9ed91e | 2019-10-21 11:49:48 +0500 | [diff] [blame] | 4762 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(version)); |
| 4763 | return PyLong_FromUnsignedLongLong((unsigned long long)version); |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4764 | } |
| 4765 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4766 | |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 4767 | static PyObject * |
| 4768 | raise_SIGINT_then_send_None(PyObject *self, PyObject *args) |
| 4769 | { |
| 4770 | PyGenObject *gen; |
| 4771 | |
| 4772 | if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen)) |
| 4773 | return NULL; |
| 4774 | |
| 4775 | /* This is used in a test to check what happens if a signal arrives just |
| 4776 | as we're in the process of entering a yield from chain (see |
| 4777 | bpo-30039). |
| 4778 | |
| 4779 | Needs to be done in C, because: |
| 4780 | - we don't have a Python wrapper for raise() |
| 4781 | - we need to make sure that the Python-level signal handler doesn't run |
| 4782 | *before* we enter the generator frame, which is impossible in Python |
| 4783 | because we check for signals before every bytecode operation. |
| 4784 | */ |
| 4785 | raise(SIGINT); |
| 4786 | return _PyGen_Send(gen, Py_None); |
| 4787 | } |
| 4788 | |
| 4789 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4790 | static int |
| 4791 | fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs) |
| 4792 | { |
| 4793 | if (args == Py_None) { |
| 4794 | *stack = NULL; |
| 4795 | *nargs = 0; |
| 4796 | } |
| 4797 | else if (PyTuple_Check(args)) { |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 4798 | *stack = ((PyTupleObject *)args)->ob_item; |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4799 | *nargs = PyTuple_GET_SIZE(args); |
| 4800 | } |
| 4801 | else { |
| 4802 | PyErr_SetString(PyExc_TypeError, "args must be None or a tuple"); |
| 4803 | return -1; |
| 4804 | } |
| 4805 | return 0; |
| 4806 | } |
| 4807 | |
| 4808 | |
| 4809 | static PyObject * |
| 4810 | test_pyobject_fastcall(PyObject *self, PyObject *args) |
| 4811 | { |
| 4812 | PyObject *func, *func_args; |
| 4813 | PyObject **stack; |
| 4814 | Py_ssize_t nargs; |
| 4815 | |
| 4816 | if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) { |
| 4817 | return NULL; |
| 4818 | } |
| 4819 | |
| 4820 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4821 | return NULL; |
| 4822 | } |
| 4823 | return _PyObject_FastCall(func, stack, nargs); |
| 4824 | } |
| 4825 | |
| 4826 | |
| 4827 | static PyObject * |
| 4828 | test_pyobject_fastcalldict(PyObject *self, PyObject *args) |
| 4829 | { |
| 4830 | PyObject *func, *func_args, *kwargs; |
| 4831 | PyObject **stack; |
| 4832 | Py_ssize_t nargs; |
| 4833 | |
| 4834 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) { |
| 4835 | return NULL; |
| 4836 | } |
| 4837 | |
| 4838 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4839 | return NULL; |
| 4840 | } |
| 4841 | |
| 4842 | if (kwargs == Py_None) { |
| 4843 | kwargs = NULL; |
| 4844 | } |
| 4845 | else if (!PyDict_Check(kwargs)) { |
| 4846 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict"); |
| 4847 | return NULL; |
| 4848 | } |
| 4849 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 4850 | return PyObject_VectorcallDict(func, stack, nargs, kwargs); |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | |
| 4854 | static PyObject * |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4855 | test_pyobject_vectorcall(PyObject *self, PyObject *args) |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4856 | { |
| 4857 | PyObject *func, *func_args, *kwnames = NULL; |
| 4858 | PyObject **stack; |
| 4859 | Py_ssize_t nargs, nkw; |
| 4860 | |
| 4861 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) { |
| 4862 | return NULL; |
| 4863 | } |
| 4864 | |
| 4865 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4866 | return NULL; |
| 4867 | } |
| 4868 | |
| 4869 | if (kwnames == Py_None) { |
| 4870 | kwnames = NULL; |
| 4871 | } |
| 4872 | else if (PyTuple_Check(kwnames)) { |
| 4873 | nkw = PyTuple_GET_SIZE(kwnames); |
| 4874 | if (nargs < nkw) { |
| 4875 | PyErr_SetString(PyExc_ValueError, "kwnames longer than args"); |
| 4876 | return NULL; |
| 4877 | } |
| 4878 | nargs -= nkw; |
| 4879 | } |
| 4880 | else { |
| 4881 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple"); |
| 4882 | return NULL; |
| 4883 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 4884 | return PyObject_Vectorcall(func, stack, nargs, kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | |
| 4888 | static PyObject * |
| 4889 | test_pyvectorcall_call(PyObject *self, PyObject *args) |
| 4890 | { |
| 4891 | PyObject *func; |
| 4892 | PyObject *argstuple; |
| 4893 | PyObject *kwargs = NULL; |
| 4894 | |
| 4895 | if (!PyArg_ParseTuple(args, "OO|O", &func, &argstuple, &kwargs)) { |
| 4896 | return NULL; |
| 4897 | } |
| 4898 | |
| 4899 | if (!PyTuple_Check(argstuple)) { |
| 4900 | PyErr_SetString(PyExc_TypeError, "args must be a tuple"); |
| 4901 | return NULL; |
| 4902 | } |
| 4903 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
| 4904 | PyErr_SetString(PyExc_TypeError, "kwargs must be a dict"); |
| 4905 | return NULL; |
| 4906 | } |
| 4907 | |
| 4908 | return PyVectorcall_Call(func, argstuple, kwargs); |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4909 | } |
| 4910 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4911 | |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 4912 | static PyObject* |
| 4913 | stack_pointer(PyObject *self, PyObject *args) |
| 4914 | { |
| 4915 | int v = 5; |
| 4916 | return PyLong_FromVoidPtr(&v); |
| 4917 | } |
| 4918 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4919 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4920 | #ifdef W_STOPCODE |
| 4921 | static PyObject* |
| 4922 | py_w_stopcode(PyObject *self, PyObject *args) |
| 4923 | { |
| 4924 | int sig, status; |
| 4925 | if (!PyArg_ParseTuple(args, "i", &sig)) { |
| 4926 | return NULL; |
| 4927 | } |
| 4928 | status = W_STOPCODE(sig); |
| 4929 | return PyLong_FromLong(status); |
| 4930 | } |
| 4931 | #endif |
| 4932 | |
| 4933 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 4934 | static PyObject * |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 4935 | get_mapping_keys(PyObject* self, PyObject *obj) |
| 4936 | { |
| 4937 | return PyMapping_Keys(obj); |
| 4938 | } |
| 4939 | |
| 4940 | static PyObject * |
| 4941 | get_mapping_values(PyObject* self, PyObject *obj) |
| 4942 | { |
| 4943 | return PyMapping_Values(obj); |
| 4944 | } |
| 4945 | |
| 4946 | static PyObject * |
| 4947 | get_mapping_items(PyObject* self, PyObject *obj) |
| 4948 | { |
| 4949 | return PyMapping_Items(obj); |
| 4950 | } |
| 4951 | |
| 4952 | |
| 4953 | static PyObject * |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 4954 | test_pythread_tss_key_state(PyObject *self, PyObject *args) |
| 4955 | { |
| 4956 | Py_tss_t tss_key = Py_tss_NEEDS_INIT; |
| 4957 | if (PyThread_tss_is_created(&tss_key)) { |
| 4958 | return raiseTestError("test_pythread_tss_key_state", |
| 4959 | "TSS key not in an uninitialized state at " |
| 4960 | "creation time"); |
| 4961 | } |
| 4962 | if (PyThread_tss_create(&tss_key) != 0) { |
| 4963 | PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed"); |
| 4964 | return NULL; |
| 4965 | } |
| 4966 | if (!PyThread_tss_is_created(&tss_key)) { |
| 4967 | return raiseTestError("test_pythread_tss_key_state", |
| 4968 | "PyThread_tss_create succeeded, " |
| 4969 | "but with TSS key in an uninitialized state"); |
| 4970 | } |
| 4971 | if (PyThread_tss_create(&tss_key) != 0) { |
| 4972 | return raiseTestError("test_pythread_tss_key_state", |
| 4973 | "PyThread_tss_create unsuccessful with " |
| 4974 | "an already initialized key"); |
| 4975 | } |
| 4976 | #define CHECK_TSS_API(expr) \ |
| 4977 | (void)(expr); \ |
| 4978 | if (!PyThread_tss_is_created(&tss_key)) { \ |
| 4979 | return raiseTestError("test_pythread_tss_key_state", \ |
| 4980 | "TSS key initialization state was not " \ |
| 4981 | "preserved after calling " #expr); } |
| 4982 | CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL)); |
| 4983 | CHECK_TSS_API(PyThread_tss_get(&tss_key)); |
| 4984 | #undef CHECK_TSS_API |
| 4985 | PyThread_tss_delete(&tss_key); |
| 4986 | if (PyThread_tss_is_created(&tss_key)) { |
| 4987 | return raiseTestError("test_pythread_tss_key_state", |
| 4988 | "PyThread_tss_delete called, but did not " |
| 4989 | "set the key state to uninitialized"); |
| 4990 | } |
| 4991 | |
| 4992 | Py_tss_t *ptr_key = PyThread_tss_alloc(); |
| 4993 | if (ptr_key == NULL) { |
| 4994 | PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed"); |
| 4995 | return NULL; |
| 4996 | } |
| 4997 | if (PyThread_tss_is_created(ptr_key)) { |
| 4998 | return raiseTestError("test_pythread_tss_key_state", |
| 4999 | "TSS key not in an uninitialized state at " |
| 5000 | "allocation time"); |
| 5001 | } |
| 5002 | PyThread_tss_free(ptr_key); |
| 5003 | ptr_key = NULL; |
| 5004 | Py_RETURN_NONE; |
| 5005 | } |
| 5006 | |
| 5007 | |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 5008 | static PyObject* |
| 5009 | new_hamt(PyObject *self, PyObject *args) |
| 5010 | { |
| 5011 | return _PyContext_NewHamtForTests(); |
| 5012 | } |
| 5013 | |
| 5014 | |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5015 | /* def bad_get(self, obj, cls): |
| 5016 | cls() |
| 5017 | return repr(self) |
| 5018 | */ |
| 5019 | static PyObject* |
| 5020 | bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 5021 | { |
Serhiy Storchaka | b1dede3 | 2018-11-20 20:45:40 +0200 | [diff] [blame] | 5022 | PyObject *self, *obj, *cls; |
| 5023 | if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) { |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5024 | return NULL; |
| 5025 | } |
| 5026 | |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 5027 | PyObject *res = _PyObject_CallNoArg(cls); |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5028 | if (res == NULL) { |
| 5029 | return NULL; |
| 5030 | } |
| 5031 | Py_DECREF(res); |
| 5032 | |
Serhiy Storchaka | b1dede3 | 2018-11-20 20:45:40 +0200 | [diff] [blame] | 5033 | return PyObject_Repr(self); |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5034 | } |
| 5035 | |
| 5036 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 5037 | static PyObject * |
| 5038 | encode_locale_ex(PyObject *self, PyObject *args) |
| 5039 | { |
| 5040 | PyObject *unicode; |
| 5041 | int current_locale = 0; |
| 5042 | wchar_t *wstr; |
| 5043 | PyObject *res = NULL; |
| 5044 | const char *errors = NULL; |
| 5045 | |
| 5046 | if (!PyArg_ParseTuple(args, "U|is", &unicode, ¤t_locale, &errors)) { |
| 5047 | return NULL; |
| 5048 | } |
| 5049 | wstr = PyUnicode_AsWideCharString(unicode, NULL); |
| 5050 | if (wstr == NULL) { |
| 5051 | return NULL; |
| 5052 | } |
| 5053 | _Py_error_handler error_handler = _Py_GetErrorHandler(errors); |
| 5054 | |
| 5055 | char *str = NULL; |
| 5056 | size_t error_pos; |
| 5057 | const char *reason = NULL; |
| 5058 | int ret = _Py_EncodeLocaleEx(wstr, |
| 5059 | &str, &error_pos, &reason, |
| 5060 | current_locale, error_handler); |
| 5061 | PyMem_Free(wstr); |
| 5062 | |
| 5063 | switch(ret) { |
| 5064 | case 0: |
| 5065 | res = PyBytes_FromString(str); |
| 5066 | PyMem_RawFree(str); |
| 5067 | break; |
| 5068 | case -1: |
| 5069 | PyErr_NoMemory(); |
| 5070 | break; |
| 5071 | case -2: |
| 5072 | PyErr_Format(PyExc_RuntimeError, "encode error: pos=%zu, reason=%s", |
| 5073 | error_pos, reason); |
| 5074 | break; |
| 5075 | case -3: |
| 5076 | PyErr_SetString(PyExc_ValueError, "unsupported error handler"); |
| 5077 | break; |
| 5078 | default: |
| 5079 | PyErr_SetString(PyExc_ValueError, "unknow error code"); |
| 5080 | break; |
| 5081 | } |
| 5082 | return res; |
| 5083 | } |
| 5084 | |
| 5085 | |
| 5086 | static PyObject * |
| 5087 | decode_locale_ex(PyObject *self, PyObject *args) |
| 5088 | { |
| 5089 | char *str; |
| 5090 | int current_locale = 0; |
| 5091 | PyObject *res = NULL; |
| 5092 | const char *errors = NULL; |
| 5093 | |
| 5094 | if (!PyArg_ParseTuple(args, "y|is", &str, ¤t_locale, &errors)) { |
| 5095 | return NULL; |
| 5096 | } |
| 5097 | _Py_error_handler error_handler = _Py_GetErrorHandler(errors); |
| 5098 | |
| 5099 | wchar_t *wstr = NULL; |
| 5100 | size_t wlen = 0; |
| 5101 | const char *reason = NULL; |
| 5102 | int ret = _Py_DecodeLocaleEx(str, |
| 5103 | &wstr, &wlen, &reason, |
| 5104 | current_locale, error_handler); |
| 5105 | |
| 5106 | switch(ret) { |
| 5107 | case 0: |
| 5108 | res = PyUnicode_FromWideChar(wstr, wlen); |
| 5109 | PyMem_RawFree(wstr); |
| 5110 | break; |
| 5111 | case -1: |
| 5112 | PyErr_NoMemory(); |
| 5113 | break; |
| 5114 | case -2: |
| 5115 | PyErr_Format(PyExc_RuntimeError, "decode error: pos=%zu, reason=%s", |
| 5116 | wlen, reason); |
| 5117 | break; |
| 5118 | case -3: |
| 5119 | PyErr_SetString(PyExc_ValueError, "unsupported error handler"); |
| 5120 | break; |
| 5121 | default: |
| 5122 | PyErr_SetString(PyExc_ValueError, "unknow error code"); |
| 5123 | break; |
| 5124 | } |
| 5125 | return res; |
| 5126 | } |
| 5127 | |
| 5128 | |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5129 | #ifdef Py_REF_DEBUG |
| 5130 | static PyObject * |
| 5131 | negative_refcount(PyObject *self, PyObject *Py_UNUSED(args)) |
| 5132 | { |
| 5133 | PyObject *obj = PyUnicode_FromString("negative_refcount"); |
| 5134 | if (obj == NULL) { |
| 5135 | return NULL; |
| 5136 | } |
| 5137 | assert(Py_REFCNT(obj) == 1); |
| 5138 | |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 5139 | Py_SET_REFCNT(obj, 0); |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5140 | /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */ |
| 5141 | Py_DECREF(obj); |
| 5142 | |
| 5143 | Py_RETURN_NONE; |
| 5144 | } |
| 5145 | #endif |
| 5146 | |
| 5147 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5148 | static PyObject* |
| 5149 | test_write_unraisable_exc(PyObject *self, PyObject *args) |
| 5150 | { |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5151 | PyObject *exc, *err_msg, *obj; |
| 5152 | if (!PyArg_ParseTuple(args, "OOO", &exc, &err_msg, &obj)) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5153 | return NULL; |
| 5154 | } |
| 5155 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5156 | const char *err_msg_utf8; |
| 5157 | if (err_msg != Py_None) { |
| 5158 | err_msg_utf8 = PyUnicode_AsUTF8(err_msg); |
| 5159 | if (err_msg_utf8 == NULL) { |
| 5160 | return NULL; |
| 5161 | } |
| 5162 | } |
| 5163 | else { |
| 5164 | err_msg_utf8 = NULL; |
| 5165 | } |
| 5166 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5167 | PyErr_SetObject((PyObject *)Py_TYPE(exc), exc); |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5168 | _PyErr_WriteUnraisableMsg(err_msg_utf8, obj); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5169 | Py_RETURN_NONE; |
| 5170 | } |
| 5171 | |
| 5172 | |
Sergey Fedoseev | 92709a2 | 2019-09-09 21:28:34 +0500 | [diff] [blame] | 5173 | static PyObject * |
| 5174 | sequence_getitem(PyObject *self, PyObject *args) |
| 5175 | { |
| 5176 | PyObject *seq; |
| 5177 | Py_ssize_t i; |
| 5178 | if (!PyArg_ParseTuple(args, "On", &seq, &i)) { |
| 5179 | return NULL; |
| 5180 | } |
| 5181 | return PySequence_GetItem(seq, i); |
| 5182 | } |
| 5183 | |
| 5184 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5185 | /* Functions for testing C calling conventions (METH_*) are named meth_*, |
| 5186 | * e.g. "meth_varargs" for METH_VARARGS. |
| 5187 | * |
| 5188 | * They all return a tuple of their C-level arguments, with None instead |
| 5189 | * of NULL and Python tuples instead of C arrays. |
| 5190 | */ |
| 5191 | |
| 5192 | |
| 5193 | static PyObject* |
| 5194 | _null_to_none(PyObject* obj) |
| 5195 | { |
| 5196 | if (obj == NULL) { |
| 5197 | Py_RETURN_NONE; |
| 5198 | } |
| 5199 | Py_INCREF(obj); |
| 5200 | return obj; |
| 5201 | } |
| 5202 | |
| 5203 | static PyObject* |
| 5204 | meth_varargs(PyObject* self, PyObject* args) |
| 5205 | { |
| 5206 | return Py_BuildValue("NO", _null_to_none(self), args); |
| 5207 | } |
| 5208 | |
| 5209 | static PyObject* |
| 5210 | meth_varargs_keywords(PyObject* self, PyObject* args, PyObject* kwargs) |
| 5211 | { |
| 5212 | return Py_BuildValue("NON", _null_to_none(self), args, _null_to_none(kwargs)); |
| 5213 | } |
| 5214 | |
| 5215 | static PyObject* |
| 5216 | meth_o(PyObject* self, PyObject* obj) |
| 5217 | { |
| 5218 | return Py_BuildValue("NO", _null_to_none(self), obj); |
| 5219 | } |
| 5220 | |
| 5221 | static PyObject* |
| 5222 | meth_noargs(PyObject* self, PyObject* ignored) |
| 5223 | { |
| 5224 | return _null_to_none(self); |
| 5225 | } |
| 5226 | |
| 5227 | static PyObject* |
| 5228 | _fastcall_to_tuple(PyObject* const* args, Py_ssize_t nargs) |
| 5229 | { |
| 5230 | PyObject *tuple = PyTuple_New(nargs); |
| 5231 | if (tuple == NULL) { |
| 5232 | return NULL; |
| 5233 | } |
| 5234 | for (Py_ssize_t i=0; i < nargs; i++) { |
| 5235 | Py_INCREF(args[i]); |
| 5236 | PyTuple_SET_ITEM(tuple, i, args[i]); |
| 5237 | } |
| 5238 | return tuple; |
| 5239 | } |
| 5240 | |
| 5241 | static PyObject* |
| 5242 | meth_fastcall(PyObject* self, PyObject* const* args, Py_ssize_t nargs) |
| 5243 | { |
| 5244 | return Py_BuildValue( |
| 5245 | "NN", _null_to_none(self), _fastcall_to_tuple(args, nargs) |
| 5246 | ); |
| 5247 | } |
| 5248 | |
| 5249 | static PyObject* |
| 5250 | meth_fastcall_keywords(PyObject* self, PyObject* const* args, |
| 5251 | Py_ssize_t nargs, PyObject* kwargs) |
| 5252 | { |
| 5253 | PyObject *pyargs = _fastcall_to_tuple(args, nargs); |
| 5254 | if (pyargs == NULL) { |
| 5255 | return NULL; |
| 5256 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5257 | PyObject *pykwargs = PyObject_Vectorcall((PyObject*)&PyDict_Type, |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5258 | args + nargs, 0, kwargs); |
| 5259 | return Py_BuildValue("NNN", _null_to_none(self), pyargs, pykwargs); |
| 5260 | } |
| 5261 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 5262 | |
Serhiy Storchaka | e5ccc94 | 2020-03-09 20:03:38 +0200 | [diff] [blame] | 5263 | static PyObject* |
| 5264 | pynumber_tobase(PyObject *module, PyObject *args) |
| 5265 | { |
| 5266 | PyObject *obj; |
| 5267 | int base; |
| 5268 | if (!PyArg_ParseTuple(args, "Oi:pynumber_tobase", |
| 5269 | &obj, &base)) { |
| 5270 | return NULL; |
| 5271 | } |
| 5272 | return PyNumber_ToBase(obj, base); |
| 5273 | } |
| 5274 | |
| 5275 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 5276 | static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *); |
| 5277 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 5278 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5279 | {"raise_exception", raise_exception, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5280 | {"raise_memoryerror", raise_memoryerror, METH_NOARGS}, |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 5281 | {"set_errno", set_errno, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5282 | {"test_config", test_config, METH_NOARGS}, |
| 5283 | {"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS}, |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 5284 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 5285 | {"datetime_check_date", datetime_check_date, METH_VARARGS}, |
| 5286 | {"datetime_check_time", datetime_check_time, METH_VARARGS}, |
| 5287 | {"datetime_check_datetime", datetime_check_datetime, METH_VARARGS}, |
| 5288 | {"datetime_check_delta", datetime_check_delta, METH_VARARGS}, |
| 5289 | {"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS}, |
| 5290 | {"make_timezones_capi", make_timezones_capi, METH_NOARGS}, |
Paul Ganssle | a049f57 | 2018-02-22 15:15:32 -0500 | [diff] [blame] | 5291 | {"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS}, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 5292 | {"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS}, |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 5293 | {"get_date_fromdate", get_date_fromdate, METH_VARARGS}, |
| 5294 | {"get_datetime_fromdateandtime", get_datetime_fromdateandtime, METH_VARARGS}, |
| 5295 | {"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS}, |
| 5296 | {"get_time_fromtime", get_time_fromtime, METH_VARARGS}, |
| 5297 | {"get_time_fromtimeandfold", get_time_fromtimeandfold, METH_VARARGS}, |
| 5298 | {"get_delta_fromdsu", get_delta_fromdsu, METH_VARARGS}, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 5299 | {"get_date_fromtimestamp", get_date_fromtimestamp, METH_VARARGS}, |
| 5300 | {"get_datetime_fromtimestamp", get_datetime_fromtimestamp, METH_VARARGS}, |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 5301 | {"PyDateTime_GET", test_PyDateTime_GET, METH_O}, |
| 5302 | {"PyDateTime_DATE_GET", test_PyDateTime_DATE_GET, METH_O}, |
| 5303 | {"PyDateTime_TIME_GET", test_PyDateTime_TIME_GET, METH_O}, |
| 5304 | {"PyDateTime_DELTA_GET", test_PyDateTime_DELTA_GET, METH_O}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5305 | {"test_list_api", test_list_api, METH_NOARGS}, |
| 5306 | {"test_dict_iteration", test_dict_iteration, METH_NOARGS}, |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 5307 | {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 5308 | {"dict_hassplittable", dict_hassplittable, METH_O}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5309 | {"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS}, |
| 5310 | {"test_long_api", test_long_api, METH_NOARGS}, |
| 5311 | {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS}, |
| 5312 | {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS}, |
| 5313 | {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS}, |
| 5314 | {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS}, |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 5315 | {"test_structseq_newtype_doesnt_leak", |
| 5316 | test_structseq_newtype_doesnt_leak, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5317 | {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS}, |
| 5318 | {"test_long_and_overflow", test_long_and_overflow, METH_NOARGS}, |
| 5319 | {"test_long_as_double", test_long_as_double, METH_NOARGS}, |
| 5320 | {"test_long_as_size_t", test_long_as_size_t, METH_NOARGS}, |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 5321 | {"test_long_as_unsigned_long_long_mask", |
| 5322 | test_long_as_unsigned_long_long_mask, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5323 | {"test_long_numbits", test_long_numbits, METH_NOARGS}, |
| 5324 | {"test_k_code", test_k_code, METH_NOARGS}, |
| 5325 | {"test_empty_argparse", test_empty_argparse, METH_NOARGS}, |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 5326 | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5327 | {"test_null_strings", test_null_strings, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5328 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5329 | {"test_with_docstring", test_with_docstring, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | PyDoc_STR("This is a pretty normal docstring.")}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5331 | {"test_string_to_double", test_string_to_double, METH_NOARGS}, |
| 5332 | {"test_unicode_compare_with_ascii", test_unicode_compare_with_ascii, |
| 5333 | METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5334 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 5335 | {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 5336 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 5337 | {"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] | 5338 | #endif |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 5339 | {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, |
Joannah Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 5340 | {"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS}, |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 5341 | {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 5342 | {"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS}, |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 5343 | {"get_args", get_args, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5344 | {"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5345 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5346 | {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5347 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5348 | {"getargs_keyword_only", (PyCFunction)(void(*)(void))getargs_keyword_only, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 5349 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 5350 | {"getargs_positional_only_and_keywords", |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5351 | (PyCFunction)(void(*)(void))getargs_positional_only_and_keywords, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 5352 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5353 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 5354 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 5355 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 5356 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 5357 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 5358 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 5359 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 5360 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 5361 | {"getargs_n", getargs_n, METH_VARARGS}, |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 5362 | {"getargs_p", getargs_p, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5363 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 5364 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 5365 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5366 | {"test_long_long_and_overflow",test_long_long_and_overflow, METH_NOARGS}, |
| 5367 | {"test_L_code", test_L_code, METH_NOARGS}, |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 5368 | {"getargs_f", getargs_f, METH_VARARGS}, |
| 5369 | {"getargs_d", getargs_d, METH_VARARGS}, |
| 5370 | {"getargs_D", getargs_D, METH_VARARGS}, |
| 5371 | {"getargs_S", getargs_S, METH_VARARGS}, |
| 5372 | {"getargs_Y", getargs_Y, METH_VARARGS}, |
| 5373 | {"getargs_U", getargs_U, METH_VARARGS}, |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 5374 | {"getargs_c", getargs_c, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 5375 | {"getargs_C", getargs_C, METH_VARARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 5376 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 5377 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 5378 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 5379 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 5380 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 5381 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 5382 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 5383 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 5384 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 5385 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 5386 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 5387 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 5388 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 5389 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 5390 | {"getargs_es", getargs_es, METH_VARARGS}, |
| 5391 | {"getargs_et", getargs_et, METH_VARARGS}, |
| 5392 | {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 5393 | {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5395 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5396 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5397 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5398 | {"test_s_code", test_s_code, METH_NOARGS}, |
| 5399 | {"test_u_code", test_u_code, METH_NOARGS}, |
| 5400 | {"test_Z_code", test_Z_code, METH_NOARGS}, |
| 5401 | {"test_widechar", test_widechar, METH_NOARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 5402 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 5403 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 5404 | {"unicode_asucs4", unicode_asucs4, METH_VARARGS}, |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 5405 | {"unicode_asutf8", unicode_asutf8, METH_VARARGS}, |
| 5406 | {"unicode_asutf8andsize", unicode_asutf8andsize, METH_VARARGS}, |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 5407 | {"unicode_findchar", unicode_findchar, METH_VARARGS}, |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 5408 | {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 5409 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 5410 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 5411 | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5412 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5413 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5414 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5415 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5416 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5417 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 5418 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 5419 | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5420 | {"argparsing", argparsing, METH_VARARGS}, |
| 5421 | {"code_newempty", code_newempty, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5422 | {"make_exception_with_doc", (PyCFunction)(void(*)(void))make_exception_with_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5423 | METH_VARARGS | METH_KEYWORDS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5424 | {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer, |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 5425 | METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5426 | {"crash_no_current_thread", crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 5427 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5428 | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
| 5429 | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 5430 | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 5431 | {"with_tp_del", with_tp_del, METH_VARARGS}, |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 5432 | {"create_cfunction", create_cfunction, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5433 | {"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS}, |
| 5434 | {"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS}, |
| 5435 | {"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS}, |
| 5436 | {"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS}, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 5437 | {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS, |
| 5438 | PyDoc_STR("set_nomemory(start:int, stop:int = 0)")}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5439 | {"remove_mem_hooks", remove_mem_hooks, METH_NOARGS, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 5440 | PyDoc_STR("Remove memory hooks.")}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5441 | {"no_docstring", |
| 5442 | (PyCFunction)test_with_docstring, METH_NOARGS}, |
| 5443 | {"docstring_empty", |
| 5444 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5445 | docstring_empty}, |
| 5446 | {"docstring_no_signature", |
| 5447 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5448 | docstring_no_signature}, |
| 5449 | {"docstring_with_invalid_signature", |
| 5450 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5451 | docstring_with_invalid_signature}, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 5452 | {"docstring_with_invalid_signature2", |
| 5453 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5454 | docstring_with_invalid_signature2}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5455 | {"docstring_with_signature", |
| 5456 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5457 | docstring_with_signature}, |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 5458 | {"docstring_with_signature_but_no_doc", |
| 5459 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5460 | docstring_with_signature_but_no_doc}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5461 | {"docstring_with_signature_and_extra_newlines", |
| 5462 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5463 | docstring_with_signature_and_extra_newlines}, |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 5464 | {"docstring_with_signature_with_defaults", |
| 5465 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5466 | docstring_with_signature_with_defaults}, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5467 | {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
| 5468 | PyDoc_STR("set_error_class(error_class) -> None")}, |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 5469 | {"pymarshal_write_long_to_file", |
| 5470 | pymarshal_write_long_to_file, METH_VARARGS}, |
| 5471 | {"pymarshal_write_object_to_file", |
| 5472 | pymarshal_write_object_to_file, METH_VARARGS}, |
| 5473 | {"pymarshal_read_short_from_file", |
| 5474 | pymarshal_read_short_from_file, METH_VARARGS}, |
| 5475 | {"pymarshal_read_long_from_file", |
| 5476 | pymarshal_read_long_from_file, METH_VARARGS}, |
| 5477 | {"pymarshal_read_last_object_from_file", |
| 5478 | pymarshal_read_last_object_from_file, METH_VARARGS}, |
| 5479 | {"pymarshal_read_object_from_file", |
| 5480 | pymarshal_read_object_from_file, METH_VARARGS}, |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 5481 | {"return_null_without_error", |
| 5482 | return_null_without_error, METH_NOARGS}, |
| 5483 | {"return_result_with_error", |
| 5484 | return_result_with_error, METH_NOARGS}, |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 5485 | {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS}, |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 5486 | {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS}, |
| 5487 | {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 5488 | {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 5489 | #ifdef HAVE_CLOCK_GETTIME |
| 5490 | {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, |
| 5491 | #endif |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 5492 | {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS}, |
| 5493 | {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS}, |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 5494 | {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS}, |
| 5495 | {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 5496 | {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 5497 | {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS}, |
Victor Stinner | 6876257 | 2019-10-07 18:42:01 +0200 | [diff] [blame] | 5498 | {"check_pyobject_null_is_freed", check_pyobject_null_is_freed, METH_NOARGS}, |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 5499 | {"check_pyobject_uninitialized_is_freed", check_pyobject_uninitialized_is_freed, METH_NOARGS}, |
| 5500 | {"check_pyobject_forbidden_bytes_is_freed", check_pyobject_forbidden_bytes_is_freed, METH_NOARGS}, |
| 5501 | {"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS}, |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 5502 | {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 5503 | {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, |
| 5504 | {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, |
| 5505 | {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 5506 | {"dict_get_version", dict_get_version, METH_VARARGS}, |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 5507 | {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS}, |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 5508 | {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS}, |
| 5509 | {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS}, |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5510 | {"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS}, |
| 5511 | {"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS}, |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 5512 | {"stack_pointer", stack_pointer, METH_NOARGS}, |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 5513 | #ifdef W_STOPCODE |
| 5514 | {"W_STOPCODE", py_w_stopcode, METH_VARARGS}, |
| 5515 | #endif |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 5516 | {"get_mapping_keys", get_mapping_keys, METH_O}, |
| 5517 | {"get_mapping_values", get_mapping_values, METH_O}, |
| 5518 | {"get_mapping_items", get_mapping_items, METH_O}, |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 5519 | {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS}, |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 5520 | {"hamt", new_hamt, METH_NOARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5521 | {"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL}, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 5522 | {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS}, |
| 5523 | {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS}, |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5524 | #ifdef Py_REF_DEBUG |
| 5525 | {"negative_refcount", negative_refcount, METH_NOARGS}, |
| 5526 | #endif |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5527 | {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS}, |
Sergey Fedoseev | 92709a2 | 2019-09-09 21:28:34 +0500 | [diff] [blame] | 5528 | {"sequence_getitem", sequence_getitem, METH_VARARGS}, |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5529 | {"meth_varargs", meth_varargs, METH_VARARGS}, |
| 5530 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, |
| 5531 | {"meth_o", meth_o, METH_O}, |
| 5532 | {"meth_noargs", meth_noargs, METH_NOARGS}, |
| 5533 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, |
| 5534 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS}, |
Serhiy Storchaka | e5ccc94 | 2020-03-09 20:03:38 +0200 | [diff] [blame] | 5535 | {"pynumber_tobase", pynumber_tobase, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5536 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 5537 | }; |
| 5538 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 5539 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 5540 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5541 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5542 | char bool_member; |
| 5543 | char byte_member; |
| 5544 | unsigned char ubyte_member; |
| 5545 | short short_member; |
| 5546 | unsigned short ushort_member; |
| 5547 | int int_member; |
| 5548 | unsigned int uint_member; |
| 5549 | long long_member; |
| 5550 | unsigned long ulong_member; |
| 5551 | Py_ssize_t pyssizet_member; |
| 5552 | float float_member; |
| 5553 | double double_member; |
| 5554 | char inplace_member[6]; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5555 | long long longlong_member; |
| 5556 | unsigned long long ulonglong_member; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5557 | } all_structmembers; |
| 5558 | |
| 5559 | typedef struct { |
| 5560 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5561 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5562 | } test_structmembers; |
| 5563 | |
| 5564 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5565 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 5566 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 5567 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 5568 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 5569 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 5570 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 5571 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 5572 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 5573 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 5574 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 5575 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 5576 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 5577 | {"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] | 5578 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 5579 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5580 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5581 | }; |
| 5582 | |
| 5583 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 5584 | static PyObject * |
| 5585 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5587 | static char *keywords[] = { |
| 5588 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 5589 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 5590 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5591 | "T_LONGLONG", "T_ULONGLONG", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5592 | NULL}; |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 5593 | static const char fmt[] = "|bbBhHiIlknfds#LK"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5594 | test_structmembers *ob; |
| 5595 | const char *s = NULL; |
| 5596 | Py_ssize_t string_len = 0; |
| 5597 | ob = PyObject_New(test_structmembers, type); |
| 5598 | if (ob == NULL) |
| 5599 | return NULL; |
| 5600 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 5601 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 5602 | &ob->structmembers.bool_member, |
| 5603 | &ob->structmembers.byte_member, |
| 5604 | &ob->structmembers.ubyte_member, |
| 5605 | &ob->structmembers.short_member, |
| 5606 | &ob->structmembers.ushort_member, |
| 5607 | &ob->structmembers.int_member, |
| 5608 | &ob->structmembers.uint_member, |
| 5609 | &ob->structmembers.long_member, |
| 5610 | &ob->structmembers.ulong_member, |
| 5611 | &ob->structmembers.pyssizet_member, |
| 5612 | &ob->structmembers.float_member, |
| 5613 | &ob->structmembers.double_member, |
| 5614 | &s, &string_len |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5615 | , &ob->structmembers.longlong_member, |
| 5616 | &ob->structmembers.ulonglong_member |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5617 | )) { |
| 5618 | Py_DECREF(ob); |
| 5619 | return NULL; |
| 5620 | } |
| 5621 | if (s != NULL) { |
| 5622 | if (string_len > 5) { |
| 5623 | Py_DECREF(ob); |
| 5624 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 5625 | return NULL; |
| 5626 | } |
| 5627 | strcpy(ob->structmembers.inplace_member, s); |
| 5628 | } |
| 5629 | else { |
| 5630 | strcpy(ob->structmembers.inplace_member, ""); |
| 5631 | } |
| 5632 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 5635 | static void |
| 5636 | test_structmembers_free(PyObject *ob) |
| 5637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5638 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 5642 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5643 | "test_structmembersType", |
| 5644 | sizeof(test_structmembers), /* tp_basicsize */ |
| 5645 | 0, /* tp_itemsize */ |
| 5646 | test_structmembers_free, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5647 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5648 | 0, /* tp_getattr */ |
| 5649 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5650 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5651 | 0, /* tp_repr */ |
| 5652 | 0, /* tp_as_number */ |
| 5653 | 0, /* tp_as_sequence */ |
| 5654 | 0, /* tp_as_mapping */ |
| 5655 | 0, /* tp_hash */ |
| 5656 | 0, /* tp_call */ |
| 5657 | 0, /* tp_str */ |
| 5658 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5659 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5660 | 0, /* tp_as_buffer */ |
| 5661 | 0, /* tp_flags */ |
| 5662 | "Type containing all structmember types", |
| 5663 | 0, /* traverseproc tp_traverse */ |
| 5664 | 0, /* tp_clear */ |
| 5665 | 0, /* tp_richcompare */ |
| 5666 | 0, /* tp_weaklistoffset */ |
| 5667 | 0, /* tp_iter */ |
| 5668 | 0, /* tp_iternext */ |
| 5669 | 0, /* tp_methods */ |
| 5670 | test_members, /* tp_members */ |
| 5671 | 0, |
| 5672 | 0, |
| 5673 | 0, |
| 5674 | 0, |
| 5675 | 0, |
| 5676 | 0, |
| 5677 | 0, |
| 5678 | 0, |
| 5679 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5680 | }; |
| 5681 | |
| 5682 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5683 | typedef struct { |
| 5684 | PyObject_HEAD |
| 5685 | } matmulObject; |
| 5686 | |
| 5687 | static PyObject * |
| 5688 | matmulType_matmul(PyObject *self, PyObject *other) |
| 5689 | { |
| 5690 | return Py_BuildValue("(sOO)", "matmul", self, other); |
| 5691 | } |
| 5692 | |
| 5693 | static PyObject * |
| 5694 | matmulType_imatmul(PyObject *self, PyObject *other) |
| 5695 | { |
| 5696 | return Py_BuildValue("(sOO)", "imatmul", self, other); |
| 5697 | } |
| 5698 | |
| 5699 | static void |
| 5700 | matmulType_dealloc(PyObject *self) |
| 5701 | { |
Zachary Ware | 420dc56 | 2014-04-23 13:51:27 -0500 | [diff] [blame] | 5702 | Py_TYPE(self)->tp_free(self); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | static PyNumberMethods matmulType_as_number = { |
| 5706 | 0, /* nb_add */ |
| 5707 | 0, /* nb_subtract */ |
| 5708 | 0, /* nb_multiply */ |
| 5709 | 0, /* nb_remainde r*/ |
| 5710 | 0, /* nb_divmod */ |
| 5711 | 0, /* nb_power */ |
| 5712 | 0, /* nb_negative */ |
| 5713 | 0, /* tp_positive */ |
| 5714 | 0, /* tp_absolute */ |
| 5715 | 0, /* tp_bool */ |
| 5716 | 0, /* nb_invert */ |
| 5717 | 0, /* nb_lshift */ |
| 5718 | 0, /* nb_rshift */ |
| 5719 | 0, /* nb_and */ |
| 5720 | 0, /* nb_xor */ |
| 5721 | 0, /* nb_or */ |
| 5722 | 0, /* nb_int */ |
| 5723 | 0, /* nb_reserved */ |
| 5724 | 0, /* nb_float */ |
| 5725 | 0, /* nb_inplace_add */ |
| 5726 | 0, /* nb_inplace_subtract */ |
| 5727 | 0, /* nb_inplace_multiply */ |
| 5728 | 0, /* nb_inplace_remainder */ |
| 5729 | 0, /* nb_inplace_power */ |
| 5730 | 0, /* nb_inplace_lshift */ |
| 5731 | 0, /* nb_inplace_rshift */ |
| 5732 | 0, /* nb_inplace_and */ |
| 5733 | 0, /* nb_inplace_xor */ |
| 5734 | 0, /* nb_inplace_or */ |
| 5735 | 0, /* nb_floor_divide */ |
| 5736 | 0, /* nb_true_divide */ |
| 5737 | 0, /* nb_inplace_floor_divide */ |
| 5738 | 0, /* nb_inplace_true_divide */ |
| 5739 | 0, /* nb_index */ |
| 5740 | matmulType_matmul, /* nb_matrix_multiply */ |
| 5741 | matmulType_imatmul /* nb_matrix_inplace_multiply */ |
| 5742 | }; |
| 5743 | |
| 5744 | static PyTypeObject matmulType = { |
| 5745 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5746 | "matmulType", |
| 5747 | sizeof(matmulObject), /* tp_basicsize */ |
| 5748 | 0, /* tp_itemsize */ |
| 5749 | matmulType_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5750 | 0, /* tp_vectorcall_offset */ |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5751 | 0, /* tp_getattr */ |
| 5752 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5753 | 0, /* tp_as_async */ |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5754 | 0, /* tp_repr */ |
| 5755 | &matmulType_as_number, /* tp_as_number */ |
| 5756 | 0, /* tp_as_sequence */ |
| 5757 | 0, /* tp_as_mapping */ |
| 5758 | 0, /* tp_hash */ |
| 5759 | 0, /* tp_call */ |
| 5760 | 0, /* tp_str */ |
| 5761 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5762 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5763 | 0, /* tp_as_buffer */ |
| 5764 | 0, /* tp_flags */ |
| 5765 | "C level type with matrix operations defined", |
| 5766 | 0, /* traverseproc tp_traverse */ |
| 5767 | 0, /* tp_clear */ |
| 5768 | 0, /* tp_richcompare */ |
| 5769 | 0, /* tp_weaklistoffset */ |
| 5770 | 0, /* tp_iter */ |
| 5771 | 0, /* tp_iternext */ |
| 5772 | 0, /* tp_methods */ |
| 5773 | 0, /* tp_members */ |
| 5774 | 0, |
| 5775 | 0, |
| 5776 | 0, |
| 5777 | 0, |
| 5778 | 0, |
| 5779 | 0, |
| 5780 | 0, |
| 5781 | 0, |
| 5782 | PyType_GenericNew, /* tp_new */ |
| 5783 | PyObject_Del, /* tp_free */ |
| 5784 | }; |
| 5785 | |
Zackery Spytz | c7f803b | 2019-05-31 03:46:36 -0600 | [diff] [blame] | 5786 | typedef struct { |
| 5787 | PyObject_HEAD |
| 5788 | } ipowObject; |
| 5789 | |
| 5790 | static PyObject * |
| 5791 | ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod) |
| 5792 | { |
| 5793 | return Py_BuildValue("OO", other, mod); |
| 5794 | } |
| 5795 | |
| 5796 | static PyNumberMethods ipowType_as_number = { |
| 5797 | .nb_inplace_power = ipowType_ipow |
| 5798 | }; |
| 5799 | |
| 5800 | static PyTypeObject ipowType = { |
| 5801 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5802 | .tp_name = "ipowType", |
| 5803 | .tp_basicsize = sizeof(ipowObject), |
| 5804 | .tp_as_number = &ipowType_as_number, |
| 5805 | .tp_new = PyType_GenericNew |
| 5806 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5807 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5808 | typedef struct { |
| 5809 | PyObject_HEAD |
| 5810 | PyObject *ao_iterator; |
| 5811 | } awaitObject; |
| 5812 | |
| 5813 | |
| 5814 | static PyObject * |
| 5815 | awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5816 | { |
| 5817 | PyObject *v; |
| 5818 | awaitObject *ao; |
| 5819 | |
| 5820 | if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v)) |
| 5821 | return NULL; |
| 5822 | |
| 5823 | ao = (awaitObject *)type->tp_alloc(type, 0); |
| 5824 | if (ao == NULL) { |
| 5825 | return NULL; |
| 5826 | } |
| 5827 | |
| 5828 | Py_INCREF(v); |
| 5829 | ao->ao_iterator = v; |
| 5830 | |
| 5831 | return (PyObject *)ao; |
| 5832 | } |
| 5833 | |
| 5834 | |
| 5835 | static void |
| 5836 | awaitObject_dealloc(awaitObject *ao) |
| 5837 | { |
| 5838 | Py_CLEAR(ao->ao_iterator); |
| 5839 | Py_TYPE(ao)->tp_free(ao); |
| 5840 | } |
| 5841 | |
| 5842 | |
| 5843 | static PyObject * |
| 5844 | awaitObject_await(awaitObject *ao) |
| 5845 | { |
| 5846 | Py_INCREF(ao->ao_iterator); |
| 5847 | return ao->ao_iterator; |
| 5848 | } |
| 5849 | |
| 5850 | static PyAsyncMethods awaitType_as_async = { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 5851 | (unaryfunc)awaitObject_await, /* am_await */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5852 | 0, /* am_aiter */ |
| 5853 | 0 /* am_anext */ |
| 5854 | }; |
| 5855 | |
| 5856 | |
| 5857 | static PyTypeObject awaitType = { |
| 5858 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5859 | "awaitType", |
| 5860 | sizeof(awaitObject), /* tp_basicsize */ |
| 5861 | 0, /* tp_itemsize */ |
| 5862 | (destructor)awaitObject_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5863 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5864 | 0, /* tp_getattr */ |
| 5865 | 0, /* tp_setattr */ |
| 5866 | &awaitType_as_async, /* tp_as_async */ |
| 5867 | 0, /* tp_repr */ |
| 5868 | 0, /* tp_as_number */ |
| 5869 | 0, /* tp_as_sequence */ |
| 5870 | 0, /* tp_as_mapping */ |
| 5871 | 0, /* tp_hash */ |
| 5872 | 0, /* tp_call */ |
| 5873 | 0, /* tp_str */ |
| 5874 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5875 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5876 | 0, /* tp_as_buffer */ |
| 5877 | 0, /* tp_flags */ |
| 5878 | "C level type with tp_as_async", |
| 5879 | 0, /* traverseproc tp_traverse */ |
| 5880 | 0, /* tp_clear */ |
| 5881 | 0, /* tp_richcompare */ |
| 5882 | 0, /* tp_weaklistoffset */ |
| 5883 | 0, /* tp_iter */ |
| 5884 | 0, /* tp_iternext */ |
| 5885 | 0, /* tp_methods */ |
| 5886 | 0, /* tp_members */ |
| 5887 | 0, |
| 5888 | 0, |
| 5889 | 0, |
| 5890 | 0, |
| 5891 | 0, |
| 5892 | 0, |
| 5893 | 0, |
| 5894 | 0, |
| 5895 | awaitObject_new, /* tp_new */ |
| 5896 | PyObject_Del, /* tp_free */ |
| 5897 | }; |
| 5898 | |
| 5899 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5900 | static int recurse_infinitely_error_init(PyObject *, PyObject *, PyObject *); |
| 5901 | |
| 5902 | static PyTypeObject PyRecursingInfinitelyError_Type = { |
| 5903 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5904 | "RecursingInfinitelyError", /* tp_name */ |
| 5905 | sizeof(PyBaseExceptionObject), /* tp_basicsize */ |
| 5906 | 0, /* tp_itemsize */ |
| 5907 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5908 | 0, /* tp_vectorcall_offset */ |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5909 | 0, /* tp_getattr */ |
| 5910 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5911 | 0, /* tp_as_async */ |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5912 | 0, /* tp_repr */ |
| 5913 | 0, /* tp_as_number */ |
| 5914 | 0, /* tp_as_sequence */ |
| 5915 | 0, /* tp_as_mapping */ |
| 5916 | 0, /* tp_hash */ |
| 5917 | 0, /* tp_call */ |
| 5918 | 0, /* tp_str */ |
| 5919 | 0, /* tp_getattro */ |
| 5920 | 0, /* tp_setattro */ |
| 5921 | 0, /* tp_as_buffer */ |
| 5922 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5923 | "Instantiating this exception starts infinite recursion.", /* tp_doc */ |
| 5924 | 0, /* tp_traverse */ |
| 5925 | 0, /* tp_clear */ |
| 5926 | 0, /* tp_richcompare */ |
| 5927 | 0, /* tp_weaklistoffset */ |
| 5928 | 0, /* tp_iter */ |
| 5929 | 0, /* tp_iternext */ |
| 5930 | 0, /* tp_methods */ |
| 5931 | 0, /* tp_members */ |
| 5932 | 0, /* tp_getset */ |
| 5933 | 0, /* tp_base */ |
| 5934 | 0, /* tp_dict */ |
| 5935 | 0, /* tp_descr_get */ |
| 5936 | 0, /* tp_descr_set */ |
| 5937 | 0, /* tp_dictoffset */ |
| 5938 | (initproc)recurse_infinitely_error_init, /* tp_init */ |
| 5939 | 0, /* tp_alloc */ |
| 5940 | 0, /* tp_new */ |
| 5941 | }; |
| 5942 | |
| 5943 | static int |
| 5944 | recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5945 | { |
| 5946 | PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type; |
| 5947 | |
| 5948 | /* Instantiating this exception starts infinite recursion. */ |
| 5949 | Py_INCREF(type); |
| 5950 | PyErr_SetObject(type, NULL); |
| 5951 | return -1; |
| 5952 | } |
| 5953 | |
| 5954 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5955 | /* Test bpo-35983: create a subclass of "list" which checks that instances |
| 5956 | * are not deallocated twice */ |
| 5957 | |
| 5958 | typedef struct { |
| 5959 | PyListObject list; |
| 5960 | int deallocated; |
| 5961 | } MyListObject; |
| 5962 | |
| 5963 | static PyObject * |
| 5964 | MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5965 | { |
| 5966 | PyObject* op = PyList_Type.tp_new(type, args, kwds); |
| 5967 | ((MyListObject*)op)->deallocated = 0; |
| 5968 | return op; |
| 5969 | } |
| 5970 | |
| 5971 | void |
| 5972 | MyList_dealloc(MyListObject* op) |
| 5973 | { |
| 5974 | if (op->deallocated) { |
| 5975 | /* We cannot raise exceptions here but we still want the testsuite |
| 5976 | * to fail when we hit this */ |
| 5977 | Py_FatalError("MyList instance deallocated twice"); |
| 5978 | } |
| 5979 | op->deallocated = 1; |
| 5980 | PyList_Type.tp_dealloc((PyObject *)op); |
| 5981 | } |
| 5982 | |
| 5983 | static PyTypeObject MyList_Type = { |
| 5984 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5985 | "MyList", |
| 5986 | sizeof(MyListObject), |
| 5987 | 0, |
| 5988 | (destructor)MyList_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5989 | 0, /* tp_vectorcall_offset */ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5990 | 0, /* tp_getattr */ |
| 5991 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5992 | 0, /* tp_as_async */ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5993 | 0, /* tp_repr */ |
| 5994 | 0, /* tp_as_number */ |
| 5995 | 0, /* tp_as_sequence */ |
| 5996 | 0, /* tp_as_mapping */ |
| 5997 | 0, /* tp_hash */ |
| 5998 | 0, /* tp_call */ |
| 5999 | 0, /* tp_str */ |
| 6000 | 0, /* tp_getattro */ |
| 6001 | 0, /* tp_setattro */ |
| 6002 | 0, /* tp_as_buffer */ |
| 6003 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6004 | 0, /* tp_doc */ |
| 6005 | 0, /* tp_traverse */ |
| 6006 | 0, /* tp_clear */ |
| 6007 | 0, /* tp_richcompare */ |
| 6008 | 0, /* tp_weaklistoffset */ |
| 6009 | 0, /* tp_iter */ |
| 6010 | 0, /* tp_iternext */ |
| 6011 | 0, /* tp_methods */ |
| 6012 | 0, /* tp_members */ |
| 6013 | 0, /* tp_getset */ |
| 6014 | 0, /* &PyList_Type */ /* tp_base */ |
| 6015 | 0, /* tp_dict */ |
| 6016 | 0, /* tp_descr_get */ |
| 6017 | 0, /* tp_descr_set */ |
| 6018 | 0, /* tp_dictoffset */ |
| 6019 | 0, /* tp_init */ |
| 6020 | 0, /* tp_alloc */ |
| 6021 | MyList_new, /* tp_new */ |
| 6022 | }; |
| 6023 | |
| 6024 | |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6025 | /* Test PEP 560 */ |
| 6026 | |
| 6027 | typedef struct { |
| 6028 | PyObject_HEAD |
| 6029 | PyObject *item; |
| 6030 | } PyGenericAliasObject; |
| 6031 | |
| 6032 | static void |
| 6033 | generic_alias_dealloc(PyGenericAliasObject *self) |
| 6034 | { |
| 6035 | Py_CLEAR(self->item); |
Victor Stinner | e860089 | 2018-01-17 23:08:18 +0100 | [diff] [blame] | 6036 | Py_TYPE(self)->tp_free((PyObject *)self); |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | static PyObject * |
| 6040 | generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases) |
| 6041 | { |
| 6042 | return PyTuple_Pack(1, self->item); |
| 6043 | } |
| 6044 | |
| 6045 | static PyMethodDef generic_alias_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6046 | {"__mro_entries__", (PyCFunction)(void(*)(void))generic_alias_mro_entries, METH_O, NULL}, |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6047 | {NULL} /* sentinel */ |
| 6048 | }; |
| 6049 | |
Benjamin Peterson | 97ae32c | 2018-07-03 22:39:09 -0700 | [diff] [blame] | 6050 | static PyTypeObject GenericAlias_Type = { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6051 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6052 | "GenericAlias", |
| 6053 | sizeof(PyGenericAliasObject), |
| 6054 | 0, |
| 6055 | .tp_dealloc = (destructor)generic_alias_dealloc, |
| 6056 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6057 | .tp_methods = generic_alias_methods, |
| 6058 | }; |
| 6059 | |
| 6060 | static PyObject * |
| 6061 | generic_alias_new(PyObject *item) |
| 6062 | { |
| 6063 | PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type); |
| 6064 | if (o == NULL) { |
| 6065 | return NULL; |
| 6066 | } |
| 6067 | Py_INCREF(item); |
| 6068 | o->item = item; |
| 6069 | return (PyObject*) o; |
| 6070 | } |
| 6071 | |
| 6072 | typedef struct { |
| 6073 | PyObject_HEAD |
| 6074 | } PyGenericObject; |
| 6075 | |
| 6076 | static PyObject * |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 6077 | generic_class_getitem(PyObject *type, PyObject *item) |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6078 | { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6079 | return generic_alias_new(item); |
| 6080 | } |
| 6081 | |
| 6082 | static PyMethodDef generic_methods[] = { |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 6083 | {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL}, |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6084 | {NULL} /* sentinel */ |
| 6085 | }; |
| 6086 | |
Benjamin Peterson | 97ae32c | 2018-07-03 22:39:09 -0700 | [diff] [blame] | 6087 | static PyTypeObject Generic_Type = { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6088 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6089 | "Generic", |
| 6090 | sizeof(PyGenericObject), |
| 6091 | 0, |
| 6092 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6093 | .tp_methods = generic_methods, |
| 6094 | }; |
| 6095 | |
| 6096 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6097 | /* Test PEP 590 */ |
| 6098 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6099 | typedef struct { |
| 6100 | PyObject_HEAD |
| 6101 | vectorcallfunc vectorcall; |
| 6102 | } MethodDescriptorObject; |
| 6103 | |
| 6104 | static PyObject * |
| 6105 | MethodDescriptor_vectorcall(PyObject *callable, PyObject *const *args, |
| 6106 | size_t nargsf, PyObject *kwnames) |
| 6107 | { |
| 6108 | /* True if using the vectorcall function in MethodDescriptorObject |
| 6109 | * but False for MethodDescriptor2Object */ |
| 6110 | MethodDescriptorObject *md = (MethodDescriptorObject *)callable; |
| 6111 | return PyBool_FromLong(md->vectorcall != NULL); |
| 6112 | } |
| 6113 | |
| 6114 | static PyObject * |
| 6115 | MethodDescriptor_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 6116 | { |
Petr Viktorin | e584cbf | 2019-06-03 01:08:14 +0200 | [diff] [blame] | 6117 | MethodDescriptorObject *op = (MethodDescriptorObject *)type->tp_alloc(type, 0); |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6118 | op->vectorcall = MethodDescriptor_vectorcall; |
| 6119 | return (PyObject *)op; |
| 6120 | } |
| 6121 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6122 | static PyObject * |
| 6123 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 6124 | { |
| 6125 | if (obj == Py_None || obj == NULL) { |
| 6126 | Py_INCREF(func); |
| 6127 | return func; |
| 6128 | } |
| 6129 | return PyMethod_New(func, obj); |
| 6130 | } |
| 6131 | |
| 6132 | static PyObject * |
| 6133 | nop_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 6134 | { |
| 6135 | Py_INCREF(func); |
| 6136 | return func; |
| 6137 | } |
| 6138 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6139 | static PyObject * |
| 6140 | call_return_args(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6141 | { |
| 6142 | Py_INCREF(args); |
| 6143 | return args; |
| 6144 | } |
| 6145 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6146 | static PyTypeObject MethodDescriptorBase_Type = { |
| 6147 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6148 | "MethodDescriptorBase", |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6149 | sizeof(MethodDescriptorObject), |
| 6150 | .tp_new = MethodDescriptor_new, |
| 6151 | .tp_call = PyVectorcall_Call, |
| 6152 | .tp_vectorcall_offset = offsetof(MethodDescriptorObject, vectorcall), |
| 6153 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 6154 | Py_TPFLAGS_METHOD_DESCRIPTOR | Py_TPFLAGS_HAVE_VECTORCALL, |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6155 | .tp_descr_get = func_descr_get, |
| 6156 | }; |
| 6157 | |
| 6158 | static PyTypeObject MethodDescriptorDerived_Type = { |
| 6159 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6160 | "MethodDescriptorDerived", |
| 6161 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6162 | }; |
| 6163 | |
| 6164 | static PyTypeObject MethodDescriptorNopGet_Type = { |
| 6165 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6166 | "MethodDescriptorNopGet", |
| 6167 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6168 | .tp_call = call_return_args, |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6169 | .tp_descr_get = nop_descr_get, |
| 6170 | }; |
| 6171 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6172 | typedef struct { |
| 6173 | MethodDescriptorObject base; |
| 6174 | vectorcallfunc vectorcall; |
| 6175 | } MethodDescriptor2Object; |
| 6176 | |
| 6177 | static PyObject * |
| 6178 | MethodDescriptor2_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 6179 | { |
| 6180 | MethodDescriptor2Object *op = PyObject_New(MethodDescriptor2Object, type); |
| 6181 | op->base.vectorcall = NULL; |
| 6182 | op->vectorcall = MethodDescriptor_vectorcall; |
| 6183 | return (PyObject *)op; |
| 6184 | } |
| 6185 | |
| 6186 | static PyTypeObject MethodDescriptor2_Type = { |
| 6187 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6188 | "MethodDescriptor2", |
| 6189 | sizeof(MethodDescriptor2Object), |
| 6190 | .tp_new = MethodDescriptor2_new, |
| 6191 | .tp_call = PyVectorcall_Call, |
| 6192 | .tp_vectorcall_offset = offsetof(MethodDescriptor2Object, vectorcall), |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 6193 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_VECTORCALL, |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6194 | }; |
| 6195 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6196 | PyDoc_STRVAR(heapgctype__doc__, |
| 6197 | "A heap type with GC, and with overridden dealloc.\n\n" |
| 6198 | "The 'value' attribute is set to 10 in __init__."); |
| 6199 | |
| 6200 | typedef struct { |
| 6201 | PyObject_HEAD |
| 6202 | int value; |
| 6203 | } HeapCTypeObject; |
| 6204 | |
| 6205 | static struct PyMemberDef heapctype_members[] = { |
| 6206 | {"value", T_INT, offsetof(HeapCTypeObject, value)}, |
| 6207 | {NULL} /* Sentinel */ |
| 6208 | }; |
| 6209 | |
| 6210 | static int |
| 6211 | heapctype_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6212 | { |
| 6213 | ((HeapCTypeObject *)self)->value = 10; |
| 6214 | return 0; |
| 6215 | } |
| 6216 | |
| 6217 | static void |
| 6218 | heapgcctype_dealloc(HeapCTypeObject *self) |
| 6219 | { |
| 6220 | PyTypeObject *tp = Py_TYPE(self); |
| 6221 | PyObject_GC_UnTrack(self); |
| 6222 | PyObject_GC_Del(self); |
| 6223 | Py_DECREF(tp); |
| 6224 | } |
| 6225 | |
| 6226 | static PyType_Slot HeapGcCType_slots[] = { |
| 6227 | {Py_tp_init, heapctype_init}, |
| 6228 | {Py_tp_members, heapctype_members}, |
| 6229 | {Py_tp_dealloc, heapgcctype_dealloc}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6230 | {Py_tp_doc, (char*)heapgctype__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6231 | {0, 0}, |
| 6232 | }; |
| 6233 | |
| 6234 | static PyType_Spec HeapGcCType_spec = { |
| 6235 | "_testcapi.HeapGcCType", |
| 6236 | sizeof(HeapCTypeObject), |
| 6237 | 0, |
| 6238 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6239 | HeapGcCType_slots |
| 6240 | }; |
| 6241 | |
| 6242 | PyDoc_STRVAR(heapctype__doc__, |
| 6243 | "A heap type without GC, but with overridden dealloc.\n\n" |
| 6244 | "The 'value' attribute is set to 10 in __init__."); |
| 6245 | |
| 6246 | static void |
| 6247 | heapctype_dealloc(HeapCTypeObject *self) |
| 6248 | { |
| 6249 | PyTypeObject *tp = Py_TYPE(self); |
| 6250 | PyObject_Del(self); |
| 6251 | Py_DECREF(tp); |
| 6252 | } |
| 6253 | |
| 6254 | static PyType_Slot HeapCType_slots[] = { |
| 6255 | {Py_tp_init, heapctype_init}, |
| 6256 | {Py_tp_members, heapctype_members}, |
| 6257 | {Py_tp_dealloc, heapctype_dealloc}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6258 | {Py_tp_doc, (char*)heapctype__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6259 | {0, 0}, |
| 6260 | }; |
| 6261 | |
| 6262 | static PyType_Spec HeapCType_spec = { |
| 6263 | "_testcapi.HeapCType", |
| 6264 | sizeof(HeapCTypeObject), |
| 6265 | 0, |
| 6266 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6267 | HeapCType_slots |
| 6268 | }; |
| 6269 | |
| 6270 | PyDoc_STRVAR(heapctypesubclass__doc__, |
| 6271 | "Subclass of HeapCType, without GC.\n\n" |
| 6272 | "__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 6273 | |
| 6274 | typedef struct { |
| 6275 | HeapCTypeObject base; |
| 6276 | int value2; |
| 6277 | } HeapCTypeSubclassObject; |
| 6278 | |
| 6279 | static int |
| 6280 | heapctypesubclass_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6281 | { |
| 6282 | /* Call __init__ of the superclass */ |
| 6283 | if (heapctype_init(self, args, kwargs) < 0) { |
| 6284 | return -1; |
| 6285 | } |
| 6286 | /* Initialize additional element */ |
| 6287 | ((HeapCTypeSubclassObject *)self)->value2 = 20; |
| 6288 | return 0; |
| 6289 | } |
| 6290 | |
| 6291 | static struct PyMemberDef heapctypesubclass_members[] = { |
| 6292 | {"value2", T_INT, offsetof(HeapCTypeSubclassObject, value2)}, |
| 6293 | {NULL} /* Sentinel */ |
| 6294 | }; |
| 6295 | |
| 6296 | static PyType_Slot HeapCTypeSubclass_slots[] = { |
| 6297 | {Py_tp_init, heapctypesubclass_init}, |
| 6298 | {Py_tp_members, heapctypesubclass_members}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6299 | {Py_tp_doc, (char*)heapctypesubclass__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6300 | {0, 0}, |
| 6301 | }; |
| 6302 | |
| 6303 | static PyType_Spec HeapCTypeSubclass_spec = { |
| 6304 | "_testcapi.HeapCTypeSubclass", |
| 6305 | sizeof(HeapCTypeSubclassObject), |
| 6306 | 0, |
| 6307 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6308 | HeapCTypeSubclass_slots |
| 6309 | }; |
| 6310 | |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 6311 | PyDoc_STRVAR(heapctypewithbuffer__doc__, |
| 6312 | "Heap type with buffer support.\n\n" |
| 6313 | "The buffer is set to [b'1', b'2', b'3', b'4']"); |
| 6314 | |
| 6315 | typedef struct { |
| 6316 | HeapCTypeObject base; |
| 6317 | char buffer[4]; |
| 6318 | } HeapCTypeWithBufferObject; |
| 6319 | |
| 6320 | static int |
| 6321 | heapctypewithbuffer_getbuffer(HeapCTypeWithBufferObject *self, Py_buffer *view, int flags) |
| 6322 | { |
| 6323 | self->buffer[0] = '1'; |
| 6324 | self->buffer[1] = '2'; |
| 6325 | self->buffer[2] = '3'; |
| 6326 | self->buffer[3] = '4'; |
| 6327 | return PyBuffer_FillInfo( |
| 6328 | view, (PyObject*)self, (void *)self->buffer, 4, 1, flags); |
| 6329 | } |
| 6330 | |
RĂ©mi Lapeyre | b8867e5 | 2020-06-07 09:05:33 +0200 | [diff] [blame] | 6331 | static void |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 6332 | heapctypewithbuffer_releasebuffer(HeapCTypeWithBufferObject *self, Py_buffer *view) |
| 6333 | { |
| 6334 | assert(view->obj == (void*) self); |
| 6335 | } |
| 6336 | |
| 6337 | static PyType_Slot HeapCTypeWithBuffer_slots[] = { |
| 6338 | {Py_bf_getbuffer, heapctypewithbuffer_getbuffer}, |
| 6339 | {Py_bf_releasebuffer, heapctypewithbuffer_releasebuffer}, |
| 6340 | {Py_tp_doc, (char*)heapctypewithbuffer__doc__}, |
| 6341 | {0, 0}, |
| 6342 | }; |
| 6343 | |
| 6344 | static PyType_Spec HeapCTypeWithBuffer_spec = { |
| 6345 | "_testcapi.HeapCTypeWithBuffer", |
| 6346 | sizeof(HeapCTypeWithBufferObject), |
| 6347 | 0, |
| 6348 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6349 | HeapCTypeWithBuffer_slots |
| 6350 | }; |
| 6351 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6352 | PyDoc_STRVAR(heapctypesubclasswithfinalizer__doc__, |
| 6353 | "Subclass of HeapCType with a finalizer that reassigns __class__.\n\n" |
| 6354 | "__class__ is set to plain HeapCTypeSubclass during finalization.\n" |
| 6355 | "__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 6356 | |
| 6357 | static int |
| 6358 | heapctypesubclasswithfinalizer_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6359 | { |
| 6360 | PyTypeObject *base = (PyTypeObject *)PyType_GetSlot(Py_TYPE(self), Py_tp_base); |
| 6361 | initproc base_init = PyType_GetSlot(base, Py_tp_init); |
| 6362 | base_init(self, args, kwargs); |
| 6363 | return 0; |
| 6364 | } |
| 6365 | |
| 6366 | static void |
| 6367 | heapctypesubclasswithfinalizer_finalize(PyObject *self) |
| 6368 | { |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6369 | PyObject *error_type, *error_value, *error_traceback, *m; |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6370 | PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL; |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6371 | |
| 6372 | /* Save the current exception, if any. */ |
| 6373 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 6374 | |
| 6375 | m = PyState_FindModule(&_testcapimodule); |
| 6376 | if (m == NULL) { |
| 6377 | goto cleanup_finalize; |
| 6378 | } |
| 6379 | oldtype = PyObject_GetAttrString(m, "HeapCTypeSubclassWithFinalizer"); |
| 6380 | newtype = PyObject_GetAttrString(m, "HeapCTypeSubclass"); |
| 6381 | if (oldtype == NULL || newtype == NULL) { |
| 6382 | goto cleanup_finalize; |
| 6383 | } |
| 6384 | |
| 6385 | if (PyObject_SetAttrString(self, "__class__", newtype) < 0) { |
| 6386 | goto cleanup_finalize; |
| 6387 | } |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6388 | refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype)); |
| 6389 | if (refcnt == NULL) { |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6390 | goto cleanup_finalize; |
| 6391 | } |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6392 | if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) { |
| 6393 | goto cleanup_finalize; |
| 6394 | } |
| 6395 | Py_DECREF(refcnt); |
| 6396 | refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype)); |
| 6397 | if (refcnt == NULL) { |
| 6398 | goto cleanup_finalize; |
| 6399 | } |
| 6400 | if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) { |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6401 | goto cleanup_finalize; |
| 6402 | } |
| 6403 | |
| 6404 | cleanup_finalize: |
| 6405 | Py_XDECREF(oldtype); |
| 6406 | Py_XDECREF(newtype); |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6407 | Py_XDECREF(refcnt); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6408 | |
| 6409 | /* Restore the saved exception. */ |
| 6410 | PyErr_Restore(error_type, error_value, error_traceback); |
| 6411 | } |
| 6412 | |
| 6413 | static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = { |
| 6414 | {Py_tp_init, heapctypesubclasswithfinalizer_init}, |
| 6415 | {Py_tp_members, heapctypesubclass_members}, |
| 6416 | {Py_tp_finalize, heapctypesubclasswithfinalizer_finalize}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6417 | {Py_tp_doc, (char*)heapctypesubclasswithfinalizer__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6418 | {0, 0}, |
| 6419 | }; |
| 6420 | |
| 6421 | static PyType_Spec HeapCTypeSubclassWithFinalizer_spec = { |
| 6422 | "_testcapi.HeapCTypeSubclassWithFinalizer", |
| 6423 | sizeof(HeapCTypeSubclassObject), |
| 6424 | 0, |
| 6425 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 6426 | HeapCTypeSubclassWithFinalizer_slots |
| 6427 | }; |
| 6428 | |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6429 | typedef struct { |
| 6430 | PyObject_HEAD |
| 6431 | PyObject *dict; |
| 6432 | } HeapCTypeWithDictObject; |
| 6433 | |
| 6434 | static void |
| 6435 | heapctypewithdict_dealloc(HeapCTypeWithDictObject* self) |
| 6436 | { |
| 6437 | |
| 6438 | PyTypeObject *tp = Py_TYPE(self); |
| 6439 | Py_XDECREF(self->dict); |
| 6440 | PyObject_DEL(self); |
| 6441 | Py_DECREF(tp); |
| 6442 | } |
| 6443 | |
| 6444 | static PyGetSetDef heapctypewithdict_getsetlist[] = { |
| 6445 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
| 6446 | {NULL} /* Sentinel */ |
| 6447 | }; |
| 6448 | |
| 6449 | static struct PyMemberDef heapctypewithdict_members[] = { |
| 6450 | {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
| 6451 | {"__dictoffset__", T_PYSSIZET, offsetof(HeapCTypeWithDictObject, dict), READONLY}, |
| 6452 | {NULL} /* Sentinel */ |
| 6453 | }; |
| 6454 | |
| 6455 | static PyType_Slot HeapCTypeWithDict_slots[] = { |
| 6456 | {Py_tp_members, heapctypewithdict_members}, |
| 6457 | {Py_tp_getset, heapctypewithdict_getsetlist}, |
| 6458 | {Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 6459 | {0, 0}, |
| 6460 | }; |
| 6461 | |
| 6462 | static PyType_Spec HeapCTypeWithDict_spec = { |
| 6463 | "_testcapi.HeapCTypeWithDict", |
| 6464 | sizeof(HeapCTypeWithDictObject), |
| 6465 | 0, |
| 6466 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6467 | HeapCTypeWithDict_slots |
| 6468 | }; |
| 6469 | |
| 6470 | static struct PyMemberDef heapctypewithnegativedict_members[] = { |
| 6471 | {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
Victor Stinner | aca8c40 | 2019-09-30 21:14:26 +0200 | [diff] [blame] | 6472 | {"__dictoffset__", T_PYSSIZET, -(Py_ssize_t)sizeof(void*), READONLY}, |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6473 | {NULL} /* Sentinel */ |
| 6474 | }; |
| 6475 | |
| 6476 | static PyType_Slot HeapCTypeWithNegativeDict_slots[] = { |
| 6477 | {Py_tp_members, heapctypewithnegativedict_members}, |
| 6478 | {Py_tp_getset, heapctypewithdict_getsetlist}, |
| 6479 | {Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 6480 | {0, 0}, |
| 6481 | }; |
| 6482 | |
| 6483 | static PyType_Spec HeapCTypeWithNegativeDict_spec = { |
| 6484 | "_testcapi.HeapCTypeWithNegativeDict", |
| 6485 | sizeof(HeapCTypeWithDictObject), |
| 6486 | 0, |
| 6487 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6488 | HeapCTypeWithNegativeDict_slots |
| 6489 | }; |
| 6490 | |
| 6491 | typedef struct { |
| 6492 | PyObject_HEAD |
| 6493 | PyObject *weakreflist; |
| 6494 | } HeapCTypeWithWeakrefObject; |
| 6495 | |
| 6496 | static struct PyMemberDef heapctypewithweakref_members[] = { |
| 6497 | {"weakreflist", T_OBJECT, offsetof(HeapCTypeWithWeakrefObject, weakreflist)}, |
| 6498 | {"__weaklistoffset__", T_PYSSIZET, |
| 6499 | offsetof(HeapCTypeWithWeakrefObject, weakreflist), READONLY}, |
| 6500 | {NULL} /* Sentinel */ |
| 6501 | }; |
| 6502 | |
| 6503 | static void |
| 6504 | heapctypewithweakref_dealloc(HeapCTypeWithWeakrefObject* self) |
| 6505 | { |
| 6506 | |
| 6507 | PyTypeObject *tp = Py_TYPE(self); |
| 6508 | if (self->weakreflist != NULL) |
| 6509 | PyObject_ClearWeakRefs((PyObject *) self); |
| 6510 | Py_XDECREF(self->weakreflist); |
| 6511 | PyObject_DEL(self); |
| 6512 | Py_DECREF(tp); |
| 6513 | } |
| 6514 | |
| 6515 | static PyType_Slot HeapCTypeWithWeakref_slots[] = { |
| 6516 | {Py_tp_members, heapctypewithweakref_members}, |
| 6517 | {Py_tp_dealloc, heapctypewithweakref_dealloc}, |
| 6518 | {0, 0}, |
| 6519 | }; |
| 6520 | |
| 6521 | static PyType_Spec HeapCTypeWithWeakref_spec = { |
| 6522 | "_testcapi.HeapCTypeWithWeakref", |
| 6523 | sizeof(HeapCTypeWithWeakrefObject), |
| 6524 | 0, |
| 6525 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6526 | HeapCTypeWithWeakref_slots |
| 6527 | }; |
| 6528 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6529 | static PyMethodDef meth_instance_methods[] = { |
| 6530 | {"meth_varargs", meth_varargs, METH_VARARGS}, |
| 6531 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, |
| 6532 | {"meth_o", meth_o, METH_O}, |
| 6533 | {"meth_noargs", meth_noargs, METH_NOARGS}, |
| 6534 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, |
| 6535 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS}, |
| 6536 | {NULL, NULL} /* sentinel */ |
| 6537 | }; |
| 6538 | |
| 6539 | |
| 6540 | static PyTypeObject MethInstance_Type = { |
| 6541 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6542 | "MethInstance", |
| 6543 | sizeof(PyObject), |
| 6544 | .tp_new = PyType_GenericNew, |
| 6545 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6546 | .tp_methods = meth_instance_methods, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6547 | .tp_doc = (char*)PyDoc_STR( |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6548 | "Class with normal (instance) methods to test calling conventions"), |
| 6549 | }; |
| 6550 | |
| 6551 | static PyMethodDef meth_class_methods[] = { |
| 6552 | {"meth_varargs", meth_varargs, METH_VARARGS|METH_CLASS}, |
| 6553 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_CLASS}, |
| 6554 | {"meth_o", meth_o, METH_O|METH_CLASS}, |
| 6555 | {"meth_noargs", meth_noargs, METH_NOARGS|METH_CLASS}, |
| 6556 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_CLASS}, |
| 6557 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_CLASS}, |
| 6558 | {NULL, NULL} /* sentinel */ |
| 6559 | }; |
| 6560 | |
| 6561 | |
| 6562 | static PyTypeObject MethClass_Type = { |
| 6563 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6564 | "MethClass", |
| 6565 | sizeof(PyObject), |
| 6566 | .tp_new = PyType_GenericNew, |
| 6567 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6568 | .tp_methods = meth_class_methods, |
| 6569 | .tp_doc = PyDoc_STR( |
| 6570 | "Class with class methods to test calling conventions"), |
| 6571 | }; |
| 6572 | |
| 6573 | static PyMethodDef meth_static_methods[] = { |
| 6574 | {"meth_varargs", meth_varargs, METH_VARARGS|METH_STATIC}, |
| 6575 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_STATIC}, |
| 6576 | {"meth_o", meth_o, METH_O|METH_STATIC}, |
| 6577 | {"meth_noargs", meth_noargs, METH_NOARGS|METH_STATIC}, |
| 6578 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_STATIC}, |
| 6579 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_STATIC}, |
| 6580 | {NULL, NULL} /* sentinel */ |
| 6581 | }; |
| 6582 | |
| 6583 | |
| 6584 | static PyTypeObject MethStatic_Type = { |
| 6585 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6586 | "MethStatic", |
| 6587 | sizeof(PyObject), |
| 6588 | .tp_new = PyType_GenericNew, |
| 6589 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6590 | .tp_methods = meth_static_methods, |
| 6591 | .tp_doc = PyDoc_STR( |
| 6592 | "Class with static methods to test calling conventions"), |
| 6593 | }; |
| 6594 | |
Neil Schemenauer | 392a13b | 2019-10-15 20:56:48 -0700 | [diff] [blame] | 6595 | /* ContainerNoGC -- a simple container without GC methods */ |
| 6596 | |
| 6597 | typedef struct { |
| 6598 | PyObject_HEAD |
| 6599 | PyObject *value; |
| 6600 | } ContainerNoGCobject; |
| 6601 | |
| 6602 | static PyObject * |
| 6603 | ContainerNoGC_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 6604 | { |
| 6605 | PyObject *value; |
| 6606 | char *names[] = {"value", NULL}; |
| 6607 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", names, &value)) { |
| 6608 | return NULL; |
| 6609 | } |
| 6610 | PyObject *self = type->tp_alloc(type, 0); |
| 6611 | if (self == NULL) { |
| 6612 | return NULL; |
| 6613 | } |
| 6614 | Py_INCREF(value); |
| 6615 | ((ContainerNoGCobject *)self)->value = value; |
| 6616 | return self; |
| 6617 | } |
| 6618 | |
| 6619 | static void |
| 6620 | ContainerNoGC_dealloc(ContainerNoGCobject *self) |
| 6621 | { |
| 6622 | Py_DECREF(self->value); |
| 6623 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6624 | } |
| 6625 | |
| 6626 | static PyMemberDef ContainerNoGC_members[] = { |
| 6627 | {"value", T_OBJECT, offsetof(ContainerNoGCobject, value), READONLY, |
| 6628 | PyDoc_STR("a container value for test purposes")}, |
| 6629 | {0} |
| 6630 | }; |
| 6631 | |
| 6632 | static PyTypeObject ContainerNoGC_type = { |
| 6633 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6634 | "_testcapi.ContainerNoGC", |
| 6635 | sizeof(ContainerNoGCobject), |
| 6636 | .tp_dealloc = (destructor)ContainerNoGC_dealloc, |
| 6637 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6638 | .tp_members = ContainerNoGC_members, |
| 6639 | .tp_new = ContainerNoGC_new, |
| 6640 | }; |
| 6641 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6642 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6643 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6644 | PyModuleDef_HEAD_INIT, |
| 6645 | "_testcapi", |
| 6646 | NULL, |
| 6647 | -1, |
| 6648 | TestMethods, |
| 6649 | NULL, |
| 6650 | NULL, |
| 6651 | NULL, |
| 6652 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6653 | }; |
| 6654 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 6655 | /* Per PEP 489, this module will not be converted to multi-phase initialization |
| 6656 | */ |
| 6657 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 6658 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6659 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6661 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6663 | m = PyModule_Create(&_testcapimodule); |
| 6664 | if (m == NULL) |
| 6665 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6666 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 6667 | Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type); |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 6668 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 6669 | Py_SET_TYPE(&test_structmembersType, &PyType_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6670 | Py_INCREF(&test_structmembersType); |
| 6671 | /* don't use a name starting with "test", since we don't want |
| 6672 | test_capi to automatically call this */ |
| 6673 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6674 | if (PyType_Ready(&matmulType) < 0) |
| 6675 | return NULL; |
| 6676 | Py_INCREF(&matmulType); |
| 6677 | PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); |
Zackery Spytz | c7f803b | 2019-05-31 03:46:36 -0600 | [diff] [blame] | 6678 | if (PyType_Ready(&ipowType) < 0) { |
| 6679 | return NULL; |
| 6680 | } |
| 6681 | Py_INCREF(&ipowType); |
| 6682 | PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6683 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6684 | if (PyType_Ready(&awaitType) < 0) |
| 6685 | return NULL; |
| 6686 | Py_INCREF(&awaitType); |
| 6687 | PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType); |
| 6688 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 6689 | MyList_Type.tp_base = &PyList_Type; |
| 6690 | if (PyType_Ready(&MyList_Type) < 0) |
| 6691 | return NULL; |
| 6692 | Py_INCREF(&MyList_Type); |
| 6693 | PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type); |
| 6694 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6695 | if (PyType_Ready(&MethodDescriptorBase_Type) < 0) |
| 6696 | return NULL; |
| 6697 | Py_INCREF(&MethodDescriptorBase_Type); |
| 6698 | PyModule_AddObject(m, "MethodDescriptorBase", (PyObject *)&MethodDescriptorBase_Type); |
| 6699 | |
| 6700 | MethodDescriptorDerived_Type.tp_base = &MethodDescriptorBase_Type; |
| 6701 | if (PyType_Ready(&MethodDescriptorDerived_Type) < 0) |
| 6702 | return NULL; |
| 6703 | Py_INCREF(&MethodDescriptorDerived_Type); |
| 6704 | PyModule_AddObject(m, "MethodDescriptorDerived", (PyObject *)&MethodDescriptorDerived_Type); |
| 6705 | |
| 6706 | MethodDescriptorNopGet_Type.tp_base = &MethodDescriptorBase_Type; |
| 6707 | if (PyType_Ready(&MethodDescriptorNopGet_Type) < 0) |
| 6708 | return NULL; |
| 6709 | Py_INCREF(&MethodDescriptorNopGet_Type); |
| 6710 | PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type); |
| 6711 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6712 | MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type; |
| 6713 | if (PyType_Ready(&MethodDescriptor2_Type) < 0) |
| 6714 | return NULL; |
| 6715 | Py_INCREF(&MethodDescriptor2_Type); |
| 6716 | PyModule_AddObject(m, "MethodDescriptor2", (PyObject *)&MethodDescriptor2_Type); |
| 6717 | |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6718 | if (PyType_Ready(&GenericAlias_Type) < 0) |
| 6719 | return NULL; |
| 6720 | Py_INCREF(&GenericAlias_Type); |
| 6721 | PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type); |
| 6722 | |
| 6723 | if (PyType_Ready(&Generic_Type) < 0) |
| 6724 | return NULL; |
| 6725 | Py_INCREF(&Generic_Type); |
| 6726 | PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type); |
| 6727 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6728 | if (PyType_Ready(&MethInstance_Type) < 0) |
| 6729 | return NULL; |
| 6730 | Py_INCREF(&MethInstance_Type); |
| 6731 | PyModule_AddObject(m, "MethInstance", (PyObject *)&MethInstance_Type); |
| 6732 | |
| 6733 | if (PyType_Ready(&MethClass_Type) < 0) |
| 6734 | return NULL; |
| 6735 | Py_INCREF(&MethClass_Type); |
| 6736 | PyModule_AddObject(m, "MethClass", (PyObject *)&MethClass_Type); |
| 6737 | |
| 6738 | if (PyType_Ready(&MethStatic_Type) < 0) |
| 6739 | return NULL; |
| 6740 | Py_INCREF(&MethStatic_Type); |
| 6741 | PyModule_AddObject(m, "MethStatic", (PyObject *)&MethStatic_Type); |
| 6742 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 6743 | PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception; |
| 6744 | if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) { |
| 6745 | return NULL; |
| 6746 | } |
| 6747 | Py_INCREF(&PyRecursingInfinitelyError_Type); |
| 6748 | PyModule_AddObject(m, "RecursingInfinitelyError", |
| 6749 | (PyObject *)&PyRecursingInfinitelyError_Type); |
| 6750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6751 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 6752 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 6753 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 6754 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 6755 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 6756 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 6757 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 6758 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 6759 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 6760 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 6761 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 6762 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 6763 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 6764 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 6765 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 6766 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 6767 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX)); |
| 6768 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN)); |
| 6769 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6770 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 6771 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 6772 | PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6773 | Py_INCREF(&PyInstanceMethod_Type); |
| 6774 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 6775 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 6776 | PyModule_AddIntConstant(m, "the_number_three", 3); |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6777 | PyObject *v; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6778 | #ifdef WITH_PYMALLOC |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6779 | v = Py_True; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6780 | #else |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6781 | v = Py_False; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6782 | #endif |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6783 | Py_INCREF(v); |
| 6784 | PyModule_AddObject(m, "WITH_PYMALLOC", v); |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 6785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6786 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 6787 | Py_INCREF(TestError); |
| 6788 | PyModule_AddObject(m, "error", TestError); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6789 | |
| 6790 | PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec); |
| 6791 | if (HeapGcCType == NULL) { |
| 6792 | return NULL; |
| 6793 | } |
| 6794 | PyModule_AddObject(m, "HeapGcCType", HeapGcCType); |
| 6795 | |
| 6796 | PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec); |
| 6797 | if (HeapCType == NULL) { |
| 6798 | return NULL; |
| 6799 | } |
| 6800 | PyObject *subclass_bases = PyTuple_Pack(1, HeapCType); |
| 6801 | if (subclass_bases == NULL) { |
| 6802 | return NULL; |
| 6803 | } |
| 6804 | PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases); |
| 6805 | if (HeapCTypeSubclass == NULL) { |
| 6806 | return NULL; |
| 6807 | } |
| 6808 | Py_DECREF(subclass_bases); |
| 6809 | PyModule_AddObject(m, "HeapCTypeSubclass", HeapCTypeSubclass); |
| 6810 | |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6811 | PyObject *HeapCTypeWithDict = PyType_FromSpec(&HeapCTypeWithDict_spec); |
| 6812 | if (HeapCTypeWithDict == NULL) { |
| 6813 | return NULL; |
| 6814 | } |
| 6815 | PyModule_AddObject(m, "HeapCTypeWithDict", HeapCTypeWithDict); |
| 6816 | |
| 6817 | PyObject *HeapCTypeWithNegativeDict = PyType_FromSpec(&HeapCTypeWithNegativeDict_spec); |
| 6818 | if (HeapCTypeWithNegativeDict == NULL) { |
| 6819 | return NULL; |
| 6820 | } |
| 6821 | PyModule_AddObject(m, "HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict); |
| 6822 | |
| 6823 | PyObject *HeapCTypeWithWeakref = PyType_FromSpec(&HeapCTypeWithWeakref_spec); |
| 6824 | if (HeapCTypeWithWeakref == NULL) { |
| 6825 | return NULL; |
| 6826 | } |
| 6827 | PyModule_AddObject(m, "HeapCTypeWithWeakref", HeapCTypeWithWeakref); |
| 6828 | |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 6829 | PyObject *HeapCTypeWithBuffer = PyType_FromSpec(&HeapCTypeWithBuffer_spec); |
| 6830 | if (HeapCTypeWithBuffer == NULL) { |
| 6831 | return NULL; |
| 6832 | } |
| 6833 | PyModule_AddObject(m, "HeapCTypeWithBuffer", HeapCTypeWithBuffer); |
| 6834 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6835 | PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass); |
| 6836 | if (subclass_with_finalizer_bases == NULL) { |
| 6837 | return NULL; |
| 6838 | } |
| 6839 | PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases( |
| 6840 | &HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases); |
| 6841 | if (HeapCTypeSubclassWithFinalizer == NULL) { |
| 6842 | return NULL; |
| 6843 | } |
| 6844 | Py_DECREF(subclass_with_finalizer_bases); |
| 6845 | PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer); |
| 6846 | |
Neil Schemenauer | 392a13b | 2019-10-15 20:56:48 -0700 | [diff] [blame] | 6847 | if (PyType_Ready(&ContainerNoGC_type) < 0) { |
| 6848 | return NULL; |
| 6849 | } |
| 6850 | Py_INCREF(&ContainerNoGC_type); |
| 6851 | if (PyModule_AddObject(m, "ContainerNoGC", |
| 6852 | (PyObject *) &ContainerNoGC_type) < 0) |
| 6853 | return NULL; |
| 6854 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6855 | PyState_AddModule(m, &_testcapimodule); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6856 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6857 | } |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6858 | |
| 6859 | |
| 6860 | /* Test the C API exposed when PY_SSIZE_T_CLEAN is not defined */ |
| 6861 | |
| 6862 | #undef Py_BuildValue |
| 6863 | PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...); |
| 6864 | |
| 6865 | static PyObject * |
| 6866 | test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 6867 | { |
| 6868 | PyObject *res; |
| 6869 | const char str[] = "string"; |
| 6870 | const Py_UNICODE unicode[] = L"unicode"; |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6871 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6872 | |
| 6873 | res = Py_BuildValue("(s#O)", str, 1, Py_None); |
| 6874 | assert(res == NULL); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6875 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6876 | return NULL; |
| 6877 | } |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6878 | PyErr_Clear(); |
| 6879 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6880 | res = Py_BuildValue("(z#O)", str, 1, Py_None); |
| 6881 | assert(res == NULL); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6882 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6883 | return NULL; |
| 6884 | } |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6885 | PyErr_Clear(); |
| 6886 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6887 | res = Py_BuildValue("(y#O)", str, 1, Py_None); |
| 6888 | assert(res == NULL); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6889 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6890 | return NULL; |
| 6891 | } |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6892 | PyErr_Clear(); |
| 6893 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6894 | res = Py_BuildValue("(u#O)", unicode, 1, Py_None); |
| 6895 | assert(res == NULL); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6896 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6897 | return NULL; |
| 6898 | } |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6899 | PyErr_Clear(); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 6900 | |
| 6901 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6902 | Py_RETURN_NONE; |
| 6903 | } |