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