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 | ab1d16b | 2011-11-22 01:45:37 +0100 | [diff] [blame] | 1441 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 1442 | { |
| 1443 | Py_UNICODE *unicode; |
| 1444 | Py_ssize_t length; |
| 1445 | char *errors = NULL; |
| 1446 | PyObject *decimal; |
| 1447 | Py_ssize_t decimal_length, new_length; |
| 1448 | int res; |
| 1449 | |
| 1450 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 1451 | return NULL; |
| 1452 | |
| 1453 | decimal_length = length * 7; /* len('€') */ |
| 1454 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 1455 | if (decimal == NULL) |
| 1456 | return NULL; |
| 1457 | |
| 1458 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 1459 | PyBytes_AS_STRING(decimal), |
| 1460 | errors); |
| 1461 | if (res < 0) { |
| 1462 | Py_DECREF(decimal); |
| 1463 | return NULL; |
| 1464 | } |
| 1465 | |
| 1466 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 1467 | assert(new_length <= decimal_length); |
| 1468 | res = _PyBytes_Resize(&decimal, new_length); |
| 1469 | if (res < 0) |
| 1470 | return NULL; |
| 1471 | |
| 1472 | return decimal; |
| 1473 | } |
| 1474 | |
| 1475 | static PyObject * |
| 1476 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 1477 | { |
| 1478 | Py_UNICODE *unicode; |
| 1479 | Py_ssize_t length; |
| 1480 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 1481 | return NULL; |
| 1482 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 1483 | } |
| 1484 | |
| 1485 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1486 | getargs_w_star(PyObject *self, PyObject *args) |
| 1487 | { |
| 1488 | Py_buffer buffer; |
| 1489 | PyObject *result; |
| 1490 | char *str; |
| 1491 | |
| 1492 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 1493 | return NULL; |
| 1494 | |
| 1495 | if (2 <= buffer.len) { |
| 1496 | str = buffer.buf; |
| 1497 | str[0] = '['; |
| 1498 | str[buffer.len-1] = ']'; |
| 1499 | } |
| 1500 | |
| 1501 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1502 | PyBuffer_Release(&buffer); |
| 1503 | return result; |
| 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1508 | test_empty_argparse(PyObject *self) |
| 1509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 1511 | PyObject *tuple, *dict = NULL; |
| 1512 | static char *kwlist[] = {NULL}; |
| 1513 | int result; |
| 1514 | tuple = PyTuple_New(0); |
| 1515 | if (!tuple) |
| 1516 | return NULL; |
| 1517 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 1518 | goto done; |
| 1519 | dict = PyDict_New(); |
| 1520 | if (!dict) |
| 1521 | goto done; |
| 1522 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1523 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | Py_DECREF(tuple); |
| 1525 | Py_XDECREF(dict); |
| 1526 | if (result < 0) |
| 1527 | return NULL; |
| 1528 | else { |
| 1529 | Py_RETURN_NONE; |
| 1530 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1534 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1535 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | const char *encoding, *errors = NULL; |
| 1537 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 1538 | &encoding, &errors)) |
| 1539 | return NULL; |
| 1540 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1543 | static PyObject * |
| 1544 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1545 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | const char *encoding, *errors = NULL; |
| 1547 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 1548 | &encoding, &errors)) |
| 1549 | return NULL; |
| 1550 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1553 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1554 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1555 | static PyObject * |
| 1556 | test_long_numbits(PyObject *self) |
| 1557 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | struct triple { |
| 1559 | long input; |
| 1560 | size_t nbits; |
| 1561 | int sign; |
| 1562 | } testcases[] = {{0, 0, 0}, |
| 1563 | {1L, 1, 1}, |
| 1564 | {-1L, 1, -1}, |
| 1565 | {2L, 2, 1}, |
| 1566 | {-2L, 2, -1}, |
| 1567 | {3L, 2, 1}, |
| 1568 | {-3L, 2, -1}, |
| 1569 | {4L, 3, 1}, |
| 1570 | {-4L, 3, -1}, |
| 1571 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 1572 | {-0x7fffL, 15, -1}, |
| 1573 | {0xffffL, 16, 1}, |
| 1574 | {-0xffffL, 16, -1}, |
| 1575 | {0xfffffffL, 28, 1}, |
| 1576 | {-0xfffffffL, 28, -1}}; |
| 1577 | int i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) { |
| 1580 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
| 1581 | size_t nbits = _PyLong_NumBits(plong); |
| 1582 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | Py_DECREF(plong); |
| 1585 | if (nbits != testcases[i].nbits) |
| 1586 | return raiseTestError("test_long_numbits", |
| 1587 | "wrong result for _PyLong_NumBits"); |
| 1588 | if (sign != testcases[i].sign) |
| 1589 | return raiseTestError("test_long_numbits", |
| 1590 | "wrong result for _PyLong_Sign"); |
| 1591 | } |
| 1592 | Py_INCREF(Py_None); |
| 1593 | return Py_None; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1596 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1597 | |
| 1598 | static PyObject * |
| 1599 | test_null_strings(PyObject *self) |
| 1600 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 1602 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 1603 | Py_XDECREF(o1); |
| 1604 | Py_XDECREF(o2); |
| 1605 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1608 | static PyObject * |
| 1609 | raise_exception(PyObject *self, PyObject *args) |
| 1610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | PyObject *exc; |
| 1612 | PyObject *exc_args, *v; |
| 1613 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1615 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1616 | &exc, &num_args)) |
| 1617 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | exc_args = PyTuple_New(num_args); |
| 1620 | if (exc_args == NULL) |
| 1621 | return NULL; |
| 1622 | for (i = 0; i < num_args; ++i) { |
| 1623 | v = PyLong_FromLong(i); |
| 1624 | if (v == NULL) { |
| 1625 | Py_DECREF(exc_args); |
| 1626 | return NULL; |
| 1627 | } |
| 1628 | PyTuple_SET_ITEM(exc_args, i, v); |
| 1629 | } |
| 1630 | PyErr_SetObject(exc, exc_args); |
| 1631 | Py_DECREF(exc_args); |
| 1632 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1633 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1634 | |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 1635 | |
| 1636 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1637 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1638 | static PyObject * |
| 1639 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | if (PyDateTimeAPI) { |
| 1641 | if (test_run_counter) { |
| 1642 | /* Probably regrtest.py -R */ |
| 1643 | Py_RETURN_NONE; |
| 1644 | } |
| 1645 | else { |
| 1646 | PyErr_SetString(PyExc_AssertionError, |
| 1647 | "PyDateTime_CAPI somehow initialized"); |
| 1648 | return NULL; |
| 1649 | } |
| 1650 | } |
| 1651 | test_run_counter++; |
| 1652 | PyDateTime_IMPORT; |
| 1653 | if (PyDateTimeAPI) |
| 1654 | Py_RETURN_NONE; |
| 1655 | else |
| 1656 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 1659 | |
| 1660 | #ifdef WITH_THREAD |
| 1661 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1662 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 1663 | * `thread_done` when it's finished. The driver code has to know when the |
| 1664 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 1665 | * may go away when the driver finishes. The former lack of this explicit |
| 1666 | * synchronization caused rare segfaults, so rare that they were seen only |
| 1667 | * on a Mac buildbot (although they were possible on any box). |
| 1668 | */ |
| 1669 | static PyThread_type_lock thread_done = NULL; |
| 1670 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1671 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1672 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | PyObject *rc; |
| 1675 | int success; |
| 1676 | PyGILState_STATE s = PyGILState_Ensure(); |
| 1677 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
| 1678 | success = (rc != NULL); |
| 1679 | Py_XDECREF(rc); |
| 1680 | PyGILState_Release(s); |
| 1681 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1684 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 1685 | * should be called only from threads spawned by test_thread_state(). |
| 1686 | */ |
| 1687 | static void |
| 1688 | _make_call_from_thread(void *callable) |
| 1689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | _make_call(callable); |
| 1691 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1694 | static PyObject * |
| 1695 | test_thread_state(PyObject *self, PyObject *args) |
| 1696 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | PyObject *fn; |
| 1698 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 1701 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | if (!PyCallable_Check(fn)) { |
| 1704 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1705 | fn->ob_type->tp_name); |
| 1706 | return NULL; |
| 1707 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | /* Ensure Python is set up for threading */ |
| 1710 | PyEval_InitThreads(); |
| 1711 | thread_done = PyThread_allocate_lock(); |
| 1712 | if (thread_done == NULL) |
| 1713 | return PyErr_NoMemory(); |
| 1714 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1715 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 | /* Start a new thread with our callback. */ |
| 1717 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1718 | /* Make the callback with the thread lock held by this thread */ |
| 1719 | success &= _make_call(fn); |
| 1720 | /* Do it all again, but this time with the thread-lock released */ |
| 1721 | Py_BEGIN_ALLOW_THREADS |
| 1722 | success &= _make_call(fn); |
| 1723 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1724 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | /* And once more with and without a thread |
| 1727 | XXX - should use a lock and work out exactly what we are trying |
| 1728 | to test <wink> |
| 1729 | */ |
| 1730 | Py_BEGIN_ALLOW_THREADS |
| 1731 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1732 | success &= _make_call(fn); |
| 1733 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1734 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 1737 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | PyThread_free_lock(thread_done); |
| 1740 | if (!success) |
| 1741 | return NULL; |
| 1742 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1743 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1744 | |
| 1745 | /* test Py_AddPendingCalls using threads */ |
| 1746 | static int _pending_callback(void *arg) |
| 1747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | /* we assume the argument is callable object to which we own a reference */ |
| 1749 | PyObject *callable = (PyObject *)arg; |
| 1750 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 1751 | Py_DECREF(callable); |
| 1752 | Py_XDECREF(r); |
| 1753 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /* The following requests n callbacks to _pending_callback. It can be |
| 1757 | * run from any python thread. |
| 1758 | */ |
| 1759 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | PyObject *callable; |
| 1762 | int r; |
| 1763 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1764 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | /* create the reference for the callbackwhile we hold the lock */ |
| 1767 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1769 | Py_BEGIN_ALLOW_THREADS |
| 1770 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1771 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | if (r<0) { |
| 1774 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1775 | Py_INCREF(Py_False); |
| 1776 | return Py_False; |
| 1777 | } |
| 1778 | Py_INCREF(Py_True); |
| 1779 | return Py_True; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1780 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1781 | #endif |
| 1782 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 1783 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1784 | static PyObject * |
| 1785 | test_string_from_format(PyObject *self, PyObject *args) |
| 1786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | PyObject *result; |
| 1788 | char *msg; |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1789 | static const Py_UNICODE one[] = {'1', 0}; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1790 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1791 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 1792 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 1793 | if (result == NULL) \ |
| 1794 | return NULL; \ |
| 1795 | if (Py_UNICODE_strcmp(PyUnicode_AS_UNICODE(result), one)) { \ |
| 1796 | msg = FORMAT " failed at 1"; \ |
| 1797 | goto Fail; \ |
| 1798 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | CHECK_1_FORMAT("%d", int); |
| 1802 | CHECK_1_FORMAT("%ld", long); |
| 1803 | /* The z width modifier was added in Python 2.5. */ |
| 1804 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1806 | /* The u type code was added in Python 2.5. */ |
| 1807 | CHECK_1_FORMAT("%u", unsigned int); |
| 1808 | CHECK_1_FORMAT("%lu", unsigned long); |
| 1809 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1812 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 1814 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1815 | #endif |
| 1816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1818 | |
| 1819 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | Py_XDECREF(result); |
| 1821 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1822 | |
| 1823 | #undef CHECK_1_FORMAT |
| 1824 | } |
| 1825 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 1826 | |
| 1827 | static PyObject * |
| 1828 | test_unicode_compare_with_ascii(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 1830 | int result; |
| 1831 | if (py_s == NULL) |
| 1832 | return NULL; |
| 1833 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 1834 | Py_DECREF(py_s); |
| 1835 | if (!result) { |
| 1836 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 1837 | "should not compare equal to c string."); |
| 1838 | return NULL; |
| 1839 | } |
| 1840 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 1841 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 1842 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1843 | /* This is here to provide a docstring for test_descr. */ |
| 1844 | static PyObject * |
| 1845 | test_with_docstring(PyObject *self) |
| 1846 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1850 | /* Test PyOS_string_to_double. */ |
| 1851 | static PyObject * |
| 1852 | test_string_to_double(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 | double result; |
| 1854 | char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | #define CHECK_STRING(STR, expected) \ |
| 1857 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1858 | if (result == -1.0 && PyErr_Occurred()) \ |
| 1859 | return NULL; \ |
| 1860 | if (result != expected) { \ |
| 1861 | msg = "conversion of " STR " to float failed"; \ |
| 1862 | goto fail; \ |
| 1863 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | #define CHECK_INVALID(STR) \ |
| 1866 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1867 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 1868 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 1869 | PyErr_Clear(); \ |
| 1870 | else \ |
| 1871 | return NULL; \ |
| 1872 | } \ |
| 1873 | else { \ |
| 1874 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 1875 | goto fail; \ |
| 1876 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | CHECK_STRING("0.1", 0.1); |
| 1879 | CHECK_STRING("1.234", 1.234); |
| 1880 | CHECK_STRING("-1.35", -1.35); |
| 1881 | CHECK_STRING(".1e01", 1.0); |
| 1882 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1884 | CHECK_INVALID(" 0.1"); |
| 1885 | CHECK_INVALID("\t\n-3"); |
| 1886 | CHECK_INVALID(".123 "); |
| 1887 | CHECK_INVALID("3\n"); |
| 1888 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1891 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1893 | #undef CHECK_STRING |
| 1894 | #undef CHECK_INVALID |
| 1895 | } |
| 1896 | |
| 1897 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1898 | /* Coverage testing of capsule objects. */ |
| 1899 | |
| 1900 | static const char *capsule_name = "capsule name"; |
| 1901 | static char *capsule_pointer = "capsule pointer"; |
| 1902 | static char *capsule_context = "capsule context"; |
| 1903 | static const char *capsule_error = NULL; |
| 1904 | static int |
| 1905 | capsule_destructor_call_count = 0; |
| 1906 | |
| 1907 | static void |
| 1908 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | capsule_destructor_call_count++; |
| 1910 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 1911 | capsule_error = "context did not match in destructor!"; |
| 1912 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 1913 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 1914 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 1915 | capsule_error = "name did not match in destructor!"; |
| 1916 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 1917 | capsule_error = "pointer did not match in destructor!"; |
| 1918 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1922 | char *name; |
| 1923 | char *module; |
| 1924 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1925 | } known_capsule; |
| 1926 | |
| 1927 | static PyObject * |
| 1928 | test_capsule(PyObject *self, PyObject *args) |
| 1929 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | PyObject *object; |
| 1931 | const char *error = NULL; |
| 1932 | void *pointer; |
| 1933 | void *pointer2; |
| 1934 | known_capsule known_capsules[] = { |
| 1935 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 1936 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 1937 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 1938 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 1939 | { NULL, NULL }, |
| 1940 | }; |
| 1941 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1942 | |
| 1943 | #define FAIL(x) { error = (x); goto exit; } |
| 1944 | |
| 1945 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1946 | if (capsule_error) { \ |
| 1947 | FAIL(capsule_error); \ |
| 1948 | } \ |
| 1949 | else if (!capsule_destructor_call_count) { \ |
| 1950 | FAIL("destructor not called!"); \ |
| 1951 | } \ |
| 1952 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 1955 | PyCapsule_SetContext(object, capsule_context); |
| 1956 | capsule_destructor(object); |
| 1957 | CHECK_DESTRUCTOR; |
| 1958 | Py_DECREF(object); |
| 1959 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | object = PyCapsule_New(known, "ignored", NULL); |
| 1962 | PyCapsule_SetPointer(object, capsule_pointer); |
| 1963 | PyCapsule_SetName(object, capsule_name); |
| 1964 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 1965 | PyCapsule_SetContext(object, capsule_context); |
| 1966 | capsule_destructor(object); |
| 1967 | CHECK_DESTRUCTOR; |
| 1968 | /* intentionally access using the wrong name */ |
| 1969 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 1970 | if (!PyErr_Occurred()) { |
| 1971 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 1972 | } |
| 1973 | PyErr_Clear(); |
| 1974 | if (pointer2) { |
| 1975 | if (pointer2 == capsule_pointer) { |
| 1976 | FAIL("PyCapsule_GetPointer should not have" |
| 1977 | " returned the internal pointer!"); |
| 1978 | } else { |
| 1979 | FAIL("PyCapsule_GetPointer should have " |
| 1980 | "returned NULL pointer but did not!"); |
| 1981 | } |
| 1982 | } |
| 1983 | PyCapsule_SetDestructor(object, NULL); |
| 1984 | Py_DECREF(object); |
| 1985 | if (capsule_destructor_call_count) { |
| 1986 | FAIL("destructor called when it should not have been!"); |
| 1987 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 1990 | /* yeah, ordinarily I wouldn't do this either, |
| 1991 | but it's fine for this test harness. |
| 1992 | */ |
| 1993 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1994 | #undef FAIL |
| 1995 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | { \ |
| 1997 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 1998 | x, known->module, known->attribute); \ |
| 1999 | error = buffer; \ |
| 2000 | goto exit; \ |
| 2001 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | PyObject *module = PyImport_ImportModule(known->module); |
| 2004 | if (module) { |
| 2005 | pointer = PyCapsule_Import(known->name, 0); |
| 2006 | if (!pointer) { |
| 2007 | Py_DECREF(module); |
| 2008 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 2009 | } |
| 2010 | object = PyObject_GetAttrString(module, known->attribute); |
| 2011 | if (!object) { |
| 2012 | Py_DECREF(module); |
| 2013 | return NULL; |
| 2014 | } |
| 2015 | pointer2 = PyCapsule_GetPointer(object, |
| 2016 | "weebles wobble but they don't fall down"); |
| 2017 | if (!PyErr_Occurred()) { |
| 2018 | Py_DECREF(object); |
| 2019 | Py_DECREF(module); |
| 2020 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2021 | } |
| 2022 | PyErr_Clear(); |
| 2023 | if (pointer2) { |
| 2024 | Py_DECREF(module); |
| 2025 | Py_DECREF(object); |
| 2026 | if (pointer2 == pointer) { |
| 2027 | FAIL("PyCapsule_GetPointer should not have" |
| 2028 | " returned its internal pointer!"); |
| 2029 | } else { |
| 2030 | FAIL("PyCapsule_GetPointer should have" |
| 2031 | " returned NULL pointer but did not!"); |
| 2032 | } |
| 2033 | } |
| 2034 | Py_DECREF(object); |
| 2035 | Py_DECREF(module); |
| 2036 | } |
| 2037 | else |
| 2038 | PyErr_Clear(); |
| 2039 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2040 | |
| 2041 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | if (error) { |
| 2043 | return raiseTestError("test_capsule", error); |
| 2044 | } |
| 2045 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2046 | #undef FAIL |
| 2047 | } |
| 2048 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2049 | #ifdef HAVE_GETTIMEOFDAY |
| 2050 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 2051 | 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] | 2052 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2053 | e->tv_sec -= s->tv_sec; |
| 2054 | e->tv_usec -= s->tv_usec; |
| 2055 | if (e->tv_usec < 0) { |
| 2056 | e->tv_sec -=1; |
| 2057 | e->tv_usec += 1000000; |
| 2058 | } |
| 2059 | 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] | 2060 | } |
| 2061 | |
| 2062 | static PyObject * |
| 2063 | profile_int(PyObject *self, PyObject* args) |
| 2064 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2065 | int i, k; |
| 2066 | struct timeval start, stop; |
| 2067 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | /* Test 1: Allocate and immediately deallocate |
| 2070 | many small integers */ |
| 2071 | gettimeofday(&start, NULL); |
| 2072 | for(k=0; k < 20000; k++) |
| 2073 | for(i=0; i < 1000; i++) { |
| 2074 | single = PyLong_FromLong(i); |
| 2075 | Py_DECREF(single); |
| 2076 | } |
| 2077 | gettimeofday(&stop, NULL); |
| 2078 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | /* Test 2: Allocate and immediately deallocate |
| 2081 | many large integers */ |
| 2082 | gettimeofday(&start, NULL); |
| 2083 | for(k=0; k < 20000; k++) |
| 2084 | for(i=0; i < 1000; i++) { |
| 2085 | single = PyLong_FromLong(i+1000000); |
| 2086 | Py_DECREF(single); |
| 2087 | } |
| 2088 | gettimeofday(&stop, NULL); |
| 2089 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | /* Test 3: Allocate a few integers, then release |
| 2092 | them all simultaneously. */ |
| 2093 | multiple = malloc(sizeof(PyObject*) * 1000); |
| 2094 | gettimeofday(&start, NULL); |
| 2095 | for(k=0; k < 20000; k++) { |
| 2096 | for(i=0; i < 1000; i++) { |
| 2097 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2098 | } |
| 2099 | for(i=0; i < 1000; i++) { |
| 2100 | Py_DECREF(multiple[i]); |
| 2101 | } |
| 2102 | } |
| 2103 | gettimeofday(&stop, NULL); |
| 2104 | print_delta(3, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | /* Test 4: Allocate many integers, then release |
| 2107 | them all simultaneously. */ |
| 2108 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 2109 | gettimeofday(&start, NULL); |
| 2110 | for(k=0; k < 20; k++) { |
| 2111 | for(i=0; i < 1000000; i++) { |
| 2112 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2113 | } |
| 2114 | for(i=0; i < 1000000; i++) { |
| 2115 | Py_DECREF(multiple[i]); |
| 2116 | } |
| 2117 | } |
| 2118 | gettimeofday(&stop, NULL); |
| 2119 | print_delta(4, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | /* Test 5: Allocate many integers < 32000 */ |
| 2122 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 2123 | gettimeofday(&start, NULL); |
| 2124 | for(k=0; k < 10; k++) { |
| 2125 | for(i=0; i < 1000000; i++) { |
| 2126 | multiple[i] = PyLong_FromLong(i+1000); |
| 2127 | } |
| 2128 | for(i=0; i < 1000000; i++) { |
| 2129 | Py_DECREF(multiple[i]); |
| 2130 | } |
| 2131 | } |
| 2132 | gettimeofday(&stop, NULL); |
| 2133 | print_delta(5, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | /* Test 6: Perform small int addition */ |
| 2136 | op1 = PyLong_FromLong(1); |
| 2137 | gettimeofday(&start, NULL); |
| 2138 | for(i=0; i < 10000000; i++) { |
| 2139 | result = PyNumber_Add(op1, op1); |
| 2140 | Py_DECREF(result); |
| 2141 | } |
| 2142 | gettimeofday(&stop, NULL); |
| 2143 | Py_DECREF(op1); |
| 2144 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | /* Test 7: Perform medium int addition */ |
| 2147 | op1 = PyLong_FromLong(1000); |
| 2148 | gettimeofday(&start, NULL); |
| 2149 | for(i=0; i < 10000000; i++) { |
| 2150 | result = PyNumber_Add(op1, op1); |
| 2151 | Py_DECREF(result); |
| 2152 | } |
| 2153 | gettimeofday(&stop, NULL); |
| 2154 | Py_DECREF(op1); |
| 2155 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2157 | Py_INCREF(Py_None); |
| 2158 | return Py_None; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2159 | } |
| 2160 | #endif |
| 2161 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2162 | /* To test the format of tracebacks as printed out. */ |
| 2163 | static PyObject * |
| 2164 | traceback_print(PyObject *self, PyObject *args) |
| 2165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | PyObject *file; |
| 2167 | PyObject *traceback; |
| 2168 | int result; |
| 2169 | |
| 2170 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 2171 | &traceback, &file)) |
| 2172 | return NULL; |
| 2173 | |
| 2174 | result = PyTraceBack_Print(traceback, file); |
| 2175 | if (result < 0) |
| 2176 | return NULL; |
| 2177 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2180 | /* To test the format of exceptions as printed out. */ |
| 2181 | static PyObject * |
| 2182 | exception_print(PyObject *self, PyObject *args) |
| 2183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | PyObject *value; |
| 2185 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 2188 | &value)) |
| 2189 | return NULL; |
| 2190 | if (!PyExceptionInstance_Check(value)) { |
| 2191 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 2192 | return NULL; |
| 2193 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2195 | tb = PyException_GetTraceback(value); |
| 2196 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 2197 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | |
| 2203 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2204 | |
| 2205 | /* reliably raise a MemoryError */ |
| 2206 | static PyObject * |
| 2207 | raise_memoryerror(PyObject *self) |
| 2208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | PyErr_NoMemory(); |
| 2210 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2213 | /* Issue 6012 */ |
| 2214 | static PyObject *str1, *str2; |
| 2215 | static int |
| 2216 | failing_converter(PyObject *obj, void *arg) |
| 2217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | /* Clone str1, then let the conversion fail. */ |
| 2219 | assert(str1); |
| 2220 | str2 = str1; |
| 2221 | Py_INCREF(str2); |
| 2222 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2223 | } |
| 2224 | static PyObject* |
| 2225 | argparsing(PyObject *o, PyObject *args) |
| 2226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | PyObject *res; |
| 2228 | str1 = str2 = NULL; |
| 2229 | if (!PyArg_ParseTuple(args, "O&O&", |
| 2230 | PyUnicode_FSConverter, &str1, |
| 2231 | failing_converter, &str2)) { |
| 2232 | if (!str2) |
| 2233 | /* argument converter not called? */ |
| 2234 | return NULL; |
| 2235 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2236 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | Py_DECREF(str2); |
| 2238 | PyErr_Clear(); |
| 2239 | return res; |
| 2240 | } |
| 2241 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2242 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2243 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2244 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 2245 | static PyObject * |
| 2246 | code_newempty(PyObject *self, PyObject *args) |
| 2247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | const char *filename; |
| 2249 | const char *funcname; |
| 2250 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 2253 | &filename, &funcname, &firstlineno)) |
| 2254 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2259 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 2260 | Run via Lib/test/test_exceptions.py */ |
| 2261 | static PyObject * |
| 2262 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | const char *name; |
| 2265 | const char *doc = NULL; |
| 2266 | PyObject *base = NULL; |
| 2267 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2272 | "s|sOO:make_exception_with_doc", kwlist, |
| 2273 | &name, &doc, &base, &dict)) |
| 2274 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Antoine Pitrou | 915605c | 2011-02-24 20:53:48 +0000 | [diff] [blame] | 2279 | static PyObject * |
| 2280 | make_memoryview_from_NULL_pointer(PyObject *self) |
| 2281 | { |
| 2282 | Py_buffer info; |
| 2283 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 2284 | return NULL; |
| 2285 | return PyMemoryView_FromBuffer(&info); |
| 2286 | } |
| 2287 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2288 | /* Test that the fatal error from not having a current thread doesn't |
| 2289 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 2290 | static PyObject * |
| 2291 | crash_no_current_thread(PyObject *self) |
| 2292 | { |
| 2293 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 2294 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 2295 | !pydebug mode. However, the test only actually tests anything |
| 2296 | in pydebug mode, since that's where the infinite loop was in |
| 2297 | the first place. */ |
| 2298 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2299 | Py_END_ALLOW_THREADS |
| 2300 | return NULL; |
| 2301 | } |
| 2302 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2303 | /* To run some code in a sub-interpreter. */ |
| 2304 | static PyObject * |
| 2305 | run_in_subinterp(PyObject *self, PyObject *args) |
| 2306 | { |
| 2307 | const char *code; |
| 2308 | int r; |
| 2309 | PyThreadState *substate, *mainstate; |
| 2310 | |
| 2311 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 2312 | &code)) |
| 2313 | return NULL; |
| 2314 | |
| 2315 | mainstate = PyThreadState_Get(); |
| 2316 | |
| 2317 | PyThreadState_Swap(NULL); |
| 2318 | |
| 2319 | substate = Py_NewInterpreter(); |
| 2320 | r = PyRun_SimpleString(code); |
| 2321 | Py_EndInterpreter(substate); |
| 2322 | |
| 2323 | PyThreadState_Swap(mainstate); |
| 2324 | |
| 2325 | return PyLong_FromLong(r); |
| 2326 | } |
| 2327 | |
| 2328 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2329 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 2331 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
| 2332 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 2333 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
| 2334 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 2335 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 2336 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
| 2337 | {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, |
| 2338 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
| 2339 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 2340 | METH_NOARGS}, |
| 2341 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 2342 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 2343 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
| 2344 | {"test_bug_7414", (PyCFunction)test_bug_7414, METH_NOARGS}, |
| 2345 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 2346 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 2347 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 2348 | PyDoc_STR("This is a pretty normal docstring.")}, |
| 2349 | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
| 2350 | {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, |
| 2351 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
| 2352 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 2353 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 2354 | METH_VARARGS|METH_KEYWORDS}, |
| 2355 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 2356 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 2357 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 2358 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 2359 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 2360 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 2361 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 2362 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 2363 | {"getargs_n", getargs_n, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2364 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 2366 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 2367 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 2368 | {"test_long_long_and_overflow", |
| 2369 | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
| 2370 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 2371 | #endif |
| 2372 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 2373 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 2374 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 2375 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 2376 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 2377 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 2378 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 2379 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 2380 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 2381 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 2382 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 2383 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 2384 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2385 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2387 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2389 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
| 2391 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 2392 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
| 2393 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Victor Stinner | ab1d16b | 2011-11-22 01:45:37 +0100 | [diff] [blame] | 2394 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 2395 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
| 2396 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 2397 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2398 | #ifdef WITH_THREAD |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2399 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2401 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2402 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2403 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2404 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2405 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 2406 | {"exception_print", exception_print, METH_VARARGS}, |
| 2407 | {"argparsing", argparsing, METH_VARARGS}, |
| 2408 | {"code_newempty", code_newempty, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2409 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 2410 | METH_VARARGS | METH_KEYWORDS}, |
Antoine Pitrou | 915605c | 2011-02-24 20:53:48 +0000 | [diff] [blame] | 2411 | {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer, |
| 2412 | METH_NOARGS}, |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2413 | {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2414 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2416 | }; |
| 2417 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2418 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 2419 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2420 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | char bool_member; |
| 2422 | char byte_member; |
| 2423 | unsigned char ubyte_member; |
| 2424 | short short_member; |
| 2425 | unsigned short ushort_member; |
| 2426 | int int_member; |
| 2427 | unsigned int uint_member; |
| 2428 | long long_member; |
| 2429 | unsigned long ulong_member; |
| 2430 | Py_ssize_t pyssizet_member; |
| 2431 | float float_member; |
| 2432 | double double_member; |
| 2433 | char inplace_member[6]; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2434 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | PY_LONG_LONG longlong_member; |
| 2436 | unsigned PY_LONG_LONG ulonglong_member; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2437 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2438 | } all_structmembers; |
| 2439 | |
| 2440 | typedef struct { |
| 2441 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2442 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2443 | } test_structmembers; |
| 2444 | |
| 2445 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2446 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 2447 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 2448 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 2449 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 2450 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 2451 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 2452 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 2453 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 2454 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 2455 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 2456 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 2457 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 2458 | {"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] | 2459 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 2461 | {"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] | 2462 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2464 | }; |
| 2465 | |
| 2466 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2467 | static PyObject * |
| 2468 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 2469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2470 | static char *keywords[] = { |
| 2471 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 2472 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 2473 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 2474 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | "T_LONGLONG", "T_ULONGLONG", |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2476 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2477 | NULL}; |
| 2478 | static char *fmt = "|bbBhHiIlknfds#" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2479 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | "LK" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2481 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | ; |
| 2483 | test_structmembers *ob; |
| 2484 | const char *s = NULL; |
| 2485 | Py_ssize_t string_len = 0; |
| 2486 | ob = PyObject_New(test_structmembers, type); |
| 2487 | if (ob == NULL) |
| 2488 | return NULL; |
| 2489 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 2490 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 2491 | &ob->structmembers.bool_member, |
| 2492 | &ob->structmembers.byte_member, |
| 2493 | &ob->structmembers.ubyte_member, |
| 2494 | &ob->structmembers.short_member, |
| 2495 | &ob->structmembers.ushort_member, |
| 2496 | &ob->structmembers.int_member, |
| 2497 | &ob->structmembers.uint_member, |
| 2498 | &ob->structmembers.long_member, |
| 2499 | &ob->structmembers.ulong_member, |
| 2500 | &ob->structmembers.pyssizet_member, |
| 2501 | &ob->structmembers.float_member, |
| 2502 | &ob->structmembers.double_member, |
| 2503 | &s, &string_len |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2504 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2505 | , &ob->structmembers.longlong_member, |
| 2506 | &ob->structmembers.ulonglong_member |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2507 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2508 | )) { |
| 2509 | Py_DECREF(ob); |
| 2510 | return NULL; |
| 2511 | } |
| 2512 | if (s != NULL) { |
| 2513 | if (string_len > 5) { |
| 2514 | Py_DECREF(ob); |
| 2515 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 2516 | return NULL; |
| 2517 | } |
| 2518 | strcpy(ob->structmembers.inplace_member, s); |
| 2519 | } |
| 2520 | else { |
| 2521 | strcpy(ob->structmembers.inplace_member, ""); |
| 2522 | } |
| 2523 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2526 | static void |
| 2527 | test_structmembers_free(PyObject *ob) |
| 2528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2533 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2534 | "test_structmembersType", |
| 2535 | sizeof(test_structmembers), /* tp_basicsize */ |
| 2536 | 0, /* tp_itemsize */ |
| 2537 | test_structmembers_free, /* destructor tp_dealloc */ |
| 2538 | 0, /* tp_print */ |
| 2539 | 0, /* tp_getattr */ |
| 2540 | 0, /* tp_setattr */ |
| 2541 | 0, /* tp_reserved */ |
| 2542 | 0, /* tp_repr */ |
| 2543 | 0, /* tp_as_number */ |
| 2544 | 0, /* tp_as_sequence */ |
| 2545 | 0, /* tp_as_mapping */ |
| 2546 | 0, /* tp_hash */ |
| 2547 | 0, /* tp_call */ |
| 2548 | 0, /* tp_str */ |
| 2549 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2550 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 2551 | 0, /* tp_as_buffer */ |
| 2552 | 0, /* tp_flags */ |
| 2553 | "Type containing all structmember types", |
| 2554 | 0, /* traverseproc tp_traverse */ |
| 2555 | 0, /* tp_clear */ |
| 2556 | 0, /* tp_richcompare */ |
| 2557 | 0, /* tp_weaklistoffset */ |
| 2558 | 0, /* tp_iter */ |
| 2559 | 0, /* tp_iternext */ |
| 2560 | 0, /* tp_methods */ |
| 2561 | test_members, /* tp_members */ |
| 2562 | 0, |
| 2563 | 0, |
| 2564 | 0, |
| 2565 | 0, |
| 2566 | 0, |
| 2567 | 0, |
| 2568 | 0, |
| 2569 | 0, |
| 2570 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2571 | }; |
| 2572 | |
| 2573 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2574 | |
| 2575 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2576 | PyModuleDef_HEAD_INIT, |
| 2577 | "_testcapi", |
| 2578 | NULL, |
| 2579 | -1, |
| 2580 | TestMethods, |
| 2581 | NULL, |
| 2582 | NULL, |
| 2583 | NULL, |
| 2584 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2585 | }; |
| 2586 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 2587 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2588 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2589 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2590 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | m = PyModule_Create(&_testcapimodule); |
| 2593 | if (m == NULL) |
| 2594 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
| 2597 | Py_TYPE(&_MemoryViewTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 2598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2599 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 2600 | Py_INCREF(&test_structmembersType); |
| 2601 | /* don't use a name starting with "test", since we don't want |
| 2602 | test_capi to automatically call this */ |
| 2603 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 2606 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 2607 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 2608 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 2609 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 2610 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 2611 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 2612 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 2613 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 2614 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 2615 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 2616 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 2617 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 2618 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 2619 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 2620 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 2621 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 2622 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 2623 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 2624 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 2625 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
| 2626 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
| 2627 | Py_INCREF(&PyInstanceMethod_Type); |
| 2628 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2630 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 2631 | Py_INCREF(TestError); |
| 2632 | PyModule_AddObject(m, "error", TestError); |
| 2633 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2634 | } |