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" |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 14 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 15 | #ifdef WITH_THREAD |
| 16 | #include "pythread.h" |
| 17 | #endif /* WITH_THREAD */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | static PyObject *TestError; /* set to exception object in init */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 19 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 20 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 21 | |
| 22 | static PyObject * |
| 23 | raiseTestError(const char* test_name, const char* msg) |
| 24 | { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 25 | PyErr_Format(TestError, "%s: %s", test_name, msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 29 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 30 | |
| 31 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 32 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 33 | platforms have these hardcoded. Better safe than sorry. |
| 34 | */ |
| 35 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 36 | sizeof_error(const char* fatname, const char* typname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | int expected, int got) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 38 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 39 | PyErr_Format(TestError, |
| 40 | "%s #define == %d but sizeof(%s) == %d", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | fatname, expected, typname, got); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | return (PyObject*)NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 46 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 47 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 48 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | if (FATNAME != sizeof(TYPE)) \ |
| 50 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 51 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 53 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 54 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 55 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 56 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 57 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
| 61 | #undef CHECK_SIZEOF |
| 62 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | Py_INCREF(Py_None); |
| 64 | return Py_None; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 67 | static PyObject* |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 68 | test_sizeof_c_types(PyObject *self) |
| 69 | { |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 70 | #define CHECK_SIZEOF(TYPE, EXPECTED) \ |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 71 | if (EXPECTED != sizeof(TYPE)) { \ |
| 72 | PyErr_Format(TestError, \ |
| 73 | "sizeof(%s) = %u instead of %u", \ |
| 74 | #TYPE, sizeof(TYPE), EXPECTED); \ |
| 75 | return (PyObject*)NULL; \ |
| 76 | } |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 77 | #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0) |
| 78 | #define CHECK_SIGNNESS(TYPE, SIGNED) \ |
| 79 | if (IS_SIGNED(TYPE) != SIGNED) { \ |
| 80 | PyErr_Format(TestError, \ |
| 81 | "%s signness is, instead of %i", \ |
| 82 | #TYPE, IS_SIGNED(TYPE), SIGNED); \ |
| 83 | return (PyObject*)NULL; \ |
| 84 | } |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 85 | |
| 86 | /* integer types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 87 | CHECK_SIZEOF(Py_UCS1, 1); |
| 88 | CHECK_SIZEOF(Py_UCS2, 2); |
| 89 | CHECK_SIZEOF(Py_UCS4, 4); |
| 90 | CHECK_SIGNNESS(Py_UCS1, 0); |
| 91 | CHECK_SIGNNESS(Py_UCS2, 0); |
| 92 | CHECK_SIGNNESS(Py_UCS4, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 93 | #ifdef HAVE_INT32_T |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 94 | CHECK_SIZEOF(PY_INT32_T, 4); |
| 95 | CHECK_SIGNNESS(PY_INT32_T, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 96 | #endif |
| 97 | #ifdef HAVE_UINT32_T |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 98 | CHECK_SIZEOF(PY_UINT32_T, 4); |
| 99 | CHECK_SIGNNESS(PY_UINT32_T, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 100 | #endif |
| 101 | #ifdef HAVE_INT64_T |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 102 | CHECK_SIZEOF(PY_INT64_T, 8); |
| 103 | CHECK_SIGNNESS(PY_INT64_T, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 104 | #endif |
| 105 | #ifdef HAVE_UINT64_T |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 106 | CHECK_SIZEOF(PY_UINT64_T, 8); |
| 107 | CHECK_SIGNNESS(PY_UINT64_T, 0); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | /* pointer/size types */ |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 111 | CHECK_SIZEOF(size_t, sizeof(void *)); |
| 112 | CHECK_SIGNNESS(size_t, 0); |
| 113 | CHECK_SIZEOF(Py_ssize_t, sizeof(void *)); |
| 114 | CHECK_SIGNNESS(Py_ssize_t, 1); |
| 115 | |
| 116 | CHECK_SIZEOF(Py_uintptr_t, sizeof(void *)); |
| 117 | CHECK_SIGNNESS(Py_uintptr_t, 0); |
| 118 | CHECK_SIZEOF(Py_intptr_t, sizeof(void *)); |
| 119 | CHECK_SIGNNESS(Py_intptr_t, 1); |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 120 | |
| 121 | Py_INCREF(Py_None); |
| 122 | return Py_None; |
| 123 | |
Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 124 | #undef IS_SIGNED |
| 125 | #undef CHECK_SIGNESS |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 126 | #undef CHECK_SIZEOF |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 131 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | PyObject* list; |
| 134 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 137 | #define NLIST 30 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | list = PyList_New(NLIST); |
| 139 | if (list == (PyObject*)NULL) |
| 140 | return (PyObject*)NULL; |
| 141 | /* list = range(NLIST) */ |
| 142 | for (i = 0; i < NLIST; ++i) { |
| 143 | PyObject* anint = PyLong_FromLong(i); |
| 144 | if (anint == (PyObject*)NULL) { |
| 145 | Py_DECREF(list); |
| 146 | return (PyObject*)NULL; |
| 147 | } |
| 148 | PyList_SET_ITEM(list, i, anint); |
| 149 | } |
| 150 | /* list.reverse(), via PyList_Reverse() */ |
| 151 | i = PyList_Reverse(list); /* should not blow up! */ |
| 152 | if (i != 0) { |
| 153 | Py_DECREF(list); |
| 154 | return (PyObject*)NULL; |
| 155 | } |
| 156 | /* Check that list == range(29, -1, -1) now */ |
| 157 | for (i = 0; i < NLIST; ++i) { |
| 158 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 159 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 160 | PyErr_SetString(TestError, |
| 161 | "test_list_api: reverse screwed up"); |
| 162 | Py_DECREF(list); |
| 163 | return (PyObject*)NULL; |
| 164 | } |
| 165 | } |
| 166 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 167 | #undef NLIST |
| 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | Py_INCREF(Py_None); |
| 170 | return Py_None; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 173 | static int |
| 174 | test_dict_inner(int count) |
| 175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | Py_ssize_t pos = 0, iterations = 0; |
| 177 | int i; |
| 178 | PyObject *dict = PyDict_New(); |
| 179 | PyObject *v, *k; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | if (dict == NULL) |
| 182 | return -1; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | for (i = 0; i < count; i++) { |
| 185 | v = PyLong_FromLong(i); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 186 | if (v == NULL) { |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 187 | return -1; |
| 188 | } |
Christian Heimes | 97cb67b | 2013-07-20 15:01:26 +0200 | [diff] [blame] | 189 | if (PyDict_SetItem(dict, v, v) < 0) { |
| 190 | Py_DECREF(v); |
| 191 | return -1; |
| 192 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | Py_DECREF(v); |
| 194 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 197 | PyObject *o; |
| 198 | iterations++; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 200 | i = PyLong_AS_LONG(v) + 1; |
| 201 | o = PyLong_FromLong(i); |
| 202 | if (o == NULL) |
| 203 | return -1; |
| 204 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 205 | Py_DECREF(o); |
| 206 | return -1; |
| 207 | } |
| 208 | Py_DECREF(o); |
| 209 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | Py_DECREF(dict); |
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 | if (iterations != count) { |
| 214 | PyErr_SetString( |
| 215 | TestError, |
| 216 | "test_dict_iteration: dict iteration went wrong "); |
| 217 | return -1; |
| 218 | } else { |
| 219 | return 0; |
| 220 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 224 | test_dict_iteration(PyObject* self) |
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 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | for (i = 0; i < 200; i++) { |
| 229 | if (test_dict_inner(i) < 0) { |
| 230 | return NULL; |
| 231 | } |
| 232 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | Py_INCREF(Py_None); |
| 235 | return Py_None; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 238 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 239 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 240 | * PyType_Ready if it hasn't already been called |
| 241 | */ |
| 242 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | PyVarObject_HEAD_INIT(NULL, 0) |
| 244 | "hashinheritancetester", /* Name of this type */ |
| 245 | sizeof(PyObject), /* Basic object size */ |
| 246 | 0, /* Item size for varobject */ |
| 247 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 248 | 0, /* tp_print */ |
| 249 | 0, /* tp_getattr */ |
| 250 | 0, /* tp_setattr */ |
| 251 | 0, /* tp_reserved */ |
| 252 | 0, /* tp_repr */ |
| 253 | 0, /* tp_as_number */ |
| 254 | 0, /* tp_as_sequence */ |
| 255 | 0, /* tp_as_mapping */ |
| 256 | 0, /* tp_hash */ |
| 257 | 0, /* tp_call */ |
| 258 | 0, /* tp_str */ |
| 259 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 260 | 0, /* tp_setattro */ |
| 261 | 0, /* tp_as_buffer */ |
| 262 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 263 | 0, /* tp_doc */ |
| 264 | 0, /* tp_traverse */ |
| 265 | 0, /* tp_clear */ |
| 266 | 0, /* tp_richcompare */ |
| 267 | 0, /* tp_weaklistoffset */ |
| 268 | 0, /* tp_iter */ |
| 269 | 0, /* tp_iternext */ |
| 270 | 0, /* tp_methods */ |
| 271 | 0, /* tp_members */ |
| 272 | 0, /* tp_getset */ |
| 273 | 0, /* tp_base */ |
| 274 | 0, /* tp_dict */ |
| 275 | 0, /* tp_descr_get */ |
| 276 | 0, /* tp_descr_set */ |
| 277 | 0, /* tp_dictoffset */ |
| 278 | 0, /* tp_init */ |
| 279 | 0, /* tp_alloc */ |
| 280 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static PyObject* |
| 284 | test_lazy_hash_inheritance(PyObject* self) |
| 285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | PyTypeObject *type; |
| 287 | PyObject *obj; |
Antoine Pitrou | 29aad00 | 2010-10-23 19:42:38 +0000 | [diff] [blame] | 288 | Py_hash_t hash; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | if (type->tp_dict != NULL) |
| 293 | /* The type has already been initialized. This probably means |
| 294 | -R is being used. */ |
| 295 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 296 | |
| 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | obj = PyObject_New(PyObject, type); |
| 299 | if (obj == NULL) { |
| 300 | PyErr_Clear(); |
| 301 | PyErr_SetString( |
| 302 | TestError, |
| 303 | "test_lazy_hash_inheritance: failed to create object"); |
| 304 | return NULL; |
| 305 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | if (type->tp_dict != NULL) { |
| 308 | PyErr_SetString( |
| 309 | TestError, |
| 310 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 311 | Py_DECREF(obj); |
| 312 | return NULL; |
| 313 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | hash = PyObject_Hash(obj); |
| 316 | if ((hash == -1) && PyErr_Occurred()) { |
| 317 | PyErr_Clear(); |
| 318 | PyErr_SetString( |
| 319 | TestError, |
| 320 | "test_lazy_hash_inheritance: could not hash object"); |
| 321 | Py_DECREF(obj); |
| 322 | return NULL; |
| 323 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | if (type->tp_dict == NULL) { |
| 326 | PyErr_SetString( |
| 327 | TestError, |
| 328 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 329 | Py_DECREF(obj); |
| 330 | return NULL; |
| 331 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 334 | PyErr_SetString( |
| 335 | TestError, |
| 336 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 337 | Py_DECREF(obj); |
| 338 | return NULL; |
| 339 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | Py_RETURN_NONE; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 347 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 348 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 349 | |
| 350 | Note that the meat of the test is contained in testcapi_long.h. |
| 351 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 352 | 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] | 353 | dependence on type names makes it impossible to use a parameterized |
| 354 | function. A giant macro would be even worse than this. A C++ template |
| 355 | would be perfect. |
| 356 | |
| 357 | The "report an error" functions are deliberately not part of the #include |
| 358 | file: if the test fails, you can set a breakpoint in the appropriate |
| 359 | error function directly, and crawl back from there in the debugger. |
| 360 | */ |
| 361 | |
| 362 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 363 | |
| 364 | static PyObject * |
| 365 | raise_test_long_error(const char* msg) |
| 366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | #define TESTNAME test_long_api_inner |
| 371 | #define TYPENAME long |
| 372 | #define F_S_TO_PY PyLong_FromLong |
| 373 | #define F_PY_TO_S PyLong_AsLong |
| 374 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 375 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 376 | |
| 377 | #include "testcapi_long.h" |
| 378 | |
| 379 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 380 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 381 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | #undef TESTNAME |
| 386 | #undef TYPENAME |
| 387 | #undef F_S_TO_PY |
| 388 | #undef F_PY_TO_S |
| 389 | #undef F_U_TO_PY |
| 390 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 391 | |
| 392 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 393 | |
| 394 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 395 | raise_test_longlong_error(const char* msg) |
| 396 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | #define TESTNAME test_longlong_api_inner |
| 401 | #define TYPENAME PY_LONG_LONG |
| 402 | #define F_S_TO_PY PyLong_FromLongLong |
| 403 | #define F_PY_TO_S PyLong_AsLongLong |
| 404 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 405 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 406 | |
| 407 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 408 | |
| 409 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 410 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 415 | #undef TESTNAME |
| 416 | #undef TYPENAME |
| 417 | #undef F_S_TO_PY |
| 418 | #undef F_PY_TO_S |
| 419 | #undef F_U_TO_PY |
| 420 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 421 | |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 422 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 423 | is tested by test_long_api_inner. This test will concentrate on proper |
| 424 | handling of overflow. |
| 425 | */ |
| 426 | |
| 427 | static PyObject * |
| 428 | test_long_and_overflow(PyObject *self) |
| 429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | PyObject *num, *one, *temp; |
| 431 | long value; |
| 432 | int overflow; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | /* Test that overflow is set properly for a large value. */ |
| 435 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 436 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 437 | if (num == NULL) |
| 438 | return NULL; |
| 439 | overflow = 1234; |
| 440 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 441 | Py_DECREF(num); |
| 442 | if (value == -1 && PyErr_Occurred()) |
| 443 | return NULL; |
| 444 | if (value != -1) |
| 445 | return raiseTestError("test_long_and_overflow", |
| 446 | "return value was not set to -1"); |
| 447 | if (overflow != 1) |
| 448 | return raiseTestError("test_long_and_overflow", |
| 449 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | /* Same again, with num = LONG_MAX + 1 */ |
| 452 | num = PyLong_FromLong(LONG_MAX); |
| 453 | if (num == NULL) |
| 454 | return NULL; |
| 455 | one = PyLong_FromLong(1L); |
| 456 | if (one == NULL) { |
| 457 | Py_DECREF(num); |
| 458 | return NULL; |
| 459 | } |
| 460 | temp = PyNumber_Add(num, one); |
| 461 | Py_DECREF(one); |
| 462 | Py_DECREF(num); |
| 463 | num = temp; |
| 464 | if (num == NULL) |
| 465 | return NULL; |
| 466 | overflow = 0; |
| 467 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 468 | Py_DECREF(num); |
| 469 | if (value == -1 && PyErr_Occurred()) |
| 470 | return NULL; |
| 471 | if (value != -1) |
| 472 | return raiseTestError("test_long_and_overflow", |
| 473 | "return value was not set to -1"); |
| 474 | if (overflow != 1) |
| 475 | return raiseTestError("test_long_and_overflow", |
| 476 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | /* Test that overflow is set properly for a large negative value. */ |
| 479 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 480 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 481 | if (num == NULL) |
| 482 | return NULL; |
| 483 | overflow = 1234; |
| 484 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 485 | Py_DECREF(num); |
| 486 | if (value == -1 && PyErr_Occurred()) |
| 487 | return NULL; |
| 488 | if (value != -1) |
| 489 | return raiseTestError("test_long_and_overflow", |
| 490 | "return value was not set to -1"); |
| 491 | if (overflow != -1) |
| 492 | return raiseTestError("test_long_and_overflow", |
| 493 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | /* Same again, with num = LONG_MIN - 1 */ |
| 496 | num = PyLong_FromLong(LONG_MIN); |
| 497 | if (num == NULL) |
| 498 | return NULL; |
| 499 | one = PyLong_FromLong(1L); |
| 500 | if (one == NULL) { |
| 501 | Py_DECREF(num); |
| 502 | return NULL; |
| 503 | } |
| 504 | temp = PyNumber_Subtract(num, one); |
| 505 | Py_DECREF(one); |
| 506 | Py_DECREF(num); |
| 507 | num = temp; |
| 508 | if (num == NULL) |
| 509 | return NULL; |
| 510 | overflow = 0; |
| 511 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 512 | Py_DECREF(num); |
| 513 | if (value == -1 && PyErr_Occurred()) |
| 514 | return NULL; |
| 515 | if (value != -1) |
| 516 | return raiseTestError("test_long_and_overflow", |
| 517 | "return value was not set to -1"); |
| 518 | if (overflow != -1) |
| 519 | return raiseTestError("test_long_and_overflow", |
| 520 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | /* Test that overflow is cleared properly for small values. */ |
| 523 | num = PyLong_FromString("FF", NULL, 16); |
| 524 | if (num == NULL) |
| 525 | return NULL; |
| 526 | overflow = 1234; |
| 527 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 528 | Py_DECREF(num); |
| 529 | if (value == -1 && PyErr_Occurred()) |
| 530 | return NULL; |
| 531 | if (value != 0xFF) |
| 532 | return raiseTestError("test_long_and_overflow", |
| 533 | "expected return value 0xFF"); |
| 534 | if (overflow != 0) |
| 535 | return raiseTestError("test_long_and_overflow", |
| 536 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | num = PyLong_FromString("-FF", NULL, 16); |
| 539 | if (num == NULL) |
| 540 | return NULL; |
| 541 | overflow = 0; |
| 542 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 543 | Py_DECREF(num); |
| 544 | if (value == -1 && PyErr_Occurred()) |
| 545 | return NULL; |
| 546 | if (value != -0xFF) |
| 547 | return raiseTestError("test_long_and_overflow", |
| 548 | "expected return value 0xFF"); |
| 549 | if (overflow != 0) |
| 550 | return raiseTestError("test_long_and_overflow", |
| 551 | "overflow was set incorrectly"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | num = PyLong_FromLong(LONG_MAX); |
| 554 | if (num == NULL) |
| 555 | return NULL; |
| 556 | overflow = 1234; |
| 557 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 558 | Py_DECREF(num); |
| 559 | if (value == -1 && PyErr_Occurred()) |
| 560 | return NULL; |
| 561 | if (value != LONG_MAX) |
| 562 | return raiseTestError("test_long_and_overflow", |
| 563 | "expected return value LONG_MAX"); |
| 564 | if (overflow != 0) |
| 565 | return raiseTestError("test_long_and_overflow", |
| 566 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | num = PyLong_FromLong(LONG_MIN); |
| 569 | if (num == NULL) |
| 570 | return NULL; |
| 571 | overflow = 0; |
| 572 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 573 | Py_DECREF(num); |
| 574 | if (value == -1 && PyErr_Occurred()) |
| 575 | return NULL; |
| 576 | if (value != LONG_MIN) |
| 577 | return raiseTestError("test_long_and_overflow", |
| 578 | "expected return value LONG_MIN"); |
| 579 | if (overflow != 0) |
| 580 | return raiseTestError("test_long_and_overflow", |
| 581 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | Py_INCREF(Py_None); |
| 584 | return Py_None; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 587 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
| 588 | PY_LONG_LONG is tested by test_long_api_inner. This test will |
| 589 | concentrate on proper handling of overflow. |
| 590 | */ |
| 591 | |
| 592 | static PyObject * |
| 593 | test_long_long_and_overflow(PyObject *self) |
| 594 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | PyObject *num, *one, *temp; |
| 596 | PY_LONG_LONG value; |
| 597 | int overflow; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | /* Test that overflow is set properly for a large value. */ |
| 600 | /* num is a number larger than PY_LLONG_MAX on a typical machine. */ |
| 601 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 602 | if (num == NULL) |
| 603 | return NULL; |
| 604 | overflow = 1234; |
| 605 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 606 | Py_DECREF(num); |
| 607 | if (value == -1 && PyErr_Occurred()) |
| 608 | return NULL; |
| 609 | if (value != -1) |
| 610 | return raiseTestError("test_long_long_and_overflow", |
| 611 | "return value was not set to -1"); |
| 612 | if (overflow != 1) |
| 613 | return raiseTestError("test_long_long_and_overflow", |
| 614 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | /* Same again, with num = PY_LLONG_MAX + 1 */ |
| 617 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 618 | if (num == NULL) |
| 619 | return NULL; |
| 620 | one = PyLong_FromLong(1L); |
| 621 | if (one == NULL) { |
| 622 | Py_DECREF(num); |
| 623 | return NULL; |
| 624 | } |
| 625 | temp = PyNumber_Add(num, one); |
| 626 | Py_DECREF(one); |
| 627 | Py_DECREF(num); |
| 628 | num = temp; |
| 629 | if (num == NULL) |
| 630 | return NULL; |
| 631 | overflow = 0; |
| 632 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 633 | Py_DECREF(num); |
| 634 | if (value == -1 && PyErr_Occurred()) |
| 635 | return NULL; |
| 636 | if (value != -1) |
| 637 | return raiseTestError("test_long_long_and_overflow", |
| 638 | "return value was not set to -1"); |
| 639 | if (overflow != 1) |
| 640 | return raiseTestError("test_long_long_and_overflow", |
| 641 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | /* Test that overflow is set properly for a large negative value. */ |
| 644 | /* num is a number smaller than PY_LLONG_MIN on a typical platform */ |
| 645 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 646 | if (num == NULL) |
| 647 | return NULL; |
| 648 | overflow = 1234; |
| 649 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 650 | Py_DECREF(num); |
| 651 | if (value == -1 && PyErr_Occurred()) |
| 652 | return NULL; |
| 653 | if (value != -1) |
| 654 | return raiseTestError("test_long_long_and_overflow", |
| 655 | "return value was not set to -1"); |
| 656 | if (overflow != -1) |
| 657 | return raiseTestError("test_long_long_and_overflow", |
| 658 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | /* Same again, with num = PY_LLONG_MIN - 1 */ |
| 661 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 662 | if (num == NULL) |
| 663 | return NULL; |
| 664 | one = PyLong_FromLong(1L); |
| 665 | if (one == NULL) { |
| 666 | Py_DECREF(num); |
| 667 | return NULL; |
| 668 | } |
| 669 | temp = PyNumber_Subtract(num, one); |
| 670 | Py_DECREF(one); |
| 671 | Py_DECREF(num); |
| 672 | num = temp; |
| 673 | if (num == NULL) |
| 674 | return NULL; |
| 675 | overflow = 0; |
| 676 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 677 | Py_DECREF(num); |
| 678 | if (value == -1 && PyErr_Occurred()) |
| 679 | return NULL; |
| 680 | if (value != -1) |
| 681 | return raiseTestError("test_long_long_and_overflow", |
| 682 | "return value was not set to -1"); |
| 683 | if (overflow != -1) |
| 684 | return raiseTestError("test_long_long_and_overflow", |
| 685 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | /* Test that overflow is cleared properly for small values. */ |
| 688 | num = PyLong_FromString("FF", NULL, 16); |
| 689 | if (num == NULL) |
| 690 | return NULL; |
| 691 | overflow = 1234; |
| 692 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 693 | Py_DECREF(num); |
| 694 | if (value == -1 && PyErr_Occurred()) |
| 695 | return NULL; |
| 696 | if (value != 0xFF) |
| 697 | return raiseTestError("test_long_long_and_overflow", |
| 698 | "expected return value 0xFF"); |
| 699 | if (overflow != 0) |
| 700 | return raiseTestError("test_long_long_and_overflow", |
| 701 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | num = PyLong_FromString("-FF", NULL, 16); |
| 704 | if (num == NULL) |
| 705 | return NULL; |
| 706 | overflow = 0; |
| 707 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 708 | Py_DECREF(num); |
| 709 | if (value == -1 && PyErr_Occurred()) |
| 710 | return NULL; |
| 711 | if (value != -0xFF) |
| 712 | return raiseTestError("test_long_long_and_overflow", |
| 713 | "expected return value 0xFF"); |
| 714 | if (overflow != 0) |
| 715 | return raiseTestError("test_long_long_and_overflow", |
| 716 | "overflow was set incorrectly"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 719 | if (num == NULL) |
| 720 | return NULL; |
| 721 | overflow = 1234; |
| 722 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 723 | Py_DECREF(num); |
| 724 | if (value == -1 && PyErr_Occurred()) |
| 725 | return NULL; |
| 726 | if (value != PY_LLONG_MAX) |
| 727 | return raiseTestError("test_long_long_and_overflow", |
| 728 | "expected return value PY_LLONG_MAX"); |
| 729 | if (overflow != 0) |
| 730 | return raiseTestError("test_long_long_and_overflow", |
| 731 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 734 | if (num == NULL) |
| 735 | return NULL; |
| 736 | overflow = 0; |
| 737 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 738 | Py_DECREF(num); |
| 739 | if (value == -1 && PyErr_Occurred()) |
| 740 | return NULL; |
| 741 | if (value != PY_LLONG_MIN) |
| 742 | return raiseTestError("test_long_long_and_overflow", |
| 743 | "expected return value PY_LLONG_MIN"); |
| 744 | if (overflow != 0) |
| 745 | return raiseTestError("test_long_long_and_overflow", |
| 746 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | Py_INCREF(Py_None); |
| 749 | return Py_None; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 752 | /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that |
| 753 | non-integer arguments are handled correctly. It should be extended to |
| 754 | test overflow handling. |
| 755 | */ |
| 756 | |
| 757 | static PyObject * |
| 758 | test_long_as_size_t(PyObject *self) |
| 759 | { |
| 760 | size_t out_u; |
| 761 | Py_ssize_t out_s; |
| 762 | |
| 763 | Py_INCREF(Py_None); |
| 764 | |
| 765 | out_u = PyLong_AsSize_t(Py_None); |
| 766 | if (out_u != (size_t)-1 || !PyErr_Occurred()) |
| 767 | return raiseTestError("test_long_as_size_t", |
| 768 | "PyLong_AsSize_t(None) didn't complain"); |
| 769 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 770 | return raiseTestError("test_long_as_size_t", |
| 771 | "PyLong_AsSize_t(None) raised " |
| 772 | "something other than TypeError"); |
| 773 | PyErr_Clear(); |
| 774 | |
| 775 | out_s = PyLong_AsSsize_t(Py_None); |
| 776 | if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) |
| 777 | return raiseTestError("test_long_as_size_t", |
| 778 | "PyLong_AsSsize_t(None) didn't complain"); |
| 779 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 780 | return raiseTestError("test_long_as_size_t", |
| 781 | "PyLong_AsSsize_t(None) raised " |
| 782 | "something other than TypeError"); |
| 783 | PyErr_Clear(); |
| 784 | |
| 785 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 786 | return Py_None; |
| 787 | } |
| 788 | |
| 789 | /* Test the PyLong_AsDouble API. At present this just tests that |
| 790 | non-integer arguments are handled correctly. |
| 791 | */ |
| 792 | |
| 793 | static PyObject * |
| 794 | test_long_as_double(PyObject *self) |
| 795 | { |
| 796 | double out; |
| 797 | |
| 798 | Py_INCREF(Py_None); |
| 799 | |
| 800 | out = PyLong_AsDouble(Py_None); |
| 801 | if (out != -1.0 || !PyErr_Occurred()) |
| 802 | return raiseTestError("test_long_as_double", |
| 803 | "PyLong_AsDouble(None) didn't complain"); |
| 804 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 805 | return raiseTestError("test_long_as_double", |
| 806 | "PyLong_AsDouble(None) raised " |
| 807 | "something other than TypeError"); |
| 808 | PyErr_Clear(); |
| 809 | |
| 810 | /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ |
| 811 | return Py_None; |
| 812 | } |
| 813 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 814 | /* 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] | 815 | for both long and int arguments. The test may leak a little memory if |
| 816 | it fails. |
| 817 | */ |
| 818 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 819 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 820 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | PyObject *tuple, *num; |
| 822 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | tuple = PyTuple_New(1); |
| 825 | if (tuple == NULL) |
| 826 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | num = PyLong_FromLong(42); |
| 829 | if (num == NULL) |
| 830 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | PyTuple_SET_ITEM(tuple, 0, num); |
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 | value = -1; |
| 835 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 836 | return NULL; |
| 837 | if (value != 42) |
| 838 | return raiseTestError("test_L_code", |
| 839 | "L code returned wrong value for long 42"); |
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 | Py_DECREF(num); |
| 842 | num = PyLong_FromLong(42); |
| 843 | if (num == NULL) |
| 844 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | value = -1; |
| 849 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 850 | return NULL; |
| 851 | if (value != 42) |
| 852 | return raiseTestError("test_L_code", |
| 853 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | Py_DECREF(tuple); |
| 856 | Py_INCREF(Py_None); |
| 857 | return Py_None; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | #endif /* ifdef HAVE_LONG_LONG */ |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 861 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 862 | /* Test tuple argument processing */ |
| 863 | static PyObject * |
| 864 | getargs_tuple(PyObject *self, PyObject *args) |
| 865 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | int a, b, c; |
| 867 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 868 | return NULL; |
| 869 | return Py_BuildValue("iii", a, b, c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 872 | /* test PyArg_ParseTupleAndKeywords */ |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 873 | static PyObject * |
| 874 | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 875 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 877 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 878 | 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] | 879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 881 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 882 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 883 | return NULL; |
| 884 | return Py_BuildValue("iiiiiiiiii", |
| 885 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 886 | 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] | 887 | } |
| 888 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 889 | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
| 890 | static PyObject * |
| 891 | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
| 892 | { |
| 893 | static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
| 894 | int required = -1; |
| 895 | int optional = -1; |
| 896 | int keyword_only = -1; |
| 897 | |
| 898 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
| 899 | &required, &optional, &keyword_only)) |
| 900 | return NULL; |
| 901 | return Py_BuildValue("iii", required, optional, keyword_only); |
| 902 | } |
| 903 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 904 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 905 | and return the result. |
| 906 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 907 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 908 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | unsigned char value; |
| 911 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 912 | return NULL; |
| 913 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 916 | static PyObject * |
| 917 | getargs_B(PyObject *self, PyObject *args) |
| 918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | unsigned char value; |
| 920 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 921 | return NULL; |
| 922 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 926 | getargs_h(PyObject *self, PyObject *args) |
| 927 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | short value; |
| 929 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 930 | return NULL; |
| 931 | return PyLong_FromLong((long)value); |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 935 | getargs_H(PyObject *self, PyObject *args) |
| 936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | unsigned short value; |
| 938 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 939 | return NULL; |
| 940 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static PyObject * |
| 944 | getargs_I(PyObject *self, PyObject *args) |
| 945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | unsigned int value; |
| 947 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 948 | return NULL; |
| 949 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | static PyObject * |
| 953 | getargs_k(PyObject *self, PyObject *args) |
| 954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | unsigned long value; |
| 956 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 957 | return NULL; |
| 958 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static PyObject * |
| 962 | getargs_i(PyObject *self, PyObject *args) |
| 963 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | int value; |
| 965 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 966 | return NULL; |
| 967 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 970 | static PyObject * |
| 971 | getargs_l(PyObject *self, PyObject *args) |
| 972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | long value; |
| 974 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 975 | return NULL; |
| 976 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 979 | static PyObject * |
| 980 | getargs_n(PyObject *self, PyObject *args) |
| 981 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | Py_ssize_t value; |
| 983 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 984 | return NULL; |
| 985 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 988 | static PyObject * |
| 989 | getargs_p(PyObject *self, PyObject *args) |
| 990 | { |
| 991 | int value; |
| 992 | if (!PyArg_ParseTuple(args, "p", &value)) |
| 993 | return NULL; |
| 994 | return PyLong_FromLong(value); |
| 995 | } |
| 996 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 997 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 998 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 999 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1000 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | PY_LONG_LONG value; |
| 1002 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 1003 | return NULL; |
| 1004 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1007 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1008 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1009 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | unsigned PY_LONG_LONG value; |
| 1011 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 1012 | return NULL; |
| 1013 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1014 | } |
| 1015 | #endif |
| 1016 | |
| 1017 | /* This function not only tests the 'k' getargs code, but also the |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1018 | PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1019 | static PyObject * |
| 1020 | test_k_code(PyObject *self) |
| 1021 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | PyObject *tuple, *num; |
| 1023 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1024 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | tuple = PyTuple_New(1); |
| 1026 | if (tuple == NULL) |
| 1027 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 1030 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 1031 | if (num == NULL) |
| 1032 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | value = PyLong_AsUnsignedLongMask(num); |
| 1035 | if (value != ULONG_MAX) |
| 1036 | return raiseTestError("test_k_code", |
| 1037 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | value = 0; |
| 1042 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 1043 | return NULL; |
| 1044 | if (value != ULONG_MAX) |
| 1045 | return raiseTestError("test_k_code", |
| 1046 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | Py_DECREF(num); |
| 1049 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 1050 | if (num == NULL) |
| 1051 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | value = PyLong_AsUnsignedLongMask(num); |
| 1054 | if (value != (unsigned long)-0x42) |
| 1055 | return raiseTestError("test_k_code", |
| 1056 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | value = 0; |
| 1061 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 1062 | return NULL; |
| 1063 | if (value != (unsigned long)-0x42) |
| 1064 | return raiseTestError("test_k_code", |
| 1065 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | Py_DECREF(tuple); |
| 1068 | Py_INCREF(Py_None); |
| 1069 | return Py_None; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1072 | static PyObject * |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1073 | getargs_c(PyObject *self, PyObject *args) |
| 1074 | { |
| 1075 | char c; |
| 1076 | if (!PyArg_ParseTuple(args, "c", &c)) |
| 1077 | return NULL; |
| 1078 | return PyBytes_FromStringAndSize(&c, 1); |
| 1079 | } |
| 1080 | |
| 1081 | static PyObject * |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1082 | getargs_s(PyObject *self, PyObject *args) |
| 1083 | { |
| 1084 | char *str; |
| 1085 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1086 | return NULL; |
| 1087 | return PyBytes_FromString(str); |
| 1088 | } |
| 1089 | |
| 1090 | static PyObject * |
| 1091 | getargs_s_star(PyObject *self, PyObject *args) |
| 1092 | { |
| 1093 | Py_buffer buffer; |
| 1094 | PyObject *bytes; |
| 1095 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1096 | return NULL; |
| 1097 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1098 | PyBuffer_Release(&buffer); |
| 1099 | return bytes; |
| 1100 | } |
| 1101 | |
| 1102 | static PyObject * |
| 1103 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1104 | { |
| 1105 | char *str; |
| 1106 | Py_ssize_t size; |
| 1107 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1108 | return NULL; |
| 1109 | return PyBytes_FromStringAndSize(str, size); |
| 1110 | } |
| 1111 | |
| 1112 | static PyObject * |
| 1113 | getargs_z(PyObject *self, PyObject *args) |
| 1114 | { |
| 1115 | char *str; |
| 1116 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1117 | return NULL; |
| 1118 | if (str != NULL) |
| 1119 | return PyBytes_FromString(str); |
| 1120 | else |
| 1121 | Py_RETURN_NONE; |
| 1122 | } |
| 1123 | |
| 1124 | static PyObject * |
| 1125 | getargs_z_star(PyObject *self, PyObject *args) |
| 1126 | { |
| 1127 | Py_buffer buffer; |
| 1128 | PyObject *bytes; |
| 1129 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1130 | return NULL; |
| 1131 | if (buffer.buf != NULL) |
| 1132 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1133 | else { |
| 1134 | Py_INCREF(Py_None); |
| 1135 | bytes = Py_None; |
| 1136 | } |
| 1137 | PyBuffer_Release(&buffer); |
| 1138 | return bytes; |
| 1139 | } |
| 1140 | |
| 1141 | static PyObject * |
| 1142 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1143 | { |
| 1144 | char *str; |
| 1145 | Py_ssize_t size; |
| 1146 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1147 | return NULL; |
| 1148 | if (str != NULL) |
| 1149 | return PyBytes_FromStringAndSize(str, size); |
| 1150 | else |
| 1151 | Py_RETURN_NONE; |
| 1152 | } |
| 1153 | |
| 1154 | static PyObject * |
| 1155 | getargs_y(PyObject *self, PyObject *args) |
| 1156 | { |
| 1157 | char *str; |
| 1158 | if (!PyArg_ParseTuple(args, "y", &str)) |
| 1159 | return NULL; |
| 1160 | return PyBytes_FromString(str); |
| 1161 | } |
| 1162 | |
| 1163 | static PyObject * |
| 1164 | getargs_y_star(PyObject *self, PyObject *args) |
| 1165 | { |
| 1166 | Py_buffer buffer; |
| 1167 | PyObject *bytes; |
| 1168 | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 1169 | return NULL; |
| 1170 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1171 | PyBuffer_Release(&buffer); |
| 1172 | return bytes; |
| 1173 | } |
| 1174 | |
| 1175 | static PyObject * |
| 1176 | getargs_y_hash(PyObject *self, PyObject *args) |
| 1177 | { |
| 1178 | char *str; |
| 1179 | Py_ssize_t size; |
| 1180 | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
| 1181 | return NULL; |
| 1182 | return PyBytes_FromStringAndSize(str, size); |
| 1183 | } |
| 1184 | |
| 1185 | static PyObject * |
| 1186 | getargs_u(PyObject *self, PyObject *args) |
| 1187 | { |
| 1188 | Py_UNICODE *str; |
| 1189 | Py_ssize_t size; |
| 1190 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1191 | return NULL; |
| 1192 | size = Py_UNICODE_strlen(str); |
| 1193 | return PyUnicode_FromUnicode(str, size); |
| 1194 | } |
| 1195 | |
| 1196 | static PyObject * |
| 1197 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1198 | { |
| 1199 | Py_UNICODE *str; |
| 1200 | Py_ssize_t size; |
| 1201 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1202 | return NULL; |
| 1203 | return PyUnicode_FromUnicode(str, size); |
| 1204 | } |
| 1205 | |
| 1206 | static PyObject * |
| 1207 | getargs_Z(PyObject *self, PyObject *args) |
| 1208 | { |
| 1209 | Py_UNICODE *str; |
| 1210 | Py_ssize_t size; |
| 1211 | if (!PyArg_ParseTuple(args, "Z", &str)) |
| 1212 | return NULL; |
| 1213 | if (str != NULL) { |
| 1214 | size = Py_UNICODE_strlen(str); |
| 1215 | return PyUnicode_FromUnicode(str, size); |
| 1216 | } else |
| 1217 | Py_RETURN_NONE; |
| 1218 | } |
| 1219 | |
| 1220 | static PyObject * |
| 1221 | getargs_Z_hash(PyObject *self, PyObject *args) |
| 1222 | { |
| 1223 | Py_UNICODE *str; |
| 1224 | Py_ssize_t size; |
| 1225 | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
| 1226 | return NULL; |
| 1227 | if (str != NULL) |
| 1228 | return PyUnicode_FromUnicode(str, size); |
| 1229 | else |
| 1230 | Py_RETURN_NONE; |
| 1231 | } |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1232 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1233 | /* Test the s and z codes for PyArg_ParseTuple. |
| 1234 | */ |
| 1235 | static PyObject * |
| 1236 | test_s_code(PyObject *self) |
| 1237 | { |
| 1238 | /* Unicode strings should be accepted */ |
| 1239 | PyObject *tuple, *obj; |
| 1240 | char *value; |
| 1241 | |
| 1242 | tuple = PyTuple_New(1); |
| 1243 | if (tuple == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1245 | |
| 1246 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1248 | if (obj == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1250 | |
| 1251 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1252 | |
| 1253 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1255 | */ |
| 1256 | if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1261 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1262 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1263 | Py_RETURN_NONE; |
| 1264 | } |
| 1265 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1266 | static PyObject * |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1267 | parse_tuple_and_keywords(PyObject *self, PyObject *args) |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1268 | { |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1269 | PyObject *sub_args; |
| 1270 | PyObject *sub_kwargs; |
| 1271 | char *sub_format; |
| 1272 | PyObject *sub_keywords; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1273 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1274 | Py_ssize_t i, size; |
| 1275 | char *keywords[8 + 1]; /* space for NULL at end */ |
| 1276 | PyObject *o; |
| 1277 | PyObject *converted[8]; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1278 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1279 | int result; |
| 1280 | PyObject *return_value = NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1281 | |
Larry Hastings | 22701e8 | 2012-08-08 14:52:22 -0700 | [diff] [blame] | 1282 | double buffers[8][4]; /* double ensures alignment where necessary */ |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1283 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1284 | if (!PyArg_ParseTuple(args, "OOyO:parse_tuple_and_keywords", |
| 1285 | &sub_args, &sub_kwargs, |
| 1286 | &sub_format, &sub_keywords)) |
| 1287 | return NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1288 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1289 | if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
| 1290 | PyErr_SetString(PyExc_ValueError, |
| 1291 | "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
| 1295 | memset(buffers, 0, sizeof(buffers)); |
| 1296 | memset(converted, 0, sizeof(converted)); |
| 1297 | memset(keywords, 0, sizeof(keywords)); |
| 1298 | |
| 1299 | size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 1300 | if (size > 8) { |
| 1301 | PyErr_SetString(PyExc_ValueError, |
| 1302 | "parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 1303 | goto exit; |
| 1304 | } |
| 1305 | |
| 1306 | for (i = 0; i < size; i++) { |
| 1307 | o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 1308 | if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { |
| 1309 | PyErr_Format(PyExc_ValueError, |
Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 1310 | "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] | 1311 | goto exit; |
| 1312 | } |
| 1313 | keywords[i] = PyBytes_AS_STRING(converted[i]); |
| 1314 | } |
| 1315 | |
| 1316 | result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 1317 | sub_format, keywords, |
| 1318 | buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 1319 | buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 1320 | |
| 1321 | if (result) { |
| 1322 | return_value = Py_None; |
| 1323 | Py_INCREF(Py_None); |
| 1324 | } |
| 1325 | |
| 1326 | exit: |
| 1327 | size = sizeof(converted) / sizeof(converted[0]); |
| 1328 | for (i = 0; i < size; i++) { |
| 1329 | Py_XDECREF(converted[i]); |
| 1330 | } |
| 1331 | return return_value; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1334 | static volatile int x; |
| 1335 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1336 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1337 | of an error. |
| 1338 | */ |
| 1339 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1340 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | PyObject *tuple, *obj; |
| 1343 | Py_UNICODE *value; |
| 1344 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1347 | /* Just use the macro and check that it compiles */ |
| 1348 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | tuple = PyTuple_New(1); |
| 1351 | if (tuple == NULL) |
| 1352 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1355 | "ascii", NULL); |
| 1356 | if (obj == NULL) |
| 1357 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | value = 0; |
| 1362 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 1363 | return NULL; |
| 1364 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1365 | return raiseTestError("test_u_code", |
| 1366 | "u code returned wrong value for u'test'"); |
| 1367 | value = 0; |
| 1368 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 1369 | return NULL; |
| 1370 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1371 | len != PyUnicode_GET_SIZE(obj)) |
| 1372 | return raiseTestError("test_u_code", |
| 1373 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | Py_DECREF(tuple); |
| 1376 | Py_INCREF(Py_None); |
| 1377 | return Py_None; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1380 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1381 | static PyObject * |
| 1382 | test_Z_code(PyObject *self) |
| 1383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | PyObject *tuple, *obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1385 | const Py_UNICODE *value1, *value2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | tuple = PyTuple_New(2); |
| 1389 | if (tuple == NULL) |
| 1390 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1392 | obj = PyUnicode_FromString("test"); |
| 1393 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1394 | Py_INCREF(Py_None); |
| 1395 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | /* swap values on purpose */ |
| 1398 | value1 = NULL; |
| 1399 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | /* Test Z for both values */ |
| 1402 | if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) |
| 1403 | return NULL; |
| 1404 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1405 | return raiseTestError("test_Z_code", |
| 1406 | "Z code returned wrong value for 'test'"); |
| 1407 | if (value2 != NULL) |
| 1408 | return raiseTestError("test_Z_code", |
| 1409 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | value1 = NULL; |
| 1412 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1413 | len1 = -1; |
| 1414 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | /* Test Z# for both values */ |
| 1417 | if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1418 | &value2, &len2) < 0) |
| 1419 | return NULL; |
| 1420 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1421 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1422 | return raiseTestError("test_Z_code", |
| 1423 | "Z# code returned wrong values for 'test'"); |
| 1424 | if (value2 != NULL || |
| 1425 | len2 != 0) |
| 1426 | return raiseTestError("test_Z_code", |
| 1427 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | Py_DECREF(tuple); |
| 1430 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1433 | static PyObject * |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1434 | test_widechar(PyObject *self) |
| 1435 | { |
| 1436 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1438 | size_t wtextlen = 1; |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1439 | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1440 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1442 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1443 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1447 | if (wide == NULL) |
| 1448 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1451 | if (utf8 == NULL) { |
| 1452 | Py_DECREF(wide); |
| 1453 | return NULL; |
| 1454 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1455 | |
Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1456 | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 | Py_DECREF(wide); |
| 1458 | Py_DECREF(utf8); |
| 1459 | return raiseTestError("test_widechar", |
| 1460 | "wide string and utf8 string " |
| 1461 | "have different length"); |
| 1462 | } |
| 1463 | if (PyUnicode_Compare(wide, utf8)) { |
| 1464 | Py_DECREF(wide); |
| 1465 | Py_DECREF(utf8); |
| 1466 | if (PyErr_Occurred()) |
| 1467 | return NULL; |
| 1468 | return raiseTestError("test_widechar", |
| 1469 | "wide string and utf8 string " |
| 1470 | "are different"); |
| 1471 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | Py_DECREF(wide); |
| 1474 | Py_DECREF(utf8); |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1475 | |
| 1476 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1477 | wide = PyUnicode_FromWideChar(invalid, 1); |
| 1478 | if (wide == NULL) |
| 1479 | PyErr_Clear(); |
| 1480 | else |
| 1481 | return raiseTestError("test_widechar", |
| 1482 | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
| 1483 | |
| 1484 | wide = PyUnicode_FromUnicode(invalid, 1); |
| 1485 | if (wide == NULL) |
| 1486 | PyErr_Clear(); |
| 1487 | else |
| 1488 | return raiseTestError("test_widechar", |
| 1489 | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1490 | |
| 1491 | wide = PyUnicode_FromUnicode(NULL, 1); |
| 1492 | if (wide == NULL) |
| 1493 | return NULL; |
| 1494 | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1495 | if (_PyUnicode_Ready(wide) < 0) { |
| 1496 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1497 | PyErr_Clear(); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1498 | } |
| 1499 | else { |
| 1500 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1501 | return raiseTestError("test_widechar", |
| 1502 | "PyUnicode_Ready() didn't fail"); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1503 | } |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1504 | #endif |
| 1505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1510 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1511 | { |
| 1512 | PyObject *unicode, *result; |
| 1513 | Py_ssize_t buflen, size; |
| 1514 | wchar_t *buffer; |
| 1515 | |
| 1516 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1517 | return NULL; |
| 1518 | buffer = PyMem_Malloc(buflen * sizeof(wchar_t)); |
| 1519 | if (buffer == NULL) |
| 1520 | return PyErr_NoMemory(); |
| 1521 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1522 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1523 | if (size == -1) { |
| 1524 | PyMem_Free(buffer); |
| 1525 | return NULL; |
| 1526 | } |
| 1527 | |
| 1528 | if (size < buflen) |
| 1529 | buflen = size + 1; |
| 1530 | else |
| 1531 | buflen = size; |
| 1532 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 1533 | PyMem_Free(buffer); |
| 1534 | if (result == NULL) |
| 1535 | return NULL; |
| 1536 | |
| 1537 | return Py_BuildValue("(Nn)", result, size); |
| 1538 | } |
| 1539 | |
| 1540 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1541 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1542 | { |
| 1543 | PyObject *unicode, *result; |
| 1544 | Py_ssize_t size; |
| 1545 | wchar_t *buffer; |
| 1546 | |
| 1547 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 1548 | return NULL; |
| 1549 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1550 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1551 | if (buffer == NULL) |
| 1552 | return NULL; |
| 1553 | |
| 1554 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 1555 | PyMem_Free(buffer); |
| 1556 | if (result == NULL) |
| 1557 | return NULL; |
| 1558 | return Py_BuildValue("(Nn)", result, size); |
| 1559 | } |
| 1560 | |
| 1561 | static PyObject * |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 1562 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 1563 | { |
| 1564 | Py_UNICODE *unicode; |
| 1565 | Py_ssize_t length; |
| 1566 | char *errors = NULL; |
| 1567 | PyObject *decimal; |
| 1568 | Py_ssize_t decimal_length, new_length; |
| 1569 | int res; |
| 1570 | |
| 1571 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 1572 | return NULL; |
| 1573 | |
| 1574 | decimal_length = length * 7; /* len('€') */ |
| 1575 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 1576 | if (decimal == NULL) |
| 1577 | return NULL; |
| 1578 | |
| 1579 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 1580 | PyBytes_AS_STRING(decimal), |
| 1581 | errors); |
| 1582 | if (res < 0) { |
| 1583 | Py_DECREF(decimal); |
| 1584 | return NULL; |
| 1585 | } |
| 1586 | |
| 1587 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 1588 | assert(new_length <= decimal_length); |
| 1589 | res = _PyBytes_Resize(&decimal, new_length); |
| 1590 | if (res < 0) |
| 1591 | return NULL; |
| 1592 | |
| 1593 | return decimal; |
| 1594 | } |
| 1595 | |
| 1596 | static PyObject * |
| 1597 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 1598 | { |
| 1599 | Py_UNICODE *unicode; |
| 1600 | Py_ssize_t length; |
| 1601 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 1602 | return NULL; |
| 1603 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 1604 | } |
| 1605 | |
| 1606 | static PyObject * |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 1607 | unicode_legacy_string(PyObject *self, PyObject *args) |
| 1608 | { |
| 1609 | Py_UNICODE *data; |
| 1610 | Py_ssize_t len; |
| 1611 | PyObject *u; |
| 1612 | |
| 1613 | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
| 1614 | return NULL; |
| 1615 | |
| 1616 | u = PyUnicode_FromUnicode(NULL, len); |
| 1617 | if (u == NULL) |
| 1618 | return NULL; |
| 1619 | |
| 1620 | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
| 1621 | |
| 1622 | if (len > 0) { /* The empty string is always ready. */ |
| 1623 | assert(!PyUnicode_IS_READY(u)); |
| 1624 | } |
| 1625 | |
| 1626 | return u; |
| 1627 | } |
| 1628 | |
| 1629 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1630 | getargs_w_star(PyObject *self, PyObject *args) |
| 1631 | { |
| 1632 | Py_buffer buffer; |
| 1633 | PyObject *result; |
| 1634 | char *str; |
| 1635 | |
| 1636 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 1637 | return NULL; |
| 1638 | |
| 1639 | if (2 <= buffer.len) { |
| 1640 | str = buffer.buf; |
| 1641 | str[0] = '['; |
| 1642 | str[buffer.len-1] = ']'; |
| 1643 | } |
| 1644 | |
| 1645 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1646 | PyBuffer_Release(&buffer); |
| 1647 | return result; |
| 1648 | } |
| 1649 | |
| 1650 | |
| 1651 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1652 | test_empty_argparse(PyObject *self) |
| 1653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 1655 | PyObject *tuple, *dict = NULL; |
| 1656 | static char *kwlist[] = {NULL}; |
| 1657 | int result; |
| 1658 | tuple = PyTuple_New(0); |
| 1659 | if (!tuple) |
| 1660 | return NULL; |
| 1661 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 1662 | goto done; |
| 1663 | dict = PyDict_New(); |
| 1664 | if (!dict) |
| 1665 | goto done; |
| 1666 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1667 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | Py_DECREF(tuple); |
| 1669 | Py_XDECREF(dict); |
| 1670 | if (result < 0) |
| 1671 | return NULL; |
| 1672 | else { |
| 1673 | Py_RETURN_NONE; |
| 1674 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1678 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1680 | const char *encoding, *errors = NULL; |
| 1681 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 1682 | &encoding, &errors)) |
| 1683 | return NULL; |
| 1684 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1687 | static PyObject * |
| 1688 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | const char *encoding, *errors = NULL; |
| 1691 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 1692 | &encoding, &errors)) |
| 1693 | return NULL; |
| 1694 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1697 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1698 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1699 | static PyObject * |
| 1700 | test_long_numbits(PyObject *self) |
| 1701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | struct triple { |
| 1703 | long input; |
| 1704 | size_t nbits; |
| 1705 | int sign; |
| 1706 | } testcases[] = {{0, 0, 0}, |
| 1707 | {1L, 1, 1}, |
| 1708 | {-1L, 1, -1}, |
| 1709 | {2L, 2, 1}, |
| 1710 | {-2L, 2, -1}, |
| 1711 | {3L, 2, 1}, |
| 1712 | {-3L, 2, -1}, |
| 1713 | {4L, 3, 1}, |
| 1714 | {-4L, 3, -1}, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1715 | {0x7fffL, 15, 1}, /* one Python int digit */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | {-0x7fffL, 15, -1}, |
| 1717 | {0xffffL, 16, 1}, |
| 1718 | {-0xffffL, 16, -1}, |
| 1719 | {0xfffffffL, 28, 1}, |
| 1720 | {-0xfffffffL, 28, -1}}; |
| 1721 | int i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1722 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 1723 | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 1724 | size_t nbits; |
| 1725 | int sign; |
| 1726 | PyObject *plong; |
| 1727 | |
| 1728 | plong = PyLong_FromLong(testcases[i].input); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 1729 | if (plong == NULL) |
| 1730 | return NULL; |
Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 1731 | nbits = _PyLong_NumBits(plong); |
| 1732 | sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | Py_DECREF(plong); |
| 1735 | if (nbits != testcases[i].nbits) |
| 1736 | return raiseTestError("test_long_numbits", |
| 1737 | "wrong result for _PyLong_NumBits"); |
| 1738 | if (sign != testcases[i].sign) |
| 1739 | return raiseTestError("test_long_numbits", |
| 1740 | "wrong result for _PyLong_Sign"); |
| 1741 | } |
| 1742 | Py_INCREF(Py_None); |
| 1743 | return Py_None; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1746 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1747 | |
| 1748 | static PyObject * |
| 1749 | test_null_strings(PyObject *self) |
| 1750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 1752 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 1753 | Py_XDECREF(o1); |
| 1754 | Py_XDECREF(o2); |
| 1755 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1758 | static PyObject * |
| 1759 | raise_exception(PyObject *self, PyObject *args) |
| 1760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | PyObject *exc; |
| 1762 | PyObject *exc_args, *v; |
| 1763 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1766 | &exc, &num_args)) |
| 1767 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1769 | exc_args = PyTuple_New(num_args); |
| 1770 | if (exc_args == NULL) |
| 1771 | return NULL; |
| 1772 | for (i = 0; i < num_args; ++i) { |
| 1773 | v = PyLong_FromLong(i); |
| 1774 | if (v == NULL) { |
| 1775 | Py_DECREF(exc_args); |
| 1776 | return NULL; |
| 1777 | } |
| 1778 | PyTuple_SET_ITEM(exc_args, i, v); |
| 1779 | } |
| 1780 | PyErr_SetObject(exc, exc_args); |
| 1781 | Py_DECREF(exc_args); |
| 1782 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1783 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1784 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 1785 | static PyObject * |
| 1786 | test_set_exc_info(PyObject *self, PyObject *args) |
| 1787 | { |
| 1788 | PyObject *orig_exc; |
| 1789 | PyObject *new_type, *new_value, *new_tb; |
| 1790 | PyObject *type, *value, *tb; |
| 1791 | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
| 1792 | &new_type, &new_value, &new_tb)) |
| 1793 | return NULL; |
| 1794 | |
| 1795 | PyErr_GetExcInfo(&type, &value, &tb); |
| 1796 | |
| 1797 | Py_INCREF(new_type); |
| 1798 | Py_INCREF(new_value); |
| 1799 | Py_INCREF(new_tb); |
| 1800 | PyErr_SetExcInfo(new_type, new_value, new_tb); |
| 1801 | |
| 1802 | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
| 1803 | Py_XDECREF(type); |
| 1804 | Py_XDECREF(value); |
| 1805 | Py_XDECREF(tb); |
| 1806 | return orig_exc; |
| 1807 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 1808 | |
| 1809 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1810 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1811 | static PyObject * |
| 1812 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | if (PyDateTimeAPI) { |
| 1814 | if (test_run_counter) { |
| 1815 | /* Probably regrtest.py -R */ |
| 1816 | Py_RETURN_NONE; |
| 1817 | } |
| 1818 | else { |
| 1819 | PyErr_SetString(PyExc_AssertionError, |
| 1820 | "PyDateTime_CAPI somehow initialized"); |
| 1821 | return NULL; |
| 1822 | } |
| 1823 | } |
| 1824 | test_run_counter++; |
| 1825 | PyDateTime_IMPORT; |
| 1826 | if (PyDateTimeAPI) |
| 1827 | Py_RETURN_NONE; |
| 1828 | else |
| 1829 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 1832 | |
| 1833 | #ifdef WITH_THREAD |
| 1834 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1835 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 1836 | * `thread_done` when it's finished. The driver code has to know when the |
| 1837 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 1838 | * may go away when the driver finishes. The former lack of this explicit |
| 1839 | * synchronization caused rare segfaults, so rare that they were seen only |
| 1840 | * on a Mac buildbot (although they were possible on any box). |
| 1841 | */ |
| 1842 | static PyThread_type_lock thread_done = NULL; |
| 1843 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1844 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1845 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1846 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | PyObject *rc; |
| 1848 | int success; |
| 1849 | PyGILState_STATE s = PyGILState_Ensure(); |
| 1850 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
| 1851 | success = (rc != NULL); |
| 1852 | Py_XDECREF(rc); |
| 1853 | PyGILState_Release(s); |
| 1854 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1857 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 1858 | * should be called only from threads spawned by test_thread_state(). |
| 1859 | */ |
| 1860 | static void |
| 1861 | _make_call_from_thread(void *callable) |
| 1862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | _make_call(callable); |
| 1864 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1867 | static PyObject * |
| 1868 | test_thread_state(PyObject *self, PyObject *args) |
| 1869 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | PyObject *fn; |
| 1871 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 1874 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | if (!PyCallable_Check(fn)) { |
| 1877 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1878 | fn->ob_type->tp_name); |
| 1879 | return NULL; |
| 1880 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | /* Ensure Python is set up for threading */ |
| 1883 | PyEval_InitThreads(); |
| 1884 | thread_done = PyThread_allocate_lock(); |
| 1885 | if (thread_done == NULL) |
| 1886 | return PyErr_NoMemory(); |
| 1887 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | /* Start a new thread with our callback. */ |
| 1890 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1891 | /* Make the callback with the thread lock held by this thread */ |
| 1892 | success &= _make_call(fn); |
| 1893 | /* Do it all again, but this time with the thread-lock released */ |
| 1894 | Py_BEGIN_ALLOW_THREADS |
| 1895 | success &= _make_call(fn); |
| 1896 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1897 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | /* And once more with and without a thread |
| 1900 | XXX - should use a lock and work out exactly what we are trying |
| 1901 | to test <wink> |
| 1902 | */ |
| 1903 | Py_BEGIN_ALLOW_THREADS |
| 1904 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1905 | success &= _make_call(fn); |
| 1906 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1907 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 1910 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | PyThread_free_lock(thread_done); |
| 1913 | if (!success) |
| 1914 | return NULL; |
| 1915 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1916 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1917 | |
| 1918 | /* test Py_AddPendingCalls using threads */ |
| 1919 | static int _pending_callback(void *arg) |
| 1920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1921 | /* we assume the argument is callable object to which we own a reference */ |
| 1922 | PyObject *callable = (PyObject *)arg; |
| 1923 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 1924 | Py_DECREF(callable); |
| 1925 | Py_XDECREF(r); |
| 1926 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | /* The following requests n callbacks to _pending_callback. It can be |
| 1930 | * run from any python thread. |
| 1931 | */ |
| 1932 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | PyObject *callable; |
| 1935 | int r; |
| 1936 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1937 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1939 | /* create the reference for the callbackwhile we hold the lock */ |
| 1940 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1942 | Py_BEGIN_ALLOW_THREADS |
| 1943 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1944 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1946 | if (r<0) { |
| 1947 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1948 | Py_INCREF(Py_False); |
| 1949 | return Py_False; |
| 1950 | } |
| 1951 | Py_INCREF(Py_True); |
| 1952 | return Py_True; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1953 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1954 | #endif |
| 1955 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 1956 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1957 | static PyObject * |
| 1958 | test_string_from_format(PyObject *self, PyObject *args) |
| 1959 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | PyObject *result; |
| 1961 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1962 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1963 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 1964 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 1965 | if (result == NULL) \ |
| 1966 | return NULL; \ |
Victor Stinner | 2b979bf | 2011-11-20 19:32:09 +0100 | [diff] [blame] | 1967 | if (PyUnicode_CompareWithASCIIString(result, "1")) { \ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1968 | msg = FORMAT " failed at 1"; \ |
| 1969 | goto Fail; \ |
| 1970 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1973 | CHECK_1_FORMAT("%d", int); |
| 1974 | CHECK_1_FORMAT("%ld", long); |
| 1975 | /* The z width modifier was added in Python 2.5. */ |
| 1976 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | /* The u type code was added in Python 2.5. */ |
| 1979 | CHECK_1_FORMAT("%u", unsigned int); |
| 1980 | CHECK_1_FORMAT("%lu", unsigned long); |
| 1981 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1984 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1985 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 1986 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1987 | #endif |
| 1988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1990 | |
| 1991 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | Py_XDECREF(result); |
| 1993 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1994 | |
| 1995 | #undef CHECK_1_FORMAT |
| 1996 | } |
| 1997 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 1998 | |
| 1999 | static PyObject * |
| 2000 | test_unicode_compare_with_ascii(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 2002 | int result; |
| 2003 | if (py_s == NULL) |
| 2004 | return NULL; |
| 2005 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 2006 | Py_DECREF(py_s); |
| 2007 | if (!result) { |
| 2008 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 2009 | "should not compare equal to c string."); |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2013 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2014 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2015 | /* This is here to provide a docstring for test_descr. */ |
| 2016 | static PyObject * |
| 2017 | test_with_docstring(PyObject *self) |
| 2018 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2022 | /* Test PyOS_string_to_double. */ |
| 2023 | static PyObject * |
| 2024 | test_string_to_double(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | double result; |
| 2026 | char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | #define CHECK_STRING(STR, expected) \ |
| 2029 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2030 | if (result == -1.0 && PyErr_Occurred()) \ |
| 2031 | return NULL; \ |
| 2032 | if (result != expected) { \ |
| 2033 | msg = "conversion of " STR " to float failed"; \ |
| 2034 | goto fail; \ |
| 2035 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | #define CHECK_INVALID(STR) \ |
| 2038 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 2039 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 2040 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 2041 | PyErr_Clear(); \ |
| 2042 | else \ |
| 2043 | return NULL; \ |
| 2044 | } \ |
| 2045 | else { \ |
| 2046 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 2047 | goto fail; \ |
| 2048 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | CHECK_STRING("0.1", 0.1); |
| 2051 | CHECK_STRING("1.234", 1.234); |
| 2052 | CHECK_STRING("-1.35", -1.35); |
| 2053 | CHECK_STRING(".1e01", 1.0); |
| 2054 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | CHECK_INVALID(" 0.1"); |
| 2057 | CHECK_INVALID("\t\n-3"); |
| 2058 | CHECK_INVALID(".123 "); |
| 2059 | CHECK_INVALID("3\n"); |
| 2060 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2063 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2065 | #undef CHECK_STRING |
| 2066 | #undef CHECK_INVALID |
| 2067 | } |
| 2068 | |
| 2069 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2070 | /* Coverage testing of capsule objects. */ |
| 2071 | |
| 2072 | static const char *capsule_name = "capsule name"; |
| 2073 | static char *capsule_pointer = "capsule pointer"; |
| 2074 | static char *capsule_context = "capsule context"; |
| 2075 | static const char *capsule_error = NULL; |
| 2076 | static int |
| 2077 | capsule_destructor_call_count = 0; |
| 2078 | |
| 2079 | static void |
| 2080 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2081 | capsule_destructor_call_count++; |
| 2082 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 2083 | capsule_error = "context did not match in destructor!"; |
| 2084 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2085 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2086 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 2087 | capsule_error = "name did not match in destructor!"; |
| 2088 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2089 | capsule_error = "pointer did not match in destructor!"; |
| 2090 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2094 | char *name; |
| 2095 | char *module; |
| 2096 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2097 | } known_capsule; |
| 2098 | |
| 2099 | static PyObject * |
| 2100 | test_capsule(PyObject *self, PyObject *args) |
| 2101 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | PyObject *object; |
| 2103 | const char *error = NULL; |
| 2104 | void *pointer; |
| 2105 | void *pointer2; |
| 2106 | known_capsule known_capsules[] = { |
| 2107 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2108 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 2109 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 2110 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2111 | { NULL, NULL }, |
| 2112 | }; |
| 2113 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2114 | |
| 2115 | #define FAIL(x) { error = (x); goto exit; } |
| 2116 | |
| 2117 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | if (capsule_error) { \ |
| 2119 | FAIL(capsule_error); \ |
| 2120 | } \ |
| 2121 | else if (!capsule_destructor_call_count) { \ |
| 2122 | FAIL("destructor not called!"); \ |
| 2123 | } \ |
| 2124 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2127 | PyCapsule_SetContext(object, capsule_context); |
| 2128 | capsule_destructor(object); |
| 2129 | CHECK_DESTRUCTOR; |
| 2130 | Py_DECREF(object); |
| 2131 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | object = PyCapsule_New(known, "ignored", NULL); |
| 2134 | PyCapsule_SetPointer(object, capsule_pointer); |
| 2135 | PyCapsule_SetName(object, capsule_name); |
| 2136 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 2137 | PyCapsule_SetContext(object, capsule_context); |
| 2138 | capsule_destructor(object); |
| 2139 | CHECK_DESTRUCTOR; |
| 2140 | /* intentionally access using the wrong name */ |
| 2141 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2142 | if (!PyErr_Occurred()) { |
| 2143 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2144 | } |
| 2145 | PyErr_Clear(); |
| 2146 | if (pointer2) { |
| 2147 | if (pointer2 == capsule_pointer) { |
| 2148 | FAIL("PyCapsule_GetPointer should not have" |
| 2149 | " returned the internal pointer!"); |
| 2150 | } else { |
| 2151 | FAIL("PyCapsule_GetPointer should have " |
| 2152 | "returned NULL pointer but did not!"); |
| 2153 | } |
| 2154 | } |
| 2155 | PyCapsule_SetDestructor(object, NULL); |
| 2156 | Py_DECREF(object); |
| 2157 | if (capsule_destructor_call_count) { |
| 2158 | FAIL("destructor called when it should not have been!"); |
| 2159 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 2162 | /* yeah, ordinarily I wouldn't do this either, |
| 2163 | but it's fine for this test harness. |
| 2164 | */ |
| 2165 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2166 | #undef FAIL |
| 2167 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | { \ |
| 2169 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 2170 | x, known->module, known->attribute); \ |
| 2171 | error = buffer; \ |
| 2172 | goto exit; \ |
| 2173 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | PyObject *module = PyImport_ImportModule(known->module); |
| 2176 | if (module) { |
| 2177 | pointer = PyCapsule_Import(known->name, 0); |
| 2178 | if (!pointer) { |
| 2179 | Py_DECREF(module); |
| 2180 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 2181 | } |
| 2182 | object = PyObject_GetAttrString(module, known->attribute); |
| 2183 | if (!object) { |
| 2184 | Py_DECREF(module); |
| 2185 | return NULL; |
| 2186 | } |
| 2187 | pointer2 = PyCapsule_GetPointer(object, |
| 2188 | "weebles wobble but they don't fall down"); |
| 2189 | if (!PyErr_Occurred()) { |
| 2190 | Py_DECREF(object); |
| 2191 | Py_DECREF(module); |
| 2192 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2193 | } |
| 2194 | PyErr_Clear(); |
| 2195 | if (pointer2) { |
| 2196 | Py_DECREF(module); |
| 2197 | Py_DECREF(object); |
| 2198 | if (pointer2 == pointer) { |
| 2199 | FAIL("PyCapsule_GetPointer should not have" |
| 2200 | " returned its internal pointer!"); |
| 2201 | } else { |
| 2202 | FAIL("PyCapsule_GetPointer should have" |
| 2203 | " returned NULL pointer but did not!"); |
| 2204 | } |
| 2205 | } |
| 2206 | Py_DECREF(object); |
| 2207 | Py_DECREF(module); |
| 2208 | } |
| 2209 | else |
| 2210 | PyErr_Clear(); |
| 2211 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2212 | |
| 2213 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | if (error) { |
| 2215 | return raiseTestError("test_capsule", error); |
| 2216 | } |
| 2217 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2218 | #undef FAIL |
| 2219 | } |
| 2220 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2221 | #ifdef HAVE_GETTIMEOFDAY |
| 2222 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 2223 | 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] | 2224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | e->tv_sec -= s->tv_sec; |
| 2226 | e->tv_usec -= s->tv_usec; |
| 2227 | if (e->tv_usec < 0) { |
| 2228 | e->tv_sec -=1; |
| 2229 | e->tv_usec += 1000000; |
| 2230 | } |
| 2231 | 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] | 2232 | } |
| 2233 | |
| 2234 | static PyObject * |
| 2235 | profile_int(PyObject *self, PyObject* args) |
| 2236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | int i, k; |
| 2238 | struct timeval start, stop; |
| 2239 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2241 | /* Test 1: Allocate and immediately deallocate |
| 2242 | many small integers */ |
| 2243 | gettimeofday(&start, NULL); |
| 2244 | for(k=0; k < 20000; k++) |
| 2245 | for(i=0; i < 1000; i++) { |
| 2246 | single = PyLong_FromLong(i); |
| 2247 | Py_DECREF(single); |
| 2248 | } |
| 2249 | gettimeofday(&stop, NULL); |
| 2250 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | /* Test 2: Allocate and immediately deallocate |
| 2253 | many large integers */ |
| 2254 | gettimeofday(&start, NULL); |
| 2255 | for(k=0; k < 20000; k++) |
| 2256 | for(i=0; i < 1000; i++) { |
| 2257 | single = PyLong_FromLong(i+1000000); |
| 2258 | Py_DECREF(single); |
| 2259 | } |
| 2260 | gettimeofday(&stop, NULL); |
| 2261 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | /* Test 3: Allocate a few integers, then release |
| 2264 | them all simultaneously. */ |
| 2265 | multiple = malloc(sizeof(PyObject*) * 1000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2266 | if (multiple == NULL) |
| 2267 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2268 | gettimeofday(&start, NULL); |
| 2269 | for(k=0; k < 20000; k++) { |
| 2270 | for(i=0; i < 1000; i++) { |
| 2271 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2272 | } |
| 2273 | for(i=0; i < 1000; i++) { |
| 2274 | Py_DECREF(multiple[i]); |
| 2275 | } |
| 2276 | } |
| 2277 | gettimeofday(&stop, NULL); |
| 2278 | print_delta(3, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2279 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | /* Test 4: Allocate many integers, then release |
| 2282 | them all simultaneously. */ |
| 2283 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2284 | if (multiple == NULL) |
| 2285 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | gettimeofday(&start, NULL); |
| 2287 | for(k=0; k < 20; k++) { |
| 2288 | for(i=0; i < 1000000; i++) { |
| 2289 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2290 | } |
| 2291 | for(i=0; i < 1000000; i++) { |
| 2292 | Py_DECREF(multiple[i]); |
| 2293 | } |
| 2294 | } |
| 2295 | gettimeofday(&stop, NULL); |
| 2296 | print_delta(4, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2297 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | /* Test 5: Allocate many integers < 32000 */ |
| 2300 | multiple = malloc(sizeof(PyObject*) * 1000000); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2301 | if (multiple == NULL) |
| 2302 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2303 | gettimeofday(&start, NULL); |
| 2304 | for(k=0; k < 10; k++) { |
| 2305 | for(i=0; i < 1000000; i++) { |
| 2306 | multiple[i] = PyLong_FromLong(i+1000); |
| 2307 | } |
| 2308 | for(i=0; i < 1000000; i++) { |
| 2309 | Py_DECREF(multiple[i]); |
| 2310 | } |
| 2311 | } |
| 2312 | gettimeofday(&stop, NULL); |
| 2313 | print_delta(5, &start, &stop); |
Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2314 | free(multiple); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2316 | /* Test 6: Perform small int addition */ |
| 2317 | op1 = PyLong_FromLong(1); |
| 2318 | gettimeofday(&start, NULL); |
| 2319 | for(i=0; i < 10000000; i++) { |
| 2320 | result = PyNumber_Add(op1, op1); |
| 2321 | Py_DECREF(result); |
| 2322 | } |
| 2323 | gettimeofday(&stop, NULL); |
| 2324 | Py_DECREF(op1); |
| 2325 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2327 | /* Test 7: Perform medium int addition */ |
| 2328 | op1 = PyLong_FromLong(1000); |
Christian Heimes | 66eda26 | 2013-07-26 15:54:07 +0200 | [diff] [blame] | 2329 | if (op1 == NULL) |
| 2330 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | gettimeofday(&start, NULL); |
| 2332 | for(i=0; i < 10000000; i++) { |
| 2333 | result = PyNumber_Add(op1, op1); |
Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2334 | Py_XDECREF(result); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | } |
| 2336 | gettimeofday(&stop, NULL); |
| 2337 | Py_DECREF(op1); |
| 2338 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | Py_INCREF(Py_None); |
| 2341 | return Py_None; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2342 | } |
| 2343 | #endif |
| 2344 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2345 | /* To test the format of tracebacks as printed out. */ |
| 2346 | static PyObject * |
| 2347 | traceback_print(PyObject *self, PyObject *args) |
| 2348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | PyObject *file; |
| 2350 | PyObject *traceback; |
| 2351 | int result; |
| 2352 | |
| 2353 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 2354 | &traceback, &file)) |
| 2355 | return NULL; |
| 2356 | |
| 2357 | result = PyTraceBack_Print(traceback, file); |
| 2358 | if (result < 0) |
| 2359 | return NULL; |
| 2360 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2363 | /* To test the format of exceptions as printed out. */ |
| 2364 | static PyObject * |
| 2365 | exception_print(PyObject *self, PyObject *args) |
| 2366 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2367 | PyObject *value; |
| 2368 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 2371 | &value)) |
| 2372 | return NULL; |
| 2373 | if (!PyExceptionInstance_Check(value)) { |
| 2374 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 2375 | return NULL; |
| 2376 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | tb = PyException_GetTraceback(value); |
| 2379 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 2380 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | |
| 2386 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2387 | |
| 2388 | /* reliably raise a MemoryError */ |
| 2389 | static PyObject * |
| 2390 | raise_memoryerror(PyObject *self) |
| 2391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | PyErr_NoMemory(); |
| 2393 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2396 | /* Issue 6012 */ |
| 2397 | static PyObject *str1, *str2; |
| 2398 | static int |
| 2399 | failing_converter(PyObject *obj, void *arg) |
| 2400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | /* Clone str1, then let the conversion fail. */ |
| 2402 | assert(str1); |
| 2403 | str2 = str1; |
| 2404 | Py_INCREF(str2); |
| 2405 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2406 | } |
| 2407 | static PyObject* |
| 2408 | argparsing(PyObject *o, PyObject *args) |
| 2409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | PyObject *res; |
| 2411 | str1 = str2 = NULL; |
| 2412 | if (!PyArg_ParseTuple(args, "O&O&", |
| 2413 | PyUnicode_FSConverter, &str1, |
| 2414 | failing_converter, &str2)) { |
| 2415 | if (!str2) |
| 2416 | /* argument converter not called? */ |
| 2417 | return NULL; |
| 2418 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2419 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | Py_DECREF(str2); |
| 2421 | PyErr_Clear(); |
| 2422 | return res; |
| 2423 | } |
| 2424 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2425 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2426 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2427 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 2428 | static PyObject * |
| 2429 | code_newempty(PyObject *self, PyObject *args) |
| 2430 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | const char *filename; |
| 2432 | const char *funcname; |
| 2433 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 2436 | &filename, &funcname, &firstlineno)) |
| 2437 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2442 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 2443 | Run via Lib/test/test_exceptions.py */ |
| 2444 | static PyObject * |
| 2445 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2447 | const char *name; |
| 2448 | const char *doc = NULL; |
| 2449 | PyObject *base = NULL; |
| 2450 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2454 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2455 | "s|sOO:make_exception_with_doc", kwlist, |
| 2456 | &name, &doc, &base, &dict)) |
| 2457 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2462 | static PyObject * |
| 2463 | make_memoryview_from_NULL_pointer(PyObject *self) |
| 2464 | { |
| 2465 | Py_buffer info; |
| 2466 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 2467 | return NULL; |
| 2468 | return PyMemoryView_FromBuffer(&info); |
| 2469 | } |
| 2470 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2471 | /* Test that the fatal error from not having a current thread doesn't |
| 2472 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 2473 | static PyObject * |
| 2474 | crash_no_current_thread(PyObject *self) |
| 2475 | { |
| 2476 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 2477 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 2478 | !pydebug mode. However, the test only actually tests anything |
| 2479 | in pydebug mode, since that's where the infinite loop was in |
| 2480 | the first place. */ |
| 2481 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2482 | Py_END_ALLOW_THREADS |
| 2483 | return NULL; |
| 2484 | } |
| 2485 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2486 | /* To run some code in a sub-interpreter. */ |
| 2487 | static PyObject * |
| 2488 | run_in_subinterp(PyObject *self, PyObject *args) |
| 2489 | { |
| 2490 | const char *code; |
| 2491 | int r; |
| 2492 | PyThreadState *substate, *mainstate; |
| 2493 | |
| 2494 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 2495 | &code)) |
| 2496 | return NULL; |
| 2497 | |
| 2498 | mainstate = PyThreadState_Get(); |
| 2499 | |
| 2500 | PyThreadState_Swap(NULL); |
| 2501 | |
| 2502 | substate = Py_NewInterpreter(); |
Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 2503 | if (substate == NULL) { |
| 2504 | /* Since no new thread state was created, there is no exception to |
| 2505 | propagate; raise a fresh one after swapping in the old thread |
| 2506 | state. */ |
| 2507 | PyThreadState_Swap(mainstate); |
| 2508 | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
| 2509 | return NULL; |
| 2510 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2511 | r = PyRun_SimpleString(code); |
| 2512 | Py_EndInterpreter(substate); |
| 2513 | |
| 2514 | PyThreadState_Swap(mainstate); |
| 2515 | |
| 2516 | return PyLong_FromLong(r); |
| 2517 | } |
| 2518 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2519 | static PyObject * |
| 2520 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
| 2521 | { |
| 2522 | PyObject *obj; |
| 2523 | time_t sec; |
| 2524 | if (!PyArg_ParseTuple(args, "O:pytime_object_to_time_t", &obj)) |
| 2525 | return NULL; |
| 2526 | if (_PyTime_ObjectToTime_t(obj, &sec) == -1) |
| 2527 | return NULL; |
| 2528 | return _PyLong_FromTime_t(sec); |
| 2529 | } |
| 2530 | |
| 2531 | static PyObject * |
| 2532 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
| 2533 | { |
| 2534 | PyObject *obj; |
| 2535 | time_t sec; |
| 2536 | long usec; |
| 2537 | if (!PyArg_ParseTuple(args, "O:pytime_object_to_timeval", &obj)) |
| 2538 | return NULL; |
| 2539 | if (_PyTime_ObjectToTimeval(obj, &sec, &usec) == -1) |
| 2540 | return NULL; |
| 2541 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
| 2542 | } |
| 2543 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2544 | static PyObject * |
| 2545 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
| 2546 | { |
| 2547 | PyObject *obj; |
| 2548 | time_t sec; |
| 2549 | long nsec; |
| 2550 | if (!PyArg_ParseTuple(args, "O:pytime_object_to_timespec", &obj)) |
| 2551 | return NULL; |
| 2552 | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec) == -1) |
| 2553 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2554 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2555 | } |
| 2556 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 2557 | static void |
| 2558 | slot_tp_del(PyObject *self) |
| 2559 | { |
| 2560 | _Py_IDENTIFIER(__tp_del__); |
| 2561 | PyObject *del, *res; |
| 2562 | PyObject *error_type, *error_value, *error_traceback; |
| 2563 | |
| 2564 | /* Temporarily resurrect the object. */ |
| 2565 | assert(self->ob_refcnt == 0); |
| 2566 | self->ob_refcnt = 1; |
| 2567 | |
| 2568 | /* Save the current exception, if any. */ |
| 2569 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 2570 | |
| 2571 | /* Execute __del__ method, if any. */ |
| 2572 | del = _PyObject_LookupSpecial(self, &PyId___tp_del__); |
| 2573 | if (del != NULL) { |
| 2574 | res = PyEval_CallObject(del, NULL); |
| 2575 | if (res == NULL) |
| 2576 | PyErr_WriteUnraisable(del); |
| 2577 | else |
| 2578 | Py_DECREF(res); |
| 2579 | Py_DECREF(del); |
| 2580 | } |
| 2581 | |
| 2582 | /* Restore the saved exception. */ |
| 2583 | PyErr_Restore(error_type, error_value, error_traceback); |
| 2584 | |
| 2585 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 2586 | * cause a recursive call. |
| 2587 | */ |
| 2588 | assert(self->ob_refcnt > 0); |
| 2589 | if (--self->ob_refcnt == 0) |
| 2590 | return; /* this is the normal path out */ |
| 2591 | |
| 2592 | /* __del__ resurrected it! Make it look like the original Py_DECREF |
| 2593 | * never happened. |
| 2594 | */ |
| 2595 | { |
| 2596 | Py_ssize_t refcnt = self->ob_refcnt; |
| 2597 | _Py_NewReference(self); |
| 2598 | self->ob_refcnt = refcnt; |
| 2599 | } |
| 2600 | assert(!PyType_IS_GC(Py_TYPE(self)) || |
| 2601 | _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); |
| 2602 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 2603 | * we need to undo that. */ |
| 2604 | _Py_DEC_REFTOTAL; |
| 2605 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object |
| 2606 | * chain, so no more to do there. |
| 2607 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 2608 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 2609 | * undone. |
| 2610 | */ |
| 2611 | #ifdef COUNT_ALLOCS |
| 2612 | --Py_TYPE(self)->tp_frees; |
| 2613 | --Py_TYPE(self)->tp_allocs; |
| 2614 | #endif |
| 2615 | } |
| 2616 | |
| 2617 | static PyObject * |
| 2618 | with_tp_del(PyObject *self, PyObject *args) |
| 2619 | { |
| 2620 | PyObject *obj; |
| 2621 | PyTypeObject *tp; |
| 2622 | |
| 2623 | if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) |
| 2624 | return NULL; |
| 2625 | tp = (PyTypeObject *) obj; |
| 2626 | if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { |
| 2627 | PyErr_Format(PyExc_TypeError, |
| 2628 | "heap type expected, got %R", obj); |
| 2629 | return NULL; |
| 2630 | } |
| 2631 | tp->tp_del = slot_tp_del; |
| 2632 | Py_INCREF(obj); |
| 2633 | return obj; |
| 2634 | } |
| 2635 | |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 2636 | static PyObject * |
| 2637 | _test_incref(PyObject *ob) |
| 2638 | { |
| 2639 | Py_INCREF(ob); |
| 2640 | return ob; |
| 2641 | } |
| 2642 | |
| 2643 | static PyObject * |
| 2644 | test_xincref_doesnt_leak(PyObject *ob) |
| 2645 | { |
| 2646 | PyObject *obj = PyLong_FromLong(0); |
| 2647 | Py_XINCREF(_test_incref(obj)); |
| 2648 | Py_DECREF(obj); |
| 2649 | Py_DECREF(obj); |
| 2650 | Py_DECREF(obj); |
| 2651 | Py_RETURN_NONE; |
| 2652 | } |
| 2653 | |
| 2654 | static PyObject * |
| 2655 | test_incref_doesnt_leak(PyObject *ob) |
| 2656 | { |
| 2657 | PyObject *obj = PyLong_FromLong(0); |
| 2658 | Py_INCREF(_test_incref(obj)); |
| 2659 | Py_DECREF(obj); |
| 2660 | Py_DECREF(obj); |
| 2661 | Py_DECREF(obj); |
| 2662 | Py_RETURN_NONE; |
| 2663 | } |
| 2664 | |
| 2665 | static PyObject * |
| 2666 | test_xdecref_doesnt_leak(PyObject *ob) |
| 2667 | { |
| 2668 | Py_XDECREF(PyLong_FromLong(0)); |
| 2669 | Py_RETURN_NONE; |
| 2670 | } |
| 2671 | |
| 2672 | static PyObject * |
| 2673 | test_decref_doesnt_leak(PyObject *ob) |
| 2674 | { |
| 2675 | Py_DECREF(PyLong_FromLong(0)); |
| 2676 | Py_RETURN_NONE; |
| 2677 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2678 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 2679 | static PyObject * |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 2680 | test_incref_decref_API(PyObject *ob) |
| 2681 | { |
| 2682 | PyObject *obj = PyLong_FromLong(0); |
| 2683 | Py_IncRef(ob); |
| 2684 | Py_DecRef(obj); |
| 2685 | Py_DecRef(obj); |
| 2686 | Py_RETURN_NONE; |
| 2687 | } |
| 2688 | |
| 2689 | static PyObject * |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 2690 | test_pymem_alloc0(PyObject *self) |
| 2691 | { |
| 2692 | void *ptr; |
| 2693 | |
| 2694 | ptr = PyMem_Malloc(0); |
| 2695 | if (ptr == NULL) { |
| 2696 | PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); |
| 2697 | return NULL; |
| 2698 | } |
| 2699 | PyMem_Free(ptr); |
| 2700 | |
| 2701 | ptr = PyObject_Malloc(0); |
| 2702 | if (ptr == NULL) { |
| 2703 | PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); |
| 2704 | return NULL; |
| 2705 | } |
| 2706 | PyObject_Free(ptr); |
| 2707 | |
| 2708 | Py_RETURN_NONE; |
| 2709 | } |
| 2710 | |
| 2711 | typedef struct { |
| 2712 | PyMemAllocator alloc; |
| 2713 | |
| 2714 | size_t malloc_size; |
| 2715 | void *realloc_ptr; |
| 2716 | size_t realloc_new_size; |
| 2717 | void *free_ptr; |
| 2718 | } alloc_hook_t; |
| 2719 | |
| 2720 | static void* hook_malloc (void* ctx, size_t size) |
| 2721 | { |
| 2722 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 2723 | hook->malloc_size = size; |
| 2724 | return hook->alloc.malloc(hook->alloc.ctx, size); |
| 2725 | } |
| 2726 | |
| 2727 | static void* hook_realloc (void* ctx, void* ptr, size_t new_size) |
| 2728 | { |
| 2729 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 2730 | hook->realloc_ptr = ptr; |
| 2731 | hook->realloc_new_size = new_size; |
| 2732 | return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); |
| 2733 | } |
| 2734 | |
| 2735 | static void hook_free (void *ctx, void *ptr) |
| 2736 | { |
| 2737 | alloc_hook_t *hook = (alloc_hook_t *)ctx; |
| 2738 | hook->free_ptr = ptr; |
| 2739 | hook->alloc.free(hook->alloc.ctx, ptr); |
| 2740 | } |
| 2741 | |
| 2742 | static PyObject * |
| 2743 | test_setallocators(PyMemAllocatorDomain domain) |
| 2744 | { |
| 2745 | PyObject *res = NULL; |
| 2746 | const char *error_msg; |
| 2747 | alloc_hook_t hook; |
| 2748 | PyMemAllocator alloc; |
| 2749 | size_t size, size2; |
| 2750 | void *ptr, *ptr2; |
| 2751 | |
| 2752 | hook.malloc_size = 0; |
| 2753 | hook.realloc_ptr = NULL; |
| 2754 | hook.realloc_new_size = 0; |
| 2755 | hook.free_ptr = NULL; |
| 2756 | |
| 2757 | alloc.ctx = &hook; |
| 2758 | alloc.malloc = &hook_malloc; |
| 2759 | alloc.realloc = &hook_realloc; |
| 2760 | alloc.free = &hook_free; |
| 2761 | PyMem_GetAllocator(domain, &hook.alloc); |
| 2762 | PyMem_SetAllocator(domain, &alloc); |
| 2763 | |
| 2764 | size = 42; |
| 2765 | switch(domain) |
| 2766 | { |
| 2767 | case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; |
| 2768 | case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; |
| 2769 | case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; |
| 2770 | default: ptr = NULL; break; |
| 2771 | } |
| 2772 | |
| 2773 | if (ptr == NULL) { |
| 2774 | error_msg = "malloc failed"; |
| 2775 | goto fail; |
| 2776 | } |
| 2777 | |
| 2778 | if (hook.malloc_size != size) { |
| 2779 | error_msg = "malloc invalid size"; |
| 2780 | goto fail; |
| 2781 | } |
| 2782 | |
| 2783 | size2 = 200; |
| 2784 | switch(domain) |
| 2785 | { |
| 2786 | case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; |
| 2787 | case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; |
| 2788 | case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; |
Christian Heimes | 865d12a | 2013-08-02 11:10:51 +0200 | [diff] [blame] | 2789 | default: ptr2 = NULL; break; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | if (ptr2 == NULL) { |
| 2793 | error_msg = "realloc failed"; |
| 2794 | goto fail; |
| 2795 | } |
| 2796 | |
| 2797 | if (hook.realloc_ptr != ptr |
| 2798 | || hook.realloc_new_size != size2) { |
| 2799 | error_msg = "realloc invalid parameters"; |
| 2800 | goto fail; |
| 2801 | } |
| 2802 | |
| 2803 | switch(domain) |
| 2804 | { |
| 2805 | case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; |
| 2806 | case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; |
| 2807 | case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; |
| 2808 | } |
| 2809 | |
| 2810 | if (hook.free_ptr != ptr2) { |
| 2811 | error_msg = "free invalid pointer"; |
| 2812 | goto fail; |
| 2813 | } |
| 2814 | |
| 2815 | Py_INCREF(Py_None); |
| 2816 | res = Py_None; |
| 2817 | goto finally; |
| 2818 | |
| 2819 | fail: |
| 2820 | PyErr_SetString(PyExc_RuntimeError, error_msg); |
| 2821 | |
| 2822 | finally: |
| 2823 | PyMem_SetAllocator(domain, &hook.alloc); |
| 2824 | return res; |
| 2825 | } |
| 2826 | |
| 2827 | static PyObject * |
| 2828 | test_pymem_setrawallocators(PyObject *self) |
| 2829 | { |
| 2830 | return test_setallocators(PYMEM_DOMAIN_RAW); |
| 2831 | } |
| 2832 | |
| 2833 | static PyObject * |
| 2834 | test_pymem_setallocators(PyObject *self) |
| 2835 | { |
| 2836 | return test_setallocators(PYMEM_DOMAIN_MEM); |
| 2837 | } |
| 2838 | |
| 2839 | static PyObject * |
| 2840 | test_pyobject_setallocators(PyObject *self) |
| 2841 | { |
| 2842 | return test_setallocators(PYMEM_DOMAIN_OBJ); |
| 2843 | } |
| 2844 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2845 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 2847 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
| 2848 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 2849 | {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
| 2851 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 2852 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 2853 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 2855 | {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS}, |
| 2856 | {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak, METH_NOARGS}, |
| 2857 | {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak, METH_NOARGS}, |
| 2858 | {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak, METH_NOARGS}, |
Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 2859 | {"test_incref_decref_API", (PyCFunction)test_incref_decref_API, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 2861 | METH_NOARGS}, |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2862 | {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS}, |
| 2863 | {"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] | 2864 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 2865 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 2866 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 2867 | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 2869 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 2870 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 2871 | PyDoc_STR("This is a pretty normal docstring.")}, |
| 2872 | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
| 2873 | {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, |
| 2874 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
| 2875 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 2876 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 2877 | METH_VARARGS|METH_KEYWORDS}, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 2878 | {"getargs_keyword_only", (PyCFunction)getargs_keyword_only, |
| 2879 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 2881 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 2882 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 2883 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 2884 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 2885 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 2886 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 2887 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 2888 | {"getargs_n", getargs_n, METH_VARARGS}, |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 2889 | {"getargs_p", getargs_p, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2890 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 2892 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 2893 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 2894 | {"test_long_long_and_overflow", |
| 2895 | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
| 2896 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 2897 | #endif |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 2898 | {"getargs_c", getargs_c, METH_VARARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 2899 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 2900 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 2901 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 2902 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 2903 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 2904 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 2905 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 2906 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 2907 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 2908 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 2909 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 2910 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 2911 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2912 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2913 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2914 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2915 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2916 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2917 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
| 2918 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 2919 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
| 2920 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2921 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 2922 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
| 2923 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 2924 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 2925 | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2926 | #ifdef WITH_THREAD |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2927 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2929 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2930 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2931 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2932 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2933 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 2934 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2935 | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2936 | {"argparsing", argparsing, METH_VARARGS}, |
| 2937 | {"code_newempty", code_newempty, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 2939 | METH_VARARGS | METH_KEYWORDS}, |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2940 | {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer, |
| 2941 | METH_NOARGS}, |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2942 | {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2943 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2944 | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
| 2945 | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2946 | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 2947 | {"with_tp_del", with_tp_del, METH_VARARGS}, |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 2948 | {"test_pymem", |
| 2949 | (PyCFunction)test_pymem_alloc0, METH_NOARGS}, |
| 2950 | {"test_pymem_alloc0", |
| 2951 | (PyCFunction)test_pymem_setrawallocators, METH_NOARGS}, |
| 2952 | {"test_pymem_setallocators", |
| 2953 | (PyCFunction)test_pymem_setallocators, METH_NOARGS}, |
| 2954 | {"test_pyobject_setallocators", |
| 2955 | (PyCFunction)test_pyobject_setallocators, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2957 | }; |
| 2958 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2959 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 2960 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2961 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2962 | char bool_member; |
| 2963 | char byte_member; |
| 2964 | unsigned char ubyte_member; |
| 2965 | short short_member; |
| 2966 | unsigned short ushort_member; |
| 2967 | int int_member; |
| 2968 | unsigned int uint_member; |
| 2969 | long long_member; |
| 2970 | unsigned long ulong_member; |
| 2971 | Py_ssize_t pyssizet_member; |
| 2972 | float float_member; |
| 2973 | double double_member; |
| 2974 | char inplace_member[6]; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2975 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | PY_LONG_LONG longlong_member; |
| 2977 | unsigned PY_LONG_LONG ulonglong_member; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2978 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2979 | } all_structmembers; |
| 2980 | |
| 2981 | typedef struct { |
| 2982 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2984 | } test_structmembers; |
| 2985 | |
| 2986 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2987 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 2988 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 2989 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 2990 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 2991 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 2992 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 2993 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 2994 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 2995 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 2996 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 2997 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 2998 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 2999 | {"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] | 3000 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 3002 | {"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] | 3003 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3004 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3005 | }; |
| 3006 | |
| 3007 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 3008 | static PyObject * |
| 3009 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 3010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | static char *keywords[] = { |
| 3012 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 3013 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 3014 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 3015 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | "T_LONGLONG", "T_ULONGLONG", |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 3017 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | NULL}; |
| 3019 | static char *fmt = "|bbBhHiIlknfds#" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 3020 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | "LK" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 3022 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | ; |
| 3024 | test_structmembers *ob; |
| 3025 | const char *s = NULL; |
| 3026 | Py_ssize_t string_len = 0; |
| 3027 | ob = PyObject_New(test_structmembers, type); |
| 3028 | if (ob == NULL) |
| 3029 | return NULL; |
| 3030 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 3031 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 3032 | &ob->structmembers.bool_member, |
| 3033 | &ob->structmembers.byte_member, |
| 3034 | &ob->structmembers.ubyte_member, |
| 3035 | &ob->structmembers.short_member, |
| 3036 | &ob->structmembers.ushort_member, |
| 3037 | &ob->structmembers.int_member, |
| 3038 | &ob->structmembers.uint_member, |
| 3039 | &ob->structmembers.long_member, |
| 3040 | &ob->structmembers.ulong_member, |
| 3041 | &ob->structmembers.pyssizet_member, |
| 3042 | &ob->structmembers.float_member, |
| 3043 | &ob->structmembers.double_member, |
| 3044 | &s, &string_len |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 3045 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3046 | , &ob->structmembers.longlong_member, |
| 3047 | &ob->structmembers.ulonglong_member |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 3048 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3049 | )) { |
| 3050 | Py_DECREF(ob); |
| 3051 | return NULL; |
| 3052 | } |
| 3053 | if (s != NULL) { |
| 3054 | if (string_len > 5) { |
| 3055 | Py_DECREF(ob); |
| 3056 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 3057 | return NULL; |
| 3058 | } |
| 3059 | strcpy(ob->structmembers.inplace_member, s); |
| 3060 | } |
| 3061 | else { |
| 3062 | strcpy(ob->structmembers.inplace_member, ""); |
| 3063 | } |
| 3064 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 3067 | static void |
| 3068 | test_structmembers_free(PyObject *ob) |
| 3069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3074 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | "test_structmembersType", |
| 3076 | sizeof(test_structmembers), /* tp_basicsize */ |
| 3077 | 0, /* tp_itemsize */ |
| 3078 | test_structmembers_free, /* destructor tp_dealloc */ |
| 3079 | 0, /* tp_print */ |
| 3080 | 0, /* tp_getattr */ |
| 3081 | 0, /* tp_setattr */ |
| 3082 | 0, /* tp_reserved */ |
| 3083 | 0, /* tp_repr */ |
| 3084 | 0, /* tp_as_number */ |
| 3085 | 0, /* tp_as_sequence */ |
| 3086 | 0, /* tp_as_mapping */ |
| 3087 | 0, /* tp_hash */ |
| 3088 | 0, /* tp_call */ |
| 3089 | 0, /* tp_str */ |
| 3090 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3091 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 3092 | 0, /* tp_as_buffer */ |
| 3093 | 0, /* tp_flags */ |
| 3094 | "Type containing all structmember types", |
| 3095 | 0, /* traverseproc tp_traverse */ |
| 3096 | 0, /* tp_clear */ |
| 3097 | 0, /* tp_richcompare */ |
| 3098 | 0, /* tp_weaklistoffset */ |
| 3099 | 0, /* tp_iter */ |
| 3100 | 0, /* tp_iternext */ |
| 3101 | 0, /* tp_methods */ |
| 3102 | test_members, /* tp_members */ |
| 3103 | 0, |
| 3104 | 0, |
| 3105 | 0, |
| 3106 | 0, |
| 3107 | 0, |
| 3108 | 0, |
| 3109 | 0, |
| 3110 | 0, |
| 3111 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3112 | }; |
| 3113 | |
| 3114 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3115 | |
| 3116 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | PyModuleDef_HEAD_INIT, |
| 3118 | "_testcapi", |
| 3119 | NULL, |
| 3120 | -1, |
| 3121 | TestMethods, |
| 3122 | NULL, |
| 3123 | NULL, |
| 3124 | NULL, |
| 3125 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3126 | }; |
| 3127 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 3128 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3129 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 3130 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 3132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | m = PyModule_Create(&_testcapimodule); |
| 3134 | if (m == NULL) |
| 3135 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 3136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 3138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 3140 | Py_INCREF(&test_structmembersType); |
| 3141 | /* don't use a name starting with "test", since we don't want |
| 3142 | test_capi to automatically call this */ |
| 3143 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 3146 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 3147 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 3148 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 3149 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 3150 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 3151 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 3152 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 3153 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 3154 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 3155 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 3156 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 3157 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 3158 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 3159 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 3160 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 3161 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 3162 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 3163 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 3164 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 3165 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
| 3166 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
| 3167 | Py_INCREF(&PyInstanceMethod_Type); |
| 3168 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 3169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 3171 | Py_INCREF(TestError); |
| 3172 | PyModule_AddObject(m, "error", TestError); |
| 3173 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 3174 | } |