Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * C Extension module to test Python interpreter C APIs. |
| 3 | * |
| 4 | * The 'test_*' functions exported by this module are run as part of the |
| 5 | * standard Python regression test, via Lib/test/test_capi.py. |
| 6 | */ |
| 7 | |
Neal Norwitz | 8866e0a | 2007-10-27 04:01:17 +0000 | [diff] [blame] | 8 | #define PY_SSIZE_T_CLEAN |
| 9 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | #include <float.h> |
| 12 | #include "structmember.h" |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 13 | #include "datetime.h" |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 14 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 15 | #ifdef WITH_THREAD |
| 16 | #include "pythread.h" |
| 17 | #endif /* WITH_THREAD */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | static PyObject *TestError; /* set to exception object in init */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 19 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 20 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
| 21 | |
| 22 | static PyObject * |
| 23 | raiseTestError(const char* test_name, const char* msg) |
| 24 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | char buf[2048]; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 26 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50) |
| 28 | PyErr_SetString(TestError, "internal error msg too large"); |
| 29 | else { |
| 30 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg); |
| 31 | PyErr_SetString(TestError, buf); |
| 32 | } |
| 33 | return NULL; |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 36 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 37 | |
| 38 | The ones derived from autoconf on the UNIX-like OSes can be relied |
| 39 | upon (in the absence of sloppy cross-compiling), but the Windows |
| 40 | platforms have these hardcoded. Better safe than sorry. |
| 41 | */ |
| 42 | static PyObject* |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 43 | sizeof_error(const char* fatname, const char* typname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | int expected, int got) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 45 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | char buf[1024]; |
| 47 | PyOS_snprintf(buf, sizeof(buf), |
| 48 | "%.200s #define == %d but sizeof(%.200s) == %d", |
| 49 | fatname, expected, typname, got); |
| 50 | PyErr_SetString(TestError, buf); |
| 51 | return (PyObject*)NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 55 | test_config(PyObject *self) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 56 | { |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 57 | #define CHECK_SIZEOF(FATNAME, TYPE) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | if (FATNAME != sizeof(TYPE)) \ |
| 59 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | CHECK_SIZEOF(SIZEOF_SHORT, short); |
| 62 | CHECK_SIZEOF(SIZEOF_INT, int); |
| 63 | CHECK_SIZEOF(SIZEOF_LONG, long); |
| 64 | CHECK_SIZEOF(SIZEOF_VOID_P, void*); |
| 65 | CHECK_SIZEOF(SIZEOF_TIME_T, time_t); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 66 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG); |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | #undef CHECK_SIZEOF |
| 71 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | Py_INCREF(Py_None); |
| 73 | return Py_None; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 76 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 77 | test_list_api(PyObject *self) |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 78 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | PyObject* list; |
| 80 | int i; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 81 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | /* SF bug 132008: PyList_Reverse segfaults */ |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 83 | #define NLIST 30 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | list = PyList_New(NLIST); |
| 85 | if (list == (PyObject*)NULL) |
| 86 | return (PyObject*)NULL; |
| 87 | /* list = range(NLIST) */ |
| 88 | for (i = 0; i < NLIST; ++i) { |
| 89 | PyObject* anint = PyLong_FromLong(i); |
| 90 | if (anint == (PyObject*)NULL) { |
| 91 | Py_DECREF(list); |
| 92 | return (PyObject*)NULL; |
| 93 | } |
| 94 | PyList_SET_ITEM(list, i, anint); |
| 95 | } |
| 96 | /* list.reverse(), via PyList_Reverse() */ |
| 97 | i = PyList_Reverse(list); /* should not blow up! */ |
| 98 | if (i != 0) { |
| 99 | Py_DECREF(list); |
| 100 | return (PyObject*)NULL; |
| 101 | } |
| 102 | /* Check that list == range(29, -1, -1) now */ |
| 103 | for (i = 0; i < NLIST; ++i) { |
| 104 | PyObject* anint = PyList_GET_ITEM(list, i); |
| 105 | if (PyLong_AS_LONG(anint) != NLIST-1-i) { |
| 106 | PyErr_SetString(TestError, |
| 107 | "test_list_api: reverse screwed up"); |
| 108 | Py_DECREF(list); |
| 109 | return (PyObject*)NULL; |
| 110 | } |
| 111 | } |
| 112 | Py_DECREF(list); |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 113 | #undef NLIST |
| 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | Py_INCREF(Py_None); |
| 116 | return Py_None; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 119 | static int |
| 120 | test_dict_inner(int count) |
| 121 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | Py_ssize_t pos = 0, iterations = 0; |
| 123 | int i; |
| 124 | PyObject *dict = PyDict_New(); |
| 125 | PyObject *v, *k; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | if (dict == NULL) |
| 128 | return -1; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | for (i = 0; i < count; i++) { |
| 131 | v = PyLong_FromLong(i); |
| 132 | PyDict_SetItem(dict, v, v); |
| 133 | Py_DECREF(v); |
| 134 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 137 | PyObject *o; |
| 138 | iterations++; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | i = PyLong_AS_LONG(v) + 1; |
| 141 | o = PyLong_FromLong(i); |
| 142 | if (o == NULL) |
| 143 | return -1; |
| 144 | if (PyDict_SetItem(dict, k, o) < 0) { |
| 145 | Py_DECREF(o); |
| 146 | return -1; |
| 147 | } |
| 148 | Py_DECREF(o); |
| 149 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | Py_DECREF(dict); |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | if (iterations != count) { |
| 154 | PyErr_SetString( |
| 155 | TestError, |
| 156 | "test_dict_iteration: dict iteration went wrong "); |
| 157 | return -1; |
| 158 | } else { |
| 159 | return 0; |
| 160 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static PyObject* |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 164 | test_dict_iteration(PyObject* self) |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | int i; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | for (i = 0; i < 200; i++) { |
| 169 | if (test_dict_inner(i) < 0) { |
| 170 | return NULL; |
| 171 | } |
| 172 | } |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | Py_INCREF(Py_None); |
| 175 | return Py_None; |
Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 178 | |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 179 | /* Issue #4701: Check that PyObject_Hash implicitly calls |
| 180 | * PyType_Ready if it hasn't already been called |
| 181 | */ |
| 182 | static PyTypeObject _HashInheritanceTester_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | PyVarObject_HEAD_INIT(NULL, 0) |
| 184 | "hashinheritancetester", /* Name of this type */ |
| 185 | sizeof(PyObject), /* Basic object size */ |
| 186 | 0, /* Item size for varobject */ |
| 187 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 188 | 0, /* tp_print */ |
| 189 | 0, /* tp_getattr */ |
| 190 | 0, /* tp_setattr */ |
| 191 | 0, /* tp_reserved */ |
| 192 | 0, /* tp_repr */ |
| 193 | 0, /* tp_as_number */ |
| 194 | 0, /* tp_as_sequence */ |
| 195 | 0, /* tp_as_mapping */ |
| 196 | 0, /* tp_hash */ |
| 197 | 0, /* tp_call */ |
| 198 | 0, /* tp_str */ |
| 199 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 200 | 0, /* tp_setattro */ |
| 201 | 0, /* tp_as_buffer */ |
| 202 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 203 | 0, /* tp_doc */ |
| 204 | 0, /* tp_traverse */ |
| 205 | 0, /* tp_clear */ |
| 206 | 0, /* tp_richcompare */ |
| 207 | 0, /* tp_weaklistoffset */ |
| 208 | 0, /* tp_iter */ |
| 209 | 0, /* tp_iternext */ |
| 210 | 0, /* tp_methods */ |
| 211 | 0, /* tp_members */ |
| 212 | 0, /* tp_getset */ |
| 213 | 0, /* tp_base */ |
| 214 | 0, /* tp_dict */ |
| 215 | 0, /* tp_descr_get */ |
| 216 | 0, /* tp_descr_set */ |
| 217 | 0, /* tp_dictoffset */ |
| 218 | 0, /* tp_init */ |
| 219 | 0, /* tp_alloc */ |
| 220 | PyType_GenericNew, /* tp_new */ |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | static PyObject* |
| 224 | test_lazy_hash_inheritance(PyObject* self) |
| 225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | PyTypeObject *type; |
| 227 | PyObject *obj; |
Antoine Pitrou | 29aad00 | 2010-10-23 19:42:38 +0000 | [diff] [blame] | 228 | Py_hash_t hash; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | type = &_HashInheritanceTester_Type; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | if (type->tp_dict != NULL) |
| 233 | /* The type has already been initialized. This probably means |
| 234 | -R is being used. */ |
| 235 | Py_RETURN_NONE; |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 236 | |
| 237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | obj = PyObject_New(PyObject, type); |
| 239 | if (obj == NULL) { |
| 240 | PyErr_Clear(); |
| 241 | PyErr_SetString( |
| 242 | TestError, |
| 243 | "test_lazy_hash_inheritance: failed to create object"); |
| 244 | return NULL; |
| 245 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | if (type->tp_dict != NULL) { |
| 248 | PyErr_SetString( |
| 249 | TestError, |
| 250 | "test_lazy_hash_inheritance: type initialised too soon"); |
| 251 | Py_DECREF(obj); |
| 252 | return NULL; |
| 253 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | hash = PyObject_Hash(obj); |
| 256 | if ((hash == -1) && PyErr_Occurred()) { |
| 257 | PyErr_Clear(); |
| 258 | PyErr_SetString( |
| 259 | TestError, |
| 260 | "test_lazy_hash_inheritance: could not hash object"); |
| 261 | Py_DECREF(obj); |
| 262 | return NULL; |
| 263 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | if (type->tp_dict == NULL) { |
| 266 | PyErr_SetString( |
| 267 | TestError, |
| 268 | "test_lazy_hash_inheritance: type not initialised by hash()"); |
| 269 | Py_DECREF(obj); |
| 270 | return NULL; |
| 271 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | if (type->tp_hash != PyType_Type.tp_hash) { |
| 274 | PyErr_SetString( |
| 275 | TestError, |
| 276 | "test_lazy_hash_inheritance: unexpected hash function"); |
| 277 | Py_DECREF(obj); |
| 278 | return NULL; |
| 279 | } |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | Py_DECREF(obj); |
Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | Py_RETURN_NONE; |
Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 287 | /* Issue #7385: Check that memoryview() does not crash |
| 288 | * when bf_getbuffer returns an error |
| 289 | */ |
| 290 | |
| 291 | static int |
| 292 | broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags) |
| 293 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | PyErr_SetString( |
| 295 | TestError, |
| 296 | "test_broken_memoryview: expected error in bf_getbuffer"); |
| 297 | return -1; |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static PyBufferProcs memoryviewtester_as_buffer = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */ |
| 302 | 0, /* bf_releasebuffer */ |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | static PyTypeObject _MemoryViewTester_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | PyVarObject_HEAD_INIT(NULL, 0) |
| 307 | "memoryviewtester", /* Name of this type */ |
| 308 | sizeof(PyObject), /* Basic object size */ |
| 309 | 0, /* Item size for varobject */ |
| 310 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 311 | 0, /* tp_print */ |
| 312 | 0, /* tp_getattr */ |
| 313 | 0, /* tp_setattr */ |
| 314 | 0, /* tp_compare */ |
| 315 | 0, /* tp_repr */ |
| 316 | 0, /* tp_as_number */ |
| 317 | 0, /* tp_as_sequence */ |
| 318 | 0, /* tp_as_mapping */ |
| 319 | 0, /* tp_hash */ |
| 320 | 0, /* tp_call */ |
| 321 | 0, /* tp_str */ |
| 322 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 323 | 0, /* tp_setattro */ |
| 324 | &memoryviewtester_as_buffer, /* tp_as_buffer */ |
| 325 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 326 | 0, /* tp_doc */ |
| 327 | 0, /* tp_traverse */ |
| 328 | 0, /* tp_clear */ |
| 329 | 0, /* tp_richcompare */ |
| 330 | 0, /* tp_weaklistoffset */ |
| 331 | 0, /* tp_iter */ |
| 332 | 0, /* tp_iternext */ |
| 333 | 0, /* tp_methods */ |
| 334 | 0, /* tp_members */ |
| 335 | 0, /* tp_getset */ |
| 336 | 0, /* tp_base */ |
| 337 | 0, /* tp_dict */ |
| 338 | 0, /* tp_descr_get */ |
| 339 | 0, /* tp_descr_set */ |
| 340 | 0, /* tp_dictoffset */ |
| 341 | 0, /* tp_init */ |
| 342 | 0, /* tp_alloc */ |
| 343 | PyType_GenericNew, /* tp_new */ |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | static PyObject* |
| 347 | test_broken_memoryview(PyObject* self) |
| 348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type); |
| 350 | PyObject *res; |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (obj == NULL) { |
| 353 | PyErr_Clear(); |
| 354 | PyErr_SetString( |
| 355 | TestError, |
| 356 | "test_broken_memoryview: failed to create object"); |
| 357 | return NULL; |
| 358 | } |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | res = PyMemoryView_FromObject(obj); |
| 361 | if (res || !PyErr_Occurred()){ |
| 362 | PyErr_SetString( |
| 363 | TestError, |
| 364 | "test_broken_memoryview: memoryview() didn't raise an Exception"); |
| 365 | Py_XDECREF(res); |
| 366 | Py_DECREF(obj); |
| 367 | return NULL; |
| 368 | } |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | PyErr_Clear(); |
| 371 | Py_DECREF(obj); |
| 372 | Py_RETURN_NONE; |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 376 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG) |
Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 377 | PyLong_{As, From}{Unsigned,}LongLong(). |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 378 | |
| 379 | Note that the meat of the test is contained in testcapi_long.h. |
| 380 | This is revolting, but delicate code duplication is worse: "almost |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 381 | 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] | 382 | dependence on type names makes it impossible to use a parameterized |
| 383 | function. A giant macro would be even worse than this. A C++ template |
| 384 | would be perfect. |
| 385 | |
| 386 | The "report an error" functions are deliberately not part of the #include |
| 387 | file: if the test fails, you can set a breakpoint in the appropriate |
| 388 | error function directly, and crawl back from there in the debugger. |
| 389 | */ |
| 390 | |
| 391 | #define UNBIND(X) Py_DECREF(X); (X) = NULL |
| 392 | |
| 393 | static PyObject * |
| 394 | raise_test_long_error(const char* msg) |
| 395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | return raiseTestError("test_long_api", msg); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | #define TESTNAME test_long_api_inner |
| 400 | #define TYPENAME long |
| 401 | #define F_S_TO_PY PyLong_FromLong |
| 402 | #define F_PY_TO_S PyLong_AsLong |
| 403 | #define F_U_TO_PY PyLong_FromUnsignedLong |
| 404 | #define F_PY_TO_U PyLong_AsUnsignedLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 405 | |
| 406 | #include "testcapi_long.h" |
| 407 | |
| 408 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 409 | test_long_api(PyObject* self) |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | return TESTNAME(raise_test_long_error); |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | #undef TESTNAME |
| 415 | #undef TYPENAME |
| 416 | #undef F_S_TO_PY |
| 417 | #undef F_PY_TO_S |
| 418 | #undef F_U_TO_PY |
| 419 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 420 | |
| 421 | #ifdef HAVE_LONG_LONG |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 422 | |
| 423 | static PyObject * |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 424 | raise_test_longlong_error(const char* msg) |
| 425 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | return raiseTestError("test_longlong_api", msg); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | #define TESTNAME test_longlong_api_inner |
| 430 | #define TYPENAME PY_LONG_LONG |
| 431 | #define F_S_TO_PY PyLong_FromLongLong |
| 432 | #define F_PY_TO_S PyLong_AsLongLong |
| 433 | #define F_U_TO_PY PyLong_FromUnsignedLongLong |
| 434 | #define F_PY_TO_U PyLong_AsUnsignedLongLong |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 435 | |
| 436 | #include "testcapi_long.h" |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 437 | |
| 438 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 439 | test_longlong_api(PyObject* self, PyObject *args) |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | return TESTNAME(raise_test_longlong_error); |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 444 | #undef TESTNAME |
| 445 | #undef TYPENAME |
| 446 | #undef F_S_TO_PY |
| 447 | #undef F_PY_TO_S |
| 448 | #undef F_U_TO_PY |
| 449 | #undef F_PY_TO_U |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 450 | |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 451 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG |
| 452 | is tested by test_long_api_inner. This test will concentrate on proper |
| 453 | handling of overflow. |
| 454 | */ |
| 455 | |
| 456 | static PyObject * |
| 457 | test_long_and_overflow(PyObject *self) |
| 458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | PyObject *num, *one, *temp; |
| 460 | long value; |
| 461 | int overflow; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | /* Test that overflow is set properly for a large value. */ |
| 464 | /* num is a number larger than LONG_MAX even on 64-bit platforms */ |
| 465 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 466 | if (num == NULL) |
| 467 | return NULL; |
| 468 | overflow = 1234; |
| 469 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 470 | Py_DECREF(num); |
| 471 | if (value == -1 && PyErr_Occurred()) |
| 472 | return NULL; |
| 473 | if (value != -1) |
| 474 | return raiseTestError("test_long_and_overflow", |
| 475 | "return value was not set to -1"); |
| 476 | if (overflow != 1) |
| 477 | return raiseTestError("test_long_and_overflow", |
| 478 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | /* Same again, with num = LONG_MAX + 1 */ |
| 481 | num = PyLong_FromLong(LONG_MAX); |
| 482 | if (num == NULL) |
| 483 | return NULL; |
| 484 | one = PyLong_FromLong(1L); |
| 485 | if (one == NULL) { |
| 486 | Py_DECREF(num); |
| 487 | return NULL; |
| 488 | } |
| 489 | temp = PyNumber_Add(num, one); |
| 490 | Py_DECREF(one); |
| 491 | Py_DECREF(num); |
| 492 | num = temp; |
| 493 | if (num == NULL) |
| 494 | return NULL; |
| 495 | overflow = 0; |
| 496 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 497 | Py_DECREF(num); |
| 498 | if (value == -1 && PyErr_Occurred()) |
| 499 | return NULL; |
| 500 | if (value != -1) |
| 501 | return raiseTestError("test_long_and_overflow", |
| 502 | "return value was not set to -1"); |
| 503 | if (overflow != 1) |
| 504 | return raiseTestError("test_long_and_overflow", |
| 505 | "overflow was not set to 1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | /* Test that overflow is set properly for a large negative value. */ |
| 508 | /* num is a number smaller than LONG_MIN even on 64-bit platforms */ |
| 509 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 510 | if (num == NULL) |
| 511 | return NULL; |
| 512 | overflow = 1234; |
| 513 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 514 | Py_DECREF(num); |
| 515 | if (value == -1 && PyErr_Occurred()) |
| 516 | return NULL; |
| 517 | if (value != -1) |
| 518 | return raiseTestError("test_long_and_overflow", |
| 519 | "return value was not set to -1"); |
| 520 | if (overflow != -1) |
| 521 | return raiseTestError("test_long_and_overflow", |
| 522 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | /* Same again, with num = LONG_MIN - 1 */ |
| 525 | num = PyLong_FromLong(LONG_MIN); |
| 526 | if (num == NULL) |
| 527 | return NULL; |
| 528 | one = PyLong_FromLong(1L); |
| 529 | if (one == NULL) { |
| 530 | Py_DECREF(num); |
| 531 | return NULL; |
| 532 | } |
| 533 | temp = PyNumber_Subtract(num, one); |
| 534 | Py_DECREF(one); |
| 535 | Py_DECREF(num); |
| 536 | num = temp; |
| 537 | if (num == NULL) |
| 538 | return NULL; |
| 539 | overflow = 0; |
| 540 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 541 | Py_DECREF(num); |
| 542 | if (value == -1 && PyErr_Occurred()) |
| 543 | return NULL; |
| 544 | if (value != -1) |
| 545 | return raiseTestError("test_long_and_overflow", |
| 546 | "return value was not set to -1"); |
| 547 | if (overflow != -1) |
| 548 | return raiseTestError("test_long_and_overflow", |
| 549 | "overflow was not set to -1"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | /* Test that overflow is cleared properly for small values. */ |
| 552 | num = PyLong_FromString("FF", NULL, 16); |
| 553 | if (num == NULL) |
| 554 | return NULL; |
| 555 | overflow = 1234; |
| 556 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 557 | Py_DECREF(num); |
| 558 | if (value == -1 && PyErr_Occurred()) |
| 559 | return NULL; |
| 560 | if (value != 0xFF) |
| 561 | return raiseTestError("test_long_and_overflow", |
| 562 | "expected return value 0xFF"); |
| 563 | if (overflow != 0) |
| 564 | return raiseTestError("test_long_and_overflow", |
| 565 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | num = PyLong_FromString("-FF", NULL, 16); |
| 568 | if (num == NULL) |
| 569 | return NULL; |
| 570 | overflow = 0; |
| 571 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 572 | Py_DECREF(num); |
| 573 | if (value == -1 && PyErr_Occurred()) |
| 574 | return NULL; |
| 575 | if (value != -0xFF) |
| 576 | return raiseTestError("test_long_and_overflow", |
| 577 | "expected return value 0xFF"); |
| 578 | if (overflow != 0) |
| 579 | return raiseTestError("test_long_and_overflow", |
| 580 | "overflow was set incorrectly"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | num = PyLong_FromLong(LONG_MAX); |
| 583 | if (num == NULL) |
| 584 | return NULL; |
| 585 | overflow = 1234; |
| 586 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 587 | Py_DECREF(num); |
| 588 | if (value == -1 && PyErr_Occurred()) |
| 589 | return NULL; |
| 590 | if (value != LONG_MAX) |
| 591 | return raiseTestError("test_long_and_overflow", |
| 592 | "expected return value LONG_MAX"); |
| 593 | if (overflow != 0) |
| 594 | return raiseTestError("test_long_and_overflow", |
| 595 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | num = PyLong_FromLong(LONG_MIN); |
| 598 | if (num == NULL) |
| 599 | return NULL; |
| 600 | overflow = 0; |
| 601 | value = PyLong_AsLongAndOverflow(num, &overflow); |
| 602 | Py_DECREF(num); |
| 603 | if (value == -1 && PyErr_Occurred()) |
| 604 | return NULL; |
| 605 | if (value != LONG_MIN) |
| 606 | return raiseTestError("test_long_and_overflow", |
| 607 | "expected return value LONG_MIN"); |
| 608 | if (overflow != 0) |
| 609 | return raiseTestError("test_long_and_overflow", |
| 610 | "overflow was not cleared"); |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | Py_INCREF(Py_None); |
| 613 | return Py_None; |
Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 616 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to |
| 617 | PY_LONG_LONG is tested by test_long_api_inner. This test will |
| 618 | concentrate on proper handling of overflow. |
| 619 | */ |
| 620 | |
| 621 | static PyObject * |
| 622 | test_long_long_and_overflow(PyObject *self) |
| 623 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | PyObject *num, *one, *temp; |
| 625 | PY_LONG_LONG value; |
| 626 | int overflow; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | /* Test that overflow is set properly for a large value. */ |
| 629 | /* num is a number larger than PY_LLONG_MAX on a typical machine. */ |
| 630 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 631 | if (num == NULL) |
| 632 | return NULL; |
| 633 | overflow = 1234; |
| 634 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 635 | Py_DECREF(num); |
| 636 | if (value == -1 && PyErr_Occurred()) |
| 637 | return NULL; |
| 638 | if (value != -1) |
| 639 | return raiseTestError("test_long_long_and_overflow", |
| 640 | "return value was not set to -1"); |
| 641 | if (overflow != 1) |
| 642 | return raiseTestError("test_long_long_and_overflow", |
| 643 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | /* Same again, with num = PY_LLONG_MAX + 1 */ |
| 646 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 647 | if (num == NULL) |
| 648 | return NULL; |
| 649 | one = PyLong_FromLong(1L); |
| 650 | if (one == NULL) { |
| 651 | Py_DECREF(num); |
| 652 | return NULL; |
| 653 | } |
| 654 | temp = PyNumber_Add(num, one); |
| 655 | Py_DECREF(one); |
| 656 | Py_DECREF(num); |
| 657 | num = temp; |
| 658 | if (num == NULL) |
| 659 | return NULL; |
| 660 | overflow = 0; |
| 661 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 662 | Py_DECREF(num); |
| 663 | if (value == -1 && PyErr_Occurred()) |
| 664 | return NULL; |
| 665 | if (value != -1) |
| 666 | return raiseTestError("test_long_long_and_overflow", |
| 667 | "return value was not set to -1"); |
| 668 | if (overflow != 1) |
| 669 | return raiseTestError("test_long_long_and_overflow", |
| 670 | "overflow was not set to 1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | /* Test that overflow is set properly for a large negative value. */ |
| 673 | /* num is a number smaller than PY_LLONG_MIN on a typical platform */ |
| 674 | num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 675 | if (num == NULL) |
| 676 | return NULL; |
| 677 | overflow = 1234; |
| 678 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 679 | Py_DECREF(num); |
| 680 | if (value == -1 && PyErr_Occurred()) |
| 681 | return NULL; |
| 682 | if (value != -1) |
| 683 | return raiseTestError("test_long_long_and_overflow", |
| 684 | "return value was not set to -1"); |
| 685 | if (overflow != -1) |
| 686 | return raiseTestError("test_long_long_and_overflow", |
| 687 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | /* Same again, with num = PY_LLONG_MIN - 1 */ |
| 690 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 691 | if (num == NULL) |
| 692 | return NULL; |
| 693 | one = PyLong_FromLong(1L); |
| 694 | if (one == NULL) { |
| 695 | Py_DECREF(num); |
| 696 | return NULL; |
| 697 | } |
| 698 | temp = PyNumber_Subtract(num, one); |
| 699 | Py_DECREF(one); |
| 700 | Py_DECREF(num); |
| 701 | num = temp; |
| 702 | if (num == NULL) |
| 703 | return NULL; |
| 704 | overflow = 0; |
| 705 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 706 | Py_DECREF(num); |
| 707 | if (value == -1 && PyErr_Occurred()) |
| 708 | return NULL; |
| 709 | if (value != -1) |
| 710 | return raiseTestError("test_long_long_and_overflow", |
| 711 | "return value was not set to -1"); |
| 712 | if (overflow != -1) |
| 713 | return raiseTestError("test_long_long_and_overflow", |
| 714 | "overflow was not set to -1"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | /* Test that overflow is cleared properly for small values. */ |
| 717 | num = PyLong_FromString("FF", NULL, 16); |
| 718 | if (num == NULL) |
| 719 | return NULL; |
| 720 | overflow = 1234; |
| 721 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 722 | Py_DECREF(num); |
| 723 | if (value == -1 && PyErr_Occurred()) |
| 724 | return NULL; |
| 725 | if (value != 0xFF) |
| 726 | return raiseTestError("test_long_long_and_overflow", |
| 727 | "expected return value 0xFF"); |
| 728 | if (overflow != 0) |
| 729 | return raiseTestError("test_long_long_and_overflow", |
| 730 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | num = PyLong_FromString("-FF", NULL, 16); |
| 733 | if (num == NULL) |
| 734 | return NULL; |
| 735 | overflow = 0; |
| 736 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 737 | Py_DECREF(num); |
| 738 | if (value == -1 && PyErr_Occurred()) |
| 739 | return NULL; |
| 740 | if (value != -0xFF) |
| 741 | return raiseTestError("test_long_long_and_overflow", |
| 742 | "expected return value 0xFF"); |
| 743 | if (overflow != 0) |
| 744 | return raiseTestError("test_long_long_and_overflow", |
| 745 | "overflow was set incorrectly"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | num = PyLong_FromLongLong(PY_LLONG_MAX); |
| 748 | if (num == NULL) |
| 749 | return NULL; |
| 750 | overflow = 1234; |
| 751 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 752 | Py_DECREF(num); |
| 753 | if (value == -1 && PyErr_Occurred()) |
| 754 | return NULL; |
| 755 | if (value != PY_LLONG_MAX) |
| 756 | return raiseTestError("test_long_long_and_overflow", |
| 757 | "expected return value PY_LLONG_MAX"); |
| 758 | if (overflow != 0) |
| 759 | return raiseTestError("test_long_long_and_overflow", |
| 760 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | num = PyLong_FromLongLong(PY_LLONG_MIN); |
| 763 | if (num == NULL) |
| 764 | return NULL; |
| 765 | overflow = 0; |
| 766 | value = PyLong_AsLongLongAndOverflow(num, &overflow); |
| 767 | Py_DECREF(num); |
| 768 | if (value == -1 && PyErr_Occurred()) |
| 769 | return NULL; |
| 770 | if (value != PY_LLONG_MIN) |
| 771 | return raiseTestError("test_long_long_and_overflow", |
| 772 | "expected return value PY_LLONG_MIN"); |
| 773 | if (overflow != 0) |
| 774 | return raiseTestError("test_long_long_and_overflow", |
| 775 | "overflow was not cleared"); |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | Py_INCREF(Py_None); |
| 778 | return Py_None; |
Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 781 | /* 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] | 782 | for both long and int arguments. The test may leak a little memory if |
| 783 | it fails. |
| 784 | */ |
| 785 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 786 | test_L_code(PyObject *self) |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | PyObject *tuple, *num; |
| 789 | PY_LONG_LONG value; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | tuple = PyTuple_New(1); |
| 792 | if (tuple == NULL) |
| 793 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | num = PyLong_FromLong(42); |
| 796 | if (num == NULL) |
| 797 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | value = -1; |
| 802 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 803 | return NULL; |
| 804 | if (value != 42) |
| 805 | return raiseTestError("test_L_code", |
| 806 | "L code returned wrong value for long 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | Py_DECREF(num); |
| 809 | num = PyLong_FromLong(42); |
| 810 | if (num == NULL) |
| 811 | return NULL; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | PyTuple_SET_ITEM(tuple, 0, num); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | value = -1; |
| 816 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) |
| 817 | return NULL; |
| 818 | if (value != 42) |
| 819 | return raiseTestError("test_L_code", |
| 820 | "L code returned wrong value for int 42"); |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | Py_DECREF(tuple); |
| 823 | Py_INCREF(Py_None); |
| 824 | return Py_None; |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | #endif /* ifdef HAVE_LONG_LONG */ |
Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 828 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 829 | /* Test tuple argument processing */ |
| 830 | static PyObject * |
| 831 | getargs_tuple(PyObject *self, PyObject *args) |
| 832 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 | int a, b, c; |
| 834 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) |
| 835 | return NULL; |
| 836 | return Py_BuildValue("iii", a, b, c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 839 | /* test PyArg_ParseTupleAndKeywords */ |
| 840 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
| 841 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 843 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 844 | 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] | 845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 847 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 848 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 849 | return NULL; |
| 850 | return Py_BuildValue("iiiiiiiiii", |
| 851 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 852 | 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] | 853 | } |
| 854 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 855 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 856 | and return the result. |
| 857 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 858 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 859 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 860 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | unsigned char value; |
| 862 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 863 | return NULL; |
| 864 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 867 | static PyObject * |
| 868 | getargs_B(PyObject *self, PyObject *args) |
| 869 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | unsigned char value; |
| 871 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 872 | return NULL; |
| 873 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 877 | getargs_h(PyObject *self, PyObject *args) |
| 878 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | short value; |
| 880 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 881 | return NULL; |
| 882 | return PyLong_FromLong((long)value); |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 886 | getargs_H(PyObject *self, PyObject *args) |
| 887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | unsigned short value; |
| 889 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 890 | return NULL; |
| 891 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | static PyObject * |
| 895 | getargs_I(PyObject *self, PyObject *args) |
| 896 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | unsigned int value; |
| 898 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 899 | return NULL; |
| 900 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static PyObject * |
| 904 | getargs_k(PyObject *self, PyObject *args) |
| 905 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | unsigned long value; |
| 907 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 908 | return NULL; |
| 909 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static PyObject * |
| 913 | getargs_i(PyObject *self, PyObject *args) |
| 914 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | int value; |
| 916 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 917 | return NULL; |
| 918 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 921 | static PyObject * |
| 922 | getargs_l(PyObject *self, PyObject *args) |
| 923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | long value; |
| 925 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 926 | return NULL; |
| 927 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 930 | static PyObject * |
| 931 | getargs_n(PyObject *self, PyObject *args) |
| 932 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | Py_ssize_t value; |
| 934 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 935 | return NULL; |
| 936 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 939 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 940 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 941 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 942 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | PY_LONG_LONG value; |
| 944 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 945 | return NULL; |
| 946 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 949 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 950 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | unsigned PY_LONG_LONG value; |
| 953 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 954 | return NULL; |
| 955 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 956 | } |
| 957 | #endif |
| 958 | |
| 959 | /* This function not only tests the 'k' getargs code, but also the |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 960 | PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 961 | static PyObject * |
| 962 | test_k_code(PyObject *self) |
| 963 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | PyObject *tuple, *num; |
| 965 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | tuple = PyTuple_New(1); |
| 968 | if (tuple == NULL) |
| 969 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 972 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 973 | if (num == NULL) |
| 974 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | value = PyLong_AsUnsignedLongMask(num); |
| 977 | if (value != ULONG_MAX) |
| 978 | return raiseTestError("test_k_code", |
| 979 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | value = 0; |
| 984 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 985 | return NULL; |
| 986 | if (value != ULONG_MAX) |
| 987 | return raiseTestError("test_k_code", |
| 988 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | Py_DECREF(num); |
| 991 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 992 | if (num == NULL) |
| 993 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 994 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | value = PyLong_AsUnsignedLongMask(num); |
| 996 | if (value != (unsigned long)-0x42) |
| 997 | return raiseTestError("test_k_code", |
| 998 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | value = 0; |
| 1003 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 1004 | return NULL; |
| 1005 | if (value != (unsigned long)-0x42) |
| 1006 | return raiseTestError("test_k_code", |
| 1007 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | Py_DECREF(tuple); |
| 1010 | Py_INCREF(Py_None); |
| 1011 | return Py_None; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1014 | static PyObject * |
| 1015 | getargs_s(PyObject *self, PyObject *args) |
| 1016 | { |
| 1017 | char *str; |
| 1018 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1019 | return NULL; |
| 1020 | return PyBytes_FromString(str); |
| 1021 | } |
| 1022 | |
| 1023 | static PyObject * |
| 1024 | getargs_s_star(PyObject *self, PyObject *args) |
| 1025 | { |
| 1026 | Py_buffer buffer; |
| 1027 | PyObject *bytes; |
| 1028 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1029 | return NULL; |
| 1030 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1031 | PyBuffer_Release(&buffer); |
| 1032 | return bytes; |
| 1033 | } |
| 1034 | |
| 1035 | static PyObject * |
| 1036 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1037 | { |
| 1038 | char *str; |
| 1039 | Py_ssize_t size; |
| 1040 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1041 | return NULL; |
| 1042 | return PyBytes_FromStringAndSize(str, size); |
| 1043 | } |
| 1044 | |
| 1045 | static PyObject * |
| 1046 | getargs_z(PyObject *self, PyObject *args) |
| 1047 | { |
| 1048 | char *str; |
| 1049 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1050 | return NULL; |
| 1051 | if (str != NULL) |
| 1052 | return PyBytes_FromString(str); |
| 1053 | else |
| 1054 | Py_RETURN_NONE; |
| 1055 | } |
| 1056 | |
| 1057 | static PyObject * |
| 1058 | getargs_z_star(PyObject *self, PyObject *args) |
| 1059 | { |
| 1060 | Py_buffer buffer; |
| 1061 | PyObject *bytes; |
| 1062 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1063 | return NULL; |
| 1064 | if (buffer.buf != NULL) |
| 1065 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1066 | else { |
| 1067 | Py_INCREF(Py_None); |
| 1068 | bytes = Py_None; |
| 1069 | } |
| 1070 | PyBuffer_Release(&buffer); |
| 1071 | return bytes; |
| 1072 | } |
| 1073 | |
| 1074 | static PyObject * |
| 1075 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1076 | { |
| 1077 | char *str; |
| 1078 | Py_ssize_t size; |
| 1079 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1080 | return NULL; |
| 1081 | if (str != NULL) |
| 1082 | return PyBytes_FromStringAndSize(str, size); |
| 1083 | else |
| 1084 | Py_RETURN_NONE; |
| 1085 | } |
| 1086 | |
| 1087 | static PyObject * |
| 1088 | getargs_y(PyObject *self, PyObject *args) |
| 1089 | { |
| 1090 | char *str; |
| 1091 | if (!PyArg_ParseTuple(args, "y", &str)) |
| 1092 | return NULL; |
| 1093 | return PyBytes_FromString(str); |
| 1094 | } |
| 1095 | |
| 1096 | static PyObject * |
| 1097 | getargs_y_star(PyObject *self, PyObject *args) |
| 1098 | { |
| 1099 | Py_buffer buffer; |
| 1100 | PyObject *bytes; |
| 1101 | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 1102 | return NULL; |
| 1103 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1104 | PyBuffer_Release(&buffer); |
| 1105 | return bytes; |
| 1106 | } |
| 1107 | |
| 1108 | static PyObject * |
| 1109 | getargs_y_hash(PyObject *self, PyObject *args) |
| 1110 | { |
| 1111 | char *str; |
| 1112 | Py_ssize_t size; |
| 1113 | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
| 1114 | return NULL; |
| 1115 | return PyBytes_FromStringAndSize(str, size); |
| 1116 | } |
| 1117 | |
| 1118 | static PyObject * |
| 1119 | getargs_u(PyObject *self, PyObject *args) |
| 1120 | { |
| 1121 | Py_UNICODE *str; |
| 1122 | Py_ssize_t size; |
| 1123 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1124 | return NULL; |
| 1125 | size = Py_UNICODE_strlen(str); |
| 1126 | return PyUnicode_FromUnicode(str, size); |
| 1127 | } |
| 1128 | |
| 1129 | static PyObject * |
| 1130 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1131 | { |
| 1132 | Py_UNICODE *str; |
| 1133 | Py_ssize_t size; |
| 1134 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1135 | return NULL; |
| 1136 | return PyUnicode_FromUnicode(str, size); |
| 1137 | } |
| 1138 | |
| 1139 | static PyObject * |
| 1140 | getargs_Z(PyObject *self, PyObject *args) |
| 1141 | { |
| 1142 | Py_UNICODE *str; |
| 1143 | Py_ssize_t size; |
| 1144 | if (!PyArg_ParseTuple(args, "Z", &str)) |
| 1145 | return NULL; |
| 1146 | if (str != NULL) { |
| 1147 | size = Py_UNICODE_strlen(str); |
| 1148 | return PyUnicode_FromUnicode(str, size); |
| 1149 | } else |
| 1150 | Py_RETURN_NONE; |
| 1151 | } |
| 1152 | |
| 1153 | static PyObject * |
| 1154 | getargs_Z_hash(PyObject *self, PyObject *args) |
| 1155 | { |
| 1156 | Py_UNICODE *str; |
| 1157 | Py_ssize_t size; |
| 1158 | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
| 1159 | return NULL; |
| 1160 | if (str != NULL) |
| 1161 | return PyUnicode_FromUnicode(str, size); |
| 1162 | else |
| 1163 | Py_RETURN_NONE; |
| 1164 | } |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1165 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1166 | /* Test the s and z codes for PyArg_ParseTuple. |
| 1167 | */ |
| 1168 | static PyObject * |
| 1169 | test_s_code(PyObject *self) |
| 1170 | { |
| 1171 | /* Unicode strings should be accepted */ |
| 1172 | PyObject *tuple, *obj; |
| 1173 | char *value; |
| 1174 | |
| 1175 | tuple = PyTuple_New(1); |
| 1176 | if (tuple == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1178 | |
| 1179 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1181 | if (obj == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1183 | |
| 1184 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1185 | |
| 1186 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1188 | */ |
| 1189 | if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1191 | |
| 1192 | if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1194 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1195 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1196 | Py_RETURN_NONE; |
| 1197 | } |
| 1198 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1199 | static PyObject * |
| 1200 | test_bug_7414(PyObject *self) |
| 1201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | /* Issue #7414: for PyArg_ParseTupleAndKeywords, 'C' code wasn't being |
| 1203 | skipped properly in skipitem() */ |
| 1204 | int a = 0, b = 0, result; |
| 1205 | char *kwlist[] = {"a", "b", NULL}; |
| 1206 | PyObject *tuple = NULL, *dict = NULL, *b_str; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | tuple = PyTuple_New(0); |
| 1209 | if (tuple == NULL) |
| 1210 | goto failure; |
| 1211 | dict = PyDict_New(); |
| 1212 | if (dict == NULL) |
| 1213 | goto failure; |
| 1214 | b_str = PyUnicode_FromString("b"); |
| 1215 | if (b_str == NULL) |
| 1216 | goto failure; |
| 1217 | result = PyDict_SetItemString(dict, "b", b_str); |
| 1218 | Py_DECREF(b_str); |
| 1219 | if (result < 0) |
| 1220 | goto failure; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|CC", |
| 1223 | kwlist, &a, &b); |
| 1224 | if (!result) |
| 1225 | goto failure; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1227 | if (a != 0) |
| 1228 | return raiseTestError("test_bug_7414", |
| 1229 | "C format code not skipped properly"); |
| 1230 | if (b != 'b') |
| 1231 | return raiseTestError("test_bug_7414", |
| 1232 | "C format code returned wrong value"); |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | Py_DECREF(dict); |
| 1235 | Py_DECREF(tuple); |
| 1236 | Py_RETURN_NONE; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1237 | |
| 1238 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | Py_XDECREF(dict); |
| 1240 | Py_XDECREF(tuple); |
| 1241 | return NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1245 | static volatile int x; |
| 1246 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1247 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1248 | of an error. |
| 1249 | */ |
| 1250 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1251 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | PyObject *tuple, *obj; |
| 1254 | Py_UNICODE *value; |
| 1255 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1258 | /* Just use the macro and check that it compiles */ |
| 1259 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | tuple = PyTuple_New(1); |
| 1262 | if (tuple == NULL) |
| 1263 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1266 | "ascii", NULL); |
| 1267 | if (obj == NULL) |
| 1268 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | value = 0; |
| 1273 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 1274 | return NULL; |
| 1275 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1276 | return raiseTestError("test_u_code", |
| 1277 | "u code returned wrong value for u'test'"); |
| 1278 | value = 0; |
| 1279 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 1280 | return NULL; |
| 1281 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1282 | len != PyUnicode_GET_SIZE(obj)) |
| 1283 | return raiseTestError("test_u_code", |
| 1284 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | Py_DECREF(tuple); |
| 1287 | Py_INCREF(Py_None); |
| 1288 | return Py_None; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1291 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1292 | static PyObject * |
| 1293 | test_Z_code(PyObject *self) |
| 1294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | PyObject *tuple, *obj; |
| 1296 | Py_UNICODE *value1, *value2; |
| 1297 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | tuple = PyTuple_New(2); |
| 1300 | if (tuple == NULL) |
| 1301 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | obj = PyUnicode_FromString("test"); |
| 1304 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1305 | Py_INCREF(Py_None); |
| 1306 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | /* swap values on purpose */ |
| 1309 | value1 = NULL; |
| 1310 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | /* Test Z for both values */ |
| 1313 | if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) |
| 1314 | return NULL; |
| 1315 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1316 | return raiseTestError("test_Z_code", |
| 1317 | "Z code returned wrong value for 'test'"); |
| 1318 | if (value2 != NULL) |
| 1319 | return raiseTestError("test_Z_code", |
| 1320 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | value1 = NULL; |
| 1323 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1324 | len1 = -1; |
| 1325 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | /* Test Z# for both values */ |
| 1328 | if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1329 | &value2, &len2) < 0) |
| 1330 | return NULL; |
| 1331 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1332 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1333 | return raiseTestError("test_Z_code", |
| 1334 | "Z# code returned wrong values for 'test'"); |
| 1335 | if (value2 != NULL || |
| 1336 | len2 != 0) |
| 1337 | return raiseTestError("test_Z_code", |
| 1338 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | Py_DECREF(tuple); |
| 1341 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1344 | static PyObject * |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1345 | test_widechar(PyObject *self) |
| 1346 | { |
| 1347 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1349 | size_t wtextlen = 1; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1350 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1352 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1353 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1357 | if (wide == NULL) |
| 1358 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1361 | if (utf8 == NULL) { |
| 1362 | Py_DECREF(wide); |
| 1363 | return NULL; |
| 1364 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) { |
| 1367 | Py_DECREF(wide); |
| 1368 | Py_DECREF(utf8); |
| 1369 | return raiseTestError("test_widechar", |
| 1370 | "wide string and utf8 string " |
| 1371 | "have different length"); |
| 1372 | } |
| 1373 | if (PyUnicode_Compare(wide, utf8)) { |
| 1374 | Py_DECREF(wide); |
| 1375 | Py_DECREF(utf8); |
| 1376 | if (PyErr_Occurred()) |
| 1377 | return NULL; |
| 1378 | return raiseTestError("test_widechar", |
| 1379 | "wide string and utf8 string " |
| 1380 | "are different"); |
| 1381 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | Py_DECREF(wide); |
| 1384 | Py_DECREF(utf8); |
| 1385 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1389 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1390 | { |
| 1391 | PyObject *unicode, *result; |
| 1392 | Py_ssize_t buflen, size; |
| 1393 | wchar_t *buffer; |
| 1394 | |
| 1395 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1396 | return NULL; |
| 1397 | buffer = PyMem_Malloc(buflen * sizeof(wchar_t)); |
| 1398 | if (buffer == NULL) |
| 1399 | return PyErr_NoMemory(); |
| 1400 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1401 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1402 | if (size == -1) { |
| 1403 | PyMem_Free(buffer); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | |
| 1407 | if (size < buflen) |
| 1408 | buflen = size + 1; |
| 1409 | else |
| 1410 | buflen = size; |
| 1411 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 1412 | PyMem_Free(buffer); |
| 1413 | if (result == NULL) |
| 1414 | return NULL; |
| 1415 | |
| 1416 | return Py_BuildValue("(Nn)", result, size); |
| 1417 | } |
| 1418 | |
| 1419 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1420 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1421 | { |
| 1422 | PyObject *unicode, *result; |
| 1423 | Py_ssize_t size; |
| 1424 | wchar_t *buffer; |
| 1425 | |
| 1426 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 1427 | return NULL; |
| 1428 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1429 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1430 | if (buffer == NULL) |
| 1431 | return NULL; |
| 1432 | |
| 1433 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 1434 | PyMem_Free(buffer); |
| 1435 | if (result == NULL) |
| 1436 | return NULL; |
| 1437 | return Py_BuildValue("(Nn)", result, size); |
| 1438 | } |
| 1439 | |
| 1440 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1441 | getargs_w_star(PyObject *self, PyObject *args) |
| 1442 | { |
| 1443 | Py_buffer buffer; |
| 1444 | PyObject *result; |
| 1445 | char *str; |
| 1446 | |
| 1447 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 1448 | return NULL; |
| 1449 | |
| 1450 | if (2 <= buffer.len) { |
| 1451 | str = buffer.buf; |
| 1452 | str[0] = '['; |
| 1453 | str[buffer.len-1] = ']'; |
| 1454 | } |
| 1455 | |
| 1456 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1457 | PyBuffer_Release(&buffer); |
| 1458 | return result; |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1463 | test_empty_argparse(PyObject *self) |
| 1464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 1466 | PyObject *tuple, *dict = NULL; |
| 1467 | static char *kwlist[] = {NULL}; |
| 1468 | int result; |
| 1469 | tuple = PyTuple_New(0); |
| 1470 | if (!tuple) |
| 1471 | return NULL; |
| 1472 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 1473 | goto done; |
| 1474 | dict = PyDict_New(); |
| 1475 | if (!dict) |
| 1476 | goto done; |
| 1477 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1478 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | Py_DECREF(tuple); |
| 1480 | Py_XDECREF(dict); |
| 1481 | if (result < 0) |
| 1482 | return NULL; |
| 1483 | else { |
| 1484 | Py_RETURN_NONE; |
| 1485 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1489 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | const char *encoding, *errors = NULL; |
| 1492 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 1493 | &encoding, &errors)) |
| 1494 | return NULL; |
| 1495 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1498 | static PyObject * |
| 1499 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1500 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | const char *encoding, *errors = NULL; |
| 1502 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 1503 | &encoding, &errors)) |
| 1504 | return NULL; |
| 1505 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1508 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1509 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1510 | static PyObject * |
| 1511 | test_long_numbits(PyObject *self) |
| 1512 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | struct triple { |
| 1514 | long input; |
| 1515 | size_t nbits; |
| 1516 | int sign; |
| 1517 | } testcases[] = {{0, 0, 0}, |
| 1518 | {1L, 1, 1}, |
| 1519 | {-1L, 1, -1}, |
| 1520 | {2L, 2, 1}, |
| 1521 | {-2L, 2, -1}, |
| 1522 | {3L, 2, 1}, |
| 1523 | {-3L, 2, -1}, |
| 1524 | {4L, 3, 1}, |
| 1525 | {-4L, 3, -1}, |
| 1526 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 1527 | {-0x7fffL, 15, -1}, |
| 1528 | {0xffffL, 16, 1}, |
| 1529 | {-0xffffL, 16, -1}, |
| 1530 | {0xfffffffL, 28, 1}, |
| 1531 | {-0xfffffffL, 28, -1}}; |
| 1532 | int i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 1535 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
| 1536 | size_t nbits = _PyLong_NumBits(plong); |
| 1537 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | Py_DECREF(plong); |
| 1540 | if (nbits != testcases[i].nbits) |
| 1541 | return raiseTestError("test_long_numbits", |
| 1542 | "wrong result for _PyLong_NumBits"); |
| 1543 | if (sign != testcases[i].sign) |
| 1544 | return raiseTestError("test_long_numbits", |
| 1545 | "wrong result for _PyLong_Sign"); |
| 1546 | } |
| 1547 | Py_INCREF(Py_None); |
| 1548 | return Py_None; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1551 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1552 | |
| 1553 | static PyObject * |
| 1554 | test_null_strings(PyObject *self) |
| 1555 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 1557 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 1558 | Py_XDECREF(o1); |
| 1559 | Py_XDECREF(o2); |
| 1560 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1563 | static PyObject * |
| 1564 | raise_exception(PyObject *self, PyObject *args) |
| 1565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | PyObject *exc; |
| 1567 | PyObject *exc_args, *v; |
| 1568 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1571 | &exc, &num_args)) |
| 1572 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | exc_args = PyTuple_New(num_args); |
| 1575 | if (exc_args == NULL) |
| 1576 | return NULL; |
| 1577 | for (i = 0; i < num_args; ++i) { |
| 1578 | v = PyLong_FromLong(i); |
| 1579 | if (v == NULL) { |
| 1580 | Py_DECREF(exc_args); |
| 1581 | return NULL; |
| 1582 | } |
| 1583 | PyTuple_SET_ITEM(exc_args, i, v); |
| 1584 | } |
| 1585 | PyErr_SetObject(exc, exc_args); |
| 1586 | Py_DECREF(exc_args); |
| 1587 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1588 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1589 | |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 1590 | |
| 1591 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1592 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1593 | static PyObject * |
| 1594 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1595 | if (PyDateTimeAPI) { |
| 1596 | if (test_run_counter) { |
| 1597 | /* Probably regrtest.py -R */ |
| 1598 | Py_RETURN_NONE; |
| 1599 | } |
| 1600 | else { |
| 1601 | PyErr_SetString(PyExc_AssertionError, |
| 1602 | "PyDateTime_CAPI somehow initialized"); |
| 1603 | return NULL; |
| 1604 | } |
| 1605 | } |
| 1606 | test_run_counter++; |
| 1607 | PyDateTime_IMPORT; |
| 1608 | if (PyDateTimeAPI) |
| 1609 | Py_RETURN_NONE; |
| 1610 | else |
| 1611 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 1614 | |
| 1615 | #ifdef WITH_THREAD |
| 1616 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1617 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 1618 | * `thread_done` when it's finished. The driver code has to know when the |
| 1619 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 1620 | * may go away when the driver finishes. The former lack of this explicit |
| 1621 | * synchronization caused rare segfaults, so rare that they were seen only |
| 1622 | * on a Mac buildbot (although they were possible on any box). |
| 1623 | */ |
| 1624 | static PyThread_type_lock thread_done = NULL; |
| 1625 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1626 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1627 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | PyObject *rc; |
| 1630 | int success; |
| 1631 | PyGILState_STATE s = PyGILState_Ensure(); |
| 1632 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
| 1633 | success = (rc != NULL); |
| 1634 | Py_XDECREF(rc); |
| 1635 | PyGILState_Release(s); |
| 1636 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1639 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 1640 | * should be called only from threads spawned by test_thread_state(). |
| 1641 | */ |
| 1642 | static void |
| 1643 | _make_call_from_thread(void *callable) |
| 1644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | _make_call(callable); |
| 1646 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1649 | static PyObject * |
| 1650 | test_thread_state(PyObject *self, PyObject *args) |
| 1651 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1652 | PyObject *fn; |
| 1653 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 1656 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | if (!PyCallable_Check(fn)) { |
| 1659 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1660 | fn->ob_type->tp_name); |
| 1661 | return NULL; |
| 1662 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1664 | /* Ensure Python is set up for threading */ |
| 1665 | PyEval_InitThreads(); |
| 1666 | thread_done = PyThread_allocate_lock(); |
| 1667 | if (thread_done == NULL) |
| 1668 | return PyErr_NoMemory(); |
| 1669 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | /* Start a new thread with our callback. */ |
| 1672 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1673 | /* Make the callback with the thread lock held by this thread */ |
| 1674 | success &= _make_call(fn); |
| 1675 | /* Do it all again, but this time with the thread-lock released */ |
| 1676 | Py_BEGIN_ALLOW_THREADS |
| 1677 | success &= _make_call(fn); |
| 1678 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1679 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1681 | /* And once more with and without a thread |
| 1682 | XXX - should use a lock and work out exactly what we are trying |
| 1683 | to test <wink> |
| 1684 | */ |
| 1685 | Py_BEGIN_ALLOW_THREADS |
| 1686 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1687 | success &= _make_call(fn); |
| 1688 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1689 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1691 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 1692 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | PyThread_free_lock(thread_done); |
| 1695 | if (!success) |
| 1696 | return NULL; |
| 1697 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1698 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1699 | |
| 1700 | /* test Py_AddPendingCalls using threads */ |
| 1701 | static int _pending_callback(void *arg) |
| 1702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | /* we assume the argument is callable object to which we own a reference */ |
| 1704 | PyObject *callable = (PyObject *)arg; |
| 1705 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 1706 | Py_DECREF(callable); |
| 1707 | Py_XDECREF(r); |
| 1708 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | /* The following requests n callbacks to _pending_callback. It can be |
| 1712 | * run from any python thread. |
| 1713 | */ |
| 1714 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1715 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | PyObject *callable; |
| 1717 | int r; |
| 1718 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1719 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | /* create the reference for the callbackwhile we hold the lock */ |
| 1722 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | Py_BEGIN_ALLOW_THREADS |
| 1725 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1726 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | if (r<0) { |
| 1729 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1730 | Py_INCREF(Py_False); |
| 1731 | return Py_False; |
| 1732 | } |
| 1733 | Py_INCREF(Py_True); |
| 1734 | return Py_True; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1735 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1736 | #endif |
| 1737 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 1738 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1739 | static PyObject * |
| 1740 | test_string_from_format(PyObject *self, PyObject *args) |
| 1741 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | PyObject *result; |
| 1743 | char *msg; |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1744 | static const Py_UNICODE one[] = {'1', 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1745 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1746 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 1747 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 1748 | if (result == NULL) \ |
| 1749 | return NULL; \ |
| 1750 | if (Py_UNICODE_strcmp(PyUnicode_AS_UNICODE(result), one)) { \ |
| 1751 | msg = FORMAT " failed at 1"; \ |
| 1752 | goto Fail; \ |
| 1753 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1756 | CHECK_1_FORMAT("%d", int); |
| 1757 | CHECK_1_FORMAT("%ld", long); |
| 1758 | /* The z width modifier was added in Python 2.5. */ |
| 1759 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | /* The u type code was added in Python 2.5. */ |
| 1762 | CHECK_1_FORMAT("%u", unsigned int); |
| 1763 | CHECK_1_FORMAT("%lu", unsigned long); |
| 1764 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1767 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 1769 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1770 | #endif |
| 1771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1773 | |
| 1774 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | Py_XDECREF(result); |
| 1776 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1777 | |
| 1778 | #undef CHECK_1_FORMAT |
| 1779 | } |
| 1780 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 1781 | |
| 1782 | static PyObject * |
| 1783 | test_unicode_compare_with_ascii(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 1785 | int result; |
| 1786 | if (py_s == NULL) |
| 1787 | return NULL; |
| 1788 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 1789 | Py_DECREF(py_s); |
| 1790 | if (!result) { |
| 1791 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 1792 | "should not compare equal to c string."); |
| 1793 | return NULL; |
| 1794 | } |
| 1795 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 1796 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 1797 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1798 | /* This is here to provide a docstring for test_descr. */ |
| 1799 | static PyObject * |
| 1800 | test_with_docstring(PyObject *self) |
| 1801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1802 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1805 | /* Test PyOS_string_to_double. */ |
| 1806 | static PyObject * |
| 1807 | test_string_to_double(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | double result; |
| 1809 | char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | #define CHECK_STRING(STR, expected) \ |
| 1812 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1813 | if (result == -1.0 && PyErr_Occurred()) \ |
| 1814 | return NULL; \ |
| 1815 | if (result != expected) { \ |
| 1816 | msg = "conversion of " STR " to float failed"; \ |
| 1817 | goto fail; \ |
| 1818 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | #define CHECK_INVALID(STR) \ |
| 1821 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1822 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 1823 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 1824 | PyErr_Clear(); \ |
| 1825 | else \ |
| 1826 | return NULL; \ |
| 1827 | } \ |
| 1828 | else { \ |
| 1829 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 1830 | goto fail; \ |
| 1831 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | CHECK_STRING("0.1", 0.1); |
| 1834 | CHECK_STRING("1.234", 1.234); |
| 1835 | CHECK_STRING("-1.35", -1.35); |
| 1836 | CHECK_STRING(".1e01", 1.0); |
| 1837 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | CHECK_INVALID(" 0.1"); |
| 1840 | CHECK_INVALID("\t\n-3"); |
| 1841 | CHECK_INVALID(".123 "); |
| 1842 | CHECK_INVALID("3\n"); |
| 1843 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1846 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1848 | #undef CHECK_STRING |
| 1849 | #undef CHECK_INVALID |
| 1850 | } |
| 1851 | |
| 1852 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1853 | /* Coverage testing of capsule objects. */ |
| 1854 | |
| 1855 | static const char *capsule_name = "capsule name"; |
| 1856 | static char *capsule_pointer = "capsule pointer"; |
| 1857 | static char *capsule_context = "capsule context"; |
| 1858 | static const char *capsule_error = NULL; |
| 1859 | static int |
| 1860 | capsule_destructor_call_count = 0; |
| 1861 | |
| 1862 | static void |
| 1863 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | capsule_destructor_call_count++; |
| 1865 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 1866 | capsule_error = "context did not match in destructor!"; |
| 1867 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 1868 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 1869 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 1870 | capsule_error = "name did not match in destructor!"; |
| 1871 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 1872 | capsule_error = "pointer did not match in destructor!"; |
| 1873 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 | char *name; |
| 1878 | char *module; |
| 1879 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1880 | } known_capsule; |
| 1881 | |
| 1882 | static PyObject * |
| 1883 | test_capsule(PyObject *self, PyObject *args) |
| 1884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | PyObject *object; |
| 1886 | const char *error = NULL; |
| 1887 | void *pointer; |
| 1888 | void *pointer2; |
| 1889 | known_capsule known_capsules[] = { |
| 1890 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 1891 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 1892 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 1893 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 1894 | { NULL, NULL }, |
| 1895 | }; |
| 1896 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1897 | |
| 1898 | #define FAIL(x) { error = (x); goto exit; } |
| 1899 | |
| 1900 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1901 | if (capsule_error) { \ |
| 1902 | FAIL(capsule_error); \ |
| 1903 | } \ |
| 1904 | else if (!capsule_destructor_call_count) { \ |
| 1905 | FAIL("destructor not called!"); \ |
| 1906 | } \ |
| 1907 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 1910 | PyCapsule_SetContext(object, capsule_context); |
| 1911 | capsule_destructor(object); |
| 1912 | CHECK_DESTRUCTOR; |
| 1913 | Py_DECREF(object); |
| 1914 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1916 | object = PyCapsule_New(known, "ignored", NULL); |
| 1917 | PyCapsule_SetPointer(object, capsule_pointer); |
| 1918 | PyCapsule_SetName(object, capsule_name); |
| 1919 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 1920 | PyCapsule_SetContext(object, capsule_context); |
| 1921 | capsule_destructor(object); |
| 1922 | CHECK_DESTRUCTOR; |
| 1923 | /* intentionally access using the wrong name */ |
| 1924 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 1925 | if (!PyErr_Occurred()) { |
| 1926 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1927 | } |
| 1928 | PyErr_Clear(); |
| 1929 | if (pointer2) { |
| 1930 | if (pointer2 == capsule_pointer) { |
| 1931 | FAIL("PyCapsule_GetPointer should not have" |
| 1932 | " returned the internal pointer!"); |
| 1933 | } else { |
| 1934 | FAIL("PyCapsule_GetPointer should have " |
| 1935 | "returned NULL pointer but did not!"); |
| 1936 | } |
| 1937 | } |
| 1938 | PyCapsule_SetDestructor(object, NULL); |
| 1939 | Py_DECREF(object); |
| 1940 | if (capsule_destructor_call_count) { |
| 1941 | FAIL("destructor called when it should not have been!"); |
| 1942 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 1945 | /* yeah, ordinarily I wouldn't do this either, |
| 1946 | but it's fine for this test harness. |
| 1947 | */ |
| 1948 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1949 | #undef FAIL |
| 1950 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | { \ |
| 1952 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 1953 | x, known->module, known->attribute); \ |
| 1954 | error = buffer; \ |
| 1955 | goto exit; \ |
| 1956 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | PyObject *module = PyImport_ImportModule(known->module); |
| 1959 | if (module) { |
| 1960 | pointer = PyCapsule_Import(known->name, 0); |
| 1961 | if (!pointer) { |
| 1962 | Py_DECREF(module); |
| 1963 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 1964 | } |
| 1965 | object = PyObject_GetAttrString(module, known->attribute); |
| 1966 | if (!object) { |
| 1967 | Py_DECREF(module); |
| 1968 | return NULL; |
| 1969 | } |
| 1970 | pointer2 = PyCapsule_GetPointer(object, |
| 1971 | "weebles wobble but they don't fall down"); |
| 1972 | if (!PyErr_Occurred()) { |
| 1973 | Py_DECREF(object); |
| 1974 | Py_DECREF(module); |
| 1975 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1976 | } |
| 1977 | PyErr_Clear(); |
| 1978 | if (pointer2) { |
| 1979 | Py_DECREF(module); |
| 1980 | Py_DECREF(object); |
| 1981 | if (pointer2 == pointer) { |
| 1982 | FAIL("PyCapsule_GetPointer should not have" |
| 1983 | " returned its internal pointer!"); |
| 1984 | } else { |
| 1985 | FAIL("PyCapsule_GetPointer should have" |
| 1986 | " returned NULL pointer but did not!"); |
| 1987 | } |
| 1988 | } |
| 1989 | Py_DECREF(object); |
| 1990 | Py_DECREF(module); |
| 1991 | } |
| 1992 | else |
| 1993 | PyErr_Clear(); |
| 1994 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1995 | |
| 1996 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | if (error) { |
| 1998 | return raiseTestError("test_capsule", error); |
| 1999 | } |
| 2000 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2001 | #undef FAIL |
| 2002 | } |
| 2003 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2004 | #ifdef HAVE_GETTIMEOFDAY |
| 2005 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 2006 | 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] | 2007 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | e->tv_sec -= s->tv_sec; |
| 2009 | e->tv_usec -= s->tv_usec; |
| 2010 | if (e->tv_usec < 0) { |
| 2011 | e->tv_sec -=1; |
| 2012 | e->tv_usec += 1000000; |
| 2013 | } |
| 2014 | 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] | 2015 | } |
| 2016 | |
| 2017 | static PyObject * |
| 2018 | profile_int(PyObject *self, PyObject* args) |
| 2019 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2020 | int i, k; |
| 2021 | struct timeval start, stop; |
| 2022 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | /* Test 1: Allocate and immediately deallocate |
| 2025 | many small integers */ |
| 2026 | gettimeofday(&start, NULL); |
| 2027 | for(k=0; k < 20000; k++) |
| 2028 | for(i=0; i < 1000; i++) { |
| 2029 | single = PyLong_FromLong(i); |
| 2030 | Py_DECREF(single); |
| 2031 | } |
| 2032 | gettimeofday(&stop, NULL); |
| 2033 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | /* Test 2: Allocate and immediately deallocate |
| 2036 | many large integers */ |
| 2037 | gettimeofday(&start, NULL); |
| 2038 | for(k=0; k < 20000; k++) |
| 2039 | for(i=0; i < 1000; i++) { |
| 2040 | single = PyLong_FromLong(i+1000000); |
| 2041 | Py_DECREF(single); |
| 2042 | } |
| 2043 | gettimeofday(&stop, NULL); |
| 2044 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2046 | /* Test 3: Allocate a few integers, then release |
| 2047 | them all simultaneously. */ |
| 2048 | multiple = malloc(sizeof(PyObject*) * 1000); |
| 2049 | gettimeofday(&start, NULL); |
| 2050 | for(k=0; k < 20000; k++) { |
| 2051 | for(i=0; i < 1000; i++) { |
| 2052 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2053 | } |
| 2054 | for(i=0; i < 1000; i++) { |
| 2055 | Py_DECREF(multiple[i]); |
| 2056 | } |
| 2057 | } |
| 2058 | gettimeofday(&stop, NULL); |
| 2059 | print_delta(3, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | /* Test 4: Allocate many integers, then release |
| 2062 | them all simultaneously. */ |
| 2063 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 2064 | gettimeofday(&start, NULL); |
| 2065 | for(k=0; k < 20; k++) { |
| 2066 | for(i=0; i < 1000000; i++) { |
| 2067 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2068 | } |
| 2069 | for(i=0; i < 1000000; i++) { |
| 2070 | Py_DECREF(multiple[i]); |
| 2071 | } |
| 2072 | } |
| 2073 | gettimeofday(&stop, NULL); |
| 2074 | print_delta(4, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | /* Test 5: Allocate many integers < 32000 */ |
| 2077 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 2078 | gettimeofday(&start, NULL); |
| 2079 | for(k=0; k < 10; k++) { |
| 2080 | for(i=0; i < 1000000; i++) { |
| 2081 | multiple[i] = PyLong_FromLong(i+1000); |
| 2082 | } |
| 2083 | for(i=0; i < 1000000; i++) { |
| 2084 | Py_DECREF(multiple[i]); |
| 2085 | } |
| 2086 | } |
| 2087 | gettimeofday(&stop, NULL); |
| 2088 | print_delta(5, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | /* Test 6: Perform small int addition */ |
| 2091 | op1 = PyLong_FromLong(1); |
| 2092 | gettimeofday(&start, NULL); |
| 2093 | for(i=0; i < 10000000; i++) { |
| 2094 | result = PyNumber_Add(op1, op1); |
| 2095 | Py_DECREF(result); |
| 2096 | } |
| 2097 | gettimeofday(&stop, NULL); |
| 2098 | Py_DECREF(op1); |
| 2099 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | /* Test 7: Perform medium int addition */ |
| 2102 | op1 = PyLong_FromLong(1000); |
| 2103 | gettimeofday(&start, NULL); |
| 2104 | for(i=0; i < 10000000; i++) { |
| 2105 | result = PyNumber_Add(op1, op1); |
| 2106 | Py_DECREF(result); |
| 2107 | } |
| 2108 | gettimeofday(&stop, NULL); |
| 2109 | Py_DECREF(op1); |
| 2110 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2112 | Py_INCREF(Py_None); |
| 2113 | return Py_None; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2114 | } |
| 2115 | #endif |
| 2116 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2117 | /* To test the format of tracebacks as printed out. */ |
| 2118 | static PyObject * |
| 2119 | traceback_print(PyObject *self, PyObject *args) |
| 2120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | PyObject *file; |
| 2122 | PyObject *traceback; |
| 2123 | int result; |
| 2124 | |
| 2125 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 2126 | &traceback, &file)) |
| 2127 | return NULL; |
| 2128 | |
| 2129 | result = PyTraceBack_Print(traceback, file); |
| 2130 | if (result < 0) |
| 2131 | return NULL; |
| 2132 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2135 | /* To test the format of exceptions as printed out. */ |
| 2136 | static PyObject * |
| 2137 | exception_print(PyObject *self, PyObject *args) |
| 2138 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | PyObject *value; |
| 2140 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 2143 | &value)) |
| 2144 | return NULL; |
| 2145 | if (!PyExceptionInstance_Check(value)) { |
| 2146 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 2147 | return NULL; |
| 2148 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | tb = PyException_GetTraceback(value); |
| 2151 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 2152 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2154 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | |
| 2158 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2159 | |
| 2160 | /* reliably raise a MemoryError */ |
| 2161 | static PyObject * |
| 2162 | raise_memoryerror(PyObject *self) |
| 2163 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | PyErr_NoMemory(); |
| 2165 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2168 | /* Issue 6012 */ |
| 2169 | static PyObject *str1, *str2; |
| 2170 | static int |
| 2171 | failing_converter(PyObject *obj, void *arg) |
| 2172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2173 | /* Clone str1, then let the conversion fail. */ |
| 2174 | assert(str1); |
| 2175 | str2 = str1; |
| 2176 | Py_INCREF(str2); |
| 2177 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2178 | } |
| 2179 | static PyObject* |
| 2180 | argparsing(PyObject *o, PyObject *args) |
| 2181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2182 | PyObject *res; |
| 2183 | str1 = str2 = NULL; |
| 2184 | if (!PyArg_ParseTuple(args, "O&O&", |
| 2185 | PyUnicode_FSConverter, &str1, |
| 2186 | failing_converter, &str2)) { |
| 2187 | if (!str2) |
| 2188 | /* argument converter not called? */ |
| 2189 | return NULL; |
| 2190 | /* Should be 1 */ |
| 2191 | res = PyLong_FromLong(Py_REFCNT(str2)); |
| 2192 | Py_DECREF(str2); |
| 2193 | PyErr_Clear(); |
| 2194 | return res; |
| 2195 | } |
| 2196 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2197 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2198 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2199 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 2200 | static PyObject * |
| 2201 | code_newempty(PyObject *self, PyObject *args) |
| 2202 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | const char *filename; |
| 2204 | const char *funcname; |
| 2205 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 2208 | &filename, &funcname, &firstlineno)) |
| 2209 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2211 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2214 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 2215 | Run via Lib/test/test_exceptions.py */ |
| 2216 | static PyObject * |
| 2217 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2218 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2219 | const char *name; |
| 2220 | const char *doc = NULL; |
| 2221 | PyObject *base = NULL; |
| 2222 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2226 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2227 | "s|sOO:make_exception_with_doc", kwlist, |
| 2228 | &name, &doc, &base, &dict)) |
| 2229 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2231 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2234 | /* Test that the fatal error from not having a current thread doesn't |
| 2235 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 2236 | static PyObject * |
| 2237 | crash_no_current_thread(PyObject *self) |
| 2238 | { |
| 2239 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 2240 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 2241 | !pydebug mode. However, the test only actually tests anything |
| 2242 | in pydebug mode, since that's where the infinite loop was in |
| 2243 | the first place. */ |
| 2244 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2245 | Py_END_ALLOW_THREADS |
| 2246 | return NULL; |
| 2247 | } |
| 2248 | |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 2249 | static PyObject * |
| 2250 | format_unicode(PyObject *self, PyObject *args) |
| 2251 | { |
| 2252 | const char *format; |
| 2253 | PyObject *arg; |
| 2254 | if (!PyArg_ParseTuple(args, "yU", &format, &arg)) |
| 2255 | return NULL; |
| 2256 | return PyUnicode_FromFormat(format, arg); |
| 2257 | |
| 2258 | } |
| 2259 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2260 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 2262 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
| 2263 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 2264 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
| 2265 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 2266 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 2267 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
| 2268 | {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, |
| 2269 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
| 2270 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 2271 | METH_NOARGS}, |
| 2272 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 2273 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 2274 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
| 2275 | {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS}, |
| 2276 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 2277 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 2278 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 2279 | PyDoc_STR("This is a pretty normal docstring.")}, |
| 2280 | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
| 2281 | {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, |
| 2282 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
| 2283 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 2284 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 2285 | METH_VARARGS|METH_KEYWORDS}, |
| 2286 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 2287 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 2288 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 2289 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 2290 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 2291 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 2292 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 2293 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 2294 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2295 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 2297 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 2298 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 2299 | {"test_long_long_and_overflow", |
| 2300 | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
| 2301 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 2302 | #endif |
| 2303 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 2304 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 2305 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 2306 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 2307 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 2308 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 2309 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 2310 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 2311 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 2312 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 2313 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 2314 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 2315 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2316 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2318 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2320 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
| 2322 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 2323 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
| 2324 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 2325 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 2326 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2327 | #ifdef WITH_THREAD |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2328 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2329 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2330 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2331 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2332 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2333 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2334 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 2335 | {"exception_print", exception_print, METH_VARARGS}, |
| 2336 | {"argparsing", argparsing, METH_VARARGS}, |
| 2337 | {"code_newempty", code_newempty, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 2339 | METH_VARARGS | METH_KEYWORDS}, |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2340 | {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2341 | {"format_unicode", format_unicode, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2343 | }; |
| 2344 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2345 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 2346 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2347 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | char bool_member; |
| 2349 | char byte_member; |
| 2350 | unsigned char ubyte_member; |
| 2351 | short short_member; |
| 2352 | unsigned short ushort_member; |
| 2353 | int int_member; |
| 2354 | unsigned int uint_member; |
| 2355 | long long_member; |
| 2356 | unsigned long ulong_member; |
| 2357 | Py_ssize_t pyssizet_member; |
| 2358 | float float_member; |
| 2359 | double double_member; |
| 2360 | char inplace_member[6]; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2361 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2362 | PY_LONG_LONG longlong_member; |
| 2363 | unsigned PY_LONG_LONG ulonglong_member; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2364 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2365 | } all_structmembers; |
| 2366 | |
| 2367 | typedef struct { |
| 2368 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2369 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2370 | } test_structmembers; |
| 2371 | |
| 2372 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 2374 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 2375 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 2376 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 2377 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 2378 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 2379 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 2380 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 2381 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 2382 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 2383 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 2384 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 2385 | {"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] | 2386 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 2388 | {"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] | 2389 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2391 | }; |
| 2392 | |
| 2393 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2394 | static PyObject * |
| 2395 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 2396 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | static char *keywords[] = { |
| 2398 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 2399 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 2400 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 2401 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2402 | "T_LONGLONG", "T_ULONGLONG", |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2403 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2404 | NULL}; |
| 2405 | static char *fmt = "|bbBhHiIlknfds#" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2406 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | "LK" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2408 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2409 | ; |
| 2410 | test_structmembers *ob; |
| 2411 | const char *s = NULL; |
| 2412 | Py_ssize_t string_len = 0; |
| 2413 | ob = PyObject_New(test_structmembers, type); |
| 2414 | if (ob == NULL) |
| 2415 | return NULL; |
| 2416 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 2417 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 2418 | &ob->structmembers.bool_member, |
| 2419 | &ob->structmembers.byte_member, |
| 2420 | &ob->structmembers.ubyte_member, |
| 2421 | &ob->structmembers.short_member, |
| 2422 | &ob->structmembers.ushort_member, |
| 2423 | &ob->structmembers.int_member, |
| 2424 | &ob->structmembers.uint_member, |
| 2425 | &ob->structmembers.long_member, |
| 2426 | &ob->structmembers.ulong_member, |
| 2427 | &ob->structmembers.pyssizet_member, |
| 2428 | &ob->structmembers.float_member, |
| 2429 | &ob->structmembers.double_member, |
| 2430 | &s, &string_len |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2431 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2432 | , &ob->structmembers.longlong_member, |
| 2433 | &ob->structmembers.ulonglong_member |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2434 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | )) { |
| 2436 | Py_DECREF(ob); |
| 2437 | return NULL; |
| 2438 | } |
| 2439 | if (s != NULL) { |
| 2440 | if (string_len > 5) { |
| 2441 | Py_DECREF(ob); |
| 2442 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | strcpy(ob->structmembers.inplace_member, s); |
| 2446 | } |
| 2447 | else { |
| 2448 | strcpy(ob->structmembers.inplace_member, ""); |
| 2449 | } |
| 2450 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2453 | static void |
| 2454 | test_structmembers_free(PyObject *ob) |
| 2455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2456 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2460 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 | "test_structmembersType", |
| 2462 | sizeof(test_structmembers), /* tp_basicsize */ |
| 2463 | 0, /* tp_itemsize */ |
| 2464 | test_structmembers_free, /* destructor tp_dealloc */ |
| 2465 | 0, /* tp_print */ |
| 2466 | 0, /* tp_getattr */ |
| 2467 | 0, /* tp_setattr */ |
| 2468 | 0, /* tp_reserved */ |
| 2469 | 0, /* tp_repr */ |
| 2470 | 0, /* tp_as_number */ |
| 2471 | 0, /* tp_as_sequence */ |
| 2472 | 0, /* tp_as_mapping */ |
| 2473 | 0, /* tp_hash */ |
| 2474 | 0, /* tp_call */ |
| 2475 | 0, /* tp_str */ |
| 2476 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2477 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 2478 | 0, /* tp_as_buffer */ |
| 2479 | 0, /* tp_flags */ |
| 2480 | "Type containing all structmember types", |
| 2481 | 0, /* traverseproc tp_traverse */ |
| 2482 | 0, /* tp_clear */ |
| 2483 | 0, /* tp_richcompare */ |
| 2484 | 0, /* tp_weaklistoffset */ |
| 2485 | 0, /* tp_iter */ |
| 2486 | 0, /* tp_iternext */ |
| 2487 | 0, /* tp_methods */ |
| 2488 | test_members, /* tp_members */ |
| 2489 | 0, |
| 2490 | 0, |
| 2491 | 0, |
| 2492 | 0, |
| 2493 | 0, |
| 2494 | 0, |
| 2495 | 0, |
| 2496 | 0, |
| 2497 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2498 | }; |
| 2499 | |
| 2500 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2501 | |
| 2502 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2503 | PyModuleDef_HEAD_INIT, |
| 2504 | "_testcapi", |
| 2505 | NULL, |
| 2506 | -1, |
| 2507 | TestMethods, |
| 2508 | NULL, |
| 2509 | NULL, |
| 2510 | NULL, |
| 2511 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2512 | }; |
| 2513 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 2514 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2515 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2517 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2519 | m = PyModule_Create(&_testcapimodule); |
| 2520 | if (m == NULL) |
| 2521 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
| 2524 | Py_TYPE(&_MemoryViewTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 2525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 2527 | Py_INCREF(&test_structmembersType); |
| 2528 | /* don't use a name starting with "test", since we don't want |
| 2529 | test_capi to automatically call this */ |
| 2530 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2532 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 2533 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 2534 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 2535 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 2536 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 2537 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 2538 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 2539 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 2540 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 2541 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 2542 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 2543 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 2544 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 2545 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 2546 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 2547 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 2548 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 2549 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 2550 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 2551 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 2552 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
| 2553 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
| 2554 | Py_INCREF(&PyInstanceMethod_Type); |
| 2555 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2557 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 2558 | Py_INCREF(TestError); |
| 2559 | PyModule_AddObject(m, "error", TestError); |
| 2560 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2561 | } |