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