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 | |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 8 | #define PY_SSIZE_T_CLEAN |
| 9 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | #include <float.h> |
| 12 | #include "structmember.h" |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 13 | #include "datetime.h" |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 14 | #include "marshal.h" |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 15 | #include <signal.h> |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 16 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 17 | #ifdef MS_WINDOWS |
Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 18 | # include <winsock2.h> /* struct timeval */ |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 19 | #endif |
| 20 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 21 | #ifdef WITH_THREAD |
| 22 | #include "pythread.h" |
| 23 | #endif /* WITH_THREAD */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | static PyObject *TestError; /* set to exception object in init */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 25 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 26 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 27 | |
| 28 | static PyObject * |
| 29 | raiseTestError(const char* test_name, const char* msg) |
| 30 | { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 31 | PyErr_Format(TestError, "%s: %s", test_name, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 35 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 36 | |
| 37 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 38 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 39 | platforms have these hardcoded. Better safe than sorry. |
| 40 | */ |
| 41 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 42 | sizeof_error(const char* fatname, const char* typname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | int expected, int got) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 44 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 45 | PyErr_Format(TestError, |
| 46 | "%s #define == %d but sizeof(%s) == %d", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | fatname, expected, typname, got); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | return (PyObject*)NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 52 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 53 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 54 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | if (FATNAME != sizeof(TYPE)) \ |
| 56 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 59 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 60 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 61 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 62 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 63 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | #undef CHECK_SIZEOF |
| 68 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | Py_INCREF(Py_None); |
| 70 | return Py_None; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 73 | static PyObject* |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 74 | test_sizeof_c_types(PyObject *self) |
| 75 | { |
Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 76 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 77 | #pragma GCC diagnostic push |
| 78 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 79 | #endif |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 80 | #define CHECK_SIZEOF(TYPE, EXPECTED) \ |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 81 | if (EXPECTED != sizeof(TYPE)) { \ |
| 82 | PyErr_Format(TestError, \ |
| 83 | "sizeof(%s) = %u instead of %u", \ |
| 84 | #TYPE, sizeof(TYPE), EXPECTED); \ |
| 85 | return (PyObject*)NULL; \ |
| 86 | } |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 87 | #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0) |
| 88 | #define CHECK_SIGNNESS(TYPE, SIGNED) \ |
| 89 | if (IS_SIGNED(TYPE) != SIGNED) { \ |
| 90 | PyErr_Format(TestError, \ |
| 91 | "%s signness is, instead of %i", \ |
| 92 | #TYPE, IS_SIGNED(TYPE), SIGNED); \ |
| 93 | return (PyObject*)NULL; \ |
| 94 | } |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 95 | |
| 96 | /* integer types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 97 | CHECK_SIZEOF(Py_UCS1, 1); |
| 98 | CHECK_SIZEOF(Py_UCS2, 2); |
| 99 | CHECK_SIZEOF(Py_UCS4, 4); |
| 100 | CHECK_SIGNNESS(Py_UCS1, 0); |
| 101 | CHECK_SIGNNESS(Py_UCS2, 0); |
| 102 | CHECK_SIGNNESS(Py_UCS4, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 103 | #ifdef HAVE_INT32_T |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 104 | CHECK_SIZEOF(PY_INT32_T, 4); |
| 105 | CHECK_SIGNNESS(PY_INT32_T, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 106 | #endif |
| 107 | #ifdef HAVE_UINT32_T |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 108 | CHECK_SIZEOF(PY_UINT32_T, 4); |
| 109 | CHECK_SIGNNESS(PY_UINT32_T, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 110 | #endif |
| 111 | #ifdef HAVE_INT64_T |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 112 | CHECK_SIZEOF(PY_INT64_T, 8); |
| 113 | CHECK_SIGNNESS(PY_INT64_T, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 114 | #endif |
| 115 | #ifdef HAVE_UINT64_T |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 116 | CHECK_SIZEOF(PY_UINT64_T, 8); |
| 117 | CHECK_SIGNNESS(PY_UINT64_T, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | /* pointer/size types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 121 | CHECK_SIZEOF(size_t, sizeof(void *)); |
| 122 | CHECK_SIGNNESS(size_t, 0); |
| 123 | CHECK_SIZEOF(Py_ssize_t, sizeof(void *)); |
| 124 | CHECK_SIGNNESS(Py_ssize_t, 1); |
| 125 | |
| 126 | CHECK_SIZEOF(Py_uintptr_t, sizeof(void *)); |
| 127 | CHECK_SIGNNESS(Py_uintptr_t, 0); |
| 128 | CHECK_SIZEOF(Py_intptr_t, sizeof(void *)); |
| 129 | CHECK_SIGNNESS(Py_intptr_t, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 130 | |
| 131 | Py_INCREF(Py_None); |
| 132 | return Py_None; |
| 133 | |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 134 | #undef IS_SIGNED |
| 135 | #undef CHECK_SIGNESS |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 136 | #undef CHECK_SIZEOF |
Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 137 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) |
Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 138 | #pragma GCC diagnostic pop |
| 139 | #endif |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 144 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | PyObject* list; |
| 147 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 150 | #define NLIST 30 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | list = PyList_New(NLIST); |
| 152 | if (list == (PyObject*)NULL) |
| 153 | return (PyObject*)NULL; |
| 154 | /* list = range(NLIST) */ |
| 155 | for (i = 0; i < NLIST; ++i) { |
| 156 | PyObject* anint = PyLong_FromLong(i); |
| 157 | if (anint == (PyObject*)NULL) { |
| 158 | Py_DECREF(list); |
| 159 | return (PyObject*)NULL; |
| 160 | } |
| 161 | PyList_SET_ITEM(list, i, anint); |
| 162 | } |
| 163 | /* list.reverse(), via PyList_Reverse() */ |
| 164 | i = PyList_Reverse(list); /* should not blow up! */ |
| 165 | if (i != 0) { |
| 166 | Py_DECREF(list); |
| 167 | return (PyObject*)NULL; |
| 168 | } |
| 169 | /* Check that list == range(29, -1, -1) now */ |
| 170 | for (i = 0; i < NLIST; ++i) { |
| 171 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 172 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 173 | PyErr_SetString(TestError, |
| 174 | "test_list_api: reverse screwed up"); |
| 175 | Py_DECREF(list); |
| 176 | return (PyObject*)NULL; |
| 177 | } |
| 178 | } |
| 179 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 180 | #undef NLIST |
| 181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | Py_INCREF(Py_None); |
| 183 | return Py_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* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 237 | test_dict_iteration(PyObject* self) |
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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | Py_INCREF(Py_None); |
| 248 | return Py_None; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 251 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 252 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 253 | * PyType_Ready if it hasn't already been called |
| 254 | */ |
| 255 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | PyVarObject_HEAD_INIT(NULL, 0) |
| 257 | "hashinheritancetester", /* Name of this type */ |
| 258 | sizeof(PyObject), /* Basic object size */ |
| 259 | 0, /* Item size for varobject */ |
| 260 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 261 | 0, /* tp_print */ |
| 262 | 0, /* tp_getattr */ |
| 263 | 0, /* tp_setattr */ |
| 264 | 0, /* tp_reserved */ |
| 265 | 0, /* tp_repr */ |
| 266 | 0, /* tp_as_number */ |
| 267 | 0, /* tp_as_sequence */ |
| 268 | 0, /* tp_as_mapping */ |
| 269 | 0, /* tp_hash */ |
| 270 | 0, /* tp_call */ |
| 271 | 0, /* tp_str */ |
| 272 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 273 | 0, /* tp_setattro */ |
| 274 | 0, /* tp_as_buffer */ |
| 275 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 276 | 0, /* tp_doc */ |
| 277 | 0, /* tp_traverse */ |
| 278 | 0, /* tp_clear */ |
| 279 | 0, /* tp_richcompare */ |
| 280 | 0, /* tp_weaklistoffset */ |
| 281 | 0, /* tp_iter */ |
| 282 | 0, /* tp_iternext */ |
| 283 | 0, /* tp_methods */ |
| 284 | 0, /* tp_members */ |
| 285 | 0, /* tp_getset */ |
| 286 | 0, /* tp_base */ |
| 287 | 0, /* tp_dict */ |
| 288 | 0, /* tp_descr_get */ |
| 289 | 0, /* tp_descr_set */ |
| 290 | 0, /* tp_dictoffset */ |
| 291 | 0, /* tp_init */ |
| 292 | 0, /* tp_alloc */ |
| 293 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | static PyObject* |
| 297 | test_lazy_hash_inheritance(PyObject* self) |
| 298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyTypeObject *type; |
| 300 | PyObject *obj; |
Antoine Pitrou | 29aad00 | 2010-10-23 19:42:38 +0000 | [diff] [blame] | 301 | Py_hash_t hash; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | if (type->tp_dict != NULL) |
| 306 | /* The type has already been initialized. This probably means |
| 307 | -R is being used. */ |
| 308 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 309 | |
| 310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | obj = PyObject_New(PyObject, type); |
| 312 | if (obj == NULL) { |
| 313 | PyErr_Clear(); |
| 314 | PyErr_SetString( |
| 315 | TestError, |
| 316 | "test_lazy_hash_inheritance: failed to create object"); |
| 317 | return NULL; |
| 318 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | if (type->tp_dict != NULL) { |
| 321 | PyErr_SetString( |
| 322 | TestError, |
| 323 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 324 | Py_DECREF(obj); |
| 325 | return NULL; |
| 326 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | hash = PyObject_Hash(obj); |
| 329 | if ((hash == -1) && PyErr_Occurred()) { |
| 330 | PyErr_Clear(); |
| 331 | PyErr_SetString( |
| 332 | TestError, |
| 333 | "test_lazy_hash_inheritance: could not hash object"); |
| 334 | Py_DECREF(obj); |
| 335 | return NULL; |
| 336 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | if (type->tp_dict == NULL) { |
| 339 | PyErr_SetString( |
| 340 | TestError, |
| 341 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 342 | Py_DECREF(obj); |
| 343 | return NULL; |
| 344 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 347 | PyErr_SetString( |
| 348 | TestError, |
| 349 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 350 | Py_DECREF(obj); |
| 351 | return NULL; |
| 352 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | Py_RETURN_NONE; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 360 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 361 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 362 | |
| 363 | Note that the meat of the test is contained in testcapi_long.h. |
| 364 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 365 | exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 366 | dependence on type names makes it impossible to use a parameterized |
| 367 | function. A giant macro would be even worse than this. A C++ template |
| 368 | would be perfect. |
| 369 | |
| 370 | The "report an error" functions are deliberately not part of the #include |
| 371 | file: if the test fails, you can set a breakpoint in the appropriate |
| 372 | error function directly, and crawl back from there in the debugger. |
| 373 | */ |
| 374 | |
| 375 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 376 | |
| 377 | static PyObject * |
| 378 | raise_test_long_error(const char* msg) |
| 379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | #define TESTNAME test_long_api_inner |
| 384 | #define TYPENAME long |
| 385 | #define F_S_TO_PY PyLong_FromLong |
| 386 | #define F_PY_TO_S PyLong_AsLong |
| 387 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 388 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 389 | |
| 390 | #include "testcapi_long.h" |
| 391 | |
| 392 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 393 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | #undef TESTNAME |
| 399 | #undef TYPENAME |
| 400 | #undef F_S_TO_PY |
| 401 | #undef F_PY_TO_S |
| 402 | #undef F_U_TO_PY |
| 403 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 404 | |
| 405 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 406 | |
| 407 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 408 | raise_test_longlong_error(const char* msg) |
| 409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | #define TESTNAME test_longlong_api_inner |
| 414 | #define TYPENAME PY_LONG_LONG |
| 415 | #define F_S_TO_PY PyLong_FromLongLong |
| 416 | #define F_PY_TO_S PyLong_AsLongLong |
| 417 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 418 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 419 | |
| 420 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 421 | |
| 422 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 423 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 428 | #undef TESTNAME |
| 429 | #undef TYPENAME |
| 430 | #undef F_S_TO_PY |
| 431 | #undef F_PY_TO_S |
| 432 | #undef F_U_TO_PY |
| 433 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 434 | |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 435 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 436 | is tested by test_long_api_inner. This test will concentrate on proper |
| 437 | handling of overflow. |
| 438 | */ |
| 439 | |
| 440 | static PyObject * |
| 441 | test_long_and_overflow(PyObject *self) |
| 442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | PyObject *num, *one, *temp; |
| 444 | long value; |
| 445 | int overflow; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | /* Test that overflow is set properly for a large value. */ |
| 448 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 449 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 450 | if (num == NULL) |
| 451 | return NULL; |
| 452 | overflow = 1234; |
| 453 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 454 | Py_DECREF(num); |
| 455 | if (value == -1 && PyErr_Occurred()) |
| 456 | return NULL; |
| 457 | if (value != -1) |
| 458 | return raiseTestError("test_long_and_overflow", |
| 459 | "return value was not set to -1"); |
| 460 | if (overflow != 1) |
| 461 | return raiseTestError("test_long_and_overflow", |
| 462 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | /* Same again, with num = LONG_MAX + 1 */ |
| 465 | num = PyLong_FromLong(LONG_MAX); |
| 466 | if (num == NULL) |
| 467 | return NULL; |
| 468 | one = PyLong_FromLong(1L); |
| 469 | if (one == NULL) { |
| 470 | Py_DECREF(num); |
| 471 | return NULL; |
| 472 | } |
| 473 | temp = PyNumber_Add(num, one); |
| 474 | Py_DECREF(one); |
| 475 | Py_DECREF(num); |
| 476 | num = temp; |
| 477 | if (num == NULL) |
| 478 | return NULL; |
| 479 | overflow = 0; |
| 480 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 481 | Py_DECREF(num); |
| 482 | if (value == -1 && PyErr_Occurred()) |
| 483 | return NULL; |
| 484 | if (value != -1) |
| 485 | return raiseTestError("test_long_and_overflow", |
| 486 | "return value was not set to -1"); |
| 487 | if (overflow != 1) |
| 488 | return raiseTestError("test_long_and_overflow", |
| 489 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | /* Test that overflow is set properly for a large negative value. */ |
| 492 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 493 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 494 | if (num == NULL) |
| 495 | return NULL; |
| 496 | overflow = 1234; |
| 497 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 498 | Py_DECREF(num); |
| 499 | if (value == -1 && PyErr_Occurred()) |
| 500 | return NULL; |
| 501 | if (value != -1) |
| 502 | return raiseTestError("test_long_and_overflow", |
| 503 | "return value was not set to -1"); |
| 504 | if (overflow != -1) |
| 505 | return raiseTestError("test_long_and_overflow", |
| 506 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | /* Same again, with num = LONG_MIN - 1 */ |
| 509 | num = PyLong_FromLong(LONG_MIN); |
| 510 | if (num == NULL) |
| 511 | return NULL; |
| 512 | one = PyLong_FromLong(1L); |
| 513 | if (one == NULL) { |
| 514 | Py_DECREF(num); |
| 515 | return NULL; |
| 516 | } |
| 517 | temp = PyNumber_Subtract(num, one); |
| 518 | Py_DECREF(one); |
| 519 | Py_DECREF(num); |
| 520 | num = temp; |
| 521 | if (num == NULL) |
| 522 | return NULL; |
| 523 | overflow = 0; |
| 524 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 525 | Py_DECREF(num); |
| 526 | if (value == -1 && PyErr_Occurred()) |
| 527 | return NULL; |
| 528 | if (value != -1) |
| 529 | return raiseTestError("test_long_and_overflow", |
| 530 | "return value was not set to -1"); |
| 531 | if (overflow != -1) |
| 532 | return raiseTestError("test_long_and_overflow", |
| 533 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | /* Test that overflow is cleared properly for small values. */ |
| 536 | num = PyLong_FromString("FF", NULL, 16); |
| 537 | if (num == NULL) |
| 538 | return NULL; |
| 539 | overflow = 1234; |
| 540 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 541 | Py_DECREF(num); |
| 542 | if (value == -1 && PyErr_Occurred()) |
| 543 | return NULL; |
| 544 | if (value != 0xFF) |
| 545 | return raiseTestError("test_long_and_overflow", |
| 546 | "expected return value 0xFF"); |
| 547 | if (overflow != 0) |
| 548 | return raiseTestError("test_long_and_overflow", |
| 549 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | num = PyLong_FromString("-FF", NULL, 16); |
| 552 | if (num == NULL) |
| 553 | return NULL; |
| 554 | overflow = 0; |
| 555 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 556 | Py_DECREF(num); |
| 557 | if (value == -1 && PyErr_Occurred()) |
| 558 | return NULL; |
| 559 | if (value != -0xFF) |
| 560 | return raiseTestError("test_long_and_overflow", |
| 561 | "expected return value 0xFF"); |
| 562 | if (overflow != 0) |
| 563 | return raiseTestError("test_long_and_overflow", |
| 564 | "overflow was set incorrectly"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | num = PyLong_FromLong(LONG_MAX); |
| 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 != LONG_MAX) |
| 575 | return raiseTestError("test_long_and_overflow", |
| 576 | "expected return value LONG_MAX"); |
| 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_FromLong(LONG_MIN); |
| 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 != LONG_MIN) |
| 590 | return raiseTestError("test_long_and_overflow", |
| 591 | "expected return value LONG_MIN"); |
| 592 | if (overflow != 0) |
| 593 | return raiseTestError("test_long_and_overflow", |
| 594 | "overflow was not cleared"); |
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 | Py_INCREF(Py_None); |
| 597 | return Py_None; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 600 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
| 601 | PY_LONG_LONG is tested by test_long_api_inner. This test will |
| 602 | concentrate on proper handling of overflow. |
| 603 | */ |
| 604 | |
| 605 | static PyObject * |
| 606 | test_long_long_and_overflow(PyObject *self) |
| 607 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | PyObject *num, *one, *temp; |
| 609 | PY_LONG_LONG value; |
| 610 | int overflow; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | /* Test that overflow is set properly for a large value. */ |
| 613 | /* num is a number larger than PY_LLONG_MAX on a typical machine. */ |
| 614 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 615 | if (num == NULL) |
| 616 | return NULL; |
| 617 | overflow = 1234; |
| 618 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 619 | Py_DECREF(num); |
| 620 | if (value == -1 && PyErr_Occurred()) |
| 621 | return NULL; |
| 622 | if (value != -1) |
| 623 | return raiseTestError("test_long_long_and_overflow", |
| 624 | "return value was not set to -1"); |
| 625 | if (overflow != 1) |
| 626 | return raiseTestError("test_long_long_and_overflow", |
| 627 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | /* Same again, with num = PY_LLONG_MAX + 1 */ |
| 630 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 631 | if (num == NULL) |
| 632 | return NULL; |
| 633 | one = PyLong_FromLong(1L); |
| 634 | if (one == NULL) { |
| 635 | Py_DECREF(num); |
| 636 | return NULL; |
| 637 | } |
| 638 | temp = PyNumber_Add(num, one); |
| 639 | Py_DECREF(one); |
| 640 | Py_DECREF(num); |
| 641 | num = temp; |
| 642 | if (num == NULL) |
| 643 | return NULL; |
| 644 | overflow = 0; |
| 645 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 646 | Py_DECREF(num); |
| 647 | if (value == -1 && PyErr_Occurred()) |
| 648 | return NULL; |
| 649 | if (value != -1) |
| 650 | return raiseTestError("test_long_long_and_overflow", |
| 651 | "return value was not set to -1"); |
| 652 | if (overflow != 1) |
| 653 | return raiseTestError("test_long_long_and_overflow", |
| 654 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | /* Test that overflow is set properly for a large negative value. */ |
| 657 | /* num is a number smaller than PY_LLONG_MIN on a typical platform */ |
| 658 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 659 | if (num == NULL) |
| 660 | return NULL; |
| 661 | overflow = 1234; |
| 662 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 663 | Py_DECREF(num); |
| 664 | if (value == -1 && PyErr_Occurred()) |
| 665 | return NULL; |
| 666 | if (value != -1) |
| 667 | return raiseTestError("test_long_long_and_overflow", |
| 668 | "return value was not set to -1"); |
| 669 | if (overflow != -1) |
| 670 | return raiseTestError("test_long_long_and_overflow", |
| 671 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | /* Same again, with num = PY_LLONG_MIN - 1 */ |
| 674 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 675 | if (num == NULL) |
| 676 | return NULL; |
| 677 | one = PyLong_FromLong(1L); |
| 678 | if (one == NULL) { |
| 679 | Py_DECREF(num); |
| 680 | return NULL; |
| 681 | } |
| 682 | temp = PyNumber_Subtract(num, one); |
| 683 | Py_DECREF(one); |
| 684 | Py_DECREF(num); |
| 685 | num = temp; |
| 686 | if (num == NULL) |
| 687 | return NULL; |
| 688 | overflow = 0; |
| 689 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 690 | Py_DECREF(num); |
| 691 | if (value == -1 && PyErr_Occurred()) |
| 692 | return NULL; |
| 693 | if (value != -1) |
| 694 | return raiseTestError("test_long_long_and_overflow", |
| 695 | "return value was not set to -1"); |
| 696 | if (overflow != -1) |
| 697 | return raiseTestError("test_long_long_and_overflow", |
| 698 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | /* Test that overflow is cleared properly for small values. */ |
| 701 | num = PyLong_FromString("FF", NULL, 16); |
| 702 | if (num == NULL) |
| 703 | return NULL; |
| 704 | overflow = 1234; |
| 705 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 706 | Py_DECREF(num); |
| 707 | if (value == -1 && PyErr_Occurred()) |
| 708 | return NULL; |
| 709 | if (value != 0xFF) |
| 710 | return raiseTestError("test_long_long_and_overflow", |
| 711 | "expected return value 0xFF"); |
| 712 | if (overflow != 0) |
| 713 | return raiseTestError("test_long_long_and_overflow", |
| 714 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | num = PyLong_FromString("-FF", NULL, 16); |
| 717 | if (num == NULL) |
| 718 | return NULL; |
| 719 | overflow = 0; |
| 720 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 721 | Py_DECREF(num); |
| 722 | if (value == -1 && PyErr_Occurred()) |
| 723 | return NULL; |
| 724 | if (value != -0xFF) |
| 725 | return raiseTestError("test_long_long_and_overflow", |
| 726 | "expected return value 0xFF"); |
| 727 | if (overflow != 0) |
| 728 | return raiseTestError("test_long_long_and_overflow", |
| 729 | "overflow was set incorrectly"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 732 | if (num == NULL) |
| 733 | return NULL; |
| 734 | overflow = 1234; |
| 735 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 736 | Py_DECREF(num); |
| 737 | if (value == -1 && PyErr_Occurred()) |
| 738 | return NULL; |
| 739 | if (value != PY_LLONG_MAX) |
| 740 | return raiseTestError("test_long_long_and_overflow", |
| 741 | "expected return value PY_LLONG_MAX"); |
| 742 | if (overflow != 0) |
| 743 | return raiseTestError("test_long_long_and_overflow", |
| 744 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 746 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 747 | if (num == NULL) |
| 748 | return NULL; |
| 749 | overflow = 0; |
| 750 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 751 | Py_DECREF(num); |
| 752 | if (value == -1 && PyErr_Occurred()) |
| 753 | return NULL; |
| 754 | if (value != PY_LLONG_MIN) |
| 755 | return raiseTestError("test_long_long_and_overflow", |
| 756 | "expected return value PY_LLONG_MIN"); |
| 757 | if (overflow != 0) |
| 758 | return raiseTestError("test_long_long_and_overflow", |
| 759 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | Py_INCREF(Py_None); |
| 762 | return Py_None; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 765 | /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that |
| 766 | non-integer arguments are handled correctly. It should be extended to |
| 767 | test overflow handling. |
| 768 | */ |
| 769 | |
| 770 | static PyObject * |
| 771 | test_long_as_size_t(PyObject *self) |
| 772 | { |
| 773 | size_t out_u; |
| 774 | Py_ssize_t out_s; |
| 775 | |
| 776 | Py_INCREF(Py_None); |
| 777 | |
| 778 | out_u = PyLong_AsSize_t(Py_None); |
| 779 | if (out_u != (size_t)-1 || !PyErr_Occurred()) |
| 780 | return raiseTestError("test_long_as_size_t", |
| 781 | "PyLong_AsSize_t(None) didn't complain"); |
| 782 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 783 | return raiseTestError("test_long_as_size_t", |
| 784 | "PyLong_AsSize_t(None) raised " |
| 785 | "something other than TypeError"); |
| 786 | PyErr_Clear(); |
| 787 | |
| 788 | out_s = PyLong_AsSsize_t(Py_None); |
| 789 | if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) |
| 790 | return raiseTestError("test_long_as_size_t", |
| 791 | "PyLong_AsSsize_t(None) didn't complain"); |
| 792 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 793 | return raiseTestError("test_long_as_size_t", |
| 794 | "PyLong_AsSsize_t(None) raised " |
| 795 | "something other than TypeError"); |
| 796 | PyErr_Clear(); |
| 797 | |
| 798 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 799 | return Py_None; |
| 800 | } |
| 801 | |
| 802 | /* Test the PyLong_AsDouble API. At present this just tests that |
| 803 | non-integer arguments are handled correctly. |
| 804 | */ |
| 805 | |
| 806 | static PyObject * |
| 807 | test_long_as_double(PyObject *self) |
| 808 | { |
| 809 | double out; |
| 810 | |
| 811 | Py_INCREF(Py_None); |
| 812 | |
| 813 | out = PyLong_AsDouble(Py_None); |
| 814 | if (out != -1.0 || !PyErr_Occurred()) |
| 815 | return raiseTestError("test_long_as_double", |
| 816 | "PyLong_AsDouble(None) didn't complain"); |
| 817 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 818 | return raiseTestError("test_long_as_double", |
| 819 | "PyLong_AsDouble(None) raised " |
| 820 | "something other than TypeError"); |
| 821 | PyErr_Clear(); |
| 822 | |
| 823 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 824 | return Py_None; |
| 825 | } |
| 826 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 827 | /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 828 | for both long and int arguments. The test may leak a little memory if |
| 829 | it fails. |
| 830 | */ |
| 831 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 832 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 833 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | PyObject *tuple, *num; |
| 835 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | tuple = PyTuple_New(1); |
| 838 | if (tuple == NULL) |
| 839 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | num = PyLong_FromLong(42); |
| 842 | if (num == NULL) |
| 843 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | value = -1; |
| 848 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 849 | return NULL; |
| 850 | if (value != 42) |
| 851 | return raiseTestError("test_L_code", |
| 852 | "L code returned wrong value for long 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | Py_DECREF(num); |
| 855 | num = PyLong_FromLong(42); |
| 856 | if (num == NULL) |
| 857 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | value = -1; |
| 862 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 863 | return NULL; |
| 864 | if (value != 42) |
| 865 | return raiseTestError("test_L_code", |
| 866 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | Py_DECREF(tuple); |
| 869 | Py_INCREF(Py_None); |
| 870 | return Py_None; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | #endif /* ifdef HAVE_LONG_LONG */ |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 874 | |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 875 | static PyObject * |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 876 | return_none(void *unused) |
| 877 | { |
| 878 | Py_RETURN_NONE; |
| 879 | } |
| 880 | |
| 881 | static PyObject * |
| 882 | raise_error(void *unused) |
| 883 | { |
| 884 | PyErr_SetNone(PyExc_ValueError); |
| 885 | return NULL; |
| 886 | } |
| 887 | |
| 888 | static int |
| 889 | test_buildvalue_N_error(const char *fmt) |
| 890 | { |
| 891 | PyObject *arg, *res; |
| 892 | |
| 893 | arg = PyList_New(0); |
| 894 | if (arg == NULL) { |
| 895 | return -1; |
| 896 | } |
| 897 | |
| 898 | Py_INCREF(arg); |
| 899 | res = Py_BuildValue(fmt, return_none, NULL, arg); |
| 900 | if (res == NULL) { |
| 901 | return -1; |
| 902 | } |
| 903 | Py_DECREF(res); |
| 904 | if (Py_REFCNT(arg) != 1) { |
| 905 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 906 | "arg was not decrefed in successful " |
| 907 | "Py_BuildValue(\"%s\")", fmt); |
| 908 | return -1; |
| 909 | } |
| 910 | |
| 911 | Py_INCREF(arg); |
| 912 | res = Py_BuildValue(fmt, raise_error, NULL, arg); |
| 913 | if (res != NULL || !PyErr_Occurred()) { |
| 914 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 915 | "Py_BuildValue(\"%s\") didn't complain", fmt); |
| 916 | return -1; |
| 917 | } |
| 918 | PyErr_Clear(); |
| 919 | if (Py_REFCNT(arg) != 1) { |
| 920 | PyErr_Format(TestError, "test_buildvalue_N: " |
| 921 | "arg was not decrefed in failed " |
| 922 | "Py_BuildValue(\"%s\")", fmt); |
| 923 | return -1; |
| 924 | } |
| 925 | Py_DECREF(arg); |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | static PyObject * |
| 930 | test_buildvalue_N(PyObject *self, PyObject *noargs) |
| 931 | { |
| 932 | PyObject *arg, *res; |
| 933 | |
| 934 | arg = PyList_New(0); |
| 935 | if (arg == NULL) { |
| 936 | return NULL; |
| 937 | } |
| 938 | Py_INCREF(arg); |
| 939 | res = Py_BuildValue("N", arg); |
| 940 | if (res == NULL) { |
| 941 | return NULL; |
| 942 | } |
| 943 | if (res != arg) { |
| 944 | return raiseTestError("test_buildvalue_N", |
| 945 | "Py_BuildValue(\"N\") returned wrong result"); |
| 946 | } |
| 947 | if (Py_REFCNT(arg) != 2) { |
| 948 | return raiseTestError("test_buildvalue_N", |
| 949 | "arg was not decrefed in Py_BuildValue(\"N\")"); |
| 950 | } |
| 951 | Py_DECREF(res); |
| 952 | Py_DECREF(arg); |
| 953 | |
| 954 | if (test_buildvalue_N_error("O&N") < 0) |
| 955 | return NULL; |
| 956 | if (test_buildvalue_N_error("(O&N)") < 0) |
| 957 | return NULL; |
| 958 | if (test_buildvalue_N_error("[O&N]") < 0) |
| 959 | return NULL; |
| 960 | if (test_buildvalue_N_error("{O&N}") < 0) |
| 961 | return NULL; |
| 962 | if (test_buildvalue_N_error("{()O&(())N}") < 0) |
| 963 | return NULL; |
| 964 | |
| 965 | Py_RETURN_NONE; |
| 966 | } |
| 967 | |
| 968 | |
| 969 | static PyObject * |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 970 | get_args(PyObject *self, PyObject *args) |
| 971 | { |
| 972 | if (args == NULL) { |
| 973 | args = Py_None; |
| 974 | } |
| 975 | Py_INCREF(args); |
| 976 | return args; |
| 977 | } |
| 978 | |
| 979 | static PyObject * |
| 980 | get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) |
| 981 | { |
| 982 | if (kwargs == NULL) { |
| 983 | kwargs = Py_None; |
| 984 | } |
| 985 | Py_INCREF(kwargs); |
| 986 | return kwargs; |
| 987 | } |
| 988 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 989 | /* Test tuple argument processing */ |
| 990 | static PyObject * |
| 991 | getargs_tuple(PyObject *self, PyObject *args) |
| 992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | int a, b, c; |
| 994 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 995 | return NULL; |
| 996 | return Py_BuildValue("iii", a, b, c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 999 | /* test PyArg_ParseTupleAndKeywords */ |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1000 | static PyObject * |
| 1001 | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1002 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1004 | static const char fmt[] = "(ii)i|(i(ii))(iii)i"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | 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] | 1006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1007 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 1008 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 1009 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 1010 | return NULL; |
| 1011 | return Py_BuildValue("iiiiiiiiii", |
| 1012 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 1013 | 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] | 1014 | } |
| 1015 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1016 | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
| 1017 | static PyObject * |
| 1018 | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1019 | { |
| 1020 | static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
| 1021 | int required = -1; |
| 1022 | int optional = -1; |
| 1023 | int keyword_only = -1; |
| 1024 | |
| 1025 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
| 1026 | &required, &optional, &keyword_only)) |
| 1027 | return NULL; |
| 1028 | return Py_BuildValue("iii", required, optional, keyword_only); |
| 1029 | } |
| 1030 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1031 | /* test PyArg_ParseTupleAndKeywords positional-only arguments */ |
| 1032 | static PyObject * |
| 1033 | getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 1034 | { |
| 1035 | static char *keywords[] = {"", "", "keyword", NULL}; |
| 1036 | int required = -1; |
| 1037 | int optional = -1; |
| 1038 | int keyword = -1; |
| 1039 | |
| 1040 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords, |
| 1041 | &required, &optional, &keyword)) |
| 1042 | return NULL; |
| 1043 | return Py_BuildValue("iii", required, optional, keyword); |
| 1044 | } |
| 1045 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1046 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 1047 | and return the result. |
| 1048 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1049 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1050 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1051 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | unsigned char value; |
| 1053 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 1054 | return NULL; |
| 1055 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1058 | static PyObject * |
| 1059 | getargs_B(PyObject *self, PyObject *args) |
| 1060 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | unsigned char value; |
| 1062 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 1063 | return NULL; |
| 1064 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1068 | getargs_h(PyObject *self, PyObject *args) |
| 1069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | short value; |
| 1071 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 1072 | return NULL; |
| 1073 | return PyLong_FromLong((long)value); |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1077 | getargs_H(PyObject *self, PyObject *args) |
| 1078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | unsigned short value; |
| 1080 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 1081 | return NULL; |
| 1082 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | static PyObject * |
| 1086 | getargs_I(PyObject *self, PyObject *args) |
| 1087 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | unsigned int value; |
| 1089 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 1090 | return NULL; |
| 1091 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static PyObject * |
| 1095 | getargs_k(PyObject *self, PyObject *args) |
| 1096 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | unsigned long value; |
| 1098 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 1099 | return NULL; |
| 1100 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static PyObject * |
| 1104 | getargs_i(PyObject *self, PyObject *args) |
| 1105 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | int value; |
| 1107 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 1108 | return NULL; |
| 1109 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1112 | static PyObject * |
| 1113 | getargs_l(PyObject *self, PyObject *args) |
| 1114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | long value; |
| 1116 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 1117 | return NULL; |
| 1118 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1121 | static PyObject * |
| 1122 | getargs_n(PyObject *self, PyObject *args) |
| 1123 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | Py_ssize_t value; |
| 1125 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 1126 | return NULL; |
| 1127 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 1130 | static PyObject * |
| 1131 | getargs_p(PyObject *self, PyObject *args) |
| 1132 | { |
| 1133 | int value; |
| 1134 | if (!PyArg_ParseTuple(args, "p", &value)) |
| 1135 | return NULL; |
| 1136 | return PyLong_FromLong(value); |
| 1137 | } |
| 1138 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1139 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1140 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1141 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | PY_LONG_LONG value; |
| 1144 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 1145 | return NULL; |
| 1146 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1149 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1150 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | unsigned PY_LONG_LONG value; |
| 1153 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 1154 | return NULL; |
| 1155 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1156 | } |
| 1157 | #endif |
| 1158 | |
| 1159 | /* This function not only tests the 'k' getargs code, but also the |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1160 | PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1161 | static PyObject * |
| 1162 | test_k_code(PyObject *self) |
| 1163 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | PyObject *tuple, *num; |
| 1165 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | tuple = PyTuple_New(1); |
| 1168 | if (tuple == NULL) |
| 1169 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 1172 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 1173 | if (num == NULL) |
| 1174 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1176 | value = PyLong_AsUnsignedLongMask(num); |
| 1177 | if (value != ULONG_MAX) |
| 1178 | return raiseTestError("test_k_code", |
Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 1179 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | value = 0; |
| 1184 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 1185 | return NULL; |
| 1186 | if (value != ULONG_MAX) |
| 1187 | return raiseTestError("test_k_code", |
| 1188 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | Py_DECREF(num); |
| 1191 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 1192 | if (num == NULL) |
| 1193 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | value = PyLong_AsUnsignedLongMask(num); |
| 1196 | if (value != (unsigned long)-0x42) |
| 1197 | return raiseTestError("test_k_code", |
Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 1198 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | value = 0; |
| 1203 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 1204 | return NULL; |
| 1205 | if (value != (unsigned long)-0x42) |
| 1206 | return raiseTestError("test_k_code", |
| 1207 | "k code returned wrong value for long -0xFFF..000042"); |
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 | Py_DECREF(tuple); |
| 1210 | Py_INCREF(Py_None); |
| 1211 | return Py_None; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1214 | static PyObject * |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 1215 | getargs_f(PyObject *self, PyObject *args) |
| 1216 | { |
| 1217 | float f; |
| 1218 | if (!PyArg_ParseTuple(args, "f", &f)) |
| 1219 | return NULL; |
| 1220 | return PyFloat_FromDouble(f); |
| 1221 | } |
| 1222 | |
| 1223 | static PyObject * |
| 1224 | getargs_d(PyObject *self, PyObject *args) |
| 1225 | { |
| 1226 | double d; |
| 1227 | if (!PyArg_ParseTuple(args, "d", &d)) |
| 1228 | return NULL; |
| 1229 | return PyFloat_FromDouble(d); |
| 1230 | } |
| 1231 | |
| 1232 | static PyObject * |
| 1233 | getargs_D(PyObject *self, PyObject *args) |
| 1234 | { |
| 1235 | Py_complex cval; |
| 1236 | if (!PyArg_ParseTuple(args, "D", &cval)) |
| 1237 | return NULL; |
| 1238 | return PyComplex_FromCComplex(cval); |
| 1239 | } |
| 1240 | |
| 1241 | static PyObject * |
| 1242 | getargs_S(PyObject *self, PyObject *args) |
| 1243 | { |
| 1244 | PyObject *obj; |
| 1245 | if (!PyArg_ParseTuple(args, "S", &obj)) |
| 1246 | return NULL; |
| 1247 | Py_INCREF(obj); |
| 1248 | return obj; |
| 1249 | } |
| 1250 | |
| 1251 | static PyObject * |
| 1252 | getargs_Y(PyObject *self, PyObject *args) |
| 1253 | { |
| 1254 | PyObject *obj; |
| 1255 | if (!PyArg_ParseTuple(args, "Y", &obj)) |
| 1256 | return NULL; |
| 1257 | Py_INCREF(obj); |
| 1258 | return obj; |
| 1259 | } |
| 1260 | |
| 1261 | static PyObject * |
| 1262 | getargs_U(PyObject *self, PyObject *args) |
| 1263 | { |
| 1264 | PyObject *obj; |
| 1265 | if (!PyArg_ParseTuple(args, "U", &obj)) |
| 1266 | return NULL; |
| 1267 | Py_INCREF(obj); |
| 1268 | return obj; |
| 1269 | } |
| 1270 | |
| 1271 | static PyObject * |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1272 | getargs_c(PyObject *self, PyObject *args) |
| 1273 | { |
| 1274 | char c; |
| 1275 | if (!PyArg_ParseTuple(args, "c", &c)) |
| 1276 | return NULL; |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1277 | return PyLong_FromLong((unsigned char)c); |
| 1278 | } |
| 1279 | |
| 1280 | static PyObject * |
| 1281 | getargs_C(PyObject *self, PyObject *args) |
| 1282 | { |
| 1283 | int c; |
| 1284 | if (!PyArg_ParseTuple(args, "C", &c)) |
| 1285 | return NULL; |
| 1286 | return PyLong_FromLong(c); |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | static PyObject * |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1290 | getargs_s(PyObject *self, PyObject *args) |
| 1291 | { |
| 1292 | char *str; |
| 1293 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1294 | return NULL; |
| 1295 | return PyBytes_FromString(str); |
| 1296 | } |
| 1297 | |
| 1298 | static PyObject * |
| 1299 | getargs_s_star(PyObject *self, PyObject *args) |
| 1300 | { |
| 1301 | Py_buffer buffer; |
| 1302 | PyObject *bytes; |
| 1303 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1304 | return NULL; |
| 1305 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1306 | PyBuffer_Release(&buffer); |
| 1307 | return bytes; |
| 1308 | } |
| 1309 | |
| 1310 | static PyObject * |
| 1311 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1312 | { |
| 1313 | char *str; |
| 1314 | Py_ssize_t size; |
| 1315 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1316 | return NULL; |
| 1317 | return PyBytes_FromStringAndSize(str, size); |
| 1318 | } |
| 1319 | |
| 1320 | static PyObject * |
| 1321 | getargs_z(PyObject *self, PyObject *args) |
| 1322 | { |
| 1323 | char *str; |
| 1324 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1325 | return NULL; |
| 1326 | if (str != NULL) |
| 1327 | return PyBytes_FromString(str); |
| 1328 | else |
| 1329 | Py_RETURN_NONE; |
| 1330 | } |
| 1331 | |
| 1332 | static PyObject * |
| 1333 | getargs_z_star(PyObject *self, PyObject *args) |
| 1334 | { |
| 1335 | Py_buffer buffer; |
| 1336 | PyObject *bytes; |
| 1337 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1338 | return NULL; |
| 1339 | if (buffer.buf != NULL) |
| 1340 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1341 | else { |
| 1342 | Py_INCREF(Py_None); |
| 1343 | bytes = Py_None; |
| 1344 | } |
| 1345 | PyBuffer_Release(&buffer); |
| 1346 | return bytes; |
| 1347 | } |
| 1348 | |
| 1349 | static PyObject * |
| 1350 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1351 | { |
| 1352 | char *str; |
| 1353 | Py_ssize_t size; |
| 1354 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1355 | return NULL; |
| 1356 | if (str != NULL) |
| 1357 | return PyBytes_FromStringAndSize(str, size); |
| 1358 | else |
| 1359 | Py_RETURN_NONE; |
| 1360 | } |
| 1361 | |
| 1362 | static PyObject * |
| 1363 | getargs_y(PyObject *self, PyObject *args) |
| 1364 | { |
| 1365 | char *str; |
| 1366 | if (!PyArg_ParseTuple(args, "y", &str)) |
| 1367 | return NULL; |
| 1368 | return PyBytes_FromString(str); |
| 1369 | } |
| 1370 | |
| 1371 | static PyObject * |
| 1372 | getargs_y_star(PyObject *self, PyObject *args) |
| 1373 | { |
| 1374 | Py_buffer buffer; |
| 1375 | PyObject *bytes; |
| 1376 | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 1377 | return NULL; |
| 1378 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1379 | PyBuffer_Release(&buffer); |
| 1380 | return bytes; |
| 1381 | } |
| 1382 | |
| 1383 | static PyObject * |
| 1384 | getargs_y_hash(PyObject *self, PyObject *args) |
| 1385 | { |
| 1386 | char *str; |
| 1387 | Py_ssize_t size; |
| 1388 | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
| 1389 | return NULL; |
| 1390 | return PyBytes_FromStringAndSize(str, size); |
| 1391 | } |
| 1392 | |
| 1393 | static PyObject * |
| 1394 | getargs_u(PyObject *self, PyObject *args) |
| 1395 | { |
| 1396 | Py_UNICODE *str; |
| 1397 | Py_ssize_t size; |
| 1398 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1399 | return NULL; |
| 1400 | size = Py_UNICODE_strlen(str); |
| 1401 | return PyUnicode_FromUnicode(str, size); |
| 1402 | } |
| 1403 | |
| 1404 | static PyObject * |
| 1405 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1406 | { |
| 1407 | Py_UNICODE *str; |
| 1408 | Py_ssize_t size; |
| 1409 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1410 | return NULL; |
| 1411 | return PyUnicode_FromUnicode(str, size); |
| 1412 | } |
| 1413 | |
| 1414 | static PyObject * |
| 1415 | getargs_Z(PyObject *self, PyObject *args) |
| 1416 | { |
| 1417 | Py_UNICODE *str; |
| 1418 | Py_ssize_t size; |
| 1419 | if (!PyArg_ParseTuple(args, "Z", &str)) |
| 1420 | return NULL; |
| 1421 | if (str != NULL) { |
| 1422 | size = Py_UNICODE_strlen(str); |
| 1423 | return PyUnicode_FromUnicode(str, size); |
| 1424 | } else |
| 1425 | Py_RETURN_NONE; |
| 1426 | } |
| 1427 | |
| 1428 | static PyObject * |
| 1429 | getargs_Z_hash(PyObject *self, PyObject *args) |
| 1430 | { |
| 1431 | Py_UNICODE *str; |
| 1432 | Py_ssize_t size; |
| 1433 | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
| 1434 | return NULL; |
| 1435 | if (str != NULL) |
| 1436 | return PyUnicode_FromUnicode(str, size); |
| 1437 | else |
| 1438 | Py_RETURN_NONE; |
| 1439 | } |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1440 | |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1441 | static PyObject * |
| 1442 | getargs_es(PyObject *self, PyObject *args) |
| 1443 | { |
| 1444 | PyObject *arg, *result; |
| 1445 | const char *encoding = NULL; |
| 1446 | char *str; |
| 1447 | |
| 1448 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1449 | return NULL; |
| 1450 | if (!PyArg_Parse(arg, "es", encoding, &str)) |
| 1451 | return NULL; |
| 1452 | result = PyBytes_FromString(str); |
| 1453 | PyMem_Free(str); |
| 1454 | return result; |
| 1455 | } |
| 1456 | |
| 1457 | static PyObject * |
| 1458 | getargs_et(PyObject *self, PyObject *args) |
| 1459 | { |
| 1460 | PyObject *arg, *result; |
| 1461 | const char *encoding = NULL; |
| 1462 | char *str; |
| 1463 | |
| 1464 | if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) |
| 1465 | return NULL; |
| 1466 | if (!PyArg_Parse(arg, "et", encoding, &str)) |
| 1467 | return NULL; |
| 1468 | result = PyBytes_FromString(str); |
| 1469 | PyMem_Free(str); |
| 1470 | return result; |
| 1471 | } |
| 1472 | |
| 1473 | static PyObject * |
| 1474 | getargs_es_hash(PyObject *self, PyObject *args) |
| 1475 | { |
| 1476 | PyObject *arg, *result; |
| 1477 | const char *encoding = NULL; |
| 1478 | PyByteArrayObject *buffer = NULL; |
| 1479 | char *str = NULL; |
| 1480 | Py_ssize_t size; |
| 1481 | |
| 1482 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1483 | return NULL; |
| 1484 | if (buffer != NULL) { |
| 1485 | str = PyByteArray_AS_STRING(buffer); |
| 1486 | size = PyByteArray_GET_SIZE(buffer); |
| 1487 | } |
| 1488 | if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) |
| 1489 | return NULL; |
| 1490 | result = PyBytes_FromStringAndSize(str, size); |
| 1491 | if (buffer == NULL) |
| 1492 | PyMem_Free(str); |
| 1493 | return result; |
| 1494 | } |
| 1495 | |
| 1496 | static PyObject * |
| 1497 | getargs_et_hash(PyObject *self, PyObject *args) |
| 1498 | { |
| 1499 | PyObject *arg, *result; |
| 1500 | const char *encoding = NULL; |
| 1501 | PyByteArrayObject *buffer = NULL; |
| 1502 | char *str = NULL; |
| 1503 | Py_ssize_t size; |
| 1504 | |
| 1505 | if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) |
| 1506 | return NULL; |
| 1507 | if (buffer != NULL) { |
| 1508 | str = PyByteArray_AS_STRING(buffer); |
| 1509 | size = PyByteArray_GET_SIZE(buffer); |
| 1510 | } |
| 1511 | if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) |
| 1512 | return NULL; |
| 1513 | result = PyBytes_FromStringAndSize(str, size); |
| 1514 | if (buffer == NULL) |
| 1515 | PyMem_Free(str); |
| 1516 | return result; |
| 1517 | } |
| 1518 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1519 | /* Test the s and z codes for PyArg_ParseTuple. |
| 1520 | */ |
| 1521 | static PyObject * |
| 1522 | test_s_code(PyObject *self) |
| 1523 | { |
| 1524 | /* Unicode strings should be accepted */ |
| 1525 | PyObject *tuple, *obj; |
| 1526 | char *value; |
| 1527 | |
| 1528 | tuple = PyTuple_New(1); |
| 1529 | if (tuple == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1531 | |
| 1532 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1534 | if (obj == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1536 | |
| 1537 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1538 | |
| 1539 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1541 | */ |
| 1542 | if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1544 | |
| 1545 | if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1547 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1548 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1549 | Py_RETURN_NONE; |
| 1550 | } |
| 1551 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1552 | static PyObject * |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1553 | parse_tuple_and_keywords(PyObject *self, PyObject *args) |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1554 | { |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1555 | PyObject *sub_args; |
| 1556 | PyObject *sub_kwargs; |
| 1557 | char *sub_format; |
| 1558 | PyObject *sub_keywords; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1559 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1560 | Py_ssize_t i, size; |
| 1561 | char *keywords[8 + 1]; /* space for NULL at end */ |
| 1562 | PyObject *o; |
| 1563 | PyObject *converted[8]; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1564 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1565 | int result; |
| 1566 | PyObject *return_value = NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1567 | |
Larry Hastings | 22701e8 | 2012-08-08 14:52:22 -0700 | [diff] [blame] | 1568 | double buffers[8][4]; /* double ensures alignment where necessary */ |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1569 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1570 | if (!PyArg_ParseTuple(args, "OOyO:parse_tuple_and_keywords", |
| 1571 | &sub_args, &sub_kwargs, |
| 1572 | &sub_format, &sub_keywords)) |
| 1573 | return NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1574 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1575 | if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
| 1576 | PyErr_SetString(PyExc_ValueError, |
| 1577 | "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
| 1578 | return NULL; |
| 1579 | } |
| 1580 | |
| 1581 | memset(buffers, 0, sizeof(buffers)); |
| 1582 | memset(converted, 0, sizeof(converted)); |
| 1583 | memset(keywords, 0, sizeof(keywords)); |
| 1584 | |
| 1585 | size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 1586 | if (size > 8) { |
| 1587 | PyErr_SetString(PyExc_ValueError, |
| 1588 | "parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 1589 | goto exit; |
| 1590 | } |
| 1591 | |
| 1592 | for (i = 0; i < size; i++) { |
| 1593 | o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 1594 | if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { |
| 1595 | PyErr_Format(PyExc_ValueError, |
Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 1596 | "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] | 1597 | goto exit; |
| 1598 | } |
| 1599 | keywords[i] = PyBytes_AS_STRING(converted[i]); |
| 1600 | } |
| 1601 | |
| 1602 | result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 1603 | sub_format, keywords, |
| 1604 | buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 1605 | buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 1606 | |
| 1607 | if (result) { |
| 1608 | return_value = Py_None; |
| 1609 | Py_INCREF(Py_None); |
| 1610 | } |
| 1611 | |
| 1612 | exit: |
| 1613 | size = sizeof(converted) / sizeof(converted[0]); |
| 1614 | for (i = 0; i < size; i++) { |
| 1615 | Py_XDECREF(converted[i]); |
| 1616 | } |
| 1617 | return return_value; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1620 | static volatile int x; |
| 1621 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1622 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1623 | of an error. |
| 1624 | */ |
| 1625 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1626 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | PyObject *tuple, *obj; |
| 1629 | Py_UNICODE *value; |
| 1630 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1633 | /* Just use the macro and check that it compiles */ |
| 1634 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1636 | tuple = PyTuple_New(1); |
| 1637 | if (tuple == NULL) |
| 1638 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1641 | "ascii", NULL); |
| 1642 | if (obj == NULL) |
| 1643 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | value = 0; |
| 1648 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 1649 | return NULL; |
| 1650 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1651 | return raiseTestError("test_u_code", |
| 1652 | "u code returned wrong value for u'test'"); |
| 1653 | value = 0; |
| 1654 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 1655 | return NULL; |
| 1656 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1657 | len != PyUnicode_GET_SIZE(obj)) |
| 1658 | return raiseTestError("test_u_code", |
| 1659 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1661 | Py_DECREF(tuple); |
| 1662 | Py_INCREF(Py_None); |
| 1663 | return Py_None; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1666 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1667 | static PyObject * |
| 1668 | test_Z_code(PyObject *self) |
| 1669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | PyObject *tuple, *obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1671 | const Py_UNICODE *value1, *value2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | tuple = PyTuple_New(2); |
| 1675 | if (tuple == NULL) |
| 1676 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | obj = PyUnicode_FromString("test"); |
| 1679 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1680 | Py_INCREF(Py_None); |
| 1681 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1683 | /* swap values on purpose */ |
| 1684 | value1 = NULL; |
| 1685 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | /* Test Z for both values */ |
| 1688 | if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) |
| 1689 | return NULL; |
| 1690 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1691 | return raiseTestError("test_Z_code", |
| 1692 | "Z code returned wrong value for 'test'"); |
| 1693 | if (value2 != NULL) |
| 1694 | return raiseTestError("test_Z_code", |
| 1695 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | value1 = NULL; |
| 1698 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1699 | len1 = -1; |
| 1700 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | /* Test Z# for both values */ |
| 1703 | if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1704 | &value2, &len2) < 0) |
| 1705 | return NULL; |
| 1706 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1707 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1708 | return raiseTestError("test_Z_code", |
| 1709 | "Z# code returned wrong values for 'test'"); |
| 1710 | if (value2 != NULL || |
| 1711 | len2 != 0) |
| 1712 | return raiseTestError("test_Z_code", |
| 1713 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | Py_DECREF(tuple); |
| 1716 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1719 | static PyObject * |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1720 | test_widechar(PyObject *self) |
| 1721 | { |
| 1722 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1724 | size_t wtextlen = 1; |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1725 | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1726 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1728 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1729 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1732 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1733 | if (wide == NULL) |
| 1734 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1737 | if (utf8 == NULL) { |
| 1738 | Py_DECREF(wide); |
| 1739 | return NULL; |
| 1740 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1741 | |
Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1742 | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | Py_DECREF(wide); |
| 1744 | Py_DECREF(utf8); |
| 1745 | return raiseTestError("test_widechar", |
| 1746 | "wide string and utf8 string " |
| 1747 | "have different length"); |
| 1748 | } |
| 1749 | if (PyUnicode_Compare(wide, utf8)) { |
| 1750 | Py_DECREF(wide); |
| 1751 | Py_DECREF(utf8); |
| 1752 | if (PyErr_Occurred()) |
| 1753 | return NULL; |
| 1754 | return raiseTestError("test_widechar", |
| 1755 | "wide string and utf8 string " |
| 1756 | "are different"); |
| 1757 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | Py_DECREF(wide); |
| 1760 | Py_DECREF(utf8); |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1761 | |
| 1762 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1763 | wide = PyUnicode_FromWideChar(invalid, 1); |
| 1764 | if (wide == NULL) |
| 1765 | PyErr_Clear(); |
| 1766 | else |
| 1767 | return raiseTestError("test_widechar", |
| 1768 | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
| 1769 | |
| 1770 | wide = PyUnicode_FromUnicode(invalid, 1); |
| 1771 | if (wide == NULL) |
| 1772 | PyErr_Clear(); |
| 1773 | else |
| 1774 | return raiseTestError("test_widechar", |
| 1775 | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1776 | |
| 1777 | wide = PyUnicode_FromUnicode(NULL, 1); |
| 1778 | if (wide == NULL) |
| 1779 | return NULL; |
| 1780 | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1781 | if (_PyUnicode_Ready(wide) < 0) { |
| 1782 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1783 | PyErr_Clear(); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1784 | } |
| 1785 | else { |
| 1786 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1787 | return raiseTestError("test_widechar", |
| 1788 | "PyUnicode_Ready() didn't fail"); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1789 | } |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1790 | #endif |
| 1791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1796 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1797 | { |
| 1798 | PyObject *unicode, *result; |
| 1799 | Py_ssize_t buflen, size; |
| 1800 | wchar_t *buffer; |
| 1801 | |
| 1802 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1803 | return NULL; |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1804 | buffer = PyMem_New(wchar_t, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1805 | if (buffer == NULL) |
| 1806 | return PyErr_NoMemory(); |
| 1807 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1808 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1809 | if (size == -1) { |
| 1810 | PyMem_Free(buffer); |
| 1811 | return NULL; |
| 1812 | } |
| 1813 | |
| 1814 | if (size < buflen) |
| 1815 | buflen = size + 1; |
| 1816 | else |
| 1817 | buflen = size; |
| 1818 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 1819 | PyMem_Free(buffer); |
| 1820 | if (result == NULL) |
| 1821 | return NULL; |
| 1822 | |
| 1823 | return Py_BuildValue("(Nn)", result, size); |
| 1824 | } |
| 1825 | |
| 1826 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1827 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1828 | { |
| 1829 | PyObject *unicode, *result; |
| 1830 | Py_ssize_t size; |
| 1831 | wchar_t *buffer; |
| 1832 | |
| 1833 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 1834 | return NULL; |
| 1835 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1836 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1837 | if (buffer == NULL) |
| 1838 | return NULL; |
| 1839 | |
| 1840 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 1841 | PyMem_Free(buffer); |
| 1842 | if (result == NULL) |
| 1843 | return NULL; |
| 1844 | return Py_BuildValue("(Nn)", result, size); |
| 1845 | } |
| 1846 | |
| 1847 | static PyObject * |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 1848 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 1849 | { |
| 1850 | Py_UNICODE *unicode; |
| 1851 | Py_ssize_t length; |
| 1852 | char *errors = NULL; |
| 1853 | PyObject *decimal; |
| 1854 | Py_ssize_t decimal_length, new_length; |
| 1855 | int res; |
| 1856 | |
| 1857 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 1858 | return NULL; |
| 1859 | |
| 1860 | decimal_length = length * 7; /* len('€') */ |
| 1861 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 1862 | if (decimal == NULL) |
| 1863 | return NULL; |
| 1864 | |
| 1865 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 1866 | PyBytes_AS_STRING(decimal), |
| 1867 | errors); |
| 1868 | if (res < 0) { |
| 1869 | Py_DECREF(decimal); |
| 1870 | return NULL; |
| 1871 | } |
| 1872 | |
| 1873 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 1874 | assert(new_length <= decimal_length); |
| 1875 | res = _PyBytes_Resize(&decimal, new_length); |
| 1876 | if (res < 0) |
| 1877 | return NULL; |
| 1878 | |
| 1879 | return decimal; |
| 1880 | } |
| 1881 | |
| 1882 | static PyObject * |
| 1883 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 1884 | { |
| 1885 | Py_UNICODE *unicode; |
| 1886 | Py_ssize_t length; |
| 1887 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 1888 | return NULL; |
| 1889 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 1890 | } |
| 1891 | |
| 1892 | static PyObject * |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 1893 | unicode_legacy_string(PyObject *self, PyObject *args) |
| 1894 | { |
| 1895 | Py_UNICODE *data; |
| 1896 | Py_ssize_t len; |
| 1897 | PyObject *u; |
| 1898 | |
| 1899 | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
| 1900 | return NULL; |
| 1901 | |
| 1902 | u = PyUnicode_FromUnicode(NULL, len); |
| 1903 | if (u == NULL) |
| 1904 | return NULL; |
| 1905 | |
| 1906 | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
| 1907 | |
| 1908 | if (len > 0) { /* The empty string is always ready. */ |
| 1909 | assert(!PyUnicode_IS_READY(u)); |
| 1910 | } |
| 1911 | |
| 1912 | return u; |
| 1913 | } |
| 1914 | |
| 1915 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1916 | getargs_w_star(PyObject *self, PyObject *args) |
| 1917 | { |
| 1918 | Py_buffer buffer; |
| 1919 | PyObject *result; |
| 1920 | char *str; |
| 1921 | |
| 1922 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 1923 | return NULL; |
| 1924 | |
| 1925 | if (2 <= buffer.len) { |
| 1926 | str = buffer.buf; |
| 1927 | str[0] = '['; |
| 1928 | str[buffer.len-1] = ']'; |
| 1929 | } |
| 1930 | |
| 1931 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1932 | PyBuffer_Release(&buffer); |
| 1933 | return result; |
| 1934 | } |
| 1935 | |
| 1936 | |
| 1937 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1938 | test_empty_argparse(PyObject *self) |
| 1939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 1941 | PyObject *tuple, *dict = NULL; |
| 1942 | static char *kwlist[] = {NULL}; |
| 1943 | int result; |
| 1944 | tuple = PyTuple_New(0); |
| 1945 | if (!tuple) |
| 1946 | return NULL; |
| 1947 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 1948 | goto done; |
| 1949 | dict = PyDict_New(); |
| 1950 | if (!dict) |
| 1951 | goto done; |
| 1952 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1953 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | Py_DECREF(tuple); |
| 1955 | Py_XDECREF(dict); |
| 1956 | if (result < 0) |
| 1957 | return NULL; |
| 1958 | else { |
| 1959 | Py_RETURN_NONE; |
| 1960 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1964 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1965 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1966 | const char *encoding, *errors = NULL; |
| 1967 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 1968 | &encoding, &errors)) |
| 1969 | return NULL; |
| 1970 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1973 | static PyObject * |
| 1974 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1975 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | const char *encoding, *errors = NULL; |
| 1977 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 1978 | &encoding, &errors)) |
| 1979 | return NULL; |
| 1980 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1983 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1984 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1985 | static PyObject * |
| 1986 | test_long_numbits(PyObject *self) |
| 1987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | struct triple { |
| 1989 | long input; |
| 1990 | size_t nbits; |
| 1991 | int sign; |
| 1992 | } testcases[] = {{0, 0, 0}, |
| 1993 | {1L, 1, 1}, |
| 1994 | {-1L, 1, -1}, |
| 1995 | {2L, 2, 1}, |
| 1996 | {-2L, 2, -1}, |
| 1997 | {3L, 2, 1}, |
| 1998 | {-3L, 2, -1}, |
| 1999 | {4L, 3, 1}, |
| 2000 | {-4L, 3, -1}, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2001 | {0x7fffL, 15, 1}, /* one Python int digit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2002 | {-0x7fffL, 15, -1}, |
| 2003 | {0xffffL, 16, 1}, |
| 2004 | {-0xffffL, 16, -1}, |
| 2005 | {0xfffffffL, 28, 1}, |
| 2006 | {-0xfffffffL, 28, -1}}; |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 2007 | size_t i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2008 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 2009 | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2010 | size_t nbits; |
| 2011 | int sign; |
| 2012 | PyObject *plong; |
| 2013 | |
| 2014 | plong = PyLong_FromLong(testcases[i].input); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2015 | if (plong == NULL) |
| 2016 | return NULL; |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2017 | nbits = _PyLong_NumBits(plong); |
| 2018 | sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2020 | Py_DECREF(plong); |
| 2021 | if (nbits != testcases[i].nbits) |
| 2022 | return raiseTestError("test_long_numbits", |
| 2023 | "wrong result for _PyLong_NumBits"); |
| 2024 | if (sign != testcases[i].sign) |
| 2025 | return raiseTestError("test_long_numbits", |
| 2026 | "wrong result for _PyLong_Sign"); |
| 2027 | } |
| 2028 | Py_INCREF(Py_None); |
| 2029 | return Py_None; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 2032 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2033 | |
| 2034 | static PyObject * |
| 2035 | test_null_strings(PyObject *self) |
| 2036 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 2038 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 2039 | Py_XDECREF(o1); |
| 2040 | Py_XDECREF(o2); |
| 2041 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2044 | static PyObject * |
| 2045 | raise_exception(PyObject *self, PyObject *args) |
| 2046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | PyObject *exc; |
| 2048 | PyObject *exc_args, *v; |
| 2049 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 2052 | &exc, &num_args)) |
| 2053 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | exc_args = PyTuple_New(num_args); |
| 2056 | if (exc_args == NULL) |
| 2057 | return NULL; |
| 2058 | for (i = 0; i < num_args; ++i) { |
| 2059 | v = PyLong_FromLong(i); |
| 2060 | if (v == NULL) { |
| 2061 | Py_DECREF(exc_args); |
| 2062 | return NULL; |
| 2063 | } |
| 2064 | PyTuple_SET_ITEM(exc_args, i, v); |
| 2065 | } |
| 2066 | PyErr_SetObject(exc, exc_args); |
| 2067 | Py_DECREF(exc_args); |
| 2068 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2069 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2070 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2071 | static PyObject * |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 2072 | set_errno(PyObject *self, PyObject *args) |
| 2073 | { |
| 2074 | int new_errno; |
| 2075 | |
| 2076 | if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) |
| 2077 | return NULL; |
| 2078 | |
| 2079 | errno = new_errno; |
| 2080 | Py_RETURN_NONE; |
| 2081 | } |
| 2082 | |
| 2083 | static PyObject * |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2084 | test_set_exc_info(PyObject *self, PyObject *args) |
| 2085 | { |
| 2086 | PyObject *orig_exc; |
| 2087 | PyObject *new_type, *new_value, *new_tb; |
| 2088 | PyObject *type, *value, *tb; |
| 2089 | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
| 2090 | &new_type, &new_value, &new_tb)) |
| 2091 | return NULL; |
| 2092 | |
| 2093 | PyErr_GetExcInfo(&type, &value, &tb); |
| 2094 | |
| 2095 | Py_INCREF(new_type); |
| 2096 | Py_INCREF(new_value); |
| 2097 | Py_INCREF(new_tb); |
| 2098 | PyErr_SetExcInfo(new_type, new_value, new_tb); |
| 2099 | |
| 2100 | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
| 2101 | Py_XDECREF(type); |
| 2102 | Py_XDECREF(value); |
| 2103 | Py_XDECREF(tb); |
| 2104 | return orig_exc; |
| 2105 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2106 | |
| 2107 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2108 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2109 | static PyObject * |
| 2110 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | if (PyDateTimeAPI) { |
| 2112 | if (test_run_counter) { |
| 2113 | /* Probably regrtest.py -R */ |
| 2114 | Py_RETURN_NONE; |
| 2115 | } |
| 2116 | else { |
| 2117 | PyErr_SetString(PyExc_AssertionError, |
| 2118 | "PyDateTime_CAPI somehow initialized"); |
| 2119 | return NULL; |
| 2120 | } |
| 2121 | } |
| 2122 | test_run_counter++; |
| 2123 | PyDateTime_IMPORT; |
| 2124 | if (PyDateTimeAPI) |
| 2125 | Py_RETURN_NONE; |
| 2126 | else |
| 2127 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2130 | |
| 2131 | #ifdef WITH_THREAD |
| 2132 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2133 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 2134 | * `thread_done` when it's finished. The driver code has to know when the |
| 2135 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 2136 | * may go away when the driver finishes. The former lack of this explicit |
| 2137 | * synchronization caused rare segfaults, so rare that they were seen only |
| 2138 | * on a Mac buildbot (although they were possible on any box). |
| 2139 | */ |
| 2140 | static PyThread_type_lock thread_done = NULL; |
| 2141 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2142 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2143 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | PyObject *rc; |
| 2146 | int success; |
| 2147 | PyGILState_STATE s = PyGILState_Ensure(); |
| 2148 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
| 2149 | success = (rc != NULL); |
| 2150 | Py_XDECREF(rc); |
| 2151 | PyGILState_Release(s); |
| 2152 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2155 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 2156 | * should be called only from threads spawned by test_thread_state(). |
| 2157 | */ |
| 2158 | static void |
| 2159 | _make_call_from_thread(void *callable) |
| 2160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | _make_call(callable); |
| 2162 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2165 | static PyObject * |
| 2166 | test_thread_state(PyObject *self, PyObject *args) |
| 2167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | PyObject *fn; |
| 2169 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2171 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 2172 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | if (!PyCallable_Check(fn)) { |
| 2175 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 2176 | fn->ob_type->tp_name); |
| 2177 | return NULL; |
| 2178 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | /* Ensure Python is set up for threading */ |
| 2181 | PyEval_InitThreads(); |
| 2182 | thread_done = PyThread_allocate_lock(); |
| 2183 | if (thread_done == NULL) |
| 2184 | return PyErr_NoMemory(); |
| 2185 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | /* Start a new thread with our callback. */ |
| 2188 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2189 | /* Make the callback with the thread lock held by this thread */ |
| 2190 | success &= _make_call(fn); |
| 2191 | /* Do it all again, but this time with the thread-lock released */ |
| 2192 | Py_BEGIN_ALLOW_THREADS |
| 2193 | success &= _make_call(fn); |
| 2194 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2195 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2197 | /* And once more with and without a thread |
| 2198 | XXX - should use a lock and work out exactly what we are trying |
| 2199 | to test <wink> |
| 2200 | */ |
| 2201 | Py_BEGIN_ALLOW_THREADS |
| 2202 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 2203 | success &= _make_call(fn); |
| 2204 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 2205 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 2208 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2210 | PyThread_free_lock(thread_done); |
| 2211 | if (!success) |
| 2212 | return NULL; |
| 2213 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2214 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2215 | |
| 2216 | /* test Py_AddPendingCalls using threads */ |
| 2217 | static int _pending_callback(void *arg) |
| 2218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | /* we assume the argument is callable object to which we own a reference */ |
| 2220 | PyObject *callable = (PyObject *)arg; |
| 2221 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 2222 | Py_DECREF(callable); |
| 2223 | Py_XDECREF(r); |
| 2224 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | /* The following requests n callbacks to _pending_callback. It can be |
| 2228 | * run from any python thread. |
| 2229 | */ |
| 2230 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 2231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | PyObject *callable; |
| 2233 | int r; |
| 2234 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 2235 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | /* create the reference for the callbackwhile we hold the lock */ |
| 2238 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | Py_BEGIN_ALLOW_THREADS |
| 2241 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 2242 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | if (r<0) { |
| 2245 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 2246 | Py_INCREF(Py_False); |
| 2247 | return Py_False; |
| 2248 | } |
| 2249 | Py_INCREF(Py_True); |
| 2250 | return Py_True; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2251 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2252 | #endif |
| 2253 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 2254 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2255 | static PyObject * |
| 2256 | test_string_from_format(PyObject *self, PyObject *args) |
| 2257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | PyObject *result; |
| 2259 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2260 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2261 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 2262 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 2263 | if (result == NULL) \ |
| 2264 | return NULL; \ |
Victor Stinner | 2b979bf | 2011-11-20 19:32:09 +0100 | [diff] [blame] | 2265 | if (PyUnicode_CompareWithASCIIString(result, "1")) { \ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2266 | msg = FORMAT " failed at 1"; \ |
| 2267 | goto Fail; \ |
| 2268 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | CHECK_1_FORMAT("%d", int); |
| 2272 | CHECK_1_FORMAT("%ld", long); |
| 2273 | /* The z width modifier was added in Python 2.5. */ |
| 2274 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | /* The u type code was added in Python 2.5. */ |
| 2277 | CHECK_1_FORMAT("%u", unsigned int); |
| 2278 | CHECK_1_FORMAT("%lu", unsigned long); |
| 2279 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2282 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 2284 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2285 | #endif |
| 2286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2288 | |
| 2289 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | Py_XDECREF(result); |
| 2291 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2292 | |
| 2293 | #undef CHECK_1_FORMAT |
| 2294 | } |
| 2295 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2296 | |
| 2297 | static PyObject * |
| 2298 | test_unicode_compare_with_ascii(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 2300 | int result; |
| 2301 | if (py_s == NULL) |
| 2302 | return NULL; |
| 2303 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 2304 | Py_DECREF(py_s); |
| 2305 | if (!result) { |
| 2306 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 2307 | "should not compare equal to c string."); |
| 2308 | return NULL; |
| 2309 | } |
| 2310 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2311 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2312 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2313 | /* This is here to provide a docstring for test_descr. */ |
| 2314 | static PyObject * |
| 2315 | test_with_docstring(PyObject *self) |
| 2316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2320 | /* Test PyOS_string_to_double. */ |
| 2321 | static PyObject * |
| 2322 | test_string_to_double(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2323 | double result; |
| 2324 | char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | #define CHECK_STRING(STR, expected) \ |
| 2327 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2328 | if (result == -1.0 && PyErr_Occurred()) \ |
| 2329 | return NULL; \ |
| 2330 | if (result != expected) { \ |
| 2331 | msg = "conversion of " STR " to float failed"; \ |
| 2332 | goto fail; \ |
| 2333 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | #define CHECK_INVALID(STR) \ |
| 2336 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2337 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 2338 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 2339 | PyErr_Clear(); \ |
| 2340 | else \ |
| 2341 | return NULL; \ |
| 2342 | } \ |
| 2343 | else { \ |
| 2344 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 2345 | goto fail; \ |
| 2346 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | CHECK_STRING("0.1", 0.1); |
| 2349 | CHECK_STRING("1.234", 1.234); |
| 2350 | CHECK_STRING("-1.35", -1.35); |
| 2351 | CHECK_STRING(".1e01", 1.0); |
| 2352 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | CHECK_INVALID(" 0.1"); |
| 2355 | CHECK_INVALID("\t\n-3"); |
| 2356 | CHECK_INVALID(".123 "); |
| 2357 | CHECK_INVALID("3\n"); |
| 2358 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2361 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2362 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2363 | #undef CHECK_STRING |
| 2364 | #undef CHECK_INVALID |
| 2365 | } |
| 2366 | |
| 2367 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2368 | /* Coverage testing of capsule objects. */ |
| 2369 | |
| 2370 | static const char *capsule_name = "capsule name"; |
| 2371 | static char *capsule_pointer = "capsule pointer"; |
| 2372 | static char *capsule_context = "capsule context"; |
| 2373 | static const char *capsule_error = NULL; |
| 2374 | static int |
| 2375 | capsule_destructor_call_count = 0; |
| 2376 | |
| 2377 | static void |
| 2378 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | capsule_destructor_call_count++; |
| 2380 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 2381 | capsule_error = "context did not match in destructor!"; |
| 2382 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2383 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2384 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 2385 | capsule_error = "name did not match in destructor!"; |
| 2386 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2387 | capsule_error = "pointer did not match in destructor!"; |
| 2388 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | char *name; |
| 2393 | char *module; |
| 2394 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2395 | } known_capsule; |
| 2396 | |
| 2397 | static PyObject * |
| 2398 | test_capsule(PyObject *self, PyObject *args) |
| 2399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | PyObject *object; |
| 2401 | const char *error = NULL; |
| 2402 | void *pointer; |
| 2403 | void *pointer2; |
| 2404 | known_capsule known_capsules[] = { |
| 2405 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2406 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 2407 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 2408 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2409 | { NULL, NULL }, |
| 2410 | }; |
| 2411 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2412 | |
| 2413 | #define FAIL(x) { error = (x); goto exit; } |
| 2414 | |
| 2415 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2416 | if (capsule_error) { \ |
| 2417 | FAIL(capsule_error); \ |
| 2418 | } \ |
| 2419 | else if (!capsule_destructor_call_count) { \ |
| 2420 | FAIL("destructor not called!"); \ |
| 2421 | } \ |
| 2422 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2425 | PyCapsule_SetContext(object, capsule_context); |
| 2426 | capsule_destructor(object); |
| 2427 | CHECK_DESTRUCTOR; |
| 2428 | Py_DECREF(object); |
| 2429 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | object = PyCapsule_New(known, "ignored", NULL); |
| 2432 | PyCapsule_SetPointer(object, capsule_pointer); |
| 2433 | PyCapsule_SetName(object, capsule_name); |
| 2434 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 2435 | PyCapsule_SetContext(object, capsule_context); |
| 2436 | capsule_destructor(object); |
| 2437 | CHECK_DESTRUCTOR; |
| 2438 | /* intentionally access using the wrong name */ |
| 2439 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2440 | if (!PyErr_Occurred()) { |
| 2441 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2442 | } |
| 2443 | PyErr_Clear(); |
| 2444 | if (pointer2) { |
| 2445 | if (pointer2 == capsule_pointer) { |
| 2446 | FAIL("PyCapsule_GetPointer should not have" |
| 2447 | " returned the internal pointer!"); |
| 2448 | } else { |
| 2449 | FAIL("PyCapsule_GetPointer should have " |
| 2450 | "returned NULL pointer but did not!"); |
| 2451 | } |
| 2452 | } |
| 2453 | PyCapsule_SetDestructor(object, NULL); |
| 2454 | Py_DECREF(object); |
| 2455 | if (capsule_destructor_call_count) { |
| 2456 | FAIL("destructor called when it should not have been!"); |
| 2457 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 2460 | /* yeah, ordinarily I wouldn't do this either, |
| 2461 | but it's fine for this test harness. |
| 2462 | */ |
| 2463 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2464 | #undef FAIL |
| 2465 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2466 | { \ |
| 2467 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 2468 | x, known->module, known->attribute); \ |
| 2469 | error = buffer; \ |
| 2470 | goto exit; \ |
| 2471 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2473 | PyObject *module = PyImport_ImportModule(known->module); |
| 2474 | if (module) { |
| 2475 | pointer = PyCapsule_Import(known->name, 0); |
| 2476 | if (!pointer) { |
| 2477 | Py_DECREF(module); |
| 2478 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 2479 | } |
| 2480 | object = PyObject_GetAttrString(module, known->attribute); |
| 2481 | if (!object) { |
| 2482 | Py_DECREF(module); |
| 2483 | return NULL; |
| 2484 | } |
| 2485 | pointer2 = PyCapsule_GetPointer(object, |
| 2486 | "weebles wobble but they don't fall down"); |
| 2487 | if (!PyErr_Occurred()) { |
| 2488 | Py_DECREF(object); |
| 2489 | Py_DECREF(module); |
| 2490 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2491 | } |
| 2492 | PyErr_Clear(); |
| 2493 | if (pointer2) { |
| 2494 | Py_DECREF(module); |
| 2495 | Py_DECREF(object); |
| 2496 | if (pointer2 == pointer) { |
| 2497 | FAIL("PyCapsule_GetPointer should not have" |
| 2498 | " returned its internal pointer!"); |
| 2499 | } else { |
| 2500 | FAIL("PyCapsule_GetPointer should have" |
| 2501 | " returned NULL pointer but did not!"); |
| 2502 | } |
| 2503 | } |
| 2504 | Py_DECREF(object); |
| 2505 | Py_DECREF(module); |
| 2506 | } |
| 2507 | else |
| 2508 | PyErr_Clear(); |
| 2509 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2510 | |
| 2511 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | if (error) { |
| 2513 | return raiseTestError("test_capsule", error); |
| 2514 | } |
| 2515 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2516 | #undef FAIL |
| 2517 | } |
| 2518 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2519 | #ifdef HAVE_GETTIMEOFDAY |
| 2520 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 2521 | 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] | 2522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | e->tv_sec -= s->tv_sec; |
| 2524 | e->tv_usec -= s->tv_usec; |
| 2525 | if (e->tv_usec < 0) { |
| 2526 | e->tv_sec -=1; |
| 2527 | e->tv_usec += 1000000; |
| 2528 | } |
| 2529 | 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] | 2530 | } |
| 2531 | |
| 2532 | static PyObject * |
| 2533 | profile_int(PyObject *self, PyObject* args) |
| 2534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | int i, k; |
| 2536 | struct timeval start, stop; |
| 2537 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | /* Test 1: Allocate and immediately deallocate |
| 2540 | many small integers */ |
| 2541 | gettimeofday(&start, NULL); |
| 2542 | for(k=0; k < 20000; k++) |
| 2543 | for(i=0; i < 1000; i++) { |
| 2544 | single = PyLong_FromLong(i); |
| 2545 | Py_DECREF(single); |
| 2546 | } |
| 2547 | gettimeofday(&stop, NULL); |
| 2548 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | /* Test 2: Allocate and immediately deallocate |
| 2551 | many large integers */ |
| 2552 | gettimeofday(&start, NULL); |
| 2553 | for(k=0; k < 20000; k++) |
| 2554 | for(i=0; i < 1000; i++) { |
| 2555 | single = PyLong_FromLong(i+1000000); |
| 2556 | Py_DECREF(single); |
| 2557 | } |
| 2558 | gettimeofday(&stop, NULL); |
| 2559 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 | /* Test 3: Allocate a few integers, then release |
| 2562 | them all simultaneously. */ |
| 2563 | multiple = malloc(sizeof(PyObject*) * 1000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2564 | if (multiple == NULL) |
| 2565 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | gettimeofday(&start, NULL); |
| 2567 | for(k=0; k < 20000; k++) { |
| 2568 | for(i=0; i < 1000; i++) { |
| 2569 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2570 | } |
| 2571 | for(i=0; i < 1000; i++) { |
| 2572 | Py_DECREF(multiple[i]); |
| 2573 | } |
| 2574 | } |
| 2575 | gettimeofday(&stop, NULL); |
| 2576 | print_delta(3, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2577 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | /* Test 4: Allocate many integers, then release |
| 2580 | them all simultaneously. */ |
| 2581 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2582 | if (multiple == NULL) |
| 2583 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2584 | gettimeofday(&start, NULL); |
| 2585 | for(k=0; k < 20; k++) { |
| 2586 | for(i=0; i < 1000000; i++) { |
| 2587 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2588 | } |
| 2589 | for(i=0; i < 1000000; i++) { |
| 2590 | Py_DECREF(multiple[i]); |
| 2591 | } |
| 2592 | } |
| 2593 | gettimeofday(&stop, NULL); |
| 2594 | print_delta(4, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2595 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | /* Test 5: Allocate many integers < 32000 */ |
| 2598 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2599 | if (multiple == NULL) |
| 2600 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2601 | gettimeofday(&start, NULL); |
| 2602 | for(k=0; k < 10; k++) { |
| 2603 | for(i=0; i < 1000000; i++) { |
| 2604 | multiple[i] = PyLong_FromLong(i+1000); |
| 2605 | } |
| 2606 | for(i=0; i < 1000000; i++) { |
| 2607 | Py_DECREF(multiple[i]); |
| 2608 | } |
| 2609 | } |
| 2610 | gettimeofday(&stop, NULL); |
| 2611 | print_delta(5, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2612 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | /* Test 6: Perform small int addition */ |
| 2615 | op1 = PyLong_FromLong(1); |
| 2616 | gettimeofday(&start, NULL); |
| 2617 | for(i=0; i < 10000000; i++) { |
| 2618 | result = PyNumber_Add(op1, op1); |
| 2619 | Py_DECREF(result); |
| 2620 | } |
| 2621 | gettimeofday(&stop, NULL); |
| 2622 | Py_DECREF(op1); |
| 2623 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2625 | /* Test 7: Perform medium int addition */ |
| 2626 | op1 = PyLong_FromLong(1000); |
Christian Heimes | 66eda26 | 2013-07-26 15:54:07 +0200 | [diff] [blame] | 2627 | if (op1 == NULL) |
| 2628 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | gettimeofday(&start, NULL); |
| 2630 | for(i=0; i < 10000000; i++) { |
| 2631 | result = PyNumber_Add(op1, op1); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2632 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2633 | } |
| 2634 | gettimeofday(&stop, NULL); |
| 2635 | Py_DECREF(op1); |
| 2636 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | Py_INCREF(Py_None); |
| 2639 | return Py_None; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2640 | } |
| 2641 | #endif |
| 2642 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2643 | /* To test the format of tracebacks as printed out. */ |
| 2644 | static PyObject * |
| 2645 | traceback_print(PyObject *self, PyObject *args) |
| 2646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | PyObject *file; |
| 2648 | PyObject *traceback; |
| 2649 | int result; |
| 2650 | |
| 2651 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 2652 | &traceback, &file)) |
| 2653 | return NULL; |
| 2654 | |
| 2655 | result = PyTraceBack_Print(traceback, file); |
| 2656 | if (result < 0) |
| 2657 | return NULL; |
| 2658 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2661 | /* To test the format of exceptions as printed out. */ |
| 2662 | static PyObject * |
| 2663 | exception_print(PyObject *self, PyObject *args) |
| 2664 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2665 | PyObject *value; |
| 2666 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 2669 | &value)) |
| 2670 | return NULL; |
| 2671 | if (!PyExceptionInstance_Check(value)) { |
| 2672 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 2673 | return NULL; |
| 2674 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | tb = PyException_GetTraceback(value); |
| 2677 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 2678 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | |
| 2684 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2685 | |
| 2686 | /* reliably raise a MemoryError */ |
| 2687 | static PyObject * |
| 2688 | raise_memoryerror(PyObject *self) |
| 2689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | PyErr_NoMemory(); |
| 2691 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2694 | /* Issue 6012 */ |
| 2695 | static PyObject *str1, *str2; |
| 2696 | static int |
| 2697 | failing_converter(PyObject *obj, void *arg) |
| 2698 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | /* Clone str1, then let the conversion fail. */ |
| 2700 | assert(str1); |
| 2701 | str2 = str1; |
| 2702 | Py_INCREF(str2); |
| 2703 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2704 | } |
| 2705 | static PyObject* |
| 2706 | argparsing(PyObject *o, PyObject *args) |
| 2707 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2708 | PyObject *res; |
| 2709 | str1 = str2 = NULL; |
| 2710 | if (!PyArg_ParseTuple(args, "O&O&", |
| 2711 | PyUnicode_FSConverter, &str1, |
| 2712 | failing_converter, &str2)) { |
| 2713 | if (!str2) |
| 2714 | /* argument converter not called? */ |
| 2715 | return NULL; |
| 2716 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2717 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | Py_DECREF(str2); |
| 2719 | PyErr_Clear(); |
| 2720 | return res; |
| 2721 | } |
| 2722 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2723 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2724 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2725 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 2726 | static PyObject * |
| 2727 | code_newempty(PyObject *self, PyObject *args) |
| 2728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | const char *filename; |
| 2730 | const char *funcname; |
| 2731 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 2734 | &filename, &funcname, &firstlineno)) |
| 2735 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2740 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 2741 | Run via Lib/test/test_exceptions.py */ |
| 2742 | static PyObject * |
| 2743 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2745 | const char *name; |
| 2746 | const char *doc = NULL; |
| 2747 | PyObject *base = NULL; |
| 2748 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2753 | "s|sOO:make_exception_with_doc", kwlist, |
| 2754 | &name, &doc, &base, &dict)) |
| 2755 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2757 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2760 | static PyObject * |
| 2761 | make_memoryview_from_NULL_pointer(PyObject *self) |
| 2762 | { |
| 2763 | Py_buffer info; |
| 2764 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 2765 | return NULL; |
| 2766 | return PyMemoryView_FromBuffer(&info); |
| 2767 | } |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 2768 | |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 2769 | static PyObject * |
| 2770 | test_from_contiguous(PyObject* self, PyObject *noargs) |
| 2771 | { |
| 2772 | int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; |
| 2773 | int init[5] = {0, 1, 2, 3, 4}; |
| 2774 | Py_ssize_t itemsize = sizeof(int); |
| 2775 | Py_ssize_t shape = 5; |
| 2776 | Py_ssize_t strides = 2 * itemsize; |
| 2777 | Py_buffer view = { |
| 2778 | data, |
| 2779 | NULL, |
| 2780 | 5 * itemsize, |
| 2781 | itemsize, |
| 2782 | 1, |
| 2783 | 1, |
| 2784 | NULL, |
| 2785 | &shape, |
| 2786 | &strides, |
| 2787 | NULL, |
| 2788 | NULL |
| 2789 | }; |
| 2790 | int *ptr; |
| 2791 | int i; |
| 2792 | |
| 2793 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 2794 | ptr = view.buf; |
| 2795 | for (i = 0; i < 5; i++) { |
| 2796 | if (ptr[2*i] != i) { |
| 2797 | PyErr_SetString(TestError, |
| 2798 | "test_from_contiguous: incorrect result"); |
| 2799 | return NULL; |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | view.buf = &data[8]; |
| 2804 | view.strides[0] = -2 * itemsize; |
| 2805 | |
| 2806 | PyBuffer_FromContiguous(&view, init, view.len, 'C'); |
| 2807 | ptr = view.buf; |
| 2808 | for (i = 0; i < 5; i++) { |
| 2809 | if (*(ptr-2*i) != i) { |
| 2810 | PyErr_SetString(TestError, |
| 2811 | "test_from_contiguous: incorrect result"); |
| 2812 | return NULL; |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | Py_RETURN_NONE; |
| 2817 | } |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2818 | |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 2819 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2820 | extern PyTypeObject _PyBytesIOBuffer_Type; |
| 2821 | |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2822 | static PyObject * |
| 2823 | test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs) |
| 2824 | { |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2825 | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2826 | PyObject *b; |
| 2827 | char *dummy[1]; |
| 2828 | int ret, match; |
| 2829 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2830 | /* PyBuffer_FillInfo() */ |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2831 | ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE); |
| 2832 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 2833 | PyErr_Clear(); |
| 2834 | if (ret != -1 || match == 0) |
| 2835 | goto error; |
| 2836 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2837 | /* bytesiobuf_getbuffer() */ |
| 2838 | b = type->tp_alloc(type, 0); |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2839 | if (b == NULL) { |
| 2840 | return NULL; |
| 2841 | } |
| 2842 | |
| 2843 | ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE); |
| 2844 | Py_DECREF(b); |
| 2845 | match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); |
| 2846 | PyErr_Clear(); |
| 2847 | if (ret != -1 || match == 0) |
| 2848 | goto error; |
| 2849 | |
| 2850 | Py_RETURN_NONE; |
| 2851 | |
| 2852 | error: |
| 2853 | PyErr_SetString(TestError, |
| 2854 | "test_pep3118_obsolete_write_locks: failure"); |
| 2855 | return NULL; |
| 2856 | } |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 2857 | #endif |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2858 | |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2859 | /* This tests functions that historically supported write locks. It is |
| 2860 | wrong to call getbuffer() with view==NULL and a compliant getbufferproc |
| 2861 | is entitled to segfault in that case. */ |
| 2862 | static PyObject * |
| 2863 | getbuffer_with_null_view(PyObject* self, PyObject *obj) |
| 2864 | { |
| 2865 | if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0) |
| 2866 | return NULL; |
| 2867 | |
| 2868 | Py_RETURN_NONE; |
| 2869 | } |
| 2870 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2871 | /* Test that the fatal error from not having a current thread doesn't |
| 2872 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 2873 | static PyObject * |
| 2874 | crash_no_current_thread(PyObject *self) |
| 2875 | { |
| 2876 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 2877 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 2878 | !pydebug mode. However, the test only actually tests anything |
| 2879 | in pydebug mode, since that's where the infinite loop was in |
| 2880 | the first place. */ |
| 2881 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2882 | Py_END_ALLOW_THREADS |
| 2883 | return NULL; |
| 2884 | } |
| 2885 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2886 | /* To run some code in a sub-interpreter. */ |
| 2887 | static PyObject * |
| 2888 | run_in_subinterp(PyObject *self, PyObject *args) |
| 2889 | { |
| 2890 | const char *code; |
| 2891 | int r; |
| 2892 | PyThreadState *substate, *mainstate; |
| 2893 | |
| 2894 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 2895 | &code)) |
| 2896 | return NULL; |
| 2897 | |
| 2898 | mainstate = PyThreadState_Get(); |
| 2899 | |
| 2900 | PyThreadState_Swap(NULL); |
| 2901 | |
| 2902 | substate = Py_NewInterpreter(); |
Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 2903 | if (substate == NULL) { |
| 2904 | /* Since no new thread state was created, there is no exception to |
| 2905 | propagate; raise a fresh one after swapping in the old thread |
| 2906 | state. */ |
| 2907 | PyThreadState_Swap(mainstate); |
| 2908 | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
| 2909 | return NULL; |
| 2910 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2911 | r = PyRun_SimpleString(code); |
| 2912 | Py_EndInterpreter(substate); |
| 2913 | |
| 2914 | PyThreadState_Swap(mainstate); |
| 2915 | |
| 2916 | return PyLong_FromLong(r); |
| 2917 | } |
| 2918 | |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2919 | static int |
| 2920 | check_time_rounding(int round) |
| 2921 | { |
Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 2922 | if (round != _PyTime_ROUND_FLOOR |
| 2923 | && round != _PyTime_ROUND_CEILING |
Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 2924 | && round != _PyTime_ROUND_HALF_EVEN) { |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2925 | PyErr_SetString(PyExc_ValueError, "invalid rounding"); |
| 2926 | return -1; |
| 2927 | } |
| 2928 | return 0; |
| 2929 | } |
| 2930 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2931 | static PyObject * |
| 2932 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
| 2933 | { |
| 2934 | PyObject *obj; |
| 2935 | time_t sec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2936 | int round; |
| 2937 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2938 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2939 | if (check_time_rounding(round) < 0) |
| 2940 | return NULL; |
| 2941 | if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2942 | return NULL; |
| 2943 | return _PyLong_FromTime_t(sec); |
| 2944 | } |
| 2945 | |
| 2946 | static PyObject * |
| 2947 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
| 2948 | { |
| 2949 | PyObject *obj; |
| 2950 | time_t sec; |
| 2951 | long usec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2952 | int round; |
| 2953 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2954 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2955 | if (check_time_rounding(round) < 0) |
| 2956 | return NULL; |
| 2957 | if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2958 | return NULL; |
| 2959 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
| 2960 | } |
| 2961 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2962 | static PyObject * |
| 2963 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
| 2964 | { |
| 2965 | PyObject *obj; |
| 2966 | time_t sec; |
| 2967 | long nsec; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2968 | int round; |
| 2969 | if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2970 | return NULL; |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2971 | if (check_time_rounding(round) < 0) |
| 2972 | return NULL; |
| 2973 | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2974 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2975 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2976 | } |
| 2977 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 2978 | static void |
| 2979 | slot_tp_del(PyObject *self) |
| 2980 | { |
| 2981 | _Py_IDENTIFIER(__tp_del__); |
| 2982 | PyObject *del, *res; |
| 2983 | PyObject *error_type, *error_value, *error_traceback; |
| 2984 | |
| 2985 | /* Temporarily resurrect the object. */ |
| 2986 | assert(self->ob_refcnt == 0); |
| 2987 | self->ob_refcnt = 1; |
| 2988 | |
| 2989 | /* Save the current exception, if any. */ |
| 2990 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 2991 | |
| 2992 | /* Execute __del__ method, if any. */ |
| 2993 | del = _PyObject_LookupSpecial(self, &PyId___tp_del__); |
| 2994 | if (del != NULL) { |
| 2995 | res = PyEval_CallObject(del, NULL); |
| 2996 | if (res == NULL) |
| 2997 | PyErr_WriteUnraisable(del); |
| 2998 | else |
| 2999 | Py_DECREF(res); |
| 3000 | Py_DECREF(del); |
| 3001 | } |
| 3002 | |
| 3003 | /* Restore the saved exception. */ |
| 3004 | PyErr_Restore(error_type, error_value, error_traceback); |
| 3005 | |
| 3006 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 3007 | * cause a recursive call. |
| 3008 | */ |
| 3009 | assert(self->ob_refcnt > 0); |
| 3010 | if (--self->ob_refcnt == 0) |
| 3011 | return; /* this is the normal path out */ |
| 3012 | |
| 3013 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 3014 | * never happened. |
| 3015 | */ |
| 3016 | { |
| 3017 | Py_ssize_t refcnt = self->ob_refcnt; |
| 3018 | _Py_NewReference(self); |
| 3019 | self->ob_refcnt = refcnt; |
| 3020 | } |
| 3021 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 3022 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 3023 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 3024 | * we need to undo that. */ |
| 3025 | _Py_DEC_REFTOTAL; |
| 3026 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 3027 | * chain, so no more to do there. |
| 3028 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 3029 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 3030 | * undone. |
| 3031 | */ |
| 3032 | #ifdef COUNT_ALLOCS |
| 3033 | --Py_TYPE(self)->tp_frees; |
| 3034 | --Py_TYPE(self)->tp_allocs; |
| 3035 | #endif |
| 3036 | } |
| 3037 | |
| 3038 | static PyObject * |
| 3039 | with_tp_del(PyObject *self, PyObject *args) |
| 3040 | { |
| 3041 | PyObject *obj; |
| 3042 | PyTypeObject *tp; |
| 3043 | |
| 3044 | if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) |
| 3045 | return NULL; |
| 3046 | tp = (PyTypeObject *) obj; |
| 3047 | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
| 3048 | PyErr_Format(PyExc_TypeError, |
| 3049 | "heap type expected, got %R", obj); |
| 3050 | return NULL; |
| 3051 | } |
| 3052 | tp->tp_del = slot_tp_del; |
| 3053 | Py_INCREF(obj); |
| 3054 | return obj; |
| 3055 | } |
| 3056 | |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 3057 | static PyMethodDef ml; |
| 3058 | |
| 3059 | static PyObject * |
| 3060 | create_cfunction(PyObject *self, PyObject *args) |
| 3061 | { |
| 3062 | return PyCFunction_NewEx(&ml, self, NULL); |
| 3063 | } |
| 3064 | |
| 3065 | static PyMethodDef ml = { |
| 3066 | "create_cfunction", |
| 3067 | create_cfunction, |
| 3068 | METH_NOARGS, |
| 3069 | NULL |
| 3070 | }; |
| 3071 | |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3072 | static PyObject * |
| 3073 | _test_incref(PyObject *ob) |
| 3074 | { |
| 3075 | Py_INCREF(ob); |
| 3076 | return ob; |
| 3077 | } |
| 3078 | |
| 3079 | static PyObject * |
| 3080 | test_xincref_doesnt_leak(PyObject *ob) |
| 3081 | { |
| 3082 | PyObject *obj = PyLong_FromLong(0); |
| 3083 | Py_XINCREF(_test_incref(obj)); |
| 3084 | Py_DECREF(obj); |
| 3085 | Py_DECREF(obj); |
| 3086 | Py_DECREF(obj); |
| 3087 | Py_RETURN_NONE; |
| 3088 | } |
| 3089 | |
| 3090 | static PyObject * |
| 3091 | test_incref_doesnt_leak(PyObject *ob) |
| 3092 | { |
| 3093 | PyObject *obj = PyLong_FromLong(0); |
| 3094 | Py_INCREF(_test_incref(obj)); |
| 3095 | Py_DECREF(obj); |
| 3096 | Py_DECREF(obj); |
| 3097 | Py_DECREF(obj); |
| 3098 | Py_RETURN_NONE; |
| 3099 | } |
| 3100 | |
| 3101 | static PyObject * |
| 3102 | test_xdecref_doesnt_leak(PyObject *ob) |
| 3103 | { |
| 3104 | Py_XDECREF(PyLong_FromLong(0)); |
| 3105 | Py_RETURN_NONE; |
| 3106 | } |
| 3107 | |
| 3108 | static PyObject * |
| 3109 | test_decref_doesnt_leak(PyObject *ob) |
| 3110 | { |
| 3111 | Py_DECREF(PyLong_FromLong(0)); |
| 3112 | Py_RETURN_NONE; |
| 3113 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3114 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3115 | static PyObject * |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3116 | test_incref_decref_API(PyObject *ob) |
| 3117 | { |
| 3118 | PyObject *obj = PyLong_FromLong(0); |
Victor Stinner | fc6a90a | 2014-10-09 22:15:41 +0200 | [diff] [blame] | 3119 | Py_IncRef(obj); |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3120 | Py_DecRef(obj); |
| 3121 | Py_DecRef(obj); |
| 3122 | Py_RETURN_NONE; |
| 3123 | } |
| 3124 | |
| 3125 | static PyObject * |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3126 | test_pymem_alloc0(PyObject *self) |
| 3127 | { |
| 3128 | void *ptr; |
| 3129 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3130 | ptr = PyMem_RawMalloc(0); |
| 3131 | if (ptr == NULL) { |
| 3132 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); |
| 3133 | return NULL; |
| 3134 | } |
| 3135 | PyMem_RawFree(ptr); |
| 3136 | |
| 3137 | ptr = PyMem_RawCalloc(0, 0); |
| 3138 | if (ptr == NULL) { |
| 3139 | PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); |
| 3140 | return NULL; |
| 3141 | } |
| 3142 | PyMem_RawFree(ptr); |
| 3143 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3144 | ptr = PyMem_Malloc(0); |
| 3145 | if (ptr == NULL) { |
| 3146 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); |
| 3147 | return NULL; |
| 3148 | } |
| 3149 | PyMem_Free(ptr); |
| 3150 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3151 | ptr = PyMem_Calloc(0, 0); |
| 3152 | if (ptr == NULL) { |
| 3153 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); |
| 3154 | return NULL; |
| 3155 | } |
| 3156 | PyMem_Free(ptr); |
| 3157 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3158 | ptr = PyObject_Malloc(0); |
| 3159 | if (ptr == NULL) { |
| 3160 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); |
| 3161 | return NULL; |
| 3162 | } |
| 3163 | PyObject_Free(ptr); |
| 3164 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3165 | ptr = PyObject_Calloc(0, 0); |
| 3166 | if (ptr == NULL) { |
| 3167 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); |
| 3168 | return NULL; |
| 3169 | } |
| 3170 | PyObject_Free(ptr); |
| 3171 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3172 | Py_RETURN_NONE; |
| 3173 | } |
| 3174 | |
| 3175 | typedef struct { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3176 | PyMemAllocatorEx alloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3177 | |
| 3178 | size_t malloc_size; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3179 | size_t calloc_nelem; |
| 3180 | size_t calloc_elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3181 | void *realloc_ptr; |
| 3182 | size_t realloc_new_size; |
| 3183 | void *free_ptr; |
| 3184 | } alloc_hook_t; |
| 3185 | |
| 3186 | static void* hook_malloc (void* ctx, size_t size) |
| 3187 | { |
| 3188 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 3189 | hook->malloc_size = size; |
| 3190 | return hook->alloc.malloc(hook->alloc.ctx, size); |
| 3191 | } |
| 3192 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3193 | static void* hook_calloc (void* ctx, size_t nelem, size_t elsize) |
| 3194 | { |
| 3195 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 3196 | hook->calloc_nelem = nelem; |
| 3197 | hook->calloc_elsize = elsize; |
| 3198 | return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); |
| 3199 | } |
| 3200 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3201 | static void* hook_realloc (void* ctx, void* ptr, size_t new_size) |
| 3202 | { |
| 3203 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 3204 | hook->realloc_ptr = ptr; |
| 3205 | hook->realloc_new_size = new_size; |
| 3206 | return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); |
| 3207 | } |
| 3208 | |
| 3209 | static void hook_free (void *ctx, void *ptr) |
| 3210 | { |
| 3211 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 3212 | hook->free_ptr = ptr; |
| 3213 | hook->alloc.free(hook->alloc.ctx, ptr); |
| 3214 | } |
| 3215 | |
| 3216 | static PyObject * |
| 3217 | test_setallocators(PyMemAllocatorDomain domain) |
| 3218 | { |
| 3219 | PyObject *res = NULL; |
| 3220 | const char *error_msg; |
| 3221 | alloc_hook_t hook; |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3222 | PyMemAllocatorEx alloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3223 | size_t size, size2, nelem, elsize; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3224 | void *ptr, *ptr2; |
| 3225 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3226 | memset(&hook, 0, sizeof(hook)); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3227 | |
| 3228 | alloc.ctx = &hook; |
| 3229 | alloc.malloc = &hook_malloc; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3230 | alloc.calloc = &hook_calloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3231 | alloc.realloc = &hook_realloc; |
| 3232 | alloc.free = &hook_free; |
| 3233 | PyMem_GetAllocator(domain, &hook.alloc); |
| 3234 | PyMem_SetAllocator(domain, &alloc); |
| 3235 | |
| 3236 | size = 42; |
| 3237 | switch(domain) |
| 3238 | { |
| 3239 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; |
| 3240 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; |
| 3241 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; |
| 3242 | default: ptr = NULL; break; |
| 3243 | } |
| 3244 | |
| 3245 | if (ptr == NULL) { |
| 3246 | error_msg = "malloc failed"; |
| 3247 | goto fail; |
| 3248 | } |
| 3249 | |
| 3250 | if (hook.malloc_size != size) { |
| 3251 | error_msg = "malloc invalid size"; |
| 3252 | goto fail; |
| 3253 | } |
| 3254 | |
| 3255 | size2 = 200; |
| 3256 | switch(domain) |
| 3257 | { |
| 3258 | case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; |
| 3259 | case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; |
| 3260 | case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; |
Christian Heimes | 865d12a | 2013-08-02 11:10:51 +0200 | [diff] [blame] | 3261 | default: ptr2 = NULL; break; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | if (ptr2 == NULL) { |
| 3265 | error_msg = "realloc failed"; |
| 3266 | goto fail; |
| 3267 | } |
| 3268 | |
| 3269 | if (hook.realloc_ptr != ptr |
| 3270 | || hook.realloc_new_size != size2) { |
| 3271 | error_msg = "realloc invalid parameters"; |
| 3272 | goto fail; |
| 3273 | } |
| 3274 | |
| 3275 | switch(domain) |
| 3276 | { |
| 3277 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; |
| 3278 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; |
| 3279 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; |
| 3280 | } |
| 3281 | |
| 3282 | if (hook.free_ptr != ptr2) { |
| 3283 | error_msg = "free invalid pointer"; |
| 3284 | goto fail; |
| 3285 | } |
| 3286 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3287 | nelem = 2; |
| 3288 | elsize = 5; |
| 3289 | switch(domain) |
| 3290 | { |
| 3291 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; |
| 3292 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; |
| 3293 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; |
| 3294 | default: ptr = NULL; break; |
| 3295 | } |
| 3296 | |
| 3297 | if (ptr == NULL) { |
| 3298 | error_msg = "calloc failed"; |
| 3299 | goto fail; |
| 3300 | } |
| 3301 | |
| 3302 | if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { |
| 3303 | error_msg = "calloc invalid nelem or elsize"; |
| 3304 | goto fail; |
| 3305 | } |
| 3306 | |
| 3307 | switch(domain) |
| 3308 | { |
| 3309 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; |
| 3310 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; |
| 3311 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; |
| 3312 | } |
| 3313 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3314 | Py_INCREF(Py_None); |
| 3315 | res = Py_None; |
| 3316 | goto finally; |
| 3317 | |
| 3318 | fail: |
| 3319 | PyErr_SetString(PyExc_RuntimeError, error_msg); |
| 3320 | |
| 3321 | finally: |
| 3322 | PyMem_SetAllocator(domain, &hook.alloc); |
| 3323 | return res; |
| 3324 | } |
| 3325 | |
| 3326 | static PyObject * |
| 3327 | test_pymem_setrawallocators(PyObject *self) |
| 3328 | { |
| 3329 | return test_setallocators(PYMEM_DOMAIN_RAW); |
| 3330 | } |
| 3331 | |
| 3332 | static PyObject * |
| 3333 | test_pymem_setallocators(PyObject *self) |
| 3334 | { |
| 3335 | return test_setallocators(PYMEM_DOMAIN_MEM); |
| 3336 | } |
| 3337 | |
| 3338 | static PyObject * |
| 3339 | test_pyobject_setallocators(PyObject *self) |
| 3340 | { |
| 3341 | return test_setallocators(PYMEM_DOMAIN_OBJ); |
| 3342 | } |
| 3343 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3344 | PyDoc_STRVAR(docstring_empty, |
| 3345 | "" |
| 3346 | ); |
| 3347 | |
| 3348 | PyDoc_STRVAR(docstring_no_signature, |
| 3349 | "This docstring has no signature." |
| 3350 | ); |
| 3351 | |
| 3352 | PyDoc_STRVAR(docstring_with_invalid_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3353 | "docstring_with_invalid_signature($module, /, boo)\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3354 | "\n" |
| 3355 | "This docstring has an invalid signature." |
| 3356 | ); |
| 3357 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3358 | PyDoc_STRVAR(docstring_with_invalid_signature2, |
| 3359 | "docstring_with_invalid_signature2($module, /, boo)\n" |
| 3360 | "\n" |
| 3361 | "--\n" |
| 3362 | "\n" |
| 3363 | "This docstring also has an invalid signature." |
| 3364 | ); |
| 3365 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3366 | PyDoc_STRVAR(docstring_with_signature, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3367 | "docstring_with_signature($module, /, sig)\n" |
| 3368 | "--\n" |
| 3369 | "\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3370 | "This docstring has a valid signature." |
| 3371 | ); |
| 3372 | |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 3373 | PyDoc_STRVAR(docstring_with_signature_but_no_doc, |
| 3374 | "docstring_with_signature_but_no_doc($module, /, sig)\n" |
| 3375 | "--\n" |
| 3376 | "\n" |
| 3377 | ); |
| 3378 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3379 | PyDoc_STRVAR(docstring_with_signature_and_extra_newlines, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3380 | "docstring_with_signature_and_extra_newlines($module, /, parameter)\n" |
| 3381 | "--\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3382 | "\n" |
| 3383 | "\n" |
| 3384 | "This docstring has a valid signature and some extra newlines." |
| 3385 | ); |
| 3386 | |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 3387 | PyDoc_STRVAR(docstring_with_signature_with_defaults, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3388 | "docstring_with_signature_with_defaults(module, s='avocado',\n" |
| 3389 | " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n" |
| 3390 | " local=the_number_three, sys=sys.maxsize,\n" |
| 3391 | " exp=sys.maxsize - 1)\n" |
| 3392 | "--\n" |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 3393 | "\n" |
| 3394 | "\n" |
| 3395 | "\n" |
| 3396 | "This docstring has a valid signature with parameters,\n" |
| 3397 | "and the parameters take defaults of varying types." |
| 3398 | ); |
| 3399 | |
Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 3400 | #ifdef WITH_THREAD |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3401 | typedef struct { |
| 3402 | PyThread_type_lock start_event; |
| 3403 | PyThread_type_lock exit_event; |
| 3404 | PyObject *callback; |
| 3405 | } test_c_thread_t; |
| 3406 | |
| 3407 | static void |
| 3408 | temporary_c_thread(void *data) |
| 3409 | { |
| 3410 | test_c_thread_t *test_c_thread = data; |
| 3411 | PyGILState_STATE state; |
| 3412 | PyObject *res; |
| 3413 | |
| 3414 | PyThread_release_lock(test_c_thread->start_event); |
| 3415 | |
| 3416 | /* Allocate a Python thread state for this thread */ |
| 3417 | state = PyGILState_Ensure(); |
| 3418 | |
| 3419 | res = PyObject_CallFunction(test_c_thread->callback, "", NULL); |
| 3420 | Py_CLEAR(test_c_thread->callback); |
| 3421 | |
| 3422 | if (res == NULL) { |
| 3423 | PyErr_Print(); |
| 3424 | } |
| 3425 | else { |
| 3426 | Py_DECREF(res); |
| 3427 | } |
| 3428 | |
| 3429 | /* Destroy the Python thread state for this thread */ |
| 3430 | PyGILState_Release(state); |
| 3431 | |
| 3432 | PyThread_release_lock(test_c_thread->exit_event); |
| 3433 | |
| 3434 | PyThread_exit_thread(); |
| 3435 | } |
| 3436 | |
| 3437 | static PyObject * |
| 3438 | call_in_temporary_c_thread(PyObject *self, PyObject *callback) |
| 3439 | { |
| 3440 | PyObject *res = NULL; |
| 3441 | test_c_thread_t test_c_thread; |
| 3442 | long thread; |
| 3443 | |
| 3444 | PyEval_InitThreads(); |
| 3445 | |
| 3446 | test_c_thread.start_event = PyThread_allocate_lock(); |
| 3447 | test_c_thread.exit_event = PyThread_allocate_lock(); |
| 3448 | test_c_thread.callback = NULL; |
| 3449 | if (!test_c_thread.start_event || !test_c_thread.exit_event) { |
| 3450 | PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); |
| 3451 | goto exit; |
| 3452 | } |
| 3453 | |
| 3454 | Py_INCREF(callback); |
| 3455 | test_c_thread.callback = callback; |
| 3456 | |
| 3457 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 3458 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 3459 | |
| 3460 | thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); |
| 3461 | if (thread == -1) { |
| 3462 | PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); |
| 3463 | PyThread_release_lock(test_c_thread.start_event); |
| 3464 | PyThread_release_lock(test_c_thread.exit_event); |
| 3465 | goto exit; |
| 3466 | } |
| 3467 | |
| 3468 | PyThread_acquire_lock(test_c_thread.start_event, 1); |
| 3469 | PyThread_release_lock(test_c_thread.start_event); |
| 3470 | |
| 3471 | Py_BEGIN_ALLOW_THREADS |
| 3472 | PyThread_acquire_lock(test_c_thread.exit_event, 1); |
| 3473 | PyThread_release_lock(test_c_thread.exit_event); |
| 3474 | Py_END_ALLOW_THREADS |
| 3475 | |
| 3476 | Py_INCREF(Py_None); |
| 3477 | res = Py_None; |
| 3478 | |
| 3479 | exit: |
| 3480 | Py_CLEAR(test_c_thread.callback); |
| 3481 | if (test_c_thread.start_event) |
| 3482 | PyThread_free_lock(test_c_thread.start_event); |
| 3483 | if (test_c_thread.exit_event) |
| 3484 | PyThread_free_lock(test_c_thread.exit_event); |
| 3485 | return res; |
| 3486 | } |
Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 3487 | #endif /* WITH_THREAD */ |
Victor Stinner | 1310510 | 2013-12-13 02:17:29 +0100 | [diff] [blame] | 3488 | |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 3489 | static PyObject* |
| 3490 | test_raise_signal(PyObject* self, PyObject *args) |
| 3491 | { |
| 3492 | int signum, err; |
| 3493 | |
| 3494 | if (PyArg_ParseTuple(args, "i:raise_signal", &signum) < 0) |
| 3495 | return NULL; |
| 3496 | |
| 3497 | err = raise(signum); |
| 3498 | if (err) |
| 3499 | return PyErr_SetFromErrno(PyExc_OSError); |
| 3500 | |
| 3501 | if (PyErr_CheckSignals() < 0) |
| 3502 | return NULL; |
| 3503 | |
| 3504 | Py_RETURN_NONE; |
| 3505 | } |
| 3506 | |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 3507 | /* marshal */ |
| 3508 | |
| 3509 | static PyObject* |
| 3510 | pymarshal_write_long_to_file(PyObject* self, PyObject *args) |
| 3511 | { |
| 3512 | long value; |
| 3513 | char *filename; |
| 3514 | int version; |
| 3515 | FILE *fp; |
| 3516 | |
| 3517 | if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", |
| 3518 | &value, &filename, &version)) |
| 3519 | return NULL; |
| 3520 | |
| 3521 | fp = fopen(filename, "wb"); |
| 3522 | if (fp == NULL) { |
| 3523 | PyErr_SetFromErrno(PyExc_OSError); |
| 3524 | return NULL; |
| 3525 | } |
| 3526 | |
| 3527 | PyMarshal_WriteLongToFile(value, fp, version); |
| 3528 | |
| 3529 | fclose(fp); |
| 3530 | if (PyErr_Occurred()) |
| 3531 | return NULL; |
| 3532 | Py_RETURN_NONE; |
| 3533 | } |
| 3534 | |
| 3535 | static PyObject* |
| 3536 | pymarshal_write_object_to_file(PyObject* self, PyObject *args) |
| 3537 | { |
| 3538 | PyObject *obj; |
| 3539 | char *filename; |
| 3540 | int version; |
| 3541 | FILE *fp; |
| 3542 | |
| 3543 | if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", |
| 3544 | &obj, &filename, &version)) |
| 3545 | return NULL; |
| 3546 | |
| 3547 | fp = fopen(filename, "wb"); |
| 3548 | if (fp == NULL) { |
| 3549 | PyErr_SetFromErrno(PyExc_OSError); |
| 3550 | return NULL; |
| 3551 | } |
| 3552 | |
| 3553 | PyMarshal_WriteObjectToFile(obj, fp, version); |
| 3554 | |
| 3555 | fclose(fp); |
| 3556 | if (PyErr_Occurred()) |
| 3557 | return NULL; |
| 3558 | Py_RETURN_NONE; |
| 3559 | } |
| 3560 | |
| 3561 | static PyObject* |
| 3562 | pymarshal_read_short_from_file(PyObject* self, PyObject *args) |
| 3563 | { |
| 3564 | int value; |
| 3565 | long pos; |
| 3566 | char *filename; |
| 3567 | FILE *fp; |
| 3568 | |
| 3569 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) |
| 3570 | return NULL; |
| 3571 | |
| 3572 | fp = fopen(filename, "rb"); |
| 3573 | if (fp == NULL) { |
| 3574 | PyErr_SetFromErrno(PyExc_OSError); |
| 3575 | return NULL; |
| 3576 | } |
| 3577 | |
| 3578 | value = PyMarshal_ReadShortFromFile(fp); |
| 3579 | pos = ftell(fp); |
| 3580 | |
| 3581 | fclose(fp); |
| 3582 | if (PyErr_Occurred()) |
| 3583 | return NULL; |
| 3584 | return Py_BuildValue("il", value, pos); |
| 3585 | } |
| 3586 | |
| 3587 | static PyObject* |
| 3588 | pymarshal_read_long_from_file(PyObject* self, PyObject *args) |
| 3589 | { |
| 3590 | long value, pos; |
| 3591 | char *filename; |
| 3592 | FILE *fp; |
| 3593 | |
| 3594 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) |
| 3595 | return NULL; |
| 3596 | |
| 3597 | fp = fopen(filename, "rb"); |
| 3598 | if (fp == NULL) { |
| 3599 | PyErr_SetFromErrno(PyExc_OSError); |
| 3600 | return NULL; |
| 3601 | } |
| 3602 | |
| 3603 | value = PyMarshal_ReadLongFromFile(fp); |
| 3604 | pos = ftell(fp); |
| 3605 | |
| 3606 | fclose(fp); |
| 3607 | if (PyErr_Occurred()) |
| 3608 | return NULL; |
| 3609 | return Py_BuildValue("ll", value, pos); |
| 3610 | } |
| 3611 | |
| 3612 | static PyObject* |
| 3613 | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) |
| 3614 | { |
| 3615 | PyObject *obj; |
| 3616 | long pos; |
| 3617 | char *filename; |
| 3618 | FILE *fp; |
| 3619 | |
| 3620 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) |
| 3621 | return NULL; |
| 3622 | |
| 3623 | fp = fopen(filename, "rb"); |
| 3624 | if (fp == NULL) { |
| 3625 | PyErr_SetFromErrno(PyExc_OSError); |
| 3626 | return NULL; |
| 3627 | } |
| 3628 | |
| 3629 | obj = PyMarshal_ReadLastObjectFromFile(fp); |
| 3630 | pos = ftell(fp); |
| 3631 | |
| 3632 | fclose(fp); |
| 3633 | return Py_BuildValue("Nl", obj, pos); |
| 3634 | } |
| 3635 | |
| 3636 | static PyObject* |
| 3637 | pymarshal_read_object_from_file(PyObject* self, PyObject *args) |
| 3638 | { |
| 3639 | PyObject *obj; |
| 3640 | long pos; |
| 3641 | char *filename; |
| 3642 | FILE *fp; |
| 3643 | |
| 3644 | if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) |
| 3645 | return NULL; |
| 3646 | |
| 3647 | fp = fopen(filename, "rb"); |
| 3648 | if (fp == NULL) { |
| 3649 | PyErr_SetFromErrno(PyExc_OSError); |
| 3650 | return NULL; |
| 3651 | } |
| 3652 | |
| 3653 | obj = PyMarshal_ReadObjectFromFile(fp); |
| 3654 | pos = ftell(fp); |
| 3655 | |
| 3656 | fclose(fp); |
| 3657 | return Py_BuildValue("Nl", obj, pos); |
| 3658 | } |
| 3659 | |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3660 | static PyObject* |
| 3661 | return_null_without_error(PyObject *self, PyObject *args) |
| 3662 | { |
| 3663 | /* invalid call: return NULL without setting an error, |
| 3664 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 3665 | PyErr_Clear(); |
| 3666 | return NULL; |
| 3667 | } |
| 3668 | |
| 3669 | static PyObject* |
| 3670 | return_result_with_error(PyObject *self, PyObject *args) |
| 3671 | { |
| 3672 | /* invalid call: return a result with an error set, |
| 3673 | * _Py_CheckFunctionResult() must detect such bug at runtime. */ |
| 3674 | PyErr_SetNone(PyExc_ValueError); |
| 3675 | Py_RETURN_NONE; |
| 3676 | } |
| 3677 | |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 3678 | static PyObject * |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 3679 | test_pytime_fromseconds(PyObject *self, PyObject *args) |
| 3680 | { |
| 3681 | int seconds; |
| 3682 | _PyTime_t ts; |
| 3683 | |
| 3684 | if (!PyArg_ParseTuple(args, "i", &seconds)) |
| 3685 | return NULL; |
| 3686 | ts = _PyTime_FromSeconds(seconds); |
| 3687 | return _PyTime_AsNanosecondsObject(ts); |
| 3688 | } |
| 3689 | |
| 3690 | static PyObject * |
Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 3691 | test_pytime_fromsecondsobject(PyObject *self, PyObject *args) |
| 3692 | { |
| 3693 | PyObject *obj; |
| 3694 | int round; |
| 3695 | _PyTime_t ts; |
| 3696 | |
| 3697 | if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) |
| 3698 | return NULL; |
| 3699 | if (check_time_rounding(round) < 0) |
| 3700 | return NULL; |
| 3701 | if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) |
| 3702 | return NULL; |
| 3703 | return _PyTime_AsNanosecondsObject(ts); |
| 3704 | } |
| 3705 | |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 3706 | static PyObject * |
| 3707 | test_pytime_assecondsdouble(PyObject *self, PyObject *args) |
| 3708 | { |
| 3709 | PY_LONG_LONG ns; |
| 3710 | _PyTime_t ts; |
| 3711 | double d; |
| 3712 | |
| 3713 | if (!PyArg_ParseTuple(args, "L", &ns)) |
| 3714 | return NULL; |
| 3715 | ts = _PyTime_FromNanoseconds(ns); |
| 3716 | d = _PyTime_AsSecondsDouble(ts); |
| 3717 | return PyFloat_FromDouble(d); |
| 3718 | } |
| 3719 | |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3720 | static PyObject * |
| 3721 | test_PyTime_AsTimeval(PyObject *self, PyObject *args) |
| 3722 | { |
| 3723 | PY_LONG_LONG ns; |
| 3724 | int round; |
| 3725 | _PyTime_t t; |
| 3726 | struct timeval tv; |
| 3727 | PyObject *seconds; |
| 3728 | |
| 3729 | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
| 3730 | return NULL; |
| 3731 | if (check_time_rounding(round) < 0) |
| 3732 | return NULL; |
| 3733 | t = _PyTime_FromNanoseconds(ns); |
Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 3734 | if (_PyTime_AsTimeval(t, &tv, round) < 0) |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3735 | return NULL; |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3736 | |
| 3737 | seconds = PyLong_FromLong((PY_LONG_LONG)tv.tv_sec); |
| 3738 | if (seconds == NULL) |
| 3739 | return NULL; |
| 3740 | return Py_BuildValue("Nl", seconds, tv.tv_usec); |
| 3741 | } |
| 3742 | |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 3743 | #ifdef HAVE_CLOCK_GETTIME |
| 3744 | static PyObject * |
| 3745 | test_PyTime_AsTimespec(PyObject *self, PyObject *args) |
| 3746 | { |
| 3747 | PY_LONG_LONG ns; |
| 3748 | _PyTime_t t; |
| 3749 | struct timespec ts; |
| 3750 | |
| 3751 | if (!PyArg_ParseTuple(args, "L", &ns)) |
| 3752 | return NULL; |
| 3753 | t = _PyTime_FromNanoseconds(ns); |
| 3754 | if (_PyTime_AsTimespec(t, &ts) == -1) |
| 3755 | return NULL; |
| 3756 | return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); |
| 3757 | } |
| 3758 | #endif |
| 3759 | |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 3760 | static PyObject * |
| 3761 | test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) |
| 3762 | { |
| 3763 | PY_LONG_LONG ns; |
| 3764 | int round; |
| 3765 | _PyTime_t t, ms; |
| 3766 | |
| 3767 | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
| 3768 | return NULL; |
| 3769 | if (check_time_rounding(round) < 0) |
| 3770 | return NULL; |
| 3771 | t = _PyTime_FromNanoseconds(ns); |
| 3772 | ms = _PyTime_AsMilliseconds(t, round); |
| 3773 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 3774 | nanoseconds */ |
| 3775 | return _PyTime_AsNanosecondsObject(ms); |
| 3776 | } |
| 3777 | |
| 3778 | static PyObject * |
| 3779 | test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) |
| 3780 | { |
| 3781 | PY_LONG_LONG ns; |
| 3782 | int round; |
| 3783 | _PyTime_t t, ms; |
| 3784 | |
| 3785 | if (!PyArg_ParseTuple(args, "Li", &ns, &round)) |
| 3786 | return NULL; |
| 3787 | if (check_time_rounding(round) < 0) |
| 3788 | return NULL; |
| 3789 | t = _PyTime_FromNanoseconds(ns); |
| 3790 | ms = _PyTime_AsMicroseconds(t, round); |
| 3791 | /* This conversion rely on the fact that _PyTime_t is a number of |
| 3792 | nanoseconds */ |
| 3793 | return _PyTime_AsNanosecondsObject(ms); |
| 3794 | } |
| 3795 | |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 3796 | static PyObject* |
| 3797 | get_recursion_depth(PyObject *self, PyObject *args) |
| 3798 | { |
| 3799 | PyThreadState *tstate = PyThreadState_GET(); |
| 3800 | |
| 3801 | /* substract one to ignore the frame of the get_recursion_depth() call */ |
| 3802 | return PyLong_FromLong(tstate->recursion_depth - 1); |
| 3803 | } |
| 3804 | |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 3805 | static PyObject* |
| 3806 | pymem_buffer_overflow(PyObject *self, PyObject *args) |
| 3807 | { |
| 3808 | char *buffer; |
| 3809 | |
| 3810 | /* Deliberate buffer overflow to check that PyMem_Free() detects |
| 3811 | the overflow when debug hooks are installed. */ |
| 3812 | buffer = PyMem_Malloc(16); |
| 3813 | buffer[16] = 'x'; |
| 3814 | PyMem_Free(buffer); |
| 3815 | |
| 3816 | Py_RETURN_NONE; |
| 3817 | } |
| 3818 | |
| 3819 | static PyObject* |
| 3820 | pymem_api_misuse(PyObject *self, PyObject *args) |
| 3821 | { |
| 3822 | char *buffer; |
| 3823 | |
| 3824 | /* Deliberate misusage of Python allocators: |
| 3825 | allococate with PyMem but release with PyMem_Raw. */ |
| 3826 | buffer = PyMem_Malloc(16); |
| 3827 | PyMem_RawFree(buffer); |
| 3828 | |
| 3829 | Py_RETURN_NONE; |
| 3830 | } |
| 3831 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 3832 | static PyObject* |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 3833 | pymem_malloc_without_gil(PyObject *self, PyObject *args) |
| 3834 | { |
| 3835 | char *buffer; |
| 3836 | |
| 3837 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 3838 | call PyMem_Malloc() without holding the GIL */ |
| 3839 | Py_BEGIN_ALLOW_THREADS |
| 3840 | buffer = PyMem_Malloc(10); |
| 3841 | Py_END_ALLOW_THREADS |
| 3842 | |
| 3843 | PyMem_Free(buffer); |
| 3844 | |
| 3845 | Py_RETURN_NONE; |
| 3846 | } |
| 3847 | |
| 3848 | static PyObject* |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 3849 | pyobject_malloc_without_gil(PyObject *self, PyObject *args) |
| 3850 | { |
| 3851 | char *buffer; |
| 3852 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 3853 | /* Deliberate bug to test debug hooks on Python memory allocators: |
| 3854 | call PyObject_Malloc() without holding the GIL */ |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 3855 | Py_BEGIN_ALLOW_THREADS |
| 3856 | buffer = PyObject_Malloc(10); |
| 3857 | Py_END_ALLOW_THREADS |
| 3858 | |
| 3859 | PyObject_Free(buffer); |
| 3860 | |
| 3861 | Py_RETURN_NONE; |
| 3862 | } |
| 3863 | |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 3864 | static PyObject * |
| 3865 | tracemalloc_track(PyObject *self, PyObject *args) |
| 3866 | { |
| 3867 | unsigned int domain; |
| 3868 | PyObject *ptr_obj; |
| 3869 | void *ptr; |
| 3870 | Py_ssize_t size; |
| 3871 | int release_gil = 0; |
| 3872 | int res; |
| 3873 | |
| 3874 | if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil)) |
| 3875 | return NULL; |
| 3876 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 3877 | if (PyErr_Occurred()) |
| 3878 | return NULL; |
| 3879 | |
| 3880 | if (release_gil) { |
| 3881 | Py_BEGIN_ALLOW_THREADS |
| 3882 | res = _PyTraceMalloc_Track(domain, (Py_uintptr_t)ptr, size); |
| 3883 | Py_END_ALLOW_THREADS |
| 3884 | } |
| 3885 | else { |
| 3886 | res = _PyTraceMalloc_Track(domain, (Py_uintptr_t)ptr, size); |
| 3887 | } |
| 3888 | |
| 3889 | if (res < 0) { |
| 3890 | PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error"); |
| 3891 | return NULL; |
| 3892 | } |
| 3893 | |
| 3894 | Py_RETURN_NONE; |
| 3895 | } |
| 3896 | |
| 3897 | static PyObject * |
| 3898 | tracemalloc_untrack(PyObject *self, PyObject *args) |
| 3899 | { |
| 3900 | unsigned int domain; |
| 3901 | PyObject *ptr_obj; |
| 3902 | void *ptr; |
| 3903 | int res; |
| 3904 | |
| 3905 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 3906 | return NULL; |
| 3907 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 3908 | if (PyErr_Occurred()) |
| 3909 | return NULL; |
| 3910 | |
| 3911 | res = _PyTraceMalloc_Untrack(domain, (Py_uintptr_t)ptr); |
| 3912 | if (res < 0) { |
| 3913 | PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error"); |
| 3914 | return NULL; |
| 3915 | } |
| 3916 | |
| 3917 | Py_RETURN_NONE; |
| 3918 | } |
| 3919 | |
| 3920 | static PyObject * |
| 3921 | tracemalloc_get_traceback(PyObject *self, PyObject *args) |
| 3922 | { |
| 3923 | unsigned int domain; |
| 3924 | PyObject *ptr_obj; |
| 3925 | void *ptr; |
| 3926 | |
| 3927 | if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) |
| 3928 | return NULL; |
| 3929 | ptr = PyLong_AsVoidPtr(ptr_obj); |
| 3930 | if (PyErr_Occurred()) |
| 3931 | return NULL; |
| 3932 | |
| 3933 | return _PyTraceMalloc_GetTraceback(domain, (Py_uintptr_t)ptr); |
| 3934 | } |
| 3935 | |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3936 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 3937 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3938 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 3939 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 3940 | {"set_errno", set_errno, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3941 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 3942 | {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3943 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
| 3944 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 3945 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 3946 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3947 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3948 | {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS}, |
| 3949 | {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak, METH_NOARGS}, |
| 3950 | {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak, METH_NOARGS}, |
| 3951 | {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak, METH_NOARGS}, |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3952 | {"test_incref_decref_API", (PyCFunction)test_incref_decref_API, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3953 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 3954 | METH_NOARGS}, |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 3955 | {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS}, |
| 3956 | {"test_long_as_size_t", (PyCFunction)test_long_as_size_t,METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 3958 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 3959 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 3960 | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3961 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 3962 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 3963 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 3964 | PyDoc_STR("This is a pretty normal docstring.")}, |
| 3965 | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
| 3966 | {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, |
| 3967 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3968 | {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, |
Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3969 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) |
Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3970 | {"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] | 3971 | #endif |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3972 | {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, |
Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 3973 | {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS}, |
Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 3974 | {"get_args", get_args, METH_VARARGS}, |
| 3975 | {"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3976 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 3977 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 3978 | METH_VARARGS|METH_KEYWORDS}, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 3979 | {"getargs_keyword_only", (PyCFunction)getargs_keyword_only, |
| 3980 | METH_VARARGS|METH_KEYWORDS}, |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 3981 | {"getargs_positional_only_and_keywords", |
| 3982 | (PyCFunction)getargs_positional_only_and_keywords, |
| 3983 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3984 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 3985 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 3986 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 3987 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 3988 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 3989 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 3990 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 3991 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 3992 | {"getargs_n", getargs_n, METH_VARARGS}, |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 3993 | {"getargs_p", getargs_p, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 3994 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3995 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 3996 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 3997 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 3998 | {"test_long_long_and_overflow", |
| 3999 | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
| 4000 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 4001 | #endif |
Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 4002 | {"getargs_f", getargs_f, METH_VARARGS}, |
| 4003 | {"getargs_d", getargs_d, METH_VARARGS}, |
| 4004 | {"getargs_D", getargs_D, METH_VARARGS}, |
| 4005 | {"getargs_S", getargs_S, METH_VARARGS}, |
| 4006 | {"getargs_Y", getargs_Y, METH_VARARGS}, |
| 4007 | {"getargs_U", getargs_U, METH_VARARGS}, |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 4008 | {"getargs_c", getargs_c, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 4009 | {"getargs_C", getargs_C, METH_VARARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 4010 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 4011 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 4012 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 4013 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 4014 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 4015 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 4016 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 4017 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 4018 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 4019 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 4020 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 4021 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 4022 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 4023 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 4024 | {"getargs_es", getargs_es, METH_VARARGS}, |
| 4025 | {"getargs_et", getargs_et, METH_VARARGS}, |
| 4026 | {"getargs_es_hash", getargs_es_hash, METH_VARARGS}, |
| 4027 | {"getargs_et_hash", getargs_et_hash, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4028 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4029 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4030 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4031 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4032 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
| 4033 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 4034 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
| 4035 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 4036 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 4037 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
| 4038 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 4039 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 4040 | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4041 | #ifdef WITH_THREAD |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4042 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4043 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4044 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4045 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4046 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4047 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4048 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 4049 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 4050 | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4051 | {"argparsing", argparsing, METH_VARARGS}, |
| 4052 | {"code_newempty", code_newempty, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4053 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 4054 | METH_VARARGS | METH_KEYWORDS}, |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 4055 | {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer, |
| 4056 | METH_NOARGS}, |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 4057 | {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 4058 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4059 | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
| 4060 | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 4061 | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 4062 | {"with_tp_del", with_tp_del, METH_VARARGS}, |
Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 4063 | {"create_cfunction", create_cfunction, METH_NOARGS}, |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4064 | {"test_pymem_alloc0", |
Victor Stinner | 797bcb5 | 2014-06-02 21:29:07 +0200 | [diff] [blame] | 4065 | (PyCFunction)test_pymem_alloc0, METH_NOARGS}, |
| 4066 | {"test_pymem_setrawallocators", |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4067 | (PyCFunction)test_pymem_setrawallocators, METH_NOARGS}, |
| 4068 | {"test_pymem_setallocators", |
| 4069 | (PyCFunction)test_pymem_setallocators, METH_NOARGS}, |
| 4070 | {"test_pyobject_setallocators", |
| 4071 | (PyCFunction)test_pyobject_setallocators, METH_NOARGS}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4072 | {"no_docstring", |
| 4073 | (PyCFunction)test_with_docstring, METH_NOARGS}, |
| 4074 | {"docstring_empty", |
| 4075 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4076 | docstring_empty}, |
| 4077 | {"docstring_no_signature", |
| 4078 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4079 | docstring_no_signature}, |
| 4080 | {"docstring_with_invalid_signature", |
| 4081 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4082 | docstring_with_invalid_signature}, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4083 | {"docstring_with_invalid_signature2", |
| 4084 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4085 | docstring_with_invalid_signature2}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4086 | {"docstring_with_signature", |
| 4087 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4088 | docstring_with_signature}, |
Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 4089 | {"docstring_with_signature_but_no_doc", |
| 4090 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4091 | docstring_with_signature_but_no_doc}, |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4092 | {"docstring_with_signature_and_extra_newlines", |
| 4093 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4094 | docstring_with_signature_and_extra_newlines}, |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4095 | {"docstring_with_signature_with_defaults", |
| 4096 | (PyCFunction)test_with_docstring, METH_NOARGS, |
| 4097 | docstring_with_signature_with_defaults}, |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 4098 | {"raise_signal", |
| 4099 | (PyCFunction)test_raise_signal, METH_VARARGS}, |
Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 4100 | #ifdef WITH_THREAD |
Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4101 | {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, |
| 4102 | PyDoc_STR("set_error_class(error_class) -> None")}, |
Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 4103 | #endif |
Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4104 | {"pymarshal_write_long_to_file", |
| 4105 | pymarshal_write_long_to_file, METH_VARARGS}, |
| 4106 | {"pymarshal_write_object_to_file", |
| 4107 | pymarshal_write_object_to_file, METH_VARARGS}, |
| 4108 | {"pymarshal_read_short_from_file", |
| 4109 | pymarshal_read_short_from_file, METH_VARARGS}, |
| 4110 | {"pymarshal_read_long_from_file", |
| 4111 | pymarshal_read_long_from_file, METH_VARARGS}, |
| 4112 | {"pymarshal_read_last_object_from_file", |
| 4113 | pymarshal_read_last_object_from_file, METH_VARARGS}, |
| 4114 | {"pymarshal_read_object_from_file", |
| 4115 | pymarshal_read_object_from_file, METH_VARARGS}, |
Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4116 | {"return_null_without_error", |
| 4117 | return_null_without_error, METH_NOARGS}, |
| 4118 | {"return_result_with_error", |
| 4119 | return_result_with_error, METH_NOARGS}, |
Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 4120 | {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS}, |
Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4121 | {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS}, |
| 4122 | {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, |
Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4123 | {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, |
Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4124 | #ifdef HAVE_CLOCK_GETTIME |
| 4125 | {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, |
| 4126 | #endif |
Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4127 | {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS}, |
| 4128 | {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS}, |
Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4129 | {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, |
Victor Stinner | 34be807 | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4130 | {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS}, |
| 4131 | {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4132 | {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4133 | {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, |
Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4134 | {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, |
| 4135 | {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, |
| 4136 | {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4137 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4138 | }; |
| 4139 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 4140 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 4141 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4142 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4143 | char bool_member; |
| 4144 | char byte_member; |
| 4145 | unsigned char ubyte_member; |
| 4146 | short short_member; |
| 4147 | unsigned short ushort_member; |
| 4148 | int int_member; |
| 4149 | unsigned int uint_member; |
| 4150 | long long_member; |
| 4151 | unsigned long ulong_member; |
| 4152 | Py_ssize_t pyssizet_member; |
| 4153 | float float_member; |
| 4154 | double double_member; |
| 4155 | char inplace_member[6]; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4156 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4157 | PY_LONG_LONG longlong_member; |
| 4158 | unsigned PY_LONG_LONG ulonglong_member; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4159 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4160 | } all_structmembers; |
| 4161 | |
| 4162 | typedef struct { |
| 4163 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4164 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4165 | } test_structmembers; |
| 4166 | |
| 4167 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4168 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 4169 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 4170 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 4171 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 4172 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 4173 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 4174 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 4175 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 4176 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 4177 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 4178 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 4179 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 4180 | {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL}, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4181 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4182 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 4183 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 4184 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4185 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4186 | }; |
| 4187 | |
| 4188 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4189 | static PyObject * |
| 4190 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 4191 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4192 | static char *keywords[] = { |
| 4193 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 4194 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 4195 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 4196 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4197 | "T_LONGLONG", "T_ULONGLONG", |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4198 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | NULL}; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 4200 | static const char fmt[] = "|bbBhHiIlknfds#" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4201 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | "LK" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4203 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4204 | ; |
| 4205 | test_structmembers *ob; |
| 4206 | const char *s = NULL; |
| 4207 | Py_ssize_t string_len = 0; |
| 4208 | ob = PyObject_New(test_structmembers, type); |
| 4209 | if (ob == NULL) |
| 4210 | return NULL; |
| 4211 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 4212 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 4213 | &ob->structmembers.bool_member, |
| 4214 | &ob->structmembers.byte_member, |
| 4215 | &ob->structmembers.ubyte_member, |
| 4216 | &ob->structmembers.short_member, |
| 4217 | &ob->structmembers.ushort_member, |
| 4218 | &ob->structmembers.int_member, |
| 4219 | &ob->structmembers.uint_member, |
| 4220 | &ob->structmembers.long_member, |
| 4221 | &ob->structmembers.ulong_member, |
| 4222 | &ob->structmembers.pyssizet_member, |
| 4223 | &ob->structmembers.float_member, |
| 4224 | &ob->structmembers.double_member, |
| 4225 | &s, &string_len |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4226 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4227 | , &ob->structmembers.longlong_member, |
| 4228 | &ob->structmembers.ulonglong_member |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4229 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4230 | )) { |
| 4231 | Py_DECREF(ob); |
| 4232 | return NULL; |
| 4233 | } |
| 4234 | if (s != NULL) { |
| 4235 | if (string_len > 5) { |
| 4236 | Py_DECREF(ob); |
| 4237 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 4238 | return NULL; |
| 4239 | } |
| 4240 | strcpy(ob->structmembers.inplace_member, s); |
| 4241 | } |
| 4242 | else { |
| 4243 | strcpy(ob->structmembers.inplace_member, ""); |
| 4244 | } |
| 4245 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4248 | static void |
| 4249 | test_structmembers_free(PyObject *ob) |
| 4250 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4255 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4256 | "test_structmembersType", |
| 4257 | sizeof(test_structmembers), /* tp_basicsize */ |
| 4258 | 0, /* tp_itemsize */ |
| 4259 | test_structmembers_free, /* destructor tp_dealloc */ |
| 4260 | 0, /* tp_print */ |
| 4261 | 0, /* tp_getattr */ |
| 4262 | 0, /* tp_setattr */ |
| 4263 | 0, /* tp_reserved */ |
| 4264 | 0, /* tp_repr */ |
| 4265 | 0, /* tp_as_number */ |
| 4266 | 0, /* tp_as_sequence */ |
| 4267 | 0, /* tp_as_mapping */ |
| 4268 | 0, /* tp_hash */ |
| 4269 | 0, /* tp_call */ |
| 4270 | 0, /* tp_str */ |
| 4271 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4272 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4273 | 0, /* tp_as_buffer */ |
| 4274 | 0, /* tp_flags */ |
| 4275 | "Type containing all structmember types", |
| 4276 | 0, /* traverseproc tp_traverse */ |
| 4277 | 0, /* tp_clear */ |
| 4278 | 0, /* tp_richcompare */ |
| 4279 | 0, /* tp_weaklistoffset */ |
| 4280 | 0, /* tp_iter */ |
| 4281 | 0, /* tp_iternext */ |
| 4282 | 0, /* tp_methods */ |
| 4283 | test_members, /* tp_members */ |
| 4284 | 0, |
| 4285 | 0, |
| 4286 | 0, |
| 4287 | 0, |
| 4288 | 0, |
| 4289 | 0, |
| 4290 | 0, |
| 4291 | 0, |
| 4292 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4293 | }; |
| 4294 | |
| 4295 | |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4296 | typedef struct { |
| 4297 | PyObject_HEAD |
| 4298 | } matmulObject; |
| 4299 | |
| 4300 | static PyObject * |
| 4301 | matmulType_matmul(PyObject *self, PyObject *other) |
| 4302 | { |
| 4303 | return Py_BuildValue("(sOO)", "matmul", self, other); |
| 4304 | } |
| 4305 | |
| 4306 | static PyObject * |
| 4307 | matmulType_imatmul(PyObject *self, PyObject *other) |
| 4308 | { |
| 4309 | return Py_BuildValue("(sOO)", "imatmul", self, other); |
| 4310 | } |
| 4311 | |
| 4312 | static void |
| 4313 | matmulType_dealloc(PyObject *self) |
| 4314 | { |
Zachary Ware | 420dc56 | 2014-04-23 13:51:27 -0500 | [diff] [blame] | 4315 | Py_TYPE(self)->tp_free(self); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | static PyNumberMethods matmulType_as_number = { |
| 4319 | 0, /* nb_add */ |
| 4320 | 0, /* nb_subtract */ |
| 4321 | 0, /* nb_multiply */ |
| 4322 | 0, /* nb_remainde r*/ |
| 4323 | 0, /* nb_divmod */ |
| 4324 | 0, /* nb_power */ |
| 4325 | 0, /* nb_negative */ |
| 4326 | 0, /* tp_positive */ |
| 4327 | 0, /* tp_absolute */ |
| 4328 | 0, /* tp_bool */ |
| 4329 | 0, /* nb_invert */ |
| 4330 | 0, /* nb_lshift */ |
| 4331 | 0, /* nb_rshift */ |
| 4332 | 0, /* nb_and */ |
| 4333 | 0, /* nb_xor */ |
| 4334 | 0, /* nb_or */ |
| 4335 | 0, /* nb_int */ |
| 4336 | 0, /* nb_reserved */ |
| 4337 | 0, /* nb_float */ |
| 4338 | 0, /* nb_inplace_add */ |
| 4339 | 0, /* nb_inplace_subtract */ |
| 4340 | 0, /* nb_inplace_multiply */ |
| 4341 | 0, /* nb_inplace_remainder */ |
| 4342 | 0, /* nb_inplace_power */ |
| 4343 | 0, /* nb_inplace_lshift */ |
| 4344 | 0, /* nb_inplace_rshift */ |
| 4345 | 0, /* nb_inplace_and */ |
| 4346 | 0, /* nb_inplace_xor */ |
| 4347 | 0, /* nb_inplace_or */ |
| 4348 | 0, /* nb_floor_divide */ |
| 4349 | 0, /* nb_true_divide */ |
| 4350 | 0, /* nb_inplace_floor_divide */ |
| 4351 | 0, /* nb_inplace_true_divide */ |
| 4352 | 0, /* nb_index */ |
| 4353 | matmulType_matmul, /* nb_matrix_multiply */ |
| 4354 | matmulType_imatmul /* nb_matrix_inplace_multiply */ |
| 4355 | }; |
| 4356 | |
| 4357 | static PyTypeObject matmulType = { |
| 4358 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4359 | "matmulType", |
| 4360 | sizeof(matmulObject), /* tp_basicsize */ |
| 4361 | 0, /* tp_itemsize */ |
| 4362 | matmulType_dealloc, /* destructor tp_dealloc */ |
| 4363 | 0, /* tp_print */ |
| 4364 | 0, /* tp_getattr */ |
| 4365 | 0, /* tp_setattr */ |
| 4366 | 0, /* tp_reserved */ |
| 4367 | 0, /* tp_repr */ |
| 4368 | &matmulType_as_number, /* tp_as_number */ |
| 4369 | 0, /* tp_as_sequence */ |
| 4370 | 0, /* tp_as_mapping */ |
| 4371 | 0, /* tp_hash */ |
| 4372 | 0, /* tp_call */ |
| 4373 | 0, /* tp_str */ |
| 4374 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4375 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4376 | 0, /* tp_as_buffer */ |
| 4377 | 0, /* tp_flags */ |
| 4378 | "C level type with matrix operations defined", |
| 4379 | 0, /* traverseproc tp_traverse */ |
| 4380 | 0, /* tp_clear */ |
| 4381 | 0, /* tp_richcompare */ |
| 4382 | 0, /* tp_weaklistoffset */ |
| 4383 | 0, /* tp_iter */ |
| 4384 | 0, /* tp_iternext */ |
| 4385 | 0, /* tp_methods */ |
| 4386 | 0, /* tp_members */ |
| 4387 | 0, |
| 4388 | 0, |
| 4389 | 0, |
| 4390 | 0, |
| 4391 | 0, |
| 4392 | 0, |
| 4393 | 0, |
| 4394 | 0, |
| 4395 | PyType_GenericNew, /* tp_new */ |
| 4396 | PyObject_Del, /* tp_free */ |
| 4397 | }; |
| 4398 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4399 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4400 | typedef struct { |
| 4401 | PyObject_HEAD |
| 4402 | PyObject *ao_iterator; |
| 4403 | } awaitObject; |
| 4404 | |
| 4405 | |
| 4406 | static PyObject * |
| 4407 | awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4408 | { |
| 4409 | PyObject *v; |
| 4410 | awaitObject *ao; |
| 4411 | |
| 4412 | if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v)) |
| 4413 | return NULL; |
| 4414 | |
| 4415 | ao = (awaitObject *)type->tp_alloc(type, 0); |
| 4416 | if (ao == NULL) { |
| 4417 | return NULL; |
| 4418 | } |
| 4419 | |
| 4420 | Py_INCREF(v); |
| 4421 | ao->ao_iterator = v; |
| 4422 | |
| 4423 | return (PyObject *)ao; |
| 4424 | } |
| 4425 | |
| 4426 | |
| 4427 | static void |
| 4428 | awaitObject_dealloc(awaitObject *ao) |
| 4429 | { |
| 4430 | Py_CLEAR(ao->ao_iterator); |
| 4431 | Py_TYPE(ao)->tp_free(ao); |
| 4432 | } |
| 4433 | |
| 4434 | |
| 4435 | static PyObject * |
| 4436 | awaitObject_await(awaitObject *ao) |
| 4437 | { |
| 4438 | Py_INCREF(ao->ao_iterator); |
| 4439 | return ao->ao_iterator; |
| 4440 | } |
| 4441 | |
| 4442 | static PyAsyncMethods awaitType_as_async = { |
Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 4443 | (unaryfunc)awaitObject_await, /* am_await */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4444 | 0, /* am_aiter */ |
| 4445 | 0 /* am_anext */ |
| 4446 | }; |
| 4447 | |
| 4448 | |
| 4449 | static PyTypeObject awaitType = { |
| 4450 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4451 | "awaitType", |
| 4452 | sizeof(awaitObject), /* tp_basicsize */ |
| 4453 | 0, /* tp_itemsize */ |
| 4454 | (destructor)awaitObject_dealloc, /* destructor tp_dealloc */ |
| 4455 | 0, /* tp_print */ |
| 4456 | 0, /* tp_getattr */ |
| 4457 | 0, /* tp_setattr */ |
| 4458 | &awaitType_as_async, /* tp_as_async */ |
| 4459 | 0, /* tp_repr */ |
| 4460 | 0, /* tp_as_number */ |
| 4461 | 0, /* tp_as_sequence */ |
| 4462 | 0, /* tp_as_mapping */ |
| 4463 | 0, /* tp_hash */ |
| 4464 | 0, /* tp_call */ |
| 4465 | 0, /* tp_str */ |
| 4466 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4467 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4468 | 0, /* tp_as_buffer */ |
| 4469 | 0, /* tp_flags */ |
| 4470 | "C level type with tp_as_async", |
| 4471 | 0, /* traverseproc tp_traverse */ |
| 4472 | 0, /* tp_clear */ |
| 4473 | 0, /* tp_richcompare */ |
| 4474 | 0, /* tp_weaklistoffset */ |
| 4475 | 0, /* tp_iter */ |
| 4476 | 0, /* tp_iternext */ |
| 4477 | 0, /* tp_methods */ |
| 4478 | 0, /* tp_members */ |
| 4479 | 0, |
| 4480 | 0, |
| 4481 | 0, |
| 4482 | 0, |
| 4483 | 0, |
| 4484 | 0, |
| 4485 | 0, |
| 4486 | 0, |
| 4487 | awaitObject_new, /* tp_new */ |
| 4488 | PyObject_Del, /* tp_free */ |
| 4489 | }; |
| 4490 | |
| 4491 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4492 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4493 | PyModuleDef_HEAD_INIT, |
| 4494 | "_testcapi", |
| 4495 | NULL, |
| 4496 | -1, |
| 4497 | TestMethods, |
| 4498 | NULL, |
| 4499 | NULL, |
| 4500 | NULL, |
| 4501 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4502 | }; |
| 4503 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 4504 | /* Per PEP 489, this module will not be converted to multi-phase initialization |
| 4505 | */ |
| 4506 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 4507 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4508 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | m = PyModule_Create(&_testcapimodule); |
| 4513 | if (m == NULL) |
| 4514 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 4517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 4519 | Py_INCREF(&test_structmembersType); |
| 4520 | /* don't use a name starting with "test", since we don't want |
| 4521 | test_capi to automatically call this */ |
| 4522 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4523 | if (PyType_Ready(&matmulType) < 0) |
| 4524 | return NULL; |
| 4525 | Py_INCREF(&matmulType); |
| 4526 | PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4527 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4528 | if (PyType_Ready(&awaitType) < 0) |
| 4529 | return NULL; |
| 4530 | Py_INCREF(&awaitType); |
| 4531 | PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType); |
| 4532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4533 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 4534 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 4535 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 4536 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 4537 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 4538 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 4539 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 4540 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 4541 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 4542 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 4543 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 4544 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 4545 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 4546 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 4547 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 4548 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 4549 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 4550 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 4551 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 4552 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 4553 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
| 4554 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 4555 | PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4556 | Py_INCREF(&PyInstanceMethod_Type); |
| 4557 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 4558 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 4559 | PyModule_AddIntConstant(m, "the_number_three", 3); |
| 4560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4561 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 4562 | Py_INCREF(TestError); |
| 4563 | PyModule_AddObject(m, "error", TestError); |
| 4564 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4565 | } |