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