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 | |
scoder | 3cc481b | 2021-04-28 18:12:16 +0200 | [diff] [blame] | 147 | static PyObject* |
| 148 | test_gc_control(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 149 | { |
| 150 | int orig_enabled = PyGC_IsEnabled(); |
| 151 | const char* msg = "ok"; |
| 152 | int old_state; |
| 153 | |
| 154 | old_state = PyGC_Enable(); |
| 155 | msg = "Enable(1)"; |
| 156 | if (old_state != orig_enabled) { |
| 157 | goto failed; |
| 158 | } |
| 159 | msg = "IsEnabled(1)"; |
| 160 | if (!PyGC_IsEnabled()) { |
| 161 | goto failed; |
| 162 | } |
| 163 | |
| 164 | old_state = PyGC_Disable(); |
| 165 | msg = "disable(2)"; |
| 166 | if (!old_state) { |
| 167 | goto failed; |
| 168 | } |
| 169 | msg = "IsEnabled(2)"; |
| 170 | if (PyGC_IsEnabled()) { |
| 171 | goto failed; |
| 172 | } |
| 173 | |
| 174 | old_state = PyGC_Enable(); |
| 175 | msg = "enable(3)"; |
| 176 | if (old_state) { |
| 177 | goto failed; |
| 178 | } |
| 179 | msg = "IsEnabled(3)"; |
| 180 | if (!PyGC_IsEnabled()) { |
| 181 | goto failed; |
| 182 | } |
| 183 | |
| 184 | if (!orig_enabled) { |
| 185 | old_state = PyGC_Disable(); |
| 186 | msg = "disable(4)"; |
| 187 | if (old_state) { |
| 188 | goto failed; |
| 189 | } |
| 190 | msg = "IsEnabled(4)"; |
| 191 | if (PyGC_IsEnabled()) { |
| 192 | goto failed; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | Py_RETURN_NONE; |
| 197 | |
| 198 | failed: |
| 199 | /* Try to clean up if we can. */ |
| 200 | if (orig_enabled) { |
| 201 | PyGC_Enable(); |
| 202 | } else { |
| 203 | PyGC_Disable(); |
| 204 | } |
| 205 | PyErr_Format(TestError, "GC control failed in %s", msg); |
| 206 | return NULL; |
| 207 | } |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 208 | |
| 209 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 210 | test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 211 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | PyObject* list; |
| 213 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 216 | #define NLIST 30 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | list = PyList_New(NLIST); |
| 218 | if (list == (PyObject*)NULL) |
| 219 | return (PyObject*)NULL; |
| 220 | /* list = range(NLIST) */ |
| 221 | for (i = 0; i < NLIST; ++i) { |
| 222 | PyObject* anint = PyLong_FromLong(i); |
| 223 | if (anint == (PyObject*)NULL) { |
| 224 | Py_DECREF(list); |
| 225 | return (PyObject*)NULL; |
| 226 | } |
| 227 | PyList_SET_ITEM(list, i, anint); |
| 228 | } |
| 229 | /* list.reverse(), via PyList_Reverse() */ |
| 230 | i = PyList_Reverse(list); /* should not blow up! */ |
| 231 | if (i != 0) { |
| 232 | Py_DECREF(list); |
| 233 | return (PyObject*)NULL; |
| 234 | } |
| 235 | /* Check that list == range(29, -1, -1) now */ |
| 236 | for (i = 0; i < NLIST; ++i) { |
| 237 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 238 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 239 | PyErr_SetString(TestError, |
| 240 | "test_list_api: reverse screwed up"); |
| 241 | Py_DECREF(list); |
| 242 | return (PyObject*)NULL; |
| 243 | } |
| 244 | } |
| 245 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 246 | #undef NLIST |
| 247 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 248 | Py_RETURN_NONE; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 251 | static int |
| 252 | test_dict_inner(int count) |
| 253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | Py_ssize_t pos = 0, iterations = 0; |
| 255 | int i; |
| 256 | PyObject *dict = PyDict_New(); |
| 257 | PyObject *v, *k; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | if (dict == NULL) |
| 260 | return -1; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | for (i = 0; i < count; i++) { |
| 263 | v = PyLong_FromLong(i); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 264 | if (v == NULL) { |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 265 | return -1; |
| 266 | } |
Christian Heimes | 97cb67b | 2013-07-20 15:01:26 +0200 | [diff] [blame] | 267 | if (PyDict_SetItem(dict, v, v) < 0) { |
| 268 | Py_DECREF(v); |
| 269 | return -1; |
| 270 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | Py_DECREF(v); |
| 272 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 275 | PyObject *o; |
| 276 | iterations++; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | i = PyLong_AS_LONG(v) + 1; |
| 279 | o = PyLong_FromLong(i); |
| 280 | if (o == NULL) |
| 281 | return -1; |
| 282 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 283 | Py_DECREF(o); |
| 284 | return -1; |
| 285 | } |
| 286 | Py_DECREF(o); |
| 287 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 | Py_DECREF(dict); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | if (iterations != count) { |
| 292 | PyErr_SetString( |
| 293 | TestError, |
| 294 | "test_dict_iteration: dict iteration went wrong "); |
| 295 | return -1; |
| 296 | } else { |
| 297 | return 0; |
| 298 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 302 | test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 303 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | for (i = 0; i < 200; i++) { |
| 307 | if (test_dict_inner(i) < 0) { |
| 308 | return NULL; |
| 309 | } |
| 310 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 311 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 312 | Py_RETURN_NONE; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 315 | static PyObject* |
| 316 | dict_getitem_knownhash(PyObject *self, PyObject *args) |
| 317 | { |
| 318 | PyObject *mp, *key, *result; |
| 319 | Py_ssize_t hash; |
| 320 | |
| 321 | if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash", |
| 322 | &mp, &key, &hash)) { |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash); |
| 327 | if (result == NULL && !PyErr_Occurred()) { |
| 328 | _PyErr_SetKeyError(key); |
| 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | Py_XINCREF(result); |
| 333 | return result; |
| 334 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 335 | |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 336 | static PyObject* |
| 337 | dict_hassplittable(PyObject *self, PyObject *arg) |
| 338 | { |
| 339 | if (!PyDict_Check(arg)) { |
| 340 | PyErr_Format(PyExc_TypeError, |
| 341 | "dict_hassplittable() argument must be dict, not '%s'", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 342 | Py_TYPE(arg)->tp_name); |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg)); |
| 347 | } |
| 348 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 349 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 350 | * PyType_Ready if it hasn't already been called |
| 351 | */ |
| 352 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | PyVarObject_HEAD_INIT(NULL, 0) |
| 354 | "hashinheritancetester", /* Name of this type */ |
| 355 | sizeof(PyObject), /* Basic object size */ |
| 356 | 0, /* Item size for varobject */ |
| 357 | (destructor)PyObject_Del, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 358 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | 0, /* tp_getattr */ |
| 360 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 361 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | 0, /* tp_repr */ |
| 363 | 0, /* tp_as_number */ |
| 364 | 0, /* tp_as_sequence */ |
| 365 | 0, /* tp_as_mapping */ |
| 366 | 0, /* tp_hash */ |
| 367 | 0, /* tp_call */ |
| 368 | 0, /* tp_str */ |
| 369 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 370 | 0, /* tp_setattro */ |
| 371 | 0, /* tp_as_buffer */ |
| 372 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 373 | 0, /* tp_doc */ |
| 374 | 0, /* tp_traverse */ |
| 375 | 0, /* tp_clear */ |
| 376 | 0, /* tp_richcompare */ |
| 377 | 0, /* tp_weaklistoffset */ |
| 378 | 0, /* tp_iter */ |
| 379 | 0, /* tp_iternext */ |
| 380 | 0, /* tp_methods */ |
| 381 | 0, /* tp_members */ |
| 382 | 0, /* tp_getset */ |
| 383 | 0, /* tp_base */ |
| 384 | 0, /* tp_dict */ |
| 385 | 0, /* tp_descr_get */ |
| 386 | 0, /* tp_descr_set */ |
| 387 | 0, /* tp_dictoffset */ |
| 388 | 0, /* tp_init */ |
| 389 | 0, /* tp_alloc */ |
| 390 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 394 | test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | PyTypeObject *type; |
| 397 | PyObject *obj; |
Antoine Pitrou | 29aad00 | 2010-10-23 19:42:38 +0000 | [diff] [blame] | 398 | Py_hash_t hash; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | if (type->tp_dict != NULL) |
| 403 | /* The type has already been initialized. This probably means |
| 404 | -R is being used. */ |
| 405 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 406 | |
| 407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | obj = PyObject_New(PyObject, type); |
| 409 | if (obj == NULL) { |
| 410 | PyErr_Clear(); |
| 411 | PyErr_SetString( |
| 412 | TestError, |
| 413 | "test_lazy_hash_inheritance: failed to create object"); |
| 414 | return NULL; |
| 415 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | if (type->tp_dict != NULL) { |
| 418 | PyErr_SetString( |
| 419 | TestError, |
| 420 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 421 | Py_DECREF(obj); |
| 422 | return NULL; |
| 423 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | hash = PyObject_Hash(obj); |
| 426 | if ((hash == -1) && PyErr_Occurred()) { |
| 427 | PyErr_Clear(); |
| 428 | PyErr_SetString( |
| 429 | TestError, |
| 430 | "test_lazy_hash_inheritance: could not hash object"); |
| 431 | Py_DECREF(obj); |
| 432 | return NULL; |
| 433 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | if (type->tp_dict == NULL) { |
| 436 | PyErr_SetString( |
| 437 | TestError, |
| 438 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 439 | Py_DECREF(obj); |
| 440 | return NULL; |
| 441 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 444 | PyErr_SetString( |
| 445 | TestError, |
| 446 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 447 | Py_DECREF(obj); |
| 448 | return NULL; |
| 449 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | Py_RETURN_NONE; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 457 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 458 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 459 | |
| 460 | Note that the meat of the test is contained in testcapi_long.h. |
| 461 | This is revolting, but delicate code duplication is worse: "almost |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 462 | 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] | 463 | dependence on type names makes it impossible to use a parameterized |
| 464 | function. A giant macro would be even worse than this. A C++ template |
| 465 | would be perfect. |
| 466 | |
| 467 | The "report an error" functions are deliberately not part of the #include |
| 468 | file: if the test fails, you can set a breakpoint in the appropriate |
| 469 | error function directly, and crawl back from there in the debugger. |
| 470 | */ |
| 471 | |
| 472 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 473 | |
| 474 | static PyObject * |
| 475 | raise_test_long_error(const char* msg) |
| 476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | #define TESTNAME test_long_api_inner |
| 481 | #define TYPENAME long |
| 482 | #define F_S_TO_PY PyLong_FromLong |
| 483 | #define F_PY_TO_S PyLong_AsLong |
| 484 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 485 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 486 | |
| 487 | #include "testcapi_long.h" |
| 488 | |
| 489 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 490 | test_long_api(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 491 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | #undef TESTNAME |
| 496 | #undef TYPENAME |
| 497 | #undef F_S_TO_PY |
| 498 | #undef F_PY_TO_S |
| 499 | #undef F_U_TO_PY |
| 500 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 501 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 502 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 503 | raise_test_longlong_error(const char* msg) |
| 504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | #define TESTNAME test_longlong_api_inner |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 509 | #define TYPENAME long long |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | #define F_S_TO_PY PyLong_FromLongLong |
| 511 | #define F_PY_TO_S PyLong_AsLongLong |
| 512 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 513 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 514 | |
| 515 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 516 | |
| 517 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 518 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 519 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 523 | #undef TESTNAME |
| 524 | #undef TYPENAME |
| 525 | #undef F_S_TO_PY |
| 526 | #undef F_PY_TO_S |
| 527 | #undef F_U_TO_PY |
| 528 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 529 | |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 530 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 531 | is tested by test_long_api_inner. This test will concentrate on proper |
| 532 | handling of overflow. |
| 533 | */ |
| 534 | |
| 535 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 536 | test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | PyObject *num, *one, *temp; |
| 539 | long value; |
| 540 | int overflow; |
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 | /* Test that overflow is set properly for a large value. */ |
| 543 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 544 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 545 | if (num == NULL) |
| 546 | return NULL; |
| 547 | overflow = 1234; |
| 548 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 549 | Py_DECREF(num); |
| 550 | if (value == -1 && PyErr_Occurred()) |
| 551 | return NULL; |
| 552 | if (value != -1) |
| 553 | return raiseTestError("test_long_and_overflow", |
| 554 | "return value was not set to -1"); |
| 555 | if (overflow != 1) |
| 556 | return raiseTestError("test_long_and_overflow", |
| 557 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | /* Same again, with num = LONG_MAX + 1 */ |
| 560 | num = PyLong_FromLong(LONG_MAX); |
| 561 | if (num == NULL) |
| 562 | return NULL; |
| 563 | one = PyLong_FromLong(1L); |
| 564 | if (one == NULL) { |
| 565 | Py_DECREF(num); |
| 566 | return NULL; |
| 567 | } |
| 568 | temp = PyNumber_Add(num, one); |
| 569 | Py_DECREF(one); |
| 570 | Py_DECREF(num); |
| 571 | num = temp; |
| 572 | if (num == NULL) |
| 573 | return NULL; |
| 574 | overflow = 0; |
| 575 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 576 | Py_DECREF(num); |
| 577 | if (value == -1 && PyErr_Occurred()) |
| 578 | return NULL; |
| 579 | if (value != -1) |
| 580 | return raiseTestError("test_long_and_overflow", |
| 581 | "return value was not set to -1"); |
| 582 | if (overflow != 1) |
| 583 | return raiseTestError("test_long_and_overflow", |
| 584 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | /* Test that overflow is set properly for a large negative value. */ |
| 587 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 588 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 589 | if (num == NULL) |
| 590 | return NULL; |
| 591 | overflow = 1234; |
| 592 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 593 | Py_DECREF(num); |
| 594 | if (value == -1 && PyErr_Occurred()) |
| 595 | return NULL; |
| 596 | if (value != -1) |
| 597 | return raiseTestError("test_long_and_overflow", |
| 598 | "return value was not set to -1"); |
| 599 | if (overflow != -1) |
| 600 | return raiseTestError("test_long_and_overflow", |
| 601 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | /* Same again, with num = LONG_MIN - 1 */ |
| 604 | num = PyLong_FromLong(LONG_MIN); |
| 605 | if (num == NULL) |
| 606 | return NULL; |
| 607 | one = PyLong_FromLong(1L); |
| 608 | if (one == NULL) { |
| 609 | Py_DECREF(num); |
| 610 | return NULL; |
| 611 | } |
| 612 | temp = PyNumber_Subtract(num, one); |
| 613 | Py_DECREF(one); |
| 614 | Py_DECREF(num); |
| 615 | num = temp; |
| 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 != -1) |
| 624 | return raiseTestError("test_long_and_overflow", |
| 625 | "return value was not set to -1"); |
| 626 | if (overflow != -1) |
| 627 | return raiseTestError("test_long_and_overflow", |
| 628 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | /* Test that overflow is cleared properly for small values. */ |
| 631 | num = PyLong_FromString("FF", NULL, 16); |
| 632 | if (num == NULL) |
| 633 | return NULL; |
| 634 | overflow = 1234; |
| 635 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 636 | Py_DECREF(num); |
| 637 | if (value == -1 && PyErr_Occurred()) |
| 638 | return NULL; |
| 639 | if (value != 0xFF) |
| 640 | return raiseTestError("test_long_and_overflow", |
| 641 | "expected return value 0xFF"); |
| 642 | if (overflow != 0) |
| 643 | return raiseTestError("test_long_and_overflow", |
| 644 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | num = PyLong_FromString("-FF", NULL, 16); |
| 647 | if (num == NULL) |
| 648 | return NULL; |
| 649 | overflow = 0; |
| 650 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 651 | Py_DECREF(num); |
| 652 | if (value == -1 && PyErr_Occurred()) |
| 653 | return NULL; |
| 654 | if (value != -0xFF) |
| 655 | return raiseTestError("test_long_and_overflow", |
| 656 | "expected return value 0xFF"); |
| 657 | if (overflow != 0) |
| 658 | return raiseTestError("test_long_and_overflow", |
| 659 | "overflow was set incorrectly"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | num = PyLong_FromLong(LONG_MAX); |
| 662 | if (num == NULL) |
| 663 | return NULL; |
| 664 | overflow = 1234; |
| 665 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 666 | Py_DECREF(num); |
| 667 | if (value == -1 && PyErr_Occurred()) |
| 668 | return NULL; |
| 669 | if (value != LONG_MAX) |
| 670 | return raiseTestError("test_long_and_overflow", |
| 671 | "expected return value LONG_MAX"); |
| 672 | if (overflow != 0) |
| 673 | return raiseTestError("test_long_and_overflow", |
| 674 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | num = PyLong_FromLong(LONG_MIN); |
| 677 | if (num == NULL) |
| 678 | return NULL; |
| 679 | overflow = 0; |
| 680 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 681 | Py_DECREF(num); |
| 682 | if (value == -1 && PyErr_Occurred()) |
| 683 | return NULL; |
| 684 | if (value != LONG_MIN) |
| 685 | return raiseTestError("test_long_and_overflow", |
| 686 | "expected return value LONG_MIN"); |
| 687 | if (overflow != 0) |
| 688 | return raiseTestError("test_long_and_overflow", |
| 689 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 690 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 691 | Py_RETURN_NONE; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 694 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 695 | long long is tested by test_long_api_inner. This test will |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 696 | concentrate on proper handling of overflow. |
| 697 | */ |
| 698 | |
| 699 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 700 | test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | PyObject *num, *one, *temp; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 703 | long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | int overflow; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | /* Test that overflow is set properly for a large value. */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 707 | /* num is a number larger than LLONG_MAX on a typical machine. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 709 | if (num == NULL) |
| 710 | return NULL; |
| 711 | overflow = 1234; |
| 712 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 713 | Py_DECREF(num); |
| 714 | if (value == -1 && PyErr_Occurred()) |
| 715 | return NULL; |
| 716 | if (value != -1) |
| 717 | return raiseTestError("test_long_long_and_overflow", |
| 718 | "return value was not set to -1"); |
| 719 | if (overflow != 1) |
| 720 | return raiseTestError("test_long_long_and_overflow", |
| 721 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 722 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 723 | /* Same again, with num = LLONG_MAX + 1 */ |
| 724 | num = PyLong_FromLongLong(LLONG_MAX); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | if (num == NULL) |
| 726 | return NULL; |
| 727 | one = PyLong_FromLong(1L); |
| 728 | if (one == NULL) { |
| 729 | Py_DECREF(num); |
| 730 | return NULL; |
| 731 | } |
| 732 | temp = PyNumber_Add(num, one); |
| 733 | Py_DECREF(one); |
| 734 | Py_DECREF(num); |
| 735 | num = temp; |
| 736 | if (num == NULL) |
| 737 | return NULL; |
| 738 | overflow = 0; |
| 739 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 740 | Py_DECREF(num); |
| 741 | if (value == -1 && PyErr_Occurred()) |
| 742 | return NULL; |
| 743 | if (value != -1) |
| 744 | return raiseTestError("test_long_long_and_overflow", |
| 745 | "return value was not set to -1"); |
| 746 | if (overflow != 1) |
| 747 | return raiseTestError("test_long_long_and_overflow", |
| 748 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | /* Test that overflow is set properly for a large negative value. */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 751 | /* num is a number smaller than LLONG_MIN on a typical platform */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 753 | if (num == NULL) |
| 754 | return NULL; |
| 755 | overflow = 1234; |
| 756 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 757 | Py_DECREF(num); |
| 758 | if (value == -1 && PyErr_Occurred()) |
| 759 | return NULL; |
| 760 | if (value != -1) |
| 761 | return raiseTestError("test_long_long_and_overflow", |
| 762 | "return value was not set to -1"); |
| 763 | if (overflow != -1) |
| 764 | return raiseTestError("test_long_long_and_overflow", |
| 765 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 766 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 767 | /* Same again, with num = LLONG_MIN - 1 */ |
| 768 | num = PyLong_FromLongLong(LLONG_MIN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | if (num == NULL) |
| 770 | return NULL; |
| 771 | one = PyLong_FromLong(1L); |
| 772 | if (one == NULL) { |
| 773 | Py_DECREF(num); |
| 774 | return NULL; |
| 775 | } |
| 776 | temp = PyNumber_Subtract(num, one); |
| 777 | Py_DECREF(one); |
| 778 | Py_DECREF(num); |
| 779 | num = temp; |
| 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; |
| 787 | if (value != -1) |
| 788 | return raiseTestError("test_long_long_and_overflow", |
| 789 | "return value was not set to -1"); |
| 790 | if (overflow != -1) |
| 791 | return raiseTestError("test_long_long_and_overflow", |
| 792 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | /* Test that overflow is cleared properly for small values. */ |
| 795 | num = PyLong_FromString("FF", NULL, 16); |
| 796 | if (num == NULL) |
| 797 | return NULL; |
| 798 | overflow = 1234; |
| 799 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 800 | Py_DECREF(num); |
| 801 | if (value == -1 && PyErr_Occurred()) |
| 802 | return NULL; |
| 803 | if (value != 0xFF) |
| 804 | return raiseTestError("test_long_long_and_overflow", |
| 805 | "expected return value 0xFF"); |
| 806 | if (overflow != 0) |
| 807 | return raiseTestError("test_long_long_and_overflow", |
| 808 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | num = PyLong_FromString("-FF", NULL, 16); |
| 811 | if (num == NULL) |
| 812 | return NULL; |
| 813 | overflow = 0; |
| 814 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 815 | Py_DECREF(num); |
| 816 | if (value == -1 && PyErr_Occurred()) |
| 817 | return NULL; |
| 818 | if (value != -0xFF) |
| 819 | return raiseTestError("test_long_long_and_overflow", |
| 820 | "expected return value 0xFF"); |
| 821 | if (overflow != 0) |
| 822 | return raiseTestError("test_long_long_and_overflow", |
| 823 | "overflow was set incorrectly"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 824 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 825 | num = PyLong_FromLongLong(LLONG_MAX); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | if (num == NULL) |
| 827 | return NULL; |
| 828 | overflow = 1234; |
| 829 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 830 | Py_DECREF(num); |
| 831 | if (value == -1 && PyErr_Occurred()) |
| 832 | return NULL; |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 833 | if (value != LLONG_MAX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | return raiseTestError("test_long_long_and_overflow", |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 835 | "expected return value LLONG_MAX"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | if (overflow != 0) |
| 837 | return raiseTestError("test_long_long_and_overflow", |
| 838 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 839 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 840 | num = PyLong_FromLongLong(LLONG_MIN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | if (num == NULL) |
| 842 | return NULL; |
| 843 | overflow = 0; |
| 844 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 845 | Py_DECREF(num); |
| 846 | if (value == -1 && PyErr_Occurred()) |
| 847 | return NULL; |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 848 | if (value != LLONG_MIN) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | return raiseTestError("test_long_long_and_overflow", |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 850 | "expected return value LLONG_MIN"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | if (overflow != 0) |
| 852 | return raiseTestError("test_long_long_and_overflow", |
| 853 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 854 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 855 | Py_RETURN_NONE; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 858 | /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that |
| 859 | non-integer arguments are handled correctly. It should be extended to |
| 860 | test overflow handling. |
| 861 | */ |
| 862 | |
| 863 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 864 | test_long_as_size_t(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 865 | { |
| 866 | size_t out_u; |
| 867 | Py_ssize_t out_s; |
| 868 | |
| 869 | Py_INCREF(Py_None); |
| 870 | |
| 871 | out_u = PyLong_AsSize_t(Py_None); |
| 872 | if (out_u != (size_t)-1 || !PyErr_Occurred()) |
| 873 | return raiseTestError("test_long_as_size_t", |
| 874 | "PyLong_AsSize_t(None) didn't complain"); |
| 875 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 876 | return raiseTestError("test_long_as_size_t", |
| 877 | "PyLong_AsSize_t(None) raised " |
| 878 | "something other than TypeError"); |
| 879 | PyErr_Clear(); |
| 880 | |
| 881 | out_s = PyLong_AsSsize_t(Py_None); |
| 882 | if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) |
| 883 | return raiseTestError("test_long_as_size_t", |
| 884 | "PyLong_AsSsize_t(None) didn't complain"); |
| 885 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 886 | return raiseTestError("test_long_as_size_t", |
| 887 | "PyLong_AsSsize_t(None) raised " |
| 888 | "something other than TypeError"); |
| 889 | PyErr_Clear(); |
| 890 | |
| 891 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 892 | return Py_None; |
| 893 | } |
| 894 | |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 895 | static PyObject * |
| 896 | test_long_as_unsigned_long_long_mask(PyObject *self, |
| 897 | PyObject *Py_UNUSED(ignored)) |
| 898 | { |
| 899 | unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL); |
| 900 | |
| 901 | if (res != (unsigned long long)-1 || !PyErr_Occurred()) { |
| 902 | return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 903 | "PyLong_AsUnsignedLongLongMask(NULL) didn't " |
| 904 | "complain"); |
| 905 | } |
| 906 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 907 | return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 908 | "PyLong_AsUnsignedLongLongMask(NULL) raised " |
| 909 | "something other than SystemError"); |
| 910 | } |
| 911 | PyErr_Clear(); |
| 912 | Py_RETURN_NONE; |
| 913 | } |
| 914 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 915 | /* Test the PyLong_AsDouble API. At present this just tests that |
| 916 | non-integer arguments are handled correctly. |
| 917 | */ |
| 918 | |
| 919 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 920 | test_long_as_double(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 921 | { |
| 922 | double out; |
| 923 | |
| 924 | Py_INCREF(Py_None); |
| 925 | |
| 926 | out = PyLong_AsDouble(Py_None); |
| 927 | if (out != -1.0 || !PyErr_Occurred()) |
| 928 | return raiseTestError("test_long_as_double", |
| 929 | "PyLong_AsDouble(None) didn't complain"); |
| 930 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 931 | return raiseTestError("test_long_as_double", |
| 932 | "PyLong_AsDouble(None) raised " |
| 933 | "something other than TypeError"); |
| 934 | PyErr_Clear(); |
| 935 | |
| 936 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 937 | return Py_None; |
| 938 | } |
| 939 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 940 | /* 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] | 941 | for both long and int arguments. The test may leak a little memory if |
| 942 | it fails. |
| 943 | */ |
| 944 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 945 | test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | PyObject *tuple, *num; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 948 | long long value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | tuple = PyTuple_New(1); |
| 951 | if (tuple == NULL) |
| 952 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | num = PyLong_FromLong(42); |
| 955 | if (num == NULL) |
| 956 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | value = -1; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 961 | if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 963 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | if (value != 42) |
| 965 | return raiseTestError("test_L_code", |
| 966 | "L code returned wrong value for long 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | Py_DECREF(num); |
| 969 | num = PyLong_FromLong(42); |
| 970 | if (num == NULL) |
| 971 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | value = -1; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 976 | if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 978 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | if (value != 42) |
| 980 | return raiseTestError("test_L_code", |
| 981 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 984 | Py_RETURN_NONE; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 987 | static PyObject * |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 988 | return_none(void *unused) |
| 989 | { |
| 990 | Py_RETURN_NONE; |
| 991 | } |
| 992 | |
| 993 | static PyObject * |
| 994 | raise_error(void *unused) |
| 995 | { |
| 996 | PyErr_SetNone(PyExc_ValueError); |
| 997 | return NULL; |
| 998 | } |
| 999 | |
| 1000 | static int |
| 1001 | test_buildvalue_N_error(const char *fmt) |
| 1002 | { |
| 1003 | PyObject *arg, *res; |
| 1004 | |
| 1005 | arg = PyList_New(0); |
| 1006 | if (arg == NULL) { |
| 1007 | return -1; |
| 1008 | } |
| 1009 | |
| 1010 | Py_INCREF(arg); |
| 1011 | res = Py_BuildValue(fmt, return_none, NULL, arg); |
| 1012 | if (res == NULL) { |
| 1013 | return -1; |
| 1014 | } |
| 1015 | Py_DECREF(res); |
| 1016 | if (Py_REFCNT(arg) != 1) { |
| 1017 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 1018 | "arg was not decrefed in successful " |
| 1019 | "Py_BuildValue(\"%s\")", fmt); |
| 1020 | return -1; |
| 1021 | } |
| 1022 | |
| 1023 | Py_INCREF(arg); |
| 1024 | res = Py_BuildValue(fmt, raise_error, NULL, arg); |
| 1025 | if (res != NULL || !PyErr_Occurred()) { |
| 1026 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 1027 | "Py_BuildValue(\"%s\") didn't complain", fmt); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | PyErr_Clear(); |
| 1031 | if (Py_REFCNT(arg) != 1) { |
| 1032 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 1033 | "arg was not decrefed in failed " |
| 1034 | "Py_BuildValue(\"%s\")", fmt); |
| 1035 | return -1; |
| 1036 | } |
| 1037 | Py_DECREF(arg); |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 1042 | test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 1043 | { |
| 1044 | PyObject *arg, *res; |
| 1045 | |
| 1046 | arg = PyList_New(0); |
| 1047 | if (arg == NULL) { |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | Py_INCREF(arg); |
| 1051 | res = Py_BuildValue("N", arg); |
| 1052 | if (res == NULL) { |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | if (res != arg) { |
| 1056 | return raiseTestError("test_buildvalue_N", |
| 1057 | "Py_BuildValue(\"N\") returned wrong result"); |
| 1058 | } |
| 1059 | if (Py_REFCNT(arg) != 2) { |
| 1060 | return raiseTestError("test_buildvalue_N", |
| 1061 | "arg was not decrefed in Py_BuildValue(\"N\")"); |
| 1062 | } |
| 1063 | Py_DECREF(res); |
| 1064 | Py_DECREF(arg); |
| 1065 | |
| 1066 | if (test_buildvalue_N_error("O&N") < 0) |
| 1067 | return NULL; |
| 1068 | if (test_buildvalue_N_error("(O&N)") < 0) |
| 1069 | return NULL; |
| 1070 | if (test_buildvalue_N_error("[O&N]") < 0) |
| 1071 | return NULL; |
| 1072 | if (test_buildvalue_N_error("{O&N}") < 0) |
| 1073 | return NULL; |
| 1074 | if (test_buildvalue_N_error("{()O&(())N}") < 0) |
| 1075 | return NULL; |
| 1076 | |
| 1077 | Py_RETURN_NONE; |
| 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | static PyObject * |
Hai Shi | a13b26c | 2020-11-11 04:53:46 +0800 | [diff] [blame] | 1082 | test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 1083 | { |
| 1084 | newfunc tp_new = PyType_GetSlot(&PyLong_Type, Py_tp_new); |
| 1085 | if (PyLong_Type.tp_new != tp_new) { |
| 1086 | PyErr_SetString(PyExc_AssertionError, "mismatch: tp_new of long"); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | reprfunc tp_repr = PyType_GetSlot(&PyLong_Type, Py_tp_repr); |
| 1091 | if (PyLong_Type.tp_repr != tp_repr) { |
| 1092 | PyErr_SetString(PyExc_AssertionError, "mismatch: tp_repr of long"); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | |
| 1096 | ternaryfunc tp_call = PyType_GetSlot(&PyLong_Type, Py_tp_call); |
| 1097 | if (tp_call != NULL) { |
| 1098 | PyErr_SetString(PyExc_AssertionError, "mismatch: tp_call of long"); |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | |
| 1102 | binaryfunc nb_add = PyType_GetSlot(&PyLong_Type, Py_nb_add); |
| 1103 | if (PyLong_Type.tp_as_number->nb_add != nb_add) { |
| 1104 | PyErr_SetString(PyExc_AssertionError, "mismatch: nb_add of long"); |
| 1105 | return NULL; |
| 1106 | } |
| 1107 | |
| 1108 | lenfunc mp_length = PyType_GetSlot(&PyLong_Type, Py_mp_length); |
| 1109 | if (mp_length != NULL) { |
| 1110 | PyErr_SetString(PyExc_AssertionError, "mismatch: mp_length of long"); |
| 1111 | return NULL; |
| 1112 | } |
| 1113 | |
| 1114 | void *over_value = PyType_GetSlot(&PyLong_Type, Py_bf_releasebuffer + 1); |
| 1115 | if (over_value != NULL) { |
| 1116 | PyErr_SetString(PyExc_AssertionError, "mismatch: max+1 of long"); |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | |
| 1120 | tp_new = PyType_GetSlot(&PyLong_Type, 0); |
| 1121 | if (tp_new != NULL) { |
| 1122 | PyErr_SetString(PyExc_AssertionError, "mismatch: slot 0 of long"); |
| 1123 | return NULL; |
| 1124 | } |
| 1125 | if (PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 1126 | // This is the right exception |
| 1127 | PyErr_Clear(); |
| 1128 | } |
| 1129 | else { |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | |
| 1133 | Py_RETURN_NONE; |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | static PyObject * |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 1138 | get_args(PyObject *self, PyObject *args) |
| 1139 | { |
| 1140 | if (args == NULL) { |
| 1141 | args = Py_None; |
| 1142 | } |
| 1143 | Py_INCREF(args); |
| 1144 | return args; |
| 1145 | } |
| 1146 | |
| 1147 | static PyObject * |
| 1148 | get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1149 | { |
| 1150 | if (kwargs == NULL) { |
| 1151 | kwargs = Py_None; |
| 1152 | } |
| 1153 | Py_INCREF(kwargs); |
| 1154 | return kwargs; |
| 1155 | } |
| 1156 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1157 | /* Test tuple argument processing */ |
| 1158 | static PyObject * |
| 1159 | getargs_tuple(PyObject *self, PyObject *args) |
| 1160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | int a, b, c; |
| 1162 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 1163 | return NULL; |
| 1164 | return Py_BuildValue("iii", a, b, c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1167 | /* test PyArg_ParseTupleAndKeywords */ |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1168 | static PyObject * |
| 1169 | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1172 | static const char fmt[] = "(ii)i|(i(ii))(iii)i"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | 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] | 1174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 1176 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 1177 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 1178 | return NULL; |
| 1179 | return Py_BuildValue("iiiiiiiiii", |
| 1180 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 1181 | 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] | 1182 | } |
| 1183 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1184 | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
| 1185 | static PyObject * |
| 1186 | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1187 | { |
| 1188 | static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
| 1189 | int required = -1; |
| 1190 | int optional = -1; |
| 1191 | int keyword_only = -1; |
| 1192 | |
| 1193 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
| 1194 | &required, &optional, &keyword_only)) |
| 1195 | return NULL; |
| 1196 | return Py_BuildValue("iii", required, optional, keyword_only); |
| 1197 | } |
| 1198 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1199 | /* test PyArg_ParseTupleAndKeywords positional-only arguments */ |
| 1200 | static PyObject * |
| 1201 | getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1202 | { |
| 1203 | static char *keywords[] = {"", "", "keyword", NULL}; |
| 1204 | int required = -1; |
| 1205 | int optional = -1; |
| 1206 | int keyword = -1; |
| 1207 | |
| 1208 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords, |
| 1209 | &required, &optional, &keyword)) |
| 1210 | return NULL; |
| 1211 | return Py_BuildValue("iii", required, optional, keyword); |
| 1212 | } |
| 1213 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1214 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 1215 | and return the result. |
| 1216 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1217 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1218 | getargs_b(PyObject *self, PyObject *args) |
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 | unsigned char value; |
| 1221 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 1222 | return NULL; |
| 1223 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1226 | static PyObject * |
| 1227 | getargs_B(PyObject *self, PyObject *args) |
| 1228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | unsigned char value; |
| 1230 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 1231 | return NULL; |
| 1232 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1236 | getargs_h(PyObject *self, PyObject *args) |
| 1237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | short value; |
| 1239 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 1240 | return NULL; |
| 1241 | return PyLong_FromLong((long)value); |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1245 | getargs_H(PyObject *self, PyObject *args) |
| 1246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | unsigned short value; |
| 1248 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 1249 | return NULL; |
| 1250 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | static PyObject * |
| 1254 | getargs_I(PyObject *self, PyObject *args) |
| 1255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | unsigned int value; |
| 1257 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 1258 | return NULL; |
| 1259 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | static PyObject * |
| 1263 | getargs_k(PyObject *self, PyObject *args) |
| 1264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | unsigned long value; |
| 1266 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 1267 | return NULL; |
| 1268 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | static PyObject * |
| 1272 | getargs_i(PyObject *self, PyObject *args) |
| 1273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | int value; |
| 1275 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 1276 | return NULL; |
| 1277 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1280 | static PyObject * |
| 1281 | getargs_l(PyObject *self, PyObject *args) |
| 1282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | long value; |
| 1284 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 1285 | return NULL; |
| 1286 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1289 | static PyObject * |
| 1290 | getargs_n(PyObject *self, PyObject *args) |
| 1291 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | Py_ssize_t value; |
| 1293 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 1294 | return NULL; |
| 1295 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 1298 | static PyObject * |
| 1299 | getargs_p(PyObject *self, PyObject *args) |
| 1300 | { |
| 1301 | int value; |
| 1302 | if (!PyArg_ParseTuple(args, "p", &value)) |
| 1303 | return NULL; |
| 1304 | return PyLong_FromLong(value); |
| 1305 | } |
| 1306 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1307 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1308 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1309 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1310 | long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 1312 | return NULL; |
| 1313 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1316 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1317 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1318 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1319 | unsigned long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 1321 | return NULL; |
| 1322 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1323 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1324 | |
| 1325 | /* This function not only tests the 'k' getargs code, but also the |
orenmn | 698845e | 2017-03-02 13:29:20 +0200 | [diff] [blame] | 1326 | PyLong_AsUnsignedLongMask() function. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1327 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1328 | test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | PyObject *tuple, *num; |
| 1331 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | tuple = PyTuple_New(1); |
| 1334 | if (tuple == NULL) |
| 1335 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 1338 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 1339 | if (num == NULL) |
| 1340 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | value = PyLong_AsUnsignedLongMask(num); |
| 1343 | if (value != ULONG_MAX) |
| 1344 | return raiseTestError("test_k_code", |
Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 1345 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1350 | if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1352 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | if (value != ULONG_MAX) |
| 1354 | return raiseTestError("test_k_code", |
| 1355 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | Py_DECREF(num); |
| 1358 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 1359 | if (num == NULL) |
| 1360 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | value = PyLong_AsUnsignedLongMask(num); |
| 1363 | if (value != (unsigned long)-0x42) |
| 1364 | return raiseTestError("test_k_code", |
orenmn | 698845e | 2017-03-02 13:29:20 +0200 | [diff] [blame] | 1365 | "PyLong_AsUnsignedLongMask() returned wrong " |
| 1366 | "value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1371 | if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1373 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | if (value != (unsigned long)-0x42) |
| 1375 | return raiseTestError("test_k_code", |
| 1376 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1379 | Py_RETURN_NONE; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1382 | static PyObject * |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 1383 | getargs_f(PyObject *self, PyObject *args) |
| 1384 | { |
| 1385 | float f; |
| 1386 | if (!PyArg_ParseTuple(args, "f", &f)) |
| 1387 | return NULL; |
| 1388 | return PyFloat_FromDouble(f); |
| 1389 | } |
| 1390 | |
| 1391 | static PyObject * |
| 1392 | getargs_d(PyObject *self, PyObject *args) |
| 1393 | { |
| 1394 | double d; |
| 1395 | if (!PyArg_ParseTuple(args, "d", &d)) |
| 1396 | return NULL; |
| 1397 | return PyFloat_FromDouble(d); |
| 1398 | } |
| 1399 | |
| 1400 | static PyObject * |
| 1401 | getargs_D(PyObject *self, PyObject *args) |
| 1402 | { |
| 1403 | Py_complex cval; |
| 1404 | if (!PyArg_ParseTuple(args, "D", &cval)) |
| 1405 | return NULL; |
| 1406 | return PyComplex_FromCComplex(cval); |
| 1407 | } |
| 1408 | |
| 1409 | static PyObject * |
| 1410 | getargs_S(PyObject *self, PyObject *args) |
| 1411 | { |
| 1412 | PyObject *obj; |
| 1413 | if (!PyArg_ParseTuple(args, "S", &obj)) |
| 1414 | return NULL; |
| 1415 | Py_INCREF(obj); |
| 1416 | return obj; |
| 1417 | } |
| 1418 | |
| 1419 | static PyObject * |
| 1420 | getargs_Y(PyObject *self, PyObject *args) |
| 1421 | { |
| 1422 | PyObject *obj; |
| 1423 | if (!PyArg_ParseTuple(args, "Y", &obj)) |
| 1424 | return NULL; |
| 1425 | Py_INCREF(obj); |
| 1426 | return obj; |
| 1427 | } |
| 1428 | |
| 1429 | static PyObject * |
| 1430 | getargs_U(PyObject *self, PyObject *args) |
| 1431 | { |
| 1432 | PyObject *obj; |
| 1433 | if (!PyArg_ParseTuple(args, "U", &obj)) |
| 1434 | return NULL; |
| 1435 | Py_INCREF(obj); |
| 1436 | return obj; |
| 1437 | } |
| 1438 | |
| 1439 | static PyObject * |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1440 | getargs_c(PyObject *self, PyObject *args) |
| 1441 | { |
| 1442 | char c; |
| 1443 | if (!PyArg_ParseTuple(args, "c", &c)) |
| 1444 | return NULL; |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1445 | return PyLong_FromLong((unsigned char)c); |
| 1446 | } |
| 1447 | |
| 1448 | static PyObject * |
| 1449 | getargs_C(PyObject *self, PyObject *args) |
| 1450 | { |
| 1451 | int c; |
| 1452 | if (!PyArg_ParseTuple(args, "C", &c)) |
| 1453 | return NULL; |
| 1454 | return PyLong_FromLong(c); |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | static PyObject * |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1458 | getargs_s(PyObject *self, PyObject *args) |
| 1459 | { |
| 1460 | char *str; |
| 1461 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1462 | return NULL; |
| 1463 | return PyBytes_FromString(str); |
| 1464 | } |
| 1465 | |
| 1466 | static PyObject * |
| 1467 | getargs_s_star(PyObject *self, PyObject *args) |
| 1468 | { |
| 1469 | Py_buffer buffer; |
| 1470 | PyObject *bytes; |
| 1471 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1472 | return NULL; |
| 1473 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1474 | PyBuffer_Release(&buffer); |
| 1475 | return bytes; |
| 1476 | } |
| 1477 | |
| 1478 | static PyObject * |
| 1479 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1480 | { |
| 1481 | char *str; |
| 1482 | Py_ssize_t size; |
| 1483 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1484 | return NULL; |
| 1485 | return PyBytes_FromStringAndSize(str, size); |
| 1486 | } |
| 1487 | |
| 1488 | static PyObject * |
| 1489 | getargs_z(PyObject *self, PyObject *args) |
| 1490 | { |
| 1491 | char *str; |
| 1492 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1493 | return NULL; |
| 1494 | if (str != NULL) |
| 1495 | return PyBytes_FromString(str); |
| 1496 | else |
| 1497 | Py_RETURN_NONE; |
| 1498 | } |
| 1499 | |
| 1500 | static PyObject * |
| 1501 | getargs_z_star(PyObject *self, PyObject *args) |
| 1502 | { |
| 1503 | Py_buffer buffer; |
| 1504 | PyObject *bytes; |
| 1505 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1506 | return NULL; |
| 1507 | if (buffer.buf != NULL) |
| 1508 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1509 | else { |
| 1510 | Py_INCREF(Py_None); |
| 1511 | bytes = Py_None; |
| 1512 | } |
| 1513 | PyBuffer_Release(&buffer); |
| 1514 | return bytes; |
| 1515 | } |
| 1516 | |
| 1517 | static PyObject * |
| 1518 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1519 | { |
| 1520 | char *str; |
| 1521 | Py_ssize_t size; |
| 1522 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1523 | return NULL; |
| 1524 | if (str != NULL) |
| 1525 | return PyBytes_FromStringAndSize(str, size); |
| 1526 | else |
| 1527 | Py_RETURN_NONE; |
| 1528 | } |
| 1529 | |
| 1530 | static PyObject * |
| 1531 | getargs_y(PyObject *self, PyObject *args) |
| 1532 | { |
| 1533 | char *str; |
| 1534 | if (!PyArg_ParseTuple(args, "y", &str)) |
| 1535 | return NULL; |
| 1536 | return PyBytes_FromString(str); |
| 1537 | } |
| 1538 | |
| 1539 | static PyObject * |
| 1540 | getargs_y_star(PyObject *self, PyObject *args) |
| 1541 | { |
| 1542 | Py_buffer buffer; |
| 1543 | PyObject *bytes; |
| 1544 | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 1545 | return NULL; |
| 1546 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1547 | PyBuffer_Release(&buffer); |
| 1548 | return bytes; |
| 1549 | } |
| 1550 | |
| 1551 | static PyObject * |
| 1552 | getargs_y_hash(PyObject *self, PyObject *args) |
| 1553 | { |
| 1554 | char *str; |
| 1555 | Py_ssize_t size; |
| 1556 | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
| 1557 | return NULL; |
| 1558 | return PyBytes_FromStringAndSize(str, size); |
| 1559 | } |
| 1560 | |
| 1561 | static PyObject * |
| 1562 | getargs_u(PyObject *self, PyObject *args) |
| 1563 | { |
| 1564 | Py_UNICODE *str; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1565 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1566 | return NULL; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1567 | return PyUnicode_FromWideChar(str, -1); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | static PyObject * |
| 1571 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1572 | { |
| 1573 | Py_UNICODE *str; |
| 1574 | Py_ssize_t size; |
| 1575 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1576 | return NULL; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1577 | return PyUnicode_FromWideChar(str, size); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | static PyObject * |
| 1581 | getargs_Z(PyObject *self, PyObject *args) |
| 1582 | { |
| 1583 | Py_UNICODE *str; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1584 | if (!PyArg_ParseTuple(args, "Z", &str)) |
| 1585 | return NULL; |
| 1586 | if (str != NULL) { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1587 | return PyUnicode_FromWideChar(str, -1); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1588 | } else |
| 1589 | Py_RETURN_NONE; |
| 1590 | } |
| 1591 | |
| 1592 | static PyObject * |
| 1593 | getargs_Z_hash(PyObject *self, PyObject *args) |
| 1594 | { |
| 1595 | Py_UNICODE *str; |
| 1596 | Py_ssize_t size; |
| 1597 | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
| 1598 | return NULL; |
| 1599 | if (str != NULL) |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1600 | return PyUnicode_FromWideChar(str, size); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1601 | else |
| 1602 | Py_RETURN_NONE; |
| 1603 | } |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1604 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1605 | static PyObject * |
| 1606 | getargs_es(PyObject *self, PyObject *args) |
| 1607 | { |
| 1608 | PyObject *arg, *result; |
| 1609 | const char *encoding = NULL; |
| 1610 | char *str; |
| 1611 | |
| 1612 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1613 | return NULL; |
| 1614 | if (!PyArg_Parse(arg, "es", encoding, &str)) |
| 1615 | return NULL; |
| 1616 | result = PyBytes_FromString(str); |
| 1617 | PyMem_Free(str); |
| 1618 | return result; |
| 1619 | } |
| 1620 | |
| 1621 | static PyObject * |
| 1622 | getargs_et(PyObject *self, PyObject *args) |
| 1623 | { |
| 1624 | PyObject *arg, *result; |
| 1625 | const char *encoding = NULL; |
| 1626 | char *str; |
| 1627 | |
| 1628 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1629 | return NULL; |
| 1630 | if (!PyArg_Parse(arg, "et", encoding, &str)) |
| 1631 | return NULL; |
| 1632 | result = PyBytes_FromString(str); |
| 1633 | PyMem_Free(str); |
| 1634 | return result; |
| 1635 | } |
| 1636 | |
| 1637 | static PyObject * |
| 1638 | getargs_es_hash(PyObject *self, PyObject *args) |
| 1639 | { |
| 1640 | PyObject *arg, *result; |
| 1641 | const char *encoding = NULL; |
| 1642 | PyByteArrayObject *buffer = NULL; |
| 1643 | char *str = NULL; |
| 1644 | Py_ssize_t size; |
| 1645 | |
| 1646 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1647 | return NULL; |
| 1648 | if (buffer != NULL) { |
| 1649 | str = PyByteArray_AS_STRING(buffer); |
| 1650 | size = PyByteArray_GET_SIZE(buffer); |
| 1651 | } |
| 1652 | if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) |
| 1653 | return NULL; |
| 1654 | result = PyBytes_FromStringAndSize(str, size); |
| 1655 | if (buffer == NULL) |
| 1656 | PyMem_Free(str); |
| 1657 | return result; |
| 1658 | } |
| 1659 | |
| 1660 | static PyObject * |
| 1661 | getargs_et_hash(PyObject *self, PyObject *args) |
| 1662 | { |
| 1663 | PyObject *arg, *result; |
| 1664 | const char *encoding = NULL; |
| 1665 | PyByteArrayObject *buffer = NULL; |
| 1666 | char *str = NULL; |
| 1667 | Py_ssize_t size; |
| 1668 | |
| 1669 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1670 | return NULL; |
| 1671 | if (buffer != NULL) { |
| 1672 | str = PyByteArray_AS_STRING(buffer); |
| 1673 | size = PyByteArray_GET_SIZE(buffer); |
| 1674 | } |
| 1675 | if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) |
| 1676 | return NULL; |
| 1677 | result = PyBytes_FromStringAndSize(str, size); |
| 1678 | if (buffer == NULL) |
| 1679 | PyMem_Free(str); |
| 1680 | return result; |
| 1681 | } |
| 1682 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1683 | /* Test the s and z codes for PyArg_ParseTuple. |
| 1684 | */ |
| 1685 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1686 | test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1687 | { |
| 1688 | /* Unicode strings should be accepted */ |
| 1689 | PyObject *tuple, *obj; |
| 1690 | char *value; |
| 1691 | |
| 1692 | tuple = PyTuple_New(1); |
| 1693 | if (tuple == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1695 | |
| 1696 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1698 | if (obj == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1700 | |
| 1701 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1702 | |
| 1703 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1705 | */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1706 | if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) { |
| 1707 | return NULL; |
| 1708 | } |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1709 | |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1710 | if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) { |
| 1711 | return NULL; |
| 1712 | } |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1713 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1714 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1715 | Py_RETURN_NONE; |
| 1716 | } |
| 1717 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1718 | static PyObject * |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1719 | parse_tuple_and_keywords(PyObject *self, PyObject *args) |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1720 | { |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1721 | PyObject *sub_args; |
| 1722 | PyObject *sub_kwargs; |
Serhiy Storchaka | 5f161fd | 2017-05-04 00:03:23 +0300 | [diff] [blame] | 1723 | const char *sub_format; |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1724 | PyObject *sub_keywords; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1725 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1726 | Py_ssize_t i, size; |
| 1727 | char *keywords[8 + 1]; /* space for NULL at end */ |
| 1728 | PyObject *o; |
| 1729 | PyObject *converted[8]; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1730 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1731 | int result; |
| 1732 | PyObject *return_value = NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1733 | |
Larry Hastings | 22701e8 | 2012-08-08 14:52:22 -0700 | [diff] [blame] | 1734 | double buffers[8][4]; /* double ensures alignment where necessary */ |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1735 | |
Serhiy Storchaka | 5f161fd | 2017-05-04 00:03:23 +0300 | [diff] [blame] | 1736 | if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords", |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1737 | &sub_args, &sub_kwargs, |
| 1738 | &sub_format, &sub_keywords)) |
| 1739 | return NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1740 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1741 | if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
| 1742 | PyErr_SetString(PyExc_ValueError, |
| 1743 | "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
| 1744 | return NULL; |
| 1745 | } |
| 1746 | |
| 1747 | memset(buffers, 0, sizeof(buffers)); |
| 1748 | memset(converted, 0, sizeof(converted)); |
| 1749 | memset(keywords, 0, sizeof(keywords)); |
| 1750 | |
| 1751 | size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 1752 | if (size > 8) { |
| 1753 | PyErr_SetString(PyExc_ValueError, |
| 1754 | "parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 1755 | goto exit; |
| 1756 | } |
| 1757 | |
| 1758 | for (i = 0; i < size; i++) { |
| 1759 | o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 1760 | if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { |
| 1761 | PyErr_Format(PyExc_ValueError, |
Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 1762 | "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] | 1763 | goto exit; |
| 1764 | } |
| 1765 | keywords[i] = PyBytes_AS_STRING(converted[i]); |
| 1766 | } |
| 1767 | |
| 1768 | result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 1769 | sub_format, keywords, |
| 1770 | buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 1771 | buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 1772 | |
| 1773 | if (result) { |
| 1774 | return_value = Py_None; |
| 1775 | Py_INCREF(Py_None); |
| 1776 | } |
| 1777 | |
| 1778 | exit: |
| 1779 | size = sizeof(converted) / sizeof(converted[0]); |
| 1780 | for (i = 0; i < size; i++) { |
| 1781 | Py_XDECREF(converted[i]); |
| 1782 | } |
| 1783 | return return_value; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1786 | static volatile int x; |
| 1787 | |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1788 | #if USE_UNICODE_WCHAR_CACHE |
Inada Naoki | 2c4928d | 2020-06-17 20:09:44 +0900 | [diff] [blame] | 1789 | /* Ignore use of deprecated APIs */ |
| 1790 | _Py_COMP_DIAG_PUSH |
| 1791 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 1792 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1793 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1794 | of an error. |
| 1795 | */ |
| 1796 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1797 | test_u_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | PyObject *tuple, *obj; |
| 1800 | Py_UNICODE *value; |
| 1801 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1804 | /* Just use the macro and check that it compiles */ |
| 1805 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1807 | tuple = PyTuple_New(1); |
| 1808 | if (tuple == NULL) |
| 1809 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1812 | "ascii", NULL); |
| 1813 | if (obj == NULL) |
| 1814 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1819 | if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1821 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1823 | return raiseTestError("test_u_code", |
| 1824 | "u code returned wrong value for u'test'"); |
| 1825 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1826 | if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1828 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1830 | len != PyUnicode_GET_SIZE(obj)) |
| 1831 | return raiseTestError("test_u_code", |
| 1832 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1835 | Py_RETURN_NONE; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1838 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1839 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1840 | test_Z_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | PyObject *tuple, *obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1843 | const Py_UNICODE *value1, *value2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | tuple = PyTuple_New(2); |
| 1847 | if (tuple == NULL) |
| 1848 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | obj = PyUnicode_FromString("test"); |
| 1851 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1852 | Py_INCREF(Py_None); |
| 1853 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | /* swap values on purpose */ |
| 1856 | value1 = NULL; |
| 1857 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | /* Test Z for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1860 | if (!PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1862 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1864 | return raiseTestError("test_Z_code", |
| 1865 | "Z code returned wrong value for 'test'"); |
| 1866 | if (value2 != NULL) |
| 1867 | return raiseTestError("test_Z_code", |
| 1868 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | value1 = NULL; |
| 1871 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1872 | len1 = -1; |
| 1873 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | /* Test Z# for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1876 | if (!PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1877 | &value2, &len2)) |
| 1878 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1880 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1881 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1882 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1883 | return raiseTestError("test_Z_code", |
| 1884 | "Z# code returned wrong values for 'test'"); |
| 1885 | if (value2 != NULL || |
| 1886 | len2 != 0) |
| 1887 | return raiseTestError("test_Z_code", |
| 1888 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | Py_DECREF(tuple); |
| 1891 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1892 | } |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1893 | _Py_COMP_DIAG_POP |
| 1894 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1895 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1896 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1897 | test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1898 | { |
| 1899 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1901 | size_t wtextlen = 1; |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1902 | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1903 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1904 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1905 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1906 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1910 | if (wide == NULL) |
| 1911 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1914 | if (utf8 == NULL) { |
| 1915 | Py_DECREF(wide); |
| 1916 | return NULL; |
| 1917 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1918 | |
Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1919 | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | Py_DECREF(wide); |
| 1921 | Py_DECREF(utf8); |
| 1922 | return raiseTestError("test_widechar", |
| 1923 | "wide string and utf8 string " |
| 1924 | "have different length"); |
| 1925 | } |
| 1926 | if (PyUnicode_Compare(wide, utf8)) { |
| 1927 | Py_DECREF(wide); |
| 1928 | Py_DECREF(utf8); |
| 1929 | if (PyErr_Occurred()) |
| 1930 | return NULL; |
| 1931 | return raiseTestError("test_widechar", |
| 1932 | "wide string and utf8 string " |
| 1933 | "are different"); |
| 1934 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1936 | Py_DECREF(wide); |
| 1937 | Py_DECREF(utf8); |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1938 | |
| 1939 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1940 | wide = PyUnicode_FromWideChar(invalid, 1); |
| 1941 | if (wide == NULL) |
| 1942 | PyErr_Clear(); |
| 1943 | else |
| 1944 | return raiseTestError("test_widechar", |
| 1945 | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
| 1946 | |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1947 | #if USE_UNICODE_WCHAR_CACHE |
| 1948 | /* Ignore use of deprecated APIs */ |
| 1949 | _Py_COMP_DIAG_PUSH |
| 1950 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1951 | wide = PyUnicode_FromUnicode(invalid, 1); |
| 1952 | if (wide == NULL) |
| 1953 | PyErr_Clear(); |
| 1954 | else |
| 1955 | return raiseTestError("test_widechar", |
| 1956 | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1957 | |
| 1958 | wide = PyUnicode_FromUnicode(NULL, 1); |
| 1959 | if (wide == NULL) |
| 1960 | return NULL; |
| 1961 | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1962 | if (_PyUnicode_Ready(wide) < 0) { |
| 1963 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1964 | PyErr_Clear(); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1965 | } |
| 1966 | else { |
| 1967 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1968 | return raiseTestError("test_widechar", |
| 1969 | "PyUnicode_Ready() didn't fail"); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1970 | } |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 1971 | _Py_COMP_DIAG_POP |
| 1972 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1973 | #endif |
| 1974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1979 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1980 | { |
| 1981 | PyObject *unicode, *result; |
| 1982 | Py_ssize_t buflen, size; |
| 1983 | wchar_t *buffer; |
| 1984 | |
| 1985 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1986 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1987 | buffer = PyMem_New(wchar_t, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1988 | if (buffer == NULL) |
| 1989 | return PyErr_NoMemory(); |
| 1990 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1991 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1992 | if (size == -1) { |
| 1993 | PyMem_Free(buffer); |
| 1994 | return NULL; |
| 1995 | } |
| 1996 | |
| 1997 | if (size < buflen) |
| 1998 | buflen = size + 1; |
| 1999 | else |
| 2000 | buflen = size; |
| 2001 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 2002 | PyMem_Free(buffer); |
| 2003 | if (result == NULL) |
| 2004 | return NULL; |
| 2005 | |
| 2006 | return Py_BuildValue("(Nn)", result, size); |
| 2007 | } |
| 2008 | |
| 2009 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2010 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2011 | { |
| 2012 | PyObject *unicode, *result; |
| 2013 | Py_ssize_t size; |
| 2014 | wchar_t *buffer; |
| 2015 | |
| 2016 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 2017 | return NULL; |
| 2018 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 2019 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2020 | if (buffer == NULL) |
| 2021 | return NULL; |
| 2022 | |
| 2023 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 2024 | PyMem_Free(buffer); |
| 2025 | if (result == NULL) |
| 2026 | return NULL; |
| 2027 | return Py_BuildValue("(Nn)", result, size); |
| 2028 | } |
| 2029 | |
| 2030 | static PyObject * |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 2031 | unicode_asucs4(PyObject *self, PyObject *args) |
| 2032 | { |
| 2033 | PyObject *unicode, *result; |
| 2034 | Py_UCS4 *buffer; |
| 2035 | int copy_null; |
| 2036 | Py_ssize_t str_len, buf_len; |
| 2037 | |
| 2038 | if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, ©_null)) { |
| 2039 | return NULL; |
| 2040 | } |
| 2041 | |
| 2042 | buf_len = str_len + 1; |
| 2043 | buffer = PyMem_NEW(Py_UCS4, buf_len); |
| 2044 | if (buffer == NULL) { |
| 2045 | return PyErr_NoMemory(); |
| 2046 | } |
| 2047 | memset(buffer, 0, sizeof(Py_UCS4)*buf_len); |
| 2048 | buffer[str_len] = 0xffffU; |
| 2049 | |
| 2050 | if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) { |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 2051 | PyMem_Free(buffer); |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 2052 | return NULL; |
| 2053 | } |
| 2054 | |
| 2055 | result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len); |
Victor Stinner | 00d7abd | 2020-12-01 09:56:42 +0100 | [diff] [blame] | 2056 | PyMem_Free(buffer); |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 2057 | return result; |
| 2058 | } |
| 2059 | |
| 2060 | static PyObject * |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 2061 | unicode_asutf8(PyObject *self, PyObject *args) |
| 2062 | { |
| 2063 | PyObject *unicode; |
| 2064 | const char *buffer; |
| 2065 | |
| 2066 | if (!PyArg_ParseTuple(args, "U", &unicode)) { |
| 2067 | return NULL; |
| 2068 | } |
| 2069 | |
| 2070 | buffer = PyUnicode_AsUTF8(unicode); |
| 2071 | if (buffer == NULL) { |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | |
| 2075 | return PyBytes_FromString(buffer); |
| 2076 | } |
| 2077 | |
| 2078 | static PyObject * |
| 2079 | unicode_asutf8andsize(PyObject *self, PyObject *args) |
| 2080 | { |
| 2081 | PyObject *unicode, *result; |
| 2082 | const char *buffer; |
| 2083 | Py_ssize_t utf8_len; |
| 2084 | |
| 2085 | if(!PyArg_ParseTuple(args, "U", &unicode)) { |
| 2086 | return NULL; |
| 2087 | } |
| 2088 | |
Victor Stinner | aca8c40 | 2019-09-30 21:14:26 +0200 | [diff] [blame] | 2089 | buffer = PyUnicode_AsUTF8AndSize(unicode, &utf8_len); |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 2090 | if (buffer == NULL) { |
| 2091 | return NULL; |
| 2092 | } |
| 2093 | |
| 2094 | result = PyBytes_FromString(buffer); |
| 2095 | if (result == NULL) { |
| 2096 | return NULL; |
| 2097 | } |
| 2098 | |
| 2099 | return Py_BuildValue("(Nn)", result, utf8_len); |
| 2100 | } |
| 2101 | |
| 2102 | static PyObject * |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 2103 | unicode_findchar(PyObject *self, PyObject *args) |
| 2104 | { |
| 2105 | PyObject *str; |
| 2106 | int direction; |
| 2107 | unsigned int ch; |
| 2108 | Py_ssize_t result; |
| 2109 | Py_ssize_t start, end; |
| 2110 | |
| 2111 | if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch, |
| 2112 | &start, &end, &direction)) { |
| 2113 | return NULL; |
| 2114 | } |
| 2115 | |
| 2116 | result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction); |
| 2117 | if (result == -2) |
| 2118 | return NULL; |
| 2119 | else |
| 2120 | return PyLong_FromSsize_t(result); |
| 2121 | } |
| 2122 | |
| 2123 | static PyObject * |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 2124 | unicode_copycharacters(PyObject *self, PyObject *args) |
| 2125 | { |
| 2126 | PyObject *from, *to, *to_copy; |
| 2127 | Py_ssize_t from_start, to_start, how_many, copied; |
| 2128 | |
| 2129 | if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start, |
| 2130 | &from, &from_start, &how_many)) { |
| 2131 | return NULL; |
| 2132 | } |
| 2133 | |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 2134 | if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to), |
| 2135 | PyUnicode_MAX_CHAR_VALUE(to)))) { |
| 2136 | return NULL; |
| 2137 | } |
| 2138 | if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) { |
| 2139 | Py_DECREF(to_copy); |
| 2140 | return NULL; |
| 2141 | } |
| 2142 | |
| 2143 | if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from, |
| 2144 | from_start, how_many)) < 0) { |
| 2145 | Py_DECREF(to_copy); |
| 2146 | return NULL; |
| 2147 | } |
| 2148 | |
| 2149 | return Py_BuildValue("(Nn)", to_copy, copied); |
| 2150 | } |
| 2151 | |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 2152 | #if USE_UNICODE_WCHAR_CACHE |
Inada Naoki | 13c90e8 | 2020-07-05 11:01:54 +0900 | [diff] [blame] | 2153 | /* Ignore use of deprecated APIs */ |
| 2154 | _Py_COMP_DIAG_PUSH |
| 2155 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 2156 | |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 2157 | static PyObject * |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2158 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 2159 | { |
| 2160 | Py_UNICODE *unicode; |
| 2161 | Py_ssize_t length; |
| 2162 | char *errors = NULL; |
| 2163 | PyObject *decimal; |
| 2164 | Py_ssize_t decimal_length, new_length; |
| 2165 | int res; |
| 2166 | |
| 2167 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 2168 | return NULL; |
| 2169 | |
| 2170 | decimal_length = length * 7; /* len('€') */ |
| 2171 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 2172 | if (decimal == NULL) |
| 2173 | return NULL; |
| 2174 | |
| 2175 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 2176 | PyBytes_AS_STRING(decimal), |
| 2177 | errors); |
| 2178 | if (res < 0) { |
| 2179 | Py_DECREF(decimal); |
| 2180 | return NULL; |
| 2181 | } |
| 2182 | |
| 2183 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 2184 | assert(new_length <= decimal_length); |
| 2185 | res = _PyBytes_Resize(&decimal, new_length); |
| 2186 | if (res < 0) |
| 2187 | return NULL; |
| 2188 | |
| 2189 | return decimal; |
| 2190 | } |
| 2191 | |
| 2192 | static PyObject * |
| 2193 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 2194 | { |
| 2195 | Py_UNICODE *unicode; |
| 2196 | Py_ssize_t length; |
| 2197 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 2198 | return NULL; |
| 2199 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 2200 | } |
| 2201 | |
| 2202 | static PyObject * |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 2203 | unicode_legacy_string(PyObject *self, PyObject *args) |
| 2204 | { |
| 2205 | Py_UNICODE *data; |
| 2206 | Py_ssize_t len; |
| 2207 | PyObject *u; |
| 2208 | |
| 2209 | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
| 2210 | return NULL; |
| 2211 | |
| 2212 | u = PyUnicode_FromUnicode(NULL, len); |
| 2213 | if (u == NULL) |
| 2214 | return NULL; |
| 2215 | |
| 2216 | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
| 2217 | |
| 2218 | if (len > 0) { /* The empty string is always ready. */ |
| 2219 | assert(!PyUnicode_IS_READY(u)); |
| 2220 | } |
| 2221 | |
| 2222 | return u; |
| 2223 | } |
Inada Naoki | 2c4928d | 2020-06-17 20:09:44 +0900 | [diff] [blame] | 2224 | _Py_COMP_DIAG_POP |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 2225 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 2226 | |
| 2227 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2228 | getargs_w_star(PyObject *self, PyObject *args) |
| 2229 | { |
| 2230 | Py_buffer buffer; |
| 2231 | PyObject *result; |
| 2232 | char *str; |
| 2233 | |
| 2234 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 2235 | return NULL; |
| 2236 | |
| 2237 | if (2 <= buffer.len) { |
| 2238 | str = buffer.buf; |
| 2239 | str[0] = '['; |
| 2240 | str[buffer.len-1] = ']'; |
| 2241 | } |
| 2242 | |
| 2243 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 2244 | PyBuffer_Release(&buffer); |
| 2245 | return result; |
| 2246 | } |
| 2247 | |
| 2248 | |
| 2249 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2250 | test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 2253 | PyObject *tuple, *dict = NULL; |
| 2254 | static char *kwlist[] = {NULL}; |
| 2255 | int result; |
| 2256 | tuple = PyTuple_New(0); |
| 2257 | if (!tuple) |
| 2258 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2259 | if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2260 | goto done; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2261 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | dict = PyDict_New(); |
| 2263 | if (!dict) |
| 2264 | goto done; |
| 2265 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2266 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | Py_DECREF(tuple); |
| 2268 | Py_XDECREF(dict); |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2269 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2271 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | else { |
| 2273 | Py_RETURN_NONE; |
| 2274 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2278 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2279 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | const char *encoding, *errors = NULL; |
| 2281 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 2282 | &encoding, &errors)) |
| 2283 | return NULL; |
| 2284 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2287 | static PyObject * |
| 2288 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | const char *encoding, *errors = NULL; |
| 2291 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 2292 | &encoding, &errors)) |
| 2293 | return NULL; |
| 2294 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 2297 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 2298 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2299 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2300 | test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | struct triple { |
| 2303 | long input; |
| 2304 | size_t nbits; |
| 2305 | int sign; |
| 2306 | } testcases[] = {{0, 0, 0}, |
| 2307 | {1L, 1, 1}, |
| 2308 | {-1L, 1, -1}, |
| 2309 | {2L, 2, 1}, |
| 2310 | {-2L, 2, -1}, |
| 2311 | {3L, 2, 1}, |
| 2312 | {-3L, 2, -1}, |
| 2313 | {4L, 3, 1}, |
| 2314 | {-4L, 3, -1}, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2315 | {0x7fffL, 15, 1}, /* one Python int digit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2316 | {-0x7fffL, 15, -1}, |
| 2317 | {0xffffL, 16, 1}, |
| 2318 | {-0xffffL, 16, -1}, |
| 2319 | {0xfffffffL, 28, 1}, |
| 2320 | {-0xfffffffL, 28, -1}}; |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 2321 | size_t i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2322 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2323 | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2324 | size_t nbits; |
| 2325 | int sign; |
| 2326 | PyObject *plong; |
| 2327 | |
| 2328 | plong = PyLong_FromLong(testcases[i].input); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2329 | if (plong == NULL) |
| 2330 | return NULL; |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2331 | nbits = _PyLong_NumBits(plong); |
| 2332 | sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | Py_DECREF(plong); |
| 2335 | if (nbits != testcases[i].nbits) |
| 2336 | return raiseTestError("test_long_numbits", |
| 2337 | "wrong result for _PyLong_NumBits"); |
| 2338 | if (sign != testcases[i].sign) |
| 2339 | return raiseTestError("test_long_numbits", |
| 2340 | "wrong result for _PyLong_Sign"); |
| 2341 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2342 | Py_RETURN_NONE; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 2345 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2346 | |
| 2347 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2348 | test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 2351 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 2352 | Py_XDECREF(o1); |
| 2353 | Py_XDECREF(o2); |
| 2354 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2357 | static PyObject * |
| 2358 | raise_exception(PyObject *self, PyObject *args) |
| 2359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | PyObject *exc; |
| 2361 | PyObject *exc_args, *v; |
| 2362 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2364 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 2365 | &exc, &num_args)) |
| 2366 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | exc_args = PyTuple_New(num_args); |
| 2369 | if (exc_args == NULL) |
| 2370 | return NULL; |
| 2371 | for (i = 0; i < num_args; ++i) { |
| 2372 | v = PyLong_FromLong(i); |
| 2373 | if (v == NULL) { |
| 2374 | Py_DECREF(exc_args); |
| 2375 | return NULL; |
| 2376 | } |
| 2377 | PyTuple_SET_ITEM(exc_args, i, v); |
| 2378 | } |
| 2379 | PyErr_SetObject(exc, exc_args); |
| 2380 | Py_DECREF(exc_args); |
| 2381 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2382 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2383 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2384 | static PyObject * |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 2385 | set_errno(PyObject *self, PyObject *args) |
| 2386 | { |
| 2387 | int new_errno; |
| 2388 | |
| 2389 | if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
| 2390 | return NULL; |
| 2391 | |
| 2392 | errno = new_errno; |
| 2393 | Py_RETURN_NONE; |
| 2394 | } |
| 2395 | |
| 2396 | static PyObject * |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2397 | test_set_exc_info(PyObject *self, PyObject *args) |
| 2398 | { |
| 2399 | PyObject *orig_exc; |
| 2400 | PyObject *new_type, *new_value, *new_tb; |
| 2401 | PyObject *type, *value, *tb; |
| 2402 | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
| 2403 | &new_type, &new_value, &new_tb)) |
| 2404 | return NULL; |
| 2405 | |
| 2406 | PyErr_GetExcInfo(&type, &value, &tb); |
| 2407 | |
| 2408 | Py_INCREF(new_type); |
| 2409 | Py_INCREF(new_value); |
| 2410 | Py_INCREF(new_tb); |
| 2411 | PyErr_SetExcInfo(new_type, new_value, new_tb); |
| 2412 | |
| 2413 | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
| 2414 | Py_XDECREF(type); |
| 2415 | Py_XDECREF(value); |
| 2416 | Py_XDECREF(tb); |
| 2417 | return orig_exc; |
| 2418 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2419 | |
| 2420 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2421 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2422 | static PyObject * |
| 2423 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | if (PyDateTimeAPI) { |
| 2425 | if (test_run_counter) { |
| 2426 | /* Probably regrtest.py -R */ |
| 2427 | Py_RETURN_NONE; |
| 2428 | } |
| 2429 | else { |
| 2430 | PyErr_SetString(PyExc_AssertionError, |
| 2431 | "PyDateTime_CAPI somehow initialized"); |
| 2432 | return NULL; |
| 2433 | } |
| 2434 | } |
| 2435 | test_run_counter++; |
| 2436 | PyDateTime_IMPORT; |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2438 | if (PyDateTimeAPI) |
| 2439 | Py_RETURN_NONE; |
| 2440 | else |
| 2441 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2444 | /* Functions exposing the C API type checking for testing */ |
| 2445 | #define MAKE_DATETIME_CHECK_FUNC(check_method, exact_method) \ |
| 2446 | PyObject *obj; \ |
| 2447 | int exact = 0; \ |
| 2448 | if (!PyArg_ParseTuple(args, "O|p", &obj, &exact)) { \ |
| 2449 | return NULL; \ |
| 2450 | } \ |
| 2451 | int rv = exact?exact_method(obj):check_method(obj); \ |
| 2452 | if (rv) { \ |
| 2453 | Py_RETURN_TRUE; \ |
| 2454 | } else { \ |
| 2455 | Py_RETURN_FALSE; \ |
| 2456 | } |
| 2457 | |
| 2458 | static PyObject * |
| 2459 | datetime_check_date(PyObject *self, PyObject *args) { |
| 2460 | MAKE_DATETIME_CHECK_FUNC(PyDate_Check, PyDate_CheckExact) |
| 2461 | } |
| 2462 | |
| 2463 | static PyObject * |
| 2464 | datetime_check_time(PyObject *self, PyObject *args) { |
| 2465 | MAKE_DATETIME_CHECK_FUNC(PyTime_Check, PyTime_CheckExact) |
| 2466 | } |
| 2467 | |
| 2468 | static PyObject * |
| 2469 | datetime_check_datetime(PyObject *self, PyObject *args) { |
| 2470 | MAKE_DATETIME_CHECK_FUNC(PyDateTime_Check, PyDateTime_CheckExact) |
| 2471 | } |
| 2472 | |
| 2473 | static PyObject * |
| 2474 | datetime_check_delta(PyObject *self, PyObject *args) { |
| 2475 | MAKE_DATETIME_CHECK_FUNC(PyDelta_Check, PyDelta_CheckExact) |
| 2476 | } |
| 2477 | |
| 2478 | static PyObject * |
| 2479 | datetime_check_tzinfo(PyObject *self, PyObject *args) { |
| 2480 | MAKE_DATETIME_CHECK_FUNC(PyTZInfo_Check, PyTZInfo_CheckExact) |
| 2481 | } |
| 2482 | |
| 2483 | |
| 2484 | /* Makes three variations on timezone representing UTC-5: |
| 2485 | 1. timezone with offset and name from PyDateTimeAPI |
| 2486 | 2. timezone with offset and name from PyTimeZone_FromOffsetAndName |
| 2487 | 3. timezone with offset (no name) from PyTimeZone_FromOffset |
| 2488 | */ |
| 2489 | static PyObject * |
| 2490 | make_timezones_capi(PyObject *self, PyObject *args) { |
| 2491 | PyObject *offset = PyDelta_FromDSU(0, -18000, 0); |
| 2492 | PyObject *name = PyUnicode_FromString("EST"); |
| 2493 | |
| 2494 | PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name); |
| 2495 | PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name); |
| 2496 | PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset); |
| 2497 | |
| 2498 | Py_DecRef(offset); |
| 2499 | Py_DecRef(name); |
| 2500 | |
| 2501 | PyObject *rv = PyTuple_New(3); |
| 2502 | |
| 2503 | PyTuple_SET_ITEM(rv, 0, est_zone_capi); |
| 2504 | PyTuple_SET_ITEM(rv, 1, est_zone_macro); |
| 2505 | PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname); |
| 2506 | |
| 2507 | return rv; |
| 2508 | } |
| 2509 | |
| 2510 | static PyObject * |
Paul Ganssle | a049f57 | 2018-02-22 15:15:32 -0500 | [diff] [blame] | 2511 | get_timezones_offset_zero(PyObject *self, PyObject *args) { |
| 2512 | PyObject *offset = PyDelta_FromDSU(0, 0, 0); |
| 2513 | PyObject *name = PyUnicode_FromString(""); |
| 2514 | |
| 2515 | // These two should return the UTC singleton |
| 2516 | PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset); |
| 2517 | PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL); |
| 2518 | |
| 2519 | // This one will return +00:00 zone, but not the UTC singleton |
| 2520 | PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name); |
| 2521 | |
| 2522 | Py_DecRef(offset); |
| 2523 | Py_DecRef(name); |
| 2524 | |
| 2525 | PyObject *rv = PyTuple_New(3); |
| 2526 | PyTuple_SET_ITEM(rv, 0, utc_singleton_0); |
| 2527 | PyTuple_SET_ITEM(rv, 1, utc_singleton_1); |
| 2528 | PyTuple_SET_ITEM(rv, 2, non_utc_zone); |
| 2529 | |
| 2530 | return rv; |
| 2531 | } |
| 2532 | |
| 2533 | static PyObject * |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2534 | get_timezone_utc_capi(PyObject* self, PyObject *args) { |
| 2535 | int macro = 0; |
| 2536 | if (!PyArg_ParseTuple(args, "|p", ¯o)) { |
| 2537 | return NULL; |
| 2538 | } |
| 2539 | if (macro) { |
Paul Ganssle | 58dc03c | 2018-01-25 08:58:07 -0500 | [diff] [blame] | 2540 | Py_INCREF(PyDateTime_TimeZone_UTC); |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2541 | return PyDateTime_TimeZone_UTC; |
| 2542 | } else { |
Paul Ganssle | 58dc03c | 2018-01-25 08:58:07 -0500 | [diff] [blame] | 2543 | Py_INCREF(PyDateTimeAPI->TimeZone_UTC); |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2544 | return PyDateTimeAPI->TimeZone_UTC; |
| 2545 | } |
| 2546 | } |
| 2547 | |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2548 | static PyObject * |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 2549 | get_date_fromdate(PyObject *self, PyObject *args) |
| 2550 | { |
| 2551 | PyObject *rv = NULL; |
| 2552 | int macro; |
| 2553 | int year, month, day; |
| 2554 | |
| 2555 | if (!PyArg_ParseTuple(args, "piii", ¯o, &year, &month, &day)) { |
| 2556 | return NULL; |
| 2557 | } |
| 2558 | |
| 2559 | if (macro) { |
| 2560 | rv = PyDate_FromDate(year, month, day); |
| 2561 | } |
| 2562 | else { |
| 2563 | rv = PyDateTimeAPI->Date_FromDate( |
| 2564 | year, month, day, |
| 2565 | PyDateTimeAPI->DateType); |
| 2566 | } |
| 2567 | return rv; |
| 2568 | } |
| 2569 | |
| 2570 | static PyObject * |
| 2571 | get_datetime_fromdateandtime(PyObject *self, PyObject *args) |
| 2572 | { |
| 2573 | PyObject *rv = NULL; |
| 2574 | int macro; |
| 2575 | int year, month, day; |
| 2576 | int hour, minute, second, microsecond; |
| 2577 | |
| 2578 | if (!PyArg_ParseTuple(args, "piiiiiii", |
| 2579 | ¯o, |
| 2580 | &year, &month, &day, |
| 2581 | &hour, &minute, &second, µsecond)) { |
| 2582 | return NULL; |
| 2583 | } |
| 2584 | |
| 2585 | if (macro) { |
| 2586 | rv = PyDateTime_FromDateAndTime( |
| 2587 | year, month, day, |
| 2588 | hour, minute, second, microsecond); |
| 2589 | } |
| 2590 | else { |
| 2591 | rv = PyDateTimeAPI->DateTime_FromDateAndTime( |
| 2592 | year, month, day, |
| 2593 | hour, minute, second, microsecond, |
| 2594 | Py_None, |
| 2595 | PyDateTimeAPI->DateTimeType); |
| 2596 | } |
| 2597 | return rv; |
| 2598 | } |
| 2599 | |
| 2600 | static PyObject * |
| 2601 | get_datetime_fromdateandtimeandfold(PyObject *self, PyObject *args) |
| 2602 | { |
| 2603 | PyObject *rv = NULL; |
| 2604 | int macro; |
| 2605 | int year, month, day; |
| 2606 | int hour, minute, second, microsecond, fold; |
| 2607 | |
| 2608 | if (!PyArg_ParseTuple(args, "piiiiiiii", |
| 2609 | ¯o, |
| 2610 | &year, &month, &day, |
| 2611 | &hour, &minute, &second, µsecond, |
| 2612 | &fold)) { |
| 2613 | return NULL; |
| 2614 | } |
| 2615 | |
| 2616 | if (macro) { |
| 2617 | rv = PyDateTime_FromDateAndTimeAndFold( |
| 2618 | year, month, day, |
| 2619 | hour, minute, second, microsecond, |
| 2620 | fold); |
| 2621 | } |
| 2622 | else { |
| 2623 | rv = PyDateTimeAPI->DateTime_FromDateAndTimeAndFold( |
| 2624 | year, month, day, |
| 2625 | hour, minute, second, microsecond, |
| 2626 | Py_None, |
| 2627 | fold, |
| 2628 | PyDateTimeAPI->DateTimeType); |
| 2629 | } |
| 2630 | return rv; |
| 2631 | } |
| 2632 | |
| 2633 | static PyObject * |
| 2634 | get_time_fromtime(PyObject *self, PyObject *args) |
| 2635 | { |
| 2636 | PyObject *rv = NULL; |
| 2637 | int macro; |
| 2638 | int hour, minute, second, microsecond; |
| 2639 | |
| 2640 | if (!PyArg_ParseTuple(args, "piiii", |
| 2641 | ¯o, |
| 2642 | &hour, &minute, &second, µsecond)) { |
| 2643 | return NULL; |
| 2644 | } |
| 2645 | |
| 2646 | if (macro) { |
| 2647 | rv = PyTime_FromTime(hour, minute, second, microsecond); |
| 2648 | } |
| 2649 | else { |
| 2650 | rv = PyDateTimeAPI->Time_FromTime( |
| 2651 | hour, minute, second, microsecond, |
| 2652 | Py_None, |
| 2653 | PyDateTimeAPI->TimeType); |
| 2654 | } |
| 2655 | return rv; |
| 2656 | } |
| 2657 | |
| 2658 | static PyObject * |
| 2659 | get_time_fromtimeandfold(PyObject *self, PyObject *args) |
| 2660 | { |
| 2661 | PyObject *rv = NULL; |
| 2662 | int macro; |
| 2663 | int hour, minute, second, microsecond, fold; |
| 2664 | |
| 2665 | if (!PyArg_ParseTuple(args, "piiiii", |
| 2666 | ¯o, |
| 2667 | &hour, &minute, &second, µsecond, |
| 2668 | &fold)) { |
| 2669 | return NULL; |
| 2670 | } |
| 2671 | |
| 2672 | if (macro) { |
| 2673 | rv = PyTime_FromTimeAndFold(hour, minute, second, microsecond, fold); |
| 2674 | } |
| 2675 | else { |
| 2676 | rv = PyDateTimeAPI->Time_FromTimeAndFold( |
| 2677 | hour, minute, second, microsecond, |
| 2678 | Py_None, |
| 2679 | fold, |
| 2680 | PyDateTimeAPI->TimeType); |
| 2681 | } |
| 2682 | return rv; |
| 2683 | } |
| 2684 | |
| 2685 | static PyObject * |
| 2686 | get_delta_fromdsu(PyObject *self, PyObject *args) |
| 2687 | { |
| 2688 | PyObject *rv = NULL; |
| 2689 | int macro; |
| 2690 | int days, seconds, microseconds; |
| 2691 | |
| 2692 | if (!PyArg_ParseTuple(args, "piii", |
| 2693 | ¯o, |
| 2694 | &days, &seconds, µseconds)) { |
| 2695 | return NULL; |
| 2696 | } |
| 2697 | |
| 2698 | if (macro) { |
| 2699 | rv = PyDelta_FromDSU(days, seconds, microseconds); |
| 2700 | } |
| 2701 | else { |
| 2702 | rv = PyDateTimeAPI->Delta_FromDelta( |
| 2703 | days, seconds, microseconds, 1, |
| 2704 | PyDateTimeAPI->DeltaType); |
| 2705 | } |
| 2706 | |
| 2707 | return rv; |
| 2708 | } |
| 2709 | |
| 2710 | static PyObject * |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2711 | get_date_fromtimestamp(PyObject* self, PyObject *args) |
| 2712 | { |
| 2713 | PyObject *tsargs = NULL, *ts = NULL, *rv = NULL; |
| 2714 | int macro = 0; |
| 2715 | |
| 2716 | if (!PyArg_ParseTuple(args, "O|p", &ts, ¯o)) { |
| 2717 | return NULL; |
| 2718 | } |
| 2719 | |
| 2720 | // Construct the argument tuple |
| 2721 | if ((tsargs = PyTuple_Pack(1, ts)) == NULL) { |
| 2722 | return NULL; |
| 2723 | } |
| 2724 | |
| 2725 | // Pass along to the API function |
| 2726 | if (macro) { |
| 2727 | rv = PyDate_FromTimestamp(tsargs); |
| 2728 | } |
| 2729 | else { |
| 2730 | rv = PyDateTimeAPI->Date_FromTimestamp( |
| 2731 | (PyObject *)PyDateTimeAPI->DateType, tsargs |
| 2732 | ); |
| 2733 | } |
| 2734 | |
| 2735 | Py_DECREF(tsargs); |
| 2736 | return rv; |
| 2737 | } |
| 2738 | |
| 2739 | static PyObject * |
| 2740 | get_datetime_fromtimestamp(PyObject* self, PyObject *args) |
| 2741 | { |
| 2742 | int macro = 0; |
| 2743 | int usetz = 0; |
| 2744 | PyObject *tsargs = NULL, *ts = NULL, *tzinfo = Py_None, *rv = NULL; |
| 2745 | if (!PyArg_ParseTuple(args, "OO|pp", &ts, &tzinfo, &usetz, ¯o)) { |
| 2746 | return NULL; |
| 2747 | } |
| 2748 | |
| 2749 | // Construct the argument tuple |
| 2750 | if (usetz) { |
| 2751 | tsargs = PyTuple_Pack(2, ts, tzinfo); |
| 2752 | } |
| 2753 | else { |
| 2754 | tsargs = PyTuple_Pack(1, ts); |
| 2755 | } |
| 2756 | |
| 2757 | if (tsargs == NULL) { |
| 2758 | return NULL; |
| 2759 | } |
| 2760 | |
| 2761 | // Pass along to the API function |
| 2762 | if (macro) { |
| 2763 | rv = PyDateTime_FromTimestamp(tsargs); |
| 2764 | } |
| 2765 | else { |
| 2766 | rv = PyDateTimeAPI->DateTime_FromTimestamp( |
| 2767 | (PyObject *)PyDateTimeAPI->DateTimeType, tsargs, NULL |
| 2768 | ); |
| 2769 | } |
| 2770 | |
| 2771 | Py_DECREF(tsargs); |
| 2772 | return rv; |
| 2773 | } |
| 2774 | |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 2775 | static PyObject * |
| 2776 | test_PyDateTime_GET(PyObject *self, PyObject *obj) |
| 2777 | { |
| 2778 | int year, month, day; |
| 2779 | |
| 2780 | year = PyDateTime_GET_YEAR(obj); |
| 2781 | month = PyDateTime_GET_MONTH(obj); |
| 2782 | day = PyDateTime_GET_DAY(obj); |
| 2783 | |
| 2784 | return Py_BuildValue("(lll)", year, month, day); |
| 2785 | } |
| 2786 | |
| 2787 | static PyObject * |
| 2788 | test_PyDateTime_DATE_GET(PyObject *self, PyObject *obj) |
| 2789 | { |
| 2790 | int hour, minute, second, microsecond; |
| 2791 | |
| 2792 | hour = PyDateTime_DATE_GET_HOUR(obj); |
| 2793 | minute = PyDateTime_DATE_GET_MINUTE(obj); |
| 2794 | second = PyDateTime_DATE_GET_SECOND(obj); |
| 2795 | microsecond = PyDateTime_DATE_GET_MICROSECOND(obj); |
Zackery Spytz | 2e4dd33 | 2020-09-23 12:43:45 -0600 | [diff] [blame] | 2796 | PyObject *tzinfo = PyDateTime_DATE_GET_TZINFO(obj); |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 2797 | |
Zackery Spytz | 2e4dd33 | 2020-09-23 12:43:45 -0600 | [diff] [blame] | 2798 | return Py_BuildValue("(llllO)", hour, minute, second, microsecond, tzinfo); |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | static PyObject * |
| 2802 | test_PyDateTime_TIME_GET(PyObject *self, PyObject *obj) |
| 2803 | { |
| 2804 | int hour, minute, second, microsecond; |
| 2805 | |
| 2806 | hour = PyDateTime_TIME_GET_HOUR(obj); |
| 2807 | minute = PyDateTime_TIME_GET_MINUTE(obj); |
| 2808 | second = PyDateTime_TIME_GET_SECOND(obj); |
| 2809 | microsecond = PyDateTime_TIME_GET_MICROSECOND(obj); |
Zackery Spytz | 2e4dd33 | 2020-09-23 12:43:45 -0600 | [diff] [blame] | 2810 | PyObject *tzinfo = PyDateTime_TIME_GET_TZINFO(obj); |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 2811 | |
Zackery Spytz | 2e4dd33 | 2020-09-23 12:43:45 -0600 | [diff] [blame] | 2812 | return Py_BuildValue("(llllO)", hour, minute, second, microsecond, tzinfo); |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | static PyObject * |
| 2816 | test_PyDateTime_DELTA_GET(PyObject *self, PyObject *obj) |
| 2817 | { |
| 2818 | int days, seconds, microseconds; |
| 2819 | |
| 2820 | days = PyDateTime_DELTA_GET_DAYS(obj); |
| 2821 | seconds = PyDateTime_DELTA_GET_SECONDS(obj); |
| 2822 | microseconds = PyDateTime_DELTA_GET_MICROSECONDS(obj); |
| 2823 | |
| 2824 | return Py_BuildValue("(lll)", days, seconds, microseconds); |
| 2825 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2826 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2827 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 2828 | * `thread_done` when it's finished. The driver code has to know when the |
| 2829 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 2830 | * may go away when the driver finishes. The former lack of this explicit |
| 2831 | * synchronization caused rare segfaults, so rare that they were seen only |
| 2832 | * on a Mac buildbot (although they were possible on any box). |
| 2833 | */ |
| 2834 | static PyThread_type_lock thread_done = NULL; |
| 2835 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2836 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2837 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | PyObject *rc; |
| 2840 | int success; |
| 2841 | PyGILState_STATE s = PyGILState_Ensure(); |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2842 | rc = _PyObject_CallNoArg((PyObject *)callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | success = (rc != NULL); |
| 2844 | Py_XDECREF(rc); |
| 2845 | PyGILState_Release(s); |
| 2846 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2849 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 2850 | * should be called only from threads spawned by test_thread_state(). |
| 2851 | */ |
| 2852 | static void |
| 2853 | _make_call_from_thread(void *callable) |
| 2854 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | _make_call(callable); |
| 2856 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2859 | static PyObject * |
| 2860 | test_thread_state(PyObject *self, PyObject *args) |
| 2861 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | PyObject *fn; |
| 2863 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 2866 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | if (!PyCallable_Check(fn)) { |
| 2869 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 2870 | Py_TYPE(fn)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | return NULL; |
| 2872 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2874 | thread_done = PyThread_allocate_lock(); |
| 2875 | if (thread_done == NULL) |
| 2876 | return PyErr_NoMemory(); |
| 2877 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | /* Start a new thread with our callback. */ |
| 2880 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2881 | /* Make the callback with the thread lock held by this thread */ |
| 2882 | success &= _make_call(fn); |
| 2883 | /* Do it all again, but this time with the thread-lock released */ |
| 2884 | Py_BEGIN_ALLOW_THREADS |
| 2885 | success &= _make_call(fn); |
| 2886 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2887 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 | /* And once more with and without a thread |
| 2890 | XXX - should use a lock and work out exactly what we are trying |
| 2891 | to test <wink> |
| 2892 | */ |
| 2893 | Py_BEGIN_ALLOW_THREADS |
| 2894 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2895 | success &= _make_call(fn); |
| 2896 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2897 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 2900 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | PyThread_free_lock(thread_done); |
| 2903 | if (!success) |
| 2904 | return NULL; |
| 2905 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2906 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2907 | |
| 2908 | /* test Py_AddPendingCalls using threads */ |
| 2909 | static int _pending_callback(void *arg) |
| 2910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | /* we assume the argument is callable object to which we own a reference */ |
| 2912 | PyObject *callable = (PyObject *)arg; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 2913 | PyObject *r = _PyObject_CallNoArg(callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2914 | Py_DECREF(callable); |
| 2915 | Py_XDECREF(r); |
| 2916 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | /* The following requests n callbacks to _pending_callback. It can be |
| 2920 | * run from any python thread. |
| 2921 | */ |
Benjamin Peterson | b4588c2 | 2018-07-03 22:30:56 -0700 | [diff] [blame] | 2922 | static PyObject * |
| 2923 | pending_threadfunc(PyObject *self, PyObject *arg) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2924 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2925 | PyObject *callable; |
| 2926 | int r; |
| 2927 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 2928 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2930 | /* create the reference for the callbackwhile we hold the lock */ |
| 2931 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2933 | Py_BEGIN_ALLOW_THREADS |
| 2934 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 2935 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2937 | if (r<0) { |
| 2938 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2939 | Py_RETURN_FALSE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2940 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2941 | Py_RETURN_TRUE; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2942 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2943 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 2944 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2945 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 2946 | test_string_from_format(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2947 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2948 | PyObject *result; |
| 2949 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2950 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2951 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 2952 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 2953 | if (result == NULL) \ |
| 2954 | return NULL; \ |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 2955 | if (!_PyUnicode_EqualToASCIIString(result, "1")) { \ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2956 | msg = FORMAT " failed at 1"; \ |
| 2957 | goto Fail; \ |
| 2958 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | CHECK_1_FORMAT("%d", int); |
| 2962 | CHECK_1_FORMAT("%ld", long); |
| 2963 | /* The z width modifier was added in Python 2.5. */ |
| 2964 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2966 | /* The u type code was added in Python 2.5. */ |
| 2967 | CHECK_1_FORMAT("%u", unsigned int); |
| 2968 | CHECK_1_FORMAT("%lu", unsigned long); |
| 2969 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 2972 | CHECK_1_FORMAT("%llu", unsigned long long); |
| 2973 | CHECK_1_FORMAT("%lld", long long); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2976 | |
| 2977 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | Py_XDECREF(result); |
| 2979 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2980 | |
| 2981 | #undef CHECK_1_FORMAT |
| 2982 | } |
| 2983 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2984 | |
| 2985 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2986 | test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2987 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 2988 | int result; |
| 2989 | if (py_s == NULL) |
| 2990 | return NULL; |
| 2991 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 2992 | Py_DECREF(py_s); |
| 2993 | if (!result) { |
| 2994 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 2995 | "should not compare equal to c string."); |
| 2996 | return NULL; |
| 2997 | } |
| 2998 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2999 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 3000 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3001 | /* This is here to provide a docstring for test_descr. */ |
| 3002 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3003 | test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3008 | /* Test PyOS_string_to_double. */ |
| 3009 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3010 | test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | double result; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 3012 | const char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3014 | #define CHECK_STRING(STR, expected) \ |
| 3015 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 3016 | if (result == -1.0 && PyErr_Occurred()) \ |
| 3017 | return NULL; \ |
Benjamin Peterson | 8f4b247 | 2016-09-07 18:09:22 -0700 | [diff] [blame] | 3018 | if (result != (double)expected) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | msg = "conversion of " STR " to float failed"; \ |
| 3020 | goto fail; \ |
| 3021 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | #define CHECK_INVALID(STR) \ |
| 3024 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 3025 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 3026 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 3027 | PyErr_Clear(); \ |
| 3028 | else \ |
| 3029 | return NULL; \ |
| 3030 | } \ |
| 3031 | else { \ |
| 3032 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 3033 | goto fail; \ |
| 3034 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | CHECK_STRING("0.1", 0.1); |
| 3037 | CHECK_STRING("1.234", 1.234); |
| 3038 | CHECK_STRING("-1.35", -1.35); |
| 3039 | CHECK_STRING(".1e01", 1.0); |
| 3040 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | CHECK_INVALID(" 0.1"); |
| 3043 | CHECK_INVALID("\t\n-3"); |
| 3044 | CHECK_INVALID(".123 "); |
| 3045 | CHECK_INVALID("3\n"); |
| 3046 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3049 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3050 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 3051 | #undef CHECK_STRING |
| 3052 | #undef CHECK_INVALID |
| 3053 | } |
| 3054 | |
| 3055 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3056 | /* Coverage testing of capsule objects. */ |
| 3057 | |
| 3058 | static const char *capsule_name = "capsule name"; |
| 3059 | static char *capsule_pointer = "capsule pointer"; |
| 3060 | static char *capsule_context = "capsule context"; |
| 3061 | static const char *capsule_error = NULL; |
| 3062 | static int |
| 3063 | capsule_destructor_call_count = 0; |
| 3064 | |
| 3065 | static void |
| 3066 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3067 | capsule_destructor_call_count++; |
| 3068 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 3069 | capsule_error = "context did not match in destructor!"; |
| 3070 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 3071 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 3072 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 3073 | capsule_error = "name did not match in destructor!"; |
| 3074 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 3075 | capsule_error = "pointer did not match in destructor!"; |
| 3076 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | char *name; |
| 3081 | char *module; |
| 3082 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3083 | } known_capsule; |
| 3084 | |
| 3085 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3086 | test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3087 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | PyObject *object; |
| 3089 | const char *error = NULL; |
| 3090 | void *pointer; |
| 3091 | void *pointer2; |
| 3092 | known_capsule known_capsules[] = { |
| 3093 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 3094 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 3095 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 3096 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 3097 | { NULL, NULL }, |
| 3098 | }; |
| 3099 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3100 | |
| 3101 | #define FAIL(x) { error = (x); goto exit; } |
| 3102 | |
| 3103 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3104 | if (capsule_error) { \ |
| 3105 | FAIL(capsule_error); \ |
| 3106 | } \ |
| 3107 | else if (!capsule_destructor_call_count) { \ |
| 3108 | FAIL("destructor not called!"); \ |
| 3109 | } \ |
| 3110 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 3113 | PyCapsule_SetContext(object, capsule_context); |
| 3114 | capsule_destructor(object); |
| 3115 | CHECK_DESTRUCTOR; |
| 3116 | Py_DECREF(object); |
| 3117 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3119 | object = PyCapsule_New(known, "ignored", NULL); |
| 3120 | PyCapsule_SetPointer(object, capsule_pointer); |
| 3121 | PyCapsule_SetName(object, capsule_name); |
| 3122 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 3123 | PyCapsule_SetContext(object, capsule_context); |
| 3124 | capsule_destructor(object); |
| 3125 | CHECK_DESTRUCTOR; |
| 3126 | /* intentionally access using the wrong name */ |
| 3127 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 3128 | if (!PyErr_Occurred()) { |
| 3129 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 3130 | } |
| 3131 | PyErr_Clear(); |
| 3132 | if (pointer2) { |
| 3133 | if (pointer2 == capsule_pointer) { |
| 3134 | FAIL("PyCapsule_GetPointer should not have" |
| 3135 | " returned the internal pointer!"); |
| 3136 | } else { |
| 3137 | FAIL("PyCapsule_GetPointer should have " |
| 3138 | "returned NULL pointer but did not!"); |
| 3139 | } |
| 3140 | } |
| 3141 | PyCapsule_SetDestructor(object, NULL); |
| 3142 | Py_DECREF(object); |
| 3143 | if (capsule_destructor_call_count) { |
| 3144 | FAIL("destructor called when it should not have been!"); |
| 3145 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 3148 | /* yeah, ordinarily I wouldn't do this either, |
| 3149 | but it's fine for this test harness. |
| 3150 | */ |
| 3151 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3152 | #undef FAIL |
| 3153 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | { \ |
| 3155 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 3156 | x, known->module, known->attribute); \ |
| 3157 | error = buffer; \ |
| 3158 | goto exit; \ |
| 3159 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | PyObject *module = PyImport_ImportModule(known->module); |
| 3162 | if (module) { |
| 3163 | pointer = PyCapsule_Import(known->name, 0); |
| 3164 | if (!pointer) { |
| 3165 | Py_DECREF(module); |
| 3166 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 3167 | } |
| 3168 | object = PyObject_GetAttrString(module, known->attribute); |
| 3169 | if (!object) { |
| 3170 | Py_DECREF(module); |
| 3171 | return NULL; |
| 3172 | } |
| 3173 | pointer2 = PyCapsule_GetPointer(object, |
| 3174 | "weebles wobble but they don't fall down"); |
| 3175 | if (!PyErr_Occurred()) { |
| 3176 | Py_DECREF(object); |
| 3177 | Py_DECREF(module); |
| 3178 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 3179 | } |
| 3180 | PyErr_Clear(); |
| 3181 | if (pointer2) { |
| 3182 | Py_DECREF(module); |
| 3183 | Py_DECREF(object); |
| 3184 | if (pointer2 == pointer) { |
| 3185 | FAIL("PyCapsule_GetPointer should not have" |
| 3186 | " returned its internal pointer!"); |
| 3187 | } else { |
| 3188 | FAIL("PyCapsule_GetPointer should have" |
| 3189 | " returned NULL pointer but did not!"); |
| 3190 | } |
| 3191 | } |
| 3192 | Py_DECREF(object); |
| 3193 | Py_DECREF(module); |
| 3194 | } |
| 3195 | else |
| 3196 | PyErr_Clear(); |
| 3197 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3198 | |
| 3199 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3200 | if (error) { |
| 3201 | return raiseTestError("test_capsule", error); |
| 3202 | } |
| 3203 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3204 | #undef FAIL |
| 3205 | } |
| 3206 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3207 | #ifdef HAVE_GETTIMEOFDAY |
| 3208 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 3209 | 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] | 3210 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | e->tv_sec -= s->tv_sec; |
| 3212 | e->tv_usec -= s->tv_usec; |
| 3213 | if (e->tv_usec < 0) { |
| 3214 | e->tv_sec -=1; |
| 3215 | e->tv_usec += 1000000; |
| 3216 | } |
| 3217 | 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] | 3218 | } |
| 3219 | |
| 3220 | static PyObject * |
| 3221 | profile_int(PyObject *self, PyObject* args) |
| 3222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | int i, k; |
| 3224 | struct timeval start, stop; |
| 3225 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | /* Test 1: Allocate and immediately deallocate |
| 3228 | many small integers */ |
| 3229 | gettimeofday(&start, NULL); |
| 3230 | for(k=0; k < 20000; k++) |
| 3231 | for(i=0; i < 1000; i++) { |
| 3232 | single = PyLong_FromLong(i); |
| 3233 | Py_DECREF(single); |
| 3234 | } |
| 3235 | gettimeofday(&stop, NULL); |
| 3236 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | /* Test 2: Allocate and immediately deallocate |
| 3239 | many large integers */ |
| 3240 | gettimeofday(&start, NULL); |
| 3241 | for(k=0; k < 20000; k++) |
| 3242 | for(i=0; i < 1000; i++) { |
| 3243 | single = PyLong_FromLong(i+1000000); |
| 3244 | Py_DECREF(single); |
| 3245 | } |
| 3246 | gettimeofday(&stop, NULL); |
| 3247 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3249 | /* Test 3: Allocate a few integers, then release |
| 3250 | them all simultaneously. */ |
| 3251 | multiple = malloc(sizeof(PyObject*) * 1000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3252 | if (multiple == NULL) |
| 3253 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | gettimeofday(&start, NULL); |
| 3255 | for(k=0; k < 20000; k++) { |
| 3256 | for(i=0; i < 1000; i++) { |
| 3257 | multiple[i] = PyLong_FromLong(i+1000000); |
| 3258 | } |
| 3259 | for(i=0; i < 1000; i++) { |
| 3260 | Py_DECREF(multiple[i]); |
| 3261 | } |
| 3262 | } |
| 3263 | gettimeofday(&stop, NULL); |
| 3264 | print_delta(3, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3265 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | /* Test 4: Allocate many integers, then release |
| 3268 | them all simultaneously. */ |
| 3269 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3270 | if (multiple == NULL) |
| 3271 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3272 | gettimeofday(&start, NULL); |
| 3273 | for(k=0; k < 20; k++) { |
| 3274 | for(i=0; i < 1000000; i++) { |
| 3275 | multiple[i] = PyLong_FromLong(i+1000000); |
| 3276 | } |
| 3277 | for(i=0; i < 1000000; i++) { |
| 3278 | Py_DECREF(multiple[i]); |
| 3279 | } |
| 3280 | } |
| 3281 | gettimeofday(&stop, NULL); |
| 3282 | print_delta(4, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3283 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | /* Test 5: Allocate many integers < 32000 */ |
| 3286 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3287 | if (multiple == NULL) |
| 3288 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3289 | gettimeofday(&start, NULL); |
| 3290 | for(k=0; k < 10; k++) { |
| 3291 | for(i=0; i < 1000000; i++) { |
| 3292 | multiple[i] = PyLong_FromLong(i+1000); |
| 3293 | } |
| 3294 | for(i=0; i < 1000000; i++) { |
| 3295 | Py_DECREF(multiple[i]); |
| 3296 | } |
| 3297 | } |
| 3298 | gettimeofday(&stop, NULL); |
| 3299 | print_delta(5, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3300 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3302 | /* Test 6: Perform small int addition */ |
| 3303 | op1 = PyLong_FromLong(1); |
| 3304 | gettimeofday(&start, NULL); |
| 3305 | for(i=0; i < 10000000; i++) { |
| 3306 | result = PyNumber_Add(op1, op1); |
| 3307 | Py_DECREF(result); |
| 3308 | } |
| 3309 | gettimeofday(&stop, NULL); |
| 3310 | Py_DECREF(op1); |
| 3311 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3313 | /* Test 7: Perform medium int addition */ |
| 3314 | op1 = PyLong_FromLong(1000); |
Christian Heimes | 66eda26 | 2013-07-26 15:54:07 +0200 | [diff] [blame] | 3315 | if (op1 == NULL) |
| 3316 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | gettimeofday(&start, NULL); |
| 3318 | for(i=0; i < 10000000; i++) { |
| 3319 | result = PyNumber_Add(op1, op1); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 3320 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | } |
| 3322 | gettimeofday(&stop, NULL); |
| 3323 | Py_DECREF(op1); |
| 3324 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3325 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 3326 | Py_RETURN_NONE; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3327 | } |
| 3328 | #endif |
| 3329 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3330 | /* To test the format of tracebacks as printed out. */ |
| 3331 | static PyObject * |
| 3332 | traceback_print(PyObject *self, PyObject *args) |
| 3333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | PyObject *file; |
| 3335 | PyObject *traceback; |
| 3336 | int result; |
| 3337 | |
| 3338 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 3339 | &traceback, &file)) |
| 3340 | return NULL; |
| 3341 | |
| 3342 | result = PyTraceBack_Print(traceback, file); |
| 3343 | if (result < 0) |
| 3344 | return NULL; |
| 3345 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3348 | /* To test the format of exceptions as printed out. */ |
| 3349 | static PyObject * |
| 3350 | exception_print(PyObject *self, PyObject *args) |
| 3351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | PyObject *value; |
| 3353 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 3356 | &value)) |
| 3357 | return NULL; |
| 3358 | if (!PyExceptionInstance_Check(value)) { |
| 3359 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 3360 | return NULL; |
| 3361 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3362 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3363 | tb = PyException_GetTraceback(value); |
| 3364 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 3365 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3367 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | |
| 3371 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3372 | |
| 3373 | /* reliably raise a MemoryError */ |
| 3374 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3375 | raise_memoryerror(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3376 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3377 | PyErr_NoMemory(); |
| 3378 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3381 | /* Issue 6012 */ |
| 3382 | static PyObject *str1, *str2; |
| 3383 | static int |
| 3384 | failing_converter(PyObject *obj, void *arg) |
| 3385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | /* Clone str1, then let the conversion fail. */ |
| 3387 | assert(str1); |
| 3388 | str2 = str1; |
| 3389 | Py_INCREF(str2); |
| 3390 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3391 | } |
| 3392 | static PyObject* |
| 3393 | argparsing(PyObject *o, PyObject *args) |
| 3394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3395 | PyObject *res; |
| 3396 | str1 = str2 = NULL; |
| 3397 | if (!PyArg_ParseTuple(args, "O&O&", |
| 3398 | PyUnicode_FSConverter, &str1, |
| 3399 | failing_converter, &str2)) { |
| 3400 | if (!str2) |
| 3401 | /* argument converter not called? */ |
| 3402 | return NULL; |
| 3403 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 3404 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3405 | Py_DECREF(str2); |
| 3406 | PyErr_Clear(); |
| 3407 | return res; |
| 3408 | } |
| 3409 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3410 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3411 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3412 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 3413 | static PyObject * |
| 3414 | code_newempty(PyObject *self, PyObject *args) |
| 3415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3416 | const char *filename; |
| 3417 | const char *funcname; |
| 3418 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 3421 | &filename, &funcname, &firstlineno)) |
| 3422 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3424 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3427 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 3428 | Run via Lib/test/test_exceptions.py */ |
| 3429 | static PyObject * |
| 3430 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | const char *name; |
| 3433 | const char *doc = NULL; |
| 3434 | PyObject *base = NULL; |
| 3435 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3440 | "s|sOO:make_exception_with_doc", kwlist, |
| 3441 | &name, &doc, &base, &dict)) |
| 3442 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3447 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3448 | make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3449 | { |
| 3450 | Py_buffer info; |
| 3451 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 3452 | return NULL; |
| 3453 | return PyMemoryView_FromBuffer(&info); |
| 3454 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 3455 | |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3456 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3457 | test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3458 | { |
| 3459 | int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 3460 | int init[5] = {0, 1, 2, 3, 4}; |
| 3461 | Py_ssize_t itemsize = sizeof(int); |
| 3462 | Py_ssize_t shape = 5; |
| 3463 | Py_ssize_t strides = 2 * itemsize; |
| 3464 | Py_buffer view = { |
| 3465 | data, |
| 3466 | NULL, |
| 3467 | 5 * itemsize, |
| 3468 | itemsize, |
| 3469 | 1, |
| 3470 | 1, |
| 3471 | NULL, |
| 3472 | &shape, |
| 3473 | &strides, |
| 3474 | NULL, |
| 3475 | NULL |
| 3476 | }; |
| 3477 | int *ptr; |
| 3478 | int i; |
| 3479 | |
| 3480 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 3481 | ptr = view.buf; |
| 3482 | for (i = 0; i < 5; i++) { |
| 3483 | if (ptr[2*i] != i) { |
| 3484 | PyErr_SetString(TestError, |
| 3485 | "test_from_contiguous: incorrect result"); |
| 3486 | return NULL; |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | view.buf = &data[8]; |
| 3491 | view.strides[0] = -2 * itemsize; |
| 3492 | |
| 3493 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 3494 | ptr = view.buf; |
| 3495 | for (i = 0; i < 5; i++) { |
| 3496 | if (*(ptr-2*i) != i) { |
| 3497 | PyErr_SetString(TestError, |
| 3498 | "test_from_contiguous: incorrect result"); |
| 3499 | return NULL; |
| 3500 | } |
| 3501 | } |
| 3502 | |
| 3503 | Py_RETURN_NONE; |
| 3504 | } |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3505 | |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3506 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3507 | extern PyTypeObject _PyBytesIOBuffer_Type; |
| 3508 | |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3509 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3510 | test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3511 | { |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3512 | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3513 | PyObject *b; |
| 3514 | char *dummy[1]; |
| 3515 | int ret, match; |
| 3516 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3517 | /* PyBuffer_FillInfo() */ |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3518 | ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE); |
| 3519 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 3520 | PyErr_Clear(); |
| 3521 | if (ret != -1 || match == 0) |
| 3522 | goto error; |
| 3523 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3524 | /* bytesiobuf_getbuffer() */ |
| 3525 | b = type->tp_alloc(type, 0); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3526 | if (b == NULL) { |
| 3527 | return NULL; |
| 3528 | } |
| 3529 | |
| 3530 | ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE); |
| 3531 | Py_DECREF(b); |
| 3532 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 3533 | PyErr_Clear(); |
| 3534 | if (ret != -1 || match == 0) |
| 3535 | goto error; |
| 3536 | |
| 3537 | Py_RETURN_NONE; |
| 3538 | |
| 3539 | error: |
| 3540 | PyErr_SetString(TestError, |
| 3541 | "test_pep3118_obsolete_write_locks: failure"); |
| 3542 | return NULL; |
| 3543 | } |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3544 | #endif |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3545 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3546 | /* This tests functions that historically supported write locks. It is |
| 3547 | wrong to call getbuffer() with view==NULL and a compliant getbufferproc |
| 3548 | is entitled to segfault in that case. */ |
| 3549 | static PyObject * |
| 3550 | getbuffer_with_null_view(PyObject* self, PyObject *obj) |
| 3551 | { |
| 3552 | if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0) |
| 3553 | return NULL; |
| 3554 | |
| 3555 | Py_RETURN_NONE; |
| 3556 | } |
| 3557 | |
Joannah Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 3558 | /* PyBuffer_SizeFromFormat() */ |
| 3559 | static PyObject * |
| 3560 | test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args) |
| 3561 | { |
| 3562 | const char *format; |
| 3563 | Py_ssize_t result; |
| 3564 | |
| 3565 | if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat", |
| 3566 | &format)) { |
| 3567 | return NULL; |
| 3568 | } |
| 3569 | |
| 3570 | result = PyBuffer_SizeFromFormat(format); |
| 3571 | if (result == -1) { |
| 3572 | return NULL; |
| 3573 | } |
| 3574 | |
| 3575 | return PyLong_FromSsize_t(result); |
| 3576 | } |
| 3577 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3578 | /* Test that the fatal error from not having a current thread doesn't |
| 3579 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 3580 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3581 | crash_no_current_thread(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3582 | { |
| 3583 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 3584 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 3585 | !pydebug mode. However, the test only actually tests anything |
| 3586 | in pydebug mode, since that's where the infinite loop was in |
| 3587 | the first place. */ |
| 3588 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3589 | Py_END_ALLOW_THREADS |
| 3590 | return NULL; |
| 3591 | } |
| 3592 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3593 | /* To run some code in a sub-interpreter. */ |
| 3594 | static PyObject * |
| 3595 | run_in_subinterp(PyObject *self, PyObject *args) |
| 3596 | { |
| 3597 | const char *code; |
| 3598 | int r; |
| 3599 | PyThreadState *substate, *mainstate; |
Guido van Rossum | 9d197c7 | 2020-06-27 17:33:49 -0700 | [diff] [blame] | 3600 | /* only initialise 'cflags.cf_flags' to test backwards compatibility */ |
| 3601 | PyCompilerFlags cflags = {0}; |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3602 | |
| 3603 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 3604 | &code)) |
| 3605 | return NULL; |
| 3606 | |
| 3607 | mainstate = PyThreadState_Get(); |
| 3608 | |
| 3609 | PyThreadState_Swap(NULL); |
| 3610 | |
| 3611 | substate = Py_NewInterpreter(); |
Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 3612 | if (substate == NULL) { |
| 3613 | /* Since no new thread state was created, there is no exception to |
| 3614 | propagate; raise a fresh one after swapping in the old thread |
| 3615 | state. */ |
| 3616 | PyThreadState_Swap(mainstate); |
| 3617 | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
| 3618 | return NULL; |
| 3619 | } |
Guido van Rossum | 9d197c7 | 2020-06-27 17:33:49 -0700 | [diff] [blame] | 3620 | r = PyRun_SimpleStringFlags(code, &cflags); |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3621 | Py_EndInterpreter(substate); |
| 3622 | |
| 3623 | PyThreadState_Swap(mainstate); |
| 3624 | |
| 3625 | return PyLong_FromLong(r); |
| 3626 | } |
| 3627 | |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3628 | static int |
| 3629 | check_time_rounding(int round) |
| 3630 | { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 3631 | if (round != _PyTime_ROUND_FLOOR |
| 3632 | && round != _PyTime_ROUND_CEILING |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 3633 | && round != _PyTime_ROUND_HALF_EVEN |
| 3634 | && round != _PyTime_ROUND_UP) { |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3635 | PyErr_SetString(PyExc_ValueError, "invalid rounding"); |
| 3636 | return -1; |
| 3637 | } |
| 3638 | return 0; |
| 3639 | } |
| 3640 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3641 | static PyObject * |
| 3642 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
| 3643 | { |
| 3644 | PyObject *obj; |
| 3645 | time_t sec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3646 | int round; |
| 3647 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3648 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3649 | if (check_time_rounding(round) < 0) |
| 3650 | return NULL; |
| 3651 | if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3652 | return NULL; |
| 3653 | return _PyLong_FromTime_t(sec); |
| 3654 | } |
| 3655 | |
| 3656 | static PyObject * |
| 3657 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
| 3658 | { |
| 3659 | PyObject *obj; |
| 3660 | time_t sec; |
| 3661 | long usec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3662 | int round; |
| 3663 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3664 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3665 | if (check_time_rounding(round) < 0) |
| 3666 | return NULL; |
| 3667 | if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3668 | return NULL; |
| 3669 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
| 3670 | } |
| 3671 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3672 | static PyObject * |
| 3673 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
| 3674 | { |
| 3675 | PyObject *obj; |
| 3676 | time_t sec; |
| 3677 | long nsec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3678 | int round; |
| 3679 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3680 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3681 | if (check_time_rounding(round) < 0) |
| 3682 | return NULL; |
| 3683 | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3684 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3685 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3686 | } |
| 3687 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3688 | static void |
| 3689 | slot_tp_del(PyObject *self) |
| 3690 | { |
| 3691 | _Py_IDENTIFIER(__tp_del__); |
| 3692 | PyObject *del, *res; |
| 3693 | PyObject *error_type, *error_value, *error_traceback; |
| 3694 | |
| 3695 | /* Temporarily resurrect the object. */ |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3696 | assert(Py_REFCNT(self) == 0); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3697 | Py_SET_REFCNT(self, 1); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3698 | |
| 3699 | /* Save the current exception, if any. */ |
| 3700 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 3701 | |
| 3702 | /* Execute __del__ method, if any. */ |
| 3703 | del = _PyObject_LookupSpecial(self, &PyId___tp_del__); |
| 3704 | if (del != NULL) { |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 3705 | res = _PyObject_CallNoArg(del); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3706 | if (res == NULL) |
| 3707 | PyErr_WriteUnraisable(del); |
| 3708 | else |
| 3709 | Py_DECREF(res); |
| 3710 | Py_DECREF(del); |
| 3711 | } |
| 3712 | |
| 3713 | /* Restore the saved exception. */ |
| 3714 | PyErr_Restore(error_type, error_value, error_traceback); |
| 3715 | |
| 3716 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 3717 | * cause a recursive call. |
| 3718 | */ |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3719 | assert(Py_REFCNT(self) > 0); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3720 | Py_SET_REFCNT(self, Py_REFCNT(self) - 1); |
| 3721 | if (Py_REFCNT(self) == 0) { |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3722 | /* this is the normal path out */ |
| 3723 | return; |
| 3724 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3725 | |
| 3726 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 3727 | * never happened. |
| 3728 | */ |
| 3729 | { |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3730 | Py_ssize_t refcnt = Py_REFCNT(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3731 | _Py_NewReference(self); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3732 | Py_SET_REFCNT(self, refcnt); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3733 | } |
Pablo Galindo | f13072b | 2020-04-11 01:21:54 +0100 | [diff] [blame] | 3734 | assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self)); |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 3735 | /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased |
| 3736 | _Py_RefTotal, so we need to undo that. */ |
| 3737 | #ifdef Py_REF_DEBUG |
| 3738 | _Py_RefTotal--; |
| 3739 | #endif |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | static PyObject * |
| 3743 | with_tp_del(PyObject *self, PyObject *args) |
| 3744 | { |
| 3745 | PyObject *obj; |
| 3746 | PyTypeObject *tp; |
| 3747 | |
| 3748 | if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) |
| 3749 | return NULL; |
| 3750 | tp = (PyTypeObject *) obj; |
| 3751 | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
| 3752 | PyErr_Format(PyExc_TypeError, |
| 3753 | "heap type expected, got %R", obj); |
| 3754 | return NULL; |
| 3755 | } |
| 3756 | tp->tp_del = slot_tp_del; |
| 3757 | Py_INCREF(obj); |
| 3758 | return obj; |
| 3759 | } |
| 3760 | |
Brandt Bucher | c13b847 | 2020-10-14 18:44:07 -0700 | [diff] [blame] | 3761 | static PyObject * |
| 3762 | without_gc(PyObject *Py_UNUSED(self), PyObject *obj) |
| 3763 | { |
| 3764 | PyTypeObject *tp = (PyTypeObject*)obj; |
| 3765 | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
| 3766 | return PyErr_Format(PyExc_TypeError, "heap type expected, got %R", obj); |
| 3767 | } |
| 3768 | if (PyType_IS_GC(tp)) { |
| 3769 | // Don't try this at home, kids: |
| 3770 | tp->tp_flags -= Py_TPFLAGS_HAVE_GC; |
| 3771 | tp->tp_free = PyObject_Del; |
| 3772 | tp->tp_traverse = NULL; |
| 3773 | tp->tp_clear = NULL; |
| 3774 | } |
| 3775 | assert(!PyType_IS_GC(tp)); |
| 3776 | Py_INCREF(obj); |
| 3777 | return obj; |
| 3778 | } |
| 3779 | |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 3780 | static PyMethodDef ml; |
| 3781 | |
| 3782 | static PyObject * |
| 3783 | create_cfunction(PyObject *self, PyObject *args) |
| 3784 | { |
| 3785 | return PyCFunction_NewEx(&ml, self, NULL); |
| 3786 | } |
| 3787 | |
| 3788 | static PyMethodDef ml = { |
| 3789 | "create_cfunction", |
| 3790 | create_cfunction, |
| 3791 | METH_NOARGS, |
| 3792 | NULL |
| 3793 | }; |
| 3794 | |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3795 | static PyObject * |
| 3796 | _test_incref(PyObject *ob) |
| 3797 | { |
| 3798 | Py_INCREF(ob); |
| 3799 | return ob; |
| 3800 | } |
| 3801 | |
| 3802 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3803 | test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3804 | { |
| 3805 | PyObject *obj = PyLong_FromLong(0); |
| 3806 | Py_XINCREF(_test_incref(obj)); |
| 3807 | Py_DECREF(obj); |
| 3808 | Py_DECREF(obj); |
| 3809 | Py_DECREF(obj); |
| 3810 | Py_RETURN_NONE; |
| 3811 | } |
| 3812 | |
| 3813 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3814 | test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3815 | { |
| 3816 | PyObject *obj = PyLong_FromLong(0); |
| 3817 | Py_INCREF(_test_incref(obj)); |
| 3818 | Py_DECREF(obj); |
| 3819 | Py_DECREF(obj); |
| 3820 | Py_DECREF(obj); |
| 3821 | Py_RETURN_NONE; |
| 3822 | } |
| 3823 | |
| 3824 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3825 | test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3826 | { |
| 3827 | Py_XDECREF(PyLong_FromLong(0)); |
| 3828 | Py_RETURN_NONE; |
| 3829 | } |
| 3830 | |
| 3831 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3832 | test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3833 | { |
| 3834 | Py_DECREF(PyLong_FromLong(0)); |
| 3835 | Py_RETURN_NONE; |
| 3836 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3837 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3838 | static PyObject * |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 3839 | test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self), |
| 3840 | PyObject *Py_UNUSED(args)) |
| 3841 | { |
| 3842 | PyStructSequence_Desc descr; |
| 3843 | PyStructSequence_Field descr_fields[3]; |
| 3844 | |
| 3845 | descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"}; |
| 3846 | descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"}; |
| 3847 | descr_fields[2] = (PyStructSequence_Field){0, NULL}; |
| 3848 | |
| 3849 | descr.name = "_testcapi.test_descr"; |
| 3850 | descr.doc = "This is used to test for memory leaks in NewType"; |
| 3851 | descr.fields = descr_fields; |
| 3852 | descr.n_in_sequence = 1; |
| 3853 | |
| 3854 | PyTypeObject* structseq_type = PyStructSequence_NewType(&descr); |
| 3855 | assert(structseq_type != NULL); |
| 3856 | assert(PyType_Check(structseq_type)); |
| 3857 | assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS)); |
| 3858 | Py_DECREF(structseq_type); |
| 3859 | |
| 3860 | Py_RETURN_NONE; |
| 3861 | } |
| 3862 | |
| 3863 | static PyObject * |
Miss Islington (bot) | 912ef3f | 2021-05-04 05:29:56 -0700 | [diff] [blame] | 3864 | test_structseq_newtype_null_descr_doc(PyObject *Py_UNUSED(self), |
| 3865 | PyObject *Py_UNUSED(args)) |
| 3866 | { |
| 3867 | PyStructSequence_Field descr_fields[1] = { |
| 3868 | (PyStructSequence_Field){NULL, NULL} |
| 3869 | }; |
| 3870 | // Test specifically for NULL .doc field. |
| 3871 | PyStructSequence_Desc descr = {"_testcapi.test_descr", NULL, &descr_fields[0], 0}; |
| 3872 | |
| 3873 | PyTypeObject* structseq_type = PyStructSequence_NewType(&descr); |
| 3874 | assert(structseq_type != NULL); |
| 3875 | assert(PyType_Check(structseq_type)); |
| 3876 | assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS)); |
| 3877 | Py_DECREF(structseq_type); |
| 3878 | |
| 3879 | Py_RETURN_NONE; |
| 3880 | } |
| 3881 | |
| 3882 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3883 | test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3884 | { |
| 3885 | PyObject *obj = PyLong_FromLong(0); |
Victor Stinner | fc6a90a | 2014-10-09 22:15:41 +0200 | [diff] [blame] | 3886 | Py_IncRef(obj); |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3887 | Py_DecRef(obj); |
| 3888 | Py_DecRef(obj); |
| 3889 | Py_RETURN_NONE; |
| 3890 | } |
| 3891 | |
| 3892 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3893 | test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3894 | { |
| 3895 | void *ptr; |
| 3896 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3897 | ptr = PyMem_RawMalloc(0); |
| 3898 | if (ptr == NULL) { |
| 3899 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); |
| 3900 | return NULL; |
| 3901 | } |
| 3902 | PyMem_RawFree(ptr); |
| 3903 | |
| 3904 | ptr = PyMem_RawCalloc(0, 0); |
| 3905 | if (ptr == NULL) { |
| 3906 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); |
| 3907 | return NULL; |
| 3908 | } |
| 3909 | PyMem_RawFree(ptr); |
| 3910 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3911 | ptr = PyMem_Malloc(0); |
| 3912 | if (ptr == NULL) { |
| 3913 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); |
| 3914 | return NULL; |
| 3915 | } |
| 3916 | PyMem_Free(ptr); |
| 3917 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3918 | ptr = PyMem_Calloc(0, 0); |
| 3919 | if (ptr == NULL) { |
| 3920 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); |
| 3921 | return NULL; |
| 3922 | } |
| 3923 | PyMem_Free(ptr); |
| 3924 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3925 | ptr = PyObject_Malloc(0); |
| 3926 | if (ptr == NULL) { |
| 3927 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); |
| 3928 | return NULL; |
| 3929 | } |
| 3930 | PyObject_Free(ptr); |
| 3931 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3932 | ptr = PyObject_Calloc(0, 0); |
| 3933 | if (ptr == NULL) { |
| 3934 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); |
| 3935 | return NULL; |
| 3936 | } |
| 3937 | PyObject_Free(ptr); |
| 3938 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3939 | Py_RETURN_NONE; |
| 3940 | } |
| 3941 | |
| 3942 | typedef struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3943 | PyMemAllocatorEx alloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3944 | |
| 3945 | size_t malloc_size; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3946 | size_t calloc_nelem; |
| 3947 | size_t calloc_elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3948 | void *realloc_ptr; |
| 3949 | size_t realloc_new_size; |
| 3950 | void *free_ptr; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3951 | void *ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3952 | } alloc_hook_t; |
| 3953 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3954 | static void* hook_malloc(void* ctx, size_t size) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3955 | { |
| 3956 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3957 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3958 | hook->malloc_size = size; |
| 3959 | return hook->alloc.malloc(hook->alloc.ctx, size); |
| 3960 | } |
| 3961 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3962 | static void* hook_calloc(void* ctx, size_t nelem, size_t elsize) |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3963 | { |
| 3964 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3965 | hook->ctx = ctx; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3966 | hook->calloc_nelem = nelem; |
| 3967 | hook->calloc_elsize = elsize; |
| 3968 | return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); |
| 3969 | } |
| 3970 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3971 | static void* hook_realloc(void* ctx, void* ptr, size_t new_size) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3972 | { |
| 3973 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3974 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3975 | hook->realloc_ptr = ptr; |
| 3976 | hook->realloc_new_size = new_size; |
| 3977 | return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); |
| 3978 | } |
| 3979 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3980 | static void hook_free(void *ctx, void *ptr) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3981 | { |
| 3982 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3983 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3984 | hook->free_ptr = ptr; |
| 3985 | hook->alloc.free(hook->alloc.ctx, ptr); |
| 3986 | } |
| 3987 | |
| 3988 | static PyObject * |
| 3989 | test_setallocators(PyMemAllocatorDomain domain) |
| 3990 | { |
| 3991 | PyObject *res = NULL; |
| 3992 | const char *error_msg; |
| 3993 | alloc_hook_t hook; |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3994 | PyMemAllocatorEx alloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3995 | size_t size, size2, nelem, elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3996 | void *ptr, *ptr2; |
| 3997 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3998 | memset(&hook, 0, sizeof(hook)); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3999 | |
| 4000 | alloc.ctx = &hook; |
| 4001 | alloc.malloc = &hook_malloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 4002 | alloc.calloc = &hook_calloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4003 | alloc.realloc = &hook_realloc; |
| 4004 | alloc.free = &hook_free; |
| 4005 | PyMem_GetAllocator(domain, &hook.alloc); |
| 4006 | PyMem_SetAllocator(domain, &alloc); |
| 4007 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4008 | /* malloc, realloc, free */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4009 | size = 42; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4010 | hook.ctx = NULL; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4011 | switch(domain) |
| 4012 | { |
| 4013 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; |
| 4014 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; |
| 4015 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; |
| 4016 | default: ptr = NULL; break; |
| 4017 | } |
| 4018 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4019 | #define CHECK_CTX(FUNC) \ |
| 4020 | if (hook.ctx != &hook) { \ |
| 4021 | error_msg = FUNC " wrong context"; \ |
| 4022 | goto fail; \ |
| 4023 | } \ |
| 4024 | hook.ctx = NULL; /* reset for next check */ |
| 4025 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4026 | if (ptr == NULL) { |
| 4027 | error_msg = "malloc failed"; |
| 4028 | goto fail; |
| 4029 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4030 | CHECK_CTX("malloc"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4031 | if (hook.malloc_size != size) { |
| 4032 | error_msg = "malloc invalid size"; |
| 4033 | goto fail; |
| 4034 | } |
| 4035 | |
| 4036 | size2 = 200; |
| 4037 | switch(domain) |
| 4038 | { |
| 4039 | case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; |
| 4040 | case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; |
| 4041 | case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; |
Christian Heimes | 865d12a | 2013-08-02 11:10:51 +0200 | [diff] [blame] | 4042 | default: ptr2 = NULL; break; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | if (ptr2 == NULL) { |
| 4046 | error_msg = "realloc failed"; |
| 4047 | goto fail; |
| 4048 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4049 | CHECK_CTX("realloc"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4050 | if (hook.realloc_ptr != ptr |
| 4051 | || hook.realloc_new_size != size2) { |
| 4052 | error_msg = "realloc invalid parameters"; |
| 4053 | goto fail; |
| 4054 | } |
| 4055 | |
| 4056 | switch(domain) |
| 4057 | { |
| 4058 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; |
| 4059 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; |
| 4060 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; |
| 4061 | } |
| 4062 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4063 | CHECK_CTX("free"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4064 | if (hook.free_ptr != ptr2) { |
| 4065 | error_msg = "free invalid pointer"; |
| 4066 | goto fail; |
| 4067 | } |
| 4068 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4069 | /* calloc, free */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 4070 | nelem = 2; |
| 4071 | elsize = 5; |
| 4072 | switch(domain) |
| 4073 | { |
| 4074 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; |
| 4075 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; |
| 4076 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; |
| 4077 | default: ptr = NULL; break; |
| 4078 | } |
| 4079 | |
| 4080 | if (ptr == NULL) { |
| 4081 | error_msg = "calloc failed"; |
| 4082 | goto fail; |
| 4083 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4084 | CHECK_CTX("calloc"); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 4085 | if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { |
| 4086 | error_msg = "calloc invalid nelem or elsize"; |
| 4087 | goto fail; |
| 4088 | } |
| 4089 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4090 | hook.free_ptr = NULL; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 4091 | switch(domain) |
| 4092 | { |
| 4093 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; |
| 4094 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; |
| 4095 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; |
| 4096 | } |
| 4097 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4098 | CHECK_CTX("calloc free"); |
| 4099 | if (hook.free_ptr != ptr) { |
| 4100 | error_msg = "calloc free invalid pointer"; |
| 4101 | goto fail; |
| 4102 | } |
| 4103 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4104 | Py_INCREF(Py_None); |
| 4105 | res = Py_None; |
| 4106 | goto finally; |
| 4107 | |
| 4108 | fail: |
| 4109 | PyErr_SetString(PyExc_RuntimeError, error_msg); |
| 4110 | |
| 4111 | finally: |
| 4112 | PyMem_SetAllocator(domain, &hook.alloc); |
| 4113 | return res; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 4114 | |
| 4115 | #undef CHECK_CTX |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4119 | test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4120 | { |
| 4121 | return test_setallocators(PYMEM_DOMAIN_RAW); |
| 4122 | } |
| 4123 | |
| 4124 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4125 | test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4126 | { |
| 4127 | return test_setallocators(PYMEM_DOMAIN_MEM); |
| 4128 | } |
| 4129 | |
| 4130 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4131 | test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4132 | { |
| 4133 | return test_setallocators(PYMEM_DOMAIN_OBJ); |
| 4134 | } |
| 4135 | |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 4136 | /* Most part of the following code is inherited from the pyfailmalloc project |
| 4137 | * written by Victor Stinner. */ |
| 4138 | static struct { |
| 4139 | int installed; |
| 4140 | PyMemAllocatorEx raw; |
| 4141 | PyMemAllocatorEx mem; |
| 4142 | PyMemAllocatorEx obj; |
| 4143 | } FmHook; |
| 4144 | |
| 4145 | static struct { |
| 4146 | int start; |
| 4147 | int stop; |
| 4148 | Py_ssize_t count; |
| 4149 | } FmData; |
| 4150 | |
| 4151 | static int |
| 4152 | fm_nomemory(void) |
| 4153 | { |
| 4154 | FmData.count++; |
| 4155 | if (FmData.count > FmData.start && |
| 4156 | (FmData.stop <= 0 || FmData.count <= FmData.stop)) { |
| 4157 | return 1; |
| 4158 | } |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
| 4162 | static void * |
| 4163 | hook_fmalloc(void *ctx, size_t size) |
| 4164 | { |
| 4165 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4166 | if (fm_nomemory()) { |
| 4167 | return NULL; |
| 4168 | } |
| 4169 | return alloc->malloc(alloc->ctx, size); |
| 4170 | } |
| 4171 | |
| 4172 | static void * |
| 4173 | hook_fcalloc(void *ctx, size_t nelem, size_t elsize) |
| 4174 | { |
| 4175 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4176 | if (fm_nomemory()) { |
| 4177 | return NULL; |
| 4178 | } |
| 4179 | return alloc->calloc(alloc->ctx, nelem, elsize); |
| 4180 | } |
| 4181 | |
| 4182 | static void * |
| 4183 | hook_frealloc(void *ctx, void *ptr, size_t new_size) |
| 4184 | { |
| 4185 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4186 | if (fm_nomemory()) { |
| 4187 | return NULL; |
| 4188 | } |
| 4189 | return alloc->realloc(alloc->ctx, ptr, new_size); |
| 4190 | } |
| 4191 | |
| 4192 | static void |
| 4193 | hook_ffree(void *ctx, void *ptr) |
| 4194 | { |
| 4195 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4196 | alloc->free(alloc->ctx, ptr); |
| 4197 | } |
| 4198 | |
| 4199 | static void |
| 4200 | fm_setup_hooks(void) |
| 4201 | { |
| 4202 | PyMemAllocatorEx alloc; |
| 4203 | |
| 4204 | if (FmHook.installed) { |
| 4205 | return; |
| 4206 | } |
| 4207 | FmHook.installed = 1; |
| 4208 | |
| 4209 | alloc.malloc = hook_fmalloc; |
| 4210 | alloc.calloc = hook_fcalloc; |
| 4211 | alloc.realloc = hook_frealloc; |
| 4212 | alloc.free = hook_ffree; |
| 4213 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 4214 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 4215 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 4216 | |
| 4217 | alloc.ctx = &FmHook.raw; |
| 4218 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 4219 | |
| 4220 | alloc.ctx = &FmHook.mem; |
| 4221 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 4222 | |
| 4223 | alloc.ctx = &FmHook.obj; |
| 4224 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 4225 | } |
| 4226 | |
| 4227 | static void |
| 4228 | fm_remove_hooks(void) |
| 4229 | { |
| 4230 | if (FmHook.installed) { |
| 4231 | FmHook.installed = 0; |
| 4232 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 4233 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 4234 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | static PyObject* |
| 4239 | set_nomemory(PyObject *self, PyObject *args) |
| 4240 | { |
| 4241 | /* Memory allocation fails after 'start' allocation requests, and until |
| 4242 | * 'stop' allocation requests except when 'stop' is negative or equal |
| 4243 | * to 0 (default) in which case allocation failures never stop. */ |
| 4244 | FmData.count = 0; |
| 4245 | FmData.stop = 0; |
| 4246 | if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) { |
| 4247 | return NULL; |
| 4248 | } |
| 4249 | fm_setup_hooks(); |
| 4250 | Py_RETURN_NONE; |
| 4251 | } |
| 4252 | |
| 4253 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4254 | remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored)) |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 4255 | { |
| 4256 | fm_remove_hooks(); |
| 4257 | Py_RETURN_NONE; |
| 4258 | } |
| 4259 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4260 | PyDoc_STRVAR(docstring_empty, |
| 4261 | "" |
| 4262 | ); |
| 4263 | |
| 4264 | PyDoc_STRVAR(docstring_no_signature, |
| 4265 | "This docstring has no signature." |
| 4266 | ); |
| 4267 | |
| 4268 | PyDoc_STRVAR(docstring_with_invalid_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4269 | "docstring_with_invalid_signature($module, /, boo)\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4270 | "\n" |
| 4271 | "This docstring has an invalid signature." |
| 4272 | ); |
| 4273 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4274 | PyDoc_STRVAR(docstring_with_invalid_signature2, |
| 4275 | "docstring_with_invalid_signature2($module, /, boo)\n" |
| 4276 | "\n" |
| 4277 | "--\n" |
| 4278 | "\n" |
| 4279 | "This docstring also has an invalid signature." |
| 4280 | ); |
| 4281 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4282 | PyDoc_STRVAR(docstring_with_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4283 | "docstring_with_signature($module, /, sig)\n" |
| 4284 | "--\n" |
| 4285 | "\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4286 | "This docstring has a valid signature." |
| 4287 | ); |
| 4288 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 4289 | PyDoc_STRVAR(docstring_with_signature_but_no_doc, |
| 4290 | "docstring_with_signature_but_no_doc($module, /, sig)\n" |
| 4291 | "--\n" |
| 4292 | "\n" |
| 4293 | ); |
| 4294 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4295 | PyDoc_STRVAR(docstring_with_signature_and_extra_newlines, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4296 | "docstring_with_signature_and_extra_newlines($module, /, parameter)\n" |
| 4297 | "--\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4298 | "\n" |
| 4299 | "\n" |
| 4300 | "This docstring has a valid signature and some extra newlines." |
| 4301 | ); |
| 4302 | |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4303 | PyDoc_STRVAR(docstring_with_signature_with_defaults, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4304 | "docstring_with_signature_with_defaults(module, s='avocado',\n" |
| 4305 | " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n" |
| 4306 | " local=the_number_three, sys=sys.maxsize,\n" |
| 4307 | " exp=sys.maxsize - 1)\n" |
| 4308 | "--\n" |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4309 | "\n" |
| 4310 | "\n" |
| 4311 | "\n" |
| 4312 | "This docstring has a valid signature with parameters,\n" |
| 4313 | "and the parameters take defaults of varying types." |
| 4314 | ); |
| 4315 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4316 | typedef struct { |
| 4317 | PyThread_type_lock start_event; |
| 4318 | PyThread_type_lock exit_event; |
| 4319 | PyObject *callback; |
| 4320 | } test_c_thread_t; |
| 4321 | |
| 4322 | static void |
| 4323 | temporary_c_thread(void *data) |
| 4324 | { |
| 4325 | test_c_thread_t *test_c_thread = data; |
| 4326 | PyGILState_STATE state; |
| 4327 | PyObject *res; |
| 4328 | |
| 4329 | PyThread_release_lock(test_c_thread->start_event); |
| 4330 | |
| 4331 | /* Allocate a Python thread state for this thread */ |
| 4332 | state = PyGILState_Ensure(); |
| 4333 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 4334 | res = _PyObject_CallNoArg(test_c_thread->callback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4335 | Py_CLEAR(test_c_thread->callback); |
| 4336 | |
| 4337 | if (res == NULL) { |
| 4338 | PyErr_Print(); |
| 4339 | } |
| 4340 | else { |
| 4341 | Py_DECREF(res); |
| 4342 | } |
| 4343 | |
| 4344 | /* Destroy the Python thread state for this thread */ |
| 4345 | PyGILState_Release(state); |
| 4346 | |
| 4347 | PyThread_release_lock(test_c_thread->exit_event); |
| 4348 | |
| 4349 | PyThread_exit_thread(); |
| 4350 | } |
| 4351 | |
| 4352 | static PyObject * |
| 4353 | call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
| 4354 | { |
| 4355 | PyObject *res = NULL; |
| 4356 | test_c_thread_t test_c_thread; |
| 4357 | long thread; |
| 4358 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4359 | test_c_thread.start_event = PyThread_allocate_lock(); |
| 4360 | test_c_thread.exit_event = PyThread_allocate_lock(); |
| 4361 | test_c_thread.callback = NULL; |
| 4362 | if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
| 4363 | PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
| 4364 | goto exit; |
| 4365 | } |
| 4366 | |
| 4367 | Py_INCREF(callback); |
| 4368 | test_c_thread.callback = callback; |
| 4369 | |
| 4370 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 4371 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 4372 | |
| 4373 | thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
| 4374 | if (thread == -1) { |
| 4375 | PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
| 4376 | PyThread_release_lock(test_c_thread.start_event); |
| 4377 | PyThread_release_lock(test_c_thread.exit_event); |
| 4378 | goto exit; |
| 4379 | } |
| 4380 | |
| 4381 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 4382 | PyThread_release_lock(test_c_thread.start_event); |
| 4383 | |
| 4384 | Py_BEGIN_ALLOW_THREADS |
| 4385 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 4386 | PyThread_release_lock(test_c_thread.exit_event); |
| 4387 | Py_END_ALLOW_THREADS |
| 4388 | |
| 4389 | Py_INCREF(Py_None); |
| 4390 | res = Py_None; |
| 4391 | |
| 4392 | exit: |
| 4393 | Py_CLEAR(test_c_thread.callback); |
| 4394 | if (test_c_thread.start_event) |
| 4395 | PyThread_free_lock(test_c_thread.start_event); |
| 4396 | if (test_c_thread.exit_event) |
| 4397 | PyThread_free_lock(test_c_thread.exit_event); |
| 4398 | return res; |
| 4399 | } |
Victor Stinner | 1310510 | 2013-12-13 02:17:29 +0100 | [diff] [blame] | 4400 | |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4401 | /* marshal */ |
| 4402 | |
| 4403 | static PyObject* |
| 4404 | pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
| 4405 | { |
| 4406 | long value; |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4407 | PyObject *filename; |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4408 | int version; |
| 4409 | FILE *fp; |
| 4410 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4411 | if (!PyArg_ParseTuple(args, "lOi:pymarshal_write_long_to_file", |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4412 | &value, &filename, &version)) |
| 4413 | return NULL; |
| 4414 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4415 | fp = _Py_fopen_obj(filename, "wb"); |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4416 | if (fp == NULL) { |
| 4417 | PyErr_SetFromErrno(PyExc_OSError); |
| 4418 | return NULL; |
| 4419 | } |
| 4420 | |
| 4421 | PyMarshal_WriteLongToFile(value, fp, version); |
| 4422 | |
| 4423 | fclose(fp); |
| 4424 | if (PyErr_Occurred()) |
| 4425 | return NULL; |
| 4426 | Py_RETURN_NONE; |
| 4427 | } |
| 4428 | |
| 4429 | static PyObject* |
| 4430 | pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
| 4431 | { |
| 4432 | PyObject *obj; |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4433 | PyObject *filename; |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4434 | int version; |
| 4435 | FILE *fp; |
| 4436 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4437 | if (!PyArg_ParseTuple(args, "OOi:pymarshal_write_object_to_file", |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4438 | &obj, &filename, &version)) |
| 4439 | return NULL; |
| 4440 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4441 | fp = _Py_fopen_obj(filename, "wb"); |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4442 | if (fp == NULL) { |
| 4443 | PyErr_SetFromErrno(PyExc_OSError); |
| 4444 | return NULL; |
| 4445 | } |
| 4446 | |
| 4447 | PyMarshal_WriteObjectToFile(obj, fp, version); |
| 4448 | |
| 4449 | fclose(fp); |
| 4450 | if (PyErr_Occurred()) |
| 4451 | return NULL; |
| 4452 | Py_RETURN_NONE; |
| 4453 | } |
| 4454 | |
| 4455 | static PyObject* |
| 4456 | pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
| 4457 | { |
| 4458 | int value; |
| 4459 | long pos; |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4460 | PyObject *filename; |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4461 | FILE *fp; |
| 4462 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4463 | if (!PyArg_ParseTuple(args, "O:pymarshal_read_short_from_file", &filename)) |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4464 | return NULL; |
| 4465 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4466 | fp = _Py_fopen_obj(filename, "rb"); |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4467 | if (fp == NULL) { |
| 4468 | PyErr_SetFromErrno(PyExc_OSError); |
| 4469 | return NULL; |
| 4470 | } |
| 4471 | |
| 4472 | value = PyMarshal_ReadShortFromFile(fp); |
| 4473 | pos = ftell(fp); |
| 4474 | |
| 4475 | fclose(fp); |
| 4476 | if (PyErr_Occurred()) |
| 4477 | return NULL; |
| 4478 | return Py_BuildValue("il", value, pos); |
| 4479 | } |
| 4480 | |
| 4481 | static PyObject* |
| 4482 | pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
| 4483 | { |
| 4484 | long value, pos; |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4485 | PyObject *filename; |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4486 | FILE *fp; |
| 4487 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4488 | if (!PyArg_ParseTuple(args, "O:pymarshal_read_long_from_file", &filename)) |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4489 | return NULL; |
| 4490 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4491 | fp = _Py_fopen_obj(filename, "rb"); |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4492 | if (fp == NULL) { |
| 4493 | PyErr_SetFromErrno(PyExc_OSError); |
| 4494 | return NULL; |
| 4495 | } |
| 4496 | |
| 4497 | value = PyMarshal_ReadLongFromFile(fp); |
| 4498 | pos = ftell(fp); |
| 4499 | |
| 4500 | fclose(fp); |
| 4501 | if (PyErr_Occurred()) |
| 4502 | return NULL; |
| 4503 | return Py_BuildValue("ll", value, pos); |
| 4504 | } |
| 4505 | |
| 4506 | static PyObject* |
| 4507 | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
| 4508 | { |
| 4509 | PyObject *obj; |
| 4510 | long pos; |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4511 | PyObject *filename; |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4512 | FILE *fp; |
| 4513 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4514 | if (!PyArg_ParseTuple(args, "O:pymarshal_read_last_object_from_file", &filename)) |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4515 | return NULL; |
| 4516 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4517 | fp = _Py_fopen_obj(filename, "rb"); |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4518 | if (fp == NULL) { |
| 4519 | PyErr_SetFromErrno(PyExc_OSError); |
| 4520 | return NULL; |
| 4521 | } |
| 4522 | |
| 4523 | obj = PyMarshal_ReadLastObjectFromFile(fp); |
| 4524 | pos = ftell(fp); |
| 4525 | |
| 4526 | fclose(fp); |
| 4527 | return Py_BuildValue("Nl", obj, pos); |
| 4528 | } |
| 4529 | |
| 4530 | static PyObject* |
| 4531 | pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
| 4532 | { |
| 4533 | PyObject *obj; |
| 4534 | long pos; |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4535 | PyObject *filename; |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4536 | FILE *fp; |
| 4537 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4538 | if (!PyArg_ParseTuple(args, "O:pymarshal_read_object_from_file", &filename)) |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4539 | return NULL; |
| 4540 | |
Serhiy Storchaka | 700cfa8 | 2020-06-25 17:56:31 +0300 | [diff] [blame] | 4541 | fp = _Py_fopen_obj(filename, "rb"); |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4542 | if (fp == NULL) { |
| 4543 | PyErr_SetFromErrno(PyExc_OSError); |
| 4544 | return NULL; |
| 4545 | } |
| 4546 | |
| 4547 | obj = PyMarshal_ReadObjectFromFile(fp); |
| 4548 | pos = ftell(fp); |
| 4549 | |
| 4550 | fclose(fp); |
| 4551 | return Py_BuildValue("Nl", obj, pos); |
| 4552 | } |
| 4553 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4554 | static PyObject* |
| 4555 | return_null_without_error(PyObject *self, PyObject *args) |
| 4556 | { |
| 4557 | /* invalid call: return NULL without setting an error, |
| 4558 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 4559 | PyErr_Clear(); |
| 4560 | return NULL; |
| 4561 | } |
| 4562 | |
| 4563 | static PyObject* |
| 4564 | return_result_with_error(PyObject *self, PyObject *args) |
| 4565 | { |
| 4566 | /* invalid call: return a result with an error set, |
| 4567 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 4568 | PyErr_SetNone(PyExc_ValueError); |
| 4569 | Py_RETURN_NONE; |
| 4570 | } |
| 4571 | |
Victor Stinner | c9b8e9c | 2021-01-27 17:39:16 +0100 | [diff] [blame] | 4572 | static PyObject* |
| 4573 | getitem_with_error(PyObject *self, PyObject *args) |
| 4574 | { |
| 4575 | PyObject *map, *key; |
| 4576 | if (!PyArg_ParseTuple(args, "OO", &map, &key)) { |
| 4577 | return NULL; |
| 4578 | } |
| 4579 | |
| 4580 | PyErr_SetString(PyExc_ValueError, "bug"); |
| 4581 | return PyObject_GetItem(map, key); |
| 4582 | } |
| 4583 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 4584 | static PyObject * |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 4585 | test_pytime_fromseconds(PyObject *self, PyObject *args) |
| 4586 | { |
| 4587 | int seconds; |
| 4588 | _PyTime_t ts; |
| 4589 | |
| 4590 | if (!PyArg_ParseTuple(args, "i", &seconds)) |
| 4591 | return NULL; |
| 4592 | ts = _PyTime_FromSeconds(seconds); |
| 4593 | return _PyTime_AsNanosecondsObject(ts); |
| 4594 | } |
| 4595 | |
| 4596 | static PyObject * |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 4597 | test_pytime_fromsecondsobject(PyObject *self, PyObject *args) |
| 4598 | { |
| 4599 | PyObject *obj; |
| 4600 | int round; |
| 4601 | _PyTime_t ts; |
| 4602 | |
| 4603 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
| 4604 | return NULL; |
| 4605 | if (check_time_rounding(round) < 0) |
| 4606 | return NULL; |
| 4607 | if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) |
| 4608 | return NULL; |
| 4609 | return _PyTime_AsNanosecondsObject(ts); |
| 4610 | } |
| 4611 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4612 | static PyObject * |
| 4613 | test_pytime_assecondsdouble(PyObject *self, PyObject *args) |
| 4614 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4615 | PyObject *obj; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4616 | _PyTime_t ts; |
| 4617 | double d; |
| 4618 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4619 | if (!PyArg_ParseTuple(args, "O", &obj)) { |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4620 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4621 | } |
| 4622 | if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) { |
| 4623 | return NULL; |
| 4624 | } |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4625 | d = _PyTime_AsSecondsDouble(ts); |
| 4626 | return PyFloat_FromDouble(d); |
| 4627 | } |
| 4628 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4629 | static PyObject * |
| 4630 | test_PyTime_AsTimeval(PyObject *self, PyObject *args) |
| 4631 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4632 | PyObject *obj; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4633 | int round; |
| 4634 | _PyTime_t t; |
| 4635 | struct timeval tv; |
| 4636 | PyObject *seconds; |
| 4637 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4638 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4639 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4640 | if (check_time_rounding(round) < 0) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4641 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4642 | } |
| 4643 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4644 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4645 | } |
| 4646 | if (_PyTime_AsTimeval(t, &tv, round) < 0) { |
| 4647 | return NULL; |
| 4648 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4649 | |
Benjamin Peterson | 2c134c3 | 2017-04-13 01:44:54 -0700 | [diff] [blame] | 4650 | seconds = PyLong_FromLongLong(tv.tv_sec); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4651 | if (seconds == NULL) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4652 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4653 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4654 | return Py_BuildValue("Nl", seconds, tv.tv_usec); |
| 4655 | } |
| 4656 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4657 | #ifdef HAVE_CLOCK_GETTIME |
| 4658 | static PyObject * |
| 4659 | test_PyTime_AsTimespec(PyObject *self, PyObject *args) |
| 4660 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4661 | PyObject *obj; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4662 | _PyTime_t t; |
| 4663 | struct timespec ts; |
| 4664 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4665 | if (!PyArg_ParseTuple(args, "O", &obj)) { |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4666 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4667 | } |
| 4668 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4669 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4670 | } |
| 4671 | if (_PyTime_AsTimespec(t, &ts) == -1) { |
| 4672 | return NULL; |
| 4673 | } |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4674 | return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); |
| 4675 | } |
| 4676 | #endif |
| 4677 | |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4678 | static PyObject * |
| 4679 | test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) |
| 4680 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4681 | PyObject *obj; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4682 | int round; |
| 4683 | _PyTime_t t, ms; |
| 4684 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4685 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4686 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4687 | } |
| 4688 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4689 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4690 | } |
| 4691 | if (check_time_rounding(round) < 0) { |
| 4692 | return NULL; |
| 4693 | } |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4694 | ms = _PyTime_AsMilliseconds(t, round); |
| 4695 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4696 | nanoseconds */ |
| 4697 | return _PyTime_AsNanosecondsObject(ms); |
| 4698 | } |
| 4699 | |
| 4700 | static PyObject * |
| 4701 | test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) |
| 4702 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4703 | PyObject *obj; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4704 | int round; |
| 4705 | _PyTime_t t, ms; |
| 4706 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4707 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4708 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4709 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4710 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4711 | } |
| 4712 | if (check_time_rounding(round) < 0) { |
| 4713 | return NULL; |
| 4714 | } |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4715 | ms = _PyTime_AsMicroseconds(t, round); |
| 4716 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4717 | nanoseconds */ |
| 4718 | return _PyTime_AsNanosecondsObject(ms); |
| 4719 | } |
| 4720 | |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4721 | static PyObject* |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4722 | pymem_buffer_overflow(PyObject *self, PyObject *args) |
| 4723 | { |
| 4724 | char *buffer; |
| 4725 | |
| 4726 | /* Deliberate buffer overflow to check that PyMem_Free() detects |
| 4727 | the overflow when debug hooks are installed. */ |
| 4728 | buffer = PyMem_Malloc(16); |
Victor Stinner | 414b1cd | 2019-03-26 14:35:30 +0100 | [diff] [blame] | 4729 | if (buffer == NULL) { |
| 4730 | PyErr_NoMemory(); |
| 4731 | return NULL; |
| 4732 | } |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4733 | buffer[16] = 'x'; |
| 4734 | PyMem_Free(buffer); |
| 4735 | |
| 4736 | Py_RETURN_NONE; |
| 4737 | } |
| 4738 | |
| 4739 | static PyObject* |
| 4740 | pymem_api_misuse(PyObject *self, PyObject *args) |
| 4741 | { |
| 4742 | char *buffer; |
| 4743 | |
| 4744 | /* Deliberate misusage of Python allocators: |
| 4745 | allococate with PyMem but release with PyMem_Raw. */ |
| 4746 | buffer = PyMem_Malloc(16); |
| 4747 | PyMem_RawFree(buffer); |
| 4748 | |
| 4749 | Py_RETURN_NONE; |
| 4750 | } |
| 4751 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4752 | static PyObject* |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4753 | pymem_malloc_without_gil(PyObject *self, PyObject *args) |
| 4754 | { |
| 4755 | char *buffer; |
| 4756 | |
| 4757 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4758 | call PyMem_Malloc() without holding the GIL */ |
| 4759 | Py_BEGIN_ALLOW_THREADS |
| 4760 | buffer = PyMem_Malloc(10); |
| 4761 | Py_END_ALLOW_THREADS |
| 4762 | |
| 4763 | PyMem_Free(buffer); |
| 4764 | |
| 4765 | Py_RETURN_NONE; |
| 4766 | } |
| 4767 | |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 4768 | |
| 4769 | static PyObject* |
| 4770 | test_pymem_getallocatorsname(PyObject *self, PyObject *args) |
| 4771 | { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 4772 | const char *name = _PyMem_GetCurrentAllocatorName(); |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 4773 | if (name == NULL) { |
| 4774 | PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name"); |
| 4775 | return NULL; |
| 4776 | } |
| 4777 | return PyUnicode_FromString(name); |
| 4778 | } |
| 4779 | |
| 4780 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4781 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4782 | test_pyobject_is_freed(const char *test_name, PyObject *op) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4783 | { |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4784 | if (!_PyObject_IsFreed(op)) { |
| 4785 | return raiseTestError(test_name, "object is not seen as freed"); |
| 4786 | } |
| 4787 | Py_RETURN_NONE; |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | |
| 4791 | static PyObject* |
Victor Stinner | 6876257 | 2019-10-07 18:42:01 +0200 | [diff] [blame] | 4792 | check_pyobject_null_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
| 4793 | { |
| 4794 | PyObject *op = NULL; |
| 4795 | return test_pyobject_is_freed("check_pyobject_null_is_freed", op); |
| 4796 | } |
| 4797 | |
| 4798 | |
| 4799 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4800 | check_pyobject_uninitialized_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4801 | { |
| 4802 | PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject)); |
| 4803 | if (op == NULL) { |
| 4804 | return NULL; |
| 4805 | } |
| 4806 | /* Initialize reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4807 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4808 | /* object fields like ob_type are uninitialized! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4809 | return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | |
| 4813 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4814 | check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4815 | { |
| 4816 | /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */ |
| 4817 | PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type)); |
| 4818 | if (op == NULL) { |
| 4819 | return NULL; |
| 4820 | } |
| 4821 | /* Initialize reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4822 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4823 | /* 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] | 4824 | when using debug hooks on memory allocators! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4825 | return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | |
| 4829 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4830 | check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4831 | { |
Miss Islington (bot) | 0895e62c | 2021-06-10 05:02:22 -0700 | [diff] [blame] | 4832 | /* This test would fail if run with the address sanitizer */ |
| 4833 | #ifdef _Py_ADDRESS_SANITIZER |
| 4834 | Py_RETURN_NONE; |
| 4835 | #else |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4836 | PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); |
| 4837 | if (op == NULL) { |
| 4838 | return NULL; |
| 4839 | } |
| 4840 | Py_TYPE(op)->tp_dealloc(op); |
| 4841 | /* Reset reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4842 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4843 | /* object memory is freed! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4844 | return test_pyobject_is_freed("check_pyobject_freed_is_freed", op); |
Miss Islington (bot) | 0895e62c | 2021-06-10 05:02:22 -0700 | [diff] [blame] | 4845 | #endif |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | |
| 4849 | static PyObject* |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4850 | pyobject_malloc_without_gil(PyObject *self, PyObject *args) |
| 4851 | { |
| 4852 | char *buffer; |
| 4853 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4854 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4855 | call PyObject_Malloc() without holding the GIL */ |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4856 | Py_BEGIN_ALLOW_THREADS |
| 4857 | buffer = PyObject_Malloc(10); |
| 4858 | Py_END_ALLOW_THREADS |
| 4859 | |
| 4860 | PyObject_Free(buffer); |
| 4861 | |
| 4862 | Py_RETURN_NONE; |
| 4863 | } |
| 4864 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4865 | static PyObject * |
| 4866 | tracemalloc_track(PyObject *self, PyObject *args) |
| 4867 | { |
| 4868 | unsigned int domain; |
| 4869 | PyObject *ptr_obj; |
| 4870 | void *ptr; |
| 4871 | Py_ssize_t size; |
| 4872 | int release_gil = 0; |
| 4873 | int res; |
| 4874 | |
| 4875 | if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil)) |
| 4876 | return NULL; |
| 4877 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4878 | if (PyErr_Occurred()) |
| 4879 | return NULL; |
| 4880 | |
| 4881 | if (release_gil) { |
| 4882 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4883 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4884 | Py_END_ALLOW_THREADS |
| 4885 | } |
| 4886 | else { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4887 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4891 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4892 | return NULL; |
| 4893 | } |
| 4894 | |
| 4895 | Py_RETURN_NONE; |
| 4896 | } |
| 4897 | |
| 4898 | static PyObject * |
| 4899 | tracemalloc_untrack(PyObject *self, PyObject *args) |
| 4900 | { |
| 4901 | unsigned int domain; |
| 4902 | PyObject *ptr_obj; |
| 4903 | void *ptr; |
| 4904 | int res; |
| 4905 | |
| 4906 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4907 | return NULL; |
| 4908 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4909 | if (PyErr_Occurred()) |
| 4910 | return NULL; |
| 4911 | |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4912 | res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4913 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4914 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4915 | return NULL; |
| 4916 | } |
| 4917 | |
| 4918 | Py_RETURN_NONE; |
| 4919 | } |
| 4920 | |
| 4921 | static PyObject * |
| 4922 | tracemalloc_get_traceback(PyObject *self, PyObject *args) |
| 4923 | { |
| 4924 | unsigned int domain; |
| 4925 | PyObject *ptr_obj; |
| 4926 | void *ptr; |
| 4927 | |
| 4928 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4929 | return NULL; |
| 4930 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4931 | if (PyErr_Occurred()) |
| 4932 | return NULL; |
| 4933 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 4934 | return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4935 | } |
| 4936 | |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4937 | static PyObject * |
| 4938 | dict_get_version(PyObject *self, PyObject *args) |
| 4939 | { |
| 4940 | PyDictObject *dict; |
| 4941 | uint64_t version; |
| 4942 | |
| 4943 | if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict)) |
| 4944 | return NULL; |
| 4945 | |
| 4946 | version = dict->ma_version_tag; |
| 4947 | |
Sergey Fedoseev | a9ed91e | 2019-10-21 11:49:48 +0500 | [diff] [blame] | 4948 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(version)); |
| 4949 | return PyLong_FromUnsignedLongLong((unsigned long long)version); |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4950 | } |
| 4951 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4952 | |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 4953 | static PyObject * |
| 4954 | raise_SIGINT_then_send_None(PyObject *self, PyObject *args) |
| 4955 | { |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 4956 | _Py_IDENTIFIER(send); |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 4957 | PyGenObject *gen; |
| 4958 | |
| 4959 | if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen)) |
| 4960 | return NULL; |
| 4961 | |
| 4962 | /* This is used in a test to check what happens if a signal arrives just |
| 4963 | as we're in the process of entering a yield from chain (see |
| 4964 | bpo-30039). |
| 4965 | |
| 4966 | Needs to be done in C, because: |
| 4967 | - we don't have a Python wrapper for raise() |
| 4968 | - we need to make sure that the Python-level signal handler doesn't run |
| 4969 | *before* we enter the generator frame, which is impossible in Python |
| 4970 | because we check for signals before every bytecode operation. |
| 4971 | */ |
| 4972 | raise(SIGINT); |
Vladimir Matveev | 037245c | 2020-10-09 17:15:15 -0700 | [diff] [blame] | 4973 | return _PyObject_CallMethodIdOneArg((PyObject *)gen, &PyId_send, Py_None); |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4977 | static int |
| 4978 | fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs) |
| 4979 | { |
| 4980 | if (args == Py_None) { |
| 4981 | *stack = NULL; |
| 4982 | *nargs = 0; |
| 4983 | } |
| 4984 | else if (PyTuple_Check(args)) { |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 4985 | *stack = ((PyTupleObject *)args)->ob_item; |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4986 | *nargs = PyTuple_GET_SIZE(args); |
| 4987 | } |
| 4988 | else { |
| 4989 | PyErr_SetString(PyExc_TypeError, "args must be None or a tuple"); |
| 4990 | return -1; |
| 4991 | } |
| 4992 | return 0; |
| 4993 | } |
| 4994 | |
| 4995 | |
| 4996 | static PyObject * |
| 4997 | test_pyobject_fastcall(PyObject *self, PyObject *args) |
| 4998 | { |
| 4999 | PyObject *func, *func_args; |
| 5000 | PyObject **stack; |
| 5001 | Py_ssize_t nargs; |
| 5002 | |
| 5003 | if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) { |
| 5004 | return NULL; |
| 5005 | } |
| 5006 | |
| 5007 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 5008 | return NULL; |
| 5009 | } |
| 5010 | return _PyObject_FastCall(func, stack, nargs); |
| 5011 | } |
| 5012 | |
| 5013 | |
| 5014 | static PyObject * |
| 5015 | test_pyobject_fastcalldict(PyObject *self, PyObject *args) |
| 5016 | { |
| 5017 | PyObject *func, *func_args, *kwargs; |
| 5018 | PyObject **stack; |
| 5019 | Py_ssize_t nargs; |
| 5020 | |
| 5021 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) { |
| 5022 | return NULL; |
| 5023 | } |
| 5024 | |
| 5025 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 5026 | return NULL; |
| 5027 | } |
| 5028 | |
| 5029 | if (kwargs == Py_None) { |
| 5030 | kwargs = NULL; |
| 5031 | } |
| 5032 | else if (!PyDict_Check(kwargs)) { |
| 5033 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict"); |
| 5034 | return NULL; |
| 5035 | } |
| 5036 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5037 | return PyObject_VectorcallDict(func, stack, nargs, kwargs); |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | |
| 5041 | static PyObject * |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5042 | test_pyobject_vectorcall(PyObject *self, PyObject *args) |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 5043 | { |
| 5044 | PyObject *func, *func_args, *kwnames = NULL; |
| 5045 | PyObject **stack; |
| 5046 | Py_ssize_t nargs, nkw; |
| 5047 | |
| 5048 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) { |
| 5049 | return NULL; |
| 5050 | } |
| 5051 | |
| 5052 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 5053 | return NULL; |
| 5054 | } |
| 5055 | |
| 5056 | if (kwnames == Py_None) { |
| 5057 | kwnames = NULL; |
| 5058 | } |
| 5059 | else if (PyTuple_Check(kwnames)) { |
| 5060 | nkw = PyTuple_GET_SIZE(kwnames); |
| 5061 | if (nargs < nkw) { |
| 5062 | PyErr_SetString(PyExc_ValueError, "kwnames longer than args"); |
| 5063 | return NULL; |
| 5064 | } |
| 5065 | nargs -= nkw; |
| 5066 | } |
| 5067 | else { |
| 5068 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple"); |
| 5069 | return NULL; |
| 5070 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5071 | return PyObject_Vectorcall(func, stack, nargs, kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5072 | } |
| 5073 | |
| 5074 | |
| 5075 | static PyObject * |
| 5076 | test_pyvectorcall_call(PyObject *self, PyObject *args) |
| 5077 | { |
| 5078 | PyObject *func; |
| 5079 | PyObject *argstuple; |
| 5080 | PyObject *kwargs = NULL; |
| 5081 | |
| 5082 | if (!PyArg_ParseTuple(args, "OO|O", &func, &argstuple, &kwargs)) { |
| 5083 | return NULL; |
| 5084 | } |
| 5085 | |
| 5086 | if (!PyTuple_Check(argstuple)) { |
| 5087 | PyErr_SetString(PyExc_TypeError, "args must be a tuple"); |
| 5088 | return NULL; |
| 5089 | } |
| 5090 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
| 5091 | PyErr_SetString(PyExc_TypeError, "kwargs must be a dict"); |
| 5092 | return NULL; |
| 5093 | } |
| 5094 | |
| 5095 | return PyVectorcall_Call(func, argstuple, kwargs); |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 5096 | } |
| 5097 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 5098 | |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 5099 | static PyObject* |
| 5100 | stack_pointer(PyObject *self, PyObject *args) |
| 5101 | { |
| 5102 | int v = 5; |
| 5103 | return PyLong_FromVoidPtr(&v); |
| 5104 | } |
| 5105 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 5106 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 5107 | #ifdef W_STOPCODE |
| 5108 | static PyObject* |
| 5109 | py_w_stopcode(PyObject *self, PyObject *args) |
| 5110 | { |
| 5111 | int sig, status; |
| 5112 | if (!PyArg_ParseTuple(args, "i", &sig)) { |
| 5113 | return NULL; |
| 5114 | } |
| 5115 | status = W_STOPCODE(sig); |
| 5116 | return PyLong_FromLong(status); |
| 5117 | } |
| 5118 | #endif |
| 5119 | |
| 5120 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 5121 | static PyObject * |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 5122 | get_mapping_keys(PyObject* self, PyObject *obj) |
| 5123 | { |
| 5124 | return PyMapping_Keys(obj); |
| 5125 | } |
| 5126 | |
| 5127 | static PyObject * |
| 5128 | get_mapping_values(PyObject* self, PyObject *obj) |
| 5129 | { |
| 5130 | return PyMapping_Values(obj); |
| 5131 | } |
| 5132 | |
| 5133 | static PyObject * |
| 5134 | get_mapping_items(PyObject* self, PyObject *obj) |
| 5135 | { |
| 5136 | return PyMapping_Items(obj); |
| 5137 | } |
| 5138 | |
| 5139 | |
| 5140 | static PyObject * |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 5141 | test_pythread_tss_key_state(PyObject *self, PyObject *args) |
| 5142 | { |
| 5143 | Py_tss_t tss_key = Py_tss_NEEDS_INIT; |
| 5144 | if (PyThread_tss_is_created(&tss_key)) { |
| 5145 | return raiseTestError("test_pythread_tss_key_state", |
| 5146 | "TSS key not in an uninitialized state at " |
| 5147 | "creation time"); |
| 5148 | } |
| 5149 | if (PyThread_tss_create(&tss_key) != 0) { |
| 5150 | PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed"); |
| 5151 | return NULL; |
| 5152 | } |
| 5153 | if (!PyThread_tss_is_created(&tss_key)) { |
| 5154 | return raiseTestError("test_pythread_tss_key_state", |
| 5155 | "PyThread_tss_create succeeded, " |
| 5156 | "but with TSS key in an uninitialized state"); |
| 5157 | } |
| 5158 | if (PyThread_tss_create(&tss_key) != 0) { |
| 5159 | return raiseTestError("test_pythread_tss_key_state", |
| 5160 | "PyThread_tss_create unsuccessful with " |
| 5161 | "an already initialized key"); |
| 5162 | } |
| 5163 | #define CHECK_TSS_API(expr) \ |
| 5164 | (void)(expr); \ |
| 5165 | if (!PyThread_tss_is_created(&tss_key)) { \ |
| 5166 | return raiseTestError("test_pythread_tss_key_state", \ |
| 5167 | "TSS key initialization state was not " \ |
| 5168 | "preserved after calling " #expr); } |
| 5169 | CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL)); |
| 5170 | CHECK_TSS_API(PyThread_tss_get(&tss_key)); |
| 5171 | #undef CHECK_TSS_API |
| 5172 | PyThread_tss_delete(&tss_key); |
| 5173 | if (PyThread_tss_is_created(&tss_key)) { |
| 5174 | return raiseTestError("test_pythread_tss_key_state", |
| 5175 | "PyThread_tss_delete called, but did not " |
| 5176 | "set the key state to uninitialized"); |
| 5177 | } |
| 5178 | |
| 5179 | Py_tss_t *ptr_key = PyThread_tss_alloc(); |
| 5180 | if (ptr_key == NULL) { |
| 5181 | PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed"); |
| 5182 | return NULL; |
| 5183 | } |
| 5184 | if (PyThread_tss_is_created(ptr_key)) { |
| 5185 | return raiseTestError("test_pythread_tss_key_state", |
| 5186 | "TSS key not in an uninitialized state at " |
| 5187 | "allocation time"); |
| 5188 | } |
| 5189 | PyThread_tss_free(ptr_key); |
| 5190 | ptr_key = NULL; |
| 5191 | Py_RETURN_NONE; |
| 5192 | } |
| 5193 | |
| 5194 | |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 5195 | static PyObject* |
| 5196 | new_hamt(PyObject *self, PyObject *args) |
| 5197 | { |
| 5198 | return _PyContext_NewHamtForTests(); |
| 5199 | } |
| 5200 | |
| 5201 | |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5202 | /* def bad_get(self, obj, cls): |
| 5203 | cls() |
| 5204 | return repr(self) |
| 5205 | */ |
| 5206 | static PyObject* |
| 5207 | bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 5208 | { |
Serhiy Storchaka | b1dede3 | 2018-11-20 20:45:40 +0200 | [diff] [blame] | 5209 | PyObject *self, *obj, *cls; |
| 5210 | if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) { |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5211 | return NULL; |
| 5212 | } |
| 5213 | |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 5214 | PyObject *res = _PyObject_CallNoArg(cls); |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5215 | if (res == NULL) { |
| 5216 | return NULL; |
| 5217 | } |
| 5218 | Py_DECREF(res); |
| 5219 | |
Serhiy Storchaka | b1dede3 | 2018-11-20 20:45:40 +0200 | [diff] [blame] | 5220 | return PyObject_Repr(self); |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 5224 | static PyObject * |
| 5225 | encode_locale_ex(PyObject *self, PyObject *args) |
| 5226 | { |
| 5227 | PyObject *unicode; |
| 5228 | int current_locale = 0; |
| 5229 | wchar_t *wstr; |
| 5230 | PyObject *res = NULL; |
| 5231 | const char *errors = NULL; |
| 5232 | |
| 5233 | if (!PyArg_ParseTuple(args, "U|is", &unicode, ¤t_locale, &errors)) { |
| 5234 | return NULL; |
| 5235 | } |
| 5236 | wstr = PyUnicode_AsWideCharString(unicode, NULL); |
| 5237 | if (wstr == NULL) { |
| 5238 | return NULL; |
| 5239 | } |
| 5240 | _Py_error_handler error_handler = _Py_GetErrorHandler(errors); |
| 5241 | |
| 5242 | char *str = NULL; |
| 5243 | size_t error_pos; |
| 5244 | const char *reason = NULL; |
| 5245 | int ret = _Py_EncodeLocaleEx(wstr, |
| 5246 | &str, &error_pos, &reason, |
| 5247 | current_locale, error_handler); |
| 5248 | PyMem_Free(wstr); |
| 5249 | |
| 5250 | switch(ret) { |
| 5251 | case 0: |
| 5252 | res = PyBytes_FromString(str); |
| 5253 | PyMem_RawFree(str); |
| 5254 | break; |
| 5255 | case -1: |
| 5256 | PyErr_NoMemory(); |
| 5257 | break; |
| 5258 | case -2: |
| 5259 | PyErr_Format(PyExc_RuntimeError, "encode error: pos=%zu, reason=%s", |
| 5260 | error_pos, reason); |
| 5261 | break; |
| 5262 | case -3: |
| 5263 | PyErr_SetString(PyExc_ValueError, "unsupported error handler"); |
| 5264 | break; |
| 5265 | default: |
| 5266 | PyErr_SetString(PyExc_ValueError, "unknow error code"); |
| 5267 | break; |
| 5268 | } |
| 5269 | return res; |
| 5270 | } |
| 5271 | |
| 5272 | |
| 5273 | static PyObject * |
| 5274 | decode_locale_ex(PyObject *self, PyObject *args) |
| 5275 | { |
| 5276 | char *str; |
| 5277 | int current_locale = 0; |
| 5278 | PyObject *res = NULL; |
| 5279 | const char *errors = NULL; |
| 5280 | |
| 5281 | if (!PyArg_ParseTuple(args, "y|is", &str, ¤t_locale, &errors)) { |
| 5282 | return NULL; |
| 5283 | } |
| 5284 | _Py_error_handler error_handler = _Py_GetErrorHandler(errors); |
| 5285 | |
| 5286 | wchar_t *wstr = NULL; |
| 5287 | size_t wlen = 0; |
| 5288 | const char *reason = NULL; |
| 5289 | int ret = _Py_DecodeLocaleEx(str, |
| 5290 | &wstr, &wlen, &reason, |
| 5291 | current_locale, error_handler); |
| 5292 | |
| 5293 | switch(ret) { |
| 5294 | case 0: |
| 5295 | res = PyUnicode_FromWideChar(wstr, wlen); |
| 5296 | PyMem_RawFree(wstr); |
| 5297 | break; |
| 5298 | case -1: |
| 5299 | PyErr_NoMemory(); |
| 5300 | break; |
| 5301 | case -2: |
| 5302 | PyErr_Format(PyExc_RuntimeError, "decode error: pos=%zu, reason=%s", |
| 5303 | wlen, reason); |
| 5304 | break; |
| 5305 | case -3: |
| 5306 | PyErr_SetString(PyExc_ValueError, "unsupported error handler"); |
| 5307 | break; |
| 5308 | default: |
| 5309 | PyErr_SetString(PyExc_ValueError, "unknow error code"); |
| 5310 | break; |
| 5311 | } |
| 5312 | return res; |
| 5313 | } |
| 5314 | |
| 5315 | |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5316 | #ifdef Py_REF_DEBUG |
| 5317 | static PyObject * |
| 5318 | negative_refcount(PyObject *self, PyObject *Py_UNUSED(args)) |
| 5319 | { |
| 5320 | PyObject *obj = PyUnicode_FromString("negative_refcount"); |
| 5321 | if (obj == NULL) { |
| 5322 | return NULL; |
| 5323 | } |
| 5324 | assert(Py_REFCNT(obj) == 1); |
| 5325 | |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 5326 | Py_SET_REFCNT(obj, 0); |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5327 | /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */ |
| 5328 | Py_DECREF(obj); |
| 5329 | |
| 5330 | Py_RETURN_NONE; |
| 5331 | } |
| 5332 | #endif |
| 5333 | |
| 5334 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5335 | static PyObject* |
| 5336 | test_write_unraisable_exc(PyObject *self, PyObject *args) |
| 5337 | { |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5338 | PyObject *exc, *err_msg, *obj; |
| 5339 | if (!PyArg_ParseTuple(args, "OOO", &exc, &err_msg, &obj)) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5340 | return NULL; |
| 5341 | } |
| 5342 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5343 | const char *err_msg_utf8; |
| 5344 | if (err_msg != Py_None) { |
| 5345 | err_msg_utf8 = PyUnicode_AsUTF8(err_msg); |
| 5346 | if (err_msg_utf8 == NULL) { |
| 5347 | return NULL; |
| 5348 | } |
| 5349 | } |
| 5350 | else { |
| 5351 | err_msg_utf8 = NULL; |
| 5352 | } |
| 5353 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5354 | PyErr_SetObject((PyObject *)Py_TYPE(exc), exc); |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5355 | _PyErr_WriteUnraisableMsg(err_msg_utf8, obj); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5356 | Py_RETURN_NONE; |
| 5357 | } |
| 5358 | |
| 5359 | |
Sergey Fedoseev | 92709a2 | 2019-09-09 21:28:34 +0500 | [diff] [blame] | 5360 | static PyObject * |
| 5361 | sequence_getitem(PyObject *self, PyObject *args) |
| 5362 | { |
| 5363 | PyObject *seq; |
| 5364 | Py_ssize_t i; |
| 5365 | if (!PyArg_ParseTuple(args, "On", &seq, &i)) { |
| 5366 | return NULL; |
| 5367 | } |
| 5368 | return PySequence_GetItem(seq, i); |
| 5369 | } |
| 5370 | |
| 5371 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5372 | /* Functions for testing C calling conventions (METH_*) are named meth_*, |
| 5373 | * e.g. "meth_varargs" for METH_VARARGS. |
| 5374 | * |
| 5375 | * They all return a tuple of their C-level arguments, with None instead |
| 5376 | * of NULL and Python tuples instead of C arrays. |
| 5377 | */ |
| 5378 | |
| 5379 | |
| 5380 | static PyObject* |
| 5381 | _null_to_none(PyObject* obj) |
| 5382 | { |
| 5383 | if (obj == NULL) { |
| 5384 | Py_RETURN_NONE; |
| 5385 | } |
| 5386 | Py_INCREF(obj); |
| 5387 | return obj; |
| 5388 | } |
| 5389 | |
| 5390 | static PyObject* |
| 5391 | meth_varargs(PyObject* self, PyObject* args) |
| 5392 | { |
| 5393 | return Py_BuildValue("NO", _null_to_none(self), args); |
| 5394 | } |
| 5395 | |
| 5396 | static PyObject* |
| 5397 | meth_varargs_keywords(PyObject* self, PyObject* args, PyObject* kwargs) |
| 5398 | { |
| 5399 | return Py_BuildValue("NON", _null_to_none(self), args, _null_to_none(kwargs)); |
| 5400 | } |
| 5401 | |
| 5402 | static PyObject* |
| 5403 | meth_o(PyObject* self, PyObject* obj) |
| 5404 | { |
| 5405 | return Py_BuildValue("NO", _null_to_none(self), obj); |
| 5406 | } |
| 5407 | |
| 5408 | static PyObject* |
| 5409 | meth_noargs(PyObject* self, PyObject* ignored) |
| 5410 | { |
| 5411 | return _null_to_none(self); |
| 5412 | } |
| 5413 | |
| 5414 | static PyObject* |
| 5415 | _fastcall_to_tuple(PyObject* const* args, Py_ssize_t nargs) |
| 5416 | { |
| 5417 | PyObject *tuple = PyTuple_New(nargs); |
| 5418 | if (tuple == NULL) { |
| 5419 | return NULL; |
| 5420 | } |
| 5421 | for (Py_ssize_t i=0; i < nargs; i++) { |
| 5422 | Py_INCREF(args[i]); |
| 5423 | PyTuple_SET_ITEM(tuple, i, args[i]); |
| 5424 | } |
| 5425 | return tuple; |
| 5426 | } |
| 5427 | |
| 5428 | static PyObject* |
| 5429 | meth_fastcall(PyObject* self, PyObject* const* args, Py_ssize_t nargs) |
| 5430 | { |
| 5431 | return Py_BuildValue( |
| 5432 | "NN", _null_to_none(self), _fastcall_to_tuple(args, nargs) |
| 5433 | ); |
| 5434 | } |
| 5435 | |
| 5436 | static PyObject* |
| 5437 | meth_fastcall_keywords(PyObject* self, PyObject* const* args, |
| 5438 | Py_ssize_t nargs, PyObject* kwargs) |
| 5439 | { |
| 5440 | PyObject *pyargs = _fastcall_to_tuple(args, nargs); |
| 5441 | if (pyargs == NULL) { |
| 5442 | return NULL; |
| 5443 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 5444 | PyObject *pykwargs = PyObject_Vectorcall((PyObject*)&PyDict_Type, |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5445 | args + nargs, 0, kwargs); |
| 5446 | return Py_BuildValue("NNN", _null_to_none(self), pyargs, pykwargs); |
| 5447 | } |
| 5448 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 5449 | |
Serhiy Storchaka | e5ccc94 | 2020-03-09 20:03:38 +0200 | [diff] [blame] | 5450 | static PyObject* |
| 5451 | pynumber_tobase(PyObject *module, PyObject *args) |
| 5452 | { |
| 5453 | PyObject *obj; |
| 5454 | int base; |
| 5455 | if (!PyArg_ParseTuple(args, "Oi:pynumber_tobase", |
| 5456 | &obj, &base)) { |
| 5457 | return NULL; |
| 5458 | } |
| 5459 | return PyNumber_ToBase(obj, base); |
| 5460 | } |
| 5461 | |
| 5462 | |
Victor Stinner | 0e2ac21 | 2020-11-18 18:48:06 +0100 | [diff] [blame] | 5463 | static PyObject* |
Victor Stinner | 8b6c4a9 | 2020-12-03 14:01:10 +0100 | [diff] [blame] | 5464 | test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0e2ac21 | 2020-11-18 18:48:06 +0100 | [diff] [blame] | 5465 | { |
| 5466 | PyObject *obj = PyList_New(0); |
| 5467 | if (obj == NULL) { |
| 5468 | return NULL; |
| 5469 | } |
| 5470 | |
| 5471 | // Ensure that following tests don't modify the object, |
| 5472 | // to ensure that Py_DECREF() will not crash. |
| 5473 | assert(Py_TYPE(obj) == &PyList_Type); |
| 5474 | assert(Py_SIZE(obj) == 0); |
| 5475 | |
| 5476 | // bpo-39573: Check that Py_TYPE() and Py_SIZE() can be used |
| 5477 | // as l-values to set an object type and size. |
| 5478 | Py_TYPE(obj) = &PyList_Type; |
| 5479 | Py_SIZE(obj) = 0; |
| 5480 | |
| 5481 | Py_DECREF(obj); |
| 5482 | Py_RETURN_NONE; |
| 5483 | } |
| 5484 | |
| 5485 | |
Victor Stinner | 09bbebe | 2021-04-11 00:17:39 +0200 | [diff] [blame] | 5486 | #define TEST_REFCOUNT() \ |
| 5487 | do { \ |
| 5488 | PyObject *obj = PyList_New(0); \ |
| 5489 | if (obj == NULL) { \ |
| 5490 | return NULL; \ |
| 5491 | } \ |
| 5492 | assert(Py_REFCNT(obj) == 1); \ |
| 5493 | \ |
| 5494 | /* test Py_NewRef() */ \ |
| 5495 | PyObject *ref = Py_NewRef(obj); \ |
| 5496 | assert(ref == obj); \ |
| 5497 | assert(Py_REFCNT(obj) == 2); \ |
| 5498 | Py_DECREF(ref); \ |
| 5499 | \ |
| 5500 | /* test Py_XNewRef() */ \ |
| 5501 | PyObject *xref = Py_XNewRef(obj); \ |
| 5502 | assert(xref == obj); \ |
| 5503 | assert(Py_REFCNT(obj) == 2); \ |
| 5504 | Py_DECREF(xref); \ |
| 5505 | \ |
| 5506 | assert(Py_XNewRef(NULL) == NULL); \ |
| 5507 | \ |
| 5508 | Py_DECREF(obj); \ |
| 5509 | Py_RETURN_NONE; \ |
| 5510 | } while (0) \ |
| 5511 | |
| 5512 | |
| 5513 | // Test Py_NewRef() and Py_XNewRef() macros |
Victor Stinner | 8b6c4a9 | 2020-12-03 14:01:10 +0100 | [diff] [blame] | 5514 | static PyObject* |
Victor Stinner | 09bbebe | 2021-04-11 00:17:39 +0200 | [diff] [blame] | 5515 | test_refcount_macros(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 8b6c4a9 | 2020-12-03 14:01:10 +0100 | [diff] [blame] | 5516 | { |
Victor Stinner | 09bbebe | 2021-04-11 00:17:39 +0200 | [diff] [blame] | 5517 | TEST_REFCOUNT(); |
| 5518 | } |
Victor Stinner | 8b6c4a9 | 2020-12-03 14:01:10 +0100 | [diff] [blame] | 5519 | |
Victor Stinner | 09bbebe | 2021-04-11 00:17:39 +0200 | [diff] [blame] | 5520 | #undef Py_NewRef |
| 5521 | #undef Py_XNewRef |
Victor Stinner | 8b6c4a9 | 2020-12-03 14:01:10 +0100 | [diff] [blame] | 5522 | |
Victor Stinner | 09bbebe | 2021-04-11 00:17:39 +0200 | [diff] [blame] | 5523 | // Test Py_NewRef() and Py_XNewRef() functions, after undefining macros. |
| 5524 | static PyObject* |
| 5525 | test_refcount_funcs(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 5526 | { |
| 5527 | TEST_REFCOUNT(); |
| 5528 | } |
Victor Stinner | 8b6c4a9 | 2020-12-03 14:01:10 +0100 | [diff] [blame] | 5529 | |
Victor Stinner | 8b6c4a9 | 2020-12-03 14:01:10 +0100 | [diff] [blame] | 5530 | |
Victor Stinner | 09bbebe | 2021-04-11 00:17:39 +0200 | [diff] [blame] | 5531 | // Test Py_Is() function |
| 5532 | #define TEST_PY_IS() \ |
| 5533 | do { \ |
| 5534 | PyObject *o_none = Py_None; \ |
| 5535 | PyObject *o_true = Py_True; \ |
| 5536 | PyObject *o_false = Py_False; \ |
| 5537 | PyObject *obj = PyList_New(0); \ |
| 5538 | if (obj == NULL) { \ |
| 5539 | return NULL; \ |
| 5540 | } \ |
| 5541 | \ |
| 5542 | /* test Py_Is() */ \ |
| 5543 | assert(Py_Is(obj, obj)); \ |
| 5544 | assert(!Py_Is(obj, o_none)); \ |
| 5545 | \ |
| 5546 | /* test Py_None */ \ |
| 5547 | assert(Py_Is(o_none, o_none)); \ |
| 5548 | assert(!Py_Is(obj, o_none)); \ |
| 5549 | \ |
| 5550 | /* test Py_True */ \ |
| 5551 | assert(Py_Is(o_true, o_true)); \ |
| 5552 | assert(!Py_Is(o_false, o_true)); \ |
| 5553 | assert(!Py_Is(obj, o_true)); \ |
| 5554 | \ |
| 5555 | /* test Py_False */ \ |
| 5556 | assert(Py_Is(o_false, o_false)); \ |
| 5557 | assert(!Py_Is(o_true, o_false)); \ |
| 5558 | assert(!Py_Is(obj, o_false)); \ |
| 5559 | \ |
| 5560 | Py_DECREF(obj); \ |
| 5561 | Py_RETURN_NONE; \ |
| 5562 | } while (0) |
| 5563 | |
| 5564 | // Test Py_Is() macro |
| 5565 | static PyObject* |
| 5566 | test_py_is_macros(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 5567 | { |
| 5568 | TEST_PY_IS(); |
| 5569 | } |
| 5570 | |
| 5571 | #undef Py_Is |
| 5572 | |
| 5573 | // Test Py_Is() function, after undefining its macro. |
| 5574 | static PyObject* |
| 5575 | test_py_is_funcs(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 5576 | { |
| 5577 | TEST_PY_IS(); |
Victor Stinner | 8b6c4a9 | 2020-12-03 14:01:10 +0100 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | |
Victor Stinner | e232025 | 2021-01-18 18:24:29 +0100 | [diff] [blame] | 5581 | static PyObject * |
| 5582 | test_fatal_error(PyObject *self, PyObject *args) |
| 5583 | { |
| 5584 | char *message; |
| 5585 | int release_gil = 0; |
| 5586 | if (!PyArg_ParseTuple(args, "y|i:fatal_error", &message, &release_gil)) |
| 5587 | return NULL; |
| 5588 | if (release_gil) { |
| 5589 | Py_BEGIN_ALLOW_THREADS |
| 5590 | Py_FatalError(message); |
| 5591 | Py_END_ALLOW_THREADS |
| 5592 | } |
| 5593 | else { |
| 5594 | Py_FatalError(message); |
| 5595 | } |
| 5596 | // Py_FatalError() does not return, but exits the process. |
| 5597 | Py_RETURN_NONE; |
| 5598 | } |
| 5599 | |
| 5600 | |
Miss Islington (bot) | 569ca81 | 2021-05-06 20:18:42 -0700 | [diff] [blame] | 5601 | static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *); |
| 5602 | static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*); |
Victor Stinner | e232025 | 2021-01-18 18:24:29 +0100 | [diff] [blame] | 5603 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 5604 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5605 | {"raise_exception", raise_exception, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5606 | {"raise_memoryerror", raise_memoryerror, METH_NOARGS}, |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 5607 | {"set_errno", set_errno, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5608 | {"test_config", test_config, METH_NOARGS}, |
| 5609 | {"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS}, |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 5610 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 5611 | {"datetime_check_date", datetime_check_date, METH_VARARGS}, |
| 5612 | {"datetime_check_time", datetime_check_time, METH_VARARGS}, |
| 5613 | {"datetime_check_datetime", datetime_check_datetime, METH_VARARGS}, |
| 5614 | {"datetime_check_delta", datetime_check_delta, METH_VARARGS}, |
| 5615 | {"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS}, |
| 5616 | {"make_timezones_capi", make_timezones_capi, METH_NOARGS}, |
Paul Ganssle | a049f57 | 2018-02-22 15:15:32 -0500 | [diff] [blame] | 5617 | {"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS}, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 5618 | {"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS}, |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 5619 | {"get_date_fromdate", get_date_fromdate, METH_VARARGS}, |
| 5620 | {"get_datetime_fromdateandtime", get_datetime_fromdateandtime, METH_VARARGS}, |
| 5621 | {"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS}, |
| 5622 | {"get_time_fromtime", get_time_fromtime, METH_VARARGS}, |
| 5623 | {"get_time_fromtimeandfold", get_time_fromtimeandfold, METH_VARARGS}, |
| 5624 | {"get_delta_fromdsu", get_delta_fromdsu, METH_VARARGS}, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 5625 | {"get_date_fromtimestamp", get_date_fromtimestamp, METH_VARARGS}, |
| 5626 | {"get_datetime_fromtimestamp", get_datetime_fromtimestamp, METH_VARARGS}, |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 5627 | {"PyDateTime_GET", test_PyDateTime_GET, METH_O}, |
| 5628 | {"PyDateTime_DATE_GET", test_PyDateTime_DATE_GET, METH_O}, |
| 5629 | {"PyDateTime_TIME_GET", test_PyDateTime_TIME_GET, METH_O}, |
| 5630 | {"PyDateTime_DELTA_GET", test_PyDateTime_DELTA_GET, METH_O}, |
scoder | 3cc481b | 2021-04-28 18:12:16 +0200 | [diff] [blame] | 5631 | {"test_gc_control", test_gc_control, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5632 | {"test_list_api", test_list_api, METH_NOARGS}, |
| 5633 | {"test_dict_iteration", test_dict_iteration, METH_NOARGS}, |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 5634 | {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 5635 | {"dict_hassplittable", dict_hassplittable, METH_O}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5636 | {"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS}, |
| 5637 | {"test_long_api", test_long_api, METH_NOARGS}, |
| 5638 | {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS}, |
| 5639 | {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS}, |
| 5640 | {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS}, |
| 5641 | {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS}, |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 5642 | {"test_structseq_newtype_doesnt_leak", |
| 5643 | test_structseq_newtype_doesnt_leak, METH_NOARGS}, |
Miss Islington (bot) | 912ef3f | 2021-05-04 05:29:56 -0700 | [diff] [blame] | 5644 | {"test_structseq_newtype_null_descr_doc", |
| 5645 | test_structseq_newtype_null_descr_doc, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5646 | {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS}, |
| 5647 | {"test_long_and_overflow", test_long_and_overflow, METH_NOARGS}, |
| 5648 | {"test_long_as_double", test_long_as_double, METH_NOARGS}, |
| 5649 | {"test_long_as_size_t", test_long_as_size_t, METH_NOARGS}, |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 5650 | {"test_long_as_unsigned_long_long_mask", |
| 5651 | test_long_as_unsigned_long_long_mask, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5652 | {"test_long_numbits", test_long_numbits, METH_NOARGS}, |
| 5653 | {"test_k_code", test_k_code, METH_NOARGS}, |
| 5654 | {"test_empty_argparse", test_empty_argparse, METH_NOARGS}, |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 5655 | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5656 | {"test_null_strings", test_null_strings, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5657 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5658 | {"test_with_docstring", test_with_docstring, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5659 | PyDoc_STR("This is a pretty normal docstring.")}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5660 | {"test_string_to_double", test_string_to_double, METH_NOARGS}, |
| 5661 | {"test_unicode_compare_with_ascii", test_unicode_compare_with_ascii, |
| 5662 | METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5663 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 5664 | {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 5665 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 5666 | {"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] | 5667 | #endif |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 5668 | {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, |
Joannah Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 5669 | {"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS}, |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 5670 | {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 5671 | {"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS}, |
Hai Shi | a13b26c | 2020-11-11 04:53:46 +0800 | [diff] [blame] | 5672 | {"get_args", get_args, METH_VARARGS}, |
| 5673 | {"test_get_statictype_slots", test_get_statictype_slots, METH_NOARGS}, |
| 5674 | {"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, |
| 5675 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5676 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5677 | {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5679 | {"getargs_keyword_only", (PyCFunction)(void(*)(void))getargs_keyword_only, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 5680 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 5681 | {"getargs_positional_only_and_keywords", |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5682 | (PyCFunction)(void(*)(void))getargs_positional_only_and_keywords, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 5683 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5684 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 5685 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 5686 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 5687 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 5688 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 5689 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 5690 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 5691 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 5692 | {"getargs_n", getargs_n, METH_VARARGS}, |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 5693 | {"getargs_p", getargs_p, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 5695 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 5696 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5697 | {"test_long_long_and_overflow",test_long_long_and_overflow, METH_NOARGS}, |
| 5698 | {"test_L_code", test_L_code, METH_NOARGS}, |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 5699 | {"getargs_f", getargs_f, METH_VARARGS}, |
| 5700 | {"getargs_d", getargs_d, METH_VARARGS}, |
| 5701 | {"getargs_D", getargs_D, METH_VARARGS}, |
| 5702 | {"getargs_S", getargs_S, METH_VARARGS}, |
| 5703 | {"getargs_Y", getargs_Y, METH_VARARGS}, |
| 5704 | {"getargs_U", getargs_U, METH_VARARGS}, |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 5705 | {"getargs_c", getargs_c, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 5706 | {"getargs_C", getargs_C, METH_VARARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 5707 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 5708 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 5709 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
Miss Islington (bot) | 569ca81 | 2021-05-06 20:18:42 -0700 | [diff] [blame] | 5710 | {"getargs_s_hash_int", (PyCFunction)(void(*)(void))getargs_s_hash_int, |
| 5711 | METH_VARARGS|METH_KEYWORDS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 5712 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 5713 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 5714 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 5715 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 5716 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 5717 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 5718 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 5719 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 5720 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 5721 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 5722 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 5723 | {"getargs_es", getargs_es, METH_VARARGS}, |
| 5724 | {"getargs_et", getargs_et, METH_VARARGS}, |
| 5725 | {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 5726 | {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5727 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5728 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5729 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5730 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5731 | {"test_s_code", test_s_code, METH_NOARGS}, |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 5732 | #if USE_UNICODE_WCHAR_CACHE |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5733 | {"test_u_code", test_u_code, METH_NOARGS}, |
| 5734 | {"test_Z_code", test_Z_code, METH_NOARGS}, |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 5735 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5736 | {"test_widechar", test_widechar, METH_NOARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 5737 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 5738 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 5739 | {"unicode_asucs4", unicode_asucs4, METH_VARARGS}, |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 5740 | {"unicode_asutf8", unicode_asutf8, METH_VARARGS}, |
| 5741 | {"unicode_asutf8andsize", unicode_asutf8andsize, METH_VARARGS}, |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 5742 | {"unicode_findchar", unicode_findchar, METH_VARARGS}, |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 5743 | {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS}, |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 5744 | #if USE_UNICODE_WCHAR_CACHE |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 5745 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 5746 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 5747 | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
Serhiy Storchaka | 4c8f09d | 2020-07-10 23:26:06 +0300 | [diff] [blame] | 5748 | #endif /* USE_UNICODE_WCHAR_CACHE */ |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5749 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5750 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5751 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5752 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5753 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5754 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 5755 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 5756 | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5757 | {"argparsing", argparsing, METH_VARARGS}, |
| 5758 | {"code_newempty", code_newempty, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5759 | {"make_exception_with_doc", (PyCFunction)(void(*)(void))make_exception_with_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5760 | METH_VARARGS | METH_KEYWORDS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5761 | {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer, |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 5762 | METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5763 | {"crash_no_current_thread", crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 5764 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5765 | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
| 5766 | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 5767 | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 5768 | {"with_tp_del", with_tp_del, METH_VARARGS}, |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 5769 | {"create_cfunction", create_cfunction, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5770 | {"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS}, |
| 5771 | {"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS}, |
| 5772 | {"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS}, |
| 5773 | {"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS}, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 5774 | {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS, |
| 5775 | PyDoc_STR("set_nomemory(start:int, stop:int = 0)")}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5776 | {"remove_mem_hooks", remove_mem_hooks, METH_NOARGS, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 5777 | PyDoc_STR("Remove memory hooks.")}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5778 | {"no_docstring", |
| 5779 | (PyCFunction)test_with_docstring, METH_NOARGS}, |
| 5780 | {"docstring_empty", |
| 5781 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5782 | docstring_empty}, |
| 5783 | {"docstring_no_signature", |
| 5784 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5785 | docstring_no_signature}, |
| 5786 | {"docstring_with_invalid_signature", |
| 5787 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5788 | docstring_with_invalid_signature}, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 5789 | {"docstring_with_invalid_signature2", |
| 5790 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5791 | docstring_with_invalid_signature2}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5792 | {"docstring_with_signature", |
| 5793 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5794 | docstring_with_signature}, |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 5795 | {"docstring_with_signature_but_no_doc", |
| 5796 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5797 | docstring_with_signature_but_no_doc}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5798 | {"docstring_with_signature_and_extra_newlines", |
| 5799 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5800 | docstring_with_signature_and_extra_newlines}, |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 5801 | {"docstring_with_signature_with_defaults", |
| 5802 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5803 | docstring_with_signature_with_defaults}, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5804 | {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
| 5805 | PyDoc_STR("set_error_class(error_class) -> None")}, |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 5806 | {"pymarshal_write_long_to_file", |
| 5807 | pymarshal_write_long_to_file, METH_VARARGS}, |
| 5808 | {"pymarshal_write_object_to_file", |
| 5809 | pymarshal_write_object_to_file, METH_VARARGS}, |
| 5810 | {"pymarshal_read_short_from_file", |
| 5811 | pymarshal_read_short_from_file, METH_VARARGS}, |
| 5812 | {"pymarshal_read_long_from_file", |
| 5813 | pymarshal_read_long_from_file, METH_VARARGS}, |
| 5814 | {"pymarshal_read_last_object_from_file", |
| 5815 | pymarshal_read_last_object_from_file, METH_VARARGS}, |
| 5816 | {"pymarshal_read_object_from_file", |
| 5817 | pymarshal_read_object_from_file, METH_VARARGS}, |
Victor Stinner | c9b8e9c | 2021-01-27 17:39:16 +0100 | [diff] [blame] | 5818 | {"return_null_without_error", return_null_without_error, METH_NOARGS}, |
| 5819 | {"return_result_with_error", return_result_with_error, METH_NOARGS}, |
| 5820 | {"getitem_with_error", getitem_with_error, METH_VARARGS}, |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 5821 | {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS}, |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 5822 | {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS}, |
| 5823 | {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 5824 | {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 5825 | #ifdef HAVE_CLOCK_GETTIME |
| 5826 | {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, |
| 5827 | #endif |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 5828 | {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS}, |
| 5829 | {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS}, |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 5830 | {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS}, |
| 5831 | {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 5832 | {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 5833 | {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS}, |
Victor Stinner | 6876257 | 2019-10-07 18:42:01 +0200 | [diff] [blame] | 5834 | {"check_pyobject_null_is_freed", check_pyobject_null_is_freed, METH_NOARGS}, |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 5835 | {"check_pyobject_uninitialized_is_freed", check_pyobject_uninitialized_is_freed, METH_NOARGS}, |
| 5836 | {"check_pyobject_forbidden_bytes_is_freed", check_pyobject_forbidden_bytes_is_freed, METH_NOARGS}, |
| 5837 | {"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS}, |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 5838 | {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 5839 | {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, |
| 5840 | {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, |
| 5841 | {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 5842 | {"dict_get_version", dict_get_version, METH_VARARGS}, |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 5843 | {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS}, |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 5844 | {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS}, |
| 5845 | {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS}, |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5846 | {"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS}, |
| 5847 | {"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS}, |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 5848 | {"stack_pointer", stack_pointer, METH_NOARGS}, |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 5849 | #ifdef W_STOPCODE |
| 5850 | {"W_STOPCODE", py_w_stopcode, METH_VARARGS}, |
| 5851 | #endif |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 5852 | {"get_mapping_keys", get_mapping_keys, METH_O}, |
| 5853 | {"get_mapping_values", get_mapping_values, METH_O}, |
| 5854 | {"get_mapping_items", get_mapping_items, METH_O}, |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 5855 | {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS}, |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 5856 | {"hamt", new_hamt, METH_NOARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5857 | {"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL}, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 5858 | {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS}, |
| 5859 | {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS}, |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5860 | #ifdef Py_REF_DEBUG |
| 5861 | {"negative_refcount", negative_refcount, METH_NOARGS}, |
| 5862 | #endif |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5863 | {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS}, |
Sergey Fedoseev | 92709a2 | 2019-09-09 21:28:34 +0500 | [diff] [blame] | 5864 | {"sequence_getitem", sequence_getitem, METH_VARARGS}, |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5865 | {"meth_varargs", meth_varargs, METH_VARARGS}, |
| 5866 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, |
| 5867 | {"meth_o", meth_o, METH_O}, |
| 5868 | {"meth_noargs", meth_noargs, METH_NOARGS}, |
| 5869 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, |
| 5870 | {"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] | 5871 | {"pynumber_tobase", pynumber_tobase, METH_VARARGS}, |
Brandt Bucher | c13b847 | 2020-10-14 18:44:07 -0700 | [diff] [blame] | 5872 | {"without_gc", without_gc, METH_O}, |
Victor Stinner | 0e2ac21 | 2020-11-18 18:48:06 +0100 | [diff] [blame] | 5873 | {"test_set_type_size", test_set_type_size, METH_NOARGS}, |
Victor Stinner | 09bbebe | 2021-04-11 00:17:39 +0200 | [diff] [blame] | 5874 | {"test_refcount_macros", test_refcount_macros, METH_NOARGS}, |
| 5875 | {"test_refcount_funcs", test_refcount_funcs, METH_NOARGS}, |
| 5876 | {"test_py_is_macros", test_py_is_macros, METH_NOARGS}, |
| 5877 | {"test_py_is_funcs", test_py_is_funcs, METH_NOARGS}, |
Victor Stinner | e232025 | 2021-01-18 18:24:29 +0100 | [diff] [blame] | 5878 | {"fatal_error", test_fatal_error, METH_VARARGS, |
| 5879 | PyDoc_STR("fatal_error(message, release_gil=False): call Py_FatalError(message)")}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5880 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 5881 | }; |
| 5882 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 5883 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 5884 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5885 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5886 | char bool_member; |
| 5887 | char byte_member; |
| 5888 | unsigned char ubyte_member; |
| 5889 | short short_member; |
| 5890 | unsigned short ushort_member; |
| 5891 | int int_member; |
| 5892 | unsigned int uint_member; |
| 5893 | long long_member; |
| 5894 | unsigned long ulong_member; |
| 5895 | Py_ssize_t pyssizet_member; |
| 5896 | float float_member; |
| 5897 | double double_member; |
| 5898 | char inplace_member[6]; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5899 | long long longlong_member; |
| 5900 | unsigned long long ulonglong_member; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5901 | } all_structmembers; |
| 5902 | |
| 5903 | typedef struct { |
| 5904 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5905 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5906 | } test_structmembers; |
| 5907 | |
| 5908 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5909 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 5910 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 5911 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 5912 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 5913 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 5914 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 5915 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 5916 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 5917 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 5918 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 5919 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 5920 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 5921 | {"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] | 5922 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 5923 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5924 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5925 | }; |
| 5926 | |
| 5927 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 5928 | static PyObject * |
| 5929 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5930 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5931 | static char *keywords[] = { |
| 5932 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 5933 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 5934 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5935 | "T_LONGLONG", "T_ULONGLONG", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | NULL}; |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 5937 | static const char fmt[] = "|bbBhHiIlknfds#LK"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5938 | test_structmembers *ob; |
| 5939 | const char *s = NULL; |
| 5940 | Py_ssize_t string_len = 0; |
| 5941 | ob = PyObject_New(test_structmembers, type); |
| 5942 | if (ob == NULL) |
| 5943 | return NULL; |
| 5944 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 5945 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 5946 | &ob->structmembers.bool_member, |
| 5947 | &ob->structmembers.byte_member, |
| 5948 | &ob->structmembers.ubyte_member, |
| 5949 | &ob->structmembers.short_member, |
| 5950 | &ob->structmembers.ushort_member, |
| 5951 | &ob->structmembers.int_member, |
| 5952 | &ob->structmembers.uint_member, |
| 5953 | &ob->structmembers.long_member, |
| 5954 | &ob->structmembers.ulong_member, |
| 5955 | &ob->structmembers.pyssizet_member, |
| 5956 | &ob->structmembers.float_member, |
| 5957 | &ob->structmembers.double_member, |
| 5958 | &s, &string_len |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5959 | , &ob->structmembers.longlong_member, |
| 5960 | &ob->structmembers.ulonglong_member |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5961 | )) { |
| 5962 | Py_DECREF(ob); |
| 5963 | return NULL; |
| 5964 | } |
| 5965 | if (s != NULL) { |
| 5966 | if (string_len > 5) { |
| 5967 | Py_DECREF(ob); |
| 5968 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 5969 | return NULL; |
| 5970 | } |
| 5971 | strcpy(ob->structmembers.inplace_member, s); |
| 5972 | } |
| 5973 | else { |
| 5974 | strcpy(ob->structmembers.inplace_member, ""); |
| 5975 | } |
| 5976 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5977 | } |
| 5978 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 5979 | static void |
| 5980 | test_structmembers_free(PyObject *ob) |
| 5981 | { |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 5982 | PyObject_Free(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 5986 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5987 | "test_structmembersType", |
| 5988 | sizeof(test_structmembers), /* tp_basicsize */ |
| 5989 | 0, /* tp_itemsize */ |
| 5990 | test_structmembers_free, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5991 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5992 | 0, /* tp_getattr */ |
| 5993 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5994 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5995 | 0, /* tp_repr */ |
| 5996 | 0, /* tp_as_number */ |
| 5997 | 0, /* tp_as_sequence */ |
| 5998 | 0, /* tp_as_mapping */ |
| 5999 | 0, /* tp_hash */ |
| 6000 | 0, /* tp_call */ |
| 6001 | 0, /* tp_str */ |
| 6002 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6003 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6004 | 0, /* tp_as_buffer */ |
| 6005 | 0, /* tp_flags */ |
| 6006 | "Type containing all structmember types", |
| 6007 | 0, /* traverseproc tp_traverse */ |
| 6008 | 0, /* tp_clear */ |
| 6009 | 0, /* tp_richcompare */ |
| 6010 | 0, /* tp_weaklistoffset */ |
| 6011 | 0, /* tp_iter */ |
| 6012 | 0, /* tp_iternext */ |
| 6013 | 0, /* tp_methods */ |
| 6014 | test_members, /* tp_members */ |
| 6015 | 0, |
| 6016 | 0, |
| 6017 | 0, |
| 6018 | 0, |
| 6019 | 0, |
| 6020 | 0, |
| 6021 | 0, |
| 6022 | 0, |
| 6023 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6024 | }; |
| 6025 | |
| 6026 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6027 | typedef struct { |
| 6028 | PyObject_HEAD |
| 6029 | } matmulObject; |
| 6030 | |
| 6031 | static PyObject * |
| 6032 | matmulType_matmul(PyObject *self, PyObject *other) |
| 6033 | { |
| 6034 | return Py_BuildValue("(sOO)", "matmul", self, other); |
| 6035 | } |
| 6036 | |
| 6037 | static PyObject * |
| 6038 | matmulType_imatmul(PyObject *self, PyObject *other) |
| 6039 | { |
| 6040 | return Py_BuildValue("(sOO)", "imatmul", self, other); |
| 6041 | } |
| 6042 | |
| 6043 | static void |
| 6044 | matmulType_dealloc(PyObject *self) |
| 6045 | { |
Zachary Ware | 420dc56 | 2014-04-23 13:51:27 -0500 | [diff] [blame] | 6046 | Py_TYPE(self)->tp_free(self); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | static PyNumberMethods matmulType_as_number = { |
| 6050 | 0, /* nb_add */ |
| 6051 | 0, /* nb_subtract */ |
| 6052 | 0, /* nb_multiply */ |
| 6053 | 0, /* nb_remainde r*/ |
| 6054 | 0, /* nb_divmod */ |
| 6055 | 0, /* nb_power */ |
| 6056 | 0, /* nb_negative */ |
| 6057 | 0, /* tp_positive */ |
| 6058 | 0, /* tp_absolute */ |
| 6059 | 0, /* tp_bool */ |
| 6060 | 0, /* nb_invert */ |
| 6061 | 0, /* nb_lshift */ |
| 6062 | 0, /* nb_rshift */ |
| 6063 | 0, /* nb_and */ |
| 6064 | 0, /* nb_xor */ |
| 6065 | 0, /* nb_or */ |
| 6066 | 0, /* nb_int */ |
| 6067 | 0, /* nb_reserved */ |
| 6068 | 0, /* nb_float */ |
| 6069 | 0, /* nb_inplace_add */ |
| 6070 | 0, /* nb_inplace_subtract */ |
| 6071 | 0, /* nb_inplace_multiply */ |
| 6072 | 0, /* nb_inplace_remainder */ |
| 6073 | 0, /* nb_inplace_power */ |
| 6074 | 0, /* nb_inplace_lshift */ |
| 6075 | 0, /* nb_inplace_rshift */ |
| 6076 | 0, /* nb_inplace_and */ |
| 6077 | 0, /* nb_inplace_xor */ |
| 6078 | 0, /* nb_inplace_or */ |
| 6079 | 0, /* nb_floor_divide */ |
| 6080 | 0, /* nb_true_divide */ |
| 6081 | 0, /* nb_inplace_floor_divide */ |
| 6082 | 0, /* nb_inplace_true_divide */ |
| 6083 | 0, /* nb_index */ |
| 6084 | matmulType_matmul, /* nb_matrix_multiply */ |
| 6085 | matmulType_imatmul /* nb_matrix_inplace_multiply */ |
| 6086 | }; |
| 6087 | |
| 6088 | static PyTypeObject matmulType = { |
| 6089 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6090 | "matmulType", |
| 6091 | sizeof(matmulObject), /* tp_basicsize */ |
| 6092 | 0, /* tp_itemsize */ |
| 6093 | matmulType_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6094 | 0, /* tp_vectorcall_offset */ |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6095 | 0, /* tp_getattr */ |
| 6096 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6097 | 0, /* tp_as_async */ |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6098 | 0, /* tp_repr */ |
| 6099 | &matmulType_as_number, /* tp_as_number */ |
| 6100 | 0, /* tp_as_sequence */ |
| 6101 | 0, /* tp_as_mapping */ |
| 6102 | 0, /* tp_hash */ |
| 6103 | 0, /* tp_call */ |
| 6104 | 0, /* tp_str */ |
| 6105 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6106 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6107 | 0, /* tp_as_buffer */ |
| 6108 | 0, /* tp_flags */ |
| 6109 | "C level type with matrix operations defined", |
| 6110 | 0, /* traverseproc tp_traverse */ |
| 6111 | 0, /* tp_clear */ |
| 6112 | 0, /* tp_richcompare */ |
| 6113 | 0, /* tp_weaklistoffset */ |
| 6114 | 0, /* tp_iter */ |
| 6115 | 0, /* tp_iternext */ |
| 6116 | 0, /* tp_methods */ |
| 6117 | 0, /* tp_members */ |
| 6118 | 0, |
| 6119 | 0, |
| 6120 | 0, |
| 6121 | 0, |
| 6122 | 0, |
| 6123 | 0, |
| 6124 | 0, |
| 6125 | 0, |
| 6126 | PyType_GenericNew, /* tp_new */ |
| 6127 | PyObject_Del, /* tp_free */ |
| 6128 | }; |
| 6129 | |
Zackery Spytz | c7f803b | 2019-05-31 03:46:36 -0600 | [diff] [blame] | 6130 | typedef struct { |
| 6131 | PyObject_HEAD |
| 6132 | } ipowObject; |
| 6133 | |
| 6134 | static PyObject * |
| 6135 | ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod) |
| 6136 | { |
| 6137 | return Py_BuildValue("OO", other, mod); |
| 6138 | } |
| 6139 | |
| 6140 | static PyNumberMethods ipowType_as_number = { |
| 6141 | .nb_inplace_power = ipowType_ipow |
| 6142 | }; |
| 6143 | |
| 6144 | static PyTypeObject ipowType = { |
| 6145 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6146 | .tp_name = "ipowType", |
| 6147 | .tp_basicsize = sizeof(ipowObject), |
| 6148 | .tp_as_number = &ipowType_as_number, |
| 6149 | .tp_new = PyType_GenericNew |
| 6150 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6151 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6152 | typedef struct { |
| 6153 | PyObject_HEAD |
| 6154 | PyObject *ao_iterator; |
| 6155 | } awaitObject; |
| 6156 | |
| 6157 | |
| 6158 | static PyObject * |
| 6159 | awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 6160 | { |
| 6161 | PyObject *v; |
| 6162 | awaitObject *ao; |
| 6163 | |
| 6164 | if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v)) |
| 6165 | return NULL; |
| 6166 | |
| 6167 | ao = (awaitObject *)type->tp_alloc(type, 0); |
| 6168 | if (ao == NULL) { |
| 6169 | return NULL; |
| 6170 | } |
| 6171 | |
| 6172 | Py_INCREF(v); |
| 6173 | ao->ao_iterator = v; |
| 6174 | |
| 6175 | return (PyObject *)ao; |
| 6176 | } |
| 6177 | |
| 6178 | |
| 6179 | static void |
| 6180 | awaitObject_dealloc(awaitObject *ao) |
| 6181 | { |
| 6182 | Py_CLEAR(ao->ao_iterator); |
| 6183 | Py_TYPE(ao)->tp_free(ao); |
| 6184 | } |
| 6185 | |
| 6186 | |
| 6187 | static PyObject * |
| 6188 | awaitObject_await(awaitObject *ao) |
| 6189 | { |
| 6190 | Py_INCREF(ao->ao_iterator); |
| 6191 | return ao->ao_iterator; |
| 6192 | } |
| 6193 | |
| 6194 | static PyAsyncMethods awaitType_as_async = { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 6195 | (unaryfunc)awaitObject_await, /* am_await */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6196 | 0, /* am_aiter */ |
Vladimir Matveev | 1e996c3 | 2020-11-10 12:09:55 -0800 | [diff] [blame] | 6197 | 0, /* am_anext */ |
| 6198 | 0, /* am_send */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6199 | }; |
| 6200 | |
| 6201 | |
| 6202 | static PyTypeObject awaitType = { |
| 6203 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6204 | "awaitType", |
| 6205 | sizeof(awaitObject), /* tp_basicsize */ |
| 6206 | 0, /* tp_itemsize */ |
| 6207 | (destructor)awaitObject_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6208 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6209 | 0, /* tp_getattr */ |
| 6210 | 0, /* tp_setattr */ |
| 6211 | &awaitType_as_async, /* tp_as_async */ |
| 6212 | 0, /* tp_repr */ |
| 6213 | 0, /* tp_as_number */ |
| 6214 | 0, /* tp_as_sequence */ |
| 6215 | 0, /* tp_as_mapping */ |
| 6216 | 0, /* tp_hash */ |
| 6217 | 0, /* tp_call */ |
| 6218 | 0, /* tp_str */ |
| 6219 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6220 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6221 | 0, /* tp_as_buffer */ |
| 6222 | 0, /* tp_flags */ |
| 6223 | "C level type with tp_as_async", |
| 6224 | 0, /* traverseproc tp_traverse */ |
| 6225 | 0, /* tp_clear */ |
| 6226 | 0, /* tp_richcompare */ |
| 6227 | 0, /* tp_weaklistoffset */ |
| 6228 | 0, /* tp_iter */ |
| 6229 | 0, /* tp_iternext */ |
| 6230 | 0, /* tp_methods */ |
| 6231 | 0, /* tp_members */ |
| 6232 | 0, |
| 6233 | 0, |
| 6234 | 0, |
| 6235 | 0, |
| 6236 | 0, |
| 6237 | 0, |
| 6238 | 0, |
| 6239 | 0, |
| 6240 | awaitObject_new, /* tp_new */ |
| 6241 | PyObject_Del, /* tp_free */ |
| 6242 | }; |
| 6243 | |
| 6244 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 6245 | static int recurse_infinitely_error_init(PyObject *, PyObject *, PyObject *); |
| 6246 | |
| 6247 | static PyTypeObject PyRecursingInfinitelyError_Type = { |
| 6248 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6249 | "RecursingInfinitelyError", /* tp_name */ |
| 6250 | sizeof(PyBaseExceptionObject), /* tp_basicsize */ |
| 6251 | 0, /* tp_itemsize */ |
| 6252 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6253 | 0, /* tp_vectorcall_offset */ |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 6254 | 0, /* tp_getattr */ |
| 6255 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6256 | 0, /* tp_as_async */ |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 6257 | 0, /* tp_repr */ |
| 6258 | 0, /* tp_as_number */ |
| 6259 | 0, /* tp_as_sequence */ |
| 6260 | 0, /* tp_as_mapping */ |
| 6261 | 0, /* tp_hash */ |
| 6262 | 0, /* tp_call */ |
| 6263 | 0, /* tp_str */ |
| 6264 | 0, /* tp_getattro */ |
| 6265 | 0, /* tp_setattro */ |
| 6266 | 0, /* tp_as_buffer */ |
| 6267 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6268 | "Instantiating this exception starts infinite recursion.", /* tp_doc */ |
| 6269 | 0, /* tp_traverse */ |
| 6270 | 0, /* tp_clear */ |
| 6271 | 0, /* tp_richcompare */ |
| 6272 | 0, /* tp_weaklistoffset */ |
| 6273 | 0, /* tp_iter */ |
| 6274 | 0, /* tp_iternext */ |
| 6275 | 0, /* tp_methods */ |
| 6276 | 0, /* tp_members */ |
| 6277 | 0, /* tp_getset */ |
| 6278 | 0, /* tp_base */ |
| 6279 | 0, /* tp_dict */ |
| 6280 | 0, /* tp_descr_get */ |
| 6281 | 0, /* tp_descr_set */ |
| 6282 | 0, /* tp_dictoffset */ |
| 6283 | (initproc)recurse_infinitely_error_init, /* tp_init */ |
| 6284 | 0, /* tp_alloc */ |
| 6285 | 0, /* tp_new */ |
| 6286 | }; |
| 6287 | |
| 6288 | static int |
| 6289 | recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6290 | { |
| 6291 | PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type; |
| 6292 | |
| 6293 | /* Instantiating this exception starts infinite recursion. */ |
| 6294 | Py_INCREF(type); |
| 6295 | PyErr_SetObject(type, NULL); |
| 6296 | return -1; |
| 6297 | } |
| 6298 | |
| 6299 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 6300 | /* Test bpo-35983: create a subclass of "list" which checks that instances |
| 6301 | * are not deallocated twice */ |
| 6302 | |
| 6303 | typedef struct { |
| 6304 | PyListObject list; |
| 6305 | int deallocated; |
| 6306 | } MyListObject; |
| 6307 | |
| 6308 | static PyObject * |
| 6309 | MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 6310 | { |
| 6311 | PyObject* op = PyList_Type.tp_new(type, args, kwds); |
| 6312 | ((MyListObject*)op)->deallocated = 0; |
| 6313 | return op; |
| 6314 | } |
| 6315 | |
| 6316 | void |
| 6317 | MyList_dealloc(MyListObject* op) |
| 6318 | { |
| 6319 | if (op->deallocated) { |
| 6320 | /* We cannot raise exceptions here but we still want the testsuite |
| 6321 | * to fail when we hit this */ |
| 6322 | Py_FatalError("MyList instance deallocated twice"); |
| 6323 | } |
| 6324 | op->deallocated = 1; |
| 6325 | PyList_Type.tp_dealloc((PyObject *)op); |
| 6326 | } |
| 6327 | |
| 6328 | static PyTypeObject MyList_Type = { |
| 6329 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6330 | "MyList", |
| 6331 | sizeof(MyListObject), |
| 6332 | 0, |
| 6333 | (destructor)MyList_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6334 | 0, /* tp_vectorcall_offset */ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 6335 | 0, /* tp_getattr */ |
| 6336 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 6337 | 0, /* tp_as_async */ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 6338 | 0, /* tp_repr */ |
| 6339 | 0, /* tp_as_number */ |
| 6340 | 0, /* tp_as_sequence */ |
| 6341 | 0, /* tp_as_mapping */ |
| 6342 | 0, /* tp_hash */ |
| 6343 | 0, /* tp_call */ |
| 6344 | 0, /* tp_str */ |
| 6345 | 0, /* tp_getattro */ |
| 6346 | 0, /* tp_setattro */ |
| 6347 | 0, /* tp_as_buffer */ |
| 6348 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 6349 | 0, /* tp_doc */ |
| 6350 | 0, /* tp_traverse */ |
| 6351 | 0, /* tp_clear */ |
| 6352 | 0, /* tp_richcompare */ |
| 6353 | 0, /* tp_weaklistoffset */ |
| 6354 | 0, /* tp_iter */ |
| 6355 | 0, /* tp_iternext */ |
| 6356 | 0, /* tp_methods */ |
| 6357 | 0, /* tp_members */ |
| 6358 | 0, /* tp_getset */ |
| 6359 | 0, /* &PyList_Type */ /* tp_base */ |
| 6360 | 0, /* tp_dict */ |
| 6361 | 0, /* tp_descr_get */ |
| 6362 | 0, /* tp_descr_set */ |
| 6363 | 0, /* tp_dictoffset */ |
| 6364 | 0, /* tp_init */ |
| 6365 | 0, /* tp_alloc */ |
| 6366 | MyList_new, /* tp_new */ |
| 6367 | }; |
| 6368 | |
| 6369 | |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6370 | /* Test PEP 560 */ |
| 6371 | |
| 6372 | typedef struct { |
| 6373 | PyObject_HEAD |
| 6374 | PyObject *item; |
| 6375 | } PyGenericAliasObject; |
| 6376 | |
| 6377 | static void |
| 6378 | generic_alias_dealloc(PyGenericAliasObject *self) |
| 6379 | { |
| 6380 | Py_CLEAR(self->item); |
Victor Stinner | e860089 | 2018-01-17 23:08:18 +0100 | [diff] [blame] | 6381 | Py_TYPE(self)->tp_free((PyObject *)self); |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6382 | } |
| 6383 | |
| 6384 | static PyObject * |
| 6385 | generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases) |
| 6386 | { |
| 6387 | return PyTuple_Pack(1, self->item); |
| 6388 | } |
| 6389 | |
| 6390 | static PyMethodDef generic_alias_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6391 | {"__mro_entries__", (PyCFunction)(void(*)(void))generic_alias_mro_entries, METH_O, NULL}, |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6392 | {NULL} /* sentinel */ |
| 6393 | }; |
| 6394 | |
Benjamin Peterson | 97ae32c | 2018-07-03 22:39:09 -0700 | [diff] [blame] | 6395 | static PyTypeObject GenericAlias_Type = { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6396 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6397 | "GenericAlias", |
| 6398 | sizeof(PyGenericAliasObject), |
| 6399 | 0, |
| 6400 | .tp_dealloc = (destructor)generic_alias_dealloc, |
| 6401 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6402 | .tp_methods = generic_alias_methods, |
| 6403 | }; |
| 6404 | |
| 6405 | static PyObject * |
| 6406 | generic_alias_new(PyObject *item) |
| 6407 | { |
| 6408 | PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type); |
| 6409 | if (o == NULL) { |
| 6410 | return NULL; |
| 6411 | } |
| 6412 | Py_INCREF(item); |
| 6413 | o->item = item; |
| 6414 | return (PyObject*) o; |
| 6415 | } |
| 6416 | |
| 6417 | typedef struct { |
| 6418 | PyObject_HEAD |
| 6419 | } PyGenericObject; |
| 6420 | |
| 6421 | static PyObject * |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 6422 | generic_class_getitem(PyObject *type, PyObject *item) |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6423 | { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6424 | return generic_alias_new(item); |
| 6425 | } |
| 6426 | |
| 6427 | static PyMethodDef generic_methods[] = { |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 6428 | {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL}, |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6429 | {NULL} /* sentinel */ |
| 6430 | }; |
| 6431 | |
Benjamin Peterson | 97ae32c | 2018-07-03 22:39:09 -0700 | [diff] [blame] | 6432 | static PyTypeObject Generic_Type = { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6433 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6434 | "Generic", |
| 6435 | sizeof(PyGenericObject), |
| 6436 | 0, |
| 6437 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6438 | .tp_methods = generic_methods, |
| 6439 | }; |
| 6440 | |
| 6441 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6442 | /* Test PEP 590 */ |
| 6443 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6444 | typedef struct { |
| 6445 | PyObject_HEAD |
| 6446 | vectorcallfunc vectorcall; |
| 6447 | } MethodDescriptorObject; |
| 6448 | |
| 6449 | static PyObject * |
| 6450 | MethodDescriptor_vectorcall(PyObject *callable, PyObject *const *args, |
| 6451 | size_t nargsf, PyObject *kwnames) |
| 6452 | { |
| 6453 | /* True if using the vectorcall function in MethodDescriptorObject |
| 6454 | * but False for MethodDescriptor2Object */ |
| 6455 | MethodDescriptorObject *md = (MethodDescriptorObject *)callable; |
| 6456 | return PyBool_FromLong(md->vectorcall != NULL); |
| 6457 | } |
| 6458 | |
| 6459 | static PyObject * |
| 6460 | MethodDescriptor_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 6461 | { |
Petr Viktorin | e584cbf | 2019-06-03 01:08:14 +0200 | [diff] [blame] | 6462 | MethodDescriptorObject *op = (MethodDescriptorObject *)type->tp_alloc(type, 0); |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6463 | op->vectorcall = MethodDescriptor_vectorcall; |
| 6464 | return (PyObject *)op; |
| 6465 | } |
| 6466 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6467 | static PyObject * |
| 6468 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 6469 | { |
| 6470 | if (obj == Py_None || obj == NULL) { |
| 6471 | Py_INCREF(func); |
| 6472 | return func; |
| 6473 | } |
| 6474 | return PyMethod_New(func, obj); |
| 6475 | } |
| 6476 | |
| 6477 | static PyObject * |
| 6478 | nop_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 6479 | { |
| 6480 | Py_INCREF(func); |
| 6481 | return func; |
| 6482 | } |
| 6483 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6484 | static PyObject * |
| 6485 | call_return_args(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6486 | { |
| 6487 | Py_INCREF(args); |
| 6488 | return args; |
| 6489 | } |
| 6490 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6491 | static PyTypeObject MethodDescriptorBase_Type = { |
| 6492 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6493 | "MethodDescriptorBase", |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6494 | sizeof(MethodDescriptorObject), |
| 6495 | .tp_new = MethodDescriptor_new, |
| 6496 | .tp_call = PyVectorcall_Call, |
| 6497 | .tp_vectorcall_offset = offsetof(MethodDescriptorObject, vectorcall), |
| 6498 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 6499 | Py_TPFLAGS_METHOD_DESCRIPTOR | Py_TPFLAGS_HAVE_VECTORCALL, |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6500 | .tp_descr_get = func_descr_get, |
| 6501 | }; |
| 6502 | |
| 6503 | static PyTypeObject MethodDescriptorDerived_Type = { |
| 6504 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6505 | "MethodDescriptorDerived", |
| 6506 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6507 | }; |
| 6508 | |
| 6509 | static PyTypeObject MethodDescriptorNopGet_Type = { |
| 6510 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6511 | "MethodDescriptorNopGet", |
| 6512 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6513 | .tp_call = call_return_args, |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6514 | .tp_descr_get = nop_descr_get, |
| 6515 | }; |
| 6516 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6517 | typedef struct { |
| 6518 | MethodDescriptorObject base; |
| 6519 | vectorcallfunc vectorcall; |
| 6520 | } MethodDescriptor2Object; |
| 6521 | |
| 6522 | static PyObject * |
| 6523 | MethodDescriptor2_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 6524 | { |
| 6525 | MethodDescriptor2Object *op = PyObject_New(MethodDescriptor2Object, type); |
| 6526 | op->base.vectorcall = NULL; |
| 6527 | op->vectorcall = MethodDescriptor_vectorcall; |
| 6528 | return (PyObject *)op; |
| 6529 | } |
| 6530 | |
| 6531 | static PyTypeObject MethodDescriptor2_Type = { |
| 6532 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6533 | "MethodDescriptor2", |
| 6534 | sizeof(MethodDescriptor2Object), |
| 6535 | .tp_new = MethodDescriptor2_new, |
| 6536 | .tp_call = PyVectorcall_Call, |
| 6537 | .tp_vectorcall_offset = offsetof(MethodDescriptor2Object, vectorcall), |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 6538 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_VECTORCALL, |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6539 | }; |
| 6540 | |
Benjamin Peterson | 3940333 | 2020-09-02 11:29:06 -0500 | [diff] [blame] | 6541 | PyDoc_STRVAR(heapdocctype__doc__, |
| 6542 | "HeapDocCType(arg1, arg2)\n" |
| 6543 | "--\n" |
| 6544 | "\n" |
| 6545 | "somedoc"); |
| 6546 | |
| 6547 | typedef struct { |
| 6548 | PyObject_HEAD |
| 6549 | } HeapDocCTypeObject; |
| 6550 | |
| 6551 | static PyType_Slot HeapDocCType_slots[] = { |
| 6552 | {Py_tp_doc, (char*)heapdocctype__doc__}, |
| 6553 | {0}, |
| 6554 | }; |
| 6555 | |
| 6556 | static PyType_Spec HeapDocCType_spec = { |
| 6557 | "_testcapi.HeapDocCType", |
| 6558 | sizeof(HeapDocCTypeObject), |
| 6559 | 0, |
| 6560 | Py_TPFLAGS_DEFAULT, |
| 6561 | HeapDocCType_slots |
| 6562 | }; |
| 6563 | |
Hai Shi | 88c2cfd | 2020-11-07 00:04:47 +0800 | [diff] [blame] | 6564 | typedef struct { |
| 6565 | PyObject_HEAD |
| 6566 | } NullTpDocTypeObject; |
| 6567 | |
| 6568 | static PyType_Slot NullTpDocType_slots[] = { |
| 6569 | {Py_tp_doc, NULL}, |
| 6570 | {0, 0}, |
| 6571 | }; |
| 6572 | |
| 6573 | static PyType_Spec NullTpDocType_spec = { |
| 6574 | "_testcapi.NullTpDocType", |
| 6575 | sizeof(NullTpDocTypeObject), |
| 6576 | 0, |
| 6577 | Py_TPFLAGS_DEFAULT, |
| 6578 | NullTpDocType_slots |
| 6579 | }; |
| 6580 | |
Benjamin Peterson | 3940333 | 2020-09-02 11:29:06 -0500 | [diff] [blame] | 6581 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6582 | PyDoc_STRVAR(heapgctype__doc__, |
| 6583 | "A heap type with GC, and with overridden dealloc.\n\n" |
| 6584 | "The 'value' attribute is set to 10 in __init__."); |
| 6585 | |
| 6586 | typedef struct { |
| 6587 | PyObject_HEAD |
| 6588 | int value; |
| 6589 | } HeapCTypeObject; |
| 6590 | |
| 6591 | static struct PyMemberDef heapctype_members[] = { |
| 6592 | {"value", T_INT, offsetof(HeapCTypeObject, value)}, |
| 6593 | {NULL} /* Sentinel */ |
| 6594 | }; |
| 6595 | |
| 6596 | static int |
| 6597 | heapctype_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6598 | { |
| 6599 | ((HeapCTypeObject *)self)->value = 10; |
| 6600 | return 0; |
| 6601 | } |
| 6602 | |
Miss Islington (bot) | 7fe9cad | 2021-05-31 04:25:47 -0700 | [diff] [blame] | 6603 | static int |
| 6604 | heapgcctype_traverse(HeapCTypeObject *self, visitproc visit, void *arg) |
| 6605 | { |
| 6606 | Py_VISIT(Py_TYPE(self)); |
| 6607 | return 0; |
| 6608 | } |
| 6609 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6610 | static void |
| 6611 | heapgcctype_dealloc(HeapCTypeObject *self) |
| 6612 | { |
| 6613 | PyTypeObject *tp = Py_TYPE(self); |
| 6614 | PyObject_GC_UnTrack(self); |
| 6615 | PyObject_GC_Del(self); |
| 6616 | Py_DECREF(tp); |
| 6617 | } |
| 6618 | |
| 6619 | static PyType_Slot HeapGcCType_slots[] = { |
| 6620 | {Py_tp_init, heapctype_init}, |
| 6621 | {Py_tp_members, heapctype_members}, |
| 6622 | {Py_tp_dealloc, heapgcctype_dealloc}, |
Miss Islington (bot) | 7fe9cad | 2021-05-31 04:25:47 -0700 | [diff] [blame] | 6623 | {Py_tp_traverse, heapgcctype_traverse}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6624 | {Py_tp_doc, (char*)heapgctype__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6625 | {0, 0}, |
| 6626 | }; |
| 6627 | |
| 6628 | static PyType_Spec HeapGcCType_spec = { |
| 6629 | "_testcapi.HeapGcCType", |
| 6630 | sizeof(HeapCTypeObject), |
| 6631 | 0, |
| 6632 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6633 | HeapGcCType_slots |
| 6634 | }; |
| 6635 | |
| 6636 | PyDoc_STRVAR(heapctype__doc__, |
| 6637 | "A heap type without GC, but with overridden dealloc.\n\n" |
| 6638 | "The 'value' attribute is set to 10 in __init__."); |
| 6639 | |
| 6640 | static void |
| 6641 | heapctype_dealloc(HeapCTypeObject *self) |
| 6642 | { |
| 6643 | PyTypeObject *tp = Py_TYPE(self); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 6644 | PyObject_Free(self); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6645 | Py_DECREF(tp); |
| 6646 | } |
| 6647 | |
| 6648 | static PyType_Slot HeapCType_slots[] = { |
| 6649 | {Py_tp_init, heapctype_init}, |
| 6650 | {Py_tp_members, heapctype_members}, |
| 6651 | {Py_tp_dealloc, heapctype_dealloc}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6652 | {Py_tp_doc, (char*)heapctype__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6653 | {0, 0}, |
| 6654 | }; |
| 6655 | |
| 6656 | static PyType_Spec HeapCType_spec = { |
| 6657 | "_testcapi.HeapCType", |
| 6658 | sizeof(HeapCTypeObject), |
| 6659 | 0, |
| 6660 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6661 | HeapCType_slots |
| 6662 | }; |
| 6663 | |
| 6664 | PyDoc_STRVAR(heapctypesubclass__doc__, |
| 6665 | "Subclass of HeapCType, without GC.\n\n" |
| 6666 | "__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 6667 | |
| 6668 | typedef struct { |
| 6669 | HeapCTypeObject base; |
| 6670 | int value2; |
| 6671 | } HeapCTypeSubclassObject; |
| 6672 | |
| 6673 | static int |
| 6674 | heapctypesubclass_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6675 | { |
| 6676 | /* Call __init__ of the superclass */ |
| 6677 | if (heapctype_init(self, args, kwargs) < 0) { |
| 6678 | return -1; |
| 6679 | } |
| 6680 | /* Initialize additional element */ |
| 6681 | ((HeapCTypeSubclassObject *)self)->value2 = 20; |
| 6682 | return 0; |
| 6683 | } |
| 6684 | |
| 6685 | static struct PyMemberDef heapctypesubclass_members[] = { |
| 6686 | {"value2", T_INT, offsetof(HeapCTypeSubclassObject, value2)}, |
| 6687 | {NULL} /* Sentinel */ |
| 6688 | }; |
| 6689 | |
| 6690 | static PyType_Slot HeapCTypeSubclass_slots[] = { |
| 6691 | {Py_tp_init, heapctypesubclass_init}, |
| 6692 | {Py_tp_members, heapctypesubclass_members}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6693 | {Py_tp_doc, (char*)heapctypesubclass__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6694 | {0, 0}, |
| 6695 | }; |
| 6696 | |
| 6697 | static PyType_Spec HeapCTypeSubclass_spec = { |
| 6698 | "_testcapi.HeapCTypeSubclass", |
| 6699 | sizeof(HeapCTypeSubclassObject), |
| 6700 | 0, |
| 6701 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6702 | HeapCTypeSubclass_slots |
| 6703 | }; |
| 6704 | |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 6705 | PyDoc_STRVAR(heapctypewithbuffer__doc__, |
| 6706 | "Heap type with buffer support.\n\n" |
| 6707 | "The buffer is set to [b'1', b'2', b'3', b'4']"); |
| 6708 | |
| 6709 | typedef struct { |
| 6710 | HeapCTypeObject base; |
| 6711 | char buffer[4]; |
| 6712 | } HeapCTypeWithBufferObject; |
| 6713 | |
| 6714 | static int |
| 6715 | heapctypewithbuffer_getbuffer(HeapCTypeWithBufferObject *self, Py_buffer *view, int flags) |
| 6716 | { |
| 6717 | self->buffer[0] = '1'; |
| 6718 | self->buffer[1] = '2'; |
| 6719 | self->buffer[2] = '3'; |
| 6720 | self->buffer[3] = '4'; |
| 6721 | return PyBuffer_FillInfo( |
| 6722 | view, (PyObject*)self, (void *)self->buffer, 4, 1, flags); |
| 6723 | } |
| 6724 | |
Rémi Lapeyre | b8867e5 | 2020-06-07 09:05:33 +0200 | [diff] [blame] | 6725 | static void |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 6726 | heapctypewithbuffer_releasebuffer(HeapCTypeWithBufferObject *self, Py_buffer *view) |
| 6727 | { |
| 6728 | assert(view->obj == (void*) self); |
| 6729 | } |
| 6730 | |
| 6731 | static PyType_Slot HeapCTypeWithBuffer_slots[] = { |
| 6732 | {Py_bf_getbuffer, heapctypewithbuffer_getbuffer}, |
| 6733 | {Py_bf_releasebuffer, heapctypewithbuffer_releasebuffer}, |
| 6734 | {Py_tp_doc, (char*)heapctypewithbuffer__doc__}, |
| 6735 | {0, 0}, |
| 6736 | }; |
| 6737 | |
| 6738 | static PyType_Spec HeapCTypeWithBuffer_spec = { |
| 6739 | "_testcapi.HeapCTypeWithBuffer", |
| 6740 | sizeof(HeapCTypeWithBufferObject), |
| 6741 | 0, |
| 6742 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6743 | HeapCTypeWithBuffer_slots |
| 6744 | }; |
| 6745 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6746 | PyDoc_STRVAR(heapctypesubclasswithfinalizer__doc__, |
| 6747 | "Subclass of HeapCType with a finalizer that reassigns __class__.\n\n" |
| 6748 | "__class__ is set to plain HeapCTypeSubclass during finalization.\n" |
| 6749 | "__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 6750 | |
| 6751 | static int |
| 6752 | heapctypesubclasswithfinalizer_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6753 | { |
| 6754 | PyTypeObject *base = (PyTypeObject *)PyType_GetSlot(Py_TYPE(self), Py_tp_base); |
| 6755 | initproc base_init = PyType_GetSlot(base, Py_tp_init); |
| 6756 | base_init(self, args, kwargs); |
| 6757 | return 0; |
| 6758 | } |
| 6759 | |
| 6760 | static void |
| 6761 | heapctypesubclasswithfinalizer_finalize(PyObject *self) |
| 6762 | { |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6763 | PyObject *error_type, *error_value, *error_traceback, *m; |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6764 | PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL; |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6765 | |
| 6766 | /* Save the current exception, if any. */ |
| 6767 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 6768 | |
| 6769 | m = PyState_FindModule(&_testcapimodule); |
| 6770 | if (m == NULL) { |
| 6771 | goto cleanup_finalize; |
| 6772 | } |
| 6773 | oldtype = PyObject_GetAttrString(m, "HeapCTypeSubclassWithFinalizer"); |
| 6774 | newtype = PyObject_GetAttrString(m, "HeapCTypeSubclass"); |
| 6775 | if (oldtype == NULL || newtype == NULL) { |
| 6776 | goto cleanup_finalize; |
| 6777 | } |
| 6778 | |
| 6779 | if (PyObject_SetAttrString(self, "__class__", newtype) < 0) { |
| 6780 | goto cleanup_finalize; |
| 6781 | } |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6782 | refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype)); |
| 6783 | if (refcnt == NULL) { |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6784 | goto cleanup_finalize; |
| 6785 | } |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6786 | if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) { |
| 6787 | goto cleanup_finalize; |
| 6788 | } |
| 6789 | Py_DECREF(refcnt); |
| 6790 | refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype)); |
| 6791 | if (refcnt == NULL) { |
| 6792 | goto cleanup_finalize; |
| 6793 | } |
| 6794 | if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) { |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6795 | goto cleanup_finalize; |
| 6796 | } |
| 6797 | |
| 6798 | cleanup_finalize: |
| 6799 | Py_XDECREF(oldtype); |
| 6800 | Py_XDECREF(newtype); |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6801 | Py_XDECREF(refcnt); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6802 | |
| 6803 | /* Restore the saved exception. */ |
| 6804 | PyErr_Restore(error_type, error_value, error_traceback); |
| 6805 | } |
| 6806 | |
| 6807 | static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = { |
| 6808 | {Py_tp_init, heapctypesubclasswithfinalizer_init}, |
| 6809 | {Py_tp_members, heapctypesubclass_members}, |
| 6810 | {Py_tp_finalize, heapctypesubclasswithfinalizer_finalize}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6811 | {Py_tp_doc, (char*)heapctypesubclasswithfinalizer__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6812 | {0, 0}, |
| 6813 | }; |
| 6814 | |
| 6815 | static PyType_Spec HeapCTypeSubclassWithFinalizer_spec = { |
| 6816 | "_testcapi.HeapCTypeSubclassWithFinalizer", |
| 6817 | sizeof(HeapCTypeSubclassObject), |
| 6818 | 0, |
| 6819 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 6820 | HeapCTypeSubclassWithFinalizer_slots |
| 6821 | }; |
| 6822 | |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6823 | typedef struct { |
| 6824 | PyObject_HEAD |
| 6825 | PyObject *dict; |
| 6826 | } HeapCTypeWithDictObject; |
| 6827 | |
| 6828 | static void |
| 6829 | heapctypewithdict_dealloc(HeapCTypeWithDictObject* self) |
| 6830 | { |
| 6831 | |
| 6832 | PyTypeObject *tp = Py_TYPE(self); |
| 6833 | Py_XDECREF(self->dict); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 6834 | PyObject_Free(self); |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6835 | Py_DECREF(tp); |
| 6836 | } |
| 6837 | |
| 6838 | static PyGetSetDef heapctypewithdict_getsetlist[] = { |
| 6839 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
| 6840 | {NULL} /* Sentinel */ |
| 6841 | }; |
| 6842 | |
| 6843 | static struct PyMemberDef heapctypewithdict_members[] = { |
| 6844 | {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
| 6845 | {"__dictoffset__", T_PYSSIZET, offsetof(HeapCTypeWithDictObject, dict), READONLY}, |
| 6846 | {NULL} /* Sentinel */ |
| 6847 | }; |
| 6848 | |
| 6849 | static PyType_Slot HeapCTypeWithDict_slots[] = { |
| 6850 | {Py_tp_members, heapctypewithdict_members}, |
| 6851 | {Py_tp_getset, heapctypewithdict_getsetlist}, |
| 6852 | {Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 6853 | {0, 0}, |
| 6854 | }; |
| 6855 | |
| 6856 | static PyType_Spec HeapCTypeWithDict_spec = { |
| 6857 | "_testcapi.HeapCTypeWithDict", |
| 6858 | sizeof(HeapCTypeWithDictObject), |
| 6859 | 0, |
| 6860 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6861 | HeapCTypeWithDict_slots |
| 6862 | }; |
| 6863 | |
| 6864 | static struct PyMemberDef heapctypewithnegativedict_members[] = { |
| 6865 | {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
Victor Stinner | aca8c40 | 2019-09-30 21:14:26 +0200 | [diff] [blame] | 6866 | {"__dictoffset__", T_PYSSIZET, -(Py_ssize_t)sizeof(void*), READONLY}, |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6867 | {NULL} /* Sentinel */ |
| 6868 | }; |
| 6869 | |
| 6870 | static PyType_Slot HeapCTypeWithNegativeDict_slots[] = { |
| 6871 | {Py_tp_members, heapctypewithnegativedict_members}, |
| 6872 | {Py_tp_getset, heapctypewithdict_getsetlist}, |
| 6873 | {Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 6874 | {0, 0}, |
| 6875 | }; |
| 6876 | |
| 6877 | static PyType_Spec HeapCTypeWithNegativeDict_spec = { |
| 6878 | "_testcapi.HeapCTypeWithNegativeDict", |
| 6879 | sizeof(HeapCTypeWithDictObject), |
| 6880 | 0, |
| 6881 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6882 | HeapCTypeWithNegativeDict_slots |
| 6883 | }; |
| 6884 | |
| 6885 | typedef struct { |
| 6886 | PyObject_HEAD |
| 6887 | PyObject *weakreflist; |
| 6888 | } HeapCTypeWithWeakrefObject; |
| 6889 | |
| 6890 | static struct PyMemberDef heapctypewithweakref_members[] = { |
| 6891 | {"weakreflist", T_OBJECT, offsetof(HeapCTypeWithWeakrefObject, weakreflist)}, |
| 6892 | {"__weaklistoffset__", T_PYSSIZET, |
| 6893 | offsetof(HeapCTypeWithWeakrefObject, weakreflist), READONLY}, |
| 6894 | {NULL} /* Sentinel */ |
| 6895 | }; |
| 6896 | |
| 6897 | static void |
| 6898 | heapctypewithweakref_dealloc(HeapCTypeWithWeakrefObject* self) |
| 6899 | { |
| 6900 | |
| 6901 | PyTypeObject *tp = Py_TYPE(self); |
| 6902 | if (self->weakreflist != NULL) |
| 6903 | PyObject_ClearWeakRefs((PyObject *) self); |
| 6904 | Py_XDECREF(self->weakreflist); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 6905 | PyObject_Free(self); |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6906 | Py_DECREF(tp); |
| 6907 | } |
| 6908 | |
| 6909 | static PyType_Slot HeapCTypeWithWeakref_slots[] = { |
| 6910 | {Py_tp_members, heapctypewithweakref_members}, |
| 6911 | {Py_tp_dealloc, heapctypewithweakref_dealloc}, |
| 6912 | {0, 0}, |
| 6913 | }; |
| 6914 | |
| 6915 | static PyType_Spec HeapCTypeWithWeakref_spec = { |
| 6916 | "_testcapi.HeapCTypeWithWeakref", |
| 6917 | sizeof(HeapCTypeWithWeakrefObject), |
| 6918 | 0, |
| 6919 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6920 | HeapCTypeWithWeakref_slots |
| 6921 | }; |
| 6922 | |
scoder | 148f329 | 2020-07-03 02:09:28 +0200 | [diff] [blame] | 6923 | PyDoc_STRVAR(heapctypesetattr__doc__, |
| 6924 | "A heap type without GC, but with overridden __setattr__.\n\n" |
| 6925 | "The 'value' attribute is set to 10 in __init__ and updated via attribute setting."); |
| 6926 | |
| 6927 | typedef struct { |
| 6928 | PyObject_HEAD |
| 6929 | long value; |
| 6930 | } HeapCTypeSetattrObject; |
| 6931 | |
| 6932 | static struct PyMemberDef heapctypesetattr_members[] = { |
| 6933 | {"pvalue", T_LONG, offsetof(HeapCTypeSetattrObject, value)}, |
| 6934 | {NULL} /* Sentinel */ |
| 6935 | }; |
| 6936 | |
| 6937 | static int |
| 6938 | heapctypesetattr_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6939 | { |
| 6940 | ((HeapCTypeSetattrObject *)self)->value = 10; |
| 6941 | return 0; |
| 6942 | } |
| 6943 | |
| 6944 | static void |
| 6945 | heapctypesetattr_dealloc(HeapCTypeSetattrObject *self) |
| 6946 | { |
| 6947 | PyTypeObject *tp = Py_TYPE(self); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 6948 | PyObject_Free(self); |
scoder | 148f329 | 2020-07-03 02:09:28 +0200 | [diff] [blame] | 6949 | Py_DECREF(tp); |
| 6950 | } |
| 6951 | |
| 6952 | static int |
| 6953 | heapctypesetattr_setattro(HeapCTypeSetattrObject *self, PyObject *attr, PyObject *value) |
| 6954 | { |
| 6955 | PyObject *svalue = PyUnicode_FromString("value"); |
| 6956 | if (svalue == NULL) |
| 6957 | return -1; |
| 6958 | int eq = PyObject_RichCompareBool(svalue, attr, Py_EQ); |
| 6959 | Py_DECREF(svalue); |
| 6960 | if (eq < 0) |
| 6961 | return -1; |
| 6962 | if (!eq) { |
| 6963 | return PyObject_GenericSetAttr((PyObject*) self, attr, value); |
| 6964 | } |
| 6965 | if (value == NULL) { |
| 6966 | self->value = 0; |
| 6967 | return 0; |
| 6968 | } |
| 6969 | PyObject *ivalue = PyNumber_Long(value); |
| 6970 | if (ivalue == NULL) |
| 6971 | return -1; |
| 6972 | long v = PyLong_AsLong(ivalue); |
| 6973 | Py_DECREF(ivalue); |
| 6974 | if (v == -1 && PyErr_Occurred()) |
| 6975 | return -1; |
| 6976 | self->value = v; |
| 6977 | return 0; |
| 6978 | } |
| 6979 | |
| 6980 | static PyType_Slot HeapCTypeSetattr_slots[] = { |
| 6981 | {Py_tp_init, heapctypesetattr_init}, |
| 6982 | {Py_tp_members, heapctypesetattr_members}, |
| 6983 | {Py_tp_setattro, heapctypesetattr_setattro}, |
| 6984 | {Py_tp_dealloc, heapctypesetattr_dealloc}, |
| 6985 | {Py_tp_doc, (char*)heapctypesetattr__doc__}, |
| 6986 | {0, 0}, |
| 6987 | }; |
| 6988 | |
| 6989 | static PyType_Spec HeapCTypeSetattr_spec = { |
| 6990 | "_testcapi.HeapCTypeSetattr", |
| 6991 | sizeof(HeapCTypeSetattrObject), |
| 6992 | 0, |
| 6993 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6994 | HeapCTypeSetattr_slots |
| 6995 | }; |
| 6996 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6997 | static PyMethodDef meth_instance_methods[] = { |
| 6998 | {"meth_varargs", meth_varargs, METH_VARARGS}, |
| 6999 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, |
| 7000 | {"meth_o", meth_o, METH_O}, |
| 7001 | {"meth_noargs", meth_noargs, METH_NOARGS}, |
| 7002 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, |
| 7003 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS}, |
| 7004 | {NULL, NULL} /* sentinel */ |
| 7005 | }; |
| 7006 | |
| 7007 | |
| 7008 | static PyTypeObject MethInstance_Type = { |
| 7009 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7010 | "MethInstance", |
| 7011 | sizeof(PyObject), |
| 7012 | .tp_new = PyType_GenericNew, |
| 7013 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 7014 | .tp_methods = meth_instance_methods, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 7015 | .tp_doc = (char*)PyDoc_STR( |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 7016 | "Class with normal (instance) methods to test calling conventions"), |
| 7017 | }; |
| 7018 | |
| 7019 | static PyMethodDef meth_class_methods[] = { |
| 7020 | {"meth_varargs", meth_varargs, METH_VARARGS|METH_CLASS}, |
| 7021 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_CLASS}, |
| 7022 | {"meth_o", meth_o, METH_O|METH_CLASS}, |
| 7023 | {"meth_noargs", meth_noargs, METH_NOARGS|METH_CLASS}, |
| 7024 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_CLASS}, |
| 7025 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_CLASS}, |
| 7026 | {NULL, NULL} /* sentinel */ |
| 7027 | }; |
| 7028 | |
| 7029 | |
| 7030 | static PyTypeObject MethClass_Type = { |
| 7031 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7032 | "MethClass", |
| 7033 | sizeof(PyObject), |
| 7034 | .tp_new = PyType_GenericNew, |
| 7035 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 7036 | .tp_methods = meth_class_methods, |
| 7037 | .tp_doc = PyDoc_STR( |
| 7038 | "Class with class methods to test calling conventions"), |
| 7039 | }; |
| 7040 | |
| 7041 | static PyMethodDef meth_static_methods[] = { |
| 7042 | {"meth_varargs", meth_varargs, METH_VARARGS|METH_STATIC}, |
| 7043 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_STATIC}, |
| 7044 | {"meth_o", meth_o, METH_O|METH_STATIC}, |
| 7045 | {"meth_noargs", meth_noargs, METH_NOARGS|METH_STATIC}, |
| 7046 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_STATIC}, |
| 7047 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_STATIC}, |
| 7048 | {NULL, NULL} /* sentinel */ |
| 7049 | }; |
| 7050 | |
| 7051 | |
| 7052 | static PyTypeObject MethStatic_Type = { |
| 7053 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7054 | "MethStatic", |
| 7055 | sizeof(PyObject), |
| 7056 | .tp_new = PyType_GenericNew, |
| 7057 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 7058 | .tp_methods = meth_static_methods, |
| 7059 | .tp_doc = PyDoc_STR( |
| 7060 | "Class with static methods to test calling conventions"), |
| 7061 | }; |
| 7062 | |
Neil Schemenauer | 392a13b | 2019-10-15 20:56:48 -0700 | [diff] [blame] | 7063 | /* ContainerNoGC -- a simple container without GC methods */ |
| 7064 | |
| 7065 | typedef struct { |
| 7066 | PyObject_HEAD |
| 7067 | PyObject *value; |
| 7068 | } ContainerNoGCobject; |
| 7069 | |
| 7070 | static PyObject * |
| 7071 | ContainerNoGC_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 7072 | { |
| 7073 | PyObject *value; |
| 7074 | char *names[] = {"value", NULL}; |
| 7075 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", names, &value)) { |
| 7076 | return NULL; |
| 7077 | } |
| 7078 | PyObject *self = type->tp_alloc(type, 0); |
| 7079 | if (self == NULL) { |
| 7080 | return NULL; |
| 7081 | } |
| 7082 | Py_INCREF(value); |
| 7083 | ((ContainerNoGCobject *)self)->value = value; |
| 7084 | return self; |
| 7085 | } |
| 7086 | |
| 7087 | static void |
| 7088 | ContainerNoGC_dealloc(ContainerNoGCobject *self) |
| 7089 | { |
| 7090 | Py_DECREF(self->value); |
| 7091 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 7092 | } |
| 7093 | |
| 7094 | static PyMemberDef ContainerNoGC_members[] = { |
| 7095 | {"value", T_OBJECT, offsetof(ContainerNoGCobject, value), READONLY, |
| 7096 | PyDoc_STR("a container value for test purposes")}, |
| 7097 | {0} |
| 7098 | }; |
| 7099 | |
| 7100 | static PyTypeObject ContainerNoGC_type = { |
| 7101 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7102 | "_testcapi.ContainerNoGC", |
| 7103 | sizeof(ContainerNoGCobject), |
| 7104 | .tp_dealloc = (destructor)ContainerNoGC_dealloc, |
| 7105 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 7106 | .tp_members = ContainerNoGC_members, |
| 7107 | .tp_new = ContainerNoGC_new, |
| 7108 | }; |
| 7109 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 7110 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7111 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7112 | PyModuleDef_HEAD_INIT, |
| 7113 | "_testcapi", |
| 7114 | NULL, |
| 7115 | -1, |
| 7116 | TestMethods, |
| 7117 | NULL, |
| 7118 | NULL, |
| 7119 | NULL, |
| 7120 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7121 | }; |
| 7122 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 7123 | /* Per PEP 489, this module will not be converted to multi-phase initialization |
| 7124 | */ |
| 7125 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 7126 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7127 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 7128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7129 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 7130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7131 | m = PyModule_Create(&_testcapimodule); |
| 7132 | if (m == NULL) |
| 7133 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 7134 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 7135 | Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type); |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 7136 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 7137 | Py_SET_TYPE(&test_structmembersType, &PyType_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7138 | Py_INCREF(&test_structmembersType); |
| 7139 | /* don't use a name starting with "test", since we don't want |
| 7140 | test_capi to automatically call this */ |
| 7141 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 7142 | if (PyType_Ready(&matmulType) < 0) |
| 7143 | return NULL; |
| 7144 | Py_INCREF(&matmulType); |
| 7145 | PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); |
Zackery Spytz | c7f803b | 2019-05-31 03:46:36 -0600 | [diff] [blame] | 7146 | if (PyType_Ready(&ipowType) < 0) { |
| 7147 | return NULL; |
| 7148 | } |
| 7149 | Py_INCREF(&ipowType); |
| 7150 | PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7151 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 7152 | if (PyType_Ready(&awaitType) < 0) |
| 7153 | return NULL; |
| 7154 | Py_INCREF(&awaitType); |
| 7155 | PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType); |
| 7156 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 7157 | MyList_Type.tp_base = &PyList_Type; |
| 7158 | if (PyType_Ready(&MyList_Type) < 0) |
| 7159 | return NULL; |
| 7160 | Py_INCREF(&MyList_Type); |
| 7161 | PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type); |
| 7162 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 7163 | if (PyType_Ready(&MethodDescriptorBase_Type) < 0) |
| 7164 | return NULL; |
| 7165 | Py_INCREF(&MethodDescriptorBase_Type); |
| 7166 | PyModule_AddObject(m, "MethodDescriptorBase", (PyObject *)&MethodDescriptorBase_Type); |
| 7167 | |
| 7168 | MethodDescriptorDerived_Type.tp_base = &MethodDescriptorBase_Type; |
| 7169 | if (PyType_Ready(&MethodDescriptorDerived_Type) < 0) |
| 7170 | return NULL; |
| 7171 | Py_INCREF(&MethodDescriptorDerived_Type); |
| 7172 | PyModule_AddObject(m, "MethodDescriptorDerived", (PyObject *)&MethodDescriptorDerived_Type); |
| 7173 | |
| 7174 | MethodDescriptorNopGet_Type.tp_base = &MethodDescriptorBase_Type; |
| 7175 | if (PyType_Ready(&MethodDescriptorNopGet_Type) < 0) |
| 7176 | return NULL; |
| 7177 | Py_INCREF(&MethodDescriptorNopGet_Type); |
| 7178 | PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type); |
| 7179 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 7180 | MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type; |
| 7181 | if (PyType_Ready(&MethodDescriptor2_Type) < 0) |
| 7182 | return NULL; |
| 7183 | Py_INCREF(&MethodDescriptor2_Type); |
| 7184 | PyModule_AddObject(m, "MethodDescriptor2", (PyObject *)&MethodDescriptor2_Type); |
| 7185 | |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 7186 | if (PyType_Ready(&GenericAlias_Type) < 0) |
| 7187 | return NULL; |
| 7188 | Py_INCREF(&GenericAlias_Type); |
| 7189 | PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type); |
| 7190 | |
| 7191 | if (PyType_Ready(&Generic_Type) < 0) |
| 7192 | return NULL; |
| 7193 | Py_INCREF(&Generic_Type); |
| 7194 | PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type); |
| 7195 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 7196 | if (PyType_Ready(&MethInstance_Type) < 0) |
| 7197 | return NULL; |
| 7198 | Py_INCREF(&MethInstance_Type); |
| 7199 | PyModule_AddObject(m, "MethInstance", (PyObject *)&MethInstance_Type); |
| 7200 | |
| 7201 | if (PyType_Ready(&MethClass_Type) < 0) |
| 7202 | return NULL; |
| 7203 | Py_INCREF(&MethClass_Type); |
| 7204 | PyModule_AddObject(m, "MethClass", (PyObject *)&MethClass_Type); |
| 7205 | |
| 7206 | if (PyType_Ready(&MethStatic_Type) < 0) |
| 7207 | return NULL; |
| 7208 | Py_INCREF(&MethStatic_Type); |
| 7209 | PyModule_AddObject(m, "MethStatic", (PyObject *)&MethStatic_Type); |
| 7210 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 7211 | PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception; |
| 7212 | if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) { |
| 7213 | return NULL; |
| 7214 | } |
| 7215 | Py_INCREF(&PyRecursingInfinitelyError_Type); |
| 7216 | PyModule_AddObject(m, "RecursingInfinitelyError", |
| 7217 | (PyObject *)&PyRecursingInfinitelyError_Type); |
| 7218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7219 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 7220 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 7221 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 7222 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 7223 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 7224 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 7225 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 7226 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 7227 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 7228 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 7229 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 7230 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 7231 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 7232 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 7233 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 7234 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 7235 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX)); |
| 7236 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN)); |
| 7237 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7238 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 7239 | 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] | 7240 | PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7241 | Py_INCREF(&PyInstanceMethod_Type); |
| 7242 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 7243 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 7244 | PyModule_AddIntConstant(m, "the_number_three", 3); |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 7245 | PyObject *v; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 7246 | #ifdef WITH_PYMALLOC |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 7247 | v = Py_True; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 7248 | #else |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 7249 | v = Py_False; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 7250 | #endif |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 7251 | Py_INCREF(v); |
| 7252 | PyModule_AddObject(m, "WITH_PYMALLOC", v); |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 7253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7254 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 7255 | Py_INCREF(TestError); |
| 7256 | PyModule_AddObject(m, "error", TestError); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 7257 | |
Benjamin Peterson | 3940333 | 2020-09-02 11:29:06 -0500 | [diff] [blame] | 7258 | PyObject *HeapDocCType = PyType_FromSpec(&HeapDocCType_spec); |
| 7259 | if (HeapDocCType == NULL) { |
| 7260 | return NULL; |
| 7261 | } |
| 7262 | PyModule_AddObject(m, "HeapDocCType", HeapDocCType); |
| 7263 | |
Hai Shi | 88c2cfd | 2020-11-07 00:04:47 +0800 | [diff] [blame] | 7264 | /* bpo-41832: Add a new type to test PyType_FromSpec() |
| 7265 | now can accept a NULL tp_doc slot. */ |
| 7266 | PyObject *NullTpDocType = PyType_FromSpec(&NullTpDocType_spec); |
| 7267 | if (NullTpDocType == NULL) { |
| 7268 | return NULL; |
| 7269 | } |
| 7270 | PyModule_AddObject(m, "NullTpDocType", NullTpDocType); |
| 7271 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 7272 | PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec); |
| 7273 | if (HeapGcCType == NULL) { |
| 7274 | return NULL; |
| 7275 | } |
| 7276 | PyModule_AddObject(m, "HeapGcCType", HeapGcCType); |
| 7277 | |
| 7278 | PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec); |
| 7279 | if (HeapCType == NULL) { |
| 7280 | return NULL; |
| 7281 | } |
| 7282 | PyObject *subclass_bases = PyTuple_Pack(1, HeapCType); |
| 7283 | if (subclass_bases == NULL) { |
| 7284 | return NULL; |
| 7285 | } |
| 7286 | PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases); |
| 7287 | if (HeapCTypeSubclass == NULL) { |
| 7288 | return NULL; |
| 7289 | } |
| 7290 | Py_DECREF(subclass_bases); |
| 7291 | PyModule_AddObject(m, "HeapCTypeSubclass", HeapCTypeSubclass); |
| 7292 | |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 7293 | PyObject *HeapCTypeWithDict = PyType_FromSpec(&HeapCTypeWithDict_spec); |
| 7294 | if (HeapCTypeWithDict == NULL) { |
| 7295 | return NULL; |
| 7296 | } |
| 7297 | PyModule_AddObject(m, "HeapCTypeWithDict", HeapCTypeWithDict); |
| 7298 | |
| 7299 | PyObject *HeapCTypeWithNegativeDict = PyType_FromSpec(&HeapCTypeWithNegativeDict_spec); |
| 7300 | if (HeapCTypeWithNegativeDict == NULL) { |
| 7301 | return NULL; |
| 7302 | } |
| 7303 | PyModule_AddObject(m, "HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict); |
| 7304 | |
| 7305 | PyObject *HeapCTypeWithWeakref = PyType_FromSpec(&HeapCTypeWithWeakref_spec); |
| 7306 | if (HeapCTypeWithWeakref == NULL) { |
| 7307 | return NULL; |
| 7308 | } |
| 7309 | PyModule_AddObject(m, "HeapCTypeWithWeakref", HeapCTypeWithWeakref); |
| 7310 | |
scoder | f7c4e23 | 2020-06-06 21:35:10 +0200 | [diff] [blame] | 7311 | PyObject *HeapCTypeWithBuffer = PyType_FromSpec(&HeapCTypeWithBuffer_spec); |
| 7312 | if (HeapCTypeWithBuffer == NULL) { |
| 7313 | return NULL; |
| 7314 | } |
| 7315 | PyModule_AddObject(m, "HeapCTypeWithBuffer", HeapCTypeWithBuffer); |
| 7316 | |
scoder | 148f329 | 2020-07-03 02:09:28 +0200 | [diff] [blame] | 7317 | PyObject *HeapCTypeSetattr = PyType_FromSpec(&HeapCTypeSetattr_spec); |
| 7318 | if (HeapCTypeSetattr == NULL) { |
| 7319 | return NULL; |
| 7320 | } |
| 7321 | PyModule_AddObject(m, "HeapCTypeSetattr", HeapCTypeSetattr); |
| 7322 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 7323 | PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass); |
| 7324 | if (subclass_with_finalizer_bases == NULL) { |
| 7325 | return NULL; |
| 7326 | } |
| 7327 | PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases( |
| 7328 | &HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases); |
| 7329 | if (HeapCTypeSubclassWithFinalizer == NULL) { |
| 7330 | return NULL; |
| 7331 | } |
| 7332 | Py_DECREF(subclass_with_finalizer_bases); |
| 7333 | PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer); |
| 7334 | |
Neil Schemenauer | 392a13b | 2019-10-15 20:56:48 -0700 | [diff] [blame] | 7335 | if (PyType_Ready(&ContainerNoGC_type) < 0) { |
| 7336 | return NULL; |
| 7337 | } |
| 7338 | Py_INCREF(&ContainerNoGC_type); |
| 7339 | if (PyModule_AddObject(m, "ContainerNoGC", |
| 7340 | (PyObject *) &ContainerNoGC_type) < 0) |
| 7341 | return NULL; |
| 7342 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 7343 | PyState_AddModule(m, &_testcapimodule); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7344 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 7345 | } |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7346 | |
| 7347 | |
| 7348 | /* Test the C API exposed when PY_SSIZE_T_CLEAN is not defined */ |
| 7349 | |
| 7350 | #undef Py_BuildValue |
| 7351 | PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...); |
| 7352 | |
| 7353 | static PyObject * |
| 7354 | test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 7355 | { |
| 7356 | PyObject *res; |
| 7357 | const char str[] = "string"; |
| 7358 | const Py_UNICODE unicode[] = L"unicode"; |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7359 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7360 | |
| 7361 | res = Py_BuildValue("(s#O)", str, 1, Py_None); |
| 7362 | assert(res == NULL); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7363 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7364 | return NULL; |
| 7365 | } |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7366 | PyErr_Clear(); |
| 7367 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7368 | res = Py_BuildValue("(z#O)", str, 1, Py_None); |
| 7369 | assert(res == NULL); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7370 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7371 | return NULL; |
| 7372 | } |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7373 | PyErr_Clear(); |
| 7374 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7375 | res = Py_BuildValue("(y#O)", str, 1, Py_None); |
| 7376 | assert(res == NULL); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7377 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7378 | return NULL; |
| 7379 | } |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7380 | PyErr_Clear(); |
| 7381 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7382 | res = Py_BuildValue("(u#O)", unicode, 1, Py_None); |
| 7383 | assert(res == NULL); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7384 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7385 | return NULL; |
| 7386 | } |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7387 | PyErr_Clear(); |
Victor Stinner | 37bb289 | 2020-06-19 11:45:31 +0200 | [diff] [blame] | 7388 | |
| 7389 | |
Serhiy Storchaka | 28d0bca | 2020-03-02 08:42:39 +0200 | [diff] [blame] | 7390 | Py_RETURN_NONE; |
| 7391 | } |
Miss Islington (bot) | 569ca81 | 2021-05-06 20:18:42 -0700 | [diff] [blame] | 7392 | |
| 7393 | #undef PyArg_ParseTupleAndKeywords |
| 7394 | PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
| 7395 | const char *, char **, ...); |
| 7396 | |
| 7397 | static PyObject * |
| 7398 | getargs_s_hash_int(PyObject *self, PyObject *args, PyObject *kwargs) |
| 7399 | { |
| 7400 | static char *keywords[] = {"", "x", NULL}; |
| 7401 | const char *s; |
| 7402 | int len; |
| 7403 | int i = 0; |
| 7404 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s#i", keywords, &s, &len, &i)) |
| 7405 | return NULL; |
| 7406 | Py_RETURN_NONE; |
| 7407 | } |