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 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1671 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1672 | of an error. |
| 1673 | */ |
| 1674 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1675 | test_u_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | PyObject *tuple, *obj; |
| 1678 | Py_UNICODE *value; |
| 1679 | Py_ssize_t len; |
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 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1682 | /* Just use the macro and check that it compiles */ |
| 1683 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | tuple = PyTuple_New(1); |
| 1686 | if (tuple == NULL) |
| 1687 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1690 | "ascii", NULL); |
| 1691 | if (obj == NULL) |
| 1692 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1697 | if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1698 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1699 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1701 | return raiseTestError("test_u_code", |
| 1702 | "u code returned wrong value for u'test'"); |
| 1703 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1704 | if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1706 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1708 | len != PyUnicode_GET_SIZE(obj)) |
| 1709 | return raiseTestError("test_u_code", |
| 1710 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1713 | Py_RETURN_NONE; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1716 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1717 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1718 | test_Z_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | PyObject *tuple, *obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1721 | const Py_UNICODE *value1, *value2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | Py_ssize_t len1, len2; |
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 | tuple = PyTuple_New(2); |
| 1725 | if (tuple == NULL) |
| 1726 | return NULL; |
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 | obj = PyUnicode_FromString("test"); |
| 1729 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1730 | Py_INCREF(Py_None); |
| 1731 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | /* swap values on purpose */ |
| 1734 | value1 = NULL; |
| 1735 | value2 = PyUnicode_AS_UNICODE(obj); |
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 | /* Test Z for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1738 | if (!PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1740 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1742 | return raiseTestError("test_Z_code", |
| 1743 | "Z code returned wrong value for 'test'"); |
| 1744 | if (value2 != NULL) |
| 1745 | return raiseTestError("test_Z_code", |
| 1746 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | value1 = NULL; |
| 1749 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1750 | len1 = -1; |
| 1751 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1753 | /* Test Z# for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1754 | if (!PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1755 | &value2, &len2)) |
| 1756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1758 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1760 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1761 | return raiseTestError("test_Z_code", |
| 1762 | "Z# code returned wrong values for 'test'"); |
| 1763 | if (value2 != NULL || |
| 1764 | len2 != 0) |
| 1765 | return raiseTestError("test_Z_code", |
| 1766 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | Py_DECREF(tuple); |
| 1769 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1772 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1773 | test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1774 | { |
| 1775 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1777 | size_t wtextlen = 1; |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1778 | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1779 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1781 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1782 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1786 | if (wide == NULL) |
| 1787 | return NULL; |
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 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1790 | if (utf8 == NULL) { |
| 1791 | Py_DECREF(wide); |
| 1792 | return NULL; |
| 1793 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1794 | |
Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1795 | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | Py_DECREF(wide); |
| 1797 | Py_DECREF(utf8); |
| 1798 | return raiseTestError("test_widechar", |
| 1799 | "wide string and utf8 string " |
| 1800 | "have different length"); |
| 1801 | } |
| 1802 | if (PyUnicode_Compare(wide, utf8)) { |
| 1803 | Py_DECREF(wide); |
| 1804 | Py_DECREF(utf8); |
| 1805 | if (PyErr_Occurred()) |
| 1806 | return NULL; |
| 1807 | return raiseTestError("test_widechar", |
| 1808 | "wide string and utf8 string " |
| 1809 | "are different"); |
| 1810 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1812 | Py_DECREF(wide); |
| 1813 | Py_DECREF(utf8); |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1814 | |
| 1815 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1816 | wide = PyUnicode_FromWideChar(invalid, 1); |
| 1817 | if (wide == NULL) |
| 1818 | PyErr_Clear(); |
| 1819 | else |
| 1820 | return raiseTestError("test_widechar", |
| 1821 | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
| 1822 | |
| 1823 | wide = PyUnicode_FromUnicode(invalid, 1); |
| 1824 | if (wide == NULL) |
| 1825 | PyErr_Clear(); |
| 1826 | else |
| 1827 | return raiseTestError("test_widechar", |
| 1828 | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1829 | |
| 1830 | wide = PyUnicode_FromUnicode(NULL, 1); |
| 1831 | if (wide == NULL) |
| 1832 | return NULL; |
| 1833 | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1834 | if (_PyUnicode_Ready(wide) < 0) { |
| 1835 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1836 | PyErr_Clear(); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1837 | } |
| 1838 | else { |
| 1839 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1840 | return raiseTestError("test_widechar", |
| 1841 | "PyUnicode_Ready() didn't fail"); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1842 | } |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1843 | #endif |
| 1844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1849 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1850 | { |
| 1851 | PyObject *unicode, *result; |
| 1852 | Py_ssize_t buflen, size; |
| 1853 | wchar_t *buffer; |
| 1854 | |
| 1855 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1856 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1857 | buffer = PyMem_New(wchar_t, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1858 | if (buffer == NULL) |
| 1859 | return PyErr_NoMemory(); |
| 1860 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1861 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1862 | if (size == -1) { |
| 1863 | PyMem_Free(buffer); |
| 1864 | return NULL; |
| 1865 | } |
| 1866 | |
| 1867 | if (size < buflen) |
| 1868 | buflen = size + 1; |
| 1869 | else |
| 1870 | buflen = size; |
| 1871 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 1872 | PyMem_Free(buffer); |
| 1873 | if (result == NULL) |
| 1874 | return NULL; |
| 1875 | |
| 1876 | return Py_BuildValue("(Nn)", result, size); |
| 1877 | } |
| 1878 | |
| 1879 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1880 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1881 | { |
| 1882 | PyObject *unicode, *result; |
| 1883 | Py_ssize_t size; |
| 1884 | wchar_t *buffer; |
| 1885 | |
| 1886 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 1887 | return NULL; |
| 1888 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1889 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1890 | if (buffer == NULL) |
| 1891 | return NULL; |
| 1892 | |
| 1893 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 1894 | PyMem_Free(buffer); |
| 1895 | if (result == NULL) |
| 1896 | return NULL; |
| 1897 | return Py_BuildValue("(Nn)", result, size); |
| 1898 | } |
| 1899 | |
| 1900 | static PyObject * |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 1901 | unicode_asucs4(PyObject *self, PyObject *args) |
| 1902 | { |
| 1903 | PyObject *unicode, *result; |
| 1904 | Py_UCS4 *buffer; |
| 1905 | int copy_null; |
| 1906 | Py_ssize_t str_len, buf_len; |
| 1907 | |
| 1908 | if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, ©_null)) { |
| 1909 | return NULL; |
| 1910 | } |
| 1911 | |
| 1912 | buf_len = str_len + 1; |
| 1913 | buffer = PyMem_NEW(Py_UCS4, buf_len); |
| 1914 | if (buffer == NULL) { |
| 1915 | return PyErr_NoMemory(); |
| 1916 | } |
| 1917 | memset(buffer, 0, sizeof(Py_UCS4)*buf_len); |
| 1918 | buffer[str_len] = 0xffffU; |
| 1919 | |
| 1920 | if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) { |
| 1921 | PyMem_FREE(buffer); |
| 1922 | return NULL; |
| 1923 | } |
| 1924 | |
| 1925 | result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len); |
| 1926 | PyMem_FREE(buffer); |
| 1927 | return result; |
| 1928 | } |
| 1929 | |
| 1930 | static PyObject * |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 1931 | unicode_asutf8(PyObject *self, PyObject *args) |
| 1932 | { |
| 1933 | PyObject *unicode; |
| 1934 | const char *buffer; |
| 1935 | |
| 1936 | if (!PyArg_ParseTuple(args, "U", &unicode)) { |
| 1937 | return NULL; |
| 1938 | } |
| 1939 | |
| 1940 | buffer = PyUnicode_AsUTF8(unicode); |
| 1941 | if (buffer == NULL) { |
| 1942 | return NULL; |
| 1943 | } |
| 1944 | |
| 1945 | return PyBytes_FromString(buffer); |
| 1946 | } |
| 1947 | |
| 1948 | static PyObject * |
| 1949 | unicode_asutf8andsize(PyObject *self, PyObject *args) |
| 1950 | { |
| 1951 | PyObject *unicode, *result; |
| 1952 | const char *buffer; |
| 1953 | Py_ssize_t utf8_len; |
| 1954 | |
| 1955 | if(!PyArg_ParseTuple(args, "U", &unicode)) { |
| 1956 | return NULL; |
| 1957 | } |
| 1958 | |
Victor Stinner | aca8c40 | 2019-09-30 21:14:26 +0200 | [diff] [blame] | 1959 | buffer = PyUnicode_AsUTF8AndSize(unicode, &utf8_len); |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 1960 | if (buffer == NULL) { |
| 1961 | return NULL; |
| 1962 | } |
| 1963 | |
| 1964 | result = PyBytes_FromString(buffer); |
| 1965 | if (result == NULL) { |
| 1966 | return NULL; |
| 1967 | } |
| 1968 | |
| 1969 | return Py_BuildValue("(Nn)", result, utf8_len); |
| 1970 | } |
| 1971 | |
| 1972 | static PyObject * |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 1973 | unicode_findchar(PyObject *self, PyObject *args) |
| 1974 | { |
| 1975 | PyObject *str; |
| 1976 | int direction; |
| 1977 | unsigned int ch; |
| 1978 | Py_ssize_t result; |
| 1979 | Py_ssize_t start, end; |
| 1980 | |
| 1981 | if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch, |
| 1982 | &start, &end, &direction)) { |
| 1983 | return NULL; |
| 1984 | } |
| 1985 | |
| 1986 | result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction); |
| 1987 | if (result == -2) |
| 1988 | return NULL; |
| 1989 | else |
| 1990 | return PyLong_FromSsize_t(result); |
| 1991 | } |
| 1992 | |
| 1993 | static PyObject * |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 1994 | unicode_copycharacters(PyObject *self, PyObject *args) |
| 1995 | { |
| 1996 | PyObject *from, *to, *to_copy; |
| 1997 | Py_ssize_t from_start, to_start, how_many, copied; |
| 1998 | |
| 1999 | if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start, |
| 2000 | &from, &from_start, &how_many)) { |
| 2001 | return NULL; |
| 2002 | } |
| 2003 | |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 2004 | if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to), |
| 2005 | PyUnicode_MAX_CHAR_VALUE(to)))) { |
| 2006 | return NULL; |
| 2007 | } |
| 2008 | if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) { |
| 2009 | Py_DECREF(to_copy); |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | |
| 2013 | if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from, |
| 2014 | from_start, how_many)) < 0) { |
| 2015 | Py_DECREF(to_copy); |
| 2016 | return NULL; |
| 2017 | } |
| 2018 | |
| 2019 | return Py_BuildValue("(Nn)", to_copy, copied); |
| 2020 | } |
| 2021 | |
| 2022 | static PyObject * |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2023 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 2024 | { |
| 2025 | Py_UNICODE *unicode; |
| 2026 | Py_ssize_t length; |
| 2027 | char *errors = NULL; |
| 2028 | PyObject *decimal; |
| 2029 | Py_ssize_t decimal_length, new_length; |
| 2030 | int res; |
| 2031 | |
| 2032 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 2033 | return NULL; |
| 2034 | |
| 2035 | decimal_length = length * 7; /* len('€') */ |
| 2036 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 2037 | if (decimal == NULL) |
| 2038 | return NULL; |
| 2039 | |
| 2040 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 2041 | PyBytes_AS_STRING(decimal), |
| 2042 | errors); |
| 2043 | if (res < 0) { |
| 2044 | Py_DECREF(decimal); |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | |
| 2048 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 2049 | assert(new_length <= decimal_length); |
| 2050 | res = _PyBytes_Resize(&decimal, new_length); |
| 2051 | if (res < 0) |
| 2052 | return NULL; |
| 2053 | |
| 2054 | return decimal; |
| 2055 | } |
| 2056 | |
| 2057 | static PyObject * |
| 2058 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 2059 | { |
| 2060 | Py_UNICODE *unicode; |
| 2061 | Py_ssize_t length; |
| 2062 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 2063 | return NULL; |
| 2064 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 2065 | } |
| 2066 | |
| 2067 | static PyObject * |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 2068 | unicode_legacy_string(PyObject *self, PyObject *args) |
| 2069 | { |
| 2070 | Py_UNICODE *data; |
| 2071 | Py_ssize_t len; |
| 2072 | PyObject *u; |
| 2073 | |
| 2074 | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
| 2075 | return NULL; |
| 2076 | |
| 2077 | u = PyUnicode_FromUnicode(NULL, len); |
| 2078 | if (u == NULL) |
| 2079 | return NULL; |
| 2080 | |
| 2081 | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
| 2082 | |
| 2083 | if (len > 0) { /* The empty string is always ready. */ |
| 2084 | assert(!PyUnicode_IS_READY(u)); |
| 2085 | } |
| 2086 | |
| 2087 | return u; |
| 2088 | } |
| 2089 | |
| 2090 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2091 | getargs_w_star(PyObject *self, PyObject *args) |
| 2092 | { |
| 2093 | Py_buffer buffer; |
| 2094 | PyObject *result; |
| 2095 | char *str; |
| 2096 | |
| 2097 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 2098 | return NULL; |
| 2099 | |
| 2100 | if (2 <= buffer.len) { |
| 2101 | str = buffer.buf; |
| 2102 | str[0] = '['; |
| 2103 | str[buffer.len-1] = ']'; |
| 2104 | } |
| 2105 | |
| 2106 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 2107 | PyBuffer_Release(&buffer); |
| 2108 | return result; |
| 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2113 | test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2115 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 2116 | PyObject *tuple, *dict = NULL; |
| 2117 | static char *kwlist[] = {NULL}; |
| 2118 | int result; |
| 2119 | tuple = PyTuple_New(0); |
| 2120 | if (!tuple) |
| 2121 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2122 | if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | goto done; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2124 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | dict = PyDict_New(); |
| 2126 | if (!dict) |
| 2127 | goto done; |
| 2128 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2129 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | Py_DECREF(tuple); |
| 2131 | Py_XDECREF(dict); |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2132 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | return NULL; |
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 | else { |
| 2136 | Py_RETURN_NONE; |
| 2137 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2141 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2143 | const char *encoding, *errors = NULL; |
| 2144 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 2145 | &encoding, &errors)) |
| 2146 | return NULL; |
| 2147 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2150 | static PyObject * |
| 2151 | codec_incrementaldecoder(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_incrementaldecoder", |
| 2155 | &encoding, &errors)) |
| 2156 | return NULL; |
| 2157 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 2160 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 2161 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2162 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2163 | test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | struct triple { |
| 2166 | long input; |
| 2167 | size_t nbits; |
| 2168 | int sign; |
| 2169 | } testcases[] = {{0, 0, 0}, |
| 2170 | {1L, 1, 1}, |
| 2171 | {-1L, 1, -1}, |
| 2172 | {2L, 2, 1}, |
| 2173 | {-2L, 2, -1}, |
| 2174 | {3L, 2, 1}, |
| 2175 | {-3L, 2, -1}, |
| 2176 | {4L, 3, 1}, |
| 2177 | {-4L, 3, -1}, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2178 | {0x7fffL, 15, 1}, /* one Python int digit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | {-0x7fffL, 15, -1}, |
| 2180 | {0xffffL, 16, 1}, |
| 2181 | {-0xffffL, 16, -1}, |
| 2182 | {0xfffffffL, 28, 1}, |
| 2183 | {-0xfffffffL, 28, -1}}; |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 2184 | size_t i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2185 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2186 | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2187 | size_t nbits; |
| 2188 | int sign; |
| 2189 | PyObject *plong; |
| 2190 | |
| 2191 | plong = PyLong_FromLong(testcases[i].input); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2192 | if (plong == NULL) |
| 2193 | return NULL; |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2194 | nbits = _PyLong_NumBits(plong); |
| 2195 | sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | Py_DECREF(plong); |
| 2198 | if (nbits != testcases[i].nbits) |
| 2199 | return raiseTestError("test_long_numbits", |
| 2200 | "wrong result for _PyLong_NumBits"); |
| 2201 | if (sign != testcases[i].sign) |
| 2202 | return raiseTestError("test_long_numbits", |
| 2203 | "wrong result for _PyLong_Sign"); |
| 2204 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2205 | Py_RETURN_NONE; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 2208 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2209 | |
| 2210 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2211 | test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 2214 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 2215 | Py_XDECREF(o1); |
| 2216 | Py_XDECREF(o2); |
| 2217 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2220 | static PyObject * |
| 2221 | raise_exception(PyObject *self, PyObject *args) |
| 2222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | PyObject *exc; |
| 2224 | PyObject *exc_args, *v; |
| 2225 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 2228 | &exc, &num_args)) |
| 2229 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2231 | exc_args = PyTuple_New(num_args); |
| 2232 | if (exc_args == NULL) |
| 2233 | return NULL; |
| 2234 | for (i = 0; i < num_args; ++i) { |
| 2235 | v = PyLong_FromLong(i); |
| 2236 | if (v == NULL) { |
| 2237 | Py_DECREF(exc_args); |
| 2238 | return NULL; |
| 2239 | } |
| 2240 | PyTuple_SET_ITEM(exc_args, i, v); |
| 2241 | } |
| 2242 | PyErr_SetObject(exc, exc_args); |
| 2243 | Py_DECREF(exc_args); |
| 2244 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2245 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2246 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2247 | static PyObject * |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 2248 | set_errno(PyObject *self, PyObject *args) |
| 2249 | { |
| 2250 | int new_errno; |
| 2251 | |
| 2252 | if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
| 2253 | return NULL; |
| 2254 | |
| 2255 | errno = new_errno; |
| 2256 | Py_RETURN_NONE; |
| 2257 | } |
| 2258 | |
| 2259 | static PyObject * |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2260 | test_set_exc_info(PyObject *self, PyObject *args) |
| 2261 | { |
| 2262 | PyObject *orig_exc; |
| 2263 | PyObject *new_type, *new_value, *new_tb; |
| 2264 | PyObject *type, *value, *tb; |
| 2265 | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
| 2266 | &new_type, &new_value, &new_tb)) |
| 2267 | return NULL; |
| 2268 | |
| 2269 | PyErr_GetExcInfo(&type, &value, &tb); |
| 2270 | |
| 2271 | Py_INCREF(new_type); |
| 2272 | Py_INCREF(new_value); |
| 2273 | Py_INCREF(new_tb); |
| 2274 | PyErr_SetExcInfo(new_type, new_value, new_tb); |
| 2275 | |
| 2276 | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
| 2277 | Py_XDECREF(type); |
| 2278 | Py_XDECREF(value); |
| 2279 | Py_XDECREF(tb); |
| 2280 | return orig_exc; |
| 2281 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2282 | |
| 2283 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2284 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2285 | static PyObject * |
| 2286 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | if (PyDateTimeAPI) { |
| 2288 | if (test_run_counter) { |
| 2289 | /* Probably regrtest.py -R */ |
| 2290 | Py_RETURN_NONE; |
| 2291 | } |
| 2292 | else { |
| 2293 | PyErr_SetString(PyExc_AssertionError, |
| 2294 | "PyDateTime_CAPI somehow initialized"); |
| 2295 | return NULL; |
| 2296 | } |
| 2297 | } |
| 2298 | test_run_counter++; |
| 2299 | PyDateTime_IMPORT; |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | if (PyDateTimeAPI) |
| 2302 | Py_RETURN_NONE; |
| 2303 | else |
| 2304 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2307 | /* Functions exposing the C API type checking for testing */ |
| 2308 | #define MAKE_DATETIME_CHECK_FUNC(check_method, exact_method) \ |
| 2309 | PyObject *obj; \ |
| 2310 | int exact = 0; \ |
| 2311 | if (!PyArg_ParseTuple(args, "O|p", &obj, &exact)) { \ |
| 2312 | return NULL; \ |
| 2313 | } \ |
| 2314 | int rv = exact?exact_method(obj):check_method(obj); \ |
| 2315 | if (rv) { \ |
| 2316 | Py_RETURN_TRUE; \ |
| 2317 | } else { \ |
| 2318 | Py_RETURN_FALSE; \ |
| 2319 | } |
| 2320 | |
| 2321 | static PyObject * |
| 2322 | datetime_check_date(PyObject *self, PyObject *args) { |
| 2323 | MAKE_DATETIME_CHECK_FUNC(PyDate_Check, PyDate_CheckExact) |
| 2324 | } |
| 2325 | |
| 2326 | static PyObject * |
| 2327 | datetime_check_time(PyObject *self, PyObject *args) { |
| 2328 | MAKE_DATETIME_CHECK_FUNC(PyTime_Check, PyTime_CheckExact) |
| 2329 | } |
| 2330 | |
| 2331 | static PyObject * |
| 2332 | datetime_check_datetime(PyObject *self, PyObject *args) { |
| 2333 | MAKE_DATETIME_CHECK_FUNC(PyDateTime_Check, PyDateTime_CheckExact) |
| 2334 | } |
| 2335 | |
| 2336 | static PyObject * |
| 2337 | datetime_check_delta(PyObject *self, PyObject *args) { |
| 2338 | MAKE_DATETIME_CHECK_FUNC(PyDelta_Check, PyDelta_CheckExact) |
| 2339 | } |
| 2340 | |
| 2341 | static PyObject * |
| 2342 | datetime_check_tzinfo(PyObject *self, PyObject *args) { |
| 2343 | MAKE_DATETIME_CHECK_FUNC(PyTZInfo_Check, PyTZInfo_CheckExact) |
| 2344 | } |
| 2345 | |
| 2346 | |
| 2347 | /* Makes three variations on timezone representing UTC-5: |
| 2348 | 1. timezone with offset and name from PyDateTimeAPI |
| 2349 | 2. timezone with offset and name from PyTimeZone_FromOffsetAndName |
| 2350 | 3. timezone with offset (no name) from PyTimeZone_FromOffset |
| 2351 | */ |
| 2352 | static PyObject * |
| 2353 | make_timezones_capi(PyObject *self, PyObject *args) { |
| 2354 | PyObject *offset = PyDelta_FromDSU(0, -18000, 0); |
| 2355 | PyObject *name = PyUnicode_FromString("EST"); |
| 2356 | |
| 2357 | PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name); |
| 2358 | PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name); |
| 2359 | PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset); |
| 2360 | |
| 2361 | Py_DecRef(offset); |
| 2362 | Py_DecRef(name); |
| 2363 | |
| 2364 | PyObject *rv = PyTuple_New(3); |
| 2365 | |
| 2366 | PyTuple_SET_ITEM(rv, 0, est_zone_capi); |
| 2367 | PyTuple_SET_ITEM(rv, 1, est_zone_macro); |
| 2368 | PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname); |
| 2369 | |
| 2370 | return rv; |
| 2371 | } |
| 2372 | |
| 2373 | static PyObject * |
Paul Ganssle | a049f57 | 2018-02-22 15:15:32 -0500 | [diff] [blame] | 2374 | get_timezones_offset_zero(PyObject *self, PyObject *args) { |
| 2375 | PyObject *offset = PyDelta_FromDSU(0, 0, 0); |
| 2376 | PyObject *name = PyUnicode_FromString(""); |
| 2377 | |
| 2378 | // These two should return the UTC singleton |
| 2379 | PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset); |
| 2380 | PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL); |
| 2381 | |
| 2382 | // This one will return +00:00 zone, but not the UTC singleton |
| 2383 | PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name); |
| 2384 | |
| 2385 | Py_DecRef(offset); |
| 2386 | Py_DecRef(name); |
| 2387 | |
| 2388 | PyObject *rv = PyTuple_New(3); |
| 2389 | PyTuple_SET_ITEM(rv, 0, utc_singleton_0); |
| 2390 | PyTuple_SET_ITEM(rv, 1, utc_singleton_1); |
| 2391 | PyTuple_SET_ITEM(rv, 2, non_utc_zone); |
| 2392 | |
| 2393 | return rv; |
| 2394 | } |
| 2395 | |
| 2396 | static PyObject * |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2397 | get_timezone_utc_capi(PyObject* self, PyObject *args) { |
| 2398 | int macro = 0; |
| 2399 | if (!PyArg_ParseTuple(args, "|p", ¯o)) { |
| 2400 | return NULL; |
| 2401 | } |
| 2402 | if (macro) { |
Paul Ganssle | 58dc03c | 2018-01-25 08:58:07 -0500 | [diff] [blame] | 2403 | Py_INCREF(PyDateTime_TimeZone_UTC); |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2404 | return PyDateTime_TimeZone_UTC; |
| 2405 | } else { |
Paul Ganssle | 58dc03c | 2018-01-25 08:58:07 -0500 | [diff] [blame] | 2406 | Py_INCREF(PyDateTimeAPI->TimeZone_UTC); |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2407 | return PyDateTimeAPI->TimeZone_UTC; |
| 2408 | } |
| 2409 | } |
| 2410 | |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2411 | static PyObject * |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 2412 | get_date_fromdate(PyObject *self, PyObject *args) |
| 2413 | { |
| 2414 | PyObject *rv = NULL; |
| 2415 | int macro; |
| 2416 | int year, month, day; |
| 2417 | |
| 2418 | if (!PyArg_ParseTuple(args, "piii", ¯o, &year, &month, &day)) { |
| 2419 | return NULL; |
| 2420 | } |
| 2421 | |
| 2422 | if (macro) { |
| 2423 | rv = PyDate_FromDate(year, month, day); |
| 2424 | } |
| 2425 | else { |
| 2426 | rv = PyDateTimeAPI->Date_FromDate( |
| 2427 | year, month, day, |
| 2428 | PyDateTimeAPI->DateType); |
| 2429 | } |
| 2430 | return rv; |
| 2431 | } |
| 2432 | |
| 2433 | static PyObject * |
| 2434 | get_datetime_fromdateandtime(PyObject *self, PyObject *args) |
| 2435 | { |
| 2436 | PyObject *rv = NULL; |
| 2437 | int macro; |
| 2438 | int year, month, day; |
| 2439 | int hour, minute, second, microsecond; |
| 2440 | |
| 2441 | if (!PyArg_ParseTuple(args, "piiiiiii", |
| 2442 | ¯o, |
| 2443 | &year, &month, &day, |
| 2444 | &hour, &minute, &second, µsecond)) { |
| 2445 | return NULL; |
| 2446 | } |
| 2447 | |
| 2448 | if (macro) { |
| 2449 | rv = PyDateTime_FromDateAndTime( |
| 2450 | year, month, day, |
| 2451 | hour, minute, second, microsecond); |
| 2452 | } |
| 2453 | else { |
| 2454 | rv = PyDateTimeAPI->DateTime_FromDateAndTime( |
| 2455 | year, month, day, |
| 2456 | hour, minute, second, microsecond, |
| 2457 | Py_None, |
| 2458 | PyDateTimeAPI->DateTimeType); |
| 2459 | } |
| 2460 | return rv; |
| 2461 | } |
| 2462 | |
| 2463 | static PyObject * |
| 2464 | get_datetime_fromdateandtimeandfold(PyObject *self, PyObject *args) |
| 2465 | { |
| 2466 | PyObject *rv = NULL; |
| 2467 | int macro; |
| 2468 | int year, month, day; |
| 2469 | int hour, minute, second, microsecond, fold; |
| 2470 | |
| 2471 | if (!PyArg_ParseTuple(args, "piiiiiiii", |
| 2472 | ¯o, |
| 2473 | &year, &month, &day, |
| 2474 | &hour, &minute, &second, µsecond, |
| 2475 | &fold)) { |
| 2476 | return NULL; |
| 2477 | } |
| 2478 | |
| 2479 | if (macro) { |
| 2480 | rv = PyDateTime_FromDateAndTimeAndFold( |
| 2481 | year, month, day, |
| 2482 | hour, minute, second, microsecond, |
| 2483 | fold); |
| 2484 | } |
| 2485 | else { |
| 2486 | rv = PyDateTimeAPI->DateTime_FromDateAndTimeAndFold( |
| 2487 | year, month, day, |
| 2488 | hour, minute, second, microsecond, |
| 2489 | Py_None, |
| 2490 | fold, |
| 2491 | PyDateTimeAPI->DateTimeType); |
| 2492 | } |
| 2493 | return rv; |
| 2494 | } |
| 2495 | |
| 2496 | static PyObject * |
| 2497 | get_time_fromtime(PyObject *self, PyObject *args) |
| 2498 | { |
| 2499 | PyObject *rv = NULL; |
| 2500 | int macro; |
| 2501 | int hour, minute, second, microsecond; |
| 2502 | |
| 2503 | if (!PyArg_ParseTuple(args, "piiii", |
| 2504 | ¯o, |
| 2505 | &hour, &minute, &second, µsecond)) { |
| 2506 | return NULL; |
| 2507 | } |
| 2508 | |
| 2509 | if (macro) { |
| 2510 | rv = PyTime_FromTime(hour, minute, second, microsecond); |
| 2511 | } |
| 2512 | else { |
| 2513 | rv = PyDateTimeAPI->Time_FromTime( |
| 2514 | hour, minute, second, microsecond, |
| 2515 | Py_None, |
| 2516 | PyDateTimeAPI->TimeType); |
| 2517 | } |
| 2518 | return rv; |
| 2519 | } |
| 2520 | |
| 2521 | static PyObject * |
| 2522 | get_time_fromtimeandfold(PyObject *self, PyObject *args) |
| 2523 | { |
| 2524 | PyObject *rv = NULL; |
| 2525 | int macro; |
| 2526 | int hour, minute, second, microsecond, fold; |
| 2527 | |
| 2528 | if (!PyArg_ParseTuple(args, "piiiii", |
| 2529 | ¯o, |
| 2530 | &hour, &minute, &second, µsecond, |
| 2531 | &fold)) { |
| 2532 | return NULL; |
| 2533 | } |
| 2534 | |
| 2535 | if (macro) { |
| 2536 | rv = PyTime_FromTimeAndFold(hour, minute, second, microsecond, fold); |
| 2537 | } |
| 2538 | else { |
| 2539 | rv = PyDateTimeAPI->Time_FromTimeAndFold( |
| 2540 | hour, minute, second, microsecond, |
| 2541 | Py_None, |
| 2542 | fold, |
| 2543 | PyDateTimeAPI->TimeType); |
| 2544 | } |
| 2545 | return rv; |
| 2546 | } |
| 2547 | |
| 2548 | static PyObject * |
| 2549 | get_delta_fromdsu(PyObject *self, PyObject *args) |
| 2550 | { |
| 2551 | PyObject *rv = NULL; |
| 2552 | int macro; |
| 2553 | int days, seconds, microseconds; |
| 2554 | |
| 2555 | if (!PyArg_ParseTuple(args, "piii", |
| 2556 | ¯o, |
| 2557 | &days, &seconds, µseconds)) { |
| 2558 | return NULL; |
| 2559 | } |
| 2560 | |
| 2561 | if (macro) { |
| 2562 | rv = PyDelta_FromDSU(days, seconds, microseconds); |
| 2563 | } |
| 2564 | else { |
| 2565 | rv = PyDateTimeAPI->Delta_FromDelta( |
| 2566 | days, seconds, microseconds, 1, |
| 2567 | PyDateTimeAPI->DeltaType); |
| 2568 | } |
| 2569 | |
| 2570 | return rv; |
| 2571 | } |
| 2572 | |
| 2573 | static PyObject * |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2574 | get_date_fromtimestamp(PyObject* self, PyObject *args) |
| 2575 | { |
| 2576 | PyObject *tsargs = NULL, *ts = NULL, *rv = NULL; |
| 2577 | int macro = 0; |
| 2578 | |
| 2579 | if (!PyArg_ParseTuple(args, "O|p", &ts, ¯o)) { |
| 2580 | return NULL; |
| 2581 | } |
| 2582 | |
| 2583 | // Construct the argument tuple |
| 2584 | if ((tsargs = PyTuple_Pack(1, ts)) == NULL) { |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | |
| 2588 | // Pass along to the API function |
| 2589 | if (macro) { |
| 2590 | rv = PyDate_FromTimestamp(tsargs); |
| 2591 | } |
| 2592 | else { |
| 2593 | rv = PyDateTimeAPI->Date_FromTimestamp( |
| 2594 | (PyObject *)PyDateTimeAPI->DateType, tsargs |
| 2595 | ); |
| 2596 | } |
| 2597 | |
| 2598 | Py_DECREF(tsargs); |
| 2599 | return rv; |
| 2600 | } |
| 2601 | |
| 2602 | static PyObject * |
| 2603 | get_datetime_fromtimestamp(PyObject* self, PyObject *args) |
| 2604 | { |
| 2605 | int macro = 0; |
| 2606 | int usetz = 0; |
| 2607 | PyObject *tsargs = NULL, *ts = NULL, *tzinfo = Py_None, *rv = NULL; |
| 2608 | if (!PyArg_ParseTuple(args, "OO|pp", &ts, &tzinfo, &usetz, ¯o)) { |
| 2609 | return NULL; |
| 2610 | } |
| 2611 | |
| 2612 | // Construct the argument tuple |
| 2613 | if (usetz) { |
| 2614 | tsargs = PyTuple_Pack(2, ts, tzinfo); |
| 2615 | } |
| 2616 | else { |
| 2617 | tsargs = PyTuple_Pack(1, ts); |
| 2618 | } |
| 2619 | |
| 2620 | if (tsargs == NULL) { |
| 2621 | return NULL; |
| 2622 | } |
| 2623 | |
| 2624 | // Pass along to the API function |
| 2625 | if (macro) { |
| 2626 | rv = PyDateTime_FromTimestamp(tsargs); |
| 2627 | } |
| 2628 | else { |
| 2629 | rv = PyDateTimeAPI->DateTime_FromTimestamp( |
| 2630 | (PyObject *)PyDateTimeAPI->DateTimeType, tsargs, NULL |
| 2631 | ); |
| 2632 | } |
| 2633 | |
| 2634 | Py_DECREF(tsargs); |
| 2635 | return rv; |
| 2636 | } |
| 2637 | |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 2638 | static PyObject * |
| 2639 | test_PyDateTime_GET(PyObject *self, PyObject *obj) |
| 2640 | { |
| 2641 | int year, month, day; |
| 2642 | |
| 2643 | year = PyDateTime_GET_YEAR(obj); |
| 2644 | month = PyDateTime_GET_MONTH(obj); |
| 2645 | day = PyDateTime_GET_DAY(obj); |
| 2646 | |
| 2647 | return Py_BuildValue("(lll)", year, month, day); |
| 2648 | } |
| 2649 | |
| 2650 | static PyObject * |
| 2651 | test_PyDateTime_DATE_GET(PyObject *self, PyObject *obj) |
| 2652 | { |
| 2653 | int hour, minute, second, microsecond; |
| 2654 | |
| 2655 | hour = PyDateTime_DATE_GET_HOUR(obj); |
| 2656 | minute = PyDateTime_DATE_GET_MINUTE(obj); |
| 2657 | second = PyDateTime_DATE_GET_SECOND(obj); |
| 2658 | microsecond = PyDateTime_DATE_GET_MICROSECOND(obj); |
| 2659 | |
| 2660 | return Py_BuildValue("(llll)", hour, minute, second, microsecond); |
| 2661 | } |
| 2662 | |
| 2663 | static PyObject * |
| 2664 | test_PyDateTime_TIME_GET(PyObject *self, PyObject *obj) |
| 2665 | { |
| 2666 | int hour, minute, second, microsecond; |
| 2667 | |
| 2668 | hour = PyDateTime_TIME_GET_HOUR(obj); |
| 2669 | minute = PyDateTime_TIME_GET_MINUTE(obj); |
| 2670 | second = PyDateTime_TIME_GET_SECOND(obj); |
| 2671 | microsecond = PyDateTime_TIME_GET_MICROSECOND(obj); |
| 2672 | |
| 2673 | return Py_BuildValue("(llll)", hour, minute, second, microsecond); |
| 2674 | } |
| 2675 | |
| 2676 | static PyObject * |
| 2677 | test_PyDateTime_DELTA_GET(PyObject *self, PyObject *obj) |
| 2678 | { |
| 2679 | int days, seconds, microseconds; |
| 2680 | |
| 2681 | days = PyDateTime_DELTA_GET_DAYS(obj); |
| 2682 | seconds = PyDateTime_DELTA_GET_SECONDS(obj); |
| 2683 | microseconds = PyDateTime_DELTA_GET_MICROSECONDS(obj); |
| 2684 | |
| 2685 | return Py_BuildValue("(lll)", days, seconds, microseconds); |
| 2686 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2687 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2688 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 2689 | * `thread_done` when it's finished. The driver code has to know when the |
| 2690 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 2691 | * may go away when the driver finishes. The former lack of this explicit |
| 2692 | * synchronization caused rare segfaults, so rare that they were seen only |
| 2693 | * on a Mac buildbot (although they were possible on any box). |
| 2694 | */ |
| 2695 | static PyThread_type_lock thread_done = NULL; |
| 2696 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2697 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2698 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2700 | PyObject *rc; |
| 2701 | int success; |
| 2702 | PyGILState_STATE s = PyGILState_Ensure(); |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2703 | rc = _PyObject_CallNoArg((PyObject *)callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2704 | success = (rc != NULL); |
| 2705 | Py_XDECREF(rc); |
| 2706 | PyGILState_Release(s); |
| 2707 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2710 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 2711 | * should be called only from threads spawned by test_thread_state(). |
| 2712 | */ |
| 2713 | static void |
| 2714 | _make_call_from_thread(void *callable) |
| 2715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | _make_call(callable); |
| 2717 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2720 | static PyObject * |
| 2721 | test_thread_state(PyObject *self, PyObject *args) |
| 2722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | PyObject *fn; |
| 2724 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 2727 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | if (!PyCallable_Check(fn)) { |
| 2730 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 2731 | Py_TYPE(fn)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2732 | return NULL; |
| 2733 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | thread_done = PyThread_allocate_lock(); |
| 2736 | if (thread_done == NULL) |
| 2737 | return PyErr_NoMemory(); |
| 2738 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2740 | /* Start a new thread with our callback. */ |
| 2741 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2742 | /* Make the callback with the thread lock held by this thread */ |
| 2743 | success &= _make_call(fn); |
| 2744 | /* Do it all again, but this time with the thread-lock released */ |
| 2745 | Py_BEGIN_ALLOW_THREADS |
| 2746 | success &= _make_call(fn); |
| 2747 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2748 | Py_END_ALLOW_THREADS |
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 | /* And once more with and without a thread |
| 2751 | XXX - should use a lock and work out exactly what we are trying |
| 2752 | to test <wink> |
| 2753 | */ |
| 2754 | Py_BEGIN_ALLOW_THREADS |
| 2755 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 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 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 2761 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | PyThread_free_lock(thread_done); |
| 2764 | if (!success) |
| 2765 | return NULL; |
| 2766 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2767 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2768 | |
| 2769 | /* test Py_AddPendingCalls using threads */ |
| 2770 | static int _pending_callback(void *arg) |
| 2771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2772 | /* we assume the argument is callable object to which we own a reference */ |
| 2773 | PyObject *callable = (PyObject *)arg; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 2774 | PyObject *r = _PyObject_CallNoArg(callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | Py_DECREF(callable); |
| 2776 | Py_XDECREF(r); |
| 2777 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | /* The following requests n callbacks to _pending_callback. It can be |
| 2781 | * run from any python thread. |
| 2782 | */ |
Benjamin Peterson | b4588c2 | 2018-07-03 22:30:56 -0700 | [diff] [blame] | 2783 | static PyObject * |
| 2784 | pending_threadfunc(PyObject *self, PyObject *arg) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | PyObject *callable; |
| 2787 | int r; |
| 2788 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 2789 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | /* create the reference for the callbackwhile we hold the lock */ |
| 2792 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2794 | Py_BEGIN_ALLOW_THREADS |
| 2795 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 2796 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | if (r<0) { |
| 2799 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2800 | Py_RETURN_FALSE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2801 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2802 | Py_RETURN_TRUE; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2803 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2804 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 2805 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2806 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 2807 | test_string_from_format(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | PyObject *result; |
| 2810 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2811 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2812 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 2813 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 2814 | if (result == NULL) \ |
| 2815 | return NULL; \ |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 2816 | if (!_PyUnicode_EqualToASCIIString(result, "1")) { \ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2817 | msg = FORMAT " failed at 1"; \ |
| 2818 | goto Fail; \ |
| 2819 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | CHECK_1_FORMAT("%d", int); |
| 2823 | CHECK_1_FORMAT("%ld", long); |
| 2824 | /* The z width modifier was added in Python 2.5. */ |
| 2825 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | /* The u type code was added in Python 2.5. */ |
| 2828 | CHECK_1_FORMAT("%u", unsigned int); |
| 2829 | CHECK_1_FORMAT("%lu", unsigned long); |
| 2830 | CHECK_1_FORMAT("%zu", size_t); |
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 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 2833 | CHECK_1_FORMAT("%llu", unsigned long long); |
| 2834 | CHECK_1_FORMAT("%lld", long long); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2837 | |
| 2838 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | Py_XDECREF(result); |
| 2840 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2841 | |
| 2842 | #undef CHECK_1_FORMAT |
| 2843 | } |
| 2844 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2845 | |
| 2846 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2847 | test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 2849 | int result; |
| 2850 | if (py_s == NULL) |
| 2851 | return NULL; |
| 2852 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 2853 | Py_DECREF(py_s); |
| 2854 | if (!result) { |
| 2855 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 2856 | "should not compare equal to c string."); |
| 2857 | return NULL; |
| 2858 | } |
| 2859 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2860 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2861 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2862 | /* This is here to provide a docstring for test_descr. */ |
| 2863 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2864 | test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2865 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2869 | /* Test PyOS_string_to_double. */ |
| 2870 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2871 | test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | double result; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 2873 | const char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | #define CHECK_STRING(STR, expected) \ |
| 2876 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2877 | if (result == -1.0 && PyErr_Occurred()) \ |
| 2878 | return NULL; \ |
Benjamin Peterson | 8f4b247 | 2016-09-07 18:09:22 -0700 | [diff] [blame] | 2879 | if (result != (double)expected) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | msg = "conversion of " STR " to float failed"; \ |
| 2881 | goto fail; \ |
| 2882 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | #define CHECK_INVALID(STR) \ |
| 2885 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2886 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 2887 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 2888 | PyErr_Clear(); \ |
| 2889 | else \ |
| 2890 | return NULL; \ |
| 2891 | } \ |
| 2892 | else { \ |
| 2893 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 2894 | goto fail; \ |
| 2895 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | CHECK_STRING("0.1", 0.1); |
| 2898 | CHECK_STRING("1.234", 1.234); |
| 2899 | CHECK_STRING("-1.35", -1.35); |
| 2900 | CHECK_STRING(".1e01", 1.0); |
| 2901 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2903 | CHECK_INVALID(" 0.1"); |
| 2904 | CHECK_INVALID("\t\n-3"); |
| 2905 | CHECK_INVALID(".123 "); |
| 2906 | CHECK_INVALID("3\n"); |
| 2907 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2910 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2912 | #undef CHECK_STRING |
| 2913 | #undef CHECK_INVALID |
| 2914 | } |
| 2915 | |
| 2916 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2917 | /* Coverage testing of capsule objects. */ |
| 2918 | |
| 2919 | static const char *capsule_name = "capsule name"; |
| 2920 | static char *capsule_pointer = "capsule pointer"; |
| 2921 | static char *capsule_context = "capsule context"; |
| 2922 | static const char *capsule_error = NULL; |
| 2923 | static int |
| 2924 | capsule_destructor_call_count = 0; |
| 2925 | |
| 2926 | static void |
| 2927 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | capsule_destructor_call_count++; |
| 2929 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 2930 | capsule_error = "context did not match in destructor!"; |
| 2931 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2932 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2933 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 2934 | capsule_error = "name did not match in destructor!"; |
| 2935 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2936 | capsule_error = "pointer did not match in destructor!"; |
| 2937 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2941 | char *name; |
| 2942 | char *module; |
| 2943 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2944 | } known_capsule; |
| 2945 | |
| 2946 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 2947 | test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2948 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | PyObject *object; |
| 2950 | const char *error = NULL; |
| 2951 | void *pointer; |
| 2952 | void *pointer2; |
| 2953 | known_capsule known_capsules[] = { |
| 2954 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2955 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 2956 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 2957 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2958 | { NULL, NULL }, |
| 2959 | }; |
| 2960 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2961 | |
| 2962 | #define FAIL(x) { error = (x); goto exit; } |
| 2963 | |
| 2964 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | if (capsule_error) { \ |
| 2966 | FAIL(capsule_error); \ |
| 2967 | } \ |
| 2968 | else if (!capsule_destructor_call_count) { \ |
| 2969 | FAIL("destructor not called!"); \ |
| 2970 | } \ |
| 2971 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2973 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2974 | PyCapsule_SetContext(object, capsule_context); |
| 2975 | capsule_destructor(object); |
| 2976 | CHECK_DESTRUCTOR; |
| 2977 | Py_DECREF(object); |
| 2978 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | object = PyCapsule_New(known, "ignored", NULL); |
| 2981 | PyCapsule_SetPointer(object, capsule_pointer); |
| 2982 | PyCapsule_SetName(object, capsule_name); |
| 2983 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 2984 | PyCapsule_SetContext(object, capsule_context); |
| 2985 | capsule_destructor(object); |
| 2986 | CHECK_DESTRUCTOR; |
| 2987 | /* intentionally access using the wrong name */ |
| 2988 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2989 | if (!PyErr_Occurred()) { |
| 2990 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2991 | } |
| 2992 | PyErr_Clear(); |
| 2993 | if (pointer2) { |
| 2994 | if (pointer2 == capsule_pointer) { |
| 2995 | FAIL("PyCapsule_GetPointer should not have" |
| 2996 | " returned the internal pointer!"); |
| 2997 | } else { |
| 2998 | FAIL("PyCapsule_GetPointer should have " |
| 2999 | "returned NULL pointer but did not!"); |
| 3000 | } |
| 3001 | } |
| 3002 | PyCapsule_SetDestructor(object, NULL); |
| 3003 | Py_DECREF(object); |
| 3004 | if (capsule_destructor_call_count) { |
| 3005 | FAIL("destructor called when it should not have been!"); |
| 3006 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 3009 | /* yeah, ordinarily I wouldn't do this either, |
| 3010 | but it's fine for this test harness. |
| 3011 | */ |
| 3012 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3013 | #undef FAIL |
| 3014 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3015 | { \ |
| 3016 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 3017 | x, known->module, known->attribute); \ |
| 3018 | error = buffer; \ |
| 3019 | goto exit; \ |
| 3020 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | PyObject *module = PyImport_ImportModule(known->module); |
| 3023 | if (module) { |
| 3024 | pointer = PyCapsule_Import(known->name, 0); |
| 3025 | if (!pointer) { |
| 3026 | Py_DECREF(module); |
| 3027 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 3028 | } |
| 3029 | object = PyObject_GetAttrString(module, known->attribute); |
| 3030 | if (!object) { |
| 3031 | Py_DECREF(module); |
| 3032 | return NULL; |
| 3033 | } |
| 3034 | pointer2 = PyCapsule_GetPointer(object, |
| 3035 | "weebles wobble but they don't fall down"); |
| 3036 | if (!PyErr_Occurred()) { |
| 3037 | Py_DECREF(object); |
| 3038 | Py_DECREF(module); |
| 3039 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 3040 | } |
| 3041 | PyErr_Clear(); |
| 3042 | if (pointer2) { |
| 3043 | Py_DECREF(module); |
| 3044 | Py_DECREF(object); |
| 3045 | if (pointer2 == pointer) { |
| 3046 | FAIL("PyCapsule_GetPointer should not have" |
| 3047 | " returned its internal pointer!"); |
| 3048 | } else { |
| 3049 | FAIL("PyCapsule_GetPointer should have" |
| 3050 | " returned NULL pointer but did not!"); |
| 3051 | } |
| 3052 | } |
| 3053 | Py_DECREF(object); |
| 3054 | Py_DECREF(module); |
| 3055 | } |
| 3056 | else |
| 3057 | PyErr_Clear(); |
| 3058 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3059 | |
| 3060 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | if (error) { |
| 3062 | return raiseTestError("test_capsule", error); |
| 3063 | } |
| 3064 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3065 | #undef FAIL |
| 3066 | } |
| 3067 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3068 | #ifdef HAVE_GETTIMEOFDAY |
| 3069 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 3070 | 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] | 3071 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | e->tv_sec -= s->tv_sec; |
| 3073 | e->tv_usec -= s->tv_usec; |
| 3074 | if (e->tv_usec < 0) { |
| 3075 | e->tv_sec -=1; |
| 3076 | e->tv_usec += 1000000; |
| 3077 | } |
| 3078 | 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] | 3079 | } |
| 3080 | |
| 3081 | static PyObject * |
| 3082 | profile_int(PyObject *self, PyObject* args) |
| 3083 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | int i, k; |
| 3085 | struct timeval start, stop; |
| 3086 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3087 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | /* Test 1: Allocate and immediately deallocate |
| 3089 | many small integers */ |
| 3090 | gettimeofday(&start, NULL); |
| 3091 | for(k=0; k < 20000; k++) |
| 3092 | for(i=0; i < 1000; i++) { |
| 3093 | single = PyLong_FromLong(i); |
| 3094 | Py_DECREF(single); |
| 3095 | } |
| 3096 | gettimeofday(&stop, NULL); |
| 3097 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3099 | /* Test 2: Allocate and immediately deallocate |
| 3100 | many large integers */ |
| 3101 | gettimeofday(&start, NULL); |
| 3102 | for(k=0; k < 20000; k++) |
| 3103 | for(i=0; i < 1000; i++) { |
| 3104 | single = PyLong_FromLong(i+1000000); |
| 3105 | Py_DECREF(single); |
| 3106 | } |
| 3107 | gettimeofday(&stop, NULL); |
| 3108 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | /* Test 3: Allocate a few integers, then release |
| 3111 | them all simultaneously. */ |
| 3112 | multiple = malloc(sizeof(PyObject*) * 1000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3113 | if (multiple == NULL) |
| 3114 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | gettimeofday(&start, NULL); |
| 3116 | for(k=0; k < 20000; k++) { |
| 3117 | for(i=0; i < 1000; i++) { |
| 3118 | multiple[i] = PyLong_FromLong(i+1000000); |
| 3119 | } |
| 3120 | for(i=0; i < 1000; i++) { |
| 3121 | Py_DECREF(multiple[i]); |
| 3122 | } |
| 3123 | } |
| 3124 | gettimeofday(&stop, NULL); |
| 3125 | print_delta(3, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3126 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3128 | /* Test 4: Allocate many integers, then release |
| 3129 | them all simultaneously. */ |
| 3130 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3131 | if (multiple == NULL) |
| 3132 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | gettimeofday(&start, NULL); |
| 3134 | for(k=0; k < 20; k++) { |
| 3135 | for(i=0; i < 1000000; i++) { |
| 3136 | multiple[i] = PyLong_FromLong(i+1000000); |
| 3137 | } |
| 3138 | for(i=0; i < 1000000; i++) { |
| 3139 | Py_DECREF(multiple[i]); |
| 3140 | } |
| 3141 | } |
| 3142 | gettimeofday(&stop, NULL); |
| 3143 | print_delta(4, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3144 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | /* Test 5: Allocate many integers < 32000 */ |
| 3147 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3148 | if (multiple == NULL) |
| 3149 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | gettimeofday(&start, NULL); |
| 3151 | for(k=0; k < 10; k++) { |
| 3152 | for(i=0; i < 1000000; i++) { |
| 3153 | multiple[i] = PyLong_FromLong(i+1000); |
| 3154 | } |
| 3155 | for(i=0; i < 1000000; i++) { |
| 3156 | Py_DECREF(multiple[i]); |
| 3157 | } |
| 3158 | } |
| 3159 | gettimeofday(&stop, NULL); |
| 3160 | print_delta(5, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3161 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3163 | /* Test 6: Perform small int addition */ |
| 3164 | op1 = PyLong_FromLong(1); |
| 3165 | gettimeofday(&start, NULL); |
| 3166 | for(i=0; i < 10000000; i++) { |
| 3167 | result = PyNumber_Add(op1, op1); |
| 3168 | Py_DECREF(result); |
| 3169 | } |
| 3170 | gettimeofday(&stop, NULL); |
| 3171 | Py_DECREF(op1); |
| 3172 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3174 | /* Test 7: Perform medium int addition */ |
| 3175 | op1 = PyLong_FromLong(1000); |
Christian Heimes | 66eda26 | 2013-07-26 15:54:07 +0200 | [diff] [blame] | 3176 | if (op1 == NULL) |
| 3177 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | gettimeofday(&start, NULL); |
| 3179 | for(i=0; i < 10000000; i++) { |
| 3180 | result = PyNumber_Add(op1, op1); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 3181 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | } |
| 3183 | gettimeofday(&stop, NULL); |
| 3184 | Py_DECREF(op1); |
| 3185 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3186 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 3187 | Py_RETURN_NONE; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3188 | } |
| 3189 | #endif |
| 3190 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3191 | /* To test the format of tracebacks as printed out. */ |
| 3192 | static PyObject * |
| 3193 | traceback_print(PyObject *self, PyObject *args) |
| 3194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3195 | PyObject *file; |
| 3196 | PyObject *traceback; |
| 3197 | int result; |
| 3198 | |
| 3199 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 3200 | &traceback, &file)) |
| 3201 | return NULL; |
| 3202 | |
| 3203 | result = PyTraceBack_Print(traceback, file); |
| 3204 | if (result < 0) |
| 3205 | return NULL; |
| 3206 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3209 | /* To test the format of exceptions as printed out. */ |
| 3210 | static PyObject * |
| 3211 | exception_print(PyObject *self, PyObject *args) |
| 3212 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | PyObject *value; |
| 3214 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 3217 | &value)) |
| 3218 | return NULL; |
| 3219 | if (!PyExceptionInstance_Check(value)) { |
| 3220 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 3221 | return NULL; |
| 3222 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3224 | tb = PyException_GetTraceback(value); |
| 3225 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 3226 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3228 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | |
| 3232 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3233 | |
| 3234 | /* reliably raise a MemoryError */ |
| 3235 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3236 | raise_memoryerror(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | PyErr_NoMemory(); |
| 3239 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3242 | /* Issue 6012 */ |
| 3243 | static PyObject *str1, *str2; |
| 3244 | static int |
| 3245 | failing_converter(PyObject *obj, void *arg) |
| 3246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3247 | /* Clone str1, then let the conversion fail. */ |
| 3248 | assert(str1); |
| 3249 | str2 = str1; |
| 3250 | Py_INCREF(str2); |
| 3251 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3252 | } |
| 3253 | static PyObject* |
| 3254 | argparsing(PyObject *o, PyObject *args) |
| 3255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | PyObject *res; |
| 3257 | str1 = str2 = NULL; |
| 3258 | if (!PyArg_ParseTuple(args, "O&O&", |
| 3259 | PyUnicode_FSConverter, &str1, |
| 3260 | failing_converter, &str2)) { |
| 3261 | if (!str2) |
| 3262 | /* argument converter not called? */ |
| 3263 | return NULL; |
| 3264 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 3265 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3266 | Py_DECREF(str2); |
| 3267 | PyErr_Clear(); |
| 3268 | return res; |
| 3269 | } |
| 3270 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3271 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3272 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3273 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 3274 | static PyObject * |
| 3275 | code_newempty(PyObject *self, PyObject *args) |
| 3276 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | const char *filename; |
| 3278 | const char *funcname; |
| 3279 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 3282 | &filename, &funcname, &firstlineno)) |
| 3283 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3288 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 3289 | Run via Lib/test/test_exceptions.py */ |
| 3290 | static PyObject * |
| 3291 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | const char *name; |
| 3294 | const char *doc = NULL; |
| 3295 | PyObject *base = NULL; |
| 3296 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3300 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3301 | "s|sOO:make_exception_with_doc", kwlist, |
| 3302 | &name, &doc, &base, &dict)) |
| 3303 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3308 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3309 | make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3310 | { |
| 3311 | Py_buffer info; |
| 3312 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 3313 | return NULL; |
| 3314 | return PyMemoryView_FromBuffer(&info); |
| 3315 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 3316 | |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3317 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3318 | test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3319 | { |
| 3320 | int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 3321 | int init[5] = {0, 1, 2, 3, 4}; |
| 3322 | Py_ssize_t itemsize = sizeof(int); |
| 3323 | Py_ssize_t shape = 5; |
| 3324 | Py_ssize_t strides = 2 * itemsize; |
| 3325 | Py_buffer view = { |
| 3326 | data, |
| 3327 | NULL, |
| 3328 | 5 * itemsize, |
| 3329 | itemsize, |
| 3330 | 1, |
| 3331 | 1, |
| 3332 | NULL, |
| 3333 | &shape, |
| 3334 | &strides, |
| 3335 | NULL, |
| 3336 | NULL |
| 3337 | }; |
| 3338 | int *ptr; |
| 3339 | int i; |
| 3340 | |
| 3341 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 3342 | ptr = view.buf; |
| 3343 | for (i = 0; i < 5; i++) { |
| 3344 | if (ptr[2*i] != i) { |
| 3345 | PyErr_SetString(TestError, |
| 3346 | "test_from_contiguous: incorrect result"); |
| 3347 | return NULL; |
| 3348 | } |
| 3349 | } |
| 3350 | |
| 3351 | view.buf = &data[8]; |
| 3352 | view.strides[0] = -2 * itemsize; |
| 3353 | |
| 3354 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 3355 | ptr = view.buf; |
| 3356 | for (i = 0; i < 5; i++) { |
| 3357 | if (*(ptr-2*i) != i) { |
| 3358 | PyErr_SetString(TestError, |
| 3359 | "test_from_contiguous: incorrect result"); |
| 3360 | return NULL; |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | Py_RETURN_NONE; |
| 3365 | } |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3366 | |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3367 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3368 | extern PyTypeObject _PyBytesIOBuffer_Type; |
| 3369 | |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3370 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3371 | test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3372 | { |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3373 | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3374 | PyObject *b; |
| 3375 | char *dummy[1]; |
| 3376 | int ret, match; |
| 3377 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3378 | /* PyBuffer_FillInfo() */ |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3379 | ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE); |
| 3380 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 3381 | PyErr_Clear(); |
| 3382 | if (ret != -1 || match == 0) |
| 3383 | goto error; |
| 3384 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3385 | /* bytesiobuf_getbuffer() */ |
| 3386 | b = type->tp_alloc(type, 0); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3387 | if (b == NULL) { |
| 3388 | return NULL; |
| 3389 | } |
| 3390 | |
| 3391 | ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE); |
| 3392 | Py_DECREF(b); |
| 3393 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 3394 | PyErr_Clear(); |
| 3395 | if (ret != -1 || match == 0) |
| 3396 | goto error; |
| 3397 | |
| 3398 | Py_RETURN_NONE; |
| 3399 | |
| 3400 | error: |
| 3401 | PyErr_SetString(TestError, |
| 3402 | "test_pep3118_obsolete_write_locks: failure"); |
| 3403 | return NULL; |
| 3404 | } |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3405 | #endif |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3406 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3407 | /* This tests functions that historically supported write locks. It is |
| 3408 | wrong to call getbuffer() with view==NULL and a compliant getbufferproc |
| 3409 | is entitled to segfault in that case. */ |
| 3410 | static PyObject * |
| 3411 | getbuffer_with_null_view(PyObject* self, PyObject *obj) |
| 3412 | { |
| 3413 | if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0) |
| 3414 | return NULL; |
| 3415 | |
| 3416 | Py_RETURN_NONE; |
| 3417 | } |
| 3418 | |
Joannah Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 3419 | /* PyBuffer_SizeFromFormat() */ |
| 3420 | static PyObject * |
| 3421 | test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args) |
| 3422 | { |
| 3423 | const char *format; |
| 3424 | Py_ssize_t result; |
| 3425 | |
| 3426 | if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat", |
| 3427 | &format)) { |
| 3428 | return NULL; |
| 3429 | } |
| 3430 | |
| 3431 | result = PyBuffer_SizeFromFormat(format); |
| 3432 | if (result == -1) { |
| 3433 | return NULL; |
| 3434 | } |
| 3435 | |
| 3436 | return PyLong_FromSsize_t(result); |
| 3437 | } |
| 3438 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3439 | /* Test that the fatal error from not having a current thread doesn't |
| 3440 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 3441 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3442 | crash_no_current_thread(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3443 | { |
| 3444 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 3445 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 3446 | !pydebug mode. However, the test only actually tests anything |
| 3447 | in pydebug mode, since that's where the infinite loop was in |
| 3448 | the first place. */ |
| 3449 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3450 | Py_END_ALLOW_THREADS |
| 3451 | return NULL; |
| 3452 | } |
| 3453 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3454 | /* To run some code in a sub-interpreter. */ |
| 3455 | static PyObject * |
| 3456 | run_in_subinterp(PyObject *self, PyObject *args) |
| 3457 | { |
| 3458 | const char *code; |
| 3459 | int r; |
| 3460 | PyThreadState *substate, *mainstate; |
| 3461 | |
| 3462 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 3463 | &code)) |
| 3464 | return NULL; |
| 3465 | |
| 3466 | mainstate = PyThreadState_Get(); |
| 3467 | |
| 3468 | PyThreadState_Swap(NULL); |
| 3469 | |
| 3470 | substate = Py_NewInterpreter(); |
Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 3471 | if (substate == NULL) { |
| 3472 | /* Since no new thread state was created, there is no exception to |
| 3473 | propagate; raise a fresh one after swapping in the old thread |
| 3474 | state. */ |
| 3475 | PyThreadState_Swap(mainstate); |
| 3476 | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
| 3477 | return NULL; |
| 3478 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3479 | r = PyRun_SimpleString(code); |
| 3480 | Py_EndInterpreter(substate); |
| 3481 | |
| 3482 | PyThreadState_Swap(mainstate); |
| 3483 | |
| 3484 | return PyLong_FromLong(r); |
| 3485 | } |
| 3486 | |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3487 | static int |
| 3488 | check_time_rounding(int round) |
| 3489 | { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 3490 | if (round != _PyTime_ROUND_FLOOR |
| 3491 | && round != _PyTime_ROUND_CEILING |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 3492 | && round != _PyTime_ROUND_HALF_EVEN |
| 3493 | && round != _PyTime_ROUND_UP) { |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3494 | PyErr_SetString(PyExc_ValueError, "invalid rounding"); |
| 3495 | return -1; |
| 3496 | } |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3500 | static PyObject * |
| 3501 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
| 3502 | { |
| 3503 | PyObject *obj; |
| 3504 | time_t sec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3505 | int round; |
| 3506 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3507 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3508 | if (check_time_rounding(round) < 0) |
| 3509 | return NULL; |
| 3510 | if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3511 | return NULL; |
| 3512 | return _PyLong_FromTime_t(sec); |
| 3513 | } |
| 3514 | |
| 3515 | static PyObject * |
| 3516 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
| 3517 | { |
| 3518 | PyObject *obj; |
| 3519 | time_t sec; |
| 3520 | long usec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3521 | int round; |
| 3522 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3523 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3524 | if (check_time_rounding(round) < 0) |
| 3525 | return NULL; |
| 3526 | if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3527 | return NULL; |
| 3528 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
| 3529 | } |
| 3530 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3531 | static PyObject * |
| 3532 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
| 3533 | { |
| 3534 | PyObject *obj; |
| 3535 | time_t sec; |
| 3536 | long nsec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3537 | int round; |
| 3538 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3539 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3540 | if (check_time_rounding(round) < 0) |
| 3541 | return NULL; |
| 3542 | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3543 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3544 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3545 | } |
| 3546 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3547 | static void |
| 3548 | slot_tp_del(PyObject *self) |
| 3549 | { |
| 3550 | _Py_IDENTIFIER(__tp_del__); |
| 3551 | PyObject *del, *res; |
| 3552 | PyObject *error_type, *error_value, *error_traceback; |
| 3553 | |
| 3554 | /* Temporarily resurrect the object. */ |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3555 | assert(Py_REFCNT(self) == 0); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3556 | Py_SET_REFCNT(self, 1); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3557 | |
| 3558 | /* Save the current exception, if any. */ |
| 3559 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 3560 | |
| 3561 | /* Execute __del__ method, if any. */ |
| 3562 | del = _PyObject_LookupSpecial(self, &PyId___tp_del__); |
| 3563 | if (del != NULL) { |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 3564 | res = _PyObject_CallNoArg(del); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3565 | if (res == NULL) |
| 3566 | PyErr_WriteUnraisable(del); |
| 3567 | else |
| 3568 | Py_DECREF(res); |
| 3569 | Py_DECREF(del); |
| 3570 | } |
| 3571 | |
| 3572 | /* Restore the saved exception. */ |
| 3573 | PyErr_Restore(error_type, error_value, error_traceback); |
| 3574 | |
| 3575 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 3576 | * cause a recursive call. |
| 3577 | */ |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3578 | assert(Py_REFCNT(self) > 0); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3579 | Py_SET_REFCNT(self, Py_REFCNT(self) - 1); |
| 3580 | if (Py_REFCNT(self) == 0) { |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3581 | /* this is the normal path out */ |
| 3582 | return; |
| 3583 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3584 | |
| 3585 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 3586 | * never happened. |
| 3587 | */ |
| 3588 | { |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3589 | Py_ssize_t refcnt = Py_REFCNT(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3590 | _Py_NewReference(self); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3591 | Py_SET_REFCNT(self, refcnt); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3592 | } |
Pablo Galindo | f13072b | 2020-04-11 01:21:54 +0100 | [diff] [blame] | 3593 | assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self)); |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 3594 | /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased |
| 3595 | _Py_RefTotal, so we need to undo that. */ |
| 3596 | #ifdef Py_REF_DEBUG |
| 3597 | _Py_RefTotal--; |
| 3598 | #endif |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | static PyObject * |
| 3602 | with_tp_del(PyObject *self, PyObject *args) |
| 3603 | { |
| 3604 | PyObject *obj; |
| 3605 | PyTypeObject *tp; |
| 3606 | |
| 3607 | if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) |
| 3608 | return NULL; |
| 3609 | tp = (PyTypeObject *) obj; |
| 3610 | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
| 3611 | PyErr_Format(PyExc_TypeError, |
| 3612 | "heap type expected, got %R", obj); |
| 3613 | return NULL; |
| 3614 | } |
| 3615 | tp->tp_del = slot_tp_del; |
| 3616 | Py_INCREF(obj); |
| 3617 | return obj; |
| 3618 | } |
| 3619 | |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 3620 | static PyMethodDef ml; |
| 3621 | |
| 3622 | static PyObject * |
| 3623 | create_cfunction(PyObject *self, PyObject *args) |
| 3624 | { |
| 3625 | return PyCFunction_NewEx(&ml, self, NULL); |
| 3626 | } |
| 3627 | |
| 3628 | static PyMethodDef ml = { |
| 3629 | "create_cfunction", |
| 3630 | create_cfunction, |
| 3631 | METH_NOARGS, |
| 3632 | NULL |
| 3633 | }; |
| 3634 | |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3635 | static PyObject * |
| 3636 | _test_incref(PyObject *ob) |
| 3637 | { |
| 3638 | Py_INCREF(ob); |
| 3639 | return ob; |
| 3640 | } |
| 3641 | |
| 3642 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3643 | test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3644 | { |
| 3645 | PyObject *obj = PyLong_FromLong(0); |
| 3646 | Py_XINCREF(_test_incref(obj)); |
| 3647 | Py_DECREF(obj); |
| 3648 | Py_DECREF(obj); |
| 3649 | Py_DECREF(obj); |
| 3650 | Py_RETURN_NONE; |
| 3651 | } |
| 3652 | |
| 3653 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3654 | test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3655 | { |
| 3656 | PyObject *obj = PyLong_FromLong(0); |
| 3657 | Py_INCREF(_test_incref(obj)); |
| 3658 | Py_DECREF(obj); |
| 3659 | Py_DECREF(obj); |
| 3660 | Py_DECREF(obj); |
| 3661 | Py_RETURN_NONE; |
| 3662 | } |
| 3663 | |
| 3664 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3665 | test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3666 | { |
| 3667 | Py_XDECREF(PyLong_FromLong(0)); |
| 3668 | Py_RETURN_NONE; |
| 3669 | } |
| 3670 | |
| 3671 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3672 | test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3673 | { |
| 3674 | Py_DECREF(PyLong_FromLong(0)); |
| 3675 | Py_RETURN_NONE; |
| 3676 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3677 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3678 | static PyObject * |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 3679 | test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self), |
| 3680 | PyObject *Py_UNUSED(args)) |
| 3681 | { |
| 3682 | PyStructSequence_Desc descr; |
| 3683 | PyStructSequence_Field descr_fields[3]; |
| 3684 | |
| 3685 | descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"}; |
| 3686 | descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"}; |
| 3687 | descr_fields[2] = (PyStructSequence_Field){0, NULL}; |
| 3688 | |
| 3689 | descr.name = "_testcapi.test_descr"; |
| 3690 | descr.doc = "This is used to test for memory leaks in NewType"; |
| 3691 | descr.fields = descr_fields; |
| 3692 | descr.n_in_sequence = 1; |
| 3693 | |
| 3694 | PyTypeObject* structseq_type = PyStructSequence_NewType(&descr); |
| 3695 | assert(structseq_type != NULL); |
| 3696 | assert(PyType_Check(structseq_type)); |
| 3697 | assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS)); |
| 3698 | Py_DECREF(structseq_type); |
| 3699 | |
| 3700 | Py_RETURN_NONE; |
| 3701 | } |
| 3702 | |
| 3703 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3704 | test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3705 | { |
| 3706 | PyObject *obj = PyLong_FromLong(0); |
Victor Stinner | fc6a90a | 2014-10-09 22:15:41 +0200 | [diff] [blame] | 3707 | Py_IncRef(obj); |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3708 | Py_DecRef(obj); |
| 3709 | Py_DecRef(obj); |
| 3710 | Py_RETURN_NONE; |
| 3711 | } |
| 3712 | |
| 3713 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3714 | test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3715 | { |
| 3716 | void *ptr; |
| 3717 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3718 | ptr = PyMem_RawMalloc(0); |
| 3719 | if (ptr == NULL) { |
| 3720 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); |
| 3721 | return NULL; |
| 3722 | } |
| 3723 | PyMem_RawFree(ptr); |
| 3724 | |
| 3725 | ptr = PyMem_RawCalloc(0, 0); |
| 3726 | if (ptr == NULL) { |
| 3727 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); |
| 3728 | return NULL; |
| 3729 | } |
| 3730 | PyMem_RawFree(ptr); |
| 3731 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3732 | ptr = PyMem_Malloc(0); |
| 3733 | if (ptr == NULL) { |
| 3734 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); |
| 3735 | return NULL; |
| 3736 | } |
| 3737 | PyMem_Free(ptr); |
| 3738 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3739 | ptr = PyMem_Calloc(0, 0); |
| 3740 | if (ptr == NULL) { |
| 3741 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); |
| 3742 | return NULL; |
| 3743 | } |
| 3744 | PyMem_Free(ptr); |
| 3745 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3746 | ptr = PyObject_Malloc(0); |
| 3747 | if (ptr == NULL) { |
| 3748 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); |
| 3749 | return NULL; |
| 3750 | } |
| 3751 | PyObject_Free(ptr); |
| 3752 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3753 | ptr = PyObject_Calloc(0, 0); |
| 3754 | if (ptr == NULL) { |
| 3755 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); |
| 3756 | return NULL; |
| 3757 | } |
| 3758 | PyObject_Free(ptr); |
| 3759 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3760 | Py_RETURN_NONE; |
| 3761 | } |
| 3762 | |
| 3763 | typedef struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3764 | PyMemAllocatorEx alloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3765 | |
| 3766 | size_t malloc_size; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3767 | size_t calloc_nelem; |
| 3768 | size_t calloc_elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3769 | void *realloc_ptr; |
| 3770 | size_t realloc_new_size; |
| 3771 | void *free_ptr; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3772 | void *ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3773 | } alloc_hook_t; |
| 3774 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3775 | static void* hook_malloc(void* ctx, size_t size) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3776 | { |
| 3777 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3778 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3779 | hook->malloc_size = size; |
| 3780 | return hook->alloc.malloc(hook->alloc.ctx, size); |
| 3781 | } |
| 3782 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3783 | static void* hook_calloc(void* ctx, size_t nelem, size_t elsize) |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3784 | { |
| 3785 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3786 | hook->ctx = ctx; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3787 | hook->calloc_nelem = nelem; |
| 3788 | hook->calloc_elsize = elsize; |
| 3789 | return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); |
| 3790 | } |
| 3791 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3792 | static void* hook_realloc(void* ctx, void* ptr, size_t new_size) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3793 | { |
| 3794 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3795 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3796 | hook->realloc_ptr = ptr; |
| 3797 | hook->realloc_new_size = new_size; |
| 3798 | return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); |
| 3799 | } |
| 3800 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3801 | static void hook_free(void *ctx, void *ptr) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3802 | { |
| 3803 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3804 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3805 | hook->free_ptr = ptr; |
| 3806 | hook->alloc.free(hook->alloc.ctx, ptr); |
| 3807 | } |
| 3808 | |
| 3809 | static PyObject * |
| 3810 | test_setallocators(PyMemAllocatorDomain domain) |
| 3811 | { |
| 3812 | PyObject *res = NULL; |
| 3813 | const char *error_msg; |
| 3814 | alloc_hook_t hook; |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3815 | PyMemAllocatorEx alloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3816 | size_t size, size2, nelem, elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3817 | void *ptr, *ptr2; |
| 3818 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3819 | memset(&hook, 0, sizeof(hook)); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3820 | |
| 3821 | alloc.ctx = &hook; |
| 3822 | alloc.malloc = &hook_malloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3823 | alloc.calloc = &hook_calloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3824 | alloc.realloc = &hook_realloc; |
| 3825 | alloc.free = &hook_free; |
| 3826 | PyMem_GetAllocator(domain, &hook.alloc); |
| 3827 | PyMem_SetAllocator(domain, &alloc); |
| 3828 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3829 | /* malloc, realloc, free */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3830 | size = 42; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3831 | hook.ctx = NULL; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3832 | switch(domain) |
| 3833 | { |
| 3834 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; |
| 3835 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; |
| 3836 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; |
| 3837 | default: ptr = NULL; break; |
| 3838 | } |
| 3839 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3840 | #define CHECK_CTX(FUNC) \ |
| 3841 | if (hook.ctx != &hook) { \ |
| 3842 | error_msg = FUNC " wrong context"; \ |
| 3843 | goto fail; \ |
| 3844 | } \ |
| 3845 | hook.ctx = NULL; /* reset for next check */ |
| 3846 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3847 | if (ptr == NULL) { |
| 3848 | error_msg = "malloc failed"; |
| 3849 | goto fail; |
| 3850 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3851 | CHECK_CTX("malloc"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3852 | if (hook.malloc_size != size) { |
| 3853 | error_msg = "malloc invalid size"; |
| 3854 | goto fail; |
| 3855 | } |
| 3856 | |
| 3857 | size2 = 200; |
| 3858 | switch(domain) |
| 3859 | { |
| 3860 | case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; |
| 3861 | case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; |
| 3862 | case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; |
Christian Heimes | 865d12a | 2013-08-02 11:10:51 +0200 | [diff] [blame] | 3863 | default: ptr2 = NULL; break; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | if (ptr2 == NULL) { |
| 3867 | error_msg = "realloc failed"; |
| 3868 | goto fail; |
| 3869 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3870 | CHECK_CTX("realloc"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3871 | if (hook.realloc_ptr != ptr |
| 3872 | || hook.realloc_new_size != size2) { |
| 3873 | error_msg = "realloc invalid parameters"; |
| 3874 | goto fail; |
| 3875 | } |
| 3876 | |
| 3877 | switch(domain) |
| 3878 | { |
| 3879 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; |
| 3880 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; |
| 3881 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; |
| 3882 | } |
| 3883 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3884 | CHECK_CTX("free"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3885 | if (hook.free_ptr != ptr2) { |
| 3886 | error_msg = "free invalid pointer"; |
| 3887 | goto fail; |
| 3888 | } |
| 3889 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3890 | /* calloc, free */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3891 | nelem = 2; |
| 3892 | elsize = 5; |
| 3893 | switch(domain) |
| 3894 | { |
| 3895 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; |
| 3896 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; |
| 3897 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; |
| 3898 | default: ptr = NULL; break; |
| 3899 | } |
| 3900 | |
| 3901 | if (ptr == NULL) { |
| 3902 | error_msg = "calloc failed"; |
| 3903 | goto fail; |
| 3904 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3905 | CHECK_CTX("calloc"); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3906 | if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { |
| 3907 | error_msg = "calloc invalid nelem or elsize"; |
| 3908 | goto fail; |
| 3909 | } |
| 3910 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3911 | hook.free_ptr = NULL; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3912 | switch(domain) |
| 3913 | { |
| 3914 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; |
| 3915 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; |
| 3916 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; |
| 3917 | } |
| 3918 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3919 | CHECK_CTX("calloc free"); |
| 3920 | if (hook.free_ptr != ptr) { |
| 3921 | error_msg = "calloc free invalid pointer"; |
| 3922 | goto fail; |
| 3923 | } |
| 3924 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3925 | Py_INCREF(Py_None); |
| 3926 | res = Py_None; |
| 3927 | goto finally; |
| 3928 | |
| 3929 | fail: |
| 3930 | PyErr_SetString(PyExc_RuntimeError, error_msg); |
| 3931 | |
| 3932 | finally: |
| 3933 | PyMem_SetAllocator(domain, &hook.alloc); |
| 3934 | return res; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3935 | |
| 3936 | #undef CHECK_CTX |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3940 | test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3941 | { |
| 3942 | return test_setallocators(PYMEM_DOMAIN_RAW); |
| 3943 | } |
| 3944 | |
| 3945 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3946 | test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3947 | { |
| 3948 | return test_setallocators(PYMEM_DOMAIN_MEM); |
| 3949 | } |
| 3950 | |
| 3951 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3952 | test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3953 | { |
| 3954 | return test_setallocators(PYMEM_DOMAIN_OBJ); |
| 3955 | } |
| 3956 | |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 3957 | /* Most part of the following code is inherited from the pyfailmalloc project |
| 3958 | * written by Victor Stinner. */ |
| 3959 | static struct { |
| 3960 | int installed; |
| 3961 | PyMemAllocatorEx raw; |
| 3962 | PyMemAllocatorEx mem; |
| 3963 | PyMemAllocatorEx obj; |
| 3964 | } FmHook; |
| 3965 | |
| 3966 | static struct { |
| 3967 | int start; |
| 3968 | int stop; |
| 3969 | Py_ssize_t count; |
| 3970 | } FmData; |
| 3971 | |
| 3972 | static int |
| 3973 | fm_nomemory(void) |
| 3974 | { |
| 3975 | FmData.count++; |
| 3976 | if (FmData.count > FmData.start && |
| 3977 | (FmData.stop <= 0 || FmData.count <= FmData.stop)) { |
| 3978 | return 1; |
| 3979 | } |
| 3980 | return 0; |
| 3981 | } |
| 3982 | |
| 3983 | static void * |
| 3984 | hook_fmalloc(void *ctx, size_t size) |
| 3985 | { |
| 3986 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3987 | if (fm_nomemory()) { |
| 3988 | return NULL; |
| 3989 | } |
| 3990 | return alloc->malloc(alloc->ctx, size); |
| 3991 | } |
| 3992 | |
| 3993 | static void * |
| 3994 | hook_fcalloc(void *ctx, size_t nelem, size_t elsize) |
| 3995 | { |
| 3996 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3997 | if (fm_nomemory()) { |
| 3998 | return NULL; |
| 3999 | } |
| 4000 | return alloc->calloc(alloc->ctx, nelem, elsize); |
| 4001 | } |
| 4002 | |
| 4003 | static void * |
| 4004 | hook_frealloc(void *ctx, void *ptr, size_t new_size) |
| 4005 | { |
| 4006 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4007 | if (fm_nomemory()) { |
| 4008 | return NULL; |
| 4009 | } |
| 4010 | return alloc->realloc(alloc->ctx, ptr, new_size); |
| 4011 | } |
| 4012 | |
| 4013 | static void |
| 4014 | hook_ffree(void *ctx, void *ptr) |
| 4015 | { |
| 4016 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4017 | alloc->free(alloc->ctx, ptr); |
| 4018 | } |
| 4019 | |
| 4020 | static void |
| 4021 | fm_setup_hooks(void) |
| 4022 | { |
| 4023 | PyMemAllocatorEx alloc; |
| 4024 | |
| 4025 | if (FmHook.installed) { |
| 4026 | return; |
| 4027 | } |
| 4028 | FmHook.installed = 1; |
| 4029 | |
| 4030 | alloc.malloc = hook_fmalloc; |
| 4031 | alloc.calloc = hook_fcalloc; |
| 4032 | alloc.realloc = hook_frealloc; |
| 4033 | alloc.free = hook_ffree; |
| 4034 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 4035 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 4036 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 4037 | |
| 4038 | alloc.ctx = &FmHook.raw; |
| 4039 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 4040 | |
| 4041 | alloc.ctx = &FmHook.mem; |
| 4042 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 4043 | |
| 4044 | alloc.ctx = &FmHook.obj; |
| 4045 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 4046 | } |
| 4047 | |
| 4048 | static void |
| 4049 | fm_remove_hooks(void) |
| 4050 | { |
| 4051 | if (FmHook.installed) { |
| 4052 | FmHook.installed = 0; |
| 4053 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 4054 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 4055 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | static PyObject* |
| 4060 | set_nomemory(PyObject *self, PyObject *args) |
| 4061 | { |
| 4062 | /* Memory allocation fails after 'start' allocation requests, and until |
| 4063 | * 'stop' allocation requests except when 'stop' is negative or equal |
| 4064 | * to 0 (default) in which case allocation failures never stop. */ |
| 4065 | FmData.count = 0; |
| 4066 | FmData.stop = 0; |
| 4067 | if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) { |
| 4068 | return NULL; |
| 4069 | } |
| 4070 | fm_setup_hooks(); |
| 4071 | Py_RETURN_NONE; |
| 4072 | } |
| 4073 | |
| 4074 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4075 | remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored)) |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 4076 | { |
| 4077 | fm_remove_hooks(); |
| 4078 | Py_RETURN_NONE; |
| 4079 | } |
| 4080 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4081 | PyDoc_STRVAR(docstring_empty, |
| 4082 | "" |
| 4083 | ); |
| 4084 | |
| 4085 | PyDoc_STRVAR(docstring_no_signature, |
| 4086 | "This docstring has no signature." |
| 4087 | ); |
| 4088 | |
| 4089 | PyDoc_STRVAR(docstring_with_invalid_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4090 | "docstring_with_invalid_signature($module, /, boo)\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4091 | "\n" |
| 4092 | "This docstring has an invalid signature." |
| 4093 | ); |
| 4094 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4095 | PyDoc_STRVAR(docstring_with_invalid_signature2, |
| 4096 | "docstring_with_invalid_signature2($module, /, boo)\n" |
| 4097 | "\n" |
| 4098 | "--\n" |
| 4099 | "\n" |
| 4100 | "This docstring also has an invalid signature." |
| 4101 | ); |
| 4102 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4103 | PyDoc_STRVAR(docstring_with_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4104 | "docstring_with_signature($module, /, sig)\n" |
| 4105 | "--\n" |
| 4106 | "\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4107 | "This docstring has a valid signature." |
| 4108 | ); |
| 4109 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 4110 | PyDoc_STRVAR(docstring_with_signature_but_no_doc, |
| 4111 | "docstring_with_signature_but_no_doc($module, /, sig)\n" |
| 4112 | "--\n" |
| 4113 | "\n" |
| 4114 | ); |
| 4115 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4116 | PyDoc_STRVAR(docstring_with_signature_and_extra_newlines, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4117 | "docstring_with_signature_and_extra_newlines($module, /, parameter)\n" |
| 4118 | "--\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4119 | "\n" |
| 4120 | "\n" |
| 4121 | "This docstring has a valid signature and some extra newlines." |
| 4122 | ); |
| 4123 | |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4124 | PyDoc_STRVAR(docstring_with_signature_with_defaults, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4125 | "docstring_with_signature_with_defaults(module, s='avocado',\n" |
| 4126 | " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n" |
| 4127 | " local=the_number_three, sys=sys.maxsize,\n" |
| 4128 | " exp=sys.maxsize - 1)\n" |
| 4129 | "--\n" |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4130 | "\n" |
| 4131 | "\n" |
| 4132 | "\n" |
| 4133 | "This docstring has a valid signature with parameters,\n" |
| 4134 | "and the parameters take defaults of varying types." |
| 4135 | ); |
| 4136 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4137 | typedef struct { |
| 4138 | PyThread_type_lock start_event; |
| 4139 | PyThread_type_lock exit_event; |
| 4140 | PyObject *callback; |
| 4141 | } test_c_thread_t; |
| 4142 | |
| 4143 | static void |
| 4144 | temporary_c_thread(void *data) |
| 4145 | { |
| 4146 | test_c_thread_t *test_c_thread = data; |
| 4147 | PyGILState_STATE state; |
| 4148 | PyObject *res; |
| 4149 | |
| 4150 | PyThread_release_lock(test_c_thread->start_event); |
| 4151 | |
| 4152 | /* Allocate a Python thread state for this thread */ |
| 4153 | state = PyGILState_Ensure(); |
| 4154 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 4155 | res = _PyObject_CallNoArg(test_c_thread->callback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4156 | Py_CLEAR(test_c_thread->callback); |
| 4157 | |
| 4158 | if (res == NULL) { |
| 4159 | PyErr_Print(); |
| 4160 | } |
| 4161 | else { |
| 4162 | Py_DECREF(res); |
| 4163 | } |
| 4164 | |
| 4165 | /* Destroy the Python thread state for this thread */ |
| 4166 | PyGILState_Release(state); |
| 4167 | |
| 4168 | PyThread_release_lock(test_c_thread->exit_event); |
| 4169 | |
| 4170 | PyThread_exit_thread(); |
| 4171 | } |
| 4172 | |
| 4173 | static PyObject * |
| 4174 | call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
| 4175 | { |
| 4176 | PyObject *res = NULL; |
| 4177 | test_c_thread_t test_c_thread; |
| 4178 | long thread; |
| 4179 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4180 | test_c_thread.start_event = PyThread_allocate_lock(); |
| 4181 | test_c_thread.exit_event = PyThread_allocate_lock(); |
| 4182 | test_c_thread.callback = NULL; |
| 4183 | if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
| 4184 | PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
| 4185 | goto exit; |
| 4186 | } |
| 4187 | |
| 4188 | Py_INCREF(callback); |
| 4189 | test_c_thread.callback = callback; |
| 4190 | |
| 4191 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 4192 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 4193 | |
| 4194 | thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
| 4195 | if (thread == -1) { |
| 4196 | PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
| 4197 | PyThread_release_lock(test_c_thread.start_event); |
| 4198 | PyThread_release_lock(test_c_thread.exit_event); |
| 4199 | goto exit; |
| 4200 | } |
| 4201 | |
| 4202 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 4203 | PyThread_release_lock(test_c_thread.start_event); |
| 4204 | |
| 4205 | Py_BEGIN_ALLOW_THREADS |
| 4206 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 4207 | PyThread_release_lock(test_c_thread.exit_event); |
| 4208 | Py_END_ALLOW_THREADS |
| 4209 | |
| 4210 | Py_INCREF(Py_None); |
| 4211 | res = Py_None; |
| 4212 | |
| 4213 | exit: |
| 4214 | Py_CLEAR(test_c_thread.callback); |
| 4215 | if (test_c_thread.start_event) |
| 4216 | PyThread_free_lock(test_c_thread.start_event); |
| 4217 | if (test_c_thread.exit_event) |
| 4218 | PyThread_free_lock(test_c_thread.exit_event); |
| 4219 | return res; |
| 4220 | } |
Victor Stinner | 1310510 | 2013-12-13 02:17:29 +0100 | [diff] [blame] | 4221 | |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4222 | /* marshal */ |
| 4223 | |
| 4224 | static PyObject* |
| 4225 | pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
| 4226 | { |
| 4227 | long value; |
| 4228 | char *filename; |
| 4229 | int version; |
| 4230 | FILE *fp; |
| 4231 | |
| 4232 | if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", |
| 4233 | &value, &filename, &version)) |
| 4234 | return NULL; |
| 4235 | |
| 4236 | fp = fopen(filename, "wb"); |
| 4237 | if (fp == NULL) { |
| 4238 | PyErr_SetFromErrno(PyExc_OSError); |
| 4239 | return NULL; |
| 4240 | } |
| 4241 | |
| 4242 | PyMarshal_WriteLongToFile(value, fp, version); |
| 4243 | |
| 4244 | fclose(fp); |
| 4245 | if (PyErr_Occurred()) |
| 4246 | return NULL; |
| 4247 | Py_RETURN_NONE; |
| 4248 | } |
| 4249 | |
| 4250 | static PyObject* |
| 4251 | pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
| 4252 | { |
| 4253 | PyObject *obj; |
| 4254 | char *filename; |
| 4255 | int version; |
| 4256 | FILE *fp; |
| 4257 | |
| 4258 | if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", |
| 4259 | &obj, &filename, &version)) |
| 4260 | return NULL; |
| 4261 | |
| 4262 | fp = fopen(filename, "wb"); |
| 4263 | if (fp == NULL) { |
| 4264 | PyErr_SetFromErrno(PyExc_OSError); |
| 4265 | return NULL; |
| 4266 | } |
| 4267 | |
| 4268 | PyMarshal_WriteObjectToFile(obj, fp, version); |
| 4269 | |
| 4270 | fclose(fp); |
| 4271 | if (PyErr_Occurred()) |
| 4272 | return NULL; |
| 4273 | Py_RETURN_NONE; |
| 4274 | } |
| 4275 | |
| 4276 | static PyObject* |
| 4277 | pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
| 4278 | { |
| 4279 | int value; |
| 4280 | long pos; |
| 4281 | char *filename; |
| 4282 | FILE *fp; |
| 4283 | |
| 4284 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) |
| 4285 | return NULL; |
| 4286 | |
| 4287 | fp = fopen(filename, "rb"); |
| 4288 | if (fp == NULL) { |
| 4289 | PyErr_SetFromErrno(PyExc_OSError); |
| 4290 | return NULL; |
| 4291 | } |
| 4292 | |
| 4293 | value = PyMarshal_ReadShortFromFile(fp); |
| 4294 | pos = ftell(fp); |
| 4295 | |
| 4296 | fclose(fp); |
| 4297 | if (PyErr_Occurred()) |
| 4298 | return NULL; |
| 4299 | return Py_BuildValue("il", value, pos); |
| 4300 | } |
| 4301 | |
| 4302 | static PyObject* |
| 4303 | pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
| 4304 | { |
| 4305 | long value, pos; |
| 4306 | char *filename; |
| 4307 | FILE *fp; |
| 4308 | |
| 4309 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) |
| 4310 | return NULL; |
| 4311 | |
| 4312 | fp = fopen(filename, "rb"); |
| 4313 | if (fp == NULL) { |
| 4314 | PyErr_SetFromErrno(PyExc_OSError); |
| 4315 | return NULL; |
| 4316 | } |
| 4317 | |
| 4318 | value = PyMarshal_ReadLongFromFile(fp); |
| 4319 | pos = ftell(fp); |
| 4320 | |
| 4321 | fclose(fp); |
| 4322 | if (PyErr_Occurred()) |
| 4323 | return NULL; |
| 4324 | return Py_BuildValue("ll", value, pos); |
| 4325 | } |
| 4326 | |
| 4327 | static PyObject* |
| 4328 | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
| 4329 | { |
| 4330 | PyObject *obj; |
| 4331 | long pos; |
| 4332 | char *filename; |
| 4333 | FILE *fp; |
| 4334 | |
| 4335 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) |
| 4336 | return NULL; |
| 4337 | |
| 4338 | fp = fopen(filename, "rb"); |
| 4339 | if (fp == NULL) { |
| 4340 | PyErr_SetFromErrno(PyExc_OSError); |
| 4341 | return NULL; |
| 4342 | } |
| 4343 | |
| 4344 | obj = PyMarshal_ReadLastObjectFromFile(fp); |
| 4345 | pos = ftell(fp); |
| 4346 | |
| 4347 | fclose(fp); |
| 4348 | return Py_BuildValue("Nl", obj, pos); |
| 4349 | } |
| 4350 | |
| 4351 | static PyObject* |
| 4352 | pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
| 4353 | { |
| 4354 | PyObject *obj; |
| 4355 | long pos; |
| 4356 | char *filename; |
| 4357 | FILE *fp; |
| 4358 | |
| 4359 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) |
| 4360 | return NULL; |
| 4361 | |
| 4362 | fp = fopen(filename, "rb"); |
| 4363 | if (fp == NULL) { |
| 4364 | PyErr_SetFromErrno(PyExc_OSError); |
| 4365 | return NULL; |
| 4366 | } |
| 4367 | |
| 4368 | obj = PyMarshal_ReadObjectFromFile(fp); |
| 4369 | pos = ftell(fp); |
| 4370 | |
| 4371 | fclose(fp); |
| 4372 | return Py_BuildValue("Nl", obj, pos); |
| 4373 | } |
| 4374 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4375 | static PyObject* |
| 4376 | return_null_without_error(PyObject *self, PyObject *args) |
| 4377 | { |
| 4378 | /* invalid call: return NULL without setting an error, |
| 4379 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 4380 | PyErr_Clear(); |
| 4381 | return NULL; |
| 4382 | } |
| 4383 | |
| 4384 | static PyObject* |
| 4385 | return_result_with_error(PyObject *self, PyObject *args) |
| 4386 | { |
| 4387 | /* invalid call: return a result with an error set, |
| 4388 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 4389 | PyErr_SetNone(PyExc_ValueError); |
| 4390 | Py_RETURN_NONE; |
| 4391 | } |
| 4392 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 4393 | static PyObject * |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 4394 | test_pytime_fromseconds(PyObject *self, PyObject *args) |
| 4395 | { |
| 4396 | int seconds; |
| 4397 | _PyTime_t ts; |
| 4398 | |
| 4399 | if (!PyArg_ParseTuple(args, "i", &seconds)) |
| 4400 | return NULL; |
| 4401 | ts = _PyTime_FromSeconds(seconds); |
| 4402 | return _PyTime_AsNanosecondsObject(ts); |
| 4403 | } |
| 4404 | |
| 4405 | static PyObject * |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 4406 | test_pytime_fromsecondsobject(PyObject *self, PyObject *args) |
| 4407 | { |
| 4408 | PyObject *obj; |
| 4409 | int round; |
| 4410 | _PyTime_t ts; |
| 4411 | |
| 4412 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
| 4413 | return NULL; |
| 4414 | if (check_time_rounding(round) < 0) |
| 4415 | return NULL; |
| 4416 | if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) |
| 4417 | return NULL; |
| 4418 | return _PyTime_AsNanosecondsObject(ts); |
| 4419 | } |
| 4420 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4421 | static PyObject * |
| 4422 | test_pytime_assecondsdouble(PyObject *self, PyObject *args) |
| 4423 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4424 | PyObject *obj; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4425 | _PyTime_t ts; |
| 4426 | double d; |
| 4427 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4428 | if (!PyArg_ParseTuple(args, "O", &obj)) { |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4429 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4430 | } |
| 4431 | if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) { |
| 4432 | return NULL; |
| 4433 | } |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4434 | d = _PyTime_AsSecondsDouble(ts); |
| 4435 | return PyFloat_FromDouble(d); |
| 4436 | } |
| 4437 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4438 | static PyObject * |
| 4439 | test_PyTime_AsTimeval(PyObject *self, PyObject *args) |
| 4440 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4441 | PyObject *obj; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4442 | int round; |
| 4443 | _PyTime_t t; |
| 4444 | struct timeval tv; |
| 4445 | PyObject *seconds; |
| 4446 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4447 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4448 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4449 | if (check_time_rounding(round) < 0) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4450 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4451 | } |
| 4452 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4453 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4454 | } |
| 4455 | if (_PyTime_AsTimeval(t, &tv, round) < 0) { |
| 4456 | return NULL; |
| 4457 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4458 | |
Benjamin Peterson | 2c134c3 | 2017-04-13 01:44:54 -0700 | [diff] [blame] | 4459 | seconds = PyLong_FromLongLong(tv.tv_sec); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4460 | if (seconds == NULL) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4461 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4462 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4463 | return Py_BuildValue("Nl", seconds, tv.tv_usec); |
| 4464 | } |
| 4465 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4466 | #ifdef HAVE_CLOCK_GETTIME |
| 4467 | static PyObject * |
| 4468 | test_PyTime_AsTimespec(PyObject *self, PyObject *args) |
| 4469 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4470 | PyObject *obj; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4471 | _PyTime_t t; |
| 4472 | struct timespec ts; |
| 4473 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4474 | if (!PyArg_ParseTuple(args, "O", &obj)) { |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4475 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4476 | } |
| 4477 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4478 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4479 | } |
| 4480 | if (_PyTime_AsTimespec(t, &ts) == -1) { |
| 4481 | return NULL; |
| 4482 | } |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4483 | return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); |
| 4484 | } |
| 4485 | #endif |
| 4486 | |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4487 | static PyObject * |
| 4488 | test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) |
| 4489 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4490 | PyObject *obj; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4491 | int round; |
| 4492 | _PyTime_t t, ms; |
| 4493 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4494 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4495 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4496 | } |
| 4497 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4498 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4499 | } |
| 4500 | if (check_time_rounding(round) < 0) { |
| 4501 | return NULL; |
| 4502 | } |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4503 | ms = _PyTime_AsMilliseconds(t, round); |
| 4504 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4505 | nanoseconds */ |
| 4506 | return _PyTime_AsNanosecondsObject(ms); |
| 4507 | } |
| 4508 | |
| 4509 | static PyObject * |
| 4510 | test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) |
| 4511 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4512 | PyObject *obj; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4513 | int round; |
| 4514 | _PyTime_t t, ms; |
| 4515 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4516 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4517 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4518 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4519 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4520 | } |
| 4521 | if (check_time_rounding(round) < 0) { |
| 4522 | return NULL; |
| 4523 | } |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4524 | ms = _PyTime_AsMicroseconds(t, round); |
| 4525 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4526 | nanoseconds */ |
| 4527 | return _PyTime_AsNanosecondsObject(ms); |
| 4528 | } |
| 4529 | |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4530 | static PyObject* |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4531 | pymem_buffer_overflow(PyObject *self, PyObject *args) |
| 4532 | { |
| 4533 | char *buffer; |
| 4534 | |
| 4535 | /* Deliberate buffer overflow to check that PyMem_Free() detects |
| 4536 | the overflow when debug hooks are installed. */ |
| 4537 | buffer = PyMem_Malloc(16); |
Victor Stinner | 414b1cd | 2019-03-26 14:35:30 +0100 | [diff] [blame] | 4538 | if (buffer == NULL) { |
| 4539 | PyErr_NoMemory(); |
| 4540 | return NULL; |
| 4541 | } |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4542 | buffer[16] = 'x'; |
| 4543 | PyMem_Free(buffer); |
| 4544 | |
| 4545 | Py_RETURN_NONE; |
| 4546 | } |
| 4547 | |
| 4548 | static PyObject* |
| 4549 | pymem_api_misuse(PyObject *self, PyObject *args) |
| 4550 | { |
| 4551 | char *buffer; |
| 4552 | |
| 4553 | /* Deliberate misusage of Python allocators: |
| 4554 | allococate with PyMem but release with PyMem_Raw. */ |
| 4555 | buffer = PyMem_Malloc(16); |
| 4556 | PyMem_RawFree(buffer); |
| 4557 | |
| 4558 | Py_RETURN_NONE; |
| 4559 | } |
| 4560 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4561 | static PyObject* |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4562 | pymem_malloc_without_gil(PyObject *self, PyObject *args) |
| 4563 | { |
| 4564 | char *buffer; |
| 4565 | |
| 4566 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4567 | call PyMem_Malloc() without holding the GIL */ |
| 4568 | Py_BEGIN_ALLOW_THREADS |
| 4569 | buffer = PyMem_Malloc(10); |
| 4570 | Py_END_ALLOW_THREADS |
| 4571 | |
| 4572 | PyMem_Free(buffer); |
| 4573 | |
| 4574 | Py_RETURN_NONE; |
| 4575 | } |
| 4576 | |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 4577 | |
| 4578 | static PyObject* |
| 4579 | test_pymem_getallocatorsname(PyObject *self, PyObject *args) |
| 4580 | { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 4581 | const char *name = _PyMem_GetCurrentAllocatorName(); |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 4582 | if (name == NULL) { |
| 4583 | PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name"); |
| 4584 | return NULL; |
| 4585 | } |
| 4586 | return PyUnicode_FromString(name); |
| 4587 | } |
| 4588 | |
| 4589 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4590 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4591 | test_pyobject_is_freed(const char *test_name, PyObject *op) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4592 | { |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4593 | if (!_PyObject_IsFreed(op)) { |
| 4594 | return raiseTestError(test_name, "object is not seen as freed"); |
| 4595 | } |
| 4596 | Py_RETURN_NONE; |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | |
| 4600 | static PyObject* |
Victor Stinner | 6876257 | 2019-10-07 18:42:01 +0200 | [diff] [blame] | 4601 | check_pyobject_null_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
| 4602 | { |
| 4603 | PyObject *op = NULL; |
| 4604 | return test_pyobject_is_freed("check_pyobject_null_is_freed", op); |
| 4605 | } |
| 4606 | |
| 4607 | |
| 4608 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4609 | check_pyobject_uninitialized_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4610 | { |
| 4611 | PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject)); |
| 4612 | if (op == NULL) { |
| 4613 | return NULL; |
| 4614 | } |
| 4615 | /* Initialize reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4616 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4617 | /* object fields like ob_type are uninitialized! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4618 | return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | |
| 4622 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4623 | check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4624 | { |
| 4625 | /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */ |
| 4626 | PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type)); |
| 4627 | if (op == NULL) { |
| 4628 | return NULL; |
| 4629 | } |
| 4630 | /* Initialize reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4631 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4632 | /* 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] | 4633 | when using debug hooks on memory allocators! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4634 | return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | |
| 4638 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4639 | check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4640 | { |
| 4641 | PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); |
| 4642 | if (op == NULL) { |
| 4643 | return NULL; |
| 4644 | } |
| 4645 | Py_TYPE(op)->tp_dealloc(op); |
| 4646 | /* Reset reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4647 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4648 | /* object memory is freed! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4649 | return test_pyobject_is_freed("check_pyobject_freed_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | |
| 4653 | static PyObject* |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4654 | pyobject_malloc_without_gil(PyObject *self, PyObject *args) |
| 4655 | { |
| 4656 | char *buffer; |
| 4657 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4658 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4659 | call PyObject_Malloc() without holding the GIL */ |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4660 | Py_BEGIN_ALLOW_THREADS |
| 4661 | buffer = PyObject_Malloc(10); |
| 4662 | Py_END_ALLOW_THREADS |
| 4663 | |
| 4664 | PyObject_Free(buffer); |
| 4665 | |
| 4666 | Py_RETURN_NONE; |
| 4667 | } |
| 4668 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4669 | static PyObject * |
| 4670 | tracemalloc_track(PyObject *self, PyObject *args) |
| 4671 | { |
| 4672 | unsigned int domain; |
| 4673 | PyObject *ptr_obj; |
| 4674 | void *ptr; |
| 4675 | Py_ssize_t size; |
| 4676 | int release_gil = 0; |
| 4677 | int res; |
| 4678 | |
| 4679 | if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil)) |
| 4680 | return NULL; |
| 4681 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4682 | if (PyErr_Occurred()) |
| 4683 | return NULL; |
| 4684 | |
| 4685 | if (release_gil) { |
| 4686 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4687 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4688 | Py_END_ALLOW_THREADS |
| 4689 | } |
| 4690 | else { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4691 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4695 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4696 | return NULL; |
| 4697 | } |
| 4698 | |
| 4699 | Py_RETURN_NONE; |
| 4700 | } |
| 4701 | |
| 4702 | static PyObject * |
| 4703 | tracemalloc_untrack(PyObject *self, PyObject *args) |
| 4704 | { |
| 4705 | unsigned int domain; |
| 4706 | PyObject *ptr_obj; |
| 4707 | void *ptr; |
| 4708 | int res; |
| 4709 | |
| 4710 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4711 | return NULL; |
| 4712 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4713 | if (PyErr_Occurred()) |
| 4714 | return NULL; |
| 4715 | |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4716 | res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4717 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4718 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4719 | return NULL; |
| 4720 | } |
| 4721 | |
| 4722 | Py_RETURN_NONE; |
| 4723 | } |
| 4724 | |
| 4725 | static PyObject * |
| 4726 | tracemalloc_get_traceback(PyObject *self, PyObject *args) |
| 4727 | { |
| 4728 | unsigned int domain; |
| 4729 | PyObject *ptr_obj; |
| 4730 | void *ptr; |
| 4731 | |
| 4732 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4733 | return NULL; |
| 4734 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4735 | if (PyErr_Occurred()) |
| 4736 | return NULL; |
| 4737 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 4738 | return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4739 | } |
| 4740 | |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4741 | static PyObject * |
| 4742 | dict_get_version(PyObject *self, PyObject *args) |
| 4743 | { |
| 4744 | PyDictObject *dict; |
| 4745 | uint64_t version; |
| 4746 | |
| 4747 | if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict)) |
| 4748 | return NULL; |
| 4749 | |
| 4750 | version = dict->ma_version_tag; |
| 4751 | |
Sergey Fedoseev | a9ed91e | 2019-10-21 11:49:48 +0500 | [diff] [blame] | 4752 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(version)); |
| 4753 | return PyLong_FromUnsignedLongLong((unsigned long long)version); |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4754 | } |
| 4755 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4756 | |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 4757 | static PyObject * |
| 4758 | raise_SIGINT_then_send_None(PyObject *self, PyObject *args) |
| 4759 | { |
| 4760 | PyGenObject *gen; |
| 4761 | |
| 4762 | if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen)) |
| 4763 | return NULL; |
| 4764 | |
| 4765 | /* This is used in a test to check what happens if a signal arrives just |
| 4766 | as we're in the process of entering a yield from chain (see |
| 4767 | bpo-30039). |
| 4768 | |
| 4769 | Needs to be done in C, because: |
| 4770 | - we don't have a Python wrapper for raise() |
| 4771 | - we need to make sure that the Python-level signal handler doesn't run |
| 4772 | *before* we enter the generator frame, which is impossible in Python |
| 4773 | because we check for signals before every bytecode operation. |
| 4774 | */ |
| 4775 | raise(SIGINT); |
| 4776 | return _PyGen_Send(gen, Py_None); |
| 4777 | } |
| 4778 | |
| 4779 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4780 | static int |
| 4781 | fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs) |
| 4782 | { |
| 4783 | if (args == Py_None) { |
| 4784 | *stack = NULL; |
| 4785 | *nargs = 0; |
| 4786 | } |
| 4787 | else if (PyTuple_Check(args)) { |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 4788 | *stack = ((PyTupleObject *)args)->ob_item; |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4789 | *nargs = PyTuple_GET_SIZE(args); |
| 4790 | } |
| 4791 | else { |
| 4792 | PyErr_SetString(PyExc_TypeError, "args must be None or a tuple"); |
| 4793 | return -1; |
| 4794 | } |
| 4795 | return 0; |
| 4796 | } |
| 4797 | |
| 4798 | |
| 4799 | static PyObject * |
| 4800 | test_pyobject_fastcall(PyObject *self, PyObject *args) |
| 4801 | { |
| 4802 | PyObject *func, *func_args; |
| 4803 | PyObject **stack; |
| 4804 | Py_ssize_t nargs; |
| 4805 | |
| 4806 | if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) { |
| 4807 | return NULL; |
| 4808 | } |
| 4809 | |
| 4810 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4811 | return NULL; |
| 4812 | } |
| 4813 | return _PyObject_FastCall(func, stack, nargs); |
| 4814 | } |
| 4815 | |
| 4816 | |
| 4817 | static PyObject * |
| 4818 | test_pyobject_fastcalldict(PyObject *self, PyObject *args) |
| 4819 | { |
| 4820 | PyObject *func, *func_args, *kwargs; |
| 4821 | PyObject **stack; |
| 4822 | Py_ssize_t nargs; |
| 4823 | |
| 4824 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) { |
| 4825 | return NULL; |
| 4826 | } |
| 4827 | |
| 4828 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4829 | return NULL; |
| 4830 | } |
| 4831 | |
| 4832 | if (kwargs == Py_None) { |
| 4833 | kwargs = NULL; |
| 4834 | } |
| 4835 | else if (!PyDict_Check(kwargs)) { |
| 4836 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict"); |
| 4837 | return NULL; |
| 4838 | } |
| 4839 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 4840 | return PyObject_VectorcallDict(func, stack, nargs, kwargs); |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | |
| 4844 | static PyObject * |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4845 | test_pyobject_vectorcall(PyObject *self, PyObject *args) |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4846 | { |
| 4847 | PyObject *func, *func_args, *kwnames = NULL; |
| 4848 | PyObject **stack; |
| 4849 | Py_ssize_t nargs, nkw; |
| 4850 | |
| 4851 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) { |
| 4852 | return NULL; |
| 4853 | } |
| 4854 | |
| 4855 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4856 | return NULL; |
| 4857 | } |
| 4858 | |
| 4859 | if (kwnames == Py_None) { |
| 4860 | kwnames = NULL; |
| 4861 | } |
| 4862 | else if (PyTuple_Check(kwnames)) { |
| 4863 | nkw = PyTuple_GET_SIZE(kwnames); |
| 4864 | if (nargs < nkw) { |
| 4865 | PyErr_SetString(PyExc_ValueError, "kwnames longer than args"); |
| 4866 | return NULL; |
| 4867 | } |
| 4868 | nargs -= nkw; |
| 4869 | } |
| 4870 | else { |
| 4871 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple"); |
| 4872 | return NULL; |
| 4873 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 4874 | return PyObject_Vectorcall(func, stack, nargs, kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | |
| 4878 | static PyObject * |
| 4879 | test_pyvectorcall_call(PyObject *self, PyObject *args) |
| 4880 | { |
| 4881 | PyObject *func; |
| 4882 | PyObject *argstuple; |
| 4883 | PyObject *kwargs = NULL; |
| 4884 | |
| 4885 | if (!PyArg_ParseTuple(args, "OO|O", &func, &argstuple, &kwargs)) { |
| 4886 | return NULL; |
| 4887 | } |
| 4888 | |
| 4889 | if (!PyTuple_Check(argstuple)) { |
| 4890 | PyErr_SetString(PyExc_TypeError, "args must be a tuple"); |
| 4891 | return NULL; |
| 4892 | } |
| 4893 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
| 4894 | PyErr_SetString(PyExc_TypeError, "kwargs must be a dict"); |
| 4895 | return NULL; |
| 4896 | } |
| 4897 | |
| 4898 | return PyVectorcall_Call(func, argstuple, kwargs); |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4899 | } |
| 4900 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4901 | |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 4902 | static PyObject* |
| 4903 | stack_pointer(PyObject *self, PyObject *args) |
| 4904 | { |
| 4905 | int v = 5; |
| 4906 | return PyLong_FromVoidPtr(&v); |
| 4907 | } |
| 4908 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4909 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4910 | #ifdef W_STOPCODE |
| 4911 | static PyObject* |
| 4912 | py_w_stopcode(PyObject *self, PyObject *args) |
| 4913 | { |
| 4914 | int sig, status; |
| 4915 | if (!PyArg_ParseTuple(args, "i", &sig)) { |
| 4916 | return NULL; |
| 4917 | } |
| 4918 | status = W_STOPCODE(sig); |
| 4919 | return PyLong_FromLong(status); |
| 4920 | } |
| 4921 | #endif |
| 4922 | |
| 4923 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 4924 | static PyObject * |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 4925 | get_mapping_keys(PyObject* self, PyObject *obj) |
| 4926 | { |
| 4927 | return PyMapping_Keys(obj); |
| 4928 | } |
| 4929 | |
| 4930 | static PyObject * |
| 4931 | get_mapping_values(PyObject* self, PyObject *obj) |
| 4932 | { |
| 4933 | return PyMapping_Values(obj); |
| 4934 | } |
| 4935 | |
| 4936 | static PyObject * |
| 4937 | get_mapping_items(PyObject* self, PyObject *obj) |
| 4938 | { |
| 4939 | return PyMapping_Items(obj); |
| 4940 | } |
| 4941 | |
| 4942 | |
| 4943 | static PyObject * |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 4944 | test_pythread_tss_key_state(PyObject *self, PyObject *args) |
| 4945 | { |
| 4946 | Py_tss_t tss_key = Py_tss_NEEDS_INIT; |
| 4947 | if (PyThread_tss_is_created(&tss_key)) { |
| 4948 | return raiseTestError("test_pythread_tss_key_state", |
| 4949 | "TSS key not in an uninitialized state at " |
| 4950 | "creation time"); |
| 4951 | } |
| 4952 | if (PyThread_tss_create(&tss_key) != 0) { |
| 4953 | PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed"); |
| 4954 | return NULL; |
| 4955 | } |
| 4956 | if (!PyThread_tss_is_created(&tss_key)) { |
| 4957 | return raiseTestError("test_pythread_tss_key_state", |
| 4958 | "PyThread_tss_create succeeded, " |
| 4959 | "but with TSS key in an uninitialized state"); |
| 4960 | } |
| 4961 | if (PyThread_tss_create(&tss_key) != 0) { |
| 4962 | return raiseTestError("test_pythread_tss_key_state", |
| 4963 | "PyThread_tss_create unsuccessful with " |
| 4964 | "an already initialized key"); |
| 4965 | } |
| 4966 | #define CHECK_TSS_API(expr) \ |
| 4967 | (void)(expr); \ |
| 4968 | if (!PyThread_tss_is_created(&tss_key)) { \ |
| 4969 | return raiseTestError("test_pythread_tss_key_state", \ |
| 4970 | "TSS key initialization state was not " \ |
| 4971 | "preserved after calling " #expr); } |
| 4972 | CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL)); |
| 4973 | CHECK_TSS_API(PyThread_tss_get(&tss_key)); |
| 4974 | #undef CHECK_TSS_API |
| 4975 | PyThread_tss_delete(&tss_key); |
| 4976 | if (PyThread_tss_is_created(&tss_key)) { |
| 4977 | return raiseTestError("test_pythread_tss_key_state", |
| 4978 | "PyThread_tss_delete called, but did not " |
| 4979 | "set the key state to uninitialized"); |
| 4980 | } |
| 4981 | |
| 4982 | Py_tss_t *ptr_key = PyThread_tss_alloc(); |
| 4983 | if (ptr_key == NULL) { |
| 4984 | PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed"); |
| 4985 | return NULL; |
| 4986 | } |
| 4987 | if (PyThread_tss_is_created(ptr_key)) { |
| 4988 | return raiseTestError("test_pythread_tss_key_state", |
| 4989 | "TSS key not in an uninitialized state at " |
| 4990 | "allocation time"); |
| 4991 | } |
| 4992 | PyThread_tss_free(ptr_key); |
| 4993 | ptr_key = NULL; |
| 4994 | Py_RETURN_NONE; |
| 4995 | } |
| 4996 | |
| 4997 | |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 4998 | static PyObject* |
| 4999 | new_hamt(PyObject *self, PyObject *args) |
| 5000 | { |
| 5001 | return _PyContext_NewHamtForTests(); |
| 5002 | } |
| 5003 | |
| 5004 | |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5005 | /* def bad_get(self, obj, cls): |
| 5006 | cls() |
| 5007 | return repr(self) |
| 5008 | */ |
| 5009 | static PyObject* |
| 5010 | bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 5011 | { |
Serhiy Storchaka | b1dede3 | 2018-11-20 20:45:40 +0200 | [diff] [blame] | 5012 | PyObject *self, *obj, *cls; |
| 5013 | if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) { |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5014 | return NULL; |
| 5015 | } |
| 5016 | |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 5017 | PyObject *res = _PyObject_CallNoArg(cls); |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5018 | if (res == NULL) { |
| 5019 | return NULL; |
| 5020 | } |
| 5021 | Py_DECREF(res); |
| 5022 | |
Serhiy Storchaka | b1dede3 | 2018-11-20 20:45:40 +0200 | [diff] [blame] | 5023 | return PyObject_Repr(self); |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 5027 | static PyObject * |
| 5028 | encode_locale_ex(PyObject *self, PyObject *args) |
| 5029 | { |
| 5030 | PyObject *unicode; |
| 5031 | int current_locale = 0; |
| 5032 | wchar_t *wstr; |
| 5033 | PyObject *res = NULL; |
| 5034 | const char *errors = NULL; |
| 5035 | |
| 5036 | if (!PyArg_ParseTuple(args, "U|is", &unicode, ¤t_locale, &errors)) { |
| 5037 | return NULL; |
| 5038 | } |
| 5039 | wstr = PyUnicode_AsWideCharString(unicode, NULL); |
| 5040 | if (wstr == NULL) { |
| 5041 | return NULL; |
| 5042 | } |
| 5043 | _Py_error_handler error_handler = _Py_GetErrorHandler(errors); |
| 5044 | |
| 5045 | char *str = NULL; |
| 5046 | size_t error_pos; |
| 5047 | const char *reason = NULL; |
| 5048 | int ret = _Py_EncodeLocaleEx(wstr, |
| 5049 | &str, &error_pos, &reason, |
| 5050 | current_locale, error_handler); |
| 5051 | PyMem_Free(wstr); |
| 5052 | |
| 5053 | switch(ret) { |
| 5054 | case 0: |
| 5055 | res = PyBytes_FromString(str); |
| 5056 | PyMem_RawFree(str); |
| 5057 | break; |
| 5058 | case -1: |
| 5059 | PyErr_NoMemory(); |
| 5060 | break; |
| 5061 | case -2: |
| 5062 | PyErr_Format(PyExc_RuntimeError, "encode error: pos=%zu, reason=%s", |
| 5063 | error_pos, reason); |
| 5064 | break; |
| 5065 | case -3: |
| 5066 | PyErr_SetString(PyExc_ValueError, "unsupported error handler"); |
| 5067 | break; |
| 5068 | default: |
| 5069 | PyErr_SetString(PyExc_ValueError, "unknow error code"); |
| 5070 | break; |
| 5071 | } |
| 5072 | return res; |
| 5073 | } |
| 5074 | |
| 5075 | |
| 5076 | static PyObject * |
| 5077 | decode_locale_ex(PyObject *self, PyObject *args) |
| 5078 | { |
| 5079 | char *str; |
| 5080 | int current_locale = 0; |
| 5081 | PyObject *res = NULL; |
| 5082 | const char *errors = NULL; |
| 5083 | |
| 5084 | if (!PyArg_ParseTuple(args, "y|is", &str, ¤t_locale, &errors)) { |
| 5085 | return NULL; |
| 5086 | } |
| 5087 | _Py_error_handler error_handler = _Py_GetErrorHandler(errors); |
| 5088 | |
| 5089 | wchar_t *wstr = NULL; |
| 5090 | size_t wlen = 0; |
| 5091 | const char *reason = NULL; |
| 5092 | int ret = _Py_DecodeLocaleEx(str, |
| 5093 | &wstr, &wlen, &reason, |
| 5094 | current_locale, error_handler); |
| 5095 | |
| 5096 | switch(ret) { |
| 5097 | case 0: |
| 5098 | res = PyUnicode_FromWideChar(wstr, wlen); |
| 5099 | PyMem_RawFree(wstr); |
| 5100 | break; |
| 5101 | case -1: |
| 5102 | PyErr_NoMemory(); |
| 5103 | break; |
| 5104 | case -2: |
| 5105 | PyErr_Format(PyExc_RuntimeError, "decode error: pos=%zu, reason=%s", |
| 5106 | wlen, reason); |
| 5107 | break; |
| 5108 | case -3: |
| 5109 | PyErr_SetString(PyExc_ValueError, "unsupported error handler"); |
| 5110 | break; |
| 5111 | default: |
| 5112 | PyErr_SetString(PyExc_ValueError, "unknow error code"); |
| 5113 | break; |
| 5114 | } |
| 5115 | return res; |
| 5116 | } |
| 5117 | |
| 5118 | |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5119 | #ifdef Py_REF_DEBUG |
| 5120 | static PyObject * |
| 5121 | negative_refcount(PyObject *self, PyObject *Py_UNUSED(args)) |
| 5122 | { |
| 5123 | PyObject *obj = PyUnicode_FromString("negative_refcount"); |
| 5124 | if (obj == NULL) { |
| 5125 | return NULL; |
| 5126 | } |
| 5127 | assert(Py_REFCNT(obj) == 1); |
| 5128 | |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 5129 | Py_SET_REFCNT(obj, 0); |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5130 | /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */ |
| 5131 | Py_DECREF(obj); |
| 5132 | |
| 5133 | Py_RETURN_NONE; |
| 5134 | } |
| 5135 | #endif |
| 5136 | |
| 5137 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5138 | static PyObject* |
| 5139 | test_write_unraisable_exc(PyObject *self, PyObject *args) |
| 5140 | { |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5141 | PyObject *exc, *err_msg, *obj; |
| 5142 | if (!PyArg_ParseTuple(args, "OOO", &exc, &err_msg, &obj)) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5143 | return NULL; |
| 5144 | } |
| 5145 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5146 | const char *err_msg_utf8; |
| 5147 | if (err_msg != Py_None) { |
| 5148 | err_msg_utf8 = PyUnicode_AsUTF8(err_msg); |
| 5149 | if (err_msg_utf8 == NULL) { |
| 5150 | return NULL; |
| 5151 | } |
| 5152 | } |
| 5153 | else { |
| 5154 | err_msg_utf8 = NULL; |
| 5155 | } |
| 5156 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5157 | PyErr_SetObject((PyObject *)Py_TYPE(exc), exc); |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5158 | _PyErr_WriteUnraisableMsg(err_msg_utf8, obj); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5159 | Py_RETURN_NONE; |
| 5160 | } |
| 5161 | |
| 5162 | |
Sergey Fedoseev | 92709a2 | 2019-09-09 21:28:34 +0500 | [diff] [blame] | 5163 | static PyObject * |
| 5164 | sequence_getitem(PyObject *self, PyObject *args) |
| 5165 | { |
| 5166 | PyObject *seq; |
| 5167 | Py_ssize_t i; |
| 5168 | if (!PyArg_ParseTuple(args, "On", &seq, &i)) { |
| 5169 | return NULL; |
| 5170 | } |
| 5171 | return PySequence_GetItem(seq, i); |
| 5172 | } |
| 5173 | |
| 5174 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5175 | /* Functions for testing C calling conventions (METH_*) are named meth_*, |
| 5176 | * e.g. "meth_varargs" for METH_VARARGS. |
| 5177 | * |
| 5178 | * They all return a tuple of their C-level arguments, with None instead |
| 5179 | * of NULL and Python tuples instead of C arrays. |
| 5180 | */ |
| 5181 | |
| 5182 | |
| 5183 | static PyObject* |
| 5184 | _null_to_none(PyObject* obj) |
| 5185 | { |
| 5186 | if (obj == NULL) { |
| 5187 | Py_RETURN_NONE; |
| 5188 | } |
| 5189 | Py_INCREF(obj); |
| 5190 | return obj; |
| 5191 | } |
| 5192 | |
| 5193 | static PyObject* |
| 5194 | meth_varargs(PyObject* self, PyObject* args) |
| 5195 | { |
| 5196 | return Py_BuildValue("NO", _null_to_none(self), args); |
| 5197 | } |
| 5198 | |
| 5199 | static PyObject* |
| 5200 | meth_varargs_keywords(PyObject* self, PyObject* args, PyObject* kwargs) |
| 5201 | { |
| 5202 | return Py_BuildValue("NON", _null_to_none(self), args, _null_to_none(kwargs)); |
| 5203 | } |
| 5204 | |
| 5205 | static PyObject* |
| 5206 | meth_o(PyObject* self, PyObject* obj) |
| 5207 | { |
| 5208 | return Py_BuildValue("NO", _null_to_none(self), obj); |
| 5209 | } |
| 5210 | |
| 5211 | static PyObject* |
| 5212 | meth_noargs(PyObject* self, PyObject* ignored) |
| 5213 | { |
| 5214 | return _null_to_none(self); |
| 5215 | } |
| 5216 | |
| 5217 | static PyObject* |
| 5218 | _fastcall_to_tuple(PyObject* const* args, Py_ssize_t nargs) |
| 5219 | { |
| 5220 | PyObject *tuple = PyTuple_New(nargs); |
| 5221 | if (tuple == NULL) { |
| 5222 | return NULL; |
| 5223 | } |
| 5224 | for (Py_ssize_t i=0; i < nargs; i++) { |
| 5225 | Py_INCREF(args[i]); |
| 5226 | PyTuple_SET_ITEM(tuple, i, args[i]); |
| 5227 | } |
| 5228 | return tuple; |
| 5229 | } |
| 5230 | |
| 5231 | static PyObject* |
| 5232 | meth_fastcall(PyObject* self, PyObject* const* args, Py_ssize_t nargs) |
| 5233 | { |
| 5234 | return Py_BuildValue( |
| 5235 | "NN", _null_to_none(self), _fastcall_to_tuple(args, nargs) |
| 5236 | ); |
| 5237 | } |
| 5238 | |
| 5239 | static PyObject* |
| 5240 | meth_fastcall_keywords(PyObject* self, PyObject* const* args, |
| 5241 | Py_ssize_t nargs, PyObject* kwargs) |
| 5242 | { |
| 5243 | PyObject *pyargs = _fastcall_to_tuple(args, nargs); |
| 5244 | if (pyargs == NULL) { |
| 5245 | return NULL; |
| 5246 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5247 | PyObject *pykwargs = PyObject_Vectorcall((PyObject*)&PyDict_Type, |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5248 | args + nargs, 0, kwargs); |
| 5249 | return Py_BuildValue("NNN", _null_to_none(self), pyargs, pykwargs); |
| 5250 | } |
| 5251 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 5252 | |
Serhiy Storchaka | e5ccc94 | 2020-03-09 20:03:38 +0200 | [diff] [blame] | 5253 | static PyObject* |
| 5254 | pynumber_tobase(PyObject *module, PyObject *args) |
| 5255 | { |
| 5256 | PyObject *obj; |
| 5257 | int base; |
| 5258 | if (!PyArg_ParseTuple(args, "Oi:pynumber_tobase", |
| 5259 | &obj, &base)) { |
| 5260 | return NULL; |
| 5261 | } |
| 5262 | return PyNumber_ToBase(obj, base); |
| 5263 | } |
| 5264 | |
| 5265 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 5266 | static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *); |
| 5267 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 5268 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5269 | {"raise_exception", raise_exception, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5270 | {"raise_memoryerror", raise_memoryerror, METH_NOARGS}, |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 5271 | {"set_errno", set_errno, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5272 | {"test_config", test_config, METH_NOARGS}, |
| 5273 | {"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS}, |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 5274 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 5275 | {"datetime_check_date", datetime_check_date, METH_VARARGS}, |
| 5276 | {"datetime_check_time", datetime_check_time, METH_VARARGS}, |
| 5277 | {"datetime_check_datetime", datetime_check_datetime, METH_VARARGS}, |
| 5278 | {"datetime_check_delta", datetime_check_delta, METH_VARARGS}, |
| 5279 | {"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS}, |
| 5280 | {"make_timezones_capi", make_timezones_capi, METH_NOARGS}, |
Paul Ganssle | a049f57 | 2018-02-22 15:15:32 -0500 | [diff] [blame] | 5281 | {"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS}, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 5282 | {"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS}, |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 5283 | {"get_date_fromdate", get_date_fromdate, METH_VARARGS}, |
| 5284 | {"get_datetime_fromdateandtime", get_datetime_fromdateandtime, METH_VARARGS}, |
| 5285 | {"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS}, |
| 5286 | {"get_time_fromtime", get_time_fromtime, METH_VARARGS}, |
| 5287 | {"get_time_fromtimeandfold", get_time_fromtimeandfold, METH_VARARGS}, |
| 5288 | {"get_delta_fromdsu", get_delta_fromdsu, METH_VARARGS}, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 5289 | {"get_date_fromtimestamp", get_date_fromtimestamp, METH_VARARGS}, |
| 5290 | {"get_datetime_fromtimestamp", get_datetime_fromtimestamp, METH_VARARGS}, |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 5291 | {"PyDateTime_GET", test_PyDateTime_GET, METH_O}, |
| 5292 | {"PyDateTime_DATE_GET", test_PyDateTime_DATE_GET, METH_O}, |
| 5293 | {"PyDateTime_TIME_GET", test_PyDateTime_TIME_GET, METH_O}, |
| 5294 | {"PyDateTime_DELTA_GET", test_PyDateTime_DELTA_GET, METH_O}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5295 | {"test_list_api", test_list_api, METH_NOARGS}, |
| 5296 | {"test_dict_iteration", test_dict_iteration, METH_NOARGS}, |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 5297 | {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 5298 | {"dict_hassplittable", dict_hassplittable, METH_O}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5299 | {"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS}, |
| 5300 | {"test_long_api", test_long_api, METH_NOARGS}, |
| 5301 | {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS}, |
| 5302 | {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS}, |
| 5303 | {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS}, |
| 5304 | {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS}, |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 5305 | {"test_structseq_newtype_doesnt_leak", |
| 5306 | test_structseq_newtype_doesnt_leak, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5307 | {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS}, |
| 5308 | {"test_long_and_overflow", test_long_and_overflow, METH_NOARGS}, |
| 5309 | {"test_long_as_double", test_long_as_double, METH_NOARGS}, |
| 5310 | {"test_long_as_size_t", test_long_as_size_t, METH_NOARGS}, |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 5311 | {"test_long_as_unsigned_long_long_mask", |
| 5312 | test_long_as_unsigned_long_long_mask, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5313 | {"test_long_numbits", test_long_numbits, METH_NOARGS}, |
| 5314 | {"test_k_code", test_k_code, METH_NOARGS}, |
| 5315 | {"test_empty_argparse", test_empty_argparse, METH_NOARGS}, |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 5316 | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5317 | {"test_null_strings", test_null_strings, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5318 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5319 | {"test_with_docstring", test_with_docstring, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5320 | PyDoc_STR("This is a pretty normal docstring.")}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5321 | {"test_string_to_double", test_string_to_double, METH_NOARGS}, |
| 5322 | {"test_unicode_compare_with_ascii", test_unicode_compare_with_ascii, |
| 5323 | METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5324 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 5325 | {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 5326 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 5327 | {"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] | 5328 | #endif |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 5329 | {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, |
Joannah Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 5330 | {"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS}, |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 5331 | {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 5332 | {"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS}, |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 5333 | {"get_args", get_args, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5334 | {"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5336 | {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5337 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5338 | {"getargs_keyword_only", (PyCFunction)(void(*)(void))getargs_keyword_only, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 5339 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 5340 | {"getargs_positional_only_and_keywords", |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5341 | (PyCFunction)(void(*)(void))getargs_positional_only_and_keywords, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 5342 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5343 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 5344 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 5345 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 5346 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 5347 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 5348 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 5349 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 5350 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 5351 | {"getargs_n", getargs_n, METH_VARARGS}, |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 5352 | {"getargs_p", getargs_p, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5353 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 5354 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 5355 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5356 | {"test_long_long_and_overflow",test_long_long_and_overflow, METH_NOARGS}, |
| 5357 | {"test_L_code", test_L_code, METH_NOARGS}, |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 5358 | {"getargs_f", getargs_f, METH_VARARGS}, |
| 5359 | {"getargs_d", getargs_d, METH_VARARGS}, |
| 5360 | {"getargs_D", getargs_D, METH_VARARGS}, |
| 5361 | {"getargs_S", getargs_S, METH_VARARGS}, |
| 5362 | {"getargs_Y", getargs_Y, METH_VARARGS}, |
| 5363 | {"getargs_U", getargs_U, METH_VARARGS}, |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 5364 | {"getargs_c", getargs_c, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 5365 | {"getargs_C", getargs_C, METH_VARARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 5366 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 5367 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 5368 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 5369 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 5370 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 5371 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 5372 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 5373 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 5374 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 5375 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 5376 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 5377 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 5378 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 5379 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 5380 | {"getargs_es", getargs_es, METH_VARARGS}, |
| 5381 | {"getargs_et", getargs_et, METH_VARARGS}, |
| 5382 | {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 5383 | {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5384 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5385 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5386 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5387 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5388 | {"test_s_code", test_s_code, METH_NOARGS}, |
| 5389 | {"test_u_code", test_u_code, METH_NOARGS}, |
| 5390 | {"test_Z_code", test_Z_code, METH_NOARGS}, |
| 5391 | {"test_widechar", test_widechar, METH_NOARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 5392 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 5393 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 5394 | {"unicode_asucs4", unicode_asucs4, METH_VARARGS}, |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 5395 | {"unicode_asutf8", unicode_asutf8, METH_VARARGS}, |
| 5396 | {"unicode_asutf8andsize", unicode_asutf8andsize, METH_VARARGS}, |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 5397 | {"unicode_findchar", unicode_findchar, METH_VARARGS}, |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 5398 | {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 5399 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 5400 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 5401 | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5402 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5403 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5404 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5405 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5406 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5407 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 5408 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 5409 | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5410 | {"argparsing", argparsing, METH_VARARGS}, |
| 5411 | {"code_newempty", code_newempty, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5412 | {"make_exception_with_doc", (PyCFunction)(void(*)(void))make_exception_with_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5413 | METH_VARARGS | METH_KEYWORDS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5414 | {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer, |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 5415 | METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5416 | {"crash_no_current_thread", crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 5417 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5418 | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
| 5419 | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 5420 | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 5421 | {"with_tp_del", with_tp_del, METH_VARARGS}, |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 5422 | {"create_cfunction", create_cfunction, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5423 | {"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS}, |
| 5424 | {"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS}, |
| 5425 | {"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS}, |
| 5426 | {"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS}, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 5427 | {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS, |
| 5428 | PyDoc_STR("set_nomemory(start:int, stop:int = 0)")}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5429 | {"remove_mem_hooks", remove_mem_hooks, METH_NOARGS, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 5430 | PyDoc_STR("Remove memory hooks.")}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5431 | {"no_docstring", |
| 5432 | (PyCFunction)test_with_docstring, METH_NOARGS}, |
| 5433 | {"docstring_empty", |
| 5434 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5435 | docstring_empty}, |
| 5436 | {"docstring_no_signature", |
| 5437 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5438 | docstring_no_signature}, |
| 5439 | {"docstring_with_invalid_signature", |
| 5440 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5441 | docstring_with_invalid_signature}, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 5442 | {"docstring_with_invalid_signature2", |
| 5443 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5444 | docstring_with_invalid_signature2}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5445 | {"docstring_with_signature", |
| 5446 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5447 | docstring_with_signature}, |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 5448 | {"docstring_with_signature_but_no_doc", |
| 5449 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5450 | docstring_with_signature_but_no_doc}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5451 | {"docstring_with_signature_and_extra_newlines", |
| 5452 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5453 | docstring_with_signature_and_extra_newlines}, |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 5454 | {"docstring_with_signature_with_defaults", |
| 5455 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5456 | docstring_with_signature_with_defaults}, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5457 | {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
| 5458 | PyDoc_STR("set_error_class(error_class) -> None")}, |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 5459 | {"pymarshal_write_long_to_file", |
| 5460 | pymarshal_write_long_to_file, METH_VARARGS}, |
| 5461 | {"pymarshal_write_object_to_file", |
| 5462 | pymarshal_write_object_to_file, METH_VARARGS}, |
| 5463 | {"pymarshal_read_short_from_file", |
| 5464 | pymarshal_read_short_from_file, METH_VARARGS}, |
| 5465 | {"pymarshal_read_long_from_file", |
| 5466 | pymarshal_read_long_from_file, METH_VARARGS}, |
| 5467 | {"pymarshal_read_last_object_from_file", |
| 5468 | pymarshal_read_last_object_from_file, METH_VARARGS}, |
| 5469 | {"pymarshal_read_object_from_file", |
| 5470 | pymarshal_read_object_from_file, METH_VARARGS}, |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 5471 | {"return_null_without_error", |
| 5472 | return_null_without_error, METH_NOARGS}, |
| 5473 | {"return_result_with_error", |
| 5474 | return_result_with_error, METH_NOARGS}, |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 5475 | {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS}, |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 5476 | {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS}, |
| 5477 | {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 5478 | {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 5479 | #ifdef HAVE_CLOCK_GETTIME |
| 5480 | {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, |
| 5481 | #endif |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 5482 | {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS}, |
| 5483 | {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS}, |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 5484 | {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS}, |
| 5485 | {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 5486 | {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 5487 | {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS}, |
Victor Stinner | 6876257 | 2019-10-07 18:42:01 +0200 | [diff] [blame] | 5488 | {"check_pyobject_null_is_freed", check_pyobject_null_is_freed, METH_NOARGS}, |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 5489 | {"check_pyobject_uninitialized_is_freed", check_pyobject_uninitialized_is_freed, METH_NOARGS}, |
| 5490 | {"check_pyobject_forbidden_bytes_is_freed", check_pyobject_forbidden_bytes_is_freed, METH_NOARGS}, |
| 5491 | {"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS}, |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 5492 | {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 5493 | {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, |
| 5494 | {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, |
| 5495 | {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 5496 | {"dict_get_version", dict_get_version, METH_VARARGS}, |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 5497 | {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS}, |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 5498 | {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS}, |
| 5499 | {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS}, |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5500 | {"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS}, |
| 5501 | {"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS}, |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 5502 | {"stack_pointer", stack_pointer, METH_NOARGS}, |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 5503 | #ifdef W_STOPCODE |
| 5504 | {"W_STOPCODE", py_w_stopcode, METH_VARARGS}, |
| 5505 | #endif |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 5506 | {"get_mapping_keys", get_mapping_keys, METH_O}, |
| 5507 | {"get_mapping_values", get_mapping_values, METH_O}, |
| 5508 | {"get_mapping_items", get_mapping_items, METH_O}, |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 5509 | {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS}, |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 5510 | {"hamt", new_hamt, METH_NOARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5511 | {"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL}, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 5512 | {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS}, |
| 5513 | {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS}, |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5514 | #ifdef Py_REF_DEBUG |
| 5515 | {"negative_refcount", negative_refcount, METH_NOARGS}, |
| 5516 | #endif |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5517 | {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS}, |
Sergey Fedoseev | 92709a2 | 2019-09-09 21:28:34 +0500 | [diff] [blame] | 5518 | {"sequence_getitem", sequence_getitem, METH_VARARGS}, |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5519 | {"meth_varargs", meth_varargs, METH_VARARGS}, |
| 5520 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, |
| 5521 | {"meth_o", meth_o, METH_O}, |
| 5522 | {"meth_noargs", meth_noargs, METH_NOARGS}, |
| 5523 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, |
| 5524 | {"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] | 5525 | {"pynumber_tobase", pynumber_tobase, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5526 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 5527 | }; |
| 5528 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 5529 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 5530 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5531 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5532 | char bool_member; |
| 5533 | char byte_member; |
| 5534 | unsigned char ubyte_member; |
| 5535 | short short_member; |
| 5536 | unsigned short ushort_member; |
| 5537 | int int_member; |
| 5538 | unsigned int uint_member; |
| 5539 | long long_member; |
| 5540 | unsigned long ulong_member; |
| 5541 | Py_ssize_t pyssizet_member; |
| 5542 | float float_member; |
| 5543 | double double_member; |
| 5544 | char inplace_member[6]; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5545 | long long longlong_member; |
| 5546 | unsigned long long ulonglong_member; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5547 | } all_structmembers; |
| 5548 | |
| 5549 | typedef struct { |
| 5550 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5551 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5552 | } test_structmembers; |
| 5553 | |
| 5554 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 5556 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 5557 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 5558 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 5559 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 5560 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 5561 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 5562 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 5563 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 5564 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 5565 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 5566 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 5567 | {"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] | 5568 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 5569 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5570 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5571 | }; |
| 5572 | |
| 5573 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 5574 | static PyObject * |
| 5575 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5576 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5577 | static char *keywords[] = { |
| 5578 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 5579 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 5580 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5581 | "T_LONGLONG", "T_ULONGLONG", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5582 | NULL}; |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 5583 | static const char fmt[] = "|bbBhHiIlknfds#LK"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5584 | test_structmembers *ob; |
| 5585 | const char *s = NULL; |
| 5586 | Py_ssize_t string_len = 0; |
| 5587 | ob = PyObject_New(test_structmembers, type); |
| 5588 | if (ob == NULL) |
| 5589 | return NULL; |
| 5590 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 5591 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 5592 | &ob->structmembers.bool_member, |
| 5593 | &ob->structmembers.byte_member, |
| 5594 | &ob->structmembers.ubyte_member, |
| 5595 | &ob->structmembers.short_member, |
| 5596 | &ob->structmembers.ushort_member, |
| 5597 | &ob->structmembers.int_member, |
| 5598 | &ob->structmembers.uint_member, |
| 5599 | &ob->structmembers.long_member, |
| 5600 | &ob->structmembers.ulong_member, |
| 5601 | &ob->structmembers.pyssizet_member, |
| 5602 | &ob->structmembers.float_member, |
| 5603 | &ob->structmembers.double_member, |
| 5604 | &s, &string_len |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5605 | , &ob->structmembers.longlong_member, |
| 5606 | &ob->structmembers.ulonglong_member |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5607 | )) { |
| 5608 | Py_DECREF(ob); |
| 5609 | return NULL; |
| 5610 | } |
| 5611 | if (s != NULL) { |
| 5612 | if (string_len > 5) { |
| 5613 | Py_DECREF(ob); |
| 5614 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 5615 | return NULL; |
| 5616 | } |
| 5617 | strcpy(ob->structmembers.inplace_member, s); |
| 5618 | } |
| 5619 | else { |
| 5620 | strcpy(ob->structmembers.inplace_member, ""); |
| 5621 | } |
| 5622 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5623 | } |
| 5624 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 5625 | static void |
| 5626 | test_structmembers_free(PyObject *ob) |
| 5627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5628 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 5632 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5633 | "test_structmembersType", |
| 5634 | sizeof(test_structmembers), /* tp_basicsize */ |
| 5635 | 0, /* tp_itemsize */ |
| 5636 | test_structmembers_free, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5637 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5638 | 0, /* tp_getattr */ |
| 5639 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5640 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5641 | 0, /* tp_repr */ |
| 5642 | 0, /* tp_as_number */ |
| 5643 | 0, /* tp_as_sequence */ |
| 5644 | 0, /* tp_as_mapping */ |
| 5645 | 0, /* tp_hash */ |
| 5646 | 0, /* tp_call */ |
| 5647 | 0, /* tp_str */ |
| 5648 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5649 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5650 | 0, /* tp_as_buffer */ |
| 5651 | 0, /* tp_flags */ |
| 5652 | "Type containing all structmember types", |
| 5653 | 0, /* traverseproc tp_traverse */ |
| 5654 | 0, /* tp_clear */ |
| 5655 | 0, /* tp_richcompare */ |
| 5656 | 0, /* tp_weaklistoffset */ |
| 5657 | 0, /* tp_iter */ |
| 5658 | 0, /* tp_iternext */ |
| 5659 | 0, /* tp_methods */ |
| 5660 | test_members, /* tp_members */ |
| 5661 | 0, |
| 5662 | 0, |
| 5663 | 0, |
| 5664 | 0, |
| 5665 | 0, |
| 5666 | 0, |
| 5667 | 0, |
| 5668 | 0, |
| 5669 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5670 | }; |
| 5671 | |
| 5672 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5673 | typedef struct { |
| 5674 | PyObject_HEAD |
| 5675 | } matmulObject; |
| 5676 | |
| 5677 | static PyObject * |
| 5678 | matmulType_matmul(PyObject *self, PyObject *other) |
| 5679 | { |
| 5680 | return Py_BuildValue("(sOO)", "matmul", self, other); |
| 5681 | } |
| 5682 | |
| 5683 | static PyObject * |
| 5684 | matmulType_imatmul(PyObject *self, PyObject *other) |
| 5685 | { |
| 5686 | return Py_BuildValue("(sOO)", "imatmul", self, other); |
| 5687 | } |
| 5688 | |
| 5689 | static void |
| 5690 | matmulType_dealloc(PyObject *self) |
| 5691 | { |
Zachary Ware | 420dc56 | 2014-04-23 13:51:27 -0500 | [diff] [blame] | 5692 | Py_TYPE(self)->tp_free(self); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | static PyNumberMethods matmulType_as_number = { |
| 5696 | 0, /* nb_add */ |
| 5697 | 0, /* nb_subtract */ |
| 5698 | 0, /* nb_multiply */ |
| 5699 | 0, /* nb_remainde r*/ |
| 5700 | 0, /* nb_divmod */ |
| 5701 | 0, /* nb_power */ |
| 5702 | 0, /* nb_negative */ |
| 5703 | 0, /* tp_positive */ |
| 5704 | 0, /* tp_absolute */ |
| 5705 | 0, /* tp_bool */ |
| 5706 | 0, /* nb_invert */ |
| 5707 | 0, /* nb_lshift */ |
| 5708 | 0, /* nb_rshift */ |
| 5709 | 0, /* nb_and */ |
| 5710 | 0, /* nb_xor */ |
| 5711 | 0, /* nb_or */ |
| 5712 | 0, /* nb_int */ |
| 5713 | 0, /* nb_reserved */ |
| 5714 | 0, /* nb_float */ |
| 5715 | 0, /* nb_inplace_add */ |
| 5716 | 0, /* nb_inplace_subtract */ |
| 5717 | 0, /* nb_inplace_multiply */ |
| 5718 | 0, /* nb_inplace_remainder */ |
| 5719 | 0, /* nb_inplace_power */ |
| 5720 | 0, /* nb_inplace_lshift */ |
| 5721 | 0, /* nb_inplace_rshift */ |
| 5722 | 0, /* nb_inplace_and */ |
| 5723 | 0, /* nb_inplace_xor */ |
| 5724 | 0, /* nb_inplace_or */ |
| 5725 | 0, /* nb_floor_divide */ |
| 5726 | 0, /* nb_true_divide */ |
| 5727 | 0, /* nb_inplace_floor_divide */ |
| 5728 | 0, /* nb_inplace_true_divide */ |
| 5729 | 0, /* nb_index */ |
| 5730 | matmulType_matmul, /* nb_matrix_multiply */ |
| 5731 | matmulType_imatmul /* nb_matrix_inplace_multiply */ |
| 5732 | }; |
| 5733 | |
| 5734 | static PyTypeObject matmulType = { |
| 5735 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5736 | "matmulType", |
| 5737 | sizeof(matmulObject), /* tp_basicsize */ |
| 5738 | 0, /* tp_itemsize */ |
| 5739 | matmulType_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5740 | 0, /* tp_vectorcall_offset */ |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5741 | 0, /* tp_getattr */ |
| 5742 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5743 | 0, /* tp_as_async */ |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5744 | 0, /* tp_repr */ |
| 5745 | &matmulType_as_number, /* tp_as_number */ |
| 5746 | 0, /* tp_as_sequence */ |
| 5747 | 0, /* tp_as_mapping */ |
| 5748 | 0, /* tp_hash */ |
| 5749 | 0, /* tp_call */ |
| 5750 | 0, /* tp_str */ |
| 5751 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5752 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5753 | 0, /* tp_as_buffer */ |
| 5754 | 0, /* tp_flags */ |
| 5755 | "C level type with matrix operations defined", |
| 5756 | 0, /* traverseproc tp_traverse */ |
| 5757 | 0, /* tp_clear */ |
| 5758 | 0, /* tp_richcompare */ |
| 5759 | 0, /* tp_weaklistoffset */ |
| 5760 | 0, /* tp_iter */ |
| 5761 | 0, /* tp_iternext */ |
| 5762 | 0, /* tp_methods */ |
| 5763 | 0, /* tp_members */ |
| 5764 | 0, |
| 5765 | 0, |
| 5766 | 0, |
| 5767 | 0, |
| 5768 | 0, |
| 5769 | 0, |
| 5770 | 0, |
| 5771 | 0, |
| 5772 | PyType_GenericNew, /* tp_new */ |
| 5773 | PyObject_Del, /* tp_free */ |
| 5774 | }; |
| 5775 | |
Zackery Spytz | c7f803b | 2019-05-31 03:46:36 -0600 | [diff] [blame] | 5776 | typedef struct { |
| 5777 | PyObject_HEAD |
| 5778 | } ipowObject; |
| 5779 | |
| 5780 | static PyObject * |
| 5781 | ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod) |
| 5782 | { |
| 5783 | return Py_BuildValue("OO", other, mod); |
| 5784 | } |
| 5785 | |
| 5786 | static PyNumberMethods ipowType_as_number = { |
| 5787 | .nb_inplace_power = ipowType_ipow |
| 5788 | }; |
| 5789 | |
| 5790 | static PyTypeObject ipowType = { |
| 5791 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5792 | .tp_name = "ipowType", |
| 5793 | .tp_basicsize = sizeof(ipowObject), |
| 5794 | .tp_as_number = &ipowType_as_number, |
| 5795 | .tp_new = PyType_GenericNew |
| 5796 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5797 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5798 | typedef struct { |
| 5799 | PyObject_HEAD |
| 5800 | PyObject *ao_iterator; |
| 5801 | } awaitObject; |
| 5802 | |
| 5803 | |
| 5804 | static PyObject * |
| 5805 | awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5806 | { |
| 5807 | PyObject *v; |
| 5808 | awaitObject *ao; |
| 5809 | |
| 5810 | if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v)) |
| 5811 | return NULL; |
| 5812 | |
| 5813 | ao = (awaitObject *)type->tp_alloc(type, 0); |
| 5814 | if (ao == NULL) { |
| 5815 | return NULL; |
| 5816 | } |
| 5817 | |
| 5818 | Py_INCREF(v); |
| 5819 | ao->ao_iterator = v; |
| 5820 | |
| 5821 | return (PyObject *)ao; |
| 5822 | } |
| 5823 | |
| 5824 | |
| 5825 | static void |
| 5826 | awaitObject_dealloc(awaitObject *ao) |
| 5827 | { |
| 5828 | Py_CLEAR(ao->ao_iterator); |
| 5829 | Py_TYPE(ao)->tp_free(ao); |
| 5830 | } |
| 5831 | |
| 5832 | |
| 5833 | static PyObject * |
| 5834 | awaitObject_await(awaitObject *ao) |
| 5835 | { |
| 5836 | Py_INCREF(ao->ao_iterator); |
| 5837 | return ao->ao_iterator; |
| 5838 | } |
| 5839 | |
| 5840 | static PyAsyncMethods awaitType_as_async = { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 5841 | (unaryfunc)awaitObject_await, /* am_await */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5842 | 0, /* am_aiter */ |
| 5843 | 0 /* am_anext */ |
| 5844 | }; |
| 5845 | |
| 5846 | |
| 5847 | static PyTypeObject awaitType = { |
| 5848 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5849 | "awaitType", |
| 5850 | sizeof(awaitObject), /* tp_basicsize */ |
| 5851 | 0, /* tp_itemsize */ |
| 5852 | (destructor)awaitObject_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5853 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5854 | 0, /* tp_getattr */ |
| 5855 | 0, /* tp_setattr */ |
| 5856 | &awaitType_as_async, /* tp_as_async */ |
| 5857 | 0, /* tp_repr */ |
| 5858 | 0, /* tp_as_number */ |
| 5859 | 0, /* tp_as_sequence */ |
| 5860 | 0, /* tp_as_mapping */ |
| 5861 | 0, /* tp_hash */ |
| 5862 | 0, /* tp_call */ |
| 5863 | 0, /* tp_str */ |
| 5864 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5865 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5866 | 0, /* tp_as_buffer */ |
| 5867 | 0, /* tp_flags */ |
| 5868 | "C level type with tp_as_async", |
| 5869 | 0, /* traverseproc tp_traverse */ |
| 5870 | 0, /* tp_clear */ |
| 5871 | 0, /* tp_richcompare */ |
| 5872 | 0, /* tp_weaklistoffset */ |
| 5873 | 0, /* tp_iter */ |
| 5874 | 0, /* tp_iternext */ |
| 5875 | 0, /* tp_methods */ |
| 5876 | 0, /* tp_members */ |
| 5877 | 0, |
| 5878 | 0, |
| 5879 | 0, |
| 5880 | 0, |
| 5881 | 0, |
| 5882 | 0, |
| 5883 | 0, |
| 5884 | 0, |
| 5885 | awaitObject_new, /* tp_new */ |
| 5886 | PyObject_Del, /* tp_free */ |
| 5887 | }; |
| 5888 | |
| 5889 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5890 | static int recurse_infinitely_error_init(PyObject *, PyObject *, PyObject *); |
| 5891 | |
| 5892 | static PyTypeObject PyRecursingInfinitelyError_Type = { |
| 5893 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5894 | "RecursingInfinitelyError", /* tp_name */ |
| 5895 | sizeof(PyBaseExceptionObject), /* tp_basicsize */ |
| 5896 | 0, /* tp_itemsize */ |
| 5897 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5898 | 0, /* tp_vectorcall_offset */ |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5899 | 0, /* tp_getattr */ |
| 5900 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5901 | 0, /* tp_as_async */ |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5902 | 0, /* tp_repr */ |
| 5903 | 0, /* tp_as_number */ |
| 5904 | 0, /* tp_as_sequence */ |
| 5905 | 0, /* tp_as_mapping */ |
| 5906 | 0, /* tp_hash */ |
| 5907 | 0, /* tp_call */ |
| 5908 | 0, /* tp_str */ |
| 5909 | 0, /* tp_getattro */ |
| 5910 | 0, /* tp_setattro */ |
| 5911 | 0, /* tp_as_buffer */ |
| 5912 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5913 | "Instantiating this exception starts infinite recursion.", /* tp_doc */ |
| 5914 | 0, /* tp_traverse */ |
| 5915 | 0, /* tp_clear */ |
| 5916 | 0, /* tp_richcompare */ |
| 5917 | 0, /* tp_weaklistoffset */ |
| 5918 | 0, /* tp_iter */ |
| 5919 | 0, /* tp_iternext */ |
| 5920 | 0, /* tp_methods */ |
| 5921 | 0, /* tp_members */ |
| 5922 | 0, /* tp_getset */ |
| 5923 | 0, /* tp_base */ |
| 5924 | 0, /* tp_dict */ |
| 5925 | 0, /* tp_descr_get */ |
| 5926 | 0, /* tp_descr_set */ |
| 5927 | 0, /* tp_dictoffset */ |
| 5928 | (initproc)recurse_infinitely_error_init, /* tp_init */ |
| 5929 | 0, /* tp_alloc */ |
| 5930 | 0, /* tp_new */ |
| 5931 | }; |
| 5932 | |
| 5933 | static int |
| 5934 | recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5935 | { |
| 5936 | PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type; |
| 5937 | |
| 5938 | /* Instantiating this exception starts infinite recursion. */ |
| 5939 | Py_INCREF(type); |
| 5940 | PyErr_SetObject(type, NULL); |
| 5941 | return -1; |
| 5942 | } |
| 5943 | |
| 5944 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5945 | /* Test bpo-35983: create a subclass of "list" which checks that instances |
| 5946 | * are not deallocated twice */ |
| 5947 | |
| 5948 | typedef struct { |
| 5949 | PyListObject list; |
| 5950 | int deallocated; |
| 5951 | } MyListObject; |
| 5952 | |
| 5953 | static PyObject * |
| 5954 | MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5955 | { |
| 5956 | PyObject* op = PyList_Type.tp_new(type, args, kwds); |
| 5957 | ((MyListObject*)op)->deallocated = 0; |
| 5958 | return op; |
| 5959 | } |
| 5960 | |
| 5961 | void |
| 5962 | MyList_dealloc(MyListObject* op) |
| 5963 | { |
| 5964 | if (op->deallocated) { |
| 5965 | /* We cannot raise exceptions here but we still want the testsuite |
| 5966 | * to fail when we hit this */ |
| 5967 | Py_FatalError("MyList instance deallocated twice"); |
| 5968 | } |
| 5969 | op->deallocated = 1; |
| 5970 | PyList_Type.tp_dealloc((PyObject *)op); |
| 5971 | } |
| 5972 | |
| 5973 | static PyTypeObject MyList_Type = { |
| 5974 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5975 | "MyList", |
| 5976 | sizeof(MyListObject), |
| 5977 | 0, |
| 5978 | (destructor)MyList_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5979 | 0, /* tp_vectorcall_offset */ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5980 | 0, /* tp_getattr */ |
| 5981 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5982 | 0, /* tp_as_async */ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5983 | 0, /* tp_repr */ |
| 5984 | 0, /* tp_as_number */ |
| 5985 | 0, /* tp_as_sequence */ |
| 5986 | 0, /* tp_as_mapping */ |
| 5987 | 0, /* tp_hash */ |
| 5988 | 0, /* tp_call */ |
| 5989 | 0, /* tp_str */ |
| 5990 | 0, /* tp_getattro */ |
| 5991 | 0, /* tp_setattro */ |
| 5992 | 0, /* tp_as_buffer */ |
| 5993 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5994 | 0, /* tp_doc */ |
| 5995 | 0, /* tp_traverse */ |
| 5996 | 0, /* tp_clear */ |
| 5997 | 0, /* tp_richcompare */ |
| 5998 | 0, /* tp_weaklistoffset */ |
| 5999 | 0, /* tp_iter */ |
| 6000 | 0, /* tp_iternext */ |
| 6001 | 0, /* tp_methods */ |
| 6002 | 0, /* tp_members */ |
| 6003 | 0, /* tp_getset */ |
| 6004 | 0, /* &PyList_Type */ /* tp_base */ |
| 6005 | 0, /* tp_dict */ |
| 6006 | 0, /* tp_descr_get */ |
| 6007 | 0, /* tp_descr_set */ |
| 6008 | 0, /* tp_dictoffset */ |
| 6009 | 0, /* tp_init */ |
| 6010 | 0, /* tp_alloc */ |
| 6011 | MyList_new, /* tp_new */ |
| 6012 | }; |
| 6013 | |
| 6014 | |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6015 | /* Test PEP 560 */ |
| 6016 | |
| 6017 | typedef struct { |
| 6018 | PyObject_HEAD |
| 6019 | PyObject *item; |
| 6020 | } PyGenericAliasObject; |
| 6021 | |
| 6022 | static void |
| 6023 | generic_alias_dealloc(PyGenericAliasObject *self) |
| 6024 | { |
| 6025 | Py_CLEAR(self->item); |
Victor Stinner | e860089 | 2018-01-17 23:08:18 +0100 | [diff] [blame] | 6026 | Py_TYPE(self)->tp_free((PyObject *)self); |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | static PyObject * |
| 6030 | generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases) |
| 6031 | { |
| 6032 | return PyTuple_Pack(1, self->item); |
| 6033 | } |
| 6034 | |
| 6035 | static PyMethodDef generic_alias_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6036 | {"__mro_entries__", (PyCFunction)(void(*)(void))generic_alias_mro_entries, METH_O, NULL}, |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6037 | {NULL} /* sentinel */ |
| 6038 | }; |
| 6039 | |
Benjamin Peterson | 97ae32c | 2018-07-03 22:39:09 -0700 | [diff] [blame] | 6040 | static PyTypeObject GenericAlias_Type = { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6041 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6042 | "GenericAlias", |
| 6043 | sizeof(PyGenericAliasObject), |
| 6044 | 0, |
| 6045 | .tp_dealloc = (destructor)generic_alias_dealloc, |
| 6046 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6047 | .tp_methods = generic_alias_methods, |
| 6048 | }; |
| 6049 | |
| 6050 | static PyObject * |
| 6051 | generic_alias_new(PyObject *item) |
| 6052 | { |
| 6053 | PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type); |
| 6054 | if (o == NULL) { |
| 6055 | return NULL; |
| 6056 | } |
| 6057 | Py_INCREF(item); |
| 6058 | o->item = item; |
| 6059 | return (PyObject*) o; |
| 6060 | } |
| 6061 | |
| 6062 | typedef struct { |
| 6063 | PyObject_HEAD |
| 6064 | } PyGenericObject; |
| 6065 | |
| 6066 | static PyObject * |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 6067 | generic_class_getitem(PyObject *type, PyObject *item) |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6068 | { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6069 | return generic_alias_new(item); |
| 6070 | } |
| 6071 | |
| 6072 | static PyMethodDef generic_methods[] = { |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 6073 | {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL}, |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6074 | {NULL} /* sentinel */ |
| 6075 | }; |
| 6076 | |
Benjamin Peterson | 97ae32c | 2018-07-03 22:39:09 -0700 | [diff] [blame] | 6077 | static PyTypeObject Generic_Type = { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6078 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6079 | "Generic", |
| 6080 | sizeof(PyGenericObject), |
| 6081 | 0, |
| 6082 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6083 | .tp_methods = generic_methods, |
| 6084 | }; |
| 6085 | |
| 6086 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6087 | /* Test PEP 590 */ |
| 6088 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6089 | typedef struct { |
| 6090 | PyObject_HEAD |
| 6091 | vectorcallfunc vectorcall; |
| 6092 | } MethodDescriptorObject; |
| 6093 | |
| 6094 | static PyObject * |
| 6095 | MethodDescriptor_vectorcall(PyObject *callable, PyObject *const *args, |
| 6096 | size_t nargsf, PyObject *kwnames) |
| 6097 | { |
| 6098 | /* True if using the vectorcall function in MethodDescriptorObject |
| 6099 | * but False for MethodDescriptor2Object */ |
| 6100 | MethodDescriptorObject *md = (MethodDescriptorObject *)callable; |
| 6101 | return PyBool_FromLong(md->vectorcall != NULL); |
| 6102 | } |
| 6103 | |
| 6104 | static PyObject * |
| 6105 | MethodDescriptor_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 6106 | { |
Petr Viktorin | e584cbf | 2019-06-03 01:08:14 +0200 | [diff] [blame] | 6107 | MethodDescriptorObject *op = (MethodDescriptorObject *)type->tp_alloc(type, 0); |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6108 | op->vectorcall = MethodDescriptor_vectorcall; |
| 6109 | return (PyObject *)op; |
| 6110 | } |
| 6111 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6112 | static PyObject * |
| 6113 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 6114 | { |
| 6115 | if (obj == Py_None || obj == NULL) { |
| 6116 | Py_INCREF(func); |
| 6117 | return func; |
| 6118 | } |
| 6119 | return PyMethod_New(func, obj); |
| 6120 | } |
| 6121 | |
| 6122 | static PyObject * |
| 6123 | nop_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 6124 | { |
| 6125 | Py_INCREF(func); |
| 6126 | return func; |
| 6127 | } |
| 6128 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6129 | static PyObject * |
| 6130 | call_return_args(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6131 | { |
| 6132 | Py_INCREF(args); |
| 6133 | return args; |
| 6134 | } |
| 6135 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6136 | static PyTypeObject MethodDescriptorBase_Type = { |
| 6137 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6138 | "MethodDescriptorBase", |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6139 | sizeof(MethodDescriptorObject), |
| 6140 | .tp_new = MethodDescriptor_new, |
| 6141 | .tp_call = PyVectorcall_Call, |
| 6142 | .tp_vectorcall_offset = offsetof(MethodDescriptorObject, vectorcall), |
| 6143 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 6144 | Py_TPFLAGS_METHOD_DESCRIPTOR | Py_TPFLAGS_HAVE_VECTORCALL, |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6145 | .tp_descr_get = func_descr_get, |
| 6146 | }; |
| 6147 | |
| 6148 | static PyTypeObject MethodDescriptorDerived_Type = { |
| 6149 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6150 | "MethodDescriptorDerived", |
| 6151 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6152 | }; |
| 6153 | |
| 6154 | static PyTypeObject MethodDescriptorNopGet_Type = { |
| 6155 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6156 | "MethodDescriptorNopGet", |
| 6157 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6158 | .tp_call = call_return_args, |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6159 | .tp_descr_get = nop_descr_get, |
| 6160 | }; |
| 6161 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6162 | typedef struct { |
| 6163 | MethodDescriptorObject base; |
| 6164 | vectorcallfunc vectorcall; |
| 6165 | } MethodDescriptor2Object; |
| 6166 | |
| 6167 | static PyObject * |
| 6168 | MethodDescriptor2_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 6169 | { |
| 6170 | MethodDescriptor2Object *op = PyObject_New(MethodDescriptor2Object, type); |
| 6171 | op->base.vectorcall = NULL; |
| 6172 | op->vectorcall = MethodDescriptor_vectorcall; |
| 6173 | return (PyObject *)op; |
| 6174 | } |
| 6175 | |
| 6176 | static PyTypeObject MethodDescriptor2_Type = { |
| 6177 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6178 | "MethodDescriptor2", |
| 6179 | sizeof(MethodDescriptor2Object), |
| 6180 | .tp_new = MethodDescriptor2_new, |
| 6181 | .tp_call = PyVectorcall_Call, |
| 6182 | .tp_vectorcall_offset = offsetof(MethodDescriptor2Object, vectorcall), |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 6183 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_VECTORCALL, |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6184 | }; |
| 6185 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6186 | PyDoc_STRVAR(heapgctype__doc__, |
| 6187 | "A heap type with GC, and with overridden dealloc.\n\n" |
| 6188 | "The 'value' attribute is set to 10 in __init__."); |
| 6189 | |
| 6190 | typedef struct { |
| 6191 | PyObject_HEAD |
| 6192 | int value; |
| 6193 | } HeapCTypeObject; |
| 6194 | |
| 6195 | static struct PyMemberDef heapctype_members[] = { |
| 6196 | {"value", T_INT, offsetof(HeapCTypeObject, value)}, |
| 6197 | {NULL} /* Sentinel */ |
| 6198 | }; |
| 6199 | |
| 6200 | static int |
| 6201 | heapctype_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6202 | { |
| 6203 | ((HeapCTypeObject *)self)->value = 10; |
| 6204 | return 0; |
| 6205 | } |
| 6206 | |
| 6207 | static void |
| 6208 | heapgcctype_dealloc(HeapCTypeObject *self) |
| 6209 | { |
| 6210 | PyTypeObject *tp = Py_TYPE(self); |
| 6211 | PyObject_GC_UnTrack(self); |
| 6212 | PyObject_GC_Del(self); |
| 6213 | Py_DECREF(tp); |
| 6214 | } |
| 6215 | |
| 6216 | static PyType_Slot HeapGcCType_slots[] = { |
| 6217 | {Py_tp_init, heapctype_init}, |
| 6218 | {Py_tp_members, heapctype_members}, |
| 6219 | {Py_tp_dealloc, heapgcctype_dealloc}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6220 | {Py_tp_doc, (char*)heapgctype__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6221 | {0, 0}, |
| 6222 | }; |
| 6223 | |
| 6224 | static PyType_Spec HeapGcCType_spec = { |
| 6225 | "_testcapi.HeapGcCType", |
| 6226 | sizeof(HeapCTypeObject), |
| 6227 | 0, |
| 6228 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6229 | HeapGcCType_slots |
| 6230 | }; |
| 6231 | |
| 6232 | PyDoc_STRVAR(heapctype__doc__, |
| 6233 | "A heap type without GC, but with overridden dealloc.\n\n" |
| 6234 | "The 'value' attribute is set to 10 in __init__."); |
| 6235 | |
| 6236 | static void |
| 6237 | heapctype_dealloc(HeapCTypeObject *self) |
| 6238 | { |
| 6239 | PyTypeObject *tp = Py_TYPE(self); |
| 6240 | PyObject_Del(self); |
| 6241 | Py_DECREF(tp); |
| 6242 | } |
| 6243 | |
| 6244 | static PyType_Slot HeapCType_slots[] = { |
| 6245 | {Py_tp_init, heapctype_init}, |
| 6246 | {Py_tp_members, heapctype_members}, |
| 6247 | {Py_tp_dealloc, heapctype_dealloc}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6248 | {Py_tp_doc, (char*)heapctype__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6249 | {0, 0}, |
| 6250 | }; |
| 6251 | |
| 6252 | static PyType_Spec HeapCType_spec = { |
| 6253 | "_testcapi.HeapCType", |
| 6254 | sizeof(HeapCTypeObject), |
| 6255 | 0, |
| 6256 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6257 | HeapCType_slots |
| 6258 | }; |
| 6259 | |
| 6260 | PyDoc_STRVAR(heapctypesubclass__doc__, |
| 6261 | "Subclass of HeapCType, without GC.\n\n" |
| 6262 | "__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 6263 | |
| 6264 | typedef struct { |
| 6265 | HeapCTypeObject base; |
| 6266 | int value2; |
| 6267 | } HeapCTypeSubclassObject; |
| 6268 | |
| 6269 | static int |
| 6270 | heapctypesubclass_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6271 | { |
| 6272 | /* Call __init__ of the superclass */ |
| 6273 | if (heapctype_init(self, args, kwargs) < 0) { |
| 6274 | return -1; |
| 6275 | } |
| 6276 | /* Initialize additional element */ |
| 6277 | ((HeapCTypeSubclassObject *)self)->value2 = 20; |
| 6278 | return 0; |
| 6279 | } |
| 6280 | |
| 6281 | static struct PyMemberDef heapctypesubclass_members[] = { |
| 6282 | {"value2", T_INT, offsetof(HeapCTypeSubclassObject, value2)}, |
| 6283 | {NULL} /* Sentinel */ |
| 6284 | }; |
| 6285 | |
| 6286 | static PyType_Slot HeapCTypeSubclass_slots[] = { |
| 6287 | {Py_tp_init, heapctypesubclass_init}, |
| 6288 | {Py_tp_members, heapctypesubclass_members}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6289 | {Py_tp_doc, (char*)heapctypesubclass__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6290 | {0, 0}, |
| 6291 | }; |
| 6292 | |
| 6293 | static PyType_Spec HeapCTypeSubclass_spec = { |
| 6294 | "_testcapi.HeapCTypeSubclass", |
| 6295 | sizeof(HeapCTypeSubclassObject), |
| 6296 | 0, |
| 6297 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6298 | HeapCTypeSubclass_slots |
| 6299 | }; |
| 6300 | |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 6301 | PyDoc_STRVAR(heapctypewithbuffer__doc__, |
| 6302 | "Heap type with buffer support.\n\n" |
| 6303 | "The buffer is set to [b'1', b'2', b'3', b'4']"); |
| 6304 | |
| 6305 | typedef struct { |
| 6306 | HeapCTypeObject base; |
| 6307 | char buffer[4]; |
| 6308 | } HeapCTypeWithBufferObject; |
| 6309 | |
| 6310 | static int |
| 6311 | heapctypewithbuffer_getbuffer(HeapCTypeWithBufferObject *self, Py_buffer *view, int flags) |
| 6312 | { |
| 6313 | self->buffer[0] = '1'; |
| 6314 | self->buffer[1] = '2'; |
| 6315 | self->buffer[2] = '3'; |
| 6316 | self->buffer[3] = '4'; |
| 6317 | return PyBuffer_FillInfo( |
| 6318 | view, (PyObject*)self, (void *)self->buffer, 4, 1, flags); |
| 6319 | } |
| 6320 | |
RĂ©mi Lapeyre | b8867e5 | 2020-06-07 09:05:33 +0200 | [diff] [blame^] | 6321 | static void |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 6322 | heapctypewithbuffer_releasebuffer(HeapCTypeWithBufferObject *self, Py_buffer *view) |
| 6323 | { |
| 6324 | assert(view->obj == (void*) self); |
| 6325 | } |
| 6326 | |
| 6327 | static PyType_Slot HeapCTypeWithBuffer_slots[] = { |
| 6328 | {Py_bf_getbuffer, heapctypewithbuffer_getbuffer}, |
| 6329 | {Py_bf_releasebuffer, heapctypewithbuffer_releasebuffer}, |
| 6330 | {Py_tp_doc, (char*)heapctypewithbuffer__doc__}, |
| 6331 | {0, 0}, |
| 6332 | }; |
| 6333 | |
| 6334 | static PyType_Spec HeapCTypeWithBuffer_spec = { |
| 6335 | "_testcapi.HeapCTypeWithBuffer", |
| 6336 | sizeof(HeapCTypeWithBufferObject), |
| 6337 | 0, |
| 6338 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6339 | HeapCTypeWithBuffer_slots |
| 6340 | }; |
| 6341 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6342 | PyDoc_STRVAR(heapctypesubclasswithfinalizer__doc__, |
| 6343 | "Subclass of HeapCType with a finalizer that reassigns __class__.\n\n" |
| 6344 | "__class__ is set to plain HeapCTypeSubclass during finalization.\n" |
| 6345 | "__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 6346 | |
| 6347 | static int |
| 6348 | heapctypesubclasswithfinalizer_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6349 | { |
| 6350 | PyTypeObject *base = (PyTypeObject *)PyType_GetSlot(Py_TYPE(self), Py_tp_base); |
| 6351 | initproc base_init = PyType_GetSlot(base, Py_tp_init); |
| 6352 | base_init(self, args, kwargs); |
| 6353 | return 0; |
| 6354 | } |
| 6355 | |
| 6356 | static void |
| 6357 | heapctypesubclasswithfinalizer_finalize(PyObject *self) |
| 6358 | { |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6359 | PyObject *error_type, *error_value, *error_traceback, *m; |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6360 | PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL; |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6361 | |
| 6362 | /* Save the current exception, if any. */ |
| 6363 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 6364 | |
| 6365 | m = PyState_FindModule(&_testcapimodule); |
| 6366 | if (m == NULL) { |
| 6367 | goto cleanup_finalize; |
| 6368 | } |
| 6369 | oldtype = PyObject_GetAttrString(m, "HeapCTypeSubclassWithFinalizer"); |
| 6370 | newtype = PyObject_GetAttrString(m, "HeapCTypeSubclass"); |
| 6371 | if (oldtype == NULL || newtype == NULL) { |
| 6372 | goto cleanup_finalize; |
| 6373 | } |
| 6374 | |
| 6375 | if (PyObject_SetAttrString(self, "__class__", newtype) < 0) { |
| 6376 | goto cleanup_finalize; |
| 6377 | } |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6378 | refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype)); |
| 6379 | if (refcnt == NULL) { |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6380 | goto cleanup_finalize; |
| 6381 | } |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6382 | if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) { |
| 6383 | goto cleanup_finalize; |
| 6384 | } |
| 6385 | Py_DECREF(refcnt); |
| 6386 | refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype)); |
| 6387 | if (refcnt == NULL) { |
| 6388 | goto cleanup_finalize; |
| 6389 | } |
| 6390 | if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) { |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6391 | goto cleanup_finalize; |
| 6392 | } |
| 6393 | |
| 6394 | cleanup_finalize: |
| 6395 | Py_XDECREF(oldtype); |
| 6396 | Py_XDECREF(newtype); |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6397 | Py_XDECREF(refcnt); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6398 | |
| 6399 | /* Restore the saved exception. */ |
| 6400 | PyErr_Restore(error_type, error_value, error_traceback); |
| 6401 | } |
| 6402 | |
| 6403 | static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = { |
| 6404 | {Py_tp_init, heapctypesubclasswithfinalizer_init}, |
| 6405 | {Py_tp_members, heapctypesubclass_members}, |
| 6406 | {Py_tp_finalize, heapctypesubclasswithfinalizer_finalize}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6407 | {Py_tp_doc, (char*)heapctypesubclasswithfinalizer__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6408 | {0, 0}, |
| 6409 | }; |
| 6410 | |
| 6411 | static PyType_Spec HeapCTypeSubclassWithFinalizer_spec = { |
| 6412 | "_testcapi.HeapCTypeSubclassWithFinalizer", |
| 6413 | sizeof(HeapCTypeSubclassObject), |
| 6414 | 0, |
| 6415 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 6416 | HeapCTypeSubclassWithFinalizer_slots |
| 6417 | }; |
| 6418 | |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6419 | typedef struct { |
| 6420 | PyObject_HEAD |
| 6421 | PyObject *dict; |
| 6422 | } HeapCTypeWithDictObject; |
| 6423 | |
| 6424 | static void |
| 6425 | heapctypewithdict_dealloc(HeapCTypeWithDictObject* self) |
| 6426 | { |
| 6427 | |
| 6428 | PyTypeObject *tp = Py_TYPE(self); |
| 6429 | Py_XDECREF(self->dict); |
| 6430 | PyObject_DEL(self); |
| 6431 | Py_DECREF(tp); |
| 6432 | } |
| 6433 | |
| 6434 | static PyGetSetDef heapctypewithdict_getsetlist[] = { |
| 6435 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
| 6436 | {NULL} /* Sentinel */ |
| 6437 | }; |
| 6438 | |
| 6439 | static struct PyMemberDef heapctypewithdict_members[] = { |
| 6440 | {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
| 6441 | {"__dictoffset__", T_PYSSIZET, offsetof(HeapCTypeWithDictObject, dict), READONLY}, |
| 6442 | {NULL} /* Sentinel */ |
| 6443 | }; |
| 6444 | |
| 6445 | static PyType_Slot HeapCTypeWithDict_slots[] = { |
| 6446 | {Py_tp_members, heapctypewithdict_members}, |
| 6447 | {Py_tp_getset, heapctypewithdict_getsetlist}, |
| 6448 | {Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 6449 | {0, 0}, |
| 6450 | }; |
| 6451 | |
| 6452 | static PyType_Spec HeapCTypeWithDict_spec = { |
| 6453 | "_testcapi.HeapCTypeWithDict", |
| 6454 | sizeof(HeapCTypeWithDictObject), |
| 6455 | 0, |
| 6456 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6457 | HeapCTypeWithDict_slots |
| 6458 | }; |
| 6459 | |
| 6460 | static struct PyMemberDef heapctypewithnegativedict_members[] = { |
| 6461 | {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
Victor Stinner | aca8c40 | 2019-09-30 21:14:26 +0200 | [diff] [blame] | 6462 | {"__dictoffset__", T_PYSSIZET, -(Py_ssize_t)sizeof(void*), READONLY}, |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6463 | {NULL} /* Sentinel */ |
| 6464 | }; |
| 6465 | |
| 6466 | static PyType_Slot HeapCTypeWithNegativeDict_slots[] = { |
| 6467 | {Py_tp_members, heapctypewithnegativedict_members}, |
| 6468 | {Py_tp_getset, heapctypewithdict_getsetlist}, |
| 6469 | {Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 6470 | {0, 0}, |
| 6471 | }; |
| 6472 | |
| 6473 | static PyType_Spec HeapCTypeWithNegativeDict_spec = { |
| 6474 | "_testcapi.HeapCTypeWithNegativeDict", |
| 6475 | sizeof(HeapCTypeWithDictObject), |
| 6476 | 0, |
| 6477 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6478 | HeapCTypeWithNegativeDict_slots |
| 6479 | }; |
| 6480 | |
| 6481 | typedef struct { |
| 6482 | PyObject_HEAD |
| 6483 | PyObject *weakreflist; |
| 6484 | } HeapCTypeWithWeakrefObject; |
| 6485 | |
| 6486 | static struct PyMemberDef heapctypewithweakref_members[] = { |
| 6487 | {"weakreflist", T_OBJECT, offsetof(HeapCTypeWithWeakrefObject, weakreflist)}, |
| 6488 | {"__weaklistoffset__", T_PYSSIZET, |
| 6489 | offsetof(HeapCTypeWithWeakrefObject, weakreflist), READONLY}, |
| 6490 | {NULL} /* Sentinel */ |
| 6491 | }; |
| 6492 | |
| 6493 | static void |
| 6494 | heapctypewithweakref_dealloc(HeapCTypeWithWeakrefObject* self) |
| 6495 | { |
| 6496 | |
| 6497 | PyTypeObject *tp = Py_TYPE(self); |
| 6498 | if (self->weakreflist != NULL) |
| 6499 | PyObject_ClearWeakRefs((PyObject *) self); |
| 6500 | Py_XDECREF(self->weakreflist); |
| 6501 | PyObject_DEL(self); |
| 6502 | Py_DECREF(tp); |
| 6503 | } |
| 6504 | |
| 6505 | static PyType_Slot HeapCTypeWithWeakref_slots[] = { |
| 6506 | {Py_tp_members, heapctypewithweakref_members}, |
| 6507 | {Py_tp_dealloc, heapctypewithweakref_dealloc}, |
| 6508 | {0, 0}, |
| 6509 | }; |
| 6510 | |
| 6511 | static PyType_Spec HeapCTypeWithWeakref_spec = { |
| 6512 | "_testcapi.HeapCTypeWithWeakref", |
| 6513 | sizeof(HeapCTypeWithWeakrefObject), |
| 6514 | 0, |
| 6515 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6516 | HeapCTypeWithWeakref_slots |
| 6517 | }; |
| 6518 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6519 | static PyMethodDef meth_instance_methods[] = { |
| 6520 | {"meth_varargs", meth_varargs, METH_VARARGS}, |
| 6521 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, |
| 6522 | {"meth_o", meth_o, METH_O}, |
| 6523 | {"meth_noargs", meth_noargs, METH_NOARGS}, |
| 6524 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, |
| 6525 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS}, |
| 6526 | {NULL, NULL} /* sentinel */ |
| 6527 | }; |
| 6528 | |
| 6529 | |
| 6530 | static PyTypeObject MethInstance_Type = { |
| 6531 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6532 | "MethInstance", |
| 6533 | sizeof(PyObject), |
| 6534 | .tp_new = PyType_GenericNew, |
| 6535 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6536 | .tp_methods = meth_instance_methods, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6537 | .tp_doc = (char*)PyDoc_STR( |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6538 | "Class with normal (instance) methods to test calling conventions"), |
| 6539 | }; |
| 6540 | |
| 6541 | static PyMethodDef meth_class_methods[] = { |
| 6542 | {"meth_varargs", meth_varargs, METH_VARARGS|METH_CLASS}, |
| 6543 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_CLASS}, |
| 6544 | {"meth_o", meth_o, METH_O|METH_CLASS}, |
| 6545 | {"meth_noargs", meth_noargs, METH_NOARGS|METH_CLASS}, |
| 6546 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_CLASS}, |
| 6547 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_CLASS}, |
| 6548 | {NULL, NULL} /* sentinel */ |
| 6549 | }; |
| 6550 | |
| 6551 | |
| 6552 | static PyTypeObject MethClass_Type = { |
| 6553 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6554 | "MethClass", |
| 6555 | sizeof(PyObject), |
| 6556 | .tp_new = PyType_GenericNew, |
| 6557 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6558 | .tp_methods = meth_class_methods, |
| 6559 | .tp_doc = PyDoc_STR( |
| 6560 | "Class with class methods to test calling conventions"), |
| 6561 | }; |
| 6562 | |
| 6563 | static PyMethodDef meth_static_methods[] = { |
| 6564 | {"meth_varargs", meth_varargs, METH_VARARGS|METH_STATIC}, |
| 6565 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_STATIC}, |
| 6566 | {"meth_o", meth_o, METH_O|METH_STATIC}, |
| 6567 | {"meth_noargs", meth_noargs, METH_NOARGS|METH_STATIC}, |
| 6568 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_STATIC}, |
| 6569 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_STATIC}, |
| 6570 | {NULL, NULL} /* sentinel */ |
| 6571 | }; |
| 6572 | |
| 6573 | |
| 6574 | static PyTypeObject MethStatic_Type = { |
| 6575 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6576 | "MethStatic", |
| 6577 | sizeof(PyObject), |
| 6578 | .tp_new = PyType_GenericNew, |
| 6579 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6580 | .tp_methods = meth_static_methods, |
| 6581 | .tp_doc = PyDoc_STR( |
| 6582 | "Class with static methods to test calling conventions"), |
| 6583 | }; |
| 6584 | |
Neil Schemenauer | 392a13b | 2019-10-15 20:56:48 -0700 | [diff] [blame] | 6585 | /* ContainerNoGC -- a simple container without GC methods */ |
| 6586 | |
| 6587 | typedef struct { |
| 6588 | PyObject_HEAD |
| 6589 | PyObject *value; |
| 6590 | } ContainerNoGCobject; |
| 6591 | |
| 6592 | static PyObject * |
| 6593 | ContainerNoGC_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 6594 | { |
| 6595 | PyObject *value; |
| 6596 | char *names[] = {"value", NULL}; |
| 6597 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", names, &value)) { |
| 6598 | return NULL; |
| 6599 | } |
| 6600 | PyObject *self = type->tp_alloc(type, 0); |
| 6601 | if (self == NULL) { |
| 6602 | return NULL; |
| 6603 | } |
| 6604 | Py_INCREF(value); |
| 6605 | ((ContainerNoGCobject *)self)->value = value; |
| 6606 | return self; |
| 6607 | } |
| 6608 | |
| 6609 | static void |
| 6610 | ContainerNoGC_dealloc(ContainerNoGCobject *self) |
| 6611 | { |
| 6612 | Py_DECREF(self->value); |
| 6613 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6614 | } |
| 6615 | |
| 6616 | static PyMemberDef ContainerNoGC_members[] = { |
| 6617 | {"value", T_OBJECT, offsetof(ContainerNoGCobject, value), READONLY, |
| 6618 | PyDoc_STR("a container value for test purposes")}, |
| 6619 | {0} |
| 6620 | }; |
| 6621 | |
| 6622 | static PyTypeObject ContainerNoGC_type = { |
| 6623 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6624 | "_testcapi.ContainerNoGC", |
| 6625 | sizeof(ContainerNoGCobject), |
| 6626 | .tp_dealloc = (destructor)ContainerNoGC_dealloc, |
| 6627 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6628 | .tp_members = ContainerNoGC_members, |
| 6629 | .tp_new = ContainerNoGC_new, |
| 6630 | }; |
| 6631 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6632 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6633 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6634 | PyModuleDef_HEAD_INIT, |
| 6635 | "_testcapi", |
| 6636 | NULL, |
| 6637 | -1, |
| 6638 | TestMethods, |
| 6639 | NULL, |
| 6640 | NULL, |
| 6641 | NULL, |
| 6642 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6643 | }; |
| 6644 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 6645 | /* Per PEP 489, this module will not be converted to multi-phase initialization |
| 6646 | */ |
| 6647 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 6648 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6649 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6651 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6653 | m = PyModule_Create(&_testcapimodule); |
| 6654 | if (m == NULL) |
| 6655 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6656 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 6657 | Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type); |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 6658 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 6659 | Py_SET_TYPE(&test_structmembersType, &PyType_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6660 | Py_INCREF(&test_structmembersType); |
| 6661 | /* don't use a name starting with "test", since we don't want |
| 6662 | test_capi to automatically call this */ |
| 6663 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6664 | if (PyType_Ready(&matmulType) < 0) |
| 6665 | return NULL; |
| 6666 | Py_INCREF(&matmulType); |
| 6667 | PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); |
Zackery Spytz | c7f803b | 2019-05-31 03:46:36 -0600 | [diff] [blame] | 6668 | if (PyType_Ready(&ipowType) < 0) { |
| 6669 | return NULL; |
| 6670 | } |
| 6671 | Py_INCREF(&ipowType); |
| 6672 | PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6673 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6674 | if (PyType_Ready(&awaitType) < 0) |
| 6675 | return NULL; |
| 6676 | Py_INCREF(&awaitType); |
| 6677 | PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType); |
| 6678 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 6679 | MyList_Type.tp_base = &PyList_Type; |
| 6680 | if (PyType_Ready(&MyList_Type) < 0) |
| 6681 | return NULL; |
| 6682 | Py_INCREF(&MyList_Type); |
| 6683 | PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type); |
| 6684 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6685 | if (PyType_Ready(&MethodDescriptorBase_Type) < 0) |
| 6686 | return NULL; |
| 6687 | Py_INCREF(&MethodDescriptorBase_Type); |
| 6688 | PyModule_AddObject(m, "MethodDescriptorBase", (PyObject *)&MethodDescriptorBase_Type); |
| 6689 | |
| 6690 | MethodDescriptorDerived_Type.tp_base = &MethodDescriptorBase_Type; |
| 6691 | if (PyType_Ready(&MethodDescriptorDerived_Type) < 0) |
| 6692 | return NULL; |
| 6693 | Py_INCREF(&MethodDescriptorDerived_Type); |
| 6694 | PyModule_AddObject(m, "MethodDescriptorDerived", (PyObject *)&MethodDescriptorDerived_Type); |
| 6695 | |
| 6696 | MethodDescriptorNopGet_Type.tp_base = &MethodDescriptorBase_Type; |
| 6697 | if (PyType_Ready(&MethodDescriptorNopGet_Type) < 0) |
| 6698 | return NULL; |
| 6699 | Py_INCREF(&MethodDescriptorNopGet_Type); |
| 6700 | PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type); |
| 6701 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6702 | MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type; |
| 6703 | if (PyType_Ready(&MethodDescriptor2_Type) < 0) |
| 6704 | return NULL; |
| 6705 | Py_INCREF(&MethodDescriptor2_Type); |
| 6706 | PyModule_AddObject(m, "MethodDescriptor2", (PyObject *)&MethodDescriptor2_Type); |
| 6707 | |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6708 | if (PyType_Ready(&GenericAlias_Type) < 0) |
| 6709 | return NULL; |
| 6710 | Py_INCREF(&GenericAlias_Type); |
| 6711 | PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type); |
| 6712 | |
| 6713 | if (PyType_Ready(&Generic_Type) < 0) |
| 6714 | return NULL; |
| 6715 | Py_INCREF(&Generic_Type); |
| 6716 | PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type); |
| 6717 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6718 | if (PyType_Ready(&MethInstance_Type) < 0) |
| 6719 | return NULL; |
| 6720 | Py_INCREF(&MethInstance_Type); |
| 6721 | PyModule_AddObject(m, "MethInstance", (PyObject *)&MethInstance_Type); |
| 6722 | |
| 6723 | if (PyType_Ready(&MethClass_Type) < 0) |
| 6724 | return NULL; |
| 6725 | Py_INCREF(&MethClass_Type); |
| 6726 | PyModule_AddObject(m, "MethClass", (PyObject *)&MethClass_Type); |
| 6727 | |
| 6728 | if (PyType_Ready(&MethStatic_Type) < 0) |
| 6729 | return NULL; |
| 6730 | Py_INCREF(&MethStatic_Type); |
| 6731 | PyModule_AddObject(m, "MethStatic", (PyObject *)&MethStatic_Type); |
| 6732 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 6733 | PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception; |
| 6734 | if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) { |
| 6735 | return NULL; |
| 6736 | } |
| 6737 | Py_INCREF(&PyRecursingInfinitelyError_Type); |
| 6738 | PyModule_AddObject(m, "RecursingInfinitelyError", |
| 6739 | (PyObject *)&PyRecursingInfinitelyError_Type); |
| 6740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6741 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 6742 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 6743 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 6744 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 6745 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 6746 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 6747 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 6748 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 6749 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 6750 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 6751 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 6752 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 6753 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 6754 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 6755 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 6756 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 6757 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX)); |
| 6758 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN)); |
| 6759 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6760 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 6761 | 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] | 6762 | PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6763 | Py_INCREF(&PyInstanceMethod_Type); |
| 6764 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 6765 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 6766 | PyModule_AddIntConstant(m, "the_number_three", 3); |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6767 | PyObject *v; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6768 | #ifdef WITH_PYMALLOC |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6769 | v = Py_True; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6770 | #else |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6771 | v = Py_False; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6772 | #endif |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6773 | Py_INCREF(v); |
| 6774 | PyModule_AddObject(m, "WITH_PYMALLOC", v); |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 6775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6776 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 6777 | Py_INCREF(TestError); |
| 6778 | PyModule_AddObject(m, "error", TestError); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6779 | |
| 6780 | PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec); |
| 6781 | if (HeapGcCType == NULL) { |
| 6782 | return NULL; |
| 6783 | } |
| 6784 | PyModule_AddObject(m, "HeapGcCType", HeapGcCType); |
| 6785 | |
| 6786 | PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec); |
| 6787 | if (HeapCType == NULL) { |
| 6788 | return NULL; |
| 6789 | } |
| 6790 | PyObject *subclass_bases = PyTuple_Pack(1, HeapCType); |
| 6791 | if (subclass_bases == NULL) { |
| 6792 | return NULL; |
| 6793 | } |
| 6794 | PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases); |
| 6795 | if (HeapCTypeSubclass == NULL) { |
| 6796 | return NULL; |
| 6797 | } |
| 6798 | Py_DECREF(subclass_bases); |
| 6799 | PyModule_AddObject(m, "HeapCTypeSubclass", HeapCTypeSubclass); |
| 6800 | |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6801 | PyObject *HeapCTypeWithDict = PyType_FromSpec(&HeapCTypeWithDict_spec); |
| 6802 | if (HeapCTypeWithDict == NULL) { |
| 6803 | return NULL; |
| 6804 | } |
| 6805 | PyModule_AddObject(m, "HeapCTypeWithDict", HeapCTypeWithDict); |
| 6806 | |
| 6807 | PyObject *HeapCTypeWithNegativeDict = PyType_FromSpec(&HeapCTypeWithNegativeDict_spec); |
| 6808 | if (HeapCTypeWithNegativeDict == NULL) { |
| 6809 | return NULL; |
| 6810 | } |
| 6811 | PyModule_AddObject(m, "HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict); |
| 6812 | |
| 6813 | PyObject *HeapCTypeWithWeakref = PyType_FromSpec(&HeapCTypeWithWeakref_spec); |
| 6814 | if (HeapCTypeWithWeakref == NULL) { |
| 6815 | return NULL; |
| 6816 | } |
| 6817 | PyModule_AddObject(m, "HeapCTypeWithWeakref", HeapCTypeWithWeakref); |
| 6818 | |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 6819 | PyObject *HeapCTypeWithBuffer = PyType_FromSpec(&HeapCTypeWithBuffer_spec); |
| 6820 | if (HeapCTypeWithBuffer == NULL) { |
| 6821 | return NULL; |
| 6822 | } |
| 6823 | PyModule_AddObject(m, "HeapCTypeWithBuffer", HeapCTypeWithBuffer); |
| 6824 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6825 | PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass); |
| 6826 | if (subclass_with_finalizer_bases == NULL) { |
| 6827 | return NULL; |
| 6828 | } |
| 6829 | PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases( |
| 6830 | &HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases); |
| 6831 | if (HeapCTypeSubclassWithFinalizer == NULL) { |
| 6832 | return NULL; |
| 6833 | } |
| 6834 | Py_DECREF(subclass_with_finalizer_bases); |
| 6835 | PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer); |
| 6836 | |
Neil Schemenauer | 392a13b | 2019-10-15 20:56:48 -0700 | [diff] [blame] | 6837 | if (PyType_Ready(&ContainerNoGC_type) < 0) { |
| 6838 | return NULL; |
| 6839 | } |
| 6840 | Py_INCREF(&ContainerNoGC_type); |
| 6841 | if (PyModule_AddObject(m, "ContainerNoGC", |
| 6842 | (PyObject *) &ContainerNoGC_type) < 0) |
| 6843 | return NULL; |
| 6844 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6845 | PyState_AddModule(m, &_testcapimodule); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6846 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6847 | } |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 6848 | |
| 6849 | |
| 6850 | /* Test the C API exposed when PY_SSIZE_T_CLEAN is not defined */ |
| 6851 | |
| 6852 | #undef Py_BuildValue |
| 6853 | PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...); |
| 6854 | |
| 6855 | static PyObject * |
| 6856 | test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 6857 | { |
| 6858 | PyObject *res; |
| 6859 | const char str[] = "string"; |
| 6860 | const Py_UNICODE unicode[] = L"unicode"; |
| 6861 | PyErr_SetNone(PyExc_ZeroDivisionError); |
| 6862 | |
| 6863 | res = Py_BuildValue("(s#O)", str, 1, Py_None); |
| 6864 | assert(res == NULL); |
| 6865 | if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) { |
| 6866 | return NULL; |
| 6867 | } |
| 6868 | res = Py_BuildValue("(z#O)", str, 1, Py_None); |
| 6869 | assert(res == NULL); |
| 6870 | if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) { |
| 6871 | return NULL; |
| 6872 | } |
| 6873 | res = Py_BuildValue("(y#O)", str, 1, Py_None); |
| 6874 | assert(res == NULL); |
| 6875 | if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) { |
| 6876 | return NULL; |
| 6877 | } |
| 6878 | res = Py_BuildValue("(u#O)", unicode, 1, Py_None); |
| 6879 | assert(res == NULL); |
| 6880 | if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) { |
| 6881 | return NULL; |
| 6882 | } |
| 6883 | |
| 6884 | PyErr_Clear(); |
| 6885 | Py_RETURN_NONE; |
| 6886 | } |