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" |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 13 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 14 | #ifdef WITH_THREAD |
| 15 | #include "pythread.h" |
| 16 | #endif /* WITH_THREAD */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 17 | static PyObject *TestError; /* set to exception object in init */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 18 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 19 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 20 | |
| 21 | static PyObject * |
| 22 | raiseTestError(const char* test_name, const char* msg) |
| 23 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 24 | char buf[2048]; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 25 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 26 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 27 | PyErr_SetString(TestError, "internal error msg too large"); |
| 28 | else { |
| 29 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
| 30 | PyErr_SetString(TestError, buf); |
| 31 | } |
| 32 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 35 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 36 | |
| 37 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 38 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 39 | platforms have these hardcoded. Better safe than sorry. |
| 40 | */ |
| 41 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 42 | sizeof_error(const char* fatname, const char* typname, |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 43 | int expected, int got) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 44 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 45 | char buf[1024]; |
| 46 | PyOS_snprintf(buf, sizeof(buf), |
| 47 | "%.200s #define == %d but sizeof(%.200s) == %d", |
| 48 | fatname, expected, typname, got); |
| 49 | PyErr_SetString(TestError, buf); |
| 50 | return (PyObject*)NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 54 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 55 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 56 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 57 | if (FATNAME != sizeof(TYPE)) \ |
| 58 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 60 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 61 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 62 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 63 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 64 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 65 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 66 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | #undef CHECK_SIZEOF |
| 70 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 71 | Py_INCREF(Py_None); |
| 72 | return Py_None; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 75 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 76 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 77 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 78 | PyObject* list; |
| 79 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 81 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 82 | #define NLIST 30 |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 83 | list = PyList_New(NLIST); |
| 84 | if (list == (PyObject*)NULL) |
| 85 | return (PyObject*)NULL; |
| 86 | /* list = range(NLIST) */ |
| 87 | for (i = 0; i < NLIST; ++i) { |
| 88 | PyObject* anint = PyLong_FromLong(i); |
| 89 | if (anint == (PyObject*)NULL) { |
| 90 | Py_DECREF(list); |
| 91 | return (PyObject*)NULL; |
| 92 | } |
| 93 | PyList_SET_ITEM(list, i, anint); |
| 94 | } |
| 95 | /* list.reverse(), via PyList_Reverse() */ |
| 96 | i = PyList_Reverse(list); /* should not blow up! */ |
| 97 | if (i != 0) { |
| 98 | Py_DECREF(list); |
| 99 | return (PyObject*)NULL; |
| 100 | } |
| 101 | /* Check that list == range(29, -1, -1) now */ |
| 102 | for (i = 0; i < NLIST; ++i) { |
| 103 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 104 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 105 | PyErr_SetString(TestError, |
| 106 | "test_list_api: reverse screwed up"); |
| 107 | Py_DECREF(list); |
| 108 | return (PyObject*)NULL; |
| 109 | } |
| 110 | } |
| 111 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 112 | #undef NLIST |
| 113 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 114 | Py_INCREF(Py_None); |
| 115 | return Py_None; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 118 | static int |
| 119 | test_dict_inner(int count) |
| 120 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 121 | Py_ssize_t pos = 0, iterations = 0; |
| 122 | int i; |
| 123 | PyObject *dict = PyDict_New(); |
| 124 | PyObject *v, *k; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 125 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 126 | if (dict == NULL) |
| 127 | return -1; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 128 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 129 | for (i = 0; i < count; i++) { |
| 130 | v = PyLong_FromLong(i); |
| 131 | PyDict_SetItem(dict, v, v); |
| 132 | Py_DECREF(v); |
| 133 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 135 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 136 | PyObject *o; |
| 137 | iterations++; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 139 | i = PyLong_AS_LONG(v) + 1; |
| 140 | o = PyLong_FromLong(i); |
| 141 | if (o == NULL) |
| 142 | return -1; |
| 143 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 144 | Py_DECREF(o); |
| 145 | return -1; |
| 146 | } |
| 147 | Py_DECREF(o); |
| 148 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 149 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 150 | Py_DECREF(dict); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 151 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 152 | if (iterations != count) { |
| 153 | PyErr_SetString( |
| 154 | TestError, |
| 155 | "test_dict_iteration: dict iteration went wrong "); |
| 156 | return -1; |
| 157 | } else { |
| 158 | return 0; |
| 159 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 163 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 164 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 165 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 166 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 167 | for (i = 0; i < 200; i++) { |
| 168 | if (test_dict_inner(i) < 0) { |
| 169 | return NULL; |
| 170 | } |
| 171 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 172 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 173 | Py_INCREF(Py_None); |
| 174 | return Py_None; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 177 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 178 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 179 | * PyType_Ready if it hasn't already been called |
| 180 | */ |
| 181 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 182 | PyVarObject_HEAD_INIT(NULL, 0) |
| 183 | "hashinheritancetester", /* Name of this type */ |
| 184 | sizeof(PyObject), /* Basic object size */ |
| 185 | 0, /* Item size for varobject */ |
| 186 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 187 | 0, /* tp_print */ |
| 188 | 0, /* tp_getattr */ |
| 189 | 0, /* tp_setattr */ |
| 190 | 0, /* tp_reserved */ |
| 191 | 0, /* tp_repr */ |
| 192 | 0, /* tp_as_number */ |
| 193 | 0, /* tp_as_sequence */ |
| 194 | 0, /* tp_as_mapping */ |
| 195 | 0, /* tp_hash */ |
| 196 | 0, /* tp_call */ |
| 197 | 0, /* tp_str */ |
| 198 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 199 | 0, /* tp_setattro */ |
| 200 | 0, /* tp_as_buffer */ |
| 201 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 202 | 0, /* tp_doc */ |
| 203 | 0, /* tp_traverse */ |
| 204 | 0, /* tp_clear */ |
| 205 | 0, /* tp_richcompare */ |
| 206 | 0, /* tp_weaklistoffset */ |
| 207 | 0, /* tp_iter */ |
| 208 | 0, /* tp_iternext */ |
| 209 | 0, /* tp_methods */ |
| 210 | 0, /* tp_members */ |
| 211 | 0, /* tp_getset */ |
| 212 | 0, /* tp_base */ |
| 213 | 0, /* tp_dict */ |
| 214 | 0, /* tp_descr_get */ |
| 215 | 0, /* tp_descr_set */ |
| 216 | 0, /* tp_dictoffset */ |
| 217 | 0, /* tp_init */ |
| 218 | 0, /* tp_alloc */ |
| 219 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | static PyObject* |
| 223 | test_lazy_hash_inheritance(PyObject* self) |
| 224 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 225 | PyTypeObject *type; |
| 226 | PyObject *obj; |
| 227 | long hash; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 229 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 230 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 231 | if (type->tp_dict != NULL) |
| 232 | /* The type has already been initialized. This probably means |
| 233 | -R is being used. */ |
| 234 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 235 | |
| 236 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 237 | obj = PyObject_New(PyObject, type); |
| 238 | if (obj == NULL) { |
| 239 | PyErr_Clear(); |
| 240 | PyErr_SetString( |
| 241 | TestError, |
| 242 | "test_lazy_hash_inheritance: failed to create object"); |
| 243 | return NULL; |
| 244 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 246 | if (type->tp_dict != NULL) { |
| 247 | PyErr_SetString( |
| 248 | TestError, |
| 249 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 250 | Py_DECREF(obj); |
| 251 | return NULL; |
| 252 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 253 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 254 | hash = PyObject_Hash(obj); |
| 255 | if ((hash == -1) && PyErr_Occurred()) { |
| 256 | PyErr_Clear(); |
| 257 | PyErr_SetString( |
| 258 | TestError, |
| 259 | "test_lazy_hash_inheritance: could not hash object"); |
| 260 | Py_DECREF(obj); |
| 261 | return NULL; |
| 262 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 263 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 264 | if (type->tp_dict == NULL) { |
| 265 | PyErr_SetString( |
| 266 | TestError, |
| 267 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 268 | Py_DECREF(obj); |
| 269 | return NULL; |
| 270 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 271 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 272 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 273 | PyErr_SetString( |
| 274 | TestError, |
| 275 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 276 | Py_DECREF(obj); |
| 277 | return NULL; |
| 278 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 280 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 282 | Py_RETURN_NONE; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
Antoine Pitrou | 9336818 | 2010-02-02 22:51:34 +0000 | [diff] [blame] | 286 | /* Issue #7385: Check that memoryview() does not crash |
| 287 | * when bf_getbuffer returns an error |
| 288 | */ |
| 289 | |
| 290 | static int |
| 291 | broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags) |
| 292 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 293 | PyErr_SetString( |
| 294 | TestError, |
| 295 | "test_broken_memoryview: expected error in bf_getbuffer"); |
| 296 | return -1; |
Antoine Pitrou | 9336818 | 2010-02-02 22:51:34 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static PyBufferProcs memoryviewtester_as_buffer = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 300 | (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */ |
| 301 | 0, /* bf_releasebuffer */ |
Antoine Pitrou | 9336818 | 2010-02-02 22:51:34 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | static PyTypeObject _MemoryViewTester_Type = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 305 | PyVarObject_HEAD_INIT(NULL, 0) |
| 306 | "memoryviewtester", /* Name of this type */ |
| 307 | sizeof(PyObject), /* Basic object size */ |
| 308 | 0, /* Item size for varobject */ |
| 309 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 310 | 0, /* tp_print */ |
| 311 | 0, /* tp_getattr */ |
| 312 | 0, /* tp_setattr */ |
| 313 | 0, /* tp_compare */ |
| 314 | 0, /* tp_repr */ |
| 315 | 0, /* tp_as_number */ |
| 316 | 0, /* tp_as_sequence */ |
| 317 | 0, /* tp_as_mapping */ |
| 318 | 0, /* tp_hash */ |
| 319 | 0, /* tp_call */ |
| 320 | 0, /* tp_str */ |
| 321 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 322 | 0, /* tp_setattro */ |
| 323 | &memoryviewtester_as_buffer, /* tp_as_buffer */ |
| 324 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 325 | 0, /* tp_doc */ |
| 326 | 0, /* tp_traverse */ |
| 327 | 0, /* tp_clear */ |
| 328 | 0, /* tp_richcompare */ |
| 329 | 0, /* tp_weaklistoffset */ |
| 330 | 0, /* tp_iter */ |
| 331 | 0, /* tp_iternext */ |
| 332 | 0, /* tp_methods */ |
| 333 | 0, /* tp_members */ |
| 334 | 0, /* tp_getset */ |
| 335 | 0, /* tp_base */ |
| 336 | 0, /* tp_dict */ |
| 337 | 0, /* tp_descr_get */ |
| 338 | 0, /* tp_descr_set */ |
| 339 | 0, /* tp_dictoffset */ |
| 340 | 0, /* tp_init */ |
| 341 | 0, /* tp_alloc */ |
| 342 | PyType_GenericNew, /* tp_new */ |
Antoine Pitrou | 9336818 | 2010-02-02 22:51:34 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | static PyObject* |
| 346 | test_broken_memoryview(PyObject* self) |
| 347 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 348 | PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type); |
| 349 | PyObject *res; |
Antoine Pitrou | 9336818 | 2010-02-02 22:51:34 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 351 | if (obj == NULL) { |
| 352 | PyErr_Clear(); |
| 353 | PyErr_SetString( |
| 354 | TestError, |
| 355 | "test_broken_memoryview: failed to create object"); |
| 356 | return NULL; |
| 357 | } |
Antoine Pitrou | 9336818 | 2010-02-02 22:51:34 +0000 | [diff] [blame] | 358 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 359 | res = PyMemoryView_FromObject(obj); |
| 360 | if (res || !PyErr_Occurred()){ |
| 361 | PyErr_SetString( |
| 362 | TestError, |
| 363 | "test_broken_memoryview: memoryview() didn't raise an Exception"); |
| 364 | Py_XDECREF(res); |
| 365 | Py_DECREF(obj); |
| 366 | return NULL; |
| 367 | } |
Antoine Pitrou | 9336818 | 2010-02-02 22:51:34 +0000 | [diff] [blame] | 368 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 369 | PyErr_Clear(); |
| 370 | Py_DECREF(obj); |
| 371 | Py_RETURN_NONE; |
Antoine Pitrou | 9336818 | 2010-02-02 22:51:34 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 375 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 376 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 377 | |
| 378 | Note that the meat of the test is contained in testcapi_long.h. |
| 379 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 380 | 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] | 381 | dependence on type names makes it impossible to use a parameterized |
| 382 | function. A giant macro would be even worse than this. A C++ template |
| 383 | would be perfect. |
| 384 | |
| 385 | The "report an error" functions are deliberately not part of the #include |
| 386 | file: if the test fails, you can set a breakpoint in the appropriate |
| 387 | error function directly, and crawl back from there in the debugger. |
| 388 | */ |
| 389 | |
| 390 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 391 | |
| 392 | static PyObject * |
| 393 | raise_test_long_error(const char* msg) |
| 394 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 395 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 398 | #define TESTNAME test_long_api_inner |
| 399 | #define TYPENAME long |
| 400 | #define F_S_TO_PY PyLong_FromLong |
| 401 | #define F_PY_TO_S PyLong_AsLong |
| 402 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 403 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 404 | |
| 405 | #include "testcapi_long.h" |
| 406 | |
| 407 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 408 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 409 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 410 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | #undef TESTNAME |
| 414 | #undef TYPENAME |
| 415 | #undef F_S_TO_PY |
| 416 | #undef F_PY_TO_S |
| 417 | #undef F_U_TO_PY |
| 418 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 419 | |
| 420 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 421 | |
| 422 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 423 | raise_test_longlong_error(const char* msg) |
| 424 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 425 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 428 | #define TESTNAME test_longlong_api_inner |
| 429 | #define TYPENAME PY_LONG_LONG |
| 430 | #define F_S_TO_PY PyLong_FromLongLong |
| 431 | #define F_PY_TO_S PyLong_AsLongLong |
| 432 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 433 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 434 | |
| 435 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 436 | |
| 437 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 438 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 439 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 440 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 443 | #undef TESTNAME |
| 444 | #undef TYPENAME |
| 445 | #undef F_S_TO_PY |
| 446 | #undef F_PY_TO_S |
| 447 | #undef F_U_TO_PY |
| 448 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 449 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 450 | /* 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] | 451 | for both long and int arguments. The test may leak a little memory if |
| 452 | it fails. |
| 453 | */ |
| 454 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 455 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 456 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 457 | PyObject *tuple, *num; |
| 458 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 460 | tuple = PyTuple_New(1); |
| 461 | if (tuple == NULL) |
| 462 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 463 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 464 | num = PyLong_FromLong(42); |
| 465 | if (num == NULL) |
| 466 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 467 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 468 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 470 | value = -1; |
| 471 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 472 | return NULL; |
| 473 | if (value != 42) |
| 474 | return raiseTestError("test_L_code", |
| 475 | "L code returned wrong value for long 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 476 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 477 | Py_DECREF(num); |
| 478 | num = PyLong_FromLong(42); |
| 479 | if (num == NULL) |
| 480 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 481 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 482 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 483 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 484 | value = -1; |
| 485 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 486 | return NULL; |
| 487 | if (value != 42) |
| 488 | return raiseTestError("test_L_code", |
| 489 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 491 | Py_DECREF(tuple); |
| 492 | Py_INCREF(Py_None); |
| 493 | return Py_None; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 496 | #endif /* ifdef HAVE_LONG_LONG */ |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 497 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 498 | /* Test tuple argument processing */ |
| 499 | static PyObject * |
| 500 | getargs_tuple(PyObject *self, PyObject *args) |
| 501 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 502 | int a, b, c; |
| 503 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 504 | return NULL; |
| 505 | return Py_BuildValue("iii", a, b, c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 508 | /* test PyArg_ParseTupleAndKeywords */ |
| 509 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 510 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 511 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 512 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 513 | 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] | 514 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 515 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 516 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 517 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 518 | return NULL; |
| 519 | return Py_BuildValue("iiiiiiiiii", |
| 520 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 521 | 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] | 522 | } |
| 523 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 524 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 525 | and return the result. |
| 526 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 527 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 528 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 529 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 530 | unsigned char value; |
| 531 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 532 | return NULL; |
| 533 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 536 | static PyObject * |
| 537 | getargs_B(PyObject *self, PyObject *args) |
| 538 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 539 | unsigned char value; |
| 540 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 541 | return NULL; |
| 542 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static PyObject * |
| 546 | getargs_H(PyObject *self, PyObject *args) |
| 547 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 548 | unsigned short value; |
| 549 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 550 | return NULL; |
| 551 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | static PyObject * |
| 555 | getargs_I(PyObject *self, PyObject *args) |
| 556 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 557 | unsigned int value; |
| 558 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 559 | return NULL; |
| 560 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static PyObject * |
| 564 | getargs_k(PyObject *self, PyObject *args) |
| 565 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 566 | unsigned long value; |
| 567 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 568 | return NULL; |
| 569 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static PyObject * |
| 573 | getargs_i(PyObject *self, PyObject *args) |
| 574 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 575 | int value; |
| 576 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 577 | return NULL; |
| 578 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 581 | static PyObject * |
| 582 | getargs_l(PyObject *self, PyObject *args) |
| 583 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 584 | long value; |
| 585 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 586 | return NULL; |
| 587 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 590 | static PyObject * |
| 591 | getargs_n(PyObject *self, PyObject *args) |
| 592 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 593 | Py_ssize_t value; |
| 594 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 595 | return NULL; |
| 596 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 599 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 600 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 601 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 602 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 603 | PY_LONG_LONG value; |
| 604 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 605 | return NULL; |
| 606 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 609 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 610 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 611 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 612 | unsigned PY_LONG_LONG value; |
| 613 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 614 | return NULL; |
| 615 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 616 | } |
| 617 | #endif |
| 618 | |
| 619 | /* This function not only tests the 'k' getargs code, but also the |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 620 | PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 621 | static PyObject * |
| 622 | test_k_code(PyObject *self) |
| 623 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 624 | PyObject *tuple, *num; |
| 625 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 626 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 627 | tuple = PyTuple_New(1); |
| 628 | if (tuple == NULL) |
| 629 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 631 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 632 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 633 | if (num == NULL) |
| 634 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 635 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 636 | value = PyLong_AsUnsignedLongMask(num); |
| 637 | if (value != ULONG_MAX) |
| 638 | return raiseTestError("test_k_code", |
| 639 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 641 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 643 | value = 0; |
| 644 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 645 | return NULL; |
| 646 | if (value != ULONG_MAX) |
| 647 | return raiseTestError("test_k_code", |
| 648 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 649 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 650 | Py_DECREF(num); |
| 651 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 652 | if (num == NULL) |
| 653 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 655 | value = PyLong_AsUnsignedLongMask(num); |
| 656 | if (value != (unsigned long)-0x42) |
| 657 | return raiseTestError("test_k_code", |
| 658 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 660 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 661 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 662 | value = 0; |
| 663 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 664 | return NULL; |
| 665 | if (value != (unsigned long)-0x42) |
| 666 | return raiseTestError("test_k_code", |
| 667 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 669 | Py_DECREF(tuple); |
| 670 | Py_INCREF(Py_None); |
| 671 | return Py_None; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 674 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 675 | /* Test the s and z codes for PyArg_ParseTuple. |
| 676 | */ |
| 677 | static PyObject * |
| 678 | test_s_code(PyObject *self) |
| 679 | { |
| 680 | /* Unicode strings should be accepted */ |
| 681 | PyObject *tuple, *obj; |
| 682 | char *value; |
| 683 | |
| 684 | tuple = PyTuple_New(1); |
| 685 | if (tuple == NULL) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 686 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 687 | |
| 688 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 689 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 690 | if (obj == NULL) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 691 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 692 | |
| 693 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 694 | |
| 695 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 696 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 697 | */ |
| 698 | if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 699 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 700 | |
| 701 | if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 702 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 703 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 704 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 705 | Py_RETURN_NONE; |
| 706 | } |
| 707 | |
Mark Dickinson | d0ed0db | 2009-12-03 11:01:53 +0000 | [diff] [blame] | 708 | static PyObject * |
| 709 | test_bug_7414(PyObject *self) |
| 710 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 711 | /* Issue #7414: for PyArg_ParseTupleAndKeywords, 'C' code wasn't being |
| 712 | skipped properly in skipitem() */ |
| 713 | int a = 0, b = 0, result; |
| 714 | char *kwlist[] = {"a", "b", NULL}; |
| 715 | PyObject *tuple = NULL, *dict = NULL, *b_str; |
Mark Dickinson | d0ed0db | 2009-12-03 11:01:53 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 717 | tuple = PyTuple_New(0); |
| 718 | if (tuple == NULL) |
| 719 | goto failure; |
| 720 | dict = PyDict_New(); |
| 721 | if (dict == NULL) |
| 722 | goto failure; |
| 723 | b_str = PyUnicode_FromString("b"); |
| 724 | if (b_str == NULL) |
| 725 | goto failure; |
| 726 | result = PyDict_SetItemString(dict, "b", b_str); |
| 727 | Py_DECREF(b_str); |
| 728 | if (result < 0) |
| 729 | goto failure; |
Mark Dickinson | d0ed0db | 2009-12-03 11:01:53 +0000 | [diff] [blame] | 730 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 731 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|CC", |
| 732 | kwlist, &a, &b); |
| 733 | if (!result) |
| 734 | goto failure; |
Mark Dickinson | d0ed0db | 2009-12-03 11:01:53 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 736 | if (a != 0) |
| 737 | return raiseTestError("test_bug_7414", |
| 738 | "C format code not skipped properly"); |
| 739 | if (b != 'b') |
| 740 | return raiseTestError("test_bug_7414", |
| 741 | "C format code returned wrong value"); |
Mark Dickinson | d0ed0db | 2009-12-03 11:01:53 +0000 | [diff] [blame] | 742 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 743 | Py_DECREF(dict); |
| 744 | Py_DECREF(tuple); |
| 745 | Py_RETURN_NONE; |
Mark Dickinson | d0ed0db | 2009-12-03 11:01:53 +0000 | [diff] [blame] | 746 | |
| 747 | failure: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 748 | Py_XDECREF(dict); |
| 749 | Py_XDECREF(tuple); |
| 750 | return NULL; |
Mark Dickinson | d0ed0db | 2009-12-03 11:01:53 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 754 | static volatile int x; |
| 755 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 756 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 757 | of an error. |
| 758 | */ |
| 759 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 760 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 761 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 762 | PyObject *tuple, *obj; |
| 763 | Py_UNICODE *value; |
| 764 | Py_ssize_t len; |
| 765 | int x; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 766 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 767 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 768 | /* Just use the macro and check that it compiles */ |
| 769 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 771 | tuple = PyTuple_New(1); |
| 772 | if (tuple == NULL) |
| 773 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 774 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 775 | obj = PyUnicode_Decode("test", strlen("test"), |
| 776 | "ascii", NULL); |
| 777 | if (obj == NULL) |
| 778 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 779 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 780 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 781 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 782 | value = 0; |
| 783 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 784 | return NULL; |
| 785 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 786 | return raiseTestError("test_u_code", |
| 787 | "u code returned wrong value for u'test'"); |
| 788 | value = 0; |
| 789 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 790 | return NULL; |
| 791 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 792 | len != PyUnicode_GET_SIZE(obj)) |
| 793 | return raiseTestError("test_u_code", |
| 794 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 795 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 796 | Py_DECREF(tuple); |
| 797 | Py_INCREF(Py_None); |
| 798 | return Py_None; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 801 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 802 | static PyObject * |
| 803 | test_Z_code(PyObject *self) |
| 804 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 805 | PyObject *tuple, *obj; |
| 806 | Py_UNICODE *value1, *value2; |
| 807 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 808 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 809 | tuple = PyTuple_New(2); |
| 810 | if (tuple == NULL) |
| 811 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 812 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 813 | obj = PyUnicode_FromString("test"); |
| 814 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 815 | Py_INCREF(Py_None); |
| 816 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 818 | /* swap values on purpose */ |
| 819 | value1 = NULL; |
| 820 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 822 | /* Test Z for both values */ |
| 823 | if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) |
| 824 | return NULL; |
| 825 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 826 | return raiseTestError("test_Z_code", |
| 827 | "Z code returned wrong value for 'test'"); |
| 828 | if (value2 != NULL) |
| 829 | return raiseTestError("test_Z_code", |
| 830 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 831 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 832 | value1 = NULL; |
| 833 | value2 = PyUnicode_AS_UNICODE(obj); |
| 834 | len1 = -1; |
| 835 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 836 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 837 | /* Test Z# for both values */ |
| 838 | if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 839 | &value2, &len2) < 0) |
| 840 | return NULL; |
| 841 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 842 | len1 != PyUnicode_GET_SIZE(obj)) |
| 843 | return raiseTestError("test_Z_code", |
| 844 | "Z# code returned wrong values for 'test'"); |
| 845 | if (value2 != NULL || |
| 846 | len2 != 0) |
| 847 | return raiseTestError("test_Z_code", |
| 848 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 849 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 850 | Py_DECREF(tuple); |
| 851 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 854 | static PyObject * |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 855 | test_widechar(PyObject *self) |
| 856 | { |
| 857 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 858 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 859 | size_t wtextlen = 1; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 860 | #else |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 861 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 862 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 863 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 864 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 866 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 867 | if (wide == NULL) |
| 868 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 870 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 871 | if (utf8 == NULL) { |
| 872 | Py_DECREF(wide); |
| 873 | return NULL; |
| 874 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 876 | if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) { |
| 877 | Py_DECREF(wide); |
| 878 | Py_DECREF(utf8); |
| 879 | return raiseTestError("test_widechar", |
| 880 | "wide string and utf8 string " |
| 881 | "have different length"); |
| 882 | } |
| 883 | if (PyUnicode_Compare(wide, utf8)) { |
| 884 | Py_DECREF(wide); |
| 885 | Py_DECREF(utf8); |
| 886 | if (PyErr_Occurred()) |
| 887 | return NULL; |
| 888 | return raiseTestError("test_widechar", |
| 889 | "wide string and utf8 string " |
| 890 | "are different"); |
| 891 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 892 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 893 | Py_DECREF(wide); |
| 894 | Py_DECREF(utf8); |
| 895 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 899 | test_empty_argparse(PyObject *self) |
| 900 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 901 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 902 | PyObject *tuple, *dict = NULL; |
| 903 | static char *kwlist[] = {NULL}; |
| 904 | int result; |
| 905 | tuple = PyTuple_New(0); |
| 906 | if (!tuple) |
| 907 | return NULL; |
| 908 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 909 | goto done; |
| 910 | dict = PyDict_New(); |
| 911 | if (!dict) |
| 912 | goto done; |
| 913 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 914 | done: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 915 | Py_DECREF(tuple); |
| 916 | Py_XDECREF(dict); |
| 917 | if (result < 0) |
| 918 | return NULL; |
| 919 | else { |
| 920 | Py_RETURN_NONE; |
| 921 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 925 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 926 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 927 | const char *encoding, *errors = NULL; |
| 928 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 929 | &encoding, &errors)) |
| 930 | return NULL; |
| 931 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 934 | static PyObject * |
| 935 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 936 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 937 | const char *encoding, *errors = NULL; |
| 938 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 939 | &encoding, &errors)) |
| 940 | return NULL; |
| 941 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 944 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 945 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 946 | static PyObject * |
| 947 | test_long_numbits(PyObject *self) |
| 948 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 949 | struct triple { |
| 950 | long input; |
| 951 | size_t nbits; |
| 952 | int sign; |
| 953 | } testcases[] = {{0, 0, 0}, |
| 954 | {1L, 1, 1}, |
| 955 | {-1L, 1, -1}, |
| 956 | {2L, 2, 1}, |
| 957 | {-2L, 2, -1}, |
| 958 | {3L, 2, 1}, |
| 959 | {-3L, 2, -1}, |
| 960 | {4L, 3, 1}, |
| 961 | {-4L, 3, -1}, |
| 962 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 963 | {-0x7fffL, 15, -1}, |
| 964 | {0xffffL, 16, 1}, |
| 965 | {-0xffffL, 16, -1}, |
| 966 | {0xfffffffL, 28, 1}, |
| 967 | {-0xfffffffL, 28, -1}}; |
| 968 | int i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 970 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 971 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
| 972 | size_t nbits = _PyLong_NumBits(plong); |
| 973 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 974 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 975 | Py_DECREF(plong); |
| 976 | if (nbits != testcases[i].nbits) |
| 977 | return raiseTestError("test_long_numbits", |
| 978 | "wrong result for _PyLong_NumBits"); |
| 979 | if (sign != testcases[i].sign) |
| 980 | return raiseTestError("test_long_numbits", |
| 981 | "wrong result for _PyLong_Sign"); |
| 982 | } |
| 983 | Py_INCREF(Py_None); |
| 984 | return Py_None; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 987 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 988 | |
| 989 | static PyObject * |
| 990 | test_null_strings(PyObject *self) |
| 991 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 992 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 993 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 994 | Py_XDECREF(o1); |
| 995 | Py_XDECREF(o2); |
| 996 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 999 | static PyObject * |
| 1000 | raise_exception(PyObject *self, PyObject *args) |
| 1001 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1002 | PyObject *exc; |
| 1003 | PyObject *exc_args, *v; |
| 1004 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1005 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1006 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1007 | &exc, &num_args)) |
| 1008 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1009 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1010 | exc_args = PyTuple_New(num_args); |
| 1011 | if (exc_args == NULL) |
| 1012 | return NULL; |
| 1013 | for (i = 0; i < num_args; ++i) { |
| 1014 | v = PyLong_FromLong(i); |
| 1015 | if (v == NULL) { |
| 1016 | Py_DECREF(exc_args); |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | PyTuple_SET_ITEM(exc_args, i, v); |
| 1020 | } |
| 1021 | PyErr_SetObject(exc, exc_args); |
| 1022 | Py_DECREF(exc_args); |
| 1023 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1024 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1025 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1026 | #ifdef WITH_THREAD |
| 1027 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1028 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 1029 | * `thread_done` when it's finished. The driver code has to know when the |
| 1030 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 1031 | * may go away when the driver finishes. The former lack of this explicit |
| 1032 | * synchronization caused rare segfaults, so rare that they were seen only |
| 1033 | * on a Mac buildbot (although they were possible on any box). |
| 1034 | */ |
| 1035 | static PyThread_type_lock thread_done = NULL; |
| 1036 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1037 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1038 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1039 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1040 | PyObject *rc; |
| 1041 | int success; |
| 1042 | PyGILState_STATE s = PyGILState_Ensure(); |
| 1043 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
| 1044 | success = (rc != NULL); |
| 1045 | Py_XDECREF(rc); |
| 1046 | PyGILState_Release(s); |
| 1047 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1050 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 1051 | * should be called only from threads spawned by test_thread_state(). |
| 1052 | */ |
| 1053 | static void |
| 1054 | _make_call_from_thread(void *callable) |
| 1055 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1056 | _make_call(callable); |
| 1057 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1060 | static PyObject * |
| 1061 | test_thread_state(PyObject *self, PyObject *args) |
| 1062 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1063 | PyObject *fn; |
| 1064 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1065 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1066 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 1067 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1068 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1069 | if (!PyCallable_Check(fn)) { |
| 1070 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1071 | fn->ob_type->tp_name); |
| 1072 | return NULL; |
| 1073 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1075 | /* Ensure Python is set up for threading */ |
| 1076 | PyEval_InitThreads(); |
| 1077 | thread_done = PyThread_allocate_lock(); |
| 1078 | if (thread_done == NULL) |
| 1079 | return PyErr_NoMemory(); |
| 1080 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1081 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1082 | /* Start a new thread with our callback. */ |
| 1083 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1084 | /* Make the callback with the thread lock held by this thread */ |
| 1085 | success &= _make_call(fn); |
| 1086 | /* Do it all again, but this time with the thread-lock released */ |
| 1087 | Py_BEGIN_ALLOW_THREADS |
| 1088 | success &= _make_call(fn); |
| 1089 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1090 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1092 | /* And once more with and without a thread |
| 1093 | XXX - should use a lock and work out exactly what we are trying |
| 1094 | to test <wink> |
| 1095 | */ |
| 1096 | Py_BEGIN_ALLOW_THREADS |
| 1097 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1098 | success &= _make_call(fn); |
| 1099 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1100 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1101 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1102 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 1103 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1104 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1105 | PyThread_free_lock(thread_done); |
| 1106 | if (!success) |
| 1107 | return NULL; |
| 1108 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1109 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1110 | |
| 1111 | /* test Py_AddPendingCalls using threads */ |
| 1112 | static int _pending_callback(void *arg) |
| 1113 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1114 | /* we assume the argument is callable object to which we own a reference */ |
| 1115 | PyObject *callable = (PyObject *)arg; |
| 1116 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 1117 | Py_DECREF(callable); |
| 1118 | Py_XDECREF(r); |
| 1119 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /* The following requests n callbacks to _pending_callback. It can be |
| 1123 | * run from any python thread. |
| 1124 | */ |
| 1125 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1126 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1127 | PyObject *callable; |
| 1128 | int r; |
| 1129 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1130 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1131 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1132 | /* create the reference for the callbackwhile we hold the lock */ |
| 1133 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1134 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1135 | Py_BEGIN_ALLOW_THREADS |
| 1136 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1137 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1138 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1139 | if (r<0) { |
| 1140 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1141 | Py_INCREF(Py_False); |
| 1142 | return Py_False; |
| 1143 | } |
| 1144 | Py_INCREF(Py_True); |
| 1145 | return Py_True; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1146 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1147 | #endif |
| 1148 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 1149 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1150 | static PyObject * |
| 1151 | test_string_from_format(PyObject *self, PyObject *args) |
| 1152 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1153 | PyObject *result; |
| 1154 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1155 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1156 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 1157 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 1158 | if (result == NULL) \ |
| 1159 | return NULL; \ |
| 1160 | if (strcmp(_PyUnicode_AsString(result), "1")) { \ |
| 1161 | msg = FORMAT " failed at 1"; \ |
| 1162 | goto Fail; \ |
| 1163 | } \ |
| 1164 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1165 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1166 | CHECK_1_FORMAT("%d", int); |
| 1167 | CHECK_1_FORMAT("%ld", long); |
| 1168 | /* The z width modifier was added in Python 2.5. */ |
| 1169 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1171 | /* The u type code was added in Python 2.5. */ |
| 1172 | CHECK_1_FORMAT("%u", unsigned int); |
| 1173 | CHECK_1_FORMAT("%lu", unsigned long); |
| 1174 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1175 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1176 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1177 | |
| 1178 | Fail: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1179 | Py_XDECREF(result); |
| 1180 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1181 | |
| 1182 | #undef CHECK_1_FORMAT |
| 1183 | } |
| 1184 | |
Benjamin Peterson | bb81c8c | 2010-01-09 21:54:39 +0000 | [diff] [blame] | 1185 | |
| 1186 | static PyObject * |
| 1187 | test_unicode_compare_with_ascii(PyObject *self) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1188 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 1189 | int result; |
| 1190 | if (py_s == NULL) |
| 1191 | return NULL; |
| 1192 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 1193 | Py_DECREF(py_s); |
| 1194 | if (!result) { |
| 1195 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 1196 | "should not compare equal to c string."); |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | Py_RETURN_NONE; |
Benjamin Peterson | bb81c8c | 2010-01-09 21:54:39 +0000 | [diff] [blame] | 1200 | }; |
| 1201 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1202 | /* This is here to provide a docstring for test_descr. */ |
| 1203 | static PyObject * |
| 1204 | test_with_docstring(PyObject *self) |
| 1205 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1206 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1209 | /* Test PyOS_string_to_double. */ |
| 1210 | static PyObject * |
| 1211 | test_string_to_double(PyObject *self) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1212 | double result; |
| 1213 | char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1215 | #define CHECK_STRING(STR, expected) \ |
| 1216 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1217 | if (result == -1.0 && PyErr_Occurred()) \ |
| 1218 | return NULL; \ |
| 1219 | if (result != expected) { \ |
| 1220 | msg = "conversion of " STR " to float failed"; \ |
| 1221 | goto fail; \ |
| 1222 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1223 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1224 | #define CHECK_INVALID(STR) \ |
| 1225 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1226 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 1227 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 1228 | PyErr_Clear(); \ |
| 1229 | else \ |
| 1230 | return NULL; \ |
| 1231 | } \ |
| 1232 | else { \ |
| 1233 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 1234 | goto fail; \ |
| 1235 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1236 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1237 | CHECK_STRING("0.1", 0.1); |
| 1238 | CHECK_STRING("1.234", 1.234); |
| 1239 | CHECK_STRING("-1.35", -1.35); |
| 1240 | CHECK_STRING(".1e01", 1.0); |
| 1241 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1242 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1243 | CHECK_INVALID(" 0.1"); |
| 1244 | CHECK_INVALID("\t\n-3"); |
| 1245 | CHECK_INVALID(".123 "); |
| 1246 | CHECK_INVALID("3\n"); |
| 1247 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1249 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1250 | fail: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1251 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1252 | #undef CHECK_STRING |
| 1253 | #undef CHECK_INVALID |
| 1254 | } |
| 1255 | |
| 1256 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1257 | /* Coverage testing of capsule objects. */ |
| 1258 | |
| 1259 | static const char *capsule_name = "capsule name"; |
| 1260 | static char *capsule_pointer = "capsule pointer"; |
| 1261 | static char *capsule_context = "capsule context"; |
| 1262 | static const char *capsule_error = NULL; |
| 1263 | static int |
| 1264 | capsule_destructor_call_count = 0; |
| 1265 | |
| 1266 | static void |
| 1267 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1268 | capsule_destructor_call_count++; |
| 1269 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 1270 | capsule_error = "context did not match in destructor!"; |
| 1271 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 1272 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 1273 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 1274 | capsule_error = "name did not match in destructor!"; |
| 1275 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 1276 | capsule_error = "pointer did not match in destructor!"; |
| 1277 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | typedef struct { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1281 | char *name; |
| 1282 | char *module; |
| 1283 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1284 | } known_capsule; |
| 1285 | |
| 1286 | static PyObject * |
| 1287 | test_capsule(PyObject *self, PyObject *args) |
| 1288 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1289 | PyObject *object; |
| 1290 | const char *error = NULL; |
| 1291 | void *pointer; |
| 1292 | void *pointer2; |
| 1293 | known_capsule known_capsules[] = { |
| 1294 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 1295 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 1296 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 1297 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 1298 | { NULL, NULL }, |
| 1299 | }; |
| 1300 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1301 | |
| 1302 | #define FAIL(x) { error = (x); goto exit; } |
| 1303 | |
| 1304 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1305 | if (capsule_error) { \ |
| 1306 | FAIL(capsule_error); \ |
| 1307 | } \ |
| 1308 | else if (!capsule_destructor_call_count) { \ |
| 1309 | FAIL("destructor not called!"); \ |
| 1310 | } \ |
| 1311 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1312 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1313 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 1314 | PyCapsule_SetContext(object, capsule_context); |
| 1315 | capsule_destructor(object); |
| 1316 | CHECK_DESTRUCTOR; |
| 1317 | Py_DECREF(object); |
| 1318 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1319 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1320 | object = PyCapsule_New(known, "ignored", NULL); |
| 1321 | PyCapsule_SetPointer(object, capsule_pointer); |
| 1322 | PyCapsule_SetName(object, capsule_name); |
| 1323 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 1324 | PyCapsule_SetContext(object, capsule_context); |
| 1325 | capsule_destructor(object); |
| 1326 | CHECK_DESTRUCTOR; |
| 1327 | /* intentionally access using the wrong name */ |
| 1328 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 1329 | if (!PyErr_Occurred()) { |
| 1330 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1331 | } |
| 1332 | PyErr_Clear(); |
| 1333 | if (pointer2) { |
| 1334 | if (pointer2 == capsule_pointer) { |
| 1335 | FAIL("PyCapsule_GetPointer should not have" |
| 1336 | " returned the internal pointer!"); |
| 1337 | } else { |
| 1338 | FAIL("PyCapsule_GetPointer should have " |
| 1339 | "returned NULL pointer but did not!"); |
| 1340 | } |
| 1341 | } |
| 1342 | PyCapsule_SetDestructor(object, NULL); |
| 1343 | Py_DECREF(object); |
| 1344 | if (capsule_destructor_call_count) { |
| 1345 | FAIL("destructor called when it should not have been!"); |
| 1346 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1348 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 1349 | /* yeah, ordinarily I wouldn't do this either, |
| 1350 | but it's fine for this test harness. |
| 1351 | */ |
| 1352 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1353 | #undef FAIL |
| 1354 | #define FAIL(x) \ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1355 | { \ |
| 1356 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 1357 | x, known->module, known->attribute); \ |
| 1358 | error = buffer; \ |
| 1359 | goto exit; \ |
| 1360 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1361 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1362 | PyObject *module = PyImport_ImportModule(known->module); |
| 1363 | if (module) { |
| 1364 | pointer = PyCapsule_Import(known->name, 0); |
| 1365 | if (!pointer) { |
| 1366 | Py_DECREF(module); |
| 1367 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 1368 | } |
| 1369 | object = PyObject_GetAttrString(module, known->attribute); |
| 1370 | if (!object) { |
| 1371 | Py_DECREF(module); |
| 1372 | return NULL; |
| 1373 | } |
| 1374 | pointer2 = PyCapsule_GetPointer(object, |
| 1375 | "weebles wobble but they don't fall down"); |
| 1376 | if (!PyErr_Occurred()) { |
| 1377 | Py_DECREF(object); |
| 1378 | Py_DECREF(module); |
| 1379 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1380 | } |
| 1381 | PyErr_Clear(); |
| 1382 | if (pointer2) { |
| 1383 | Py_DECREF(module); |
| 1384 | Py_DECREF(object); |
| 1385 | if (pointer2 == pointer) { |
| 1386 | FAIL("PyCapsule_GetPointer should not have" |
| 1387 | " returned its internal pointer!"); |
| 1388 | } else { |
| 1389 | FAIL("PyCapsule_GetPointer should have" |
| 1390 | " returned NULL pointer but did not!"); |
| 1391 | } |
| 1392 | } |
| 1393 | Py_DECREF(object); |
| 1394 | Py_DECREF(module); |
| 1395 | } |
| 1396 | else |
| 1397 | PyErr_Clear(); |
| 1398 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1399 | |
| 1400 | exit: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1401 | if (error) { |
| 1402 | return raiseTestError("test_capsule", error); |
| 1403 | } |
| 1404 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1405 | #undef FAIL |
| 1406 | } |
| 1407 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1408 | #ifdef HAVE_GETTIMEOFDAY |
| 1409 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 1410 | 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] | 1411 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1412 | e->tv_sec -= s->tv_sec; |
| 1413 | e->tv_usec -= s->tv_usec; |
| 1414 | if (e->tv_usec < 0) { |
| 1415 | e->tv_sec -=1; |
| 1416 | e->tv_usec += 1000000; |
| 1417 | } |
| 1418 | 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] | 1419 | } |
| 1420 | |
| 1421 | static PyObject * |
| 1422 | profile_int(PyObject *self, PyObject* args) |
| 1423 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1424 | int i, k; |
| 1425 | struct timeval start, stop; |
| 1426 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1428 | /* Test 1: Allocate and immediately deallocate |
| 1429 | many small integers */ |
| 1430 | gettimeofday(&start, NULL); |
| 1431 | for(k=0; k < 20000; k++) |
| 1432 | for(i=0; i < 1000; i++) { |
| 1433 | single = PyLong_FromLong(i); |
| 1434 | Py_DECREF(single); |
| 1435 | } |
| 1436 | gettimeofday(&stop, NULL); |
| 1437 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1439 | /* Test 2: Allocate and immediately deallocate |
| 1440 | many large integers */ |
| 1441 | gettimeofday(&start, NULL); |
| 1442 | for(k=0; k < 20000; k++) |
| 1443 | for(i=0; i < 1000; i++) { |
| 1444 | single = PyLong_FromLong(i+1000000); |
| 1445 | Py_DECREF(single); |
| 1446 | } |
| 1447 | gettimeofday(&stop, NULL); |
| 1448 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1450 | /* Test 3: Allocate a few integers, then release |
| 1451 | them all simultaneously. */ |
| 1452 | multiple = malloc(sizeof(PyObject*) * 1000); |
| 1453 | gettimeofday(&start, NULL); |
| 1454 | for(k=0; k < 20000; k++) { |
| 1455 | for(i=0; i < 1000; i++) { |
| 1456 | multiple[i] = PyLong_FromLong(i+1000000); |
| 1457 | } |
| 1458 | for(i=0; i < 1000; i++) { |
| 1459 | Py_DECREF(multiple[i]); |
| 1460 | } |
| 1461 | } |
| 1462 | gettimeofday(&stop, NULL); |
| 1463 | print_delta(3, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1464 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1465 | /* Test 4: Allocate many integers, then release |
| 1466 | them all simultaneously. */ |
| 1467 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 1468 | gettimeofday(&start, NULL); |
| 1469 | for(k=0; k < 20; k++) { |
| 1470 | for(i=0; i < 1000000; i++) { |
| 1471 | multiple[i] = PyLong_FromLong(i+1000000); |
| 1472 | } |
| 1473 | for(i=0; i < 1000000; i++) { |
| 1474 | Py_DECREF(multiple[i]); |
| 1475 | } |
| 1476 | } |
| 1477 | gettimeofday(&stop, NULL); |
| 1478 | print_delta(4, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1479 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1480 | /* Test 5: Allocate many integers < 32000 */ |
| 1481 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 1482 | gettimeofday(&start, NULL); |
| 1483 | for(k=0; k < 10; k++) { |
| 1484 | for(i=0; i < 1000000; i++) { |
| 1485 | multiple[i] = PyLong_FromLong(i+1000); |
| 1486 | } |
| 1487 | for(i=0; i < 1000000; i++) { |
| 1488 | Py_DECREF(multiple[i]); |
| 1489 | } |
| 1490 | } |
| 1491 | gettimeofday(&stop, NULL); |
| 1492 | print_delta(5, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1494 | /* Test 6: Perform small int addition */ |
| 1495 | op1 = PyLong_FromLong(1); |
| 1496 | gettimeofday(&start, NULL); |
| 1497 | for(i=0; i < 10000000; i++) { |
| 1498 | result = PyNumber_Add(op1, op1); |
| 1499 | Py_DECREF(result); |
| 1500 | } |
| 1501 | gettimeofday(&stop, NULL); |
| 1502 | Py_DECREF(op1); |
| 1503 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1505 | /* Test 7: Perform medium int addition */ |
| 1506 | op1 = PyLong_FromLong(1000); |
| 1507 | gettimeofday(&start, NULL); |
| 1508 | for(i=0; i < 10000000; i++) { |
| 1509 | result = PyNumber_Add(op1, op1); |
| 1510 | Py_DECREF(result); |
| 1511 | } |
| 1512 | gettimeofday(&stop, NULL); |
| 1513 | Py_DECREF(op1); |
| 1514 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1515 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1516 | Py_INCREF(Py_None); |
| 1517 | return Py_None; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1518 | } |
| 1519 | #endif |
| 1520 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1521 | /* To test the format of tracebacks as printed out. */ |
| 1522 | static PyObject * |
| 1523 | traceback_print(PyObject *self, PyObject *args) |
| 1524 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1525 | PyObject *file; |
| 1526 | PyObject *traceback; |
| 1527 | int result; |
| 1528 | |
| 1529 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 1530 | &traceback, &file)) |
| 1531 | return NULL; |
| 1532 | |
| 1533 | result = PyTraceBack_Print(traceback, file); |
| 1534 | if (result < 0) |
| 1535 | return NULL; |
| 1536 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1539 | /* To test the format of exceptions as printed out. */ |
| 1540 | static PyObject * |
| 1541 | exception_print(PyObject *self, PyObject *args) |
| 1542 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1543 | PyObject *value; |
| 1544 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1545 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1546 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 1547 | &value)) |
| 1548 | return NULL; |
| 1549 | if (!PyExceptionInstance_Check(value)) { |
| 1550 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 1551 | return NULL; |
| 1552 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1553 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1554 | tb = PyException_GetTraceback(value); |
| 1555 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 1556 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1557 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1558 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 1563 | |
| 1564 | /* reliably raise a MemoryError */ |
| 1565 | static PyObject * |
| 1566 | raise_memoryerror(PyObject *self) |
| 1567 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1568 | PyErr_NoMemory(); |
| 1569 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1572 | /* Issue 6012 */ |
| 1573 | static PyObject *str1, *str2; |
| 1574 | static int |
| 1575 | failing_converter(PyObject *obj, void *arg) |
| 1576 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1577 | /* Clone str1, then let the conversion fail. */ |
| 1578 | assert(str1); |
| 1579 | str2 = str1; |
| 1580 | Py_INCREF(str2); |
| 1581 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1582 | } |
| 1583 | static PyObject* |
| 1584 | argparsing(PyObject *o, PyObject *args) |
| 1585 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1586 | PyObject *res; |
| 1587 | str1 = str2 = NULL; |
| 1588 | if (!PyArg_ParseTuple(args, "O&O&", |
| 1589 | PyUnicode_FSConverter, &str1, |
| 1590 | failing_converter, &str2)) { |
| 1591 | if (!str2) |
| 1592 | /* argument converter not called? */ |
| 1593 | return NULL; |
| 1594 | /* Should be 1 */ |
| 1595 | res = PyLong_FromLong(Py_REFCNT(str2)); |
| 1596 | Py_DECREF(str2); |
| 1597 | PyErr_Clear(); |
| 1598 | return res; |
| 1599 | } |
| 1600 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 1601 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 1602 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1603 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1604 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 1605 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
| 1606 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 1607 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 1608 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 1609 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
| 1610 | {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, |
| 1611 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
| 1612 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 1613 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 1614 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
| 1615 | {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS}, |
| 1616 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 1617 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 1618 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 1619 | PyDoc_STR("This is a pretty normal docstring.")}, |
| 1620 | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
| 1621 | {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, |
| 1622 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
| 1623 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 1624 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 1625 | METH_VARARGS|METH_KEYWORDS}, |
| 1626 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 1627 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 1628 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 1629 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 1630 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 1631 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 1632 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 1633 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1634 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1635 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 1636 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 1637 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 1638 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
| 1639 | {"codec_incrementalencoder", |
| 1640 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
| 1641 | {"codec_incrementaldecoder", |
| 1642 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1643 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1644 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
| 1645 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 1646 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
| 1647 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1648 | #ifdef WITH_THREAD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1649 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
| 1650 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1651 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1652 | #ifdef HAVE_GETTIMEOFDAY |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1653 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1654 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1655 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 1656 | {"exception_print", exception_print, METH_VARARGS}, |
| 1657 | {"argparsing", argparsing, METH_VARARGS}, |
| 1658 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1659 | }; |
| 1660 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1661 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 1662 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1663 | typedef struct { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1664 | char bool_member; |
| 1665 | char byte_member; |
| 1666 | unsigned char ubyte_member; |
| 1667 | short short_member; |
| 1668 | unsigned short ushort_member; |
| 1669 | int int_member; |
| 1670 | unsigned int uint_member; |
| 1671 | long long_member; |
| 1672 | unsigned long ulong_member; |
| 1673 | Py_ssize_t pyssizet_member; |
| 1674 | float float_member; |
| 1675 | double double_member; |
| 1676 | char inplace_member[6]; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1677 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1678 | PY_LONG_LONG longlong_member; |
| 1679 | unsigned PY_LONG_LONG ulonglong_member; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1680 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1681 | } all_structmembers; |
| 1682 | |
| 1683 | typedef struct { |
| 1684 | PyObject_HEAD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1685 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1686 | } test_structmembers; |
| 1687 | |
| 1688 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1689 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 1690 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 1691 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 1692 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 1693 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 1694 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 1695 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 1696 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 1697 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 1698 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 1699 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 1700 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 1701 | {"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] | 1702 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1703 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 1704 | {"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] | 1705 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1706 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1707 | }; |
| 1708 | |
| 1709 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1710 | static PyObject * |
| 1711 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 1712 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1713 | static char *keywords[] = { |
| 1714 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 1715 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 1716 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Mark Dickinson | bee1fb0 | 2010-04-06 15:44:57 +0000 | [diff] [blame] | 1717 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1718 | "T_LONGLONG", "T_ULONGLONG", |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1719 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1720 | NULL}; |
| 1721 | static char *fmt = "|bbBhHiIlknfds#" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1722 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1723 | "LK" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1724 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1725 | ; |
| 1726 | test_structmembers *ob; |
| 1727 | const char *s = NULL; |
| 1728 | Py_ssize_t string_len = 0; |
| 1729 | ob = PyObject_New(test_structmembers, type); |
| 1730 | if (ob == NULL) |
| 1731 | return NULL; |
| 1732 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 1733 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 1734 | &ob->structmembers.bool_member, |
| 1735 | &ob->structmembers.byte_member, |
| 1736 | &ob->structmembers.ubyte_member, |
| 1737 | &ob->structmembers.short_member, |
| 1738 | &ob->structmembers.ushort_member, |
| 1739 | &ob->structmembers.int_member, |
| 1740 | &ob->structmembers.uint_member, |
| 1741 | &ob->structmembers.long_member, |
| 1742 | &ob->structmembers.ulong_member, |
| 1743 | &ob->structmembers.pyssizet_member, |
| 1744 | &ob->structmembers.float_member, |
| 1745 | &ob->structmembers.double_member, |
| 1746 | &s, &string_len |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1747 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1748 | , &ob->structmembers.longlong_member, |
| 1749 | &ob->structmembers.ulonglong_member |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1750 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1751 | )) { |
| 1752 | Py_DECREF(ob); |
| 1753 | return NULL; |
| 1754 | } |
| 1755 | if (s != NULL) { |
| 1756 | if (string_len > 5) { |
| 1757 | Py_DECREF(ob); |
| 1758 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 1759 | return NULL; |
| 1760 | } |
| 1761 | strcpy(ob->structmembers.inplace_member, s); |
| 1762 | } |
| 1763 | else { |
| 1764 | strcpy(ob->structmembers.inplace_member, ""); |
| 1765 | } |
| 1766 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 1769 | static void |
| 1770 | test_structmembers_free(PyObject *ob) |
| 1771 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1772 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1776 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1777 | "test_structmembersType", |
| 1778 | sizeof(test_structmembers), /* tp_basicsize */ |
| 1779 | 0, /* tp_itemsize */ |
| 1780 | test_structmembers_free, /* destructor tp_dealloc */ |
| 1781 | 0, /* tp_print */ |
| 1782 | 0, /* tp_getattr */ |
| 1783 | 0, /* tp_setattr */ |
| 1784 | 0, /* tp_reserved */ |
| 1785 | 0, /* tp_repr */ |
| 1786 | 0, /* tp_as_number */ |
| 1787 | 0, /* tp_as_sequence */ |
| 1788 | 0, /* tp_as_mapping */ |
| 1789 | 0, /* tp_hash */ |
| 1790 | 0, /* tp_call */ |
| 1791 | 0, /* tp_str */ |
| 1792 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1793 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 1794 | 0, /* tp_as_buffer */ |
| 1795 | 0, /* tp_flags */ |
| 1796 | "Type containing all structmember types", |
| 1797 | 0, /* traverseproc tp_traverse */ |
| 1798 | 0, /* tp_clear */ |
| 1799 | 0, /* tp_richcompare */ |
| 1800 | 0, /* tp_weaklistoffset */ |
| 1801 | 0, /* tp_iter */ |
| 1802 | 0, /* tp_iternext */ |
| 1803 | 0, /* tp_methods */ |
| 1804 | test_members, /* tp_members */ |
| 1805 | 0, |
| 1806 | 0, |
| 1807 | 0, |
| 1808 | 0, |
| 1809 | 0, |
| 1810 | 0, |
| 1811 | 0, |
| 1812 | 0, |
| 1813 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1814 | }; |
| 1815 | |
| 1816 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1817 | |
| 1818 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1819 | PyModuleDef_HEAD_INIT, |
| 1820 | "_testcapi", |
| 1821 | NULL, |
| 1822 | -1, |
| 1823 | TestMethods, |
| 1824 | NULL, |
| 1825 | NULL, |
| 1826 | NULL, |
| 1827 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1828 | }; |
| 1829 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1830 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1831 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1832 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1833 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1834 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1835 | m = PyModule_Create(&_testcapimodule); |
| 1836 | if (m == NULL) |
| 1837 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1838 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1839 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
| 1840 | Py_TYPE(&_MemoryViewTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 1841 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1842 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 1843 | Py_INCREF(&test_structmembersType); |
| 1844 | /* don't use a name starting with "test", since we don't want |
| 1845 | test_capi to automatically call this */ |
| 1846 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1847 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1848 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 1849 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 1850 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 1851 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 1852 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 1853 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 1854 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 1855 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 1856 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 1857 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 1858 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 1859 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 1860 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 1861 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 1862 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 1863 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 1864 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 1865 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 1866 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 1867 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 1868 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
| 1869 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
| 1870 | Py_INCREF(&PyInstanceMethod_Type); |
| 1871 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1872 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 1873 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 1874 | Py_INCREF(TestError); |
| 1875 | PyModule_AddObject(m, "error", TestError); |
| 1876 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1877 | } |