Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * C Extension module to test Python interpreter C APIs. |
| 3 | * |
| 4 | * The 'test_*' functions exported by this module are run as part of the |
| 5 | * standard Python regression test, via Lib/test/test_capi.py. |
| 6 | */ |
| 7 | |
Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 8 | /* The Visual Studio projects builds _testcapi with Py_BUILD_CORE_MODULE |
| 9 | define, but we only want to test the public C API, not the internal |
| 10 | C API. */ |
| 11 | #undef Py_BUILD_CORE_MODULE |
| 12 | |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 13 | #define PY_SSIZE_T_CLEAN |
| 14 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 15 | #include "Python.h" |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 16 | #include "datetime.h" |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 17 | #include "marshal.h" |
Victor Stinner | a1c249c | 2018-11-01 03:15:58 +0100 | [diff] [blame] | 18 | #include "pythread.h" |
| 19 | #include "structmember.h" |
| 20 | #include <float.h> |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 21 | #include <signal.h> |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 22 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 23 | #ifdef MS_WINDOWS |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 24 | # include <winsock2.h> /* struct timeval */ |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 25 | #endif |
| 26 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 27 | #ifdef HAVE_SYS_WAIT_H |
| 28 | #include <sys/wait.h> /* For W_STOPCODE */ |
| 29 | #endif |
| 30 | |
Victor Stinner | 5ed6995 | 2018-11-06 15:59:52 +0100 | [diff] [blame] | 31 | #ifdef Py_BUILD_CORE |
| 32 | # error "_testcapi must test the public Python C API, not CPython internal C API" |
| 33 | #endif |
| 34 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 35 | static struct PyModuleDef _testcapimodule; |
| 36 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | static PyObject *TestError; /* set to exception object in init */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 38 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 39 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 40 | |
| 41 | static PyObject * |
| 42 | raiseTestError(const char* test_name, const char* msg) |
| 43 | { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 44 | PyErr_Format(TestError, "%s: %s", test_name, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 48 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 49 | |
| 50 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 51 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 52 | platforms have these hardcoded. Better safe than sorry. |
| 53 | */ |
| 54 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 55 | sizeof_error(const char* fatname, const char* typname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | int expected, int got) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 57 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 58 | PyErr_Format(TestError, |
| 59 | "%s #define == %d but sizeof(%s) == %d", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | fatname, expected, typname, got); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | return (PyObject*)NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 65 | test_config(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 66 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 67 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | if (FATNAME != sizeof(TYPE)) \ |
| 69 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 72 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 73 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 74 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 75 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 76 | CHECK_SIZEOF(SIZEOF_LONG_LONG, long long); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 77 | |
| 78 | #undef CHECK_SIZEOF |
| 79 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 80 | Py_RETURN_NONE; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 83 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 84 | test_sizeof_c_types(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 85 | { |
Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 86 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 87 | #pragma GCC diagnostic push |
| 88 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 89 | #endif |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 90 | #define CHECK_SIZEOF(TYPE, EXPECTED) \ |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 91 | if (EXPECTED != sizeof(TYPE)) { \ |
| 92 | PyErr_Format(TestError, \ |
| 93 | "sizeof(%s) = %u instead of %u", \ |
| 94 | #TYPE, sizeof(TYPE), EXPECTED); \ |
| 95 | return (PyObject*)NULL; \ |
| 96 | } |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 97 | #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0) |
| 98 | #define CHECK_SIGNNESS(TYPE, SIGNED) \ |
| 99 | if (IS_SIGNED(TYPE) != SIGNED) { \ |
| 100 | PyErr_Format(TestError, \ |
| 101 | "%s signness is, instead of %i", \ |
| 102 | #TYPE, IS_SIGNED(TYPE), SIGNED); \ |
| 103 | return (PyObject*)NULL; \ |
| 104 | } |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 105 | |
| 106 | /* integer types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 107 | CHECK_SIZEOF(Py_UCS1, 1); |
| 108 | CHECK_SIZEOF(Py_UCS2, 2); |
| 109 | CHECK_SIZEOF(Py_UCS4, 4); |
| 110 | CHECK_SIGNNESS(Py_UCS1, 0); |
| 111 | CHECK_SIGNNESS(Py_UCS2, 0); |
| 112 | CHECK_SIGNNESS(Py_UCS4, 0); |
Benjamin Peterson | 9b3d770 | 2016-09-06 13:24:00 -0700 | [diff] [blame] | 113 | CHECK_SIZEOF(int32_t, 4); |
| 114 | CHECK_SIGNNESS(int32_t, 1); |
| 115 | CHECK_SIZEOF(uint32_t, 4); |
| 116 | CHECK_SIGNNESS(uint32_t, 0); |
| 117 | CHECK_SIZEOF(int64_t, 8); |
| 118 | CHECK_SIGNNESS(int64_t, 1); |
| 119 | CHECK_SIZEOF(uint64_t, 8); |
| 120 | CHECK_SIGNNESS(uint64_t, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 121 | |
| 122 | /* pointer/size types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 123 | CHECK_SIZEOF(size_t, sizeof(void *)); |
| 124 | CHECK_SIGNNESS(size_t, 0); |
| 125 | CHECK_SIZEOF(Py_ssize_t, sizeof(void *)); |
| 126 | CHECK_SIGNNESS(Py_ssize_t, 1); |
| 127 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 128 | CHECK_SIZEOF(uintptr_t, sizeof(void *)); |
| 129 | CHECK_SIGNNESS(uintptr_t, 0); |
| 130 | CHECK_SIZEOF(intptr_t, sizeof(void *)); |
| 131 | CHECK_SIGNNESS(intptr_t, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 132 | |
Serhiy Storchaka | d1302c0 | 2017-01-23 10:23:58 +0200 | [diff] [blame] | 133 | Py_RETURN_NONE; |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 134 | |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 135 | #undef IS_SIGNED |
| 136 | #undef CHECK_SIGNESS |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 137 | #undef CHECK_SIZEOF |
Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 138 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 139 | #pragma GCC diagnostic pop |
| 140 | #endif |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 145 | test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | PyObject* list; |
| 148 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 151 | #define NLIST 30 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | list = PyList_New(NLIST); |
| 153 | if (list == (PyObject*)NULL) |
| 154 | return (PyObject*)NULL; |
| 155 | /* list = range(NLIST) */ |
| 156 | for (i = 0; i < NLIST; ++i) { |
| 157 | PyObject* anint = PyLong_FromLong(i); |
| 158 | if (anint == (PyObject*)NULL) { |
| 159 | Py_DECREF(list); |
| 160 | return (PyObject*)NULL; |
| 161 | } |
| 162 | PyList_SET_ITEM(list, i, anint); |
| 163 | } |
| 164 | /* list.reverse(), via PyList_Reverse() */ |
| 165 | i = PyList_Reverse(list); /* should not blow up! */ |
| 166 | if (i != 0) { |
| 167 | Py_DECREF(list); |
| 168 | return (PyObject*)NULL; |
| 169 | } |
| 170 | /* Check that list == range(29, -1, -1) now */ |
| 171 | for (i = 0; i < NLIST; ++i) { |
| 172 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 173 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 174 | PyErr_SetString(TestError, |
| 175 | "test_list_api: reverse screwed up"); |
| 176 | Py_DECREF(list); |
| 177 | return (PyObject*)NULL; |
| 178 | } |
| 179 | } |
| 180 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 181 | #undef NLIST |
| 182 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 183 | Py_RETURN_NONE; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 186 | static int |
| 187 | test_dict_inner(int count) |
| 188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | Py_ssize_t pos = 0, iterations = 0; |
| 190 | int i; |
| 191 | PyObject *dict = PyDict_New(); |
| 192 | PyObject *v, *k; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | if (dict == NULL) |
| 195 | return -1; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | for (i = 0; i < count; i++) { |
| 198 | v = PyLong_FromLong(i); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 199 | if (v == NULL) { |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 200 | return -1; |
| 201 | } |
Christian Heimes | 97cb67b | 2013-07-20 15:01:26 +0200 | [diff] [blame] | 202 | if (PyDict_SetItem(dict, v, v) < 0) { |
| 203 | Py_DECREF(v); |
| 204 | return -1; |
| 205 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | Py_DECREF(v); |
| 207 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 210 | PyObject *o; |
| 211 | iterations++; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | i = PyLong_AS_LONG(v) + 1; |
| 214 | o = PyLong_FromLong(i); |
| 215 | if (o == NULL) |
| 216 | return -1; |
| 217 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 218 | Py_DECREF(o); |
| 219 | return -1; |
| 220 | } |
| 221 | Py_DECREF(o); |
| 222 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | Py_DECREF(dict); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | if (iterations != count) { |
| 227 | PyErr_SetString( |
| 228 | TestError, |
| 229 | "test_dict_iteration: dict iteration went wrong "); |
| 230 | return -1; |
| 231 | } else { |
| 232 | return 0; |
| 233 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 237 | test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 238 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | for (i = 0; i < 200; i++) { |
| 242 | if (test_dict_inner(i) < 0) { |
| 243 | return NULL; |
| 244 | } |
| 245 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 246 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 247 | Py_RETURN_NONE; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 250 | static PyObject* |
| 251 | dict_getitem_knownhash(PyObject *self, PyObject *args) |
| 252 | { |
| 253 | PyObject *mp, *key, *result; |
| 254 | Py_ssize_t hash; |
| 255 | |
| 256 | if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash", |
| 257 | &mp, &key, &hash)) { |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash); |
| 262 | if (result == NULL && !PyErr_Occurred()) { |
| 263 | _PyErr_SetKeyError(key); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | Py_XINCREF(result); |
| 268 | return result; |
| 269 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 270 | |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 271 | static PyObject* |
| 272 | dict_hassplittable(PyObject *self, PyObject *arg) |
| 273 | { |
| 274 | if (!PyDict_Check(arg)) { |
| 275 | PyErr_Format(PyExc_TypeError, |
| 276 | "dict_hassplittable() argument must be dict, not '%s'", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 277 | Py_TYPE(arg)->tp_name); |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg)); |
| 282 | } |
| 283 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 284 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 285 | * PyType_Ready if it hasn't already been called |
| 286 | */ |
| 287 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | PyVarObject_HEAD_INIT(NULL, 0) |
| 289 | "hashinheritancetester", /* Name of this type */ |
| 290 | sizeof(PyObject), /* Basic object size */ |
| 291 | 0, /* Item size for varobject */ |
| 292 | (destructor)PyObject_Del, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 293 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | 0, /* tp_getattr */ |
| 295 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 296 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | 0, /* tp_repr */ |
| 298 | 0, /* tp_as_number */ |
| 299 | 0, /* tp_as_sequence */ |
| 300 | 0, /* tp_as_mapping */ |
| 301 | 0, /* tp_hash */ |
| 302 | 0, /* tp_call */ |
| 303 | 0, /* tp_str */ |
| 304 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 305 | 0, /* tp_setattro */ |
| 306 | 0, /* tp_as_buffer */ |
| 307 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 308 | 0, /* tp_doc */ |
| 309 | 0, /* tp_traverse */ |
| 310 | 0, /* tp_clear */ |
| 311 | 0, /* tp_richcompare */ |
| 312 | 0, /* tp_weaklistoffset */ |
| 313 | 0, /* tp_iter */ |
| 314 | 0, /* tp_iternext */ |
| 315 | 0, /* tp_methods */ |
| 316 | 0, /* tp_members */ |
| 317 | 0, /* tp_getset */ |
| 318 | 0, /* tp_base */ |
| 319 | 0, /* tp_dict */ |
| 320 | 0, /* tp_descr_get */ |
| 321 | 0, /* tp_descr_set */ |
| 322 | 0, /* tp_dictoffset */ |
| 323 | 0, /* tp_init */ |
| 324 | 0, /* tp_alloc */ |
| 325 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 329 | test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | PyTypeObject *type; |
| 332 | PyObject *obj; |
Antoine Pitrou | 29aad00 | 2010-10-23 19:42:38 +0000 | [diff] [blame] | 333 | Py_hash_t hash; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | if (type->tp_dict != NULL) |
| 338 | /* The type has already been initialized. This probably means |
| 339 | -R is being used. */ |
| 340 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 341 | |
| 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | obj = PyObject_New(PyObject, type); |
| 344 | if (obj == NULL) { |
| 345 | PyErr_Clear(); |
| 346 | PyErr_SetString( |
| 347 | TestError, |
| 348 | "test_lazy_hash_inheritance: failed to create object"); |
| 349 | return NULL; |
| 350 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (type->tp_dict != NULL) { |
| 353 | PyErr_SetString( |
| 354 | TestError, |
| 355 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 356 | Py_DECREF(obj); |
| 357 | return NULL; |
| 358 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | hash = PyObject_Hash(obj); |
| 361 | if ((hash == -1) && PyErr_Occurred()) { |
| 362 | PyErr_Clear(); |
| 363 | PyErr_SetString( |
| 364 | TestError, |
| 365 | "test_lazy_hash_inheritance: could not hash object"); |
| 366 | Py_DECREF(obj); |
| 367 | return NULL; |
| 368 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | if (type->tp_dict == NULL) { |
| 371 | PyErr_SetString( |
| 372 | TestError, |
| 373 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 374 | Py_DECREF(obj); |
| 375 | return NULL; |
| 376 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 379 | PyErr_SetString( |
| 380 | TestError, |
| 381 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 382 | Py_DECREF(obj); |
| 383 | return NULL; |
| 384 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | Py_RETURN_NONE; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 392 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 393 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 394 | |
| 395 | Note that the meat of the test is contained in testcapi_long.h. |
| 396 | This is revolting, but delicate code duplication is worse: "almost |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 397 | 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] | 398 | dependence on type names makes it impossible to use a parameterized |
| 399 | function. A giant macro would be even worse than this. A C++ template |
| 400 | would be perfect. |
| 401 | |
| 402 | The "report an error" functions are deliberately not part of the #include |
| 403 | file: if the test fails, you can set a breakpoint in the appropriate |
| 404 | error function directly, and crawl back from there in the debugger. |
| 405 | */ |
| 406 | |
| 407 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 408 | |
| 409 | static PyObject * |
| 410 | raise_test_long_error(const char* msg) |
| 411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | #define TESTNAME test_long_api_inner |
| 416 | #define TYPENAME long |
| 417 | #define F_S_TO_PY PyLong_FromLong |
| 418 | #define F_PY_TO_S PyLong_AsLong |
| 419 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 420 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 421 | |
| 422 | #include "testcapi_long.h" |
| 423 | |
| 424 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 425 | test_long_api(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 426 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | #undef TESTNAME |
| 431 | #undef TYPENAME |
| 432 | #undef F_S_TO_PY |
| 433 | #undef F_PY_TO_S |
| 434 | #undef F_U_TO_PY |
| 435 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 436 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 437 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 438 | raise_test_longlong_error(const char* msg) |
| 439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | #define TESTNAME test_longlong_api_inner |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 444 | #define TYPENAME long long |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | #define F_S_TO_PY PyLong_FromLongLong |
| 446 | #define F_PY_TO_S PyLong_AsLongLong |
| 447 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 448 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 449 | |
| 450 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 451 | |
| 452 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 453 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 458 | #undef TESTNAME |
| 459 | #undef TYPENAME |
| 460 | #undef F_S_TO_PY |
| 461 | #undef F_PY_TO_S |
| 462 | #undef F_U_TO_PY |
| 463 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 464 | |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 465 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 466 | is tested by test_long_api_inner. This test will concentrate on proper |
| 467 | handling of overflow. |
| 468 | */ |
| 469 | |
| 470 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 471 | test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | PyObject *num, *one, *temp; |
| 474 | long value; |
| 475 | int overflow; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | /* Test that overflow is set properly for a large value. */ |
| 478 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 479 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 480 | if (num == NULL) |
| 481 | return NULL; |
| 482 | overflow = 1234; |
| 483 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 484 | Py_DECREF(num); |
| 485 | if (value == -1 && PyErr_Occurred()) |
| 486 | return NULL; |
| 487 | if (value != -1) |
| 488 | return raiseTestError("test_long_and_overflow", |
| 489 | "return value was not set to -1"); |
| 490 | if (overflow != 1) |
| 491 | return raiseTestError("test_long_and_overflow", |
| 492 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | /* Same again, with num = LONG_MAX + 1 */ |
| 495 | num = PyLong_FromLong(LONG_MAX); |
| 496 | if (num == NULL) |
| 497 | return NULL; |
| 498 | one = PyLong_FromLong(1L); |
| 499 | if (one == NULL) { |
| 500 | Py_DECREF(num); |
| 501 | return NULL; |
| 502 | } |
| 503 | temp = PyNumber_Add(num, one); |
| 504 | Py_DECREF(one); |
| 505 | Py_DECREF(num); |
| 506 | num = temp; |
| 507 | if (num == NULL) |
| 508 | return NULL; |
| 509 | overflow = 0; |
| 510 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 511 | Py_DECREF(num); |
| 512 | if (value == -1 && PyErr_Occurred()) |
| 513 | return NULL; |
| 514 | if (value != -1) |
| 515 | return raiseTestError("test_long_and_overflow", |
| 516 | "return value was not set to -1"); |
| 517 | if (overflow != 1) |
| 518 | return raiseTestError("test_long_and_overflow", |
| 519 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | /* Test that overflow is set properly for a large negative value. */ |
| 522 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 523 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 524 | if (num == NULL) |
| 525 | return NULL; |
| 526 | overflow = 1234; |
| 527 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 528 | Py_DECREF(num); |
| 529 | if (value == -1 && PyErr_Occurred()) |
| 530 | return NULL; |
| 531 | if (value != -1) |
| 532 | return raiseTestError("test_long_and_overflow", |
| 533 | "return value was not set to -1"); |
| 534 | if (overflow != -1) |
| 535 | return raiseTestError("test_long_and_overflow", |
| 536 | "overflow was not set to -1"); |
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 | /* Same again, with num = LONG_MIN - 1 */ |
| 539 | num = PyLong_FromLong(LONG_MIN); |
| 540 | if (num == NULL) |
| 541 | return NULL; |
| 542 | one = PyLong_FromLong(1L); |
| 543 | if (one == NULL) { |
| 544 | Py_DECREF(num); |
| 545 | return NULL; |
| 546 | } |
| 547 | temp = PyNumber_Subtract(num, one); |
| 548 | Py_DECREF(one); |
| 549 | Py_DECREF(num); |
| 550 | num = temp; |
| 551 | if (num == NULL) |
| 552 | return NULL; |
| 553 | overflow = 0; |
| 554 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 555 | Py_DECREF(num); |
| 556 | if (value == -1 && PyErr_Occurred()) |
| 557 | return NULL; |
| 558 | if (value != -1) |
| 559 | return raiseTestError("test_long_and_overflow", |
| 560 | "return value was not set to -1"); |
| 561 | if (overflow != -1) |
| 562 | return raiseTestError("test_long_and_overflow", |
| 563 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | /* Test that overflow is cleared properly for small values. */ |
| 566 | num = PyLong_FromString("FF", NULL, 16); |
| 567 | if (num == NULL) |
| 568 | return NULL; |
| 569 | overflow = 1234; |
| 570 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 571 | Py_DECREF(num); |
| 572 | if (value == -1 && PyErr_Occurred()) |
| 573 | return NULL; |
| 574 | if (value != 0xFF) |
| 575 | return raiseTestError("test_long_and_overflow", |
| 576 | "expected return value 0xFF"); |
| 577 | if (overflow != 0) |
| 578 | return raiseTestError("test_long_and_overflow", |
| 579 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | num = PyLong_FromString("-FF", NULL, 16); |
| 582 | if (num == NULL) |
| 583 | return NULL; |
| 584 | overflow = 0; |
| 585 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 586 | Py_DECREF(num); |
| 587 | if (value == -1 && PyErr_Occurred()) |
| 588 | return NULL; |
| 589 | if (value != -0xFF) |
| 590 | return raiseTestError("test_long_and_overflow", |
| 591 | "expected return value 0xFF"); |
| 592 | if (overflow != 0) |
| 593 | return raiseTestError("test_long_and_overflow", |
| 594 | "overflow was set incorrectly"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | num = PyLong_FromLong(LONG_MAX); |
| 597 | if (num == NULL) |
| 598 | return NULL; |
| 599 | overflow = 1234; |
| 600 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 601 | Py_DECREF(num); |
| 602 | if (value == -1 && PyErr_Occurred()) |
| 603 | return NULL; |
| 604 | if (value != LONG_MAX) |
| 605 | return raiseTestError("test_long_and_overflow", |
| 606 | "expected return value LONG_MAX"); |
| 607 | if (overflow != 0) |
| 608 | return raiseTestError("test_long_and_overflow", |
| 609 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | num = PyLong_FromLong(LONG_MIN); |
| 612 | if (num == NULL) |
| 613 | return NULL; |
| 614 | overflow = 0; |
| 615 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 616 | Py_DECREF(num); |
| 617 | if (value == -1 && PyErr_Occurred()) |
| 618 | return NULL; |
| 619 | if (value != LONG_MIN) |
| 620 | return raiseTestError("test_long_and_overflow", |
| 621 | "expected return value LONG_MIN"); |
| 622 | if (overflow != 0) |
| 623 | return raiseTestError("test_long_and_overflow", |
| 624 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 625 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 626 | Py_RETURN_NONE; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 629 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 630 | long long is tested by test_long_api_inner. This test will |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 631 | concentrate on proper handling of overflow. |
| 632 | */ |
| 633 | |
| 634 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 635 | test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 636 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | PyObject *num, *one, *temp; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 638 | long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | int overflow; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | /* Test that overflow is set properly for a large value. */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 642 | /* num is a number larger than LLONG_MAX on a typical machine. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 644 | if (num == NULL) |
| 645 | return NULL; |
| 646 | overflow = 1234; |
| 647 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 648 | Py_DECREF(num); |
| 649 | if (value == -1 && PyErr_Occurred()) |
| 650 | return NULL; |
| 651 | if (value != -1) |
| 652 | return raiseTestError("test_long_long_and_overflow", |
| 653 | "return value was not set to -1"); |
| 654 | if (overflow != 1) |
| 655 | return raiseTestError("test_long_long_and_overflow", |
| 656 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 657 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 658 | /* Same again, with num = LLONG_MAX + 1 */ |
| 659 | num = PyLong_FromLongLong(LLONG_MAX); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | if (num == NULL) |
| 661 | return NULL; |
| 662 | one = PyLong_FromLong(1L); |
| 663 | if (one == NULL) { |
| 664 | Py_DECREF(num); |
| 665 | return NULL; |
| 666 | } |
| 667 | temp = PyNumber_Add(num, one); |
| 668 | Py_DECREF(one); |
| 669 | Py_DECREF(num); |
| 670 | num = temp; |
| 671 | if (num == NULL) |
| 672 | return NULL; |
| 673 | overflow = 0; |
| 674 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 675 | Py_DECREF(num); |
| 676 | if (value == -1 && PyErr_Occurred()) |
| 677 | return NULL; |
| 678 | if (value != -1) |
| 679 | return raiseTestError("test_long_long_and_overflow", |
| 680 | "return value was not set to -1"); |
| 681 | if (overflow != 1) |
| 682 | return raiseTestError("test_long_long_and_overflow", |
| 683 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | /* Test that overflow is set properly for a large negative value. */ |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 686 | /* num is a number smaller than LLONG_MIN on a typical platform */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 688 | if (num == NULL) |
| 689 | return NULL; |
| 690 | overflow = 1234; |
| 691 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 692 | Py_DECREF(num); |
| 693 | if (value == -1 && PyErr_Occurred()) |
| 694 | return NULL; |
| 695 | if (value != -1) |
| 696 | return raiseTestError("test_long_long_and_overflow", |
| 697 | "return value was not set to -1"); |
| 698 | if (overflow != -1) |
| 699 | return raiseTestError("test_long_long_and_overflow", |
| 700 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 701 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 702 | /* Same again, with num = LLONG_MIN - 1 */ |
| 703 | num = PyLong_FromLongLong(LLONG_MIN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | if (num == NULL) |
| 705 | return NULL; |
| 706 | one = PyLong_FromLong(1L); |
| 707 | if (one == NULL) { |
| 708 | Py_DECREF(num); |
| 709 | return NULL; |
| 710 | } |
| 711 | temp = PyNumber_Subtract(num, one); |
| 712 | Py_DECREF(one); |
| 713 | Py_DECREF(num); |
| 714 | num = temp; |
| 715 | if (num == NULL) |
| 716 | return NULL; |
| 717 | overflow = 0; |
| 718 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 719 | Py_DECREF(num); |
| 720 | if (value == -1 && PyErr_Occurred()) |
| 721 | return NULL; |
| 722 | if (value != -1) |
| 723 | return raiseTestError("test_long_long_and_overflow", |
| 724 | "return value was not set to -1"); |
| 725 | if (overflow != -1) |
| 726 | return raiseTestError("test_long_long_and_overflow", |
| 727 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | /* Test that overflow is cleared properly for small values. */ |
| 730 | num = PyLong_FromString("FF", NULL, 16); |
| 731 | if (num == NULL) |
| 732 | return NULL; |
| 733 | overflow = 1234; |
| 734 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 735 | Py_DECREF(num); |
| 736 | if (value == -1 && PyErr_Occurred()) |
| 737 | return NULL; |
| 738 | if (value != 0xFF) |
| 739 | return raiseTestError("test_long_long_and_overflow", |
| 740 | "expected return value 0xFF"); |
| 741 | if (overflow != 0) |
| 742 | return raiseTestError("test_long_long_and_overflow", |
| 743 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | num = PyLong_FromString("-FF", NULL, 16); |
| 746 | if (num == NULL) |
| 747 | return NULL; |
| 748 | overflow = 0; |
| 749 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 750 | Py_DECREF(num); |
| 751 | if (value == -1 && PyErr_Occurred()) |
| 752 | return NULL; |
| 753 | if (value != -0xFF) |
| 754 | return raiseTestError("test_long_long_and_overflow", |
| 755 | "expected return value 0xFF"); |
| 756 | if (overflow != 0) |
| 757 | return raiseTestError("test_long_long_and_overflow", |
| 758 | "overflow was set incorrectly"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 759 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 760 | num = PyLong_FromLongLong(LLONG_MAX); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | if (num == NULL) |
| 762 | return NULL; |
| 763 | overflow = 1234; |
| 764 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 765 | Py_DECREF(num); |
| 766 | if (value == -1 && PyErr_Occurred()) |
| 767 | return NULL; |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 768 | if (value != LLONG_MAX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | return raiseTestError("test_long_long_and_overflow", |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 770 | "expected return value LLONG_MAX"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | if (overflow != 0) |
| 772 | return raiseTestError("test_long_long_and_overflow", |
| 773 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 774 | |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 775 | num = PyLong_FromLongLong(LLONG_MIN); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 776 | if (num == NULL) |
| 777 | return NULL; |
| 778 | overflow = 0; |
| 779 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 780 | Py_DECREF(num); |
| 781 | if (value == -1 && PyErr_Occurred()) |
| 782 | return NULL; |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 783 | if (value != LLONG_MIN) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | return raiseTestError("test_long_long_and_overflow", |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 785 | "expected return value LLONG_MIN"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | if (overflow != 0) |
| 787 | return raiseTestError("test_long_long_and_overflow", |
| 788 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 789 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 790 | Py_RETURN_NONE; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 793 | /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that |
| 794 | non-integer arguments are handled correctly. It should be extended to |
| 795 | test overflow handling. |
| 796 | */ |
| 797 | |
| 798 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 799 | test_long_as_size_t(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 800 | { |
| 801 | size_t out_u; |
| 802 | Py_ssize_t out_s; |
| 803 | |
| 804 | Py_INCREF(Py_None); |
| 805 | |
| 806 | out_u = PyLong_AsSize_t(Py_None); |
| 807 | if (out_u != (size_t)-1 || !PyErr_Occurred()) |
| 808 | return raiseTestError("test_long_as_size_t", |
| 809 | "PyLong_AsSize_t(None) didn't complain"); |
| 810 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 811 | return raiseTestError("test_long_as_size_t", |
| 812 | "PyLong_AsSize_t(None) raised " |
| 813 | "something other than TypeError"); |
| 814 | PyErr_Clear(); |
| 815 | |
| 816 | out_s = PyLong_AsSsize_t(Py_None); |
| 817 | if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) |
| 818 | return raiseTestError("test_long_as_size_t", |
| 819 | "PyLong_AsSsize_t(None) didn't complain"); |
| 820 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 821 | return raiseTestError("test_long_as_size_t", |
| 822 | "PyLong_AsSsize_t(None) raised " |
| 823 | "something other than TypeError"); |
| 824 | PyErr_Clear(); |
| 825 | |
| 826 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 827 | return Py_None; |
| 828 | } |
| 829 | |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 830 | static PyObject * |
| 831 | test_long_as_unsigned_long_long_mask(PyObject *self, |
| 832 | PyObject *Py_UNUSED(ignored)) |
| 833 | { |
| 834 | unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL); |
| 835 | |
| 836 | if (res != (unsigned long long)-1 || !PyErr_Occurred()) { |
| 837 | return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 838 | "PyLong_AsUnsignedLongLongMask(NULL) didn't " |
| 839 | "complain"); |
| 840 | } |
| 841 | if (!PyErr_ExceptionMatches(PyExc_SystemError)) { |
| 842 | return raiseTestError("test_long_as_unsigned_long_long_mask", |
| 843 | "PyLong_AsUnsignedLongLongMask(NULL) raised " |
| 844 | "something other than SystemError"); |
| 845 | } |
| 846 | PyErr_Clear(); |
| 847 | Py_RETURN_NONE; |
| 848 | } |
| 849 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 850 | /* Test the PyLong_AsDouble API. At present this just tests that |
| 851 | non-integer arguments are handled correctly. |
| 852 | */ |
| 853 | |
| 854 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 855 | test_long_as_double(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 856 | { |
| 857 | double out; |
| 858 | |
| 859 | Py_INCREF(Py_None); |
| 860 | |
| 861 | out = PyLong_AsDouble(Py_None); |
| 862 | if (out != -1.0 || !PyErr_Occurred()) |
| 863 | return raiseTestError("test_long_as_double", |
| 864 | "PyLong_AsDouble(None) didn't complain"); |
| 865 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 866 | return raiseTestError("test_long_as_double", |
| 867 | "PyLong_AsDouble(None) raised " |
| 868 | "something other than TypeError"); |
| 869 | PyErr_Clear(); |
| 870 | |
| 871 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 872 | return Py_None; |
| 873 | } |
| 874 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 875 | /* 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] | 876 | for both long and int arguments. The test may leak a little memory if |
| 877 | it fails. |
| 878 | */ |
| 879 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 880 | test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 881 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | PyObject *tuple, *num; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 883 | long long value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | tuple = PyTuple_New(1); |
| 886 | if (tuple == NULL) |
| 887 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | num = PyLong_FromLong(42); |
| 890 | if (num == NULL) |
| 891 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | value = -1; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 896 | if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 898 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | if (value != 42) |
| 900 | return raiseTestError("test_L_code", |
| 901 | "L code returned wrong value for long 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | Py_DECREF(num); |
| 904 | num = PyLong_FromLong(42); |
| 905 | if (num == NULL) |
| 906 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | value = -1; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 911 | if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 913 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | if (value != 42) |
| 915 | return raiseTestError("test_L_code", |
| 916 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 919 | Py_RETURN_NONE; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 922 | static PyObject * |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 923 | return_none(void *unused) |
| 924 | { |
| 925 | Py_RETURN_NONE; |
| 926 | } |
| 927 | |
| 928 | static PyObject * |
| 929 | raise_error(void *unused) |
| 930 | { |
| 931 | PyErr_SetNone(PyExc_ValueError); |
| 932 | return NULL; |
| 933 | } |
| 934 | |
| 935 | static int |
| 936 | test_buildvalue_N_error(const char *fmt) |
| 937 | { |
| 938 | PyObject *arg, *res; |
| 939 | |
| 940 | arg = PyList_New(0); |
| 941 | if (arg == NULL) { |
| 942 | return -1; |
| 943 | } |
| 944 | |
| 945 | Py_INCREF(arg); |
| 946 | res = Py_BuildValue(fmt, return_none, NULL, arg); |
| 947 | if (res == NULL) { |
| 948 | return -1; |
| 949 | } |
| 950 | Py_DECREF(res); |
| 951 | if (Py_REFCNT(arg) != 1) { |
| 952 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 953 | "arg was not decrefed in successful " |
| 954 | "Py_BuildValue(\"%s\")", fmt); |
| 955 | return -1; |
| 956 | } |
| 957 | |
| 958 | Py_INCREF(arg); |
| 959 | res = Py_BuildValue(fmt, raise_error, NULL, arg); |
| 960 | if (res != NULL || !PyErr_Occurred()) { |
| 961 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 962 | "Py_BuildValue(\"%s\") didn't complain", fmt); |
| 963 | return -1; |
| 964 | } |
| 965 | PyErr_Clear(); |
| 966 | if (Py_REFCNT(arg) != 1) { |
| 967 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 968 | "arg was not decrefed in failed " |
| 969 | "Py_BuildValue(\"%s\")", fmt); |
| 970 | return -1; |
| 971 | } |
| 972 | Py_DECREF(arg); |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 977 | test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 978 | { |
| 979 | PyObject *arg, *res; |
| 980 | |
| 981 | arg = PyList_New(0); |
| 982 | if (arg == NULL) { |
| 983 | return NULL; |
| 984 | } |
| 985 | Py_INCREF(arg); |
| 986 | res = Py_BuildValue("N", arg); |
| 987 | if (res == NULL) { |
| 988 | return NULL; |
| 989 | } |
| 990 | if (res != arg) { |
| 991 | return raiseTestError("test_buildvalue_N", |
| 992 | "Py_BuildValue(\"N\") returned wrong result"); |
| 993 | } |
| 994 | if (Py_REFCNT(arg) != 2) { |
| 995 | return raiseTestError("test_buildvalue_N", |
| 996 | "arg was not decrefed in Py_BuildValue(\"N\")"); |
| 997 | } |
| 998 | Py_DECREF(res); |
| 999 | Py_DECREF(arg); |
| 1000 | |
| 1001 | if (test_buildvalue_N_error("O&N") < 0) |
| 1002 | return NULL; |
| 1003 | if (test_buildvalue_N_error("(O&N)") < 0) |
| 1004 | return NULL; |
| 1005 | if (test_buildvalue_N_error("[O&N]") < 0) |
| 1006 | return NULL; |
| 1007 | if (test_buildvalue_N_error("{O&N}") < 0) |
| 1008 | return NULL; |
| 1009 | if (test_buildvalue_N_error("{()O&(())N}") < 0) |
| 1010 | return NULL; |
| 1011 | |
| 1012 | Py_RETURN_NONE; |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | static PyObject * |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 1017 | get_args(PyObject *self, PyObject *args) |
| 1018 | { |
| 1019 | if (args == NULL) { |
| 1020 | args = Py_None; |
| 1021 | } |
| 1022 | Py_INCREF(args); |
| 1023 | return args; |
| 1024 | } |
| 1025 | |
| 1026 | static PyObject * |
| 1027 | get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1028 | { |
| 1029 | if (kwargs == NULL) { |
| 1030 | kwargs = Py_None; |
| 1031 | } |
| 1032 | Py_INCREF(kwargs); |
| 1033 | return kwargs; |
| 1034 | } |
| 1035 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1036 | /* Test tuple argument processing */ |
| 1037 | static PyObject * |
| 1038 | getargs_tuple(PyObject *self, PyObject *args) |
| 1039 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | int a, b, c; |
| 1041 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 1042 | return NULL; |
| 1043 | return Py_BuildValue("iii", a, b, c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1046 | /* test PyArg_ParseTupleAndKeywords */ |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1047 | static PyObject * |
| 1048 | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1051 | static const char fmt[] = "(ii)i|(i(ii))(iii)i"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | 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] | 1053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 1055 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 1056 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 1057 | return NULL; |
| 1058 | return Py_BuildValue("iiiiiiiiii", |
| 1059 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 1060 | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]); |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1063 | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
| 1064 | static PyObject * |
| 1065 | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1066 | { |
| 1067 | static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
| 1068 | int required = -1; |
| 1069 | int optional = -1; |
| 1070 | int keyword_only = -1; |
| 1071 | |
| 1072 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
| 1073 | &required, &optional, &keyword_only)) |
| 1074 | return NULL; |
| 1075 | return Py_BuildValue("iii", required, optional, keyword_only); |
| 1076 | } |
| 1077 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1078 | /* test PyArg_ParseTupleAndKeywords positional-only arguments */ |
| 1079 | static PyObject * |
| 1080 | getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1081 | { |
| 1082 | static char *keywords[] = {"", "", "keyword", NULL}; |
| 1083 | int required = -1; |
| 1084 | int optional = -1; |
| 1085 | int keyword = -1; |
| 1086 | |
| 1087 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords, |
| 1088 | &required, &optional, &keyword)) |
| 1089 | return NULL; |
| 1090 | return Py_BuildValue("iii", required, optional, keyword); |
| 1091 | } |
| 1092 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1093 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 1094 | and return the result. |
| 1095 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1096 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1097 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1098 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1099 | unsigned char value; |
| 1100 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 1101 | return NULL; |
| 1102 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1105 | static PyObject * |
| 1106 | getargs_B(PyObject *self, PyObject *args) |
| 1107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | unsigned char value; |
| 1109 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 1110 | return NULL; |
| 1111 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1115 | getargs_h(PyObject *self, PyObject *args) |
| 1116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | short value; |
| 1118 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 1119 | return NULL; |
| 1120 | return PyLong_FromLong((long)value); |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1124 | getargs_H(PyObject *self, PyObject *args) |
| 1125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | unsigned short value; |
| 1127 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 1128 | return NULL; |
| 1129 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | static PyObject * |
| 1133 | getargs_I(PyObject *self, PyObject *args) |
| 1134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | unsigned int value; |
| 1136 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 1137 | return NULL; |
| 1138 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static PyObject * |
| 1142 | getargs_k(PyObject *self, PyObject *args) |
| 1143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | unsigned long value; |
| 1145 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 1146 | return NULL; |
| 1147 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | static PyObject * |
| 1151 | getargs_i(PyObject *self, PyObject *args) |
| 1152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | int value; |
| 1154 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 1155 | return NULL; |
| 1156 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1159 | static PyObject * |
| 1160 | getargs_l(PyObject *self, PyObject *args) |
| 1161 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | long value; |
| 1163 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 1164 | return NULL; |
| 1165 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1168 | static PyObject * |
| 1169 | getargs_n(PyObject *self, PyObject *args) |
| 1170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | Py_ssize_t value; |
| 1172 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 1173 | return NULL; |
| 1174 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 1177 | static PyObject * |
| 1178 | getargs_p(PyObject *self, PyObject *args) |
| 1179 | { |
| 1180 | int value; |
| 1181 | if (!PyArg_ParseTuple(args, "p", &value)) |
| 1182 | return NULL; |
| 1183 | return PyLong_FromLong(value); |
| 1184 | } |
| 1185 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1186 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1187 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1188 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1189 | long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 1191 | return NULL; |
| 1192 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1195 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1196 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1197 | { |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1198 | unsigned long long value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 1200 | return NULL; |
| 1201 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1202 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1203 | |
| 1204 | /* This function not only tests the 'k' getargs code, but also the |
orenmn | 698845e | 2017-03-02 13:29:20 +0200 | [diff] [blame] | 1205 | PyLong_AsUnsignedLongMask() function. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1206 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1207 | test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | PyObject *tuple, *num; |
| 1210 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | tuple = PyTuple_New(1); |
| 1213 | if (tuple == NULL) |
| 1214 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 1217 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 1218 | if (num == NULL) |
| 1219 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | value = PyLong_AsUnsignedLongMask(num); |
| 1222 | if (value != ULONG_MAX) |
| 1223 | return raiseTestError("test_k_code", |
Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 1224 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1229 | if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1231 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | if (value != ULONG_MAX) |
| 1233 | return raiseTestError("test_k_code", |
| 1234 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | Py_DECREF(num); |
| 1237 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 1238 | if (num == NULL) |
| 1239 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | value = PyLong_AsUnsignedLongMask(num); |
| 1242 | if (value != (unsigned long)-0x42) |
| 1243 | return raiseTestError("test_k_code", |
orenmn | 698845e | 2017-03-02 13:29:20 +0200 | [diff] [blame] | 1244 | "PyLong_AsUnsignedLongMask() returned wrong " |
| 1245 | "value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1250 | if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1252 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | if (value != (unsigned long)-0x42) |
| 1254 | return raiseTestError("test_k_code", |
| 1255 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1258 | Py_RETURN_NONE; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1261 | static PyObject * |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 1262 | getargs_f(PyObject *self, PyObject *args) |
| 1263 | { |
| 1264 | float f; |
| 1265 | if (!PyArg_ParseTuple(args, "f", &f)) |
| 1266 | return NULL; |
| 1267 | return PyFloat_FromDouble(f); |
| 1268 | } |
| 1269 | |
| 1270 | static PyObject * |
| 1271 | getargs_d(PyObject *self, PyObject *args) |
| 1272 | { |
| 1273 | double d; |
| 1274 | if (!PyArg_ParseTuple(args, "d", &d)) |
| 1275 | return NULL; |
| 1276 | return PyFloat_FromDouble(d); |
| 1277 | } |
| 1278 | |
| 1279 | static PyObject * |
| 1280 | getargs_D(PyObject *self, PyObject *args) |
| 1281 | { |
| 1282 | Py_complex cval; |
| 1283 | if (!PyArg_ParseTuple(args, "D", &cval)) |
| 1284 | return NULL; |
| 1285 | return PyComplex_FromCComplex(cval); |
| 1286 | } |
| 1287 | |
| 1288 | static PyObject * |
| 1289 | getargs_S(PyObject *self, PyObject *args) |
| 1290 | { |
| 1291 | PyObject *obj; |
| 1292 | if (!PyArg_ParseTuple(args, "S", &obj)) |
| 1293 | return NULL; |
| 1294 | Py_INCREF(obj); |
| 1295 | return obj; |
| 1296 | } |
| 1297 | |
| 1298 | static PyObject * |
| 1299 | getargs_Y(PyObject *self, PyObject *args) |
| 1300 | { |
| 1301 | PyObject *obj; |
| 1302 | if (!PyArg_ParseTuple(args, "Y", &obj)) |
| 1303 | return NULL; |
| 1304 | Py_INCREF(obj); |
| 1305 | return obj; |
| 1306 | } |
| 1307 | |
| 1308 | static PyObject * |
| 1309 | getargs_U(PyObject *self, PyObject *args) |
| 1310 | { |
| 1311 | PyObject *obj; |
| 1312 | if (!PyArg_ParseTuple(args, "U", &obj)) |
| 1313 | return NULL; |
| 1314 | Py_INCREF(obj); |
| 1315 | return obj; |
| 1316 | } |
| 1317 | |
| 1318 | static PyObject * |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1319 | getargs_c(PyObject *self, PyObject *args) |
| 1320 | { |
| 1321 | char c; |
| 1322 | if (!PyArg_ParseTuple(args, "c", &c)) |
| 1323 | return NULL; |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1324 | return PyLong_FromLong((unsigned char)c); |
| 1325 | } |
| 1326 | |
| 1327 | static PyObject * |
| 1328 | getargs_C(PyObject *self, PyObject *args) |
| 1329 | { |
| 1330 | int c; |
| 1331 | if (!PyArg_ParseTuple(args, "C", &c)) |
| 1332 | return NULL; |
| 1333 | return PyLong_FromLong(c); |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static PyObject * |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1337 | getargs_s(PyObject *self, PyObject *args) |
| 1338 | { |
| 1339 | char *str; |
| 1340 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1341 | return NULL; |
| 1342 | return PyBytes_FromString(str); |
| 1343 | } |
| 1344 | |
| 1345 | static PyObject * |
| 1346 | getargs_s_star(PyObject *self, PyObject *args) |
| 1347 | { |
| 1348 | Py_buffer buffer; |
| 1349 | PyObject *bytes; |
| 1350 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1351 | return NULL; |
| 1352 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1353 | PyBuffer_Release(&buffer); |
| 1354 | return bytes; |
| 1355 | } |
| 1356 | |
| 1357 | static PyObject * |
| 1358 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1359 | { |
| 1360 | char *str; |
| 1361 | Py_ssize_t size; |
| 1362 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1363 | return NULL; |
| 1364 | return PyBytes_FromStringAndSize(str, size); |
| 1365 | } |
| 1366 | |
| 1367 | static PyObject * |
| 1368 | getargs_z(PyObject *self, PyObject *args) |
| 1369 | { |
| 1370 | char *str; |
| 1371 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1372 | return NULL; |
| 1373 | if (str != NULL) |
| 1374 | return PyBytes_FromString(str); |
| 1375 | else |
| 1376 | Py_RETURN_NONE; |
| 1377 | } |
| 1378 | |
| 1379 | static PyObject * |
| 1380 | getargs_z_star(PyObject *self, PyObject *args) |
| 1381 | { |
| 1382 | Py_buffer buffer; |
| 1383 | PyObject *bytes; |
| 1384 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1385 | return NULL; |
| 1386 | if (buffer.buf != NULL) |
| 1387 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1388 | else { |
| 1389 | Py_INCREF(Py_None); |
| 1390 | bytes = Py_None; |
| 1391 | } |
| 1392 | PyBuffer_Release(&buffer); |
| 1393 | return bytes; |
| 1394 | } |
| 1395 | |
| 1396 | static PyObject * |
| 1397 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1398 | { |
| 1399 | char *str; |
| 1400 | Py_ssize_t size; |
| 1401 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1402 | return NULL; |
| 1403 | if (str != NULL) |
| 1404 | return PyBytes_FromStringAndSize(str, size); |
| 1405 | else |
| 1406 | Py_RETURN_NONE; |
| 1407 | } |
| 1408 | |
| 1409 | static PyObject * |
| 1410 | getargs_y(PyObject *self, PyObject *args) |
| 1411 | { |
| 1412 | char *str; |
| 1413 | if (!PyArg_ParseTuple(args, "y", &str)) |
| 1414 | return NULL; |
| 1415 | return PyBytes_FromString(str); |
| 1416 | } |
| 1417 | |
| 1418 | static PyObject * |
| 1419 | getargs_y_star(PyObject *self, PyObject *args) |
| 1420 | { |
| 1421 | Py_buffer buffer; |
| 1422 | PyObject *bytes; |
| 1423 | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 1424 | return NULL; |
| 1425 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1426 | PyBuffer_Release(&buffer); |
| 1427 | return bytes; |
| 1428 | } |
| 1429 | |
| 1430 | static PyObject * |
| 1431 | getargs_y_hash(PyObject *self, PyObject *args) |
| 1432 | { |
| 1433 | char *str; |
| 1434 | Py_ssize_t size; |
| 1435 | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
| 1436 | return NULL; |
| 1437 | return PyBytes_FromStringAndSize(str, size); |
| 1438 | } |
| 1439 | |
| 1440 | static PyObject * |
| 1441 | getargs_u(PyObject *self, PyObject *args) |
| 1442 | { |
| 1443 | Py_UNICODE *str; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1444 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1445 | return NULL; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1446 | return PyUnicode_FromWideChar(str, -1); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | static PyObject * |
| 1450 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1451 | { |
| 1452 | Py_UNICODE *str; |
| 1453 | Py_ssize_t size; |
| 1454 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1455 | return NULL; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1456 | return PyUnicode_FromWideChar(str, size); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static PyObject * |
| 1460 | getargs_Z(PyObject *self, PyObject *args) |
| 1461 | { |
| 1462 | Py_UNICODE *str; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1463 | if (!PyArg_ParseTuple(args, "Z", &str)) |
| 1464 | return NULL; |
| 1465 | if (str != NULL) { |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1466 | return PyUnicode_FromWideChar(str, -1); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1467 | } else |
| 1468 | Py_RETURN_NONE; |
| 1469 | } |
| 1470 | |
| 1471 | static PyObject * |
| 1472 | getargs_Z_hash(PyObject *self, PyObject *args) |
| 1473 | { |
| 1474 | Py_UNICODE *str; |
| 1475 | Py_ssize_t size; |
| 1476 | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
| 1477 | return NULL; |
| 1478 | if (str != NULL) |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1479 | return PyUnicode_FromWideChar(str, size); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1480 | else |
| 1481 | Py_RETURN_NONE; |
| 1482 | } |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1483 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1484 | static PyObject * |
| 1485 | getargs_es(PyObject *self, PyObject *args) |
| 1486 | { |
| 1487 | PyObject *arg, *result; |
| 1488 | const char *encoding = NULL; |
| 1489 | char *str; |
| 1490 | |
| 1491 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1492 | return NULL; |
| 1493 | if (!PyArg_Parse(arg, "es", encoding, &str)) |
| 1494 | return NULL; |
| 1495 | result = PyBytes_FromString(str); |
| 1496 | PyMem_Free(str); |
| 1497 | return result; |
| 1498 | } |
| 1499 | |
| 1500 | static PyObject * |
| 1501 | getargs_et(PyObject *self, PyObject *args) |
| 1502 | { |
| 1503 | PyObject *arg, *result; |
| 1504 | const char *encoding = NULL; |
| 1505 | char *str; |
| 1506 | |
| 1507 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1508 | return NULL; |
| 1509 | if (!PyArg_Parse(arg, "et", encoding, &str)) |
| 1510 | return NULL; |
| 1511 | result = PyBytes_FromString(str); |
| 1512 | PyMem_Free(str); |
| 1513 | return result; |
| 1514 | } |
| 1515 | |
| 1516 | static PyObject * |
| 1517 | getargs_es_hash(PyObject *self, PyObject *args) |
| 1518 | { |
| 1519 | PyObject *arg, *result; |
| 1520 | const char *encoding = NULL; |
| 1521 | PyByteArrayObject *buffer = NULL; |
| 1522 | char *str = NULL; |
| 1523 | Py_ssize_t size; |
| 1524 | |
| 1525 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1526 | return NULL; |
| 1527 | if (buffer != NULL) { |
| 1528 | str = PyByteArray_AS_STRING(buffer); |
| 1529 | size = PyByteArray_GET_SIZE(buffer); |
| 1530 | } |
| 1531 | if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) |
| 1532 | return NULL; |
| 1533 | result = PyBytes_FromStringAndSize(str, size); |
| 1534 | if (buffer == NULL) |
| 1535 | PyMem_Free(str); |
| 1536 | return result; |
| 1537 | } |
| 1538 | |
| 1539 | static PyObject * |
| 1540 | getargs_et_hash(PyObject *self, PyObject *args) |
| 1541 | { |
| 1542 | PyObject *arg, *result; |
| 1543 | const char *encoding = NULL; |
| 1544 | PyByteArrayObject *buffer = NULL; |
| 1545 | char *str = NULL; |
| 1546 | Py_ssize_t size; |
| 1547 | |
| 1548 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1549 | return NULL; |
| 1550 | if (buffer != NULL) { |
| 1551 | str = PyByteArray_AS_STRING(buffer); |
| 1552 | size = PyByteArray_GET_SIZE(buffer); |
| 1553 | } |
| 1554 | if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) |
| 1555 | return NULL; |
| 1556 | result = PyBytes_FromStringAndSize(str, size); |
| 1557 | if (buffer == NULL) |
| 1558 | PyMem_Free(str); |
| 1559 | return result; |
| 1560 | } |
| 1561 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1562 | /* Test the s and z codes for PyArg_ParseTuple. |
| 1563 | */ |
| 1564 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1565 | test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1566 | { |
| 1567 | /* Unicode strings should be accepted */ |
| 1568 | PyObject *tuple, *obj; |
| 1569 | char *value; |
| 1570 | |
| 1571 | tuple = PyTuple_New(1); |
| 1572 | if (tuple == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1574 | |
| 1575 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1577 | if (obj == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1578 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1579 | |
| 1580 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1581 | |
| 1582 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1584 | */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1585 | if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) { |
| 1586 | return NULL; |
| 1587 | } |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1588 | |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1589 | if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) { |
| 1590 | return NULL; |
| 1591 | } |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1592 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1593 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1594 | Py_RETURN_NONE; |
| 1595 | } |
| 1596 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1597 | static PyObject * |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1598 | parse_tuple_and_keywords(PyObject *self, PyObject *args) |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1599 | { |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1600 | PyObject *sub_args; |
| 1601 | PyObject *sub_kwargs; |
Serhiy Storchaka | 5f161fd | 2017-05-04 00:03:23 +0300 | [diff] [blame] | 1602 | const char *sub_format; |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1603 | PyObject *sub_keywords; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1604 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1605 | Py_ssize_t i, size; |
| 1606 | char *keywords[8 + 1]; /* space for NULL at end */ |
| 1607 | PyObject *o; |
| 1608 | PyObject *converted[8]; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1609 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1610 | int result; |
| 1611 | PyObject *return_value = NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1612 | |
Larry Hastings | 22701e8 | 2012-08-08 14:52:22 -0700 | [diff] [blame] | 1613 | double buffers[8][4]; /* double ensures alignment where necessary */ |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1614 | |
Serhiy Storchaka | 5f161fd | 2017-05-04 00:03:23 +0300 | [diff] [blame] | 1615 | if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords", |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1616 | &sub_args, &sub_kwargs, |
| 1617 | &sub_format, &sub_keywords)) |
| 1618 | return NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1619 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1620 | if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
| 1621 | PyErr_SetString(PyExc_ValueError, |
| 1622 | "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
| 1623 | return NULL; |
| 1624 | } |
| 1625 | |
| 1626 | memset(buffers, 0, sizeof(buffers)); |
| 1627 | memset(converted, 0, sizeof(converted)); |
| 1628 | memset(keywords, 0, sizeof(keywords)); |
| 1629 | |
| 1630 | size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 1631 | if (size > 8) { |
| 1632 | PyErr_SetString(PyExc_ValueError, |
| 1633 | "parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 1634 | goto exit; |
| 1635 | } |
| 1636 | |
| 1637 | for (i = 0; i < size; i++) { |
| 1638 | o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 1639 | if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { |
| 1640 | PyErr_Format(PyExc_ValueError, |
Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 1641 | "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] | 1642 | goto exit; |
| 1643 | } |
| 1644 | keywords[i] = PyBytes_AS_STRING(converted[i]); |
| 1645 | } |
| 1646 | |
| 1647 | result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 1648 | sub_format, keywords, |
| 1649 | buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 1650 | buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 1651 | |
| 1652 | if (result) { |
| 1653 | return_value = Py_None; |
| 1654 | Py_INCREF(Py_None); |
| 1655 | } |
| 1656 | |
| 1657 | exit: |
| 1658 | size = sizeof(converted) / sizeof(converted[0]); |
| 1659 | for (i = 0; i < size; i++) { |
| 1660 | Py_XDECREF(converted[i]); |
| 1661 | } |
| 1662 | return return_value; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1665 | static volatile int x; |
| 1666 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1667 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1668 | of an error. |
| 1669 | */ |
| 1670 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1671 | test_u_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | PyObject *tuple, *obj; |
| 1674 | Py_UNICODE *value; |
| 1675 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1678 | /* Just use the macro and check that it compiles */ |
| 1679 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1681 | tuple = PyTuple_New(1); |
| 1682 | if (tuple == NULL) |
| 1683 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1686 | "ascii", NULL); |
| 1687 | if (obj == NULL) |
| 1688 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1692 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1693 | if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1695 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1697 | return raiseTestError("test_u_code", |
| 1698 | "u code returned wrong value for u'test'"); |
| 1699 | value = 0; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1700 | if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1702 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1704 | len != PyUnicode_GET_SIZE(obj)) |
| 1705 | return raiseTestError("test_u_code", |
| 1706 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1708 | Py_DECREF(tuple); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1709 | Py_RETURN_NONE; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1712 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1713 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1714 | test_Z_code(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | PyObject *tuple, *obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1717 | const Py_UNICODE *value1, *value2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | tuple = PyTuple_New(2); |
| 1721 | if (tuple == NULL) |
| 1722 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | obj = PyUnicode_FromString("test"); |
| 1725 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1726 | Py_INCREF(Py_None); |
| 1727 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | /* swap values on purpose */ |
| 1730 | value1 = NULL; |
| 1731 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | /* Test Z for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1734 | if (!PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1736 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1738 | return raiseTestError("test_Z_code", |
| 1739 | "Z code returned wrong value for 'test'"); |
| 1740 | if (value2 != NULL) |
| 1741 | return raiseTestError("test_Z_code", |
| 1742 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | value1 = NULL; |
| 1745 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1746 | len1 = -1; |
| 1747 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1749 | /* Test Z# for both values */ |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1750 | if (!PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1751 | &value2, &len2)) |
| 1752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1753 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 1754 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1755 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1756 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1757 | return raiseTestError("test_Z_code", |
| 1758 | "Z# code returned wrong values for 'test'"); |
| 1759 | if (value2 != NULL || |
| 1760 | len2 != 0) |
| 1761 | return raiseTestError("test_Z_code", |
| 1762 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1764 | Py_DECREF(tuple); |
| 1765 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1768 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 1769 | test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1770 | { |
| 1771 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1773 | size_t wtextlen = 1; |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1774 | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1775 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1777 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1778 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1782 | if (wide == NULL) |
| 1783 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1786 | if (utf8 == NULL) { |
| 1787 | Py_DECREF(wide); |
| 1788 | return NULL; |
| 1789 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1790 | |
Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1791 | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | Py_DECREF(wide); |
| 1793 | Py_DECREF(utf8); |
| 1794 | return raiseTestError("test_widechar", |
| 1795 | "wide string and utf8 string " |
| 1796 | "have different length"); |
| 1797 | } |
| 1798 | if (PyUnicode_Compare(wide, utf8)) { |
| 1799 | Py_DECREF(wide); |
| 1800 | Py_DECREF(utf8); |
| 1801 | if (PyErr_Occurred()) |
| 1802 | return NULL; |
| 1803 | return raiseTestError("test_widechar", |
| 1804 | "wide string and utf8 string " |
| 1805 | "are different"); |
| 1806 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | Py_DECREF(wide); |
| 1809 | Py_DECREF(utf8); |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1810 | |
| 1811 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1812 | wide = PyUnicode_FromWideChar(invalid, 1); |
| 1813 | if (wide == NULL) |
| 1814 | PyErr_Clear(); |
| 1815 | else |
| 1816 | return raiseTestError("test_widechar", |
| 1817 | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
| 1818 | |
| 1819 | wide = PyUnicode_FromUnicode(invalid, 1); |
| 1820 | if (wide == NULL) |
| 1821 | PyErr_Clear(); |
| 1822 | else |
| 1823 | return raiseTestError("test_widechar", |
| 1824 | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1825 | |
| 1826 | wide = PyUnicode_FromUnicode(NULL, 1); |
| 1827 | if (wide == NULL) |
| 1828 | return NULL; |
| 1829 | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1830 | if (_PyUnicode_Ready(wide) < 0) { |
| 1831 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1832 | PyErr_Clear(); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1833 | } |
| 1834 | else { |
| 1835 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1836 | return raiseTestError("test_widechar", |
| 1837 | "PyUnicode_Ready() didn't fail"); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1838 | } |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1839 | #endif |
| 1840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1845 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1846 | { |
| 1847 | PyObject *unicode, *result; |
| 1848 | Py_ssize_t buflen, size; |
| 1849 | wchar_t *buffer; |
| 1850 | |
| 1851 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1852 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1853 | buffer = PyMem_New(wchar_t, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1854 | if (buffer == NULL) |
| 1855 | return PyErr_NoMemory(); |
| 1856 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1857 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1858 | if (size == -1) { |
| 1859 | PyMem_Free(buffer); |
| 1860 | return NULL; |
| 1861 | } |
| 1862 | |
| 1863 | if (size < buflen) |
| 1864 | buflen = size + 1; |
| 1865 | else |
| 1866 | buflen = size; |
| 1867 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 1868 | PyMem_Free(buffer); |
| 1869 | if (result == NULL) |
| 1870 | return NULL; |
| 1871 | |
| 1872 | return Py_BuildValue("(Nn)", result, size); |
| 1873 | } |
| 1874 | |
| 1875 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1876 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1877 | { |
| 1878 | PyObject *unicode, *result; |
| 1879 | Py_ssize_t size; |
| 1880 | wchar_t *buffer; |
| 1881 | |
| 1882 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 1883 | return NULL; |
| 1884 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1885 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1886 | if (buffer == NULL) |
| 1887 | return NULL; |
| 1888 | |
| 1889 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 1890 | PyMem_Free(buffer); |
| 1891 | if (result == NULL) |
| 1892 | return NULL; |
| 1893 | return Py_BuildValue("(Nn)", result, size); |
| 1894 | } |
| 1895 | |
| 1896 | static PyObject * |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 1897 | unicode_asucs4(PyObject *self, PyObject *args) |
| 1898 | { |
| 1899 | PyObject *unicode, *result; |
| 1900 | Py_UCS4 *buffer; |
| 1901 | int copy_null; |
| 1902 | Py_ssize_t str_len, buf_len; |
| 1903 | |
| 1904 | if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, ©_null)) { |
| 1905 | return NULL; |
| 1906 | } |
| 1907 | |
| 1908 | buf_len = str_len + 1; |
| 1909 | buffer = PyMem_NEW(Py_UCS4, buf_len); |
| 1910 | if (buffer == NULL) { |
| 1911 | return PyErr_NoMemory(); |
| 1912 | } |
| 1913 | memset(buffer, 0, sizeof(Py_UCS4)*buf_len); |
| 1914 | buffer[str_len] = 0xffffU; |
| 1915 | |
| 1916 | if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) { |
| 1917 | PyMem_FREE(buffer); |
| 1918 | return NULL; |
| 1919 | } |
| 1920 | |
| 1921 | result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len); |
| 1922 | PyMem_FREE(buffer); |
| 1923 | return result; |
| 1924 | } |
| 1925 | |
| 1926 | static PyObject * |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 1927 | unicode_asutf8(PyObject *self, PyObject *args) |
| 1928 | { |
| 1929 | PyObject *unicode; |
| 1930 | const char *buffer; |
| 1931 | |
| 1932 | if (!PyArg_ParseTuple(args, "U", &unicode)) { |
| 1933 | return NULL; |
| 1934 | } |
| 1935 | |
| 1936 | buffer = PyUnicode_AsUTF8(unicode); |
| 1937 | if (buffer == NULL) { |
| 1938 | return NULL; |
| 1939 | } |
| 1940 | |
| 1941 | return PyBytes_FromString(buffer); |
| 1942 | } |
| 1943 | |
| 1944 | static PyObject * |
| 1945 | unicode_asutf8andsize(PyObject *self, PyObject *args) |
| 1946 | { |
| 1947 | PyObject *unicode, *result; |
| 1948 | const char *buffer; |
| 1949 | Py_ssize_t utf8_len; |
| 1950 | |
| 1951 | if(!PyArg_ParseTuple(args, "U", &unicode)) { |
| 1952 | return NULL; |
| 1953 | } |
| 1954 | |
Victor Stinner | aca8c40 | 2019-09-30 21:14:26 +0200 | [diff] [blame] | 1955 | buffer = PyUnicode_AsUTF8AndSize(unicode, &utf8_len); |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 1956 | if (buffer == NULL) { |
| 1957 | return NULL; |
| 1958 | } |
| 1959 | |
| 1960 | result = PyBytes_FromString(buffer); |
| 1961 | if (result == NULL) { |
| 1962 | return NULL; |
| 1963 | } |
| 1964 | |
| 1965 | return Py_BuildValue("(Nn)", result, utf8_len); |
| 1966 | } |
| 1967 | |
| 1968 | static PyObject * |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 1969 | unicode_findchar(PyObject *self, PyObject *args) |
| 1970 | { |
| 1971 | PyObject *str; |
| 1972 | int direction; |
| 1973 | unsigned int ch; |
| 1974 | Py_ssize_t result; |
| 1975 | Py_ssize_t start, end; |
| 1976 | |
| 1977 | if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch, |
| 1978 | &start, &end, &direction)) { |
| 1979 | return NULL; |
| 1980 | } |
| 1981 | |
| 1982 | result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction); |
| 1983 | if (result == -2) |
| 1984 | return NULL; |
| 1985 | else |
| 1986 | return PyLong_FromSsize_t(result); |
| 1987 | } |
| 1988 | |
| 1989 | static PyObject * |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 1990 | unicode_copycharacters(PyObject *self, PyObject *args) |
| 1991 | { |
| 1992 | PyObject *from, *to, *to_copy; |
| 1993 | Py_ssize_t from_start, to_start, how_many, copied; |
| 1994 | |
| 1995 | if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start, |
| 1996 | &from, &from_start, &how_many)) { |
| 1997 | return NULL; |
| 1998 | } |
| 1999 | |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 2000 | if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to), |
| 2001 | PyUnicode_MAX_CHAR_VALUE(to)))) { |
| 2002 | return NULL; |
| 2003 | } |
| 2004 | if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) { |
| 2005 | Py_DECREF(to_copy); |
| 2006 | return NULL; |
| 2007 | } |
| 2008 | |
| 2009 | if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from, |
| 2010 | from_start, how_many)) < 0) { |
| 2011 | Py_DECREF(to_copy); |
| 2012 | return NULL; |
| 2013 | } |
| 2014 | |
| 2015 | return Py_BuildValue("(Nn)", to_copy, copied); |
| 2016 | } |
| 2017 | |
| 2018 | static PyObject * |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2019 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 2020 | { |
| 2021 | Py_UNICODE *unicode; |
| 2022 | Py_ssize_t length; |
| 2023 | char *errors = NULL; |
| 2024 | PyObject *decimal; |
| 2025 | Py_ssize_t decimal_length, new_length; |
| 2026 | int res; |
| 2027 | |
| 2028 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 2029 | return NULL; |
| 2030 | |
| 2031 | decimal_length = length * 7; /* len('€') */ |
| 2032 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 2033 | if (decimal == NULL) |
| 2034 | return NULL; |
| 2035 | |
| 2036 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 2037 | PyBytes_AS_STRING(decimal), |
| 2038 | errors); |
| 2039 | if (res < 0) { |
| 2040 | Py_DECREF(decimal); |
| 2041 | return NULL; |
| 2042 | } |
| 2043 | |
| 2044 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 2045 | assert(new_length <= decimal_length); |
| 2046 | res = _PyBytes_Resize(&decimal, new_length); |
| 2047 | if (res < 0) |
| 2048 | return NULL; |
| 2049 | |
| 2050 | return decimal; |
| 2051 | } |
| 2052 | |
| 2053 | static PyObject * |
| 2054 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 2055 | { |
| 2056 | Py_UNICODE *unicode; |
| 2057 | Py_ssize_t length; |
| 2058 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 2059 | return NULL; |
| 2060 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 2061 | } |
| 2062 | |
| 2063 | static PyObject * |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 2064 | unicode_legacy_string(PyObject *self, PyObject *args) |
| 2065 | { |
| 2066 | Py_UNICODE *data; |
| 2067 | Py_ssize_t len; |
| 2068 | PyObject *u; |
| 2069 | |
| 2070 | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
| 2071 | return NULL; |
| 2072 | |
| 2073 | u = PyUnicode_FromUnicode(NULL, len); |
| 2074 | if (u == NULL) |
| 2075 | return NULL; |
| 2076 | |
| 2077 | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
| 2078 | |
| 2079 | if (len > 0) { /* The empty string is always ready. */ |
| 2080 | assert(!PyUnicode_IS_READY(u)); |
| 2081 | } |
| 2082 | |
| 2083 | return u; |
| 2084 | } |
| 2085 | |
| 2086 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2087 | getargs_w_star(PyObject *self, PyObject *args) |
| 2088 | { |
| 2089 | Py_buffer buffer; |
| 2090 | PyObject *result; |
| 2091 | char *str; |
| 2092 | |
| 2093 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 2094 | return NULL; |
| 2095 | |
| 2096 | if (2 <= buffer.len) { |
| 2097 | str = buffer.buf; |
| 2098 | str[0] = '['; |
| 2099 | str[buffer.len-1] = ']'; |
| 2100 | } |
| 2101 | |
| 2102 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 2103 | PyBuffer_Release(&buffer); |
| 2104 | return result; |
| 2105 | } |
| 2106 | |
| 2107 | |
| 2108 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2109 | test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 2112 | PyObject *tuple, *dict = NULL; |
| 2113 | static char *kwlist[] = {NULL}; |
| 2114 | int result; |
| 2115 | tuple = PyTuple_New(0); |
| 2116 | if (!tuple) |
| 2117 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2118 | if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | goto done; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2120 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | dict = PyDict_New(); |
| 2122 | if (!dict) |
| 2123 | goto done; |
| 2124 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2125 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | Py_DECREF(tuple); |
| 2127 | Py_XDECREF(dict); |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2128 | if (!result) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | return NULL; |
Oren Milman | ba7d736 | 2017-08-29 11:58:27 +0300 | [diff] [blame] | 2130 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | else { |
| 2132 | Py_RETURN_NONE; |
| 2133 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2137 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | const char *encoding, *errors = NULL; |
| 2140 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 2141 | &encoding, &errors)) |
| 2142 | return NULL; |
| 2143 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2146 | static PyObject * |
| 2147 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | const char *encoding, *errors = NULL; |
| 2150 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 2151 | &encoding, &errors)) |
| 2152 | return NULL; |
| 2153 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 2156 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 2157 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2158 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2159 | test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | struct triple { |
| 2162 | long input; |
| 2163 | size_t nbits; |
| 2164 | int sign; |
| 2165 | } testcases[] = {{0, 0, 0}, |
| 2166 | {1L, 1, 1}, |
| 2167 | {-1L, 1, -1}, |
| 2168 | {2L, 2, 1}, |
| 2169 | {-2L, 2, -1}, |
| 2170 | {3L, 2, 1}, |
| 2171 | {-3L, 2, -1}, |
| 2172 | {4L, 3, 1}, |
| 2173 | {-4L, 3, -1}, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2174 | {0x7fffL, 15, 1}, /* one Python int digit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | {-0x7fffL, 15, -1}, |
| 2176 | {0xffffL, 16, 1}, |
| 2177 | {-0xffffL, 16, -1}, |
| 2178 | {0xfffffffL, 28, 1}, |
| 2179 | {-0xfffffffL, 28, -1}}; |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 2180 | size_t i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2181 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2182 | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2183 | size_t nbits; |
| 2184 | int sign; |
| 2185 | PyObject *plong; |
| 2186 | |
| 2187 | plong = PyLong_FromLong(testcases[i].input); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2188 | if (plong == NULL) |
| 2189 | return NULL; |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2190 | nbits = _PyLong_NumBits(plong); |
| 2191 | sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2193 | Py_DECREF(plong); |
| 2194 | if (nbits != testcases[i].nbits) |
| 2195 | return raiseTestError("test_long_numbits", |
| 2196 | "wrong result for _PyLong_NumBits"); |
| 2197 | if (sign != testcases[i].sign) |
| 2198 | return raiseTestError("test_long_numbits", |
| 2199 | "wrong result for _PyLong_Sign"); |
| 2200 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2201 | Py_RETURN_NONE; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 2204 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2205 | |
| 2206 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2207 | test_null_strings(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 2210 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 2211 | Py_XDECREF(o1); |
| 2212 | Py_XDECREF(o2); |
| 2213 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2216 | static PyObject * |
| 2217 | raise_exception(PyObject *self, PyObject *args) |
| 2218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | PyObject *exc; |
| 2220 | PyObject *exc_args, *v; |
| 2221 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 2224 | &exc, &num_args)) |
| 2225 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | exc_args = PyTuple_New(num_args); |
| 2228 | if (exc_args == NULL) |
| 2229 | return NULL; |
| 2230 | for (i = 0; i < num_args; ++i) { |
| 2231 | v = PyLong_FromLong(i); |
| 2232 | if (v == NULL) { |
| 2233 | Py_DECREF(exc_args); |
| 2234 | return NULL; |
| 2235 | } |
| 2236 | PyTuple_SET_ITEM(exc_args, i, v); |
| 2237 | } |
| 2238 | PyErr_SetObject(exc, exc_args); |
| 2239 | Py_DECREF(exc_args); |
| 2240 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2241 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2242 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2243 | static PyObject * |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 2244 | set_errno(PyObject *self, PyObject *args) |
| 2245 | { |
| 2246 | int new_errno; |
| 2247 | |
| 2248 | if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
| 2249 | return NULL; |
| 2250 | |
| 2251 | errno = new_errno; |
| 2252 | Py_RETURN_NONE; |
| 2253 | } |
| 2254 | |
| 2255 | static PyObject * |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2256 | test_set_exc_info(PyObject *self, PyObject *args) |
| 2257 | { |
| 2258 | PyObject *orig_exc; |
| 2259 | PyObject *new_type, *new_value, *new_tb; |
| 2260 | PyObject *type, *value, *tb; |
| 2261 | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
| 2262 | &new_type, &new_value, &new_tb)) |
| 2263 | return NULL; |
| 2264 | |
| 2265 | PyErr_GetExcInfo(&type, &value, &tb); |
| 2266 | |
| 2267 | Py_INCREF(new_type); |
| 2268 | Py_INCREF(new_value); |
| 2269 | Py_INCREF(new_tb); |
| 2270 | PyErr_SetExcInfo(new_type, new_value, new_tb); |
| 2271 | |
| 2272 | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
| 2273 | Py_XDECREF(type); |
| 2274 | Py_XDECREF(value); |
| 2275 | Py_XDECREF(tb); |
| 2276 | return orig_exc; |
| 2277 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2278 | |
| 2279 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2280 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2281 | static PyObject * |
| 2282 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | if (PyDateTimeAPI) { |
| 2284 | if (test_run_counter) { |
| 2285 | /* Probably regrtest.py -R */ |
| 2286 | Py_RETURN_NONE; |
| 2287 | } |
| 2288 | else { |
| 2289 | PyErr_SetString(PyExc_AssertionError, |
| 2290 | "PyDateTime_CAPI somehow initialized"); |
| 2291 | return NULL; |
| 2292 | } |
| 2293 | } |
| 2294 | test_run_counter++; |
| 2295 | PyDateTime_IMPORT; |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2297 | if (PyDateTimeAPI) |
| 2298 | Py_RETURN_NONE; |
| 2299 | else |
| 2300 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2303 | /* Functions exposing the C API type checking for testing */ |
| 2304 | #define MAKE_DATETIME_CHECK_FUNC(check_method, exact_method) \ |
| 2305 | PyObject *obj; \ |
| 2306 | int exact = 0; \ |
| 2307 | if (!PyArg_ParseTuple(args, "O|p", &obj, &exact)) { \ |
| 2308 | return NULL; \ |
| 2309 | } \ |
| 2310 | int rv = exact?exact_method(obj):check_method(obj); \ |
| 2311 | if (rv) { \ |
| 2312 | Py_RETURN_TRUE; \ |
| 2313 | } else { \ |
| 2314 | Py_RETURN_FALSE; \ |
| 2315 | } |
| 2316 | |
| 2317 | static PyObject * |
| 2318 | datetime_check_date(PyObject *self, PyObject *args) { |
| 2319 | MAKE_DATETIME_CHECK_FUNC(PyDate_Check, PyDate_CheckExact) |
| 2320 | } |
| 2321 | |
| 2322 | static PyObject * |
| 2323 | datetime_check_time(PyObject *self, PyObject *args) { |
| 2324 | MAKE_DATETIME_CHECK_FUNC(PyTime_Check, PyTime_CheckExact) |
| 2325 | } |
| 2326 | |
| 2327 | static PyObject * |
| 2328 | datetime_check_datetime(PyObject *self, PyObject *args) { |
| 2329 | MAKE_DATETIME_CHECK_FUNC(PyDateTime_Check, PyDateTime_CheckExact) |
| 2330 | } |
| 2331 | |
| 2332 | static PyObject * |
| 2333 | datetime_check_delta(PyObject *self, PyObject *args) { |
| 2334 | MAKE_DATETIME_CHECK_FUNC(PyDelta_Check, PyDelta_CheckExact) |
| 2335 | } |
| 2336 | |
| 2337 | static PyObject * |
| 2338 | datetime_check_tzinfo(PyObject *self, PyObject *args) { |
| 2339 | MAKE_DATETIME_CHECK_FUNC(PyTZInfo_Check, PyTZInfo_CheckExact) |
| 2340 | } |
| 2341 | |
| 2342 | |
| 2343 | /* Makes three variations on timezone representing UTC-5: |
| 2344 | 1. timezone with offset and name from PyDateTimeAPI |
| 2345 | 2. timezone with offset and name from PyTimeZone_FromOffsetAndName |
| 2346 | 3. timezone with offset (no name) from PyTimeZone_FromOffset |
| 2347 | */ |
| 2348 | static PyObject * |
| 2349 | make_timezones_capi(PyObject *self, PyObject *args) { |
| 2350 | PyObject *offset = PyDelta_FromDSU(0, -18000, 0); |
| 2351 | PyObject *name = PyUnicode_FromString("EST"); |
| 2352 | |
| 2353 | PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name); |
| 2354 | PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name); |
| 2355 | PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset); |
| 2356 | |
| 2357 | Py_DecRef(offset); |
| 2358 | Py_DecRef(name); |
| 2359 | |
| 2360 | PyObject *rv = PyTuple_New(3); |
| 2361 | |
| 2362 | PyTuple_SET_ITEM(rv, 0, est_zone_capi); |
| 2363 | PyTuple_SET_ITEM(rv, 1, est_zone_macro); |
| 2364 | PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname); |
| 2365 | |
| 2366 | return rv; |
| 2367 | } |
| 2368 | |
| 2369 | static PyObject * |
Paul Ganssle | a049f57 | 2018-02-22 15:15:32 -0500 | [diff] [blame] | 2370 | get_timezones_offset_zero(PyObject *self, PyObject *args) { |
| 2371 | PyObject *offset = PyDelta_FromDSU(0, 0, 0); |
| 2372 | PyObject *name = PyUnicode_FromString(""); |
| 2373 | |
| 2374 | // These two should return the UTC singleton |
| 2375 | PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset); |
| 2376 | PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL); |
| 2377 | |
| 2378 | // This one will return +00:00 zone, but not the UTC singleton |
| 2379 | PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name); |
| 2380 | |
| 2381 | Py_DecRef(offset); |
| 2382 | Py_DecRef(name); |
| 2383 | |
| 2384 | PyObject *rv = PyTuple_New(3); |
| 2385 | PyTuple_SET_ITEM(rv, 0, utc_singleton_0); |
| 2386 | PyTuple_SET_ITEM(rv, 1, utc_singleton_1); |
| 2387 | PyTuple_SET_ITEM(rv, 2, non_utc_zone); |
| 2388 | |
| 2389 | return rv; |
| 2390 | } |
| 2391 | |
| 2392 | static PyObject * |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2393 | get_timezone_utc_capi(PyObject* self, PyObject *args) { |
| 2394 | int macro = 0; |
| 2395 | if (!PyArg_ParseTuple(args, "|p", ¯o)) { |
| 2396 | return NULL; |
| 2397 | } |
| 2398 | if (macro) { |
Paul Ganssle | 58dc03c | 2018-01-25 08:58:07 -0500 | [diff] [blame] | 2399 | Py_INCREF(PyDateTime_TimeZone_UTC); |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2400 | return PyDateTime_TimeZone_UTC; |
| 2401 | } else { |
Paul Ganssle | 58dc03c | 2018-01-25 08:58:07 -0500 | [diff] [blame] | 2402 | Py_INCREF(PyDateTimeAPI->TimeZone_UTC); |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 2403 | return PyDateTimeAPI->TimeZone_UTC; |
| 2404 | } |
| 2405 | } |
| 2406 | |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2407 | static PyObject * |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 2408 | get_date_fromdate(PyObject *self, PyObject *args) |
| 2409 | { |
| 2410 | PyObject *rv = NULL; |
| 2411 | int macro; |
| 2412 | int year, month, day; |
| 2413 | |
| 2414 | if (!PyArg_ParseTuple(args, "piii", ¯o, &year, &month, &day)) { |
| 2415 | return NULL; |
| 2416 | } |
| 2417 | |
| 2418 | if (macro) { |
| 2419 | rv = PyDate_FromDate(year, month, day); |
| 2420 | } |
| 2421 | else { |
| 2422 | rv = PyDateTimeAPI->Date_FromDate( |
| 2423 | year, month, day, |
| 2424 | PyDateTimeAPI->DateType); |
| 2425 | } |
| 2426 | return rv; |
| 2427 | } |
| 2428 | |
| 2429 | static PyObject * |
| 2430 | get_datetime_fromdateandtime(PyObject *self, PyObject *args) |
| 2431 | { |
| 2432 | PyObject *rv = NULL; |
| 2433 | int macro; |
| 2434 | int year, month, day; |
| 2435 | int hour, minute, second, microsecond; |
| 2436 | |
| 2437 | if (!PyArg_ParseTuple(args, "piiiiiii", |
| 2438 | ¯o, |
| 2439 | &year, &month, &day, |
| 2440 | &hour, &minute, &second, µsecond)) { |
| 2441 | return NULL; |
| 2442 | } |
| 2443 | |
| 2444 | if (macro) { |
| 2445 | rv = PyDateTime_FromDateAndTime( |
| 2446 | year, month, day, |
| 2447 | hour, minute, second, microsecond); |
| 2448 | } |
| 2449 | else { |
| 2450 | rv = PyDateTimeAPI->DateTime_FromDateAndTime( |
| 2451 | year, month, day, |
| 2452 | hour, minute, second, microsecond, |
| 2453 | Py_None, |
| 2454 | PyDateTimeAPI->DateTimeType); |
| 2455 | } |
| 2456 | return rv; |
| 2457 | } |
| 2458 | |
| 2459 | static PyObject * |
| 2460 | get_datetime_fromdateandtimeandfold(PyObject *self, PyObject *args) |
| 2461 | { |
| 2462 | PyObject *rv = NULL; |
| 2463 | int macro; |
| 2464 | int year, month, day; |
| 2465 | int hour, minute, second, microsecond, fold; |
| 2466 | |
| 2467 | if (!PyArg_ParseTuple(args, "piiiiiiii", |
| 2468 | ¯o, |
| 2469 | &year, &month, &day, |
| 2470 | &hour, &minute, &second, µsecond, |
| 2471 | &fold)) { |
| 2472 | return NULL; |
| 2473 | } |
| 2474 | |
| 2475 | if (macro) { |
| 2476 | rv = PyDateTime_FromDateAndTimeAndFold( |
| 2477 | year, month, day, |
| 2478 | hour, minute, second, microsecond, |
| 2479 | fold); |
| 2480 | } |
| 2481 | else { |
| 2482 | rv = PyDateTimeAPI->DateTime_FromDateAndTimeAndFold( |
| 2483 | year, month, day, |
| 2484 | hour, minute, second, microsecond, |
| 2485 | Py_None, |
| 2486 | fold, |
| 2487 | PyDateTimeAPI->DateTimeType); |
| 2488 | } |
| 2489 | return rv; |
| 2490 | } |
| 2491 | |
| 2492 | static PyObject * |
| 2493 | get_time_fromtime(PyObject *self, PyObject *args) |
| 2494 | { |
| 2495 | PyObject *rv = NULL; |
| 2496 | int macro; |
| 2497 | int hour, minute, second, microsecond; |
| 2498 | |
| 2499 | if (!PyArg_ParseTuple(args, "piiii", |
| 2500 | ¯o, |
| 2501 | &hour, &minute, &second, µsecond)) { |
| 2502 | return NULL; |
| 2503 | } |
| 2504 | |
| 2505 | if (macro) { |
| 2506 | rv = PyTime_FromTime(hour, minute, second, microsecond); |
| 2507 | } |
| 2508 | else { |
| 2509 | rv = PyDateTimeAPI->Time_FromTime( |
| 2510 | hour, minute, second, microsecond, |
| 2511 | Py_None, |
| 2512 | PyDateTimeAPI->TimeType); |
| 2513 | } |
| 2514 | return rv; |
| 2515 | } |
| 2516 | |
| 2517 | static PyObject * |
| 2518 | get_time_fromtimeandfold(PyObject *self, PyObject *args) |
| 2519 | { |
| 2520 | PyObject *rv = NULL; |
| 2521 | int macro; |
| 2522 | int hour, minute, second, microsecond, fold; |
| 2523 | |
| 2524 | if (!PyArg_ParseTuple(args, "piiiii", |
| 2525 | ¯o, |
| 2526 | &hour, &minute, &second, µsecond, |
| 2527 | &fold)) { |
| 2528 | return NULL; |
| 2529 | } |
| 2530 | |
| 2531 | if (macro) { |
| 2532 | rv = PyTime_FromTimeAndFold(hour, minute, second, microsecond, fold); |
| 2533 | } |
| 2534 | else { |
| 2535 | rv = PyDateTimeAPI->Time_FromTimeAndFold( |
| 2536 | hour, minute, second, microsecond, |
| 2537 | Py_None, |
| 2538 | fold, |
| 2539 | PyDateTimeAPI->TimeType); |
| 2540 | } |
| 2541 | return rv; |
| 2542 | } |
| 2543 | |
| 2544 | static PyObject * |
| 2545 | get_delta_fromdsu(PyObject *self, PyObject *args) |
| 2546 | { |
| 2547 | PyObject *rv = NULL; |
| 2548 | int macro; |
| 2549 | int days, seconds, microseconds; |
| 2550 | |
| 2551 | if (!PyArg_ParseTuple(args, "piii", |
| 2552 | ¯o, |
| 2553 | &days, &seconds, µseconds)) { |
| 2554 | return NULL; |
| 2555 | } |
| 2556 | |
| 2557 | if (macro) { |
| 2558 | rv = PyDelta_FromDSU(days, seconds, microseconds); |
| 2559 | } |
| 2560 | else { |
| 2561 | rv = PyDateTimeAPI->Delta_FromDelta( |
| 2562 | days, seconds, microseconds, 1, |
| 2563 | PyDateTimeAPI->DeltaType); |
| 2564 | } |
| 2565 | |
| 2566 | return rv; |
| 2567 | } |
| 2568 | |
| 2569 | static PyObject * |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 2570 | get_date_fromtimestamp(PyObject* self, PyObject *args) |
| 2571 | { |
| 2572 | PyObject *tsargs = NULL, *ts = NULL, *rv = NULL; |
| 2573 | int macro = 0; |
| 2574 | |
| 2575 | if (!PyArg_ParseTuple(args, "O|p", &ts, ¯o)) { |
| 2576 | return NULL; |
| 2577 | } |
| 2578 | |
| 2579 | // Construct the argument tuple |
| 2580 | if ((tsargs = PyTuple_Pack(1, ts)) == NULL) { |
| 2581 | return NULL; |
| 2582 | } |
| 2583 | |
| 2584 | // Pass along to the API function |
| 2585 | if (macro) { |
| 2586 | rv = PyDate_FromTimestamp(tsargs); |
| 2587 | } |
| 2588 | else { |
| 2589 | rv = PyDateTimeAPI->Date_FromTimestamp( |
| 2590 | (PyObject *)PyDateTimeAPI->DateType, tsargs |
| 2591 | ); |
| 2592 | } |
| 2593 | |
| 2594 | Py_DECREF(tsargs); |
| 2595 | return rv; |
| 2596 | } |
| 2597 | |
| 2598 | static PyObject * |
| 2599 | get_datetime_fromtimestamp(PyObject* self, PyObject *args) |
| 2600 | { |
| 2601 | int macro = 0; |
| 2602 | int usetz = 0; |
| 2603 | PyObject *tsargs = NULL, *ts = NULL, *tzinfo = Py_None, *rv = NULL; |
| 2604 | if (!PyArg_ParseTuple(args, "OO|pp", &ts, &tzinfo, &usetz, ¯o)) { |
| 2605 | return NULL; |
| 2606 | } |
| 2607 | |
| 2608 | // Construct the argument tuple |
| 2609 | if (usetz) { |
| 2610 | tsargs = PyTuple_Pack(2, ts, tzinfo); |
| 2611 | } |
| 2612 | else { |
| 2613 | tsargs = PyTuple_Pack(1, ts); |
| 2614 | } |
| 2615 | |
| 2616 | if (tsargs == NULL) { |
| 2617 | return NULL; |
| 2618 | } |
| 2619 | |
| 2620 | // Pass along to the API function |
| 2621 | if (macro) { |
| 2622 | rv = PyDateTime_FromTimestamp(tsargs); |
| 2623 | } |
| 2624 | else { |
| 2625 | rv = PyDateTimeAPI->DateTime_FromTimestamp( |
| 2626 | (PyObject *)PyDateTimeAPI->DateTimeType, tsargs, NULL |
| 2627 | ); |
| 2628 | } |
| 2629 | |
| 2630 | Py_DECREF(tsargs); |
| 2631 | return rv; |
| 2632 | } |
| 2633 | |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 2634 | static PyObject * |
| 2635 | test_PyDateTime_GET(PyObject *self, PyObject *obj) |
| 2636 | { |
| 2637 | int year, month, day; |
| 2638 | |
| 2639 | year = PyDateTime_GET_YEAR(obj); |
| 2640 | month = PyDateTime_GET_MONTH(obj); |
| 2641 | day = PyDateTime_GET_DAY(obj); |
| 2642 | |
| 2643 | return Py_BuildValue("(lll)", year, month, day); |
| 2644 | } |
| 2645 | |
| 2646 | static PyObject * |
| 2647 | test_PyDateTime_DATE_GET(PyObject *self, PyObject *obj) |
| 2648 | { |
| 2649 | int hour, minute, second, microsecond; |
| 2650 | |
| 2651 | hour = PyDateTime_DATE_GET_HOUR(obj); |
| 2652 | minute = PyDateTime_DATE_GET_MINUTE(obj); |
| 2653 | second = PyDateTime_DATE_GET_SECOND(obj); |
| 2654 | microsecond = PyDateTime_DATE_GET_MICROSECOND(obj); |
| 2655 | |
| 2656 | return Py_BuildValue("(llll)", hour, minute, second, microsecond); |
| 2657 | } |
| 2658 | |
| 2659 | static PyObject * |
| 2660 | test_PyDateTime_TIME_GET(PyObject *self, PyObject *obj) |
| 2661 | { |
| 2662 | int hour, minute, second, microsecond; |
| 2663 | |
| 2664 | hour = PyDateTime_TIME_GET_HOUR(obj); |
| 2665 | minute = PyDateTime_TIME_GET_MINUTE(obj); |
| 2666 | second = PyDateTime_TIME_GET_SECOND(obj); |
| 2667 | microsecond = PyDateTime_TIME_GET_MICROSECOND(obj); |
| 2668 | |
| 2669 | return Py_BuildValue("(llll)", hour, minute, second, microsecond); |
| 2670 | } |
| 2671 | |
| 2672 | static PyObject * |
| 2673 | test_PyDateTime_DELTA_GET(PyObject *self, PyObject *obj) |
| 2674 | { |
| 2675 | int days, seconds, microseconds; |
| 2676 | |
| 2677 | days = PyDateTime_DELTA_GET_DAYS(obj); |
| 2678 | seconds = PyDateTime_DELTA_GET_SECONDS(obj); |
| 2679 | microseconds = PyDateTime_DELTA_GET_MICROSECONDS(obj); |
| 2680 | |
| 2681 | return Py_BuildValue("(lll)", days, seconds, microseconds); |
| 2682 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2683 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2684 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 2685 | * `thread_done` when it's finished. The driver code has to know when the |
| 2686 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 2687 | * may go away when the driver finishes. The former lack of this explicit |
| 2688 | * synchronization caused rare segfaults, so rare that they were seen only |
| 2689 | * on a Mac buildbot (although they were possible on any box). |
| 2690 | */ |
| 2691 | static PyThread_type_lock thread_done = NULL; |
| 2692 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2693 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2694 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | PyObject *rc; |
| 2697 | int success; |
| 2698 | PyGILState_STATE s = PyGILState_Ensure(); |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2699 | rc = _PyObject_CallNoArg((PyObject *)callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2700 | success = (rc != NULL); |
| 2701 | Py_XDECREF(rc); |
| 2702 | PyGILState_Release(s); |
| 2703 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2706 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 2707 | * should be called only from threads spawned by test_thread_state(). |
| 2708 | */ |
| 2709 | static void |
| 2710 | _make_call_from_thread(void *callable) |
| 2711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2712 | _make_call(callable); |
| 2713 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2716 | static PyObject * |
| 2717 | test_thread_state(PyObject *self, PyObject *args) |
| 2718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | PyObject *fn; |
| 2720 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2722 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 2723 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | if (!PyCallable_Check(fn)) { |
| 2726 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 2727 | Py_TYPE(fn)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | return NULL; |
| 2729 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | /* Ensure Python is set up for threading */ |
| 2732 | PyEval_InitThreads(); |
| 2733 | thread_done = PyThread_allocate_lock(); |
| 2734 | if (thread_done == NULL) |
| 2735 | return PyErr_NoMemory(); |
| 2736 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | /* Start a new thread with our callback. */ |
| 2739 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2740 | /* Make the callback with the thread lock held by this thread */ |
| 2741 | success &= _make_call(fn); |
| 2742 | /* Do it all again, but this time with the thread-lock released */ |
| 2743 | Py_BEGIN_ALLOW_THREADS |
| 2744 | success &= _make_call(fn); |
| 2745 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2746 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | /* And once more with and without a thread |
| 2749 | XXX - should use a lock and work out exactly what we are trying |
| 2750 | to test <wink> |
| 2751 | */ |
| 2752 | Py_BEGIN_ALLOW_THREADS |
| 2753 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2754 | success &= _make_call(fn); |
| 2755 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2756 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 2759 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | PyThread_free_lock(thread_done); |
| 2762 | if (!success) |
| 2763 | return NULL; |
| 2764 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2765 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2766 | |
| 2767 | /* test Py_AddPendingCalls using threads */ |
| 2768 | static int _pending_callback(void *arg) |
| 2769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | /* we assume the argument is callable object to which we own a reference */ |
| 2771 | PyObject *callable = (PyObject *)arg; |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 2772 | PyObject *r = _PyObject_CallNoArg(callable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | Py_DECREF(callable); |
| 2774 | Py_XDECREF(r); |
| 2775 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | /* The following requests n callbacks to _pending_callback. It can be |
| 2779 | * run from any python thread. |
| 2780 | */ |
Benjamin Peterson | b4588c2 | 2018-07-03 22:30:56 -0700 | [diff] [blame] | 2781 | static PyObject * |
| 2782 | pending_threadfunc(PyObject *self, PyObject *arg) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | PyObject *callable; |
| 2785 | int r; |
| 2786 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 2787 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | /* create the reference for the callbackwhile we hold the lock */ |
| 2790 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | Py_BEGIN_ALLOW_THREADS |
| 2793 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 2794 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | if (r<0) { |
| 2797 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2798 | Py_RETURN_FALSE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 2800 | Py_RETURN_TRUE; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2801 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2802 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 2803 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2804 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 2805 | test_string_from_format(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2807 | PyObject *result; |
| 2808 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2809 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2810 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 2811 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 2812 | if (result == NULL) \ |
| 2813 | return NULL; \ |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 2814 | if (!_PyUnicode_EqualToASCIIString(result, "1")) { \ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2815 | msg = FORMAT " failed at 1"; \ |
| 2816 | goto Fail; \ |
| 2817 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | CHECK_1_FORMAT("%d", int); |
| 2821 | CHECK_1_FORMAT("%ld", long); |
| 2822 | /* The z width modifier was added in Python 2.5. */ |
| 2823 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | /* The u type code was added in Python 2.5. */ |
| 2826 | CHECK_1_FORMAT("%u", unsigned int); |
| 2827 | CHECK_1_FORMAT("%lu", unsigned long); |
| 2828 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 2831 | CHECK_1_FORMAT("%llu", unsigned long long); |
| 2832 | CHECK_1_FORMAT("%lld", long long); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2835 | |
| 2836 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | Py_XDECREF(result); |
| 2838 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2839 | |
| 2840 | #undef CHECK_1_FORMAT |
| 2841 | } |
| 2842 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2843 | |
| 2844 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2845 | test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 2847 | int result; |
| 2848 | if (py_s == NULL) |
| 2849 | return NULL; |
| 2850 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 2851 | Py_DECREF(py_s); |
| 2852 | if (!result) { |
| 2853 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 2854 | "should not compare equal to c string."); |
| 2855 | return NULL; |
| 2856 | } |
| 2857 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2858 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2859 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2860 | /* This is here to provide a docstring for test_descr. */ |
| 2861 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2862 | test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2863 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2867 | /* Test PyOS_string_to_double. */ |
| 2868 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2869 | test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | double result; |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 2871 | const char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | #define CHECK_STRING(STR, expected) \ |
| 2874 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2875 | if (result == -1.0 && PyErr_Occurred()) \ |
| 2876 | return NULL; \ |
Benjamin Peterson | 8f4b247 | 2016-09-07 18:09:22 -0700 | [diff] [blame] | 2877 | if (result != (double)expected) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | msg = "conversion of " STR " to float failed"; \ |
| 2879 | goto fail; \ |
| 2880 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | #define CHECK_INVALID(STR) \ |
| 2883 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2884 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 2885 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 2886 | PyErr_Clear(); \ |
| 2887 | else \ |
| 2888 | return NULL; \ |
| 2889 | } \ |
| 2890 | else { \ |
| 2891 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 2892 | goto fail; \ |
| 2893 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | CHECK_STRING("0.1", 0.1); |
| 2896 | CHECK_STRING("1.234", 1.234); |
| 2897 | CHECK_STRING("-1.35", -1.35); |
| 2898 | CHECK_STRING(".1e01", 1.0); |
| 2899 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | CHECK_INVALID(" 0.1"); |
| 2902 | CHECK_INVALID("\t\n-3"); |
| 2903 | CHECK_INVALID(".123 "); |
| 2904 | CHECK_INVALID("3\n"); |
| 2905 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2908 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2910 | #undef CHECK_STRING |
| 2911 | #undef CHECK_INVALID |
| 2912 | } |
| 2913 | |
| 2914 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2915 | /* Coverage testing of capsule objects. */ |
| 2916 | |
| 2917 | static const char *capsule_name = "capsule name"; |
| 2918 | static char *capsule_pointer = "capsule pointer"; |
| 2919 | static char *capsule_context = "capsule context"; |
| 2920 | static const char *capsule_error = NULL; |
| 2921 | static int |
| 2922 | capsule_destructor_call_count = 0; |
| 2923 | |
| 2924 | static void |
| 2925 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | capsule_destructor_call_count++; |
| 2927 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 2928 | capsule_error = "context did not match in destructor!"; |
| 2929 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2930 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2931 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 2932 | capsule_error = "name did not match in destructor!"; |
| 2933 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2934 | capsule_error = "pointer did not match in destructor!"; |
| 2935 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | char *name; |
| 2940 | char *module; |
| 2941 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2942 | } known_capsule; |
| 2943 | |
| 2944 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 2945 | test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2946 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | PyObject *object; |
| 2948 | const char *error = NULL; |
| 2949 | void *pointer; |
| 2950 | void *pointer2; |
| 2951 | known_capsule known_capsules[] = { |
| 2952 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2953 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 2954 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 2955 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2956 | { NULL, NULL }, |
| 2957 | }; |
| 2958 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2959 | |
| 2960 | #define FAIL(x) { error = (x); goto exit; } |
| 2961 | |
| 2962 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | if (capsule_error) { \ |
| 2964 | FAIL(capsule_error); \ |
| 2965 | } \ |
| 2966 | else if (!capsule_destructor_call_count) { \ |
| 2967 | FAIL("destructor not called!"); \ |
| 2968 | } \ |
| 2969 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2972 | PyCapsule_SetContext(object, capsule_context); |
| 2973 | capsule_destructor(object); |
| 2974 | CHECK_DESTRUCTOR; |
| 2975 | Py_DECREF(object); |
| 2976 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | object = PyCapsule_New(known, "ignored", NULL); |
| 2979 | PyCapsule_SetPointer(object, capsule_pointer); |
| 2980 | PyCapsule_SetName(object, capsule_name); |
| 2981 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 2982 | PyCapsule_SetContext(object, capsule_context); |
| 2983 | capsule_destructor(object); |
| 2984 | CHECK_DESTRUCTOR; |
| 2985 | /* intentionally access using the wrong name */ |
| 2986 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2987 | if (!PyErr_Occurred()) { |
| 2988 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2989 | } |
| 2990 | PyErr_Clear(); |
| 2991 | if (pointer2) { |
| 2992 | if (pointer2 == capsule_pointer) { |
| 2993 | FAIL("PyCapsule_GetPointer should not have" |
| 2994 | " returned the internal pointer!"); |
| 2995 | } else { |
| 2996 | FAIL("PyCapsule_GetPointer should have " |
| 2997 | "returned NULL pointer but did not!"); |
| 2998 | } |
| 2999 | } |
| 3000 | PyCapsule_SetDestructor(object, NULL); |
| 3001 | Py_DECREF(object); |
| 3002 | if (capsule_destructor_call_count) { |
| 3003 | FAIL("destructor called when it should not have been!"); |
| 3004 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3006 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 3007 | /* yeah, ordinarily I wouldn't do this either, |
| 3008 | but it's fine for this test harness. |
| 3009 | */ |
| 3010 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3011 | #undef FAIL |
| 3012 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | { \ |
| 3014 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 3015 | x, known->module, known->attribute); \ |
| 3016 | error = buffer; \ |
| 3017 | goto exit; \ |
| 3018 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3020 | PyObject *module = PyImport_ImportModule(known->module); |
| 3021 | if (module) { |
| 3022 | pointer = PyCapsule_Import(known->name, 0); |
| 3023 | if (!pointer) { |
| 3024 | Py_DECREF(module); |
| 3025 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 3026 | } |
| 3027 | object = PyObject_GetAttrString(module, known->attribute); |
| 3028 | if (!object) { |
| 3029 | Py_DECREF(module); |
| 3030 | return NULL; |
| 3031 | } |
| 3032 | pointer2 = PyCapsule_GetPointer(object, |
| 3033 | "weebles wobble but they don't fall down"); |
| 3034 | if (!PyErr_Occurred()) { |
| 3035 | Py_DECREF(object); |
| 3036 | Py_DECREF(module); |
| 3037 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 3038 | } |
| 3039 | PyErr_Clear(); |
| 3040 | if (pointer2) { |
| 3041 | Py_DECREF(module); |
| 3042 | Py_DECREF(object); |
| 3043 | if (pointer2 == pointer) { |
| 3044 | FAIL("PyCapsule_GetPointer should not have" |
| 3045 | " returned its internal pointer!"); |
| 3046 | } else { |
| 3047 | FAIL("PyCapsule_GetPointer should have" |
| 3048 | " returned NULL pointer but did not!"); |
| 3049 | } |
| 3050 | } |
| 3051 | Py_DECREF(object); |
| 3052 | Py_DECREF(module); |
| 3053 | } |
| 3054 | else |
| 3055 | PyErr_Clear(); |
| 3056 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3057 | |
| 3058 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | if (error) { |
| 3060 | return raiseTestError("test_capsule", error); |
| 3061 | } |
| 3062 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 3063 | #undef FAIL |
| 3064 | } |
| 3065 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3066 | #ifdef HAVE_GETTIMEOFDAY |
| 3067 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 3068 | 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] | 3069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | e->tv_sec -= s->tv_sec; |
| 3071 | e->tv_usec -= s->tv_usec; |
| 3072 | if (e->tv_usec < 0) { |
| 3073 | e->tv_sec -=1; |
| 3074 | e->tv_usec += 1000000; |
| 3075 | } |
| 3076 | 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] | 3077 | } |
| 3078 | |
| 3079 | static PyObject * |
| 3080 | profile_int(PyObject *self, PyObject* args) |
| 3081 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | int i, k; |
| 3083 | struct timeval start, stop; |
| 3084 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | /* Test 1: Allocate and immediately deallocate |
| 3087 | many small integers */ |
| 3088 | gettimeofday(&start, NULL); |
| 3089 | for(k=0; k < 20000; k++) |
| 3090 | for(i=0; i < 1000; i++) { |
| 3091 | single = PyLong_FromLong(i); |
| 3092 | Py_DECREF(single); |
| 3093 | } |
| 3094 | gettimeofday(&stop, NULL); |
| 3095 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | /* Test 2: Allocate and immediately deallocate |
| 3098 | many large integers */ |
| 3099 | gettimeofday(&start, NULL); |
| 3100 | for(k=0; k < 20000; k++) |
| 3101 | for(i=0; i < 1000; i++) { |
| 3102 | single = PyLong_FromLong(i+1000000); |
| 3103 | Py_DECREF(single); |
| 3104 | } |
| 3105 | gettimeofday(&stop, NULL); |
| 3106 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | /* Test 3: Allocate a few integers, then release |
| 3109 | them all simultaneously. */ |
| 3110 | multiple = malloc(sizeof(PyObject*) * 1000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3111 | if (multiple == NULL) |
| 3112 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | gettimeofday(&start, NULL); |
| 3114 | for(k=0; k < 20000; k++) { |
| 3115 | for(i=0; i < 1000; i++) { |
| 3116 | multiple[i] = PyLong_FromLong(i+1000000); |
| 3117 | } |
| 3118 | for(i=0; i < 1000; i++) { |
| 3119 | Py_DECREF(multiple[i]); |
| 3120 | } |
| 3121 | } |
| 3122 | gettimeofday(&stop, NULL); |
| 3123 | print_delta(3, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3124 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3126 | /* Test 4: Allocate many integers, then release |
| 3127 | them all simultaneously. */ |
| 3128 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3129 | if (multiple == NULL) |
| 3130 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 | gettimeofday(&start, NULL); |
| 3132 | for(k=0; k < 20; k++) { |
| 3133 | for(i=0; i < 1000000; i++) { |
| 3134 | multiple[i] = PyLong_FromLong(i+1000000); |
| 3135 | } |
| 3136 | for(i=0; i < 1000000; i++) { |
| 3137 | Py_DECREF(multiple[i]); |
| 3138 | } |
| 3139 | } |
| 3140 | gettimeofday(&stop, NULL); |
| 3141 | print_delta(4, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3142 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3144 | /* Test 5: Allocate many integers < 32000 */ |
| 3145 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3146 | if (multiple == NULL) |
| 3147 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | gettimeofday(&start, NULL); |
| 3149 | for(k=0; k < 10; k++) { |
| 3150 | for(i=0; i < 1000000; i++) { |
| 3151 | multiple[i] = PyLong_FromLong(i+1000); |
| 3152 | } |
| 3153 | for(i=0; i < 1000000; i++) { |
| 3154 | Py_DECREF(multiple[i]); |
| 3155 | } |
| 3156 | } |
| 3157 | gettimeofday(&stop, NULL); |
| 3158 | print_delta(5, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 3159 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | /* Test 6: Perform small int addition */ |
| 3162 | op1 = PyLong_FromLong(1); |
| 3163 | gettimeofday(&start, NULL); |
| 3164 | for(i=0; i < 10000000; i++) { |
| 3165 | result = PyNumber_Add(op1, op1); |
| 3166 | Py_DECREF(result); |
| 3167 | } |
| 3168 | gettimeofday(&stop, NULL); |
| 3169 | Py_DECREF(op1); |
| 3170 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | /* Test 7: Perform medium int addition */ |
| 3173 | op1 = PyLong_FromLong(1000); |
Christian Heimes | 66eda26 | 2013-07-26 15:54:07 +0200 | [diff] [blame] | 3174 | if (op1 == NULL) |
| 3175 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3176 | gettimeofday(&start, NULL); |
| 3177 | for(i=0; i < 10000000; i++) { |
| 3178 | result = PyNumber_Add(op1, op1); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 3179 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3180 | } |
| 3181 | gettimeofday(&stop, NULL); |
| 3182 | Py_DECREF(op1); |
| 3183 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3184 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 3185 | Py_RETURN_NONE; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3186 | } |
| 3187 | #endif |
| 3188 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3189 | /* To test the format of tracebacks as printed out. */ |
| 3190 | static PyObject * |
| 3191 | traceback_print(PyObject *self, PyObject *args) |
| 3192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3193 | PyObject *file; |
| 3194 | PyObject *traceback; |
| 3195 | int result; |
| 3196 | |
| 3197 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 3198 | &traceback, &file)) |
| 3199 | return NULL; |
| 3200 | |
| 3201 | result = PyTraceBack_Print(traceback, file); |
| 3202 | if (result < 0) |
| 3203 | return NULL; |
| 3204 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3207 | /* To test the format of exceptions as printed out. */ |
| 3208 | static PyObject * |
| 3209 | exception_print(PyObject *self, PyObject *args) |
| 3210 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | PyObject *value; |
| 3212 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 3215 | &value)) |
| 3216 | return NULL; |
| 3217 | if (!PyExceptionInstance_Check(value)) { |
| 3218 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 3219 | return NULL; |
| 3220 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | tb = PyException_GetTraceback(value); |
| 3223 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 3224 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | |
| 3230 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3231 | |
| 3232 | /* reliably raise a MemoryError */ |
| 3233 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3234 | raise_memoryerror(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | PyErr_NoMemory(); |
| 3237 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3240 | /* Issue 6012 */ |
| 3241 | static PyObject *str1, *str2; |
| 3242 | static int |
| 3243 | failing_converter(PyObject *obj, void *arg) |
| 3244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | /* Clone str1, then let the conversion fail. */ |
| 3246 | assert(str1); |
| 3247 | str2 = str1; |
| 3248 | Py_INCREF(str2); |
| 3249 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3250 | } |
| 3251 | static PyObject* |
| 3252 | argparsing(PyObject *o, PyObject *args) |
| 3253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | PyObject *res; |
| 3255 | str1 = str2 = NULL; |
| 3256 | if (!PyArg_ParseTuple(args, "O&O&", |
| 3257 | PyUnicode_FSConverter, &str1, |
| 3258 | failing_converter, &str2)) { |
| 3259 | if (!str2) |
| 3260 | /* argument converter not called? */ |
| 3261 | return NULL; |
| 3262 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 3263 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | Py_DECREF(str2); |
| 3265 | PyErr_Clear(); |
| 3266 | return res; |
| 3267 | } |
| 3268 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 3269 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 3270 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3271 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 3272 | static PyObject * |
| 3273 | code_newempty(PyObject *self, PyObject *args) |
| 3274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | const char *filename; |
| 3276 | const char *funcname; |
| 3277 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3279 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 3280 | &filename, &funcname, &firstlineno)) |
| 3281 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3286 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 3287 | Run via Lib/test/test_exceptions.py */ |
| 3288 | static PyObject * |
| 3289 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 3290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | const char *name; |
| 3292 | const char *doc = NULL; |
| 3293 | PyObject *base = NULL; |
| 3294 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3296 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3299 | "s|sOO:make_exception_with_doc", kwlist, |
| 3300 | &name, &doc, &base, &dict)) |
| 3301 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3303 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3306 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3307 | make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3308 | { |
| 3309 | Py_buffer info; |
| 3310 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 3311 | return NULL; |
| 3312 | return PyMemoryView_FromBuffer(&info); |
| 3313 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 3314 | |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3315 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3316 | test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3317 | { |
| 3318 | int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 3319 | int init[5] = {0, 1, 2, 3, 4}; |
| 3320 | Py_ssize_t itemsize = sizeof(int); |
| 3321 | Py_ssize_t shape = 5; |
| 3322 | Py_ssize_t strides = 2 * itemsize; |
| 3323 | Py_buffer view = { |
| 3324 | data, |
| 3325 | NULL, |
| 3326 | 5 * itemsize, |
| 3327 | itemsize, |
| 3328 | 1, |
| 3329 | 1, |
| 3330 | NULL, |
| 3331 | &shape, |
| 3332 | &strides, |
| 3333 | NULL, |
| 3334 | NULL |
| 3335 | }; |
| 3336 | int *ptr; |
| 3337 | int i; |
| 3338 | |
| 3339 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 3340 | ptr = view.buf; |
| 3341 | for (i = 0; i < 5; i++) { |
| 3342 | if (ptr[2*i] != i) { |
| 3343 | PyErr_SetString(TestError, |
| 3344 | "test_from_contiguous: incorrect result"); |
| 3345 | return NULL; |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | view.buf = &data[8]; |
| 3350 | view.strides[0] = -2 * itemsize; |
| 3351 | |
| 3352 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 3353 | ptr = view.buf; |
| 3354 | for (i = 0; i < 5; i++) { |
| 3355 | if (*(ptr-2*i) != i) { |
| 3356 | PyErr_SetString(TestError, |
| 3357 | "test_from_contiguous: incorrect result"); |
| 3358 | return NULL; |
| 3359 | } |
| 3360 | } |
| 3361 | |
| 3362 | Py_RETURN_NONE; |
| 3363 | } |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3364 | |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3365 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3366 | extern PyTypeObject _PyBytesIOBuffer_Type; |
| 3367 | |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3368 | static PyObject * |
Serhiy Storchaka | 8152402 | 2018-11-27 13:05:02 +0200 | [diff] [blame] | 3369 | test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored)) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3370 | { |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3371 | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3372 | PyObject *b; |
| 3373 | char *dummy[1]; |
| 3374 | int ret, match; |
| 3375 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3376 | /* PyBuffer_FillInfo() */ |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3377 | ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE); |
| 3378 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 3379 | PyErr_Clear(); |
| 3380 | if (ret != -1 || match == 0) |
| 3381 | goto error; |
| 3382 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3383 | /* bytesiobuf_getbuffer() */ |
| 3384 | b = type->tp_alloc(type, 0); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3385 | if (b == NULL) { |
| 3386 | return NULL; |
| 3387 | } |
| 3388 | |
| 3389 | ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE); |
| 3390 | Py_DECREF(b); |
| 3391 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 3392 | PyErr_Clear(); |
| 3393 | if (ret != -1 || match == 0) |
| 3394 | goto error; |
| 3395 | |
| 3396 | Py_RETURN_NONE; |
| 3397 | |
| 3398 | error: |
| 3399 | PyErr_SetString(TestError, |
| 3400 | "test_pep3118_obsolete_write_locks: failure"); |
| 3401 | return NULL; |
| 3402 | } |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3403 | #endif |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 3404 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3405 | /* This tests functions that historically supported write locks. It is |
| 3406 | wrong to call getbuffer() with view==NULL and a compliant getbufferproc |
| 3407 | is entitled to segfault in that case. */ |
| 3408 | static PyObject * |
| 3409 | getbuffer_with_null_view(PyObject* self, PyObject *obj) |
| 3410 | { |
| 3411 | if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0) |
| 3412 | return NULL; |
| 3413 | |
| 3414 | Py_RETURN_NONE; |
| 3415 | } |
| 3416 | |
Joannah Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 3417 | /* PyBuffer_SizeFromFormat() */ |
| 3418 | static PyObject * |
| 3419 | test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args) |
| 3420 | { |
| 3421 | const char *format; |
| 3422 | Py_ssize_t result; |
| 3423 | |
| 3424 | if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat", |
| 3425 | &format)) { |
| 3426 | return NULL; |
| 3427 | } |
| 3428 | |
| 3429 | result = PyBuffer_SizeFromFormat(format); |
| 3430 | if (result == -1) { |
| 3431 | return NULL; |
| 3432 | } |
| 3433 | |
| 3434 | return PyLong_FromSsize_t(result); |
| 3435 | } |
| 3436 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3437 | /* Test that the fatal error from not having a current thread doesn't |
| 3438 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 3439 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3440 | crash_no_current_thread(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3441 | { |
| 3442 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 3443 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 3444 | !pydebug mode. However, the test only actually tests anything |
| 3445 | in pydebug mode, since that's where the infinite loop was in |
| 3446 | the first place. */ |
| 3447 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 3448 | Py_END_ALLOW_THREADS |
| 3449 | return NULL; |
| 3450 | } |
| 3451 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3452 | /* To run some code in a sub-interpreter. */ |
| 3453 | static PyObject * |
| 3454 | run_in_subinterp(PyObject *self, PyObject *args) |
| 3455 | { |
| 3456 | const char *code; |
| 3457 | int r; |
| 3458 | PyThreadState *substate, *mainstate; |
| 3459 | |
| 3460 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 3461 | &code)) |
| 3462 | return NULL; |
| 3463 | |
| 3464 | mainstate = PyThreadState_Get(); |
| 3465 | |
| 3466 | PyThreadState_Swap(NULL); |
| 3467 | |
| 3468 | substate = Py_NewInterpreter(); |
Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 3469 | if (substate == NULL) { |
| 3470 | /* Since no new thread state was created, there is no exception to |
| 3471 | propagate; raise a fresh one after swapping in the old thread |
| 3472 | state. */ |
| 3473 | PyThreadState_Swap(mainstate); |
| 3474 | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
| 3475 | return NULL; |
| 3476 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3477 | r = PyRun_SimpleString(code); |
| 3478 | Py_EndInterpreter(substate); |
| 3479 | |
| 3480 | PyThreadState_Swap(mainstate); |
| 3481 | |
| 3482 | return PyLong_FromLong(r); |
| 3483 | } |
| 3484 | |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3485 | static int |
| 3486 | check_time_rounding(int round) |
| 3487 | { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 3488 | if (round != _PyTime_ROUND_FLOOR |
| 3489 | && round != _PyTime_ROUND_CEILING |
Pablo Galindo | 2c15b29 | 2017-10-17 15:14:41 +0100 | [diff] [blame] | 3490 | && round != _PyTime_ROUND_HALF_EVEN |
| 3491 | && round != _PyTime_ROUND_UP) { |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3492 | PyErr_SetString(PyExc_ValueError, "invalid rounding"); |
| 3493 | return -1; |
| 3494 | } |
| 3495 | return 0; |
| 3496 | } |
| 3497 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3498 | static PyObject * |
| 3499 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
| 3500 | { |
| 3501 | PyObject *obj; |
| 3502 | time_t sec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3503 | int round; |
| 3504 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3505 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3506 | if (check_time_rounding(round) < 0) |
| 3507 | return NULL; |
| 3508 | if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3509 | return NULL; |
| 3510 | return _PyLong_FromTime_t(sec); |
| 3511 | } |
| 3512 | |
| 3513 | static PyObject * |
| 3514 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
| 3515 | { |
| 3516 | PyObject *obj; |
| 3517 | time_t sec; |
| 3518 | long usec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3519 | int round; |
| 3520 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3521 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3522 | if (check_time_rounding(round) < 0) |
| 3523 | return NULL; |
| 3524 | if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3525 | return NULL; |
| 3526 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
| 3527 | } |
| 3528 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3529 | static PyObject * |
| 3530 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
| 3531 | { |
| 3532 | PyObject *obj; |
| 3533 | time_t sec; |
| 3534 | long nsec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3535 | int round; |
| 3536 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3537 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 3538 | if (check_time_rounding(round) < 0) |
| 3539 | return NULL; |
| 3540 | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3541 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 3542 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 3543 | } |
| 3544 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3545 | static void |
| 3546 | slot_tp_del(PyObject *self) |
| 3547 | { |
| 3548 | _Py_IDENTIFIER(__tp_del__); |
| 3549 | PyObject *del, *res; |
| 3550 | PyObject *error_type, *error_value, *error_traceback; |
| 3551 | |
| 3552 | /* Temporarily resurrect the object. */ |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3553 | assert(Py_REFCNT(self) == 0); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3554 | Py_SET_REFCNT(self, 1); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3555 | |
| 3556 | /* Save the current exception, if any. */ |
| 3557 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 3558 | |
| 3559 | /* Execute __del__ method, if any. */ |
| 3560 | del = _PyObject_LookupSpecial(self, &PyId___tp_del__); |
| 3561 | if (del != NULL) { |
INADA Naoki | 72dccde | 2017-02-16 09:26:01 +0900 | [diff] [blame] | 3562 | res = _PyObject_CallNoArg(del); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3563 | if (res == NULL) |
| 3564 | PyErr_WriteUnraisable(del); |
| 3565 | else |
| 3566 | Py_DECREF(res); |
| 3567 | Py_DECREF(del); |
| 3568 | } |
| 3569 | |
| 3570 | /* Restore the saved exception. */ |
| 3571 | PyErr_Restore(error_type, error_value, error_traceback); |
| 3572 | |
| 3573 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 3574 | * cause a recursive call. |
| 3575 | */ |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3576 | assert(Py_REFCNT(self) > 0); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3577 | Py_SET_REFCNT(self, Py_REFCNT(self) - 1); |
| 3578 | if (Py_REFCNT(self) == 0) { |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3579 | /* this is the normal path out */ |
| 3580 | return; |
| 3581 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3582 | |
| 3583 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 3584 | * never happened. |
| 3585 | */ |
| 3586 | { |
Victor Stinner | a93c51e | 2020-02-07 00:38:59 +0100 | [diff] [blame] | 3587 | Py_ssize_t refcnt = Py_REFCNT(self); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3588 | _Py_NewReference(self); |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 3589 | Py_SET_REFCNT(self, refcnt); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3590 | } |
INADA Naoki | d852142 | 2018-05-17 11:07:21 +0900 | [diff] [blame] | 3591 | assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self)); |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 3592 | /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased |
| 3593 | _Py_RefTotal, so we need to undo that. */ |
| 3594 | #ifdef Py_REF_DEBUG |
| 3595 | _Py_RefTotal--; |
| 3596 | #endif |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 3597 | } |
| 3598 | |
| 3599 | static PyObject * |
| 3600 | with_tp_del(PyObject *self, PyObject *args) |
| 3601 | { |
| 3602 | PyObject *obj; |
| 3603 | PyTypeObject *tp; |
| 3604 | |
| 3605 | if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) |
| 3606 | return NULL; |
| 3607 | tp = (PyTypeObject *) obj; |
| 3608 | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
| 3609 | PyErr_Format(PyExc_TypeError, |
| 3610 | "heap type expected, got %R", obj); |
| 3611 | return NULL; |
| 3612 | } |
| 3613 | tp->tp_del = slot_tp_del; |
| 3614 | Py_INCREF(obj); |
| 3615 | return obj; |
| 3616 | } |
| 3617 | |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 3618 | static PyMethodDef ml; |
| 3619 | |
| 3620 | static PyObject * |
| 3621 | create_cfunction(PyObject *self, PyObject *args) |
| 3622 | { |
| 3623 | return PyCFunction_NewEx(&ml, self, NULL); |
| 3624 | } |
| 3625 | |
| 3626 | static PyMethodDef ml = { |
| 3627 | "create_cfunction", |
| 3628 | create_cfunction, |
| 3629 | METH_NOARGS, |
| 3630 | NULL |
| 3631 | }; |
| 3632 | |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3633 | static PyObject * |
| 3634 | _test_incref(PyObject *ob) |
| 3635 | { |
| 3636 | Py_INCREF(ob); |
| 3637 | return ob; |
| 3638 | } |
| 3639 | |
| 3640 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3641 | test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3642 | { |
| 3643 | PyObject *obj = PyLong_FromLong(0); |
| 3644 | Py_XINCREF(_test_incref(obj)); |
| 3645 | Py_DECREF(obj); |
| 3646 | Py_DECREF(obj); |
| 3647 | Py_DECREF(obj); |
| 3648 | Py_RETURN_NONE; |
| 3649 | } |
| 3650 | |
| 3651 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3652 | test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3653 | { |
| 3654 | PyObject *obj = PyLong_FromLong(0); |
| 3655 | Py_INCREF(_test_incref(obj)); |
| 3656 | Py_DECREF(obj); |
| 3657 | Py_DECREF(obj); |
| 3658 | Py_DECREF(obj); |
| 3659 | Py_RETURN_NONE; |
| 3660 | } |
| 3661 | |
| 3662 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3663 | test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3664 | { |
| 3665 | Py_XDECREF(PyLong_FromLong(0)); |
| 3666 | Py_RETURN_NONE; |
| 3667 | } |
| 3668 | |
| 3669 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3670 | test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3671 | { |
| 3672 | Py_DECREF(PyLong_FromLong(0)); |
| 3673 | Py_RETURN_NONE; |
| 3674 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3675 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3676 | static PyObject * |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 3677 | test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self), |
| 3678 | PyObject *Py_UNUSED(args)) |
| 3679 | { |
| 3680 | PyStructSequence_Desc descr; |
| 3681 | PyStructSequence_Field descr_fields[3]; |
| 3682 | |
| 3683 | descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"}; |
| 3684 | descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"}; |
| 3685 | descr_fields[2] = (PyStructSequence_Field){0, NULL}; |
| 3686 | |
| 3687 | descr.name = "_testcapi.test_descr"; |
| 3688 | descr.doc = "This is used to test for memory leaks in NewType"; |
| 3689 | descr.fields = descr_fields; |
| 3690 | descr.n_in_sequence = 1; |
| 3691 | |
| 3692 | PyTypeObject* structseq_type = PyStructSequence_NewType(&descr); |
| 3693 | assert(structseq_type != NULL); |
| 3694 | assert(PyType_Check(structseq_type)); |
| 3695 | assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS)); |
| 3696 | Py_DECREF(structseq_type); |
| 3697 | |
| 3698 | Py_RETURN_NONE; |
| 3699 | } |
| 3700 | |
| 3701 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3702 | test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored)) |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3703 | { |
| 3704 | PyObject *obj = PyLong_FromLong(0); |
Victor Stinner | fc6a90a | 2014-10-09 22:15:41 +0200 | [diff] [blame] | 3705 | Py_IncRef(obj); |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3706 | Py_DecRef(obj); |
| 3707 | Py_DecRef(obj); |
| 3708 | Py_RETURN_NONE; |
| 3709 | } |
| 3710 | |
| 3711 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3712 | test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3713 | { |
| 3714 | void *ptr; |
| 3715 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3716 | ptr = PyMem_RawMalloc(0); |
| 3717 | if (ptr == NULL) { |
| 3718 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); |
| 3719 | return NULL; |
| 3720 | } |
| 3721 | PyMem_RawFree(ptr); |
| 3722 | |
| 3723 | ptr = PyMem_RawCalloc(0, 0); |
| 3724 | if (ptr == NULL) { |
| 3725 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); |
| 3726 | return NULL; |
| 3727 | } |
| 3728 | PyMem_RawFree(ptr); |
| 3729 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3730 | ptr = PyMem_Malloc(0); |
| 3731 | if (ptr == NULL) { |
| 3732 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); |
| 3733 | return NULL; |
| 3734 | } |
| 3735 | PyMem_Free(ptr); |
| 3736 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3737 | ptr = PyMem_Calloc(0, 0); |
| 3738 | if (ptr == NULL) { |
| 3739 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); |
| 3740 | return NULL; |
| 3741 | } |
| 3742 | PyMem_Free(ptr); |
| 3743 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3744 | ptr = PyObject_Malloc(0); |
| 3745 | if (ptr == NULL) { |
| 3746 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); |
| 3747 | return NULL; |
| 3748 | } |
| 3749 | PyObject_Free(ptr); |
| 3750 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3751 | ptr = PyObject_Calloc(0, 0); |
| 3752 | if (ptr == NULL) { |
| 3753 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); |
| 3754 | return NULL; |
| 3755 | } |
| 3756 | PyObject_Free(ptr); |
| 3757 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3758 | Py_RETURN_NONE; |
| 3759 | } |
| 3760 | |
| 3761 | typedef struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3762 | PyMemAllocatorEx alloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3763 | |
| 3764 | size_t malloc_size; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3765 | size_t calloc_nelem; |
| 3766 | size_t calloc_elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3767 | void *realloc_ptr; |
| 3768 | size_t realloc_new_size; |
| 3769 | void *free_ptr; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3770 | void *ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3771 | } alloc_hook_t; |
| 3772 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3773 | static void* hook_malloc(void* ctx, size_t size) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3774 | { |
| 3775 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3776 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3777 | hook->malloc_size = size; |
| 3778 | return hook->alloc.malloc(hook->alloc.ctx, size); |
| 3779 | } |
| 3780 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3781 | static void* hook_calloc(void* ctx, size_t nelem, size_t elsize) |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3782 | { |
| 3783 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3784 | hook->ctx = ctx; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3785 | hook->calloc_nelem = nelem; |
| 3786 | hook->calloc_elsize = elsize; |
| 3787 | return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); |
| 3788 | } |
| 3789 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3790 | static void* hook_realloc(void* ctx, void* ptr, size_t new_size) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3791 | { |
| 3792 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3793 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3794 | hook->realloc_ptr = ptr; |
| 3795 | hook->realloc_new_size = new_size; |
| 3796 | return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); |
| 3797 | } |
| 3798 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3799 | static void hook_free(void *ctx, void *ptr) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3800 | { |
| 3801 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3802 | hook->ctx = ctx; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3803 | hook->free_ptr = ptr; |
| 3804 | hook->alloc.free(hook->alloc.ctx, ptr); |
| 3805 | } |
| 3806 | |
| 3807 | static PyObject * |
| 3808 | test_setallocators(PyMemAllocatorDomain domain) |
| 3809 | { |
| 3810 | PyObject *res = NULL; |
| 3811 | const char *error_msg; |
| 3812 | alloc_hook_t hook; |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3813 | PyMemAllocatorEx alloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3814 | size_t size, size2, nelem, elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3815 | void *ptr, *ptr2; |
| 3816 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3817 | memset(&hook, 0, sizeof(hook)); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3818 | |
| 3819 | alloc.ctx = &hook; |
| 3820 | alloc.malloc = &hook_malloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3821 | alloc.calloc = &hook_calloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3822 | alloc.realloc = &hook_realloc; |
| 3823 | alloc.free = &hook_free; |
| 3824 | PyMem_GetAllocator(domain, &hook.alloc); |
| 3825 | PyMem_SetAllocator(domain, &alloc); |
| 3826 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3827 | /* malloc, realloc, free */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3828 | size = 42; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3829 | hook.ctx = NULL; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3830 | switch(domain) |
| 3831 | { |
| 3832 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; |
| 3833 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; |
| 3834 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; |
| 3835 | default: ptr = NULL; break; |
| 3836 | } |
| 3837 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3838 | #define CHECK_CTX(FUNC) \ |
| 3839 | if (hook.ctx != &hook) { \ |
| 3840 | error_msg = FUNC " wrong context"; \ |
| 3841 | goto fail; \ |
| 3842 | } \ |
| 3843 | hook.ctx = NULL; /* reset for next check */ |
| 3844 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3845 | if (ptr == NULL) { |
| 3846 | error_msg = "malloc failed"; |
| 3847 | goto fail; |
| 3848 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3849 | CHECK_CTX("malloc"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3850 | if (hook.malloc_size != size) { |
| 3851 | error_msg = "malloc invalid size"; |
| 3852 | goto fail; |
| 3853 | } |
| 3854 | |
| 3855 | size2 = 200; |
| 3856 | switch(domain) |
| 3857 | { |
| 3858 | case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; |
| 3859 | case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; |
| 3860 | case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; |
Christian Heimes | 865d12a | 2013-08-02 11:10:51 +0200 | [diff] [blame] | 3861 | default: ptr2 = NULL; break; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | if (ptr2 == NULL) { |
| 3865 | error_msg = "realloc failed"; |
| 3866 | goto fail; |
| 3867 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3868 | CHECK_CTX("realloc"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3869 | if (hook.realloc_ptr != ptr |
| 3870 | || hook.realloc_new_size != size2) { |
| 3871 | error_msg = "realloc invalid parameters"; |
| 3872 | goto fail; |
| 3873 | } |
| 3874 | |
| 3875 | switch(domain) |
| 3876 | { |
| 3877 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; |
| 3878 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; |
| 3879 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; |
| 3880 | } |
| 3881 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3882 | CHECK_CTX("free"); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3883 | if (hook.free_ptr != ptr2) { |
| 3884 | error_msg = "free invalid pointer"; |
| 3885 | goto fail; |
| 3886 | } |
| 3887 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3888 | /* calloc, free */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3889 | nelem = 2; |
| 3890 | elsize = 5; |
| 3891 | switch(domain) |
| 3892 | { |
| 3893 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; |
| 3894 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; |
| 3895 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; |
| 3896 | default: ptr = NULL; break; |
| 3897 | } |
| 3898 | |
| 3899 | if (ptr == NULL) { |
| 3900 | error_msg = "calloc failed"; |
| 3901 | goto fail; |
| 3902 | } |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3903 | CHECK_CTX("calloc"); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3904 | if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { |
| 3905 | error_msg = "calloc invalid nelem or elsize"; |
| 3906 | goto fail; |
| 3907 | } |
| 3908 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3909 | hook.free_ptr = NULL; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3910 | switch(domain) |
| 3911 | { |
| 3912 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; |
| 3913 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; |
| 3914 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; |
| 3915 | } |
| 3916 | |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3917 | CHECK_CTX("calloc free"); |
| 3918 | if (hook.free_ptr != ptr) { |
| 3919 | error_msg = "calloc free invalid pointer"; |
| 3920 | goto fail; |
| 3921 | } |
| 3922 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3923 | Py_INCREF(Py_None); |
| 3924 | res = Py_None; |
| 3925 | goto finally; |
| 3926 | |
| 3927 | fail: |
| 3928 | PyErr_SetString(PyExc_RuntimeError, error_msg); |
| 3929 | |
| 3930 | finally: |
| 3931 | PyMem_SetAllocator(domain, &hook.alloc); |
| 3932 | return res; |
Victor Stinner | 9ed83c4 | 2017-10-31 12:18:10 -0700 | [diff] [blame] | 3933 | |
| 3934 | #undef CHECK_CTX |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3938 | test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3939 | { |
| 3940 | return test_setallocators(PYMEM_DOMAIN_RAW); |
| 3941 | } |
| 3942 | |
| 3943 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3944 | test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3945 | { |
| 3946 | return test_setallocators(PYMEM_DOMAIN_MEM); |
| 3947 | } |
| 3948 | |
| 3949 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3950 | test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored)) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3951 | { |
| 3952 | return test_setallocators(PYMEM_DOMAIN_OBJ); |
| 3953 | } |
| 3954 | |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 3955 | /* Most part of the following code is inherited from the pyfailmalloc project |
| 3956 | * written by Victor Stinner. */ |
| 3957 | static struct { |
| 3958 | int installed; |
| 3959 | PyMemAllocatorEx raw; |
| 3960 | PyMemAllocatorEx mem; |
| 3961 | PyMemAllocatorEx obj; |
| 3962 | } FmHook; |
| 3963 | |
| 3964 | static struct { |
| 3965 | int start; |
| 3966 | int stop; |
| 3967 | Py_ssize_t count; |
| 3968 | } FmData; |
| 3969 | |
| 3970 | static int |
| 3971 | fm_nomemory(void) |
| 3972 | { |
| 3973 | FmData.count++; |
| 3974 | if (FmData.count > FmData.start && |
| 3975 | (FmData.stop <= 0 || FmData.count <= FmData.stop)) { |
| 3976 | return 1; |
| 3977 | } |
| 3978 | return 0; |
| 3979 | } |
| 3980 | |
| 3981 | static void * |
| 3982 | hook_fmalloc(void *ctx, size_t size) |
| 3983 | { |
| 3984 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3985 | if (fm_nomemory()) { |
| 3986 | return NULL; |
| 3987 | } |
| 3988 | return alloc->malloc(alloc->ctx, size); |
| 3989 | } |
| 3990 | |
| 3991 | static void * |
| 3992 | hook_fcalloc(void *ctx, size_t nelem, size_t elsize) |
| 3993 | { |
| 3994 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 3995 | if (fm_nomemory()) { |
| 3996 | return NULL; |
| 3997 | } |
| 3998 | return alloc->calloc(alloc->ctx, nelem, elsize); |
| 3999 | } |
| 4000 | |
| 4001 | static void * |
| 4002 | hook_frealloc(void *ctx, void *ptr, size_t new_size) |
| 4003 | { |
| 4004 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4005 | if (fm_nomemory()) { |
| 4006 | return NULL; |
| 4007 | } |
| 4008 | return alloc->realloc(alloc->ctx, ptr, new_size); |
| 4009 | } |
| 4010 | |
| 4011 | static void |
| 4012 | hook_ffree(void *ctx, void *ptr) |
| 4013 | { |
| 4014 | PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx; |
| 4015 | alloc->free(alloc->ctx, ptr); |
| 4016 | } |
| 4017 | |
| 4018 | static void |
| 4019 | fm_setup_hooks(void) |
| 4020 | { |
| 4021 | PyMemAllocatorEx alloc; |
| 4022 | |
| 4023 | if (FmHook.installed) { |
| 4024 | return; |
| 4025 | } |
| 4026 | FmHook.installed = 1; |
| 4027 | |
| 4028 | alloc.malloc = hook_fmalloc; |
| 4029 | alloc.calloc = hook_fcalloc; |
| 4030 | alloc.realloc = hook_frealloc; |
| 4031 | alloc.free = hook_ffree; |
| 4032 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 4033 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 4034 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 4035 | |
| 4036 | alloc.ctx = &FmHook.raw; |
| 4037 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 4038 | |
| 4039 | alloc.ctx = &FmHook.mem; |
| 4040 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 4041 | |
| 4042 | alloc.ctx = &FmHook.obj; |
| 4043 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 4044 | } |
| 4045 | |
| 4046 | static void |
| 4047 | fm_remove_hooks(void) |
| 4048 | { |
| 4049 | if (FmHook.installed) { |
| 4050 | FmHook.installed = 0; |
| 4051 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw); |
| 4052 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem); |
| 4053 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj); |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | static PyObject* |
| 4058 | set_nomemory(PyObject *self, PyObject *args) |
| 4059 | { |
| 4060 | /* Memory allocation fails after 'start' allocation requests, and until |
| 4061 | * 'stop' allocation requests except when 'stop' is negative or equal |
| 4062 | * to 0 (default) in which case allocation failures never stop. */ |
| 4063 | FmData.count = 0; |
| 4064 | FmData.stop = 0; |
| 4065 | if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) { |
| 4066 | return NULL; |
| 4067 | } |
| 4068 | fm_setup_hooks(); |
| 4069 | Py_RETURN_NONE; |
| 4070 | } |
| 4071 | |
| 4072 | static PyObject* |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4073 | remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored)) |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 4074 | { |
| 4075 | fm_remove_hooks(); |
| 4076 | Py_RETURN_NONE; |
| 4077 | } |
| 4078 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4079 | PyDoc_STRVAR(docstring_empty, |
| 4080 | "" |
| 4081 | ); |
| 4082 | |
| 4083 | PyDoc_STRVAR(docstring_no_signature, |
| 4084 | "This docstring has no signature." |
| 4085 | ); |
| 4086 | |
| 4087 | PyDoc_STRVAR(docstring_with_invalid_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4088 | "docstring_with_invalid_signature($module, /, boo)\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4089 | "\n" |
| 4090 | "This docstring has an invalid signature." |
| 4091 | ); |
| 4092 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4093 | PyDoc_STRVAR(docstring_with_invalid_signature2, |
| 4094 | "docstring_with_invalid_signature2($module, /, boo)\n" |
| 4095 | "\n" |
| 4096 | "--\n" |
| 4097 | "\n" |
| 4098 | "This docstring also has an invalid signature." |
| 4099 | ); |
| 4100 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4101 | PyDoc_STRVAR(docstring_with_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4102 | "docstring_with_signature($module, /, sig)\n" |
| 4103 | "--\n" |
| 4104 | "\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4105 | "This docstring has a valid signature." |
| 4106 | ); |
| 4107 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 4108 | PyDoc_STRVAR(docstring_with_signature_but_no_doc, |
| 4109 | "docstring_with_signature_but_no_doc($module, /, sig)\n" |
| 4110 | "--\n" |
| 4111 | "\n" |
| 4112 | ); |
| 4113 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4114 | PyDoc_STRVAR(docstring_with_signature_and_extra_newlines, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4115 | "docstring_with_signature_and_extra_newlines($module, /, parameter)\n" |
| 4116 | "--\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4117 | "\n" |
| 4118 | "\n" |
| 4119 | "This docstring has a valid signature and some extra newlines." |
| 4120 | ); |
| 4121 | |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4122 | PyDoc_STRVAR(docstring_with_signature_with_defaults, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4123 | "docstring_with_signature_with_defaults(module, s='avocado',\n" |
| 4124 | " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n" |
| 4125 | " local=the_number_three, sys=sys.maxsize,\n" |
| 4126 | " exp=sys.maxsize - 1)\n" |
| 4127 | "--\n" |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4128 | "\n" |
| 4129 | "\n" |
| 4130 | "\n" |
| 4131 | "This docstring has a valid signature with parameters,\n" |
| 4132 | "and the parameters take defaults of varying types." |
| 4133 | ); |
| 4134 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4135 | typedef struct { |
| 4136 | PyThread_type_lock start_event; |
| 4137 | PyThread_type_lock exit_event; |
| 4138 | PyObject *callback; |
| 4139 | } test_c_thread_t; |
| 4140 | |
| 4141 | static void |
| 4142 | temporary_c_thread(void *data) |
| 4143 | { |
| 4144 | test_c_thread_t *test_c_thread = data; |
| 4145 | PyGILState_STATE state; |
| 4146 | PyObject *res; |
| 4147 | |
| 4148 | PyThread_release_lock(test_c_thread->start_event); |
| 4149 | |
| 4150 | /* Allocate a Python thread state for this thread */ |
| 4151 | state = PyGILState_Ensure(); |
| 4152 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 4153 | res = _PyObject_CallNoArg(test_c_thread->callback); |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4154 | Py_CLEAR(test_c_thread->callback); |
| 4155 | |
| 4156 | if (res == NULL) { |
| 4157 | PyErr_Print(); |
| 4158 | } |
| 4159 | else { |
| 4160 | Py_DECREF(res); |
| 4161 | } |
| 4162 | |
| 4163 | /* Destroy the Python thread state for this thread */ |
| 4164 | PyGILState_Release(state); |
| 4165 | |
| 4166 | PyThread_release_lock(test_c_thread->exit_event); |
| 4167 | |
| 4168 | PyThread_exit_thread(); |
| 4169 | } |
| 4170 | |
| 4171 | static PyObject * |
| 4172 | call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
| 4173 | { |
| 4174 | PyObject *res = NULL; |
| 4175 | test_c_thread_t test_c_thread; |
| 4176 | long thread; |
| 4177 | |
| 4178 | PyEval_InitThreads(); |
| 4179 | |
| 4180 | test_c_thread.start_event = PyThread_allocate_lock(); |
| 4181 | test_c_thread.exit_event = PyThread_allocate_lock(); |
| 4182 | test_c_thread.callback = NULL; |
| 4183 | if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
| 4184 | PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
| 4185 | goto exit; |
| 4186 | } |
| 4187 | |
| 4188 | Py_INCREF(callback); |
| 4189 | test_c_thread.callback = callback; |
| 4190 | |
| 4191 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 4192 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 4193 | |
| 4194 | thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
| 4195 | if (thread == -1) { |
| 4196 | PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
| 4197 | PyThread_release_lock(test_c_thread.start_event); |
| 4198 | PyThread_release_lock(test_c_thread.exit_event); |
| 4199 | goto exit; |
| 4200 | } |
| 4201 | |
| 4202 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 4203 | PyThread_release_lock(test_c_thread.start_event); |
| 4204 | |
| 4205 | Py_BEGIN_ALLOW_THREADS |
| 4206 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 4207 | PyThread_release_lock(test_c_thread.exit_event); |
| 4208 | Py_END_ALLOW_THREADS |
| 4209 | |
| 4210 | Py_INCREF(Py_None); |
| 4211 | res = Py_None; |
| 4212 | |
| 4213 | exit: |
| 4214 | Py_CLEAR(test_c_thread.callback); |
| 4215 | if (test_c_thread.start_event) |
| 4216 | PyThread_free_lock(test_c_thread.start_event); |
| 4217 | if (test_c_thread.exit_event) |
| 4218 | PyThread_free_lock(test_c_thread.exit_event); |
| 4219 | return res; |
| 4220 | } |
Victor Stinner | 1310510 | 2013-12-13 02:17:29 +0100 | [diff] [blame] | 4221 | |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4222 | /* marshal */ |
| 4223 | |
| 4224 | static PyObject* |
| 4225 | pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
| 4226 | { |
| 4227 | long value; |
| 4228 | char *filename; |
| 4229 | int version; |
| 4230 | FILE *fp; |
| 4231 | |
| 4232 | if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", |
| 4233 | &value, &filename, &version)) |
| 4234 | return NULL; |
| 4235 | |
| 4236 | fp = fopen(filename, "wb"); |
| 4237 | if (fp == NULL) { |
| 4238 | PyErr_SetFromErrno(PyExc_OSError); |
| 4239 | return NULL; |
| 4240 | } |
| 4241 | |
| 4242 | PyMarshal_WriteLongToFile(value, fp, version); |
| 4243 | |
| 4244 | fclose(fp); |
| 4245 | if (PyErr_Occurred()) |
| 4246 | return NULL; |
| 4247 | Py_RETURN_NONE; |
| 4248 | } |
| 4249 | |
| 4250 | static PyObject* |
| 4251 | pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
| 4252 | { |
| 4253 | PyObject *obj; |
| 4254 | char *filename; |
| 4255 | int version; |
| 4256 | FILE *fp; |
| 4257 | |
| 4258 | if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", |
| 4259 | &obj, &filename, &version)) |
| 4260 | return NULL; |
| 4261 | |
| 4262 | fp = fopen(filename, "wb"); |
| 4263 | if (fp == NULL) { |
| 4264 | PyErr_SetFromErrno(PyExc_OSError); |
| 4265 | return NULL; |
| 4266 | } |
| 4267 | |
| 4268 | PyMarshal_WriteObjectToFile(obj, fp, version); |
| 4269 | |
| 4270 | fclose(fp); |
| 4271 | if (PyErr_Occurred()) |
| 4272 | return NULL; |
| 4273 | Py_RETURN_NONE; |
| 4274 | } |
| 4275 | |
| 4276 | static PyObject* |
| 4277 | pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
| 4278 | { |
| 4279 | int value; |
| 4280 | long pos; |
| 4281 | char *filename; |
| 4282 | FILE *fp; |
| 4283 | |
| 4284 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) |
| 4285 | return NULL; |
| 4286 | |
| 4287 | fp = fopen(filename, "rb"); |
| 4288 | if (fp == NULL) { |
| 4289 | PyErr_SetFromErrno(PyExc_OSError); |
| 4290 | return NULL; |
| 4291 | } |
| 4292 | |
| 4293 | value = PyMarshal_ReadShortFromFile(fp); |
| 4294 | pos = ftell(fp); |
| 4295 | |
| 4296 | fclose(fp); |
| 4297 | if (PyErr_Occurred()) |
| 4298 | return NULL; |
| 4299 | return Py_BuildValue("il", value, pos); |
| 4300 | } |
| 4301 | |
| 4302 | static PyObject* |
| 4303 | pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
| 4304 | { |
| 4305 | long value, pos; |
| 4306 | char *filename; |
| 4307 | FILE *fp; |
| 4308 | |
| 4309 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) |
| 4310 | return NULL; |
| 4311 | |
| 4312 | fp = fopen(filename, "rb"); |
| 4313 | if (fp == NULL) { |
| 4314 | PyErr_SetFromErrno(PyExc_OSError); |
| 4315 | return NULL; |
| 4316 | } |
| 4317 | |
| 4318 | value = PyMarshal_ReadLongFromFile(fp); |
| 4319 | pos = ftell(fp); |
| 4320 | |
| 4321 | fclose(fp); |
| 4322 | if (PyErr_Occurred()) |
| 4323 | return NULL; |
| 4324 | return Py_BuildValue("ll", value, pos); |
| 4325 | } |
| 4326 | |
| 4327 | static PyObject* |
| 4328 | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
| 4329 | { |
| 4330 | PyObject *obj; |
| 4331 | long pos; |
| 4332 | char *filename; |
| 4333 | FILE *fp; |
| 4334 | |
| 4335 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) |
| 4336 | return NULL; |
| 4337 | |
| 4338 | fp = fopen(filename, "rb"); |
| 4339 | if (fp == NULL) { |
| 4340 | PyErr_SetFromErrno(PyExc_OSError); |
| 4341 | return NULL; |
| 4342 | } |
| 4343 | |
| 4344 | obj = PyMarshal_ReadLastObjectFromFile(fp); |
| 4345 | pos = ftell(fp); |
| 4346 | |
| 4347 | fclose(fp); |
| 4348 | return Py_BuildValue("Nl", obj, pos); |
| 4349 | } |
| 4350 | |
| 4351 | static PyObject* |
| 4352 | pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
| 4353 | { |
| 4354 | PyObject *obj; |
| 4355 | long pos; |
| 4356 | char *filename; |
| 4357 | FILE *fp; |
| 4358 | |
| 4359 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) |
| 4360 | return NULL; |
| 4361 | |
| 4362 | fp = fopen(filename, "rb"); |
| 4363 | if (fp == NULL) { |
| 4364 | PyErr_SetFromErrno(PyExc_OSError); |
| 4365 | return NULL; |
| 4366 | } |
| 4367 | |
| 4368 | obj = PyMarshal_ReadObjectFromFile(fp); |
| 4369 | pos = ftell(fp); |
| 4370 | |
| 4371 | fclose(fp); |
| 4372 | return Py_BuildValue("Nl", obj, pos); |
| 4373 | } |
| 4374 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4375 | static PyObject* |
| 4376 | return_null_without_error(PyObject *self, PyObject *args) |
| 4377 | { |
| 4378 | /* invalid call: return NULL without setting an error, |
| 4379 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 4380 | PyErr_Clear(); |
| 4381 | return NULL; |
| 4382 | } |
| 4383 | |
| 4384 | static PyObject* |
| 4385 | return_result_with_error(PyObject *self, PyObject *args) |
| 4386 | { |
| 4387 | /* invalid call: return a result with an error set, |
| 4388 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 4389 | PyErr_SetNone(PyExc_ValueError); |
| 4390 | Py_RETURN_NONE; |
| 4391 | } |
| 4392 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 4393 | static PyObject * |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 4394 | test_pytime_fromseconds(PyObject *self, PyObject *args) |
| 4395 | { |
| 4396 | int seconds; |
| 4397 | _PyTime_t ts; |
| 4398 | |
| 4399 | if (!PyArg_ParseTuple(args, "i", &seconds)) |
| 4400 | return NULL; |
| 4401 | ts = _PyTime_FromSeconds(seconds); |
| 4402 | return _PyTime_AsNanosecondsObject(ts); |
| 4403 | } |
| 4404 | |
| 4405 | static PyObject * |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 4406 | test_pytime_fromsecondsobject(PyObject *self, PyObject *args) |
| 4407 | { |
| 4408 | PyObject *obj; |
| 4409 | int round; |
| 4410 | _PyTime_t ts; |
| 4411 | |
| 4412 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
| 4413 | return NULL; |
| 4414 | if (check_time_rounding(round) < 0) |
| 4415 | return NULL; |
| 4416 | if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) |
| 4417 | return NULL; |
| 4418 | return _PyTime_AsNanosecondsObject(ts); |
| 4419 | } |
| 4420 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4421 | static PyObject * |
| 4422 | test_pytime_assecondsdouble(PyObject *self, PyObject *args) |
| 4423 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4424 | PyObject *obj; |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4425 | _PyTime_t ts; |
| 4426 | double d; |
| 4427 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4428 | if (!PyArg_ParseTuple(args, "O", &obj)) { |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4429 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4430 | } |
| 4431 | if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) { |
| 4432 | return NULL; |
| 4433 | } |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4434 | d = _PyTime_AsSecondsDouble(ts); |
| 4435 | return PyFloat_FromDouble(d); |
| 4436 | } |
| 4437 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4438 | static PyObject * |
| 4439 | test_PyTime_AsTimeval(PyObject *self, PyObject *args) |
| 4440 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4441 | PyObject *obj; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4442 | int round; |
| 4443 | _PyTime_t t; |
| 4444 | struct timeval tv; |
| 4445 | PyObject *seconds; |
| 4446 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4447 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4448 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4449 | if (check_time_rounding(round) < 0) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4450 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4451 | } |
| 4452 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4453 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4454 | } |
| 4455 | if (_PyTime_AsTimeval(t, &tv, round) < 0) { |
| 4456 | return NULL; |
| 4457 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4458 | |
Benjamin Peterson | 2c134c3 | 2017-04-13 01:44:54 -0700 | [diff] [blame] | 4459 | seconds = PyLong_FromLongLong(tv.tv_sec); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4460 | if (seconds == NULL) { |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4461 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4462 | } |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4463 | return Py_BuildValue("Nl", seconds, tv.tv_usec); |
| 4464 | } |
| 4465 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4466 | #ifdef HAVE_CLOCK_GETTIME |
| 4467 | static PyObject * |
| 4468 | test_PyTime_AsTimespec(PyObject *self, PyObject *args) |
| 4469 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4470 | PyObject *obj; |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4471 | _PyTime_t t; |
| 4472 | struct timespec ts; |
| 4473 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4474 | if (!PyArg_ParseTuple(args, "O", &obj)) { |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4475 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4476 | } |
| 4477 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4478 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4479 | } |
| 4480 | if (_PyTime_AsTimespec(t, &ts) == -1) { |
| 4481 | return NULL; |
| 4482 | } |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4483 | return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); |
| 4484 | } |
| 4485 | #endif |
| 4486 | |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4487 | static PyObject * |
| 4488 | test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) |
| 4489 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4490 | PyObject *obj; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4491 | int round; |
| 4492 | _PyTime_t t, ms; |
| 4493 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4494 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4495 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4496 | } |
| 4497 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4498 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4499 | } |
| 4500 | if (check_time_rounding(round) < 0) { |
| 4501 | return NULL; |
| 4502 | } |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4503 | ms = _PyTime_AsMilliseconds(t, round); |
| 4504 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4505 | nanoseconds */ |
| 4506 | return _PyTime_AsNanosecondsObject(ms); |
| 4507 | } |
| 4508 | |
| 4509 | static PyObject * |
| 4510 | test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) |
| 4511 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4512 | PyObject *obj; |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4513 | int round; |
| 4514 | _PyTime_t t, ms; |
| 4515 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4516 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4517 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4518 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4519 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 4520 | } |
| 4521 | if (check_time_rounding(round) < 0) { |
| 4522 | return NULL; |
| 4523 | } |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4524 | ms = _PyTime_AsMicroseconds(t, round); |
| 4525 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 4526 | nanoseconds */ |
| 4527 | return _PyTime_AsNanosecondsObject(ms); |
| 4528 | } |
| 4529 | |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4530 | static PyObject* |
| 4531 | get_recursion_depth(PyObject *self, PyObject *args) |
| 4532 | { |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 4533 | PyThreadState *tstate = PyThreadState_Get(); |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4534 | |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 4535 | /* subtract one to ignore the frame of the get_recursion_depth() call */ |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4536 | return PyLong_FromLong(tstate->recursion_depth - 1); |
| 4537 | } |
| 4538 | |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4539 | static PyObject* |
| 4540 | pymem_buffer_overflow(PyObject *self, PyObject *args) |
| 4541 | { |
| 4542 | char *buffer; |
| 4543 | |
| 4544 | /* Deliberate buffer overflow to check that PyMem_Free() detects |
| 4545 | the overflow when debug hooks are installed. */ |
| 4546 | buffer = PyMem_Malloc(16); |
Victor Stinner | 414b1cd | 2019-03-26 14:35:30 +0100 | [diff] [blame] | 4547 | if (buffer == NULL) { |
| 4548 | PyErr_NoMemory(); |
| 4549 | return NULL; |
| 4550 | } |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4551 | buffer[16] = 'x'; |
| 4552 | PyMem_Free(buffer); |
| 4553 | |
| 4554 | Py_RETURN_NONE; |
| 4555 | } |
| 4556 | |
| 4557 | static PyObject* |
| 4558 | pymem_api_misuse(PyObject *self, PyObject *args) |
| 4559 | { |
| 4560 | char *buffer; |
| 4561 | |
| 4562 | /* Deliberate misusage of Python allocators: |
| 4563 | allococate with PyMem but release with PyMem_Raw. */ |
| 4564 | buffer = PyMem_Malloc(16); |
| 4565 | PyMem_RawFree(buffer); |
| 4566 | |
| 4567 | Py_RETURN_NONE; |
| 4568 | } |
| 4569 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4570 | static PyObject* |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4571 | pymem_malloc_without_gil(PyObject *self, PyObject *args) |
| 4572 | { |
| 4573 | char *buffer; |
| 4574 | |
| 4575 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4576 | call PyMem_Malloc() without holding the GIL */ |
| 4577 | Py_BEGIN_ALLOW_THREADS |
| 4578 | buffer = PyMem_Malloc(10); |
| 4579 | Py_END_ALLOW_THREADS |
| 4580 | |
| 4581 | PyMem_Free(buffer); |
| 4582 | |
| 4583 | Py_RETURN_NONE; |
| 4584 | } |
| 4585 | |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 4586 | |
| 4587 | static PyObject* |
| 4588 | test_pymem_getallocatorsname(PyObject *self, PyObject *args) |
| 4589 | { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 4590 | const char *name = _PyMem_GetCurrentAllocatorName(); |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 4591 | if (name == NULL) { |
| 4592 | PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name"); |
| 4593 | return NULL; |
| 4594 | } |
| 4595 | return PyUnicode_FromString(name); |
| 4596 | } |
| 4597 | |
| 4598 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4599 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4600 | test_pyobject_is_freed(const char *test_name, PyObject *op) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4601 | { |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4602 | if (!_PyObject_IsFreed(op)) { |
| 4603 | return raiseTestError(test_name, "object is not seen as freed"); |
| 4604 | } |
| 4605 | Py_RETURN_NONE; |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | |
| 4609 | static PyObject* |
Victor Stinner | 6876257 | 2019-10-07 18:42:01 +0200 | [diff] [blame] | 4610 | check_pyobject_null_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
| 4611 | { |
| 4612 | PyObject *op = NULL; |
| 4613 | return test_pyobject_is_freed("check_pyobject_null_is_freed", op); |
| 4614 | } |
| 4615 | |
| 4616 | |
| 4617 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4618 | check_pyobject_uninitialized_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4619 | { |
| 4620 | PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject)); |
| 4621 | if (op == NULL) { |
| 4622 | return NULL; |
| 4623 | } |
| 4624 | /* Initialize reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4625 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4626 | /* object fields like ob_type are uninitialized! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4627 | return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | |
| 4631 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4632 | check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4633 | { |
| 4634 | /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */ |
| 4635 | PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type)); |
| 4636 | if (op == NULL) { |
| 4637 | return NULL; |
| 4638 | } |
| 4639 | /* Initialize reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4640 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4641 | /* 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] | 4642 | when using debug hooks on memory allocators! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4643 | return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | |
| 4647 | static PyObject* |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4648 | check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4649 | { |
| 4650 | PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); |
| 4651 | if (op == NULL) { |
| 4652 | return NULL; |
| 4653 | } |
| 4654 | Py_TYPE(op)->tp_dealloc(op); |
| 4655 | /* Reset reference count to avoid early crash in ceval or GC */ |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 4656 | Py_SET_REFCNT(op, 1); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4657 | /* object memory is freed! */ |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 4658 | return test_pyobject_is_freed("check_pyobject_freed_is_freed", op); |
Victor Stinner | 2b00db6 | 2019-04-11 11:33:27 +0200 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | |
| 4662 | static PyObject* |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4663 | pyobject_malloc_without_gil(PyObject *self, PyObject *args) |
| 4664 | { |
| 4665 | char *buffer; |
| 4666 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4667 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 4668 | call PyObject_Malloc() without holding the GIL */ |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4669 | Py_BEGIN_ALLOW_THREADS |
| 4670 | buffer = PyObject_Malloc(10); |
| 4671 | Py_END_ALLOW_THREADS |
| 4672 | |
| 4673 | PyObject_Free(buffer); |
| 4674 | |
| 4675 | Py_RETURN_NONE; |
| 4676 | } |
| 4677 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4678 | static PyObject * |
| 4679 | tracemalloc_track(PyObject *self, PyObject *args) |
| 4680 | { |
| 4681 | unsigned int domain; |
| 4682 | PyObject *ptr_obj; |
| 4683 | void *ptr; |
| 4684 | Py_ssize_t size; |
| 4685 | int release_gil = 0; |
| 4686 | int res; |
| 4687 | |
| 4688 | if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil)) |
| 4689 | return NULL; |
| 4690 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4691 | if (PyErr_Occurred()) |
| 4692 | return NULL; |
| 4693 | |
| 4694 | if (release_gil) { |
| 4695 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4696 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4697 | Py_END_ALLOW_THREADS |
| 4698 | } |
| 4699 | else { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4700 | res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4704 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4705 | return NULL; |
| 4706 | } |
| 4707 | |
| 4708 | Py_RETURN_NONE; |
| 4709 | } |
| 4710 | |
| 4711 | static PyObject * |
| 4712 | tracemalloc_untrack(PyObject *self, PyObject *args) |
| 4713 | { |
| 4714 | unsigned int domain; |
| 4715 | PyObject *ptr_obj; |
| 4716 | void *ptr; |
| 4717 | int res; |
| 4718 | |
| 4719 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4720 | return NULL; |
| 4721 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4722 | if (PyErr_Occurred()) |
| 4723 | return NULL; |
| 4724 | |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4725 | res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4726 | if (res < 0) { |
Victor Stinner | 5ea4c06 | 2017-06-20 17:46:36 +0200 | [diff] [blame] | 4727 | PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error"); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4728 | return NULL; |
| 4729 | } |
| 4730 | |
| 4731 | Py_RETURN_NONE; |
| 4732 | } |
| 4733 | |
| 4734 | static PyObject * |
| 4735 | tracemalloc_get_traceback(PyObject *self, PyObject *args) |
| 4736 | { |
| 4737 | unsigned int domain; |
| 4738 | PyObject *ptr_obj; |
| 4739 | void *ptr; |
| 4740 | |
| 4741 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 4742 | return NULL; |
| 4743 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 4744 | if (PyErr_Occurred()) |
| 4745 | return NULL; |
| 4746 | |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 4747 | return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr); |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4748 | } |
| 4749 | |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4750 | static PyObject * |
| 4751 | dict_get_version(PyObject *self, PyObject *args) |
| 4752 | { |
| 4753 | PyDictObject *dict; |
| 4754 | uint64_t version; |
| 4755 | |
| 4756 | if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict)) |
| 4757 | return NULL; |
| 4758 | |
| 4759 | version = dict->ma_version_tag; |
| 4760 | |
Sergey Fedoseev | a9ed91e | 2019-10-21 11:49:48 +0500 | [diff] [blame] | 4761 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(version)); |
| 4762 | return PyLong_FromUnsignedLongLong((unsigned long long)version); |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 4763 | } |
| 4764 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4765 | |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 4766 | static PyObject * |
| 4767 | raise_SIGINT_then_send_None(PyObject *self, PyObject *args) |
| 4768 | { |
| 4769 | PyGenObject *gen; |
| 4770 | |
| 4771 | if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen)) |
| 4772 | return NULL; |
| 4773 | |
| 4774 | /* This is used in a test to check what happens if a signal arrives just |
| 4775 | as we're in the process of entering a yield from chain (see |
| 4776 | bpo-30039). |
| 4777 | |
| 4778 | Needs to be done in C, because: |
| 4779 | - we don't have a Python wrapper for raise() |
| 4780 | - we need to make sure that the Python-level signal handler doesn't run |
| 4781 | *before* we enter the generator frame, which is impossible in Python |
| 4782 | because we check for signals before every bytecode operation. |
| 4783 | */ |
| 4784 | raise(SIGINT); |
| 4785 | return _PyGen_Send(gen, Py_None); |
| 4786 | } |
| 4787 | |
| 4788 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4789 | static int |
| 4790 | fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs) |
| 4791 | { |
| 4792 | if (args == Py_None) { |
| 4793 | *stack = NULL; |
| 4794 | *nargs = 0; |
| 4795 | } |
| 4796 | else if (PyTuple_Check(args)) { |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 4797 | *stack = ((PyTupleObject *)args)->ob_item; |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4798 | *nargs = PyTuple_GET_SIZE(args); |
| 4799 | } |
| 4800 | else { |
| 4801 | PyErr_SetString(PyExc_TypeError, "args must be None or a tuple"); |
| 4802 | return -1; |
| 4803 | } |
| 4804 | return 0; |
| 4805 | } |
| 4806 | |
| 4807 | |
| 4808 | static PyObject * |
| 4809 | test_pyobject_fastcall(PyObject *self, PyObject *args) |
| 4810 | { |
| 4811 | PyObject *func, *func_args; |
| 4812 | PyObject **stack; |
| 4813 | Py_ssize_t nargs; |
| 4814 | |
| 4815 | if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) { |
| 4816 | return NULL; |
| 4817 | } |
| 4818 | |
| 4819 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4820 | return NULL; |
| 4821 | } |
| 4822 | return _PyObject_FastCall(func, stack, nargs); |
| 4823 | } |
| 4824 | |
| 4825 | |
| 4826 | static PyObject * |
| 4827 | test_pyobject_fastcalldict(PyObject *self, PyObject *args) |
| 4828 | { |
| 4829 | PyObject *func, *func_args, *kwargs; |
| 4830 | PyObject **stack; |
| 4831 | Py_ssize_t nargs; |
| 4832 | |
| 4833 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) { |
| 4834 | return NULL; |
| 4835 | } |
| 4836 | |
| 4837 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4838 | return NULL; |
| 4839 | } |
| 4840 | |
| 4841 | if (kwargs == Py_None) { |
| 4842 | kwargs = NULL; |
| 4843 | } |
| 4844 | else if (!PyDict_Check(kwargs)) { |
| 4845 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict"); |
| 4846 | return NULL; |
| 4847 | } |
| 4848 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame^] | 4849 | return PyObject_VectorcallDict(func, stack, nargs, kwargs); |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | |
| 4853 | static PyObject * |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4854 | test_pyobject_vectorcall(PyObject *self, PyObject *args) |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4855 | { |
| 4856 | PyObject *func, *func_args, *kwnames = NULL; |
| 4857 | PyObject **stack; |
| 4858 | Py_ssize_t nargs, nkw; |
| 4859 | |
| 4860 | if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) { |
| 4861 | return NULL; |
| 4862 | } |
| 4863 | |
| 4864 | if (fastcall_args(func_args, &stack, &nargs) < 0) { |
| 4865 | return NULL; |
| 4866 | } |
| 4867 | |
| 4868 | if (kwnames == Py_None) { |
| 4869 | kwnames = NULL; |
| 4870 | } |
| 4871 | else if (PyTuple_Check(kwnames)) { |
| 4872 | nkw = PyTuple_GET_SIZE(kwnames); |
| 4873 | if (nargs < nkw) { |
| 4874 | PyErr_SetString(PyExc_ValueError, "kwnames longer than args"); |
| 4875 | return NULL; |
| 4876 | } |
| 4877 | nargs -= nkw; |
| 4878 | } |
| 4879 | else { |
| 4880 | PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple"); |
| 4881 | return NULL; |
| 4882 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame^] | 4883 | return PyObject_Vectorcall(func, stack, nargs, kwnames); |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | |
| 4887 | static PyObject * |
| 4888 | test_pyvectorcall_call(PyObject *self, PyObject *args) |
| 4889 | { |
| 4890 | PyObject *func; |
| 4891 | PyObject *argstuple; |
| 4892 | PyObject *kwargs = NULL; |
| 4893 | |
| 4894 | if (!PyArg_ParseTuple(args, "OO|O", &func, &argstuple, &kwargs)) { |
| 4895 | return NULL; |
| 4896 | } |
| 4897 | |
| 4898 | if (!PyTuple_Check(argstuple)) { |
| 4899 | PyErr_SetString(PyExc_TypeError, "args must be a tuple"); |
| 4900 | return NULL; |
| 4901 | } |
| 4902 | if (kwargs != NULL && !PyDict_Check(kwargs)) { |
| 4903 | PyErr_SetString(PyExc_TypeError, "kwargs must be a dict"); |
| 4904 | return NULL; |
| 4905 | } |
| 4906 | |
| 4907 | return PyVectorcall_Call(func, argstuple, kwargs); |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4908 | } |
| 4909 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4910 | |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 4911 | static PyObject* |
| 4912 | stack_pointer(PyObject *self, PyObject *args) |
| 4913 | { |
| 4914 | int v = 5; |
| 4915 | return PyLong_FromVoidPtr(&v); |
| 4916 | } |
| 4917 | |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 4918 | |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 4919 | #ifdef W_STOPCODE |
| 4920 | static PyObject* |
| 4921 | py_w_stopcode(PyObject *self, PyObject *args) |
| 4922 | { |
| 4923 | int sig, status; |
| 4924 | if (!PyArg_ParseTuple(args, "i", &sig)) { |
| 4925 | return NULL; |
| 4926 | } |
| 4927 | status = W_STOPCODE(sig); |
| 4928 | return PyLong_FromLong(status); |
| 4929 | } |
| 4930 | #endif |
| 4931 | |
| 4932 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 4933 | static PyObject * |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 4934 | get_mapping_keys(PyObject* self, PyObject *obj) |
| 4935 | { |
| 4936 | return PyMapping_Keys(obj); |
| 4937 | } |
| 4938 | |
| 4939 | static PyObject * |
| 4940 | get_mapping_values(PyObject* self, PyObject *obj) |
| 4941 | { |
| 4942 | return PyMapping_Values(obj); |
| 4943 | } |
| 4944 | |
| 4945 | static PyObject * |
| 4946 | get_mapping_items(PyObject* self, PyObject *obj) |
| 4947 | { |
| 4948 | return PyMapping_Items(obj); |
| 4949 | } |
| 4950 | |
| 4951 | |
| 4952 | static PyObject * |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 4953 | test_pythread_tss_key_state(PyObject *self, PyObject *args) |
| 4954 | { |
| 4955 | Py_tss_t tss_key = Py_tss_NEEDS_INIT; |
| 4956 | if (PyThread_tss_is_created(&tss_key)) { |
| 4957 | return raiseTestError("test_pythread_tss_key_state", |
| 4958 | "TSS key not in an uninitialized state at " |
| 4959 | "creation time"); |
| 4960 | } |
| 4961 | if (PyThread_tss_create(&tss_key) != 0) { |
| 4962 | PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed"); |
| 4963 | return NULL; |
| 4964 | } |
| 4965 | if (!PyThread_tss_is_created(&tss_key)) { |
| 4966 | return raiseTestError("test_pythread_tss_key_state", |
| 4967 | "PyThread_tss_create succeeded, " |
| 4968 | "but with TSS key in an uninitialized state"); |
| 4969 | } |
| 4970 | if (PyThread_tss_create(&tss_key) != 0) { |
| 4971 | return raiseTestError("test_pythread_tss_key_state", |
| 4972 | "PyThread_tss_create unsuccessful with " |
| 4973 | "an already initialized key"); |
| 4974 | } |
| 4975 | #define CHECK_TSS_API(expr) \ |
| 4976 | (void)(expr); \ |
| 4977 | if (!PyThread_tss_is_created(&tss_key)) { \ |
| 4978 | return raiseTestError("test_pythread_tss_key_state", \ |
| 4979 | "TSS key initialization state was not " \ |
| 4980 | "preserved after calling " #expr); } |
| 4981 | CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL)); |
| 4982 | CHECK_TSS_API(PyThread_tss_get(&tss_key)); |
| 4983 | #undef CHECK_TSS_API |
| 4984 | PyThread_tss_delete(&tss_key); |
| 4985 | if (PyThread_tss_is_created(&tss_key)) { |
| 4986 | return raiseTestError("test_pythread_tss_key_state", |
| 4987 | "PyThread_tss_delete called, but did not " |
| 4988 | "set the key state to uninitialized"); |
| 4989 | } |
| 4990 | |
| 4991 | Py_tss_t *ptr_key = PyThread_tss_alloc(); |
| 4992 | if (ptr_key == NULL) { |
| 4993 | PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed"); |
| 4994 | return NULL; |
| 4995 | } |
| 4996 | if (PyThread_tss_is_created(ptr_key)) { |
| 4997 | return raiseTestError("test_pythread_tss_key_state", |
| 4998 | "TSS key not in an uninitialized state at " |
| 4999 | "allocation time"); |
| 5000 | } |
| 5001 | PyThread_tss_free(ptr_key); |
| 5002 | ptr_key = NULL; |
| 5003 | Py_RETURN_NONE; |
| 5004 | } |
| 5005 | |
| 5006 | |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 5007 | static PyObject* |
| 5008 | new_hamt(PyObject *self, PyObject *args) |
| 5009 | { |
| 5010 | return _PyContext_NewHamtForTests(); |
| 5011 | } |
| 5012 | |
| 5013 | |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5014 | /* def bad_get(self, obj, cls): |
| 5015 | cls() |
| 5016 | return repr(self) |
| 5017 | */ |
| 5018 | static PyObject* |
| 5019 | bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 5020 | { |
Serhiy Storchaka | b1dede3 | 2018-11-20 20:45:40 +0200 | [diff] [blame] | 5021 | PyObject *self, *obj, *cls; |
| 5022 | if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) { |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5023 | return NULL; |
| 5024 | } |
| 5025 | |
Jeroen Demeyer | 7f41c8e | 2019-07-04 12:35:31 +0200 | [diff] [blame] | 5026 | PyObject *res = _PyObject_CallNoArg(cls); |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5027 | if (res == NULL) { |
| 5028 | return NULL; |
| 5029 | } |
| 5030 | Py_DECREF(res); |
| 5031 | |
Serhiy Storchaka | b1dede3 | 2018-11-20 20:45:40 +0200 | [diff] [blame] | 5032 | return PyObject_Repr(self); |
jdemeyer | 5a30620 | 2018-10-19 23:50:06 +0200 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 5036 | static PyObject * |
| 5037 | encode_locale_ex(PyObject *self, PyObject *args) |
| 5038 | { |
| 5039 | PyObject *unicode; |
| 5040 | int current_locale = 0; |
| 5041 | wchar_t *wstr; |
| 5042 | PyObject *res = NULL; |
| 5043 | const char *errors = NULL; |
| 5044 | |
| 5045 | if (!PyArg_ParseTuple(args, "U|is", &unicode, ¤t_locale, &errors)) { |
| 5046 | return NULL; |
| 5047 | } |
| 5048 | wstr = PyUnicode_AsWideCharString(unicode, NULL); |
| 5049 | if (wstr == NULL) { |
| 5050 | return NULL; |
| 5051 | } |
| 5052 | _Py_error_handler error_handler = _Py_GetErrorHandler(errors); |
| 5053 | |
| 5054 | char *str = NULL; |
| 5055 | size_t error_pos; |
| 5056 | const char *reason = NULL; |
| 5057 | int ret = _Py_EncodeLocaleEx(wstr, |
| 5058 | &str, &error_pos, &reason, |
| 5059 | current_locale, error_handler); |
| 5060 | PyMem_Free(wstr); |
| 5061 | |
| 5062 | switch(ret) { |
| 5063 | case 0: |
| 5064 | res = PyBytes_FromString(str); |
| 5065 | PyMem_RawFree(str); |
| 5066 | break; |
| 5067 | case -1: |
| 5068 | PyErr_NoMemory(); |
| 5069 | break; |
| 5070 | case -2: |
| 5071 | PyErr_Format(PyExc_RuntimeError, "encode error: pos=%zu, reason=%s", |
| 5072 | error_pos, reason); |
| 5073 | break; |
| 5074 | case -3: |
| 5075 | PyErr_SetString(PyExc_ValueError, "unsupported error handler"); |
| 5076 | break; |
| 5077 | default: |
| 5078 | PyErr_SetString(PyExc_ValueError, "unknow error code"); |
| 5079 | break; |
| 5080 | } |
| 5081 | return res; |
| 5082 | } |
| 5083 | |
| 5084 | |
| 5085 | static PyObject * |
| 5086 | decode_locale_ex(PyObject *self, PyObject *args) |
| 5087 | { |
| 5088 | char *str; |
| 5089 | int current_locale = 0; |
| 5090 | PyObject *res = NULL; |
| 5091 | const char *errors = NULL; |
| 5092 | |
| 5093 | if (!PyArg_ParseTuple(args, "y|is", &str, ¤t_locale, &errors)) { |
| 5094 | return NULL; |
| 5095 | } |
| 5096 | _Py_error_handler error_handler = _Py_GetErrorHandler(errors); |
| 5097 | |
| 5098 | wchar_t *wstr = NULL; |
| 5099 | size_t wlen = 0; |
| 5100 | const char *reason = NULL; |
| 5101 | int ret = _Py_DecodeLocaleEx(str, |
| 5102 | &wstr, &wlen, &reason, |
| 5103 | current_locale, error_handler); |
| 5104 | |
| 5105 | switch(ret) { |
| 5106 | case 0: |
| 5107 | res = PyUnicode_FromWideChar(wstr, wlen); |
| 5108 | PyMem_RawFree(wstr); |
| 5109 | break; |
| 5110 | case -1: |
| 5111 | PyErr_NoMemory(); |
| 5112 | break; |
| 5113 | case -2: |
| 5114 | PyErr_Format(PyExc_RuntimeError, "decode error: pos=%zu, reason=%s", |
| 5115 | wlen, reason); |
| 5116 | break; |
| 5117 | case -3: |
| 5118 | PyErr_SetString(PyExc_ValueError, "unsupported error handler"); |
| 5119 | break; |
| 5120 | default: |
| 5121 | PyErr_SetString(PyExc_ValueError, "unknow error code"); |
| 5122 | break; |
| 5123 | } |
| 5124 | return res; |
| 5125 | } |
| 5126 | |
| 5127 | |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5128 | #ifdef Py_REF_DEBUG |
| 5129 | static PyObject * |
| 5130 | negative_refcount(PyObject *self, PyObject *Py_UNUSED(args)) |
| 5131 | { |
| 5132 | PyObject *obj = PyUnicode_FromString("negative_refcount"); |
| 5133 | if (obj == NULL) { |
| 5134 | return NULL; |
| 5135 | } |
| 5136 | assert(Py_REFCNT(obj) == 1); |
| 5137 | |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 5138 | Py_SET_REFCNT(obj, 0); |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5139 | /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */ |
| 5140 | Py_DECREF(obj); |
| 5141 | |
| 5142 | Py_RETURN_NONE; |
| 5143 | } |
| 5144 | #endif |
| 5145 | |
| 5146 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5147 | static PyObject* |
| 5148 | test_write_unraisable_exc(PyObject *self, PyObject *args) |
| 5149 | { |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5150 | PyObject *exc, *err_msg, *obj; |
| 5151 | if (!PyArg_ParseTuple(args, "OOO", &exc, &err_msg, &obj)) { |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5152 | return NULL; |
| 5153 | } |
| 5154 | |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5155 | const char *err_msg_utf8; |
| 5156 | if (err_msg != Py_None) { |
| 5157 | err_msg_utf8 = PyUnicode_AsUTF8(err_msg); |
| 5158 | if (err_msg_utf8 == NULL) { |
| 5159 | return NULL; |
| 5160 | } |
| 5161 | } |
| 5162 | else { |
| 5163 | err_msg_utf8 = NULL; |
| 5164 | } |
| 5165 | |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5166 | PyErr_SetObject((PyObject *)Py_TYPE(exc), exc); |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 5167 | _PyErr_WriteUnraisableMsg(err_msg_utf8, obj); |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5168 | Py_RETURN_NONE; |
| 5169 | } |
| 5170 | |
| 5171 | |
Sergey Fedoseev | 92709a2 | 2019-09-09 21:28:34 +0500 | [diff] [blame] | 5172 | static PyObject * |
| 5173 | sequence_getitem(PyObject *self, PyObject *args) |
| 5174 | { |
| 5175 | PyObject *seq; |
| 5176 | Py_ssize_t i; |
| 5177 | if (!PyArg_ParseTuple(args, "On", &seq, &i)) { |
| 5178 | return NULL; |
| 5179 | } |
| 5180 | return PySequence_GetItem(seq, i); |
| 5181 | } |
| 5182 | |
| 5183 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5184 | /* Functions for testing C calling conventions (METH_*) are named meth_*, |
| 5185 | * e.g. "meth_varargs" for METH_VARARGS. |
| 5186 | * |
| 5187 | * They all return a tuple of their C-level arguments, with None instead |
| 5188 | * of NULL and Python tuples instead of C arrays. |
| 5189 | */ |
| 5190 | |
| 5191 | |
| 5192 | static PyObject* |
| 5193 | _null_to_none(PyObject* obj) |
| 5194 | { |
| 5195 | if (obj == NULL) { |
| 5196 | Py_RETURN_NONE; |
| 5197 | } |
| 5198 | Py_INCREF(obj); |
| 5199 | return obj; |
| 5200 | } |
| 5201 | |
| 5202 | static PyObject* |
| 5203 | meth_varargs(PyObject* self, PyObject* args) |
| 5204 | { |
| 5205 | return Py_BuildValue("NO", _null_to_none(self), args); |
| 5206 | } |
| 5207 | |
| 5208 | static PyObject* |
| 5209 | meth_varargs_keywords(PyObject* self, PyObject* args, PyObject* kwargs) |
| 5210 | { |
| 5211 | return Py_BuildValue("NON", _null_to_none(self), args, _null_to_none(kwargs)); |
| 5212 | } |
| 5213 | |
| 5214 | static PyObject* |
| 5215 | meth_o(PyObject* self, PyObject* obj) |
| 5216 | { |
| 5217 | return Py_BuildValue("NO", _null_to_none(self), obj); |
| 5218 | } |
| 5219 | |
| 5220 | static PyObject* |
| 5221 | meth_noargs(PyObject* self, PyObject* ignored) |
| 5222 | { |
| 5223 | return _null_to_none(self); |
| 5224 | } |
| 5225 | |
| 5226 | static PyObject* |
| 5227 | _fastcall_to_tuple(PyObject* const* args, Py_ssize_t nargs) |
| 5228 | { |
| 5229 | PyObject *tuple = PyTuple_New(nargs); |
| 5230 | if (tuple == NULL) { |
| 5231 | return NULL; |
| 5232 | } |
| 5233 | for (Py_ssize_t i=0; i < nargs; i++) { |
| 5234 | Py_INCREF(args[i]); |
| 5235 | PyTuple_SET_ITEM(tuple, i, args[i]); |
| 5236 | } |
| 5237 | return tuple; |
| 5238 | } |
| 5239 | |
| 5240 | static PyObject* |
| 5241 | meth_fastcall(PyObject* self, PyObject* const* args, Py_ssize_t nargs) |
| 5242 | { |
| 5243 | return Py_BuildValue( |
| 5244 | "NN", _null_to_none(self), _fastcall_to_tuple(args, nargs) |
| 5245 | ); |
| 5246 | } |
| 5247 | |
| 5248 | static PyObject* |
| 5249 | meth_fastcall_keywords(PyObject* self, PyObject* const* args, |
| 5250 | Py_ssize_t nargs, PyObject* kwargs) |
| 5251 | { |
| 5252 | PyObject *pyargs = _fastcall_to_tuple(args, nargs); |
| 5253 | if (pyargs == NULL) { |
| 5254 | return NULL; |
| 5255 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame^] | 5256 | PyObject *pykwargs = PyObject_Vectorcall((PyObject*)&PyDict_Type, |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5257 | args + nargs, 0, kwargs); |
| 5258 | return Py_BuildValue("NNN", _null_to_none(self), pyargs, pykwargs); |
| 5259 | } |
| 5260 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 5261 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5262 | {"raise_exception", raise_exception, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5263 | {"raise_memoryerror", raise_memoryerror, METH_NOARGS}, |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 5264 | {"set_errno", set_errno, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5265 | {"test_config", test_config, METH_NOARGS}, |
| 5266 | {"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS}, |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 5267 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
Paul Ganssle | 04af5b1 | 2018-01-24 17:29:30 -0500 | [diff] [blame] | 5268 | {"datetime_check_date", datetime_check_date, METH_VARARGS}, |
| 5269 | {"datetime_check_time", datetime_check_time, METH_VARARGS}, |
| 5270 | {"datetime_check_datetime", datetime_check_datetime, METH_VARARGS}, |
| 5271 | {"datetime_check_delta", datetime_check_delta, METH_VARARGS}, |
| 5272 | {"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS}, |
| 5273 | {"make_timezones_capi", make_timezones_capi, METH_NOARGS}, |
Paul Ganssle | a049f57 | 2018-02-22 15:15:32 -0500 | [diff] [blame] | 5274 | {"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS}, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 5275 | {"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS}, |
Edison A | 98ff4d5 | 2019-05-17 13:28:42 -0700 | [diff] [blame] | 5276 | {"get_date_fromdate", get_date_fromdate, METH_VARARGS}, |
| 5277 | {"get_datetime_fromdateandtime", get_datetime_fromdateandtime, METH_VARARGS}, |
| 5278 | {"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS}, |
| 5279 | {"get_time_fromtime", get_time_fromtime, METH_VARARGS}, |
| 5280 | {"get_time_fromtimeandfold", get_time_fromtimeandfold, METH_VARARGS}, |
| 5281 | {"get_delta_fromdsu", get_delta_fromdsu, METH_VARARGS}, |
Paul Ganssle | 4d8c8c0 | 2019-04-27 15:39:40 -0400 | [diff] [blame] | 5282 | {"get_date_fromtimestamp", get_date_fromtimestamp, METH_VARARGS}, |
| 5283 | {"get_datetime_fromtimestamp", get_datetime_fromtimestamp, METH_VARARGS}, |
Joannah Nanjekye | 2c5fb17 | 2019-08-29 09:54:46 -0300 | [diff] [blame] | 5284 | {"PyDateTime_GET", test_PyDateTime_GET, METH_O}, |
| 5285 | {"PyDateTime_DATE_GET", test_PyDateTime_DATE_GET, METH_O}, |
| 5286 | {"PyDateTime_TIME_GET", test_PyDateTime_TIME_GET, METH_O}, |
| 5287 | {"PyDateTime_DELTA_GET", test_PyDateTime_DELTA_GET, METH_O}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5288 | {"test_list_api", test_list_api, METH_NOARGS}, |
| 5289 | {"test_dict_iteration", test_dict_iteration, METH_NOARGS}, |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 5290 | {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 5291 | {"dict_hassplittable", dict_hassplittable, METH_O}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5292 | {"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS}, |
| 5293 | {"test_long_api", test_long_api, METH_NOARGS}, |
| 5294 | {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS}, |
| 5295 | {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS}, |
| 5296 | {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS}, |
| 5297 | {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS}, |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 5298 | {"test_structseq_newtype_doesnt_leak", |
| 5299 | test_structseq_newtype_doesnt_leak, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5300 | {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS}, |
| 5301 | {"test_long_and_overflow", test_long_and_overflow, METH_NOARGS}, |
| 5302 | {"test_long_as_double", test_long_as_double, METH_NOARGS}, |
| 5303 | {"test_long_as_size_t", test_long_as_size_t, METH_NOARGS}, |
Zackery Spytz | dc24765 | 2019-06-06 14:39:23 -0600 | [diff] [blame] | 5304 | {"test_long_as_unsigned_long_long_mask", |
| 5305 | test_long_as_unsigned_long_long_mask, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5306 | {"test_long_numbits", test_long_numbits, METH_NOARGS}, |
| 5307 | {"test_k_code", test_k_code, METH_NOARGS}, |
| 5308 | {"test_empty_argparse", test_empty_argparse, METH_NOARGS}, |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 5309 | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5310 | {"test_null_strings", test_null_strings, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5312 | {"test_with_docstring", test_with_docstring, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5313 | PyDoc_STR("This is a pretty normal docstring.")}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5314 | {"test_string_to_double", test_string_to_double, METH_NOARGS}, |
| 5315 | {"test_unicode_compare_with_ascii", test_unicode_compare_with_ascii, |
| 5316 | METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5317 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 5318 | {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 5319 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 5320 | {"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] | 5321 | #endif |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 5322 | {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, |
Joannah Nanjekye | 9e66aba | 2019-08-20 11:46:36 -0300 | [diff] [blame] | 5323 | {"PyBuffer_SizeFromFormat", test_PyBuffer_SizeFromFormat, METH_VARARGS}, |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 5324 | {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 5325 | {"get_args", get_args, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5326 | {"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5327 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5328 | {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5329 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5330 | {"getargs_keyword_only", (PyCFunction)(void(*)(void))getargs_keyword_only, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 5331 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 5332 | {"getargs_positional_only_and_keywords", |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5333 | (PyCFunction)(void(*)(void))getargs_positional_only_and_keywords, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 5334 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 5336 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 5337 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 5338 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 5339 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 5340 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 5341 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 5342 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 5343 | {"getargs_n", getargs_n, METH_VARARGS}, |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 5344 | {"getargs_p", getargs_p, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5345 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 5346 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 5347 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5348 | {"test_long_long_and_overflow",test_long_long_and_overflow, METH_NOARGS}, |
| 5349 | {"test_L_code", test_L_code, METH_NOARGS}, |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 5350 | {"getargs_f", getargs_f, METH_VARARGS}, |
| 5351 | {"getargs_d", getargs_d, METH_VARARGS}, |
| 5352 | {"getargs_D", getargs_D, METH_VARARGS}, |
| 5353 | {"getargs_S", getargs_S, METH_VARARGS}, |
| 5354 | {"getargs_Y", getargs_Y, METH_VARARGS}, |
| 5355 | {"getargs_U", getargs_U, METH_VARARGS}, |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 5356 | {"getargs_c", getargs_c, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 5357 | {"getargs_C", getargs_C, METH_VARARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 5358 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 5359 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 5360 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 5361 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 5362 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 5363 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 5364 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 5365 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 5366 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 5367 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 5368 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 5369 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 5370 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 5371 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 5372 | {"getargs_es", getargs_es, METH_VARARGS}, |
| 5373 | {"getargs_et", getargs_et, METH_VARARGS}, |
| 5374 | {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 5375 | {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5376 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5377 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5378 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5379 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5380 | {"test_s_code", test_s_code, METH_NOARGS}, |
| 5381 | {"test_u_code", test_u_code, METH_NOARGS}, |
| 5382 | {"test_Z_code", test_Z_code, METH_NOARGS}, |
| 5383 | {"test_widechar", test_widechar, METH_NOARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 5384 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 5385 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
Serhiy Storchaka | cc16423 | 2016-10-02 21:29:26 +0300 | [diff] [blame] | 5386 | {"unicode_asucs4", unicode_asucs4, METH_VARARGS}, |
Hai Shi | 5623ac8 | 2019-07-20 02:56:23 -0500 | [diff] [blame] | 5387 | {"unicode_asutf8", unicode_asutf8, METH_VARARGS}, |
| 5388 | {"unicode_asutf8andsize", unicode_asutf8andsize, METH_VARARGS}, |
Xiang Zhang | b211068 | 2016-12-20 22:52:33 +0800 | [diff] [blame] | 5389 | {"unicode_findchar", unicode_findchar, METH_VARARGS}, |
Serhiy Storchaka | 9c0e1f8 | 2016-10-08 22:45:38 +0300 | [diff] [blame] | 5390 | {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 5391 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 5392 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 5393 | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5394 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5395 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5396 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5397 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 5398 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5399 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 5400 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 5401 | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 5402 | {"argparsing", argparsing, METH_VARARGS}, |
| 5403 | {"code_newempty", code_newempty, METH_VARARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5404 | {"make_exception_with_doc", (PyCFunction)(void(*)(void))make_exception_with_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5405 | METH_VARARGS | METH_KEYWORDS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5406 | {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer, |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 5407 | METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5408 | {"crash_no_current_thread", crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 5409 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 5410 | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
| 5411 | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 5412 | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 5413 | {"with_tp_del", with_tp_del, METH_VARARGS}, |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 5414 | {"create_cfunction", create_cfunction, METH_NOARGS}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5415 | {"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS}, |
| 5416 | {"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS}, |
| 5417 | {"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS}, |
| 5418 | {"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS}, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 5419 | {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS, |
| 5420 | PyDoc_STR("set_nomemory(start:int, stop:int = 0)")}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 5421 | {"remove_mem_hooks", remove_mem_hooks, METH_NOARGS, |
xdegaye | 85f6430 | 2017-07-01 14:14:45 +0200 | [diff] [blame] | 5422 | PyDoc_STR("Remove memory hooks.")}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5423 | {"no_docstring", |
| 5424 | (PyCFunction)test_with_docstring, METH_NOARGS}, |
| 5425 | {"docstring_empty", |
| 5426 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5427 | docstring_empty}, |
| 5428 | {"docstring_no_signature", |
| 5429 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5430 | docstring_no_signature}, |
| 5431 | {"docstring_with_invalid_signature", |
| 5432 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5433 | docstring_with_invalid_signature}, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 5434 | {"docstring_with_invalid_signature2", |
| 5435 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5436 | docstring_with_invalid_signature2}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5437 | {"docstring_with_signature", |
| 5438 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5439 | docstring_with_signature}, |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 5440 | {"docstring_with_signature_but_no_doc", |
| 5441 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5442 | docstring_with_signature_but_no_doc}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5443 | {"docstring_with_signature_and_extra_newlines", |
| 5444 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5445 | docstring_with_signature_and_extra_newlines}, |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 5446 | {"docstring_with_signature_with_defaults", |
| 5447 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 5448 | docstring_with_signature_with_defaults}, |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 5449 | {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
| 5450 | PyDoc_STR("set_error_class(error_class) -> None")}, |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 5451 | {"pymarshal_write_long_to_file", |
| 5452 | pymarshal_write_long_to_file, METH_VARARGS}, |
| 5453 | {"pymarshal_write_object_to_file", |
| 5454 | pymarshal_write_object_to_file, METH_VARARGS}, |
| 5455 | {"pymarshal_read_short_from_file", |
| 5456 | pymarshal_read_short_from_file, METH_VARARGS}, |
| 5457 | {"pymarshal_read_long_from_file", |
| 5458 | pymarshal_read_long_from_file, METH_VARARGS}, |
| 5459 | {"pymarshal_read_last_object_from_file", |
| 5460 | pymarshal_read_last_object_from_file, METH_VARARGS}, |
| 5461 | {"pymarshal_read_object_from_file", |
| 5462 | pymarshal_read_object_from_file, METH_VARARGS}, |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 5463 | {"return_null_without_error", |
| 5464 | return_null_without_error, METH_NOARGS}, |
| 5465 | {"return_result_with_error", |
| 5466 | return_result_with_error, METH_NOARGS}, |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 5467 | {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS}, |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 5468 | {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS}, |
| 5469 | {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 5470 | {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 5471 | #ifdef HAVE_CLOCK_GETTIME |
| 5472 | {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, |
| 5473 | #endif |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 5474 | {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS}, |
| 5475 | {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS}, |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 5476 | {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 5477 | {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS}, |
| 5478 | {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 5479 | {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 5480 | {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS}, |
Victor Stinner | 6876257 | 2019-10-07 18:42:01 +0200 | [diff] [blame] | 5481 | {"check_pyobject_null_is_freed", check_pyobject_null_is_freed, METH_NOARGS}, |
Victor Stinner | 3bf0f3a | 2019-06-07 16:22:21 +0200 | [diff] [blame] | 5482 | {"check_pyobject_uninitialized_is_freed", check_pyobject_uninitialized_is_freed, METH_NOARGS}, |
| 5483 | {"check_pyobject_forbidden_bytes_is_freed", check_pyobject_forbidden_bytes_is_freed, METH_NOARGS}, |
| 5484 | {"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS}, |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 5485 | {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 5486 | {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, |
| 5487 | {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, |
| 5488 | {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 5489 | {"dict_get_version", dict_get_version, METH_VARARGS}, |
Nathaniel J. Smith | ab4413a | 2017-05-17 13:33:23 -0700 | [diff] [blame] | 5490 | {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS}, |
Victor Stinner | 3b5cf85 | 2017-06-09 16:48:45 +0200 | [diff] [blame] | 5491 | {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS}, |
| 5492 | {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS}, |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 5493 | {"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS}, |
| 5494 | {"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS}, |
Victor Stinner | 64fa449 | 2017-07-10 14:37:49 +0200 | [diff] [blame] | 5495 | {"stack_pointer", stack_pointer, METH_NOARGS}, |
Victor Stinner | 7b7c6dc | 2017-08-10 12:37:39 +0200 | [diff] [blame] | 5496 | #ifdef W_STOPCODE |
| 5497 | {"W_STOPCODE", py_w_stopcode, METH_VARARGS}, |
| 5498 | #endif |
Oren Milman | 0ccc0f6 | 2017-10-08 11:17:46 +0300 | [diff] [blame] | 5499 | {"get_mapping_keys", get_mapping_keys, METH_O}, |
| 5500 | {"get_mapping_values", get_mapping_values, METH_O}, |
| 5501 | {"get_mapping_items", get_mapping_items, METH_O}, |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 5502 | {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS}, |
Yury Selivanov | f23746a | 2018-01-22 19:11:18 -0500 | [diff] [blame] | 5503 | {"hamt", new_hamt, METH_NOARGS}, |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 5504 | {"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL}, |
Victor Stinner | 3d4226a | 2018-08-29 22:21:32 +0200 | [diff] [blame] | 5505 | {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS}, |
| 5506 | {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS}, |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 5507 | #ifdef Py_REF_DEBUG |
| 5508 | {"negative_refcount", negative_refcount, METH_NOARGS}, |
| 5509 | #endif |
Victor Stinner | ef9d9b6 | 2019-05-22 11:28:22 +0200 | [diff] [blame] | 5510 | {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS}, |
Sergey Fedoseev | 92709a2 | 2019-09-09 21:28:34 +0500 | [diff] [blame] | 5511 | {"sequence_getitem", sequence_getitem, METH_VARARGS}, |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 5512 | {"meth_varargs", meth_varargs, METH_VARARGS}, |
| 5513 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, |
| 5514 | {"meth_o", meth_o, METH_O}, |
| 5515 | {"meth_noargs", meth_noargs, METH_NOARGS}, |
| 5516 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, |
| 5517 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5518 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 5519 | }; |
| 5520 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 5521 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 5522 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5523 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5524 | char bool_member; |
| 5525 | char byte_member; |
| 5526 | unsigned char ubyte_member; |
| 5527 | short short_member; |
| 5528 | unsigned short ushort_member; |
| 5529 | int int_member; |
| 5530 | unsigned int uint_member; |
| 5531 | long long_member; |
| 5532 | unsigned long ulong_member; |
| 5533 | Py_ssize_t pyssizet_member; |
| 5534 | float float_member; |
| 5535 | double double_member; |
| 5536 | char inplace_member[6]; |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 5537 | long long longlong_member; |
| 5538 | unsigned long long ulonglong_member; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5539 | } all_structmembers; |
| 5540 | |
| 5541 | typedef struct { |
| 5542 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5543 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5544 | } test_structmembers; |
| 5545 | |
| 5546 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 5548 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 5549 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 5550 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 5551 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 5552 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 5553 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 5554 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 5555 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 5556 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 5557 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 5558 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 5559 | {"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] | 5560 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 5561 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5562 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5563 | }; |
| 5564 | |
| 5565 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 5566 | static PyObject * |
| 5567 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 5568 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5569 | static char *keywords[] = { |
| 5570 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 5571 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 5572 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5573 | "T_LONGLONG", "T_ULONGLONG", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5574 | NULL}; |
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 5575 | static const char fmt[] = "|bbBhHiIlknfds#LK"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5576 | test_structmembers *ob; |
| 5577 | const char *s = NULL; |
| 5578 | Py_ssize_t string_len = 0; |
| 5579 | ob = PyObject_New(test_structmembers, type); |
| 5580 | if (ob == NULL) |
| 5581 | return NULL; |
| 5582 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 5583 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 5584 | &ob->structmembers.bool_member, |
| 5585 | &ob->structmembers.byte_member, |
| 5586 | &ob->structmembers.ubyte_member, |
| 5587 | &ob->structmembers.short_member, |
| 5588 | &ob->structmembers.ushort_member, |
| 5589 | &ob->structmembers.int_member, |
| 5590 | &ob->structmembers.uint_member, |
| 5591 | &ob->structmembers.long_member, |
| 5592 | &ob->structmembers.ulong_member, |
| 5593 | &ob->structmembers.pyssizet_member, |
| 5594 | &ob->structmembers.float_member, |
| 5595 | &ob->structmembers.double_member, |
| 5596 | &s, &string_len |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5597 | , &ob->structmembers.longlong_member, |
| 5598 | &ob->structmembers.ulonglong_member |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5599 | )) { |
| 5600 | Py_DECREF(ob); |
| 5601 | return NULL; |
| 5602 | } |
| 5603 | if (s != NULL) { |
| 5604 | if (string_len > 5) { |
| 5605 | Py_DECREF(ob); |
| 5606 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 5607 | return NULL; |
| 5608 | } |
| 5609 | strcpy(ob->structmembers.inplace_member, s); |
| 5610 | } |
| 5611 | else { |
| 5612 | strcpy(ob->structmembers.inplace_member, ""); |
| 5613 | } |
| 5614 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 5617 | static void |
| 5618 | test_structmembers_free(PyObject *ob) |
| 5619 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5620 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 5624 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5625 | "test_structmembersType", |
| 5626 | sizeof(test_structmembers), /* tp_basicsize */ |
| 5627 | 0, /* tp_itemsize */ |
| 5628 | test_structmembers_free, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5629 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | 0, /* tp_getattr */ |
| 5631 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5632 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5633 | 0, /* tp_repr */ |
| 5634 | 0, /* tp_as_number */ |
| 5635 | 0, /* tp_as_sequence */ |
| 5636 | 0, /* tp_as_mapping */ |
| 5637 | 0, /* tp_hash */ |
| 5638 | 0, /* tp_call */ |
| 5639 | 0, /* tp_str */ |
| 5640 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5641 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5642 | 0, /* tp_as_buffer */ |
| 5643 | 0, /* tp_flags */ |
| 5644 | "Type containing all structmember types", |
| 5645 | 0, /* traverseproc tp_traverse */ |
| 5646 | 0, /* tp_clear */ |
| 5647 | 0, /* tp_richcompare */ |
| 5648 | 0, /* tp_weaklistoffset */ |
| 5649 | 0, /* tp_iter */ |
| 5650 | 0, /* tp_iternext */ |
| 5651 | 0, /* tp_methods */ |
| 5652 | test_members, /* tp_members */ |
| 5653 | 0, |
| 5654 | 0, |
| 5655 | 0, |
| 5656 | 0, |
| 5657 | 0, |
| 5658 | 0, |
| 5659 | 0, |
| 5660 | 0, |
| 5661 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5662 | }; |
| 5663 | |
| 5664 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5665 | typedef struct { |
| 5666 | PyObject_HEAD |
| 5667 | } matmulObject; |
| 5668 | |
| 5669 | static PyObject * |
| 5670 | matmulType_matmul(PyObject *self, PyObject *other) |
| 5671 | { |
| 5672 | return Py_BuildValue("(sOO)", "matmul", self, other); |
| 5673 | } |
| 5674 | |
| 5675 | static PyObject * |
| 5676 | matmulType_imatmul(PyObject *self, PyObject *other) |
| 5677 | { |
| 5678 | return Py_BuildValue("(sOO)", "imatmul", self, other); |
| 5679 | } |
| 5680 | |
| 5681 | static void |
| 5682 | matmulType_dealloc(PyObject *self) |
| 5683 | { |
Zachary Ware | 420dc56 | 2014-04-23 13:51:27 -0500 | [diff] [blame] | 5684 | Py_TYPE(self)->tp_free(self); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | static PyNumberMethods matmulType_as_number = { |
| 5688 | 0, /* nb_add */ |
| 5689 | 0, /* nb_subtract */ |
| 5690 | 0, /* nb_multiply */ |
| 5691 | 0, /* nb_remainde r*/ |
| 5692 | 0, /* nb_divmod */ |
| 5693 | 0, /* nb_power */ |
| 5694 | 0, /* nb_negative */ |
| 5695 | 0, /* tp_positive */ |
| 5696 | 0, /* tp_absolute */ |
| 5697 | 0, /* tp_bool */ |
| 5698 | 0, /* nb_invert */ |
| 5699 | 0, /* nb_lshift */ |
| 5700 | 0, /* nb_rshift */ |
| 5701 | 0, /* nb_and */ |
| 5702 | 0, /* nb_xor */ |
| 5703 | 0, /* nb_or */ |
| 5704 | 0, /* nb_int */ |
| 5705 | 0, /* nb_reserved */ |
| 5706 | 0, /* nb_float */ |
| 5707 | 0, /* nb_inplace_add */ |
| 5708 | 0, /* nb_inplace_subtract */ |
| 5709 | 0, /* nb_inplace_multiply */ |
| 5710 | 0, /* nb_inplace_remainder */ |
| 5711 | 0, /* nb_inplace_power */ |
| 5712 | 0, /* nb_inplace_lshift */ |
| 5713 | 0, /* nb_inplace_rshift */ |
| 5714 | 0, /* nb_inplace_and */ |
| 5715 | 0, /* nb_inplace_xor */ |
| 5716 | 0, /* nb_inplace_or */ |
| 5717 | 0, /* nb_floor_divide */ |
| 5718 | 0, /* nb_true_divide */ |
| 5719 | 0, /* nb_inplace_floor_divide */ |
| 5720 | 0, /* nb_inplace_true_divide */ |
| 5721 | 0, /* nb_index */ |
| 5722 | matmulType_matmul, /* nb_matrix_multiply */ |
| 5723 | matmulType_imatmul /* nb_matrix_inplace_multiply */ |
| 5724 | }; |
| 5725 | |
| 5726 | static PyTypeObject matmulType = { |
| 5727 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5728 | "matmulType", |
| 5729 | sizeof(matmulObject), /* tp_basicsize */ |
| 5730 | 0, /* tp_itemsize */ |
| 5731 | matmulType_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5732 | 0, /* tp_vectorcall_offset */ |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5733 | 0, /* tp_getattr */ |
| 5734 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5735 | 0, /* tp_as_async */ |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 5736 | 0, /* tp_repr */ |
| 5737 | &matmulType_as_number, /* tp_as_number */ |
| 5738 | 0, /* tp_as_sequence */ |
| 5739 | 0, /* tp_as_mapping */ |
| 5740 | 0, /* tp_hash */ |
| 5741 | 0, /* tp_call */ |
| 5742 | 0, /* tp_str */ |
| 5743 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5744 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5745 | 0, /* tp_as_buffer */ |
| 5746 | 0, /* tp_flags */ |
| 5747 | "C level type with matrix operations defined", |
| 5748 | 0, /* traverseproc tp_traverse */ |
| 5749 | 0, /* tp_clear */ |
| 5750 | 0, /* tp_richcompare */ |
| 5751 | 0, /* tp_weaklistoffset */ |
| 5752 | 0, /* tp_iter */ |
| 5753 | 0, /* tp_iternext */ |
| 5754 | 0, /* tp_methods */ |
| 5755 | 0, /* tp_members */ |
| 5756 | 0, |
| 5757 | 0, |
| 5758 | 0, |
| 5759 | 0, |
| 5760 | 0, |
| 5761 | 0, |
| 5762 | 0, |
| 5763 | 0, |
| 5764 | PyType_GenericNew, /* tp_new */ |
| 5765 | PyObject_Del, /* tp_free */ |
| 5766 | }; |
| 5767 | |
Zackery Spytz | c7f803b | 2019-05-31 03:46:36 -0600 | [diff] [blame] | 5768 | typedef struct { |
| 5769 | PyObject_HEAD |
| 5770 | } ipowObject; |
| 5771 | |
| 5772 | static PyObject * |
| 5773 | ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod) |
| 5774 | { |
| 5775 | return Py_BuildValue("OO", other, mod); |
| 5776 | } |
| 5777 | |
| 5778 | static PyNumberMethods ipowType_as_number = { |
| 5779 | .nb_inplace_power = ipowType_ipow |
| 5780 | }; |
| 5781 | |
| 5782 | static PyTypeObject ipowType = { |
| 5783 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5784 | .tp_name = "ipowType", |
| 5785 | .tp_basicsize = sizeof(ipowObject), |
| 5786 | .tp_as_number = &ipowType_as_number, |
| 5787 | .tp_new = PyType_GenericNew |
| 5788 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5789 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5790 | typedef struct { |
| 5791 | PyObject_HEAD |
| 5792 | PyObject *ao_iterator; |
| 5793 | } awaitObject; |
| 5794 | |
| 5795 | |
| 5796 | static PyObject * |
| 5797 | awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5798 | { |
| 5799 | PyObject *v; |
| 5800 | awaitObject *ao; |
| 5801 | |
| 5802 | if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v)) |
| 5803 | return NULL; |
| 5804 | |
| 5805 | ao = (awaitObject *)type->tp_alloc(type, 0); |
| 5806 | if (ao == NULL) { |
| 5807 | return NULL; |
| 5808 | } |
| 5809 | |
| 5810 | Py_INCREF(v); |
| 5811 | ao->ao_iterator = v; |
| 5812 | |
| 5813 | return (PyObject *)ao; |
| 5814 | } |
| 5815 | |
| 5816 | |
| 5817 | static void |
| 5818 | awaitObject_dealloc(awaitObject *ao) |
| 5819 | { |
| 5820 | Py_CLEAR(ao->ao_iterator); |
| 5821 | Py_TYPE(ao)->tp_free(ao); |
| 5822 | } |
| 5823 | |
| 5824 | |
| 5825 | static PyObject * |
| 5826 | awaitObject_await(awaitObject *ao) |
| 5827 | { |
| 5828 | Py_INCREF(ao->ao_iterator); |
| 5829 | return ao->ao_iterator; |
| 5830 | } |
| 5831 | |
| 5832 | static PyAsyncMethods awaitType_as_async = { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 5833 | (unaryfunc)awaitObject_await, /* am_await */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5834 | 0, /* am_aiter */ |
| 5835 | 0 /* am_anext */ |
| 5836 | }; |
| 5837 | |
| 5838 | |
| 5839 | static PyTypeObject awaitType = { |
| 5840 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5841 | "awaitType", |
| 5842 | sizeof(awaitObject), /* tp_basicsize */ |
| 5843 | 0, /* tp_itemsize */ |
| 5844 | (destructor)awaitObject_dealloc, /* destructor tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5845 | 0, /* tp_vectorcall_offset */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5846 | 0, /* tp_getattr */ |
| 5847 | 0, /* tp_setattr */ |
| 5848 | &awaitType_as_async, /* tp_as_async */ |
| 5849 | 0, /* tp_repr */ |
| 5850 | 0, /* tp_as_number */ |
| 5851 | 0, /* tp_as_sequence */ |
| 5852 | 0, /* tp_as_mapping */ |
| 5853 | 0, /* tp_hash */ |
| 5854 | 0, /* tp_call */ |
| 5855 | 0, /* tp_str */ |
| 5856 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 5857 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 5858 | 0, /* tp_as_buffer */ |
| 5859 | 0, /* tp_flags */ |
| 5860 | "C level type with tp_as_async", |
| 5861 | 0, /* traverseproc tp_traverse */ |
| 5862 | 0, /* tp_clear */ |
| 5863 | 0, /* tp_richcompare */ |
| 5864 | 0, /* tp_weaklistoffset */ |
| 5865 | 0, /* tp_iter */ |
| 5866 | 0, /* tp_iternext */ |
| 5867 | 0, /* tp_methods */ |
| 5868 | 0, /* tp_members */ |
| 5869 | 0, |
| 5870 | 0, |
| 5871 | 0, |
| 5872 | 0, |
| 5873 | 0, |
| 5874 | 0, |
| 5875 | 0, |
| 5876 | 0, |
| 5877 | awaitObject_new, /* tp_new */ |
| 5878 | PyObject_Del, /* tp_free */ |
| 5879 | }; |
| 5880 | |
| 5881 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5882 | static int recurse_infinitely_error_init(PyObject *, PyObject *, PyObject *); |
| 5883 | |
| 5884 | static PyTypeObject PyRecursingInfinitelyError_Type = { |
| 5885 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5886 | "RecursingInfinitelyError", /* tp_name */ |
| 5887 | sizeof(PyBaseExceptionObject), /* tp_basicsize */ |
| 5888 | 0, /* tp_itemsize */ |
| 5889 | 0, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5890 | 0, /* tp_vectorcall_offset */ |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5891 | 0, /* tp_getattr */ |
| 5892 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5893 | 0, /* tp_as_async */ |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 5894 | 0, /* tp_repr */ |
| 5895 | 0, /* tp_as_number */ |
| 5896 | 0, /* tp_as_sequence */ |
| 5897 | 0, /* tp_as_mapping */ |
| 5898 | 0, /* tp_hash */ |
| 5899 | 0, /* tp_call */ |
| 5900 | 0, /* tp_str */ |
| 5901 | 0, /* tp_getattro */ |
| 5902 | 0, /* tp_setattro */ |
| 5903 | 0, /* tp_as_buffer */ |
| 5904 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5905 | "Instantiating this exception starts infinite recursion.", /* tp_doc */ |
| 5906 | 0, /* tp_traverse */ |
| 5907 | 0, /* tp_clear */ |
| 5908 | 0, /* tp_richcompare */ |
| 5909 | 0, /* tp_weaklistoffset */ |
| 5910 | 0, /* tp_iter */ |
| 5911 | 0, /* tp_iternext */ |
| 5912 | 0, /* tp_methods */ |
| 5913 | 0, /* tp_members */ |
| 5914 | 0, /* tp_getset */ |
| 5915 | 0, /* tp_base */ |
| 5916 | 0, /* tp_dict */ |
| 5917 | 0, /* tp_descr_get */ |
| 5918 | 0, /* tp_descr_set */ |
| 5919 | 0, /* tp_dictoffset */ |
| 5920 | (initproc)recurse_infinitely_error_init, /* tp_init */ |
| 5921 | 0, /* tp_alloc */ |
| 5922 | 0, /* tp_new */ |
| 5923 | }; |
| 5924 | |
| 5925 | static int |
| 5926 | recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 5927 | { |
| 5928 | PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type; |
| 5929 | |
| 5930 | /* Instantiating this exception starts infinite recursion. */ |
| 5931 | Py_INCREF(type); |
| 5932 | PyErr_SetObject(type, NULL); |
| 5933 | return -1; |
| 5934 | } |
| 5935 | |
| 5936 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5937 | /* Test bpo-35983: create a subclass of "list" which checks that instances |
| 5938 | * are not deallocated twice */ |
| 5939 | |
| 5940 | typedef struct { |
| 5941 | PyListObject list; |
| 5942 | int deallocated; |
| 5943 | } MyListObject; |
| 5944 | |
| 5945 | static PyObject * |
| 5946 | MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 5947 | { |
| 5948 | PyObject* op = PyList_Type.tp_new(type, args, kwds); |
| 5949 | ((MyListObject*)op)->deallocated = 0; |
| 5950 | return op; |
| 5951 | } |
| 5952 | |
| 5953 | void |
| 5954 | MyList_dealloc(MyListObject* op) |
| 5955 | { |
| 5956 | if (op->deallocated) { |
| 5957 | /* We cannot raise exceptions here but we still want the testsuite |
| 5958 | * to fail when we hit this */ |
| 5959 | Py_FatalError("MyList instance deallocated twice"); |
| 5960 | } |
| 5961 | op->deallocated = 1; |
| 5962 | PyList_Type.tp_dealloc((PyObject *)op); |
| 5963 | } |
| 5964 | |
| 5965 | static PyTypeObject MyList_Type = { |
| 5966 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5967 | "MyList", |
| 5968 | sizeof(MyListObject), |
| 5969 | 0, |
| 5970 | (destructor)MyList_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5971 | 0, /* tp_vectorcall_offset */ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5972 | 0, /* tp_getattr */ |
| 5973 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5974 | 0, /* tp_as_async */ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 5975 | 0, /* tp_repr */ |
| 5976 | 0, /* tp_as_number */ |
| 5977 | 0, /* tp_as_sequence */ |
| 5978 | 0, /* tp_as_mapping */ |
| 5979 | 0, /* tp_hash */ |
| 5980 | 0, /* tp_call */ |
| 5981 | 0, /* tp_str */ |
| 5982 | 0, /* tp_getattro */ |
| 5983 | 0, /* tp_setattro */ |
| 5984 | 0, /* tp_as_buffer */ |
| 5985 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5986 | 0, /* tp_doc */ |
| 5987 | 0, /* tp_traverse */ |
| 5988 | 0, /* tp_clear */ |
| 5989 | 0, /* tp_richcompare */ |
| 5990 | 0, /* tp_weaklistoffset */ |
| 5991 | 0, /* tp_iter */ |
| 5992 | 0, /* tp_iternext */ |
| 5993 | 0, /* tp_methods */ |
| 5994 | 0, /* tp_members */ |
| 5995 | 0, /* tp_getset */ |
| 5996 | 0, /* &PyList_Type */ /* tp_base */ |
| 5997 | 0, /* tp_dict */ |
| 5998 | 0, /* tp_descr_get */ |
| 5999 | 0, /* tp_descr_set */ |
| 6000 | 0, /* tp_dictoffset */ |
| 6001 | 0, /* tp_init */ |
| 6002 | 0, /* tp_alloc */ |
| 6003 | MyList_new, /* tp_new */ |
| 6004 | }; |
| 6005 | |
| 6006 | |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6007 | /* Test PEP 560 */ |
| 6008 | |
| 6009 | typedef struct { |
| 6010 | PyObject_HEAD |
| 6011 | PyObject *item; |
| 6012 | } PyGenericAliasObject; |
| 6013 | |
| 6014 | static void |
| 6015 | generic_alias_dealloc(PyGenericAliasObject *self) |
| 6016 | { |
| 6017 | Py_CLEAR(self->item); |
Victor Stinner | e860089 | 2018-01-17 23:08:18 +0100 | [diff] [blame] | 6018 | Py_TYPE(self)->tp_free((PyObject *)self); |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6019 | } |
| 6020 | |
| 6021 | static PyObject * |
| 6022 | generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases) |
| 6023 | { |
| 6024 | return PyTuple_Pack(1, self->item); |
| 6025 | } |
| 6026 | |
| 6027 | static PyMethodDef generic_alias_methods[] = { |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 6028 | {"__mro_entries__", (PyCFunction)(void(*)(void))generic_alias_mro_entries, METH_O, NULL}, |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6029 | {NULL} /* sentinel */ |
| 6030 | }; |
| 6031 | |
Benjamin Peterson | 97ae32c | 2018-07-03 22:39:09 -0700 | [diff] [blame] | 6032 | static PyTypeObject GenericAlias_Type = { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6033 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6034 | "GenericAlias", |
| 6035 | sizeof(PyGenericAliasObject), |
| 6036 | 0, |
| 6037 | .tp_dealloc = (destructor)generic_alias_dealloc, |
| 6038 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6039 | .tp_methods = generic_alias_methods, |
| 6040 | }; |
| 6041 | |
| 6042 | static PyObject * |
| 6043 | generic_alias_new(PyObject *item) |
| 6044 | { |
| 6045 | PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type); |
| 6046 | if (o == NULL) { |
| 6047 | return NULL; |
| 6048 | } |
| 6049 | Py_INCREF(item); |
| 6050 | o->item = item; |
| 6051 | return (PyObject*) o; |
| 6052 | } |
| 6053 | |
| 6054 | typedef struct { |
| 6055 | PyObject_HEAD |
| 6056 | } PyGenericObject; |
| 6057 | |
| 6058 | static PyObject * |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 6059 | generic_class_getitem(PyObject *type, PyObject *item) |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6060 | { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6061 | return generic_alias_new(item); |
| 6062 | } |
| 6063 | |
| 6064 | static PyMethodDef generic_methods[] = { |
Serhiy Storchaka | ce5b0e9 | 2018-01-05 00:21:41 +0200 | [diff] [blame] | 6065 | {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL}, |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6066 | {NULL} /* sentinel */ |
| 6067 | }; |
| 6068 | |
Benjamin Peterson | 97ae32c | 2018-07-03 22:39:09 -0700 | [diff] [blame] | 6069 | static PyTypeObject Generic_Type = { |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6070 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6071 | "Generic", |
| 6072 | sizeof(PyGenericObject), |
| 6073 | 0, |
| 6074 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6075 | .tp_methods = generic_methods, |
| 6076 | }; |
| 6077 | |
| 6078 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6079 | /* Test PEP 590 */ |
| 6080 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6081 | typedef struct { |
| 6082 | PyObject_HEAD |
| 6083 | vectorcallfunc vectorcall; |
| 6084 | } MethodDescriptorObject; |
| 6085 | |
| 6086 | static PyObject * |
| 6087 | MethodDescriptor_vectorcall(PyObject *callable, PyObject *const *args, |
| 6088 | size_t nargsf, PyObject *kwnames) |
| 6089 | { |
| 6090 | /* True if using the vectorcall function in MethodDescriptorObject |
| 6091 | * but False for MethodDescriptor2Object */ |
| 6092 | MethodDescriptorObject *md = (MethodDescriptorObject *)callable; |
| 6093 | return PyBool_FromLong(md->vectorcall != NULL); |
| 6094 | } |
| 6095 | |
| 6096 | static PyObject * |
| 6097 | MethodDescriptor_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 6098 | { |
Petr Viktorin | e584cbf | 2019-06-03 01:08:14 +0200 | [diff] [blame] | 6099 | MethodDescriptorObject *op = (MethodDescriptorObject *)type->tp_alloc(type, 0); |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6100 | op->vectorcall = MethodDescriptor_vectorcall; |
| 6101 | return (PyObject *)op; |
| 6102 | } |
| 6103 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6104 | static PyObject * |
| 6105 | func_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 6106 | { |
| 6107 | if (obj == Py_None || obj == NULL) { |
| 6108 | Py_INCREF(func); |
| 6109 | return func; |
| 6110 | } |
| 6111 | return PyMethod_New(func, obj); |
| 6112 | } |
| 6113 | |
| 6114 | static PyObject * |
| 6115 | nop_descr_get(PyObject *func, PyObject *obj, PyObject *type) |
| 6116 | { |
| 6117 | Py_INCREF(func); |
| 6118 | return func; |
| 6119 | } |
| 6120 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6121 | static PyObject * |
| 6122 | call_return_args(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6123 | { |
| 6124 | Py_INCREF(args); |
| 6125 | return args; |
| 6126 | } |
| 6127 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6128 | static PyTypeObject MethodDescriptorBase_Type = { |
| 6129 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6130 | "MethodDescriptorBase", |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6131 | sizeof(MethodDescriptorObject), |
| 6132 | .tp_new = MethodDescriptor_new, |
| 6133 | .tp_call = PyVectorcall_Call, |
| 6134 | .tp_vectorcall_offset = offsetof(MethodDescriptorObject, vectorcall), |
| 6135 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame^] | 6136 | Py_TPFLAGS_METHOD_DESCRIPTOR | Py_TPFLAGS_HAVE_VECTORCALL, |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6137 | .tp_descr_get = func_descr_get, |
| 6138 | }; |
| 6139 | |
| 6140 | static PyTypeObject MethodDescriptorDerived_Type = { |
| 6141 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6142 | "MethodDescriptorDerived", |
| 6143 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6144 | }; |
| 6145 | |
| 6146 | static PyTypeObject MethodDescriptorNopGet_Type = { |
| 6147 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6148 | "MethodDescriptorNopGet", |
| 6149 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6150 | .tp_call = call_return_args, |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6151 | .tp_descr_get = nop_descr_get, |
| 6152 | }; |
| 6153 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6154 | typedef struct { |
| 6155 | MethodDescriptorObject base; |
| 6156 | vectorcallfunc vectorcall; |
| 6157 | } MethodDescriptor2Object; |
| 6158 | |
| 6159 | static PyObject * |
| 6160 | MethodDescriptor2_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 6161 | { |
| 6162 | MethodDescriptor2Object *op = PyObject_New(MethodDescriptor2Object, type); |
| 6163 | op->base.vectorcall = NULL; |
| 6164 | op->vectorcall = MethodDescriptor_vectorcall; |
| 6165 | return (PyObject *)op; |
| 6166 | } |
| 6167 | |
| 6168 | static PyTypeObject MethodDescriptor2_Type = { |
| 6169 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6170 | "MethodDescriptor2", |
| 6171 | sizeof(MethodDescriptor2Object), |
| 6172 | .tp_new = MethodDescriptor2_new, |
| 6173 | .tp_call = PyVectorcall_Call, |
| 6174 | .tp_vectorcall_offset = offsetof(MethodDescriptor2Object, vectorcall), |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame^] | 6175 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_VECTORCALL, |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6176 | }; |
| 6177 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6178 | PyDoc_STRVAR(heapgctype__doc__, |
| 6179 | "A heap type with GC, and with overridden dealloc.\n\n" |
| 6180 | "The 'value' attribute is set to 10 in __init__."); |
| 6181 | |
| 6182 | typedef struct { |
| 6183 | PyObject_HEAD |
| 6184 | int value; |
| 6185 | } HeapCTypeObject; |
| 6186 | |
| 6187 | static struct PyMemberDef heapctype_members[] = { |
| 6188 | {"value", T_INT, offsetof(HeapCTypeObject, value)}, |
| 6189 | {NULL} /* Sentinel */ |
| 6190 | }; |
| 6191 | |
| 6192 | static int |
| 6193 | heapctype_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6194 | { |
| 6195 | ((HeapCTypeObject *)self)->value = 10; |
| 6196 | return 0; |
| 6197 | } |
| 6198 | |
| 6199 | static void |
| 6200 | heapgcctype_dealloc(HeapCTypeObject *self) |
| 6201 | { |
| 6202 | PyTypeObject *tp = Py_TYPE(self); |
| 6203 | PyObject_GC_UnTrack(self); |
| 6204 | PyObject_GC_Del(self); |
| 6205 | Py_DECREF(tp); |
| 6206 | } |
| 6207 | |
| 6208 | static PyType_Slot HeapGcCType_slots[] = { |
| 6209 | {Py_tp_init, heapctype_init}, |
| 6210 | {Py_tp_members, heapctype_members}, |
| 6211 | {Py_tp_dealloc, heapgcctype_dealloc}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6212 | {Py_tp_doc, (char*)heapgctype__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6213 | {0, 0}, |
| 6214 | }; |
| 6215 | |
| 6216 | static PyType_Spec HeapGcCType_spec = { |
| 6217 | "_testcapi.HeapGcCType", |
| 6218 | sizeof(HeapCTypeObject), |
| 6219 | 0, |
| 6220 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6221 | HeapGcCType_slots |
| 6222 | }; |
| 6223 | |
| 6224 | PyDoc_STRVAR(heapctype__doc__, |
| 6225 | "A heap type without GC, but with overridden dealloc.\n\n" |
| 6226 | "The 'value' attribute is set to 10 in __init__."); |
| 6227 | |
| 6228 | static void |
| 6229 | heapctype_dealloc(HeapCTypeObject *self) |
| 6230 | { |
| 6231 | PyTypeObject *tp = Py_TYPE(self); |
| 6232 | PyObject_Del(self); |
| 6233 | Py_DECREF(tp); |
| 6234 | } |
| 6235 | |
| 6236 | static PyType_Slot HeapCType_slots[] = { |
| 6237 | {Py_tp_init, heapctype_init}, |
| 6238 | {Py_tp_members, heapctype_members}, |
| 6239 | {Py_tp_dealloc, heapctype_dealloc}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6240 | {Py_tp_doc, (char*)heapctype__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6241 | {0, 0}, |
| 6242 | }; |
| 6243 | |
| 6244 | static PyType_Spec HeapCType_spec = { |
| 6245 | "_testcapi.HeapCType", |
| 6246 | sizeof(HeapCTypeObject), |
| 6247 | 0, |
| 6248 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6249 | HeapCType_slots |
| 6250 | }; |
| 6251 | |
| 6252 | PyDoc_STRVAR(heapctypesubclass__doc__, |
| 6253 | "Subclass of HeapCType, without GC.\n\n" |
| 6254 | "__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 6255 | |
| 6256 | typedef struct { |
| 6257 | HeapCTypeObject base; |
| 6258 | int value2; |
| 6259 | } HeapCTypeSubclassObject; |
| 6260 | |
| 6261 | static int |
| 6262 | heapctypesubclass_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6263 | { |
| 6264 | /* Call __init__ of the superclass */ |
| 6265 | if (heapctype_init(self, args, kwargs) < 0) { |
| 6266 | return -1; |
| 6267 | } |
| 6268 | /* Initialize additional element */ |
| 6269 | ((HeapCTypeSubclassObject *)self)->value2 = 20; |
| 6270 | return 0; |
| 6271 | } |
| 6272 | |
| 6273 | static struct PyMemberDef heapctypesubclass_members[] = { |
| 6274 | {"value2", T_INT, offsetof(HeapCTypeSubclassObject, value2)}, |
| 6275 | {NULL} /* Sentinel */ |
| 6276 | }; |
| 6277 | |
| 6278 | static PyType_Slot HeapCTypeSubclass_slots[] = { |
| 6279 | {Py_tp_init, heapctypesubclass_init}, |
| 6280 | {Py_tp_members, heapctypesubclass_members}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6281 | {Py_tp_doc, (char*)heapctypesubclass__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6282 | {0, 0}, |
| 6283 | }; |
| 6284 | |
| 6285 | static PyType_Spec HeapCTypeSubclass_spec = { |
| 6286 | "_testcapi.HeapCTypeSubclass", |
| 6287 | sizeof(HeapCTypeSubclassObject), |
| 6288 | 0, |
| 6289 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6290 | HeapCTypeSubclass_slots |
| 6291 | }; |
| 6292 | |
| 6293 | PyDoc_STRVAR(heapctypesubclasswithfinalizer__doc__, |
| 6294 | "Subclass of HeapCType with a finalizer that reassigns __class__.\n\n" |
| 6295 | "__class__ is set to plain HeapCTypeSubclass during finalization.\n" |
| 6296 | "__init__ sets the 'value' attribute to 10 and 'value2' to 20."); |
| 6297 | |
| 6298 | static int |
| 6299 | heapctypesubclasswithfinalizer_init(PyObject *self, PyObject *args, PyObject *kwargs) |
| 6300 | { |
| 6301 | PyTypeObject *base = (PyTypeObject *)PyType_GetSlot(Py_TYPE(self), Py_tp_base); |
| 6302 | initproc base_init = PyType_GetSlot(base, Py_tp_init); |
| 6303 | base_init(self, args, kwargs); |
| 6304 | return 0; |
| 6305 | } |
| 6306 | |
| 6307 | static void |
| 6308 | heapctypesubclasswithfinalizer_finalize(PyObject *self) |
| 6309 | { |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6310 | PyObject *error_type, *error_value, *error_traceback, *m; |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6311 | PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL; |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6312 | |
| 6313 | /* Save the current exception, if any. */ |
| 6314 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 6315 | |
| 6316 | m = PyState_FindModule(&_testcapimodule); |
| 6317 | if (m == NULL) { |
| 6318 | goto cleanup_finalize; |
| 6319 | } |
| 6320 | oldtype = PyObject_GetAttrString(m, "HeapCTypeSubclassWithFinalizer"); |
| 6321 | newtype = PyObject_GetAttrString(m, "HeapCTypeSubclass"); |
| 6322 | if (oldtype == NULL || newtype == NULL) { |
| 6323 | goto cleanup_finalize; |
| 6324 | } |
| 6325 | |
| 6326 | if (PyObject_SetAttrString(self, "__class__", newtype) < 0) { |
| 6327 | goto cleanup_finalize; |
| 6328 | } |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6329 | refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype)); |
| 6330 | if (refcnt == NULL) { |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6331 | goto cleanup_finalize; |
| 6332 | } |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6333 | if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) { |
| 6334 | goto cleanup_finalize; |
| 6335 | } |
| 6336 | Py_DECREF(refcnt); |
| 6337 | refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype)); |
| 6338 | if (refcnt == NULL) { |
| 6339 | goto cleanup_finalize; |
| 6340 | } |
| 6341 | if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) { |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6342 | goto cleanup_finalize; |
| 6343 | } |
| 6344 | |
| 6345 | cleanup_finalize: |
| 6346 | Py_XDECREF(oldtype); |
| 6347 | Py_XDECREF(newtype); |
Eddie Elizondo | a67ac2f | 2019-09-13 12:48:03 -0400 | [diff] [blame] | 6348 | Py_XDECREF(refcnt); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6349 | |
| 6350 | /* Restore the saved exception. */ |
| 6351 | PyErr_Restore(error_type, error_value, error_traceback); |
| 6352 | } |
| 6353 | |
| 6354 | static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = { |
| 6355 | {Py_tp_init, heapctypesubclasswithfinalizer_init}, |
| 6356 | {Py_tp_members, heapctypesubclass_members}, |
| 6357 | {Py_tp_finalize, heapctypesubclasswithfinalizer_finalize}, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6358 | {Py_tp_doc, (char*)heapctypesubclasswithfinalizer__doc__}, |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6359 | {0, 0}, |
| 6360 | }; |
| 6361 | |
| 6362 | static PyType_Spec HeapCTypeSubclassWithFinalizer_spec = { |
| 6363 | "_testcapi.HeapCTypeSubclassWithFinalizer", |
| 6364 | sizeof(HeapCTypeSubclassObject), |
| 6365 | 0, |
| 6366 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE, |
| 6367 | HeapCTypeSubclassWithFinalizer_slots |
| 6368 | }; |
| 6369 | |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6370 | typedef struct { |
| 6371 | PyObject_HEAD |
| 6372 | PyObject *dict; |
| 6373 | } HeapCTypeWithDictObject; |
| 6374 | |
| 6375 | static void |
| 6376 | heapctypewithdict_dealloc(HeapCTypeWithDictObject* self) |
| 6377 | { |
| 6378 | |
| 6379 | PyTypeObject *tp = Py_TYPE(self); |
| 6380 | Py_XDECREF(self->dict); |
| 6381 | PyObject_DEL(self); |
| 6382 | Py_DECREF(tp); |
| 6383 | } |
| 6384 | |
| 6385 | static PyGetSetDef heapctypewithdict_getsetlist[] = { |
| 6386 | {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict}, |
| 6387 | {NULL} /* Sentinel */ |
| 6388 | }; |
| 6389 | |
| 6390 | static struct PyMemberDef heapctypewithdict_members[] = { |
| 6391 | {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
| 6392 | {"__dictoffset__", T_PYSSIZET, offsetof(HeapCTypeWithDictObject, dict), READONLY}, |
| 6393 | {NULL} /* Sentinel */ |
| 6394 | }; |
| 6395 | |
| 6396 | static PyType_Slot HeapCTypeWithDict_slots[] = { |
| 6397 | {Py_tp_members, heapctypewithdict_members}, |
| 6398 | {Py_tp_getset, heapctypewithdict_getsetlist}, |
| 6399 | {Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 6400 | {0, 0}, |
| 6401 | }; |
| 6402 | |
| 6403 | static PyType_Spec HeapCTypeWithDict_spec = { |
| 6404 | "_testcapi.HeapCTypeWithDict", |
| 6405 | sizeof(HeapCTypeWithDictObject), |
| 6406 | 0, |
| 6407 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6408 | HeapCTypeWithDict_slots |
| 6409 | }; |
| 6410 | |
| 6411 | static struct PyMemberDef heapctypewithnegativedict_members[] = { |
| 6412 | {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)}, |
Victor Stinner | aca8c40 | 2019-09-30 21:14:26 +0200 | [diff] [blame] | 6413 | {"__dictoffset__", T_PYSSIZET, -(Py_ssize_t)sizeof(void*), READONLY}, |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6414 | {NULL} /* Sentinel */ |
| 6415 | }; |
| 6416 | |
| 6417 | static PyType_Slot HeapCTypeWithNegativeDict_slots[] = { |
| 6418 | {Py_tp_members, heapctypewithnegativedict_members}, |
| 6419 | {Py_tp_getset, heapctypewithdict_getsetlist}, |
| 6420 | {Py_tp_dealloc, heapctypewithdict_dealloc}, |
| 6421 | {0, 0}, |
| 6422 | }; |
| 6423 | |
| 6424 | static PyType_Spec HeapCTypeWithNegativeDict_spec = { |
| 6425 | "_testcapi.HeapCTypeWithNegativeDict", |
| 6426 | sizeof(HeapCTypeWithDictObject), |
| 6427 | 0, |
| 6428 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6429 | HeapCTypeWithNegativeDict_slots |
| 6430 | }; |
| 6431 | |
| 6432 | typedef struct { |
| 6433 | PyObject_HEAD |
| 6434 | PyObject *weakreflist; |
| 6435 | } HeapCTypeWithWeakrefObject; |
| 6436 | |
| 6437 | static struct PyMemberDef heapctypewithweakref_members[] = { |
| 6438 | {"weakreflist", T_OBJECT, offsetof(HeapCTypeWithWeakrefObject, weakreflist)}, |
| 6439 | {"__weaklistoffset__", T_PYSSIZET, |
| 6440 | offsetof(HeapCTypeWithWeakrefObject, weakreflist), READONLY}, |
| 6441 | {NULL} /* Sentinel */ |
| 6442 | }; |
| 6443 | |
| 6444 | static void |
| 6445 | heapctypewithweakref_dealloc(HeapCTypeWithWeakrefObject* self) |
| 6446 | { |
| 6447 | |
| 6448 | PyTypeObject *tp = Py_TYPE(self); |
| 6449 | if (self->weakreflist != NULL) |
| 6450 | PyObject_ClearWeakRefs((PyObject *) self); |
| 6451 | Py_XDECREF(self->weakreflist); |
| 6452 | PyObject_DEL(self); |
| 6453 | Py_DECREF(tp); |
| 6454 | } |
| 6455 | |
| 6456 | static PyType_Slot HeapCTypeWithWeakref_slots[] = { |
| 6457 | {Py_tp_members, heapctypewithweakref_members}, |
| 6458 | {Py_tp_dealloc, heapctypewithweakref_dealloc}, |
| 6459 | {0, 0}, |
| 6460 | }; |
| 6461 | |
| 6462 | static PyType_Spec HeapCTypeWithWeakref_spec = { |
| 6463 | "_testcapi.HeapCTypeWithWeakref", |
| 6464 | sizeof(HeapCTypeWithWeakrefObject), |
| 6465 | 0, |
| 6466 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6467 | HeapCTypeWithWeakref_slots |
| 6468 | }; |
| 6469 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6470 | static PyMethodDef meth_instance_methods[] = { |
| 6471 | {"meth_varargs", meth_varargs, METH_VARARGS}, |
| 6472 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS}, |
| 6473 | {"meth_o", meth_o, METH_O}, |
| 6474 | {"meth_noargs", meth_noargs, METH_NOARGS}, |
| 6475 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL}, |
| 6476 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS}, |
| 6477 | {NULL, NULL} /* sentinel */ |
| 6478 | }; |
| 6479 | |
| 6480 | |
| 6481 | static PyTypeObject MethInstance_Type = { |
| 6482 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6483 | "MethInstance", |
| 6484 | sizeof(PyObject), |
| 6485 | .tp_new = PyType_GenericNew, |
| 6486 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6487 | .tp_methods = meth_instance_methods, |
Petr Viktorin | 5e9caee | 2019-09-12 10:12:53 +0100 | [diff] [blame] | 6488 | .tp_doc = (char*)PyDoc_STR( |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6489 | "Class with normal (instance) methods to test calling conventions"), |
| 6490 | }; |
| 6491 | |
| 6492 | static PyMethodDef meth_class_methods[] = { |
| 6493 | {"meth_varargs", meth_varargs, METH_VARARGS|METH_CLASS}, |
| 6494 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_CLASS}, |
| 6495 | {"meth_o", meth_o, METH_O|METH_CLASS}, |
| 6496 | {"meth_noargs", meth_noargs, METH_NOARGS|METH_CLASS}, |
| 6497 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_CLASS}, |
| 6498 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_CLASS}, |
| 6499 | {NULL, NULL} /* sentinel */ |
| 6500 | }; |
| 6501 | |
| 6502 | |
| 6503 | static PyTypeObject MethClass_Type = { |
| 6504 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6505 | "MethClass", |
| 6506 | sizeof(PyObject), |
| 6507 | .tp_new = PyType_GenericNew, |
| 6508 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6509 | .tp_methods = meth_class_methods, |
| 6510 | .tp_doc = PyDoc_STR( |
| 6511 | "Class with class methods to test calling conventions"), |
| 6512 | }; |
| 6513 | |
| 6514 | static PyMethodDef meth_static_methods[] = { |
| 6515 | {"meth_varargs", meth_varargs, METH_VARARGS|METH_STATIC}, |
| 6516 | {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_STATIC}, |
| 6517 | {"meth_o", meth_o, METH_O|METH_STATIC}, |
| 6518 | {"meth_noargs", meth_noargs, METH_NOARGS|METH_STATIC}, |
| 6519 | {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_STATIC}, |
| 6520 | {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_STATIC}, |
| 6521 | {NULL, NULL} /* sentinel */ |
| 6522 | }; |
| 6523 | |
| 6524 | |
| 6525 | static PyTypeObject MethStatic_Type = { |
| 6526 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6527 | "MethStatic", |
| 6528 | sizeof(PyObject), |
| 6529 | .tp_new = PyType_GenericNew, |
| 6530 | .tp_flags = Py_TPFLAGS_DEFAULT, |
| 6531 | .tp_methods = meth_static_methods, |
| 6532 | .tp_doc = PyDoc_STR( |
| 6533 | "Class with static methods to test calling conventions"), |
| 6534 | }; |
| 6535 | |
Neil Schemenauer | 392a13b | 2019-10-15 20:56:48 -0700 | [diff] [blame] | 6536 | /* ContainerNoGC -- a simple container without GC methods */ |
| 6537 | |
| 6538 | typedef struct { |
| 6539 | PyObject_HEAD |
| 6540 | PyObject *value; |
| 6541 | } ContainerNoGCobject; |
| 6542 | |
| 6543 | static PyObject * |
| 6544 | ContainerNoGC_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 6545 | { |
| 6546 | PyObject *value; |
| 6547 | char *names[] = {"value", NULL}; |
| 6548 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", names, &value)) { |
| 6549 | return NULL; |
| 6550 | } |
| 6551 | PyObject *self = type->tp_alloc(type, 0); |
| 6552 | if (self == NULL) { |
| 6553 | return NULL; |
| 6554 | } |
| 6555 | Py_INCREF(value); |
| 6556 | ((ContainerNoGCobject *)self)->value = value; |
| 6557 | return self; |
| 6558 | } |
| 6559 | |
| 6560 | static void |
| 6561 | ContainerNoGC_dealloc(ContainerNoGCobject *self) |
| 6562 | { |
| 6563 | Py_DECREF(self->value); |
| 6564 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6565 | } |
| 6566 | |
| 6567 | static PyMemberDef ContainerNoGC_members[] = { |
| 6568 | {"value", T_OBJECT, offsetof(ContainerNoGCobject, value), READONLY, |
| 6569 | PyDoc_STR("a container value for test purposes")}, |
| 6570 | {0} |
| 6571 | }; |
| 6572 | |
| 6573 | static PyTypeObject ContainerNoGC_type = { |
| 6574 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6575 | "_testcapi.ContainerNoGC", |
| 6576 | sizeof(ContainerNoGCobject), |
| 6577 | .tp_dealloc = (destructor)ContainerNoGC_dealloc, |
| 6578 | .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 6579 | .tp_members = ContainerNoGC_members, |
| 6580 | .tp_new = ContainerNoGC_new, |
| 6581 | }; |
| 6582 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6583 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6584 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6585 | PyModuleDef_HEAD_INIT, |
| 6586 | "_testcapi", |
| 6587 | NULL, |
| 6588 | -1, |
| 6589 | TestMethods, |
| 6590 | NULL, |
| 6591 | NULL, |
| 6592 | NULL, |
| 6593 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6594 | }; |
| 6595 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 6596 | /* Per PEP 489, this module will not be converted to multi-phase initialization |
| 6597 | */ |
| 6598 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 6599 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 6600 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6602 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6604 | m = PyModule_Create(&_testcapimodule); |
| 6605 | if (m == NULL) |
| 6606 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6607 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 6608 | Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type); |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 6609 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 6610 | Py_SET_TYPE(&test_structmembersType, &PyType_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6611 | Py_INCREF(&test_structmembersType); |
| 6612 | /* don't use a name starting with "test", since we don't want |
| 6613 | test_capi to automatically call this */ |
| 6614 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 6615 | if (PyType_Ready(&matmulType) < 0) |
| 6616 | return NULL; |
| 6617 | Py_INCREF(&matmulType); |
| 6618 | PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); |
Zackery Spytz | c7f803b | 2019-05-31 03:46:36 -0600 | [diff] [blame] | 6619 | if (PyType_Ready(&ipowType) < 0) { |
| 6620 | return NULL; |
| 6621 | } |
| 6622 | Py_INCREF(&ipowType); |
| 6623 | PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 6624 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 6625 | if (PyType_Ready(&awaitType) < 0) |
| 6626 | return NULL; |
| 6627 | Py_INCREF(&awaitType); |
| 6628 | PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType); |
| 6629 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 6630 | MyList_Type.tp_base = &PyList_Type; |
| 6631 | if (PyType_Ready(&MyList_Type) < 0) |
| 6632 | return NULL; |
| 6633 | Py_INCREF(&MyList_Type); |
| 6634 | PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type); |
| 6635 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 6636 | if (PyType_Ready(&MethodDescriptorBase_Type) < 0) |
| 6637 | return NULL; |
| 6638 | Py_INCREF(&MethodDescriptorBase_Type); |
| 6639 | PyModule_AddObject(m, "MethodDescriptorBase", (PyObject *)&MethodDescriptorBase_Type); |
| 6640 | |
| 6641 | MethodDescriptorDerived_Type.tp_base = &MethodDescriptorBase_Type; |
| 6642 | if (PyType_Ready(&MethodDescriptorDerived_Type) < 0) |
| 6643 | return NULL; |
| 6644 | Py_INCREF(&MethodDescriptorDerived_Type); |
| 6645 | PyModule_AddObject(m, "MethodDescriptorDerived", (PyObject *)&MethodDescriptorDerived_Type); |
| 6646 | |
| 6647 | MethodDescriptorNopGet_Type.tp_base = &MethodDescriptorBase_Type; |
| 6648 | if (PyType_Ready(&MethodDescriptorNopGet_Type) < 0) |
| 6649 | return NULL; |
| 6650 | Py_INCREF(&MethodDescriptorNopGet_Type); |
| 6651 | PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type); |
| 6652 | |
Jeroen Demeyer | 735e8af | 2019-05-30 12:43:19 +0200 | [diff] [blame] | 6653 | MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type; |
| 6654 | if (PyType_Ready(&MethodDescriptor2_Type) < 0) |
| 6655 | return NULL; |
| 6656 | Py_INCREF(&MethodDescriptor2_Type); |
| 6657 | PyModule_AddObject(m, "MethodDescriptor2", (PyObject *)&MethodDescriptor2_Type); |
| 6658 | |
Serhiy Storchaka | 45700fb | 2017-12-16 11:25:56 +0200 | [diff] [blame] | 6659 | if (PyType_Ready(&GenericAlias_Type) < 0) |
| 6660 | return NULL; |
| 6661 | Py_INCREF(&GenericAlias_Type); |
| 6662 | PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type); |
| 6663 | |
| 6664 | if (PyType_Ready(&Generic_Type) < 0) |
| 6665 | return NULL; |
| 6666 | Py_INCREF(&Generic_Type); |
| 6667 | PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type); |
| 6668 | |
Petr Viktorin | f958377 | 2019-09-10 12:21:09 +0100 | [diff] [blame] | 6669 | if (PyType_Ready(&MethInstance_Type) < 0) |
| 6670 | return NULL; |
| 6671 | Py_INCREF(&MethInstance_Type); |
| 6672 | PyModule_AddObject(m, "MethInstance", (PyObject *)&MethInstance_Type); |
| 6673 | |
| 6674 | if (PyType_Ready(&MethClass_Type) < 0) |
| 6675 | return NULL; |
| 6676 | Py_INCREF(&MethClass_Type); |
| 6677 | PyModule_AddObject(m, "MethClass", (PyObject *)&MethClass_Type); |
| 6678 | |
| 6679 | if (PyType_Ready(&MethStatic_Type) < 0) |
| 6680 | return NULL; |
| 6681 | Py_INCREF(&MethStatic_Type); |
| 6682 | PyModule_AddObject(m, "MethStatic", (PyObject *)&MethStatic_Type); |
| 6683 | |
xdegaye | 56d1f5c | 2017-10-26 15:09:06 +0200 | [diff] [blame] | 6684 | PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception; |
| 6685 | if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) { |
| 6686 | return NULL; |
| 6687 | } |
| 6688 | Py_INCREF(&PyRecursingInfinitelyError_Type); |
| 6689 | PyModule_AddObject(m, "RecursingInfinitelyError", |
| 6690 | (PyObject *)&PyRecursingInfinitelyError_Type); |
| 6691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6692 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 6693 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 6694 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 6695 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 6696 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 6697 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 6698 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 6699 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 6700 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 6701 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 6702 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 6703 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 6704 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 6705 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 6706 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 6707 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
Sergey Fedoseev | 1f9f69d | 2019-12-05 19:55:28 +0500 | [diff] [blame] | 6708 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX)); |
| 6709 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN)); |
| 6710 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6711 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 6712 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
| 6713 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 6714 | PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6715 | Py_INCREF(&PyInstanceMethod_Type); |
| 6716 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 6717 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 6718 | PyModule_AddIntConstant(m, "the_number_three", 3); |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6719 | PyObject *v; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6720 | #ifdef WITH_PYMALLOC |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6721 | v = Py_True; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6722 | #else |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6723 | v = Py_False; |
Victor Stinner | 5d39e04 | 2017-11-29 17:20:38 +0100 | [diff] [blame] | 6724 | #endif |
Victor Stinner | 310e2d2 | 2019-11-22 10:58:00 +0100 | [diff] [blame] | 6725 | Py_INCREF(v); |
| 6726 | PyModule_AddObject(m, "WITH_PYMALLOC", v); |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 6727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6728 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 6729 | Py_INCREF(TestError); |
| 6730 | PyModule_AddObject(m, "error", TestError); |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6731 | |
| 6732 | PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec); |
| 6733 | if (HeapGcCType == NULL) { |
| 6734 | return NULL; |
| 6735 | } |
| 6736 | PyModule_AddObject(m, "HeapGcCType", HeapGcCType); |
| 6737 | |
| 6738 | PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec); |
| 6739 | if (HeapCType == NULL) { |
| 6740 | return NULL; |
| 6741 | } |
| 6742 | PyObject *subclass_bases = PyTuple_Pack(1, HeapCType); |
| 6743 | if (subclass_bases == NULL) { |
| 6744 | return NULL; |
| 6745 | } |
| 6746 | PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases); |
| 6747 | if (HeapCTypeSubclass == NULL) { |
| 6748 | return NULL; |
| 6749 | } |
| 6750 | Py_DECREF(subclass_bases); |
| 6751 | PyModule_AddObject(m, "HeapCTypeSubclass", HeapCTypeSubclass); |
| 6752 | |
Eddie Elizondo | 3368f3c | 2019-09-19 09:29:05 -0700 | [diff] [blame] | 6753 | PyObject *HeapCTypeWithDict = PyType_FromSpec(&HeapCTypeWithDict_spec); |
| 6754 | if (HeapCTypeWithDict == NULL) { |
| 6755 | return NULL; |
| 6756 | } |
| 6757 | PyModule_AddObject(m, "HeapCTypeWithDict", HeapCTypeWithDict); |
| 6758 | |
| 6759 | PyObject *HeapCTypeWithNegativeDict = PyType_FromSpec(&HeapCTypeWithNegativeDict_spec); |
| 6760 | if (HeapCTypeWithNegativeDict == NULL) { |
| 6761 | return NULL; |
| 6762 | } |
| 6763 | PyModule_AddObject(m, "HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict); |
| 6764 | |
| 6765 | PyObject *HeapCTypeWithWeakref = PyType_FromSpec(&HeapCTypeWithWeakref_spec); |
| 6766 | if (HeapCTypeWithWeakref == NULL) { |
| 6767 | return NULL; |
| 6768 | } |
| 6769 | PyModule_AddObject(m, "HeapCTypeWithWeakref", HeapCTypeWithWeakref); |
| 6770 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6771 | PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass); |
| 6772 | if (subclass_with_finalizer_bases == NULL) { |
| 6773 | return NULL; |
| 6774 | } |
| 6775 | PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases( |
| 6776 | &HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases); |
| 6777 | if (HeapCTypeSubclassWithFinalizer == NULL) { |
| 6778 | return NULL; |
| 6779 | } |
| 6780 | Py_DECREF(subclass_with_finalizer_bases); |
| 6781 | PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer); |
| 6782 | |
Neil Schemenauer | 392a13b | 2019-10-15 20:56:48 -0700 | [diff] [blame] | 6783 | if (PyType_Ready(&ContainerNoGC_type) < 0) { |
| 6784 | return NULL; |
| 6785 | } |
| 6786 | Py_INCREF(&ContainerNoGC_type); |
| 6787 | if (PyModule_AddObject(m, "ContainerNoGC", |
| 6788 | (PyObject *) &ContainerNoGC_type) < 0) |
| 6789 | return NULL; |
| 6790 | |
Eddie Elizondo | ff023ed | 2019-09-11 05:17:13 -0400 | [diff] [blame] | 6791 | PyState_AddModule(m, &_testcapimodule); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6792 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 6793 | } |