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 */ |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 804 | static PyObject * |
| 805 | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 808 | static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 809 | 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] | 810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 812 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], |
| 813 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) |
| 814 | return NULL; |
| 815 | return Py_BuildValue("iiiiiiiiii", |
| 816 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 817 | 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] | 818 | } |
| 819 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 820 | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ |
| 821 | static PyObject * |
| 822 | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) |
| 823 | { |
| 824 | static char *keywords[] = {"required", "optional", "keyword_only", NULL}; |
| 825 | int required = -1; |
| 826 | int optional = -1; |
| 827 | int keyword_only = -1; |
| 828 | |
| 829 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, |
| 830 | &required, &optional, &keyword_only)) |
| 831 | return NULL; |
| 832 | return Py_BuildValue("iii", required, optional, keyword_only); |
| 833 | } |
| 834 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 835 | /* Functions to call PyArg_ParseTuple with integer format codes, |
| 836 | and return the result. |
| 837 | */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 838 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 839 | getargs_b(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | unsigned char value; |
| 842 | if (!PyArg_ParseTuple(args, "b", &value)) |
| 843 | return NULL; |
| 844 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 847 | static PyObject * |
| 848 | getargs_B(PyObject *self, PyObject *args) |
| 849 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | unsigned char value; |
| 851 | if (!PyArg_ParseTuple(args, "B", &value)) |
| 852 | return NULL; |
| 853 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static PyObject * |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 857 | getargs_h(PyObject *self, PyObject *args) |
| 858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | short value; |
| 860 | if (!PyArg_ParseTuple(args, "h", &value)) |
| 861 | return NULL; |
| 862 | return PyLong_FromLong((long)value); |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 866 | getargs_H(PyObject *self, PyObject *args) |
| 867 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | unsigned short value; |
| 869 | if (!PyArg_ParseTuple(args, "H", &value)) |
| 870 | return NULL; |
| 871 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static PyObject * |
| 875 | getargs_I(PyObject *self, PyObject *args) |
| 876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | unsigned int value; |
| 878 | if (!PyArg_ParseTuple(args, "I", &value)) |
| 879 | return NULL; |
| 880 | return PyLong_FromUnsignedLong((unsigned long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static PyObject * |
| 884 | getargs_k(PyObject *self, PyObject *args) |
| 885 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | unsigned long value; |
| 887 | if (!PyArg_ParseTuple(args, "k", &value)) |
| 888 | return NULL; |
| 889 | return PyLong_FromUnsignedLong(value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static PyObject * |
| 893 | getargs_i(PyObject *self, PyObject *args) |
| 894 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | int value; |
| 896 | if (!PyArg_ParseTuple(args, "i", &value)) |
| 897 | return NULL; |
| 898 | return PyLong_FromLong((long)value); |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 901 | static PyObject * |
| 902 | getargs_l(PyObject *self, PyObject *args) |
| 903 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | long value; |
| 905 | if (!PyArg_ParseTuple(args, "l", &value)) |
| 906 | return NULL; |
| 907 | return PyLong_FromLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 910 | static PyObject * |
| 911 | getargs_n(PyObject *self, PyObject *args) |
| 912 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | Py_ssize_t value; |
| 914 | if (!PyArg_ParseTuple(args, "n", &value)) |
| 915 | return NULL; |
| 916 | return PyLong_FromSsize_t(value); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 919 | static PyObject * |
| 920 | getargs_p(PyObject *self, PyObject *args) |
| 921 | { |
| 922 | int value; |
| 923 | if (!PyArg_ParseTuple(args, "p", &value)) |
| 924 | return NULL; |
| 925 | return PyLong_FromLong(value); |
| 926 | } |
| 927 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 928 | #ifdef HAVE_LONG_LONG |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 929 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 930 | getargs_L(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 931 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | PY_LONG_LONG value; |
| 933 | if (!PyArg_ParseTuple(args, "L", &value)) |
| 934 | return NULL; |
| 935 | return PyLong_FromLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 938 | static PyObject * |
Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 939 | getargs_K(PyObject *self, PyObject *args) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 940 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | unsigned PY_LONG_LONG value; |
| 942 | if (!PyArg_ParseTuple(args, "K", &value)) |
| 943 | return NULL; |
| 944 | return PyLong_FromUnsignedLongLong(value); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 945 | } |
| 946 | #endif |
| 947 | |
| 948 | /* This function not only tests the 'k' getargs code, but also the |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 949 | PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 950 | static PyObject * |
| 951 | test_k_code(PyObject *self) |
| 952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | PyObject *tuple, *num; |
| 954 | unsigned long value; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | tuple = PyTuple_New(1); |
| 957 | if (tuple == NULL) |
| 958 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | /* a number larger than ULONG_MAX even on 64-bit platforms */ |
| 961 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); |
| 962 | if (num == NULL) |
| 963 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | value = PyLong_AsUnsignedLongMask(num); |
| 966 | if (value != ULONG_MAX) |
| 967 | return raiseTestError("test_k_code", |
| 968 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | value = 0; |
| 973 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 974 | return NULL; |
| 975 | if (value != ULONG_MAX) |
| 976 | return raiseTestError("test_k_code", |
| 977 | "k code returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | Py_DECREF(num); |
| 980 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); |
| 981 | if (num == NULL) |
| 982 | return NULL; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | value = PyLong_AsUnsignedLongMask(num); |
| 985 | if (value != (unsigned long)-0x42) |
| 986 | return raiseTestError("test_k_code", |
| 987 | "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | PyTuple_SET_ITEM(tuple, 0, num); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | value = 0; |
| 992 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) |
| 993 | return NULL; |
| 994 | if (value != (unsigned long)-0x42) |
| 995 | return raiseTestError("test_k_code", |
| 996 | "k code returned wrong value for long -0xFFF..000042"); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | Py_DECREF(tuple); |
| 999 | Py_INCREF(Py_None); |
| 1000 | return Py_None; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1003 | static PyObject * |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1004 | getargs_c(PyObject *self, PyObject *args) |
| 1005 | { |
| 1006 | char c; |
| 1007 | if (!PyArg_ParseTuple(args, "c", &c)) |
| 1008 | return NULL; |
| 1009 | return PyBytes_FromStringAndSize(&c, 1); |
| 1010 | } |
| 1011 | |
| 1012 | static PyObject * |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1013 | getargs_s(PyObject *self, PyObject *args) |
| 1014 | { |
| 1015 | char *str; |
| 1016 | if (!PyArg_ParseTuple(args, "s", &str)) |
| 1017 | return NULL; |
| 1018 | return PyBytes_FromString(str); |
| 1019 | } |
| 1020 | |
| 1021 | static PyObject * |
| 1022 | getargs_s_star(PyObject *self, PyObject *args) |
| 1023 | { |
| 1024 | Py_buffer buffer; |
| 1025 | PyObject *bytes; |
| 1026 | if (!PyArg_ParseTuple(args, "s*", &buffer)) |
| 1027 | return NULL; |
| 1028 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1029 | PyBuffer_Release(&buffer); |
| 1030 | return bytes; |
| 1031 | } |
| 1032 | |
| 1033 | static PyObject * |
| 1034 | getargs_s_hash(PyObject *self, PyObject *args) |
| 1035 | { |
| 1036 | char *str; |
| 1037 | Py_ssize_t size; |
| 1038 | if (!PyArg_ParseTuple(args, "s#", &str, &size)) |
| 1039 | return NULL; |
| 1040 | return PyBytes_FromStringAndSize(str, size); |
| 1041 | } |
| 1042 | |
| 1043 | static PyObject * |
| 1044 | getargs_z(PyObject *self, PyObject *args) |
| 1045 | { |
| 1046 | char *str; |
| 1047 | if (!PyArg_ParseTuple(args, "z", &str)) |
| 1048 | return NULL; |
| 1049 | if (str != NULL) |
| 1050 | return PyBytes_FromString(str); |
| 1051 | else |
| 1052 | Py_RETURN_NONE; |
| 1053 | } |
| 1054 | |
| 1055 | static PyObject * |
| 1056 | getargs_z_star(PyObject *self, PyObject *args) |
| 1057 | { |
| 1058 | Py_buffer buffer; |
| 1059 | PyObject *bytes; |
| 1060 | if (!PyArg_ParseTuple(args, "z*", &buffer)) |
| 1061 | return NULL; |
| 1062 | if (buffer.buf != NULL) |
| 1063 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1064 | else { |
| 1065 | Py_INCREF(Py_None); |
| 1066 | bytes = Py_None; |
| 1067 | } |
| 1068 | PyBuffer_Release(&buffer); |
| 1069 | return bytes; |
| 1070 | } |
| 1071 | |
| 1072 | static PyObject * |
| 1073 | getargs_z_hash(PyObject *self, PyObject *args) |
| 1074 | { |
| 1075 | char *str; |
| 1076 | Py_ssize_t size; |
| 1077 | if (!PyArg_ParseTuple(args, "z#", &str, &size)) |
| 1078 | return NULL; |
| 1079 | if (str != NULL) |
| 1080 | return PyBytes_FromStringAndSize(str, size); |
| 1081 | else |
| 1082 | Py_RETURN_NONE; |
| 1083 | } |
| 1084 | |
| 1085 | static PyObject * |
| 1086 | getargs_y(PyObject *self, PyObject *args) |
| 1087 | { |
| 1088 | char *str; |
| 1089 | if (!PyArg_ParseTuple(args, "y", &str)) |
| 1090 | return NULL; |
| 1091 | return PyBytes_FromString(str); |
| 1092 | } |
| 1093 | |
| 1094 | static PyObject * |
| 1095 | getargs_y_star(PyObject *self, PyObject *args) |
| 1096 | { |
| 1097 | Py_buffer buffer; |
| 1098 | PyObject *bytes; |
| 1099 | if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 1100 | return NULL; |
| 1101 | bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1102 | PyBuffer_Release(&buffer); |
| 1103 | return bytes; |
| 1104 | } |
| 1105 | |
| 1106 | static PyObject * |
| 1107 | getargs_y_hash(PyObject *self, PyObject *args) |
| 1108 | { |
| 1109 | char *str; |
| 1110 | Py_ssize_t size; |
| 1111 | if (!PyArg_ParseTuple(args, "y#", &str, &size)) |
| 1112 | return NULL; |
| 1113 | return PyBytes_FromStringAndSize(str, size); |
| 1114 | } |
| 1115 | |
| 1116 | static PyObject * |
| 1117 | getargs_u(PyObject *self, PyObject *args) |
| 1118 | { |
| 1119 | Py_UNICODE *str; |
| 1120 | Py_ssize_t size; |
| 1121 | if (!PyArg_ParseTuple(args, "u", &str)) |
| 1122 | return NULL; |
| 1123 | size = Py_UNICODE_strlen(str); |
| 1124 | return PyUnicode_FromUnicode(str, size); |
| 1125 | } |
| 1126 | |
| 1127 | static PyObject * |
| 1128 | getargs_u_hash(PyObject *self, PyObject *args) |
| 1129 | { |
| 1130 | Py_UNICODE *str; |
| 1131 | Py_ssize_t size; |
| 1132 | if (!PyArg_ParseTuple(args, "u#", &str, &size)) |
| 1133 | return NULL; |
| 1134 | return PyUnicode_FromUnicode(str, size); |
| 1135 | } |
| 1136 | |
| 1137 | static PyObject * |
| 1138 | getargs_Z(PyObject *self, PyObject *args) |
| 1139 | { |
| 1140 | Py_UNICODE *str; |
| 1141 | Py_ssize_t size; |
| 1142 | if (!PyArg_ParseTuple(args, "Z", &str)) |
| 1143 | return NULL; |
| 1144 | if (str != NULL) { |
| 1145 | size = Py_UNICODE_strlen(str); |
| 1146 | return PyUnicode_FromUnicode(str, size); |
| 1147 | } else |
| 1148 | Py_RETURN_NONE; |
| 1149 | } |
| 1150 | |
| 1151 | static PyObject * |
| 1152 | getargs_Z_hash(PyObject *self, PyObject *args) |
| 1153 | { |
| 1154 | Py_UNICODE *str; |
| 1155 | Py_ssize_t size; |
| 1156 | if (!PyArg_ParseTuple(args, "Z#", &str, &size)) |
| 1157 | return NULL; |
| 1158 | if (str != NULL) |
| 1159 | return PyUnicode_FromUnicode(str, size); |
| 1160 | else |
| 1161 | Py_RETURN_NONE; |
| 1162 | } |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1163 | |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1164 | /* Test the s and z codes for PyArg_ParseTuple. |
| 1165 | */ |
| 1166 | static PyObject * |
| 1167 | test_s_code(PyObject *self) |
| 1168 | { |
| 1169 | /* Unicode strings should be accepted */ |
| 1170 | PyObject *tuple, *obj; |
| 1171 | char *value; |
| 1172 | |
| 1173 | tuple = PyTuple_New(1); |
| 1174 | if (tuple == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1176 | |
| 1177 | obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | "latin-1", NULL); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1179 | if (obj == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1181 | |
| 1182 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1183 | |
| 1184 | /* These two blocks used to raise a TypeError: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | * "argument must be string without null bytes, not str" |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1186 | */ |
| 1187 | if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1189 | |
| 1190 | if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | return NULL; |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1192 | |
Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1193 | Py_DECREF(tuple); |
Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1194 | Py_RETURN_NONE; |
| 1195 | } |
| 1196 | |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1197 | static PyObject * |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1198 | parse_tuple_and_keywords(PyObject *self, PyObject *args) |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1199 | { |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1200 | PyObject *sub_args; |
| 1201 | PyObject *sub_kwargs; |
| 1202 | char *sub_format; |
| 1203 | PyObject *sub_keywords; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1204 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1205 | Py_ssize_t i, size; |
| 1206 | char *keywords[8 + 1]; /* space for NULL at end */ |
| 1207 | PyObject *o; |
| 1208 | PyObject *converted[8]; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1209 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1210 | int result; |
| 1211 | PyObject *return_value = NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1212 | |
Larry Hastings | 22701e8 | 2012-08-08 14:52:22 -0700 | [diff] [blame] | 1213 | double buffers[8][4]; /* double ensures alignment where necessary */ |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1214 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1215 | if (!PyArg_ParseTuple(args, "OOyO:parse_tuple_and_keywords", |
| 1216 | &sub_args, &sub_kwargs, |
| 1217 | &sub_format, &sub_keywords)) |
| 1218 | return NULL; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1219 | |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1220 | if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { |
| 1221 | PyErr_SetString(PyExc_ValueError, |
| 1222 | "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); |
| 1223 | return NULL; |
| 1224 | } |
| 1225 | |
| 1226 | memset(buffers, 0, sizeof(buffers)); |
| 1227 | memset(converted, 0, sizeof(converted)); |
| 1228 | memset(keywords, 0, sizeof(keywords)); |
| 1229 | |
| 1230 | size = PySequence_Fast_GET_SIZE(sub_keywords); |
| 1231 | if (size > 8) { |
| 1232 | PyErr_SetString(PyExc_ValueError, |
| 1233 | "parse_tuple_and_keywords: too many keywords in sub_keywords"); |
| 1234 | goto exit; |
| 1235 | } |
| 1236 | |
| 1237 | for (i = 0; i < size; i++) { |
| 1238 | o = PySequence_Fast_GET_ITEM(sub_keywords, i); |
| 1239 | if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { |
| 1240 | PyErr_Format(PyExc_ValueError, |
Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 1241 | "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i); |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1242 | goto exit; |
| 1243 | } |
| 1244 | keywords[i] = PyBytes_AS_STRING(converted[i]); |
| 1245 | } |
| 1246 | |
| 1247 | result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, |
| 1248 | sub_format, keywords, |
| 1249 | buffers + 0, buffers + 1, buffers + 2, buffers + 3, |
| 1250 | buffers + 4, buffers + 5, buffers + 6, buffers + 7); |
| 1251 | |
| 1252 | if (result) { |
| 1253 | return_value = Py_None; |
| 1254 | Py_INCREF(Py_None); |
| 1255 | } |
| 1256 | |
| 1257 | exit: |
| 1258 | size = sizeof(converted) / sizeof(converted[0]); |
| 1259 | for (i = 0; i < size; i++) { |
| 1260 | Py_XDECREF(converted[i]); |
| 1261 | } |
| 1262 | return return_value; |
Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1265 | static volatile int x; |
| 1266 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1267 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case |
| 1268 | of an error. |
| 1269 | */ |
| 1270 | static PyObject * |
Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1271 | test_u_code(PyObject *self) |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1272 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | PyObject *tuple, *obj; |
| 1274 | Py_UNICODE *value; |
| 1275 | Py_ssize_t len; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ |
| 1278 | /* Just use the macro and check that it compiles */ |
| 1279 | x = Py_UNICODE_ISSPACE(25); |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | tuple = PyTuple_New(1); |
| 1282 | if (tuple == NULL) |
| 1283 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | obj = PyUnicode_Decode("test", strlen("test"), |
| 1286 | "ascii", NULL); |
| 1287 | if (obj == NULL) |
| 1288 | return NULL; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | PyTuple_SET_ITEM(tuple, 0, obj); |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | value = 0; |
| 1293 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) |
| 1294 | return NULL; |
| 1295 | if (value != PyUnicode_AS_UNICODE(obj)) |
| 1296 | return raiseTestError("test_u_code", |
| 1297 | "u code returned wrong value for u'test'"); |
| 1298 | value = 0; |
| 1299 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) |
| 1300 | return NULL; |
| 1301 | if (value != PyUnicode_AS_UNICODE(obj) || |
| 1302 | len != PyUnicode_GET_SIZE(obj)) |
| 1303 | return raiseTestError("test_u_code", |
| 1304 | "u# code returned wrong values for u'test'"); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | Py_DECREF(tuple); |
| 1307 | Py_INCREF(Py_None); |
| 1308 | return Py_None; |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1311 | /* Test Z and Z# codes for PyArg_ParseTuple */ |
| 1312 | static PyObject * |
| 1313 | test_Z_code(PyObject *self) |
| 1314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | PyObject *tuple, *obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1316 | const Py_UNICODE *value1, *value2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | Py_ssize_t len1, len2; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | tuple = PyTuple_New(2); |
| 1320 | if (tuple == NULL) |
| 1321 | return NULL; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | obj = PyUnicode_FromString("test"); |
| 1324 | PyTuple_SET_ITEM(tuple, 0, obj); |
| 1325 | Py_INCREF(Py_None); |
| 1326 | PyTuple_SET_ITEM(tuple, 1, Py_None); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | /* swap values on purpose */ |
| 1329 | value1 = NULL; |
| 1330 | value2 = PyUnicode_AS_UNICODE(obj); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | /* Test Z for both values */ |
| 1333 | if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) |
| 1334 | return NULL; |
| 1335 | if (value1 != PyUnicode_AS_UNICODE(obj)) |
| 1336 | return raiseTestError("test_Z_code", |
| 1337 | "Z code returned wrong value for 'test'"); |
| 1338 | if (value2 != NULL) |
| 1339 | return raiseTestError("test_Z_code", |
| 1340 | "Z code returned wrong value for None"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | value1 = NULL; |
| 1343 | value2 = PyUnicode_AS_UNICODE(obj); |
| 1344 | len1 = -1; |
| 1345 | len2 = -1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | /* Test Z# for both values */ |
| 1348 | if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, |
| 1349 | &value2, &len2) < 0) |
| 1350 | return NULL; |
| 1351 | if (value1 != PyUnicode_AS_UNICODE(obj) || |
| 1352 | len1 != PyUnicode_GET_SIZE(obj)) |
| 1353 | return raiseTestError("test_Z_code", |
| 1354 | "Z# code returned wrong values for 'test'"); |
| 1355 | if (value2 != NULL || |
| 1356 | len2 != 0) |
| 1357 | return raiseTestError("test_Z_code", |
| 1358 | "Z# code returned wrong values for None'"); |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | Py_DECREF(tuple); |
| 1361 | Py_RETURN_NONE; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1364 | static PyObject * |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1365 | test_widechar(PyObject *self) |
| 1366 | { |
| 1367 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 1369 | size_t wtextlen = 1; |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1370 | const wchar_t invalid[1] = {(wchar_t)0x110000u}; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1371 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 1373 | size_t wtextlen = 2; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1374 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | PyObject *wide, *utf8; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | wide = PyUnicode_FromWideChar(wtext, wtextlen); |
| 1378 | if (wide == NULL) |
| 1379 | return NULL; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); |
| 1382 | if (utf8 == NULL) { |
| 1383 | Py_DECREF(wide); |
| 1384 | return NULL; |
| 1385 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1386 | |
Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1387 | if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | Py_DECREF(wide); |
| 1389 | Py_DECREF(utf8); |
| 1390 | return raiseTestError("test_widechar", |
| 1391 | "wide string and utf8 string " |
| 1392 | "have different length"); |
| 1393 | } |
| 1394 | if (PyUnicode_Compare(wide, utf8)) { |
| 1395 | Py_DECREF(wide); |
| 1396 | Py_DECREF(utf8); |
| 1397 | if (PyErr_Occurred()) |
| 1398 | return NULL; |
| 1399 | return raiseTestError("test_widechar", |
| 1400 | "wide string and utf8 string " |
| 1401 | "are different"); |
| 1402 | } |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | Py_DECREF(wide); |
| 1405 | Py_DECREF(utf8); |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1406 | |
| 1407 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 1408 | wide = PyUnicode_FromWideChar(invalid, 1); |
| 1409 | if (wide == NULL) |
| 1410 | PyErr_Clear(); |
| 1411 | else |
| 1412 | return raiseTestError("test_widechar", |
| 1413 | "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); |
| 1414 | |
| 1415 | wide = PyUnicode_FromUnicode(invalid, 1); |
| 1416 | if (wide == NULL) |
| 1417 | PyErr_Clear(); |
| 1418 | else |
| 1419 | return raiseTestError("test_widechar", |
| 1420 | "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1421 | |
| 1422 | wide = PyUnicode_FromUnicode(NULL, 1); |
| 1423 | if (wide == NULL) |
| 1424 | return NULL; |
| 1425 | PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1426 | if (_PyUnicode_Ready(wide) < 0) { |
| 1427 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1428 | PyErr_Clear(); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1429 | } |
| 1430 | else { |
| 1431 | Py_DECREF(wide); |
Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1432 | return raiseTestError("test_widechar", |
| 1433 | "PyUnicode_Ready() didn't fail"); |
Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1434 | } |
Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1435 | #endif |
| 1436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | Py_RETURN_NONE; |
Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1441 | unicode_aswidechar(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1442 | { |
| 1443 | PyObject *unicode, *result; |
| 1444 | Py_ssize_t buflen, size; |
| 1445 | wchar_t *buffer; |
| 1446 | |
| 1447 | if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) |
| 1448 | return NULL; |
| 1449 | buffer = PyMem_Malloc(buflen * sizeof(wchar_t)); |
| 1450 | if (buffer == NULL) |
| 1451 | return PyErr_NoMemory(); |
| 1452 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1453 | size = PyUnicode_AsWideChar(unicode, buffer, buflen); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1454 | if (size == -1) { |
| 1455 | PyMem_Free(buffer); |
| 1456 | return NULL; |
| 1457 | } |
| 1458 | |
| 1459 | if (size < buflen) |
| 1460 | buflen = size + 1; |
| 1461 | else |
| 1462 | buflen = size; |
| 1463 | result = PyUnicode_FromWideChar(buffer, buflen); |
| 1464 | PyMem_Free(buffer); |
| 1465 | if (result == NULL) |
| 1466 | return NULL; |
| 1467 | |
| 1468 | return Py_BuildValue("(Nn)", result, size); |
| 1469 | } |
| 1470 | |
| 1471 | static PyObject * |
Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1472 | unicode_aswidecharstring(PyObject *self, PyObject *args) |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1473 | { |
| 1474 | PyObject *unicode, *result; |
| 1475 | Py_ssize_t size; |
| 1476 | wchar_t *buffer; |
| 1477 | |
| 1478 | if (!PyArg_ParseTuple(args, "U", &unicode)) |
| 1479 | return NULL; |
| 1480 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1481 | buffer = PyUnicode_AsWideCharString(unicode, &size); |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1482 | if (buffer == NULL) |
| 1483 | return NULL; |
| 1484 | |
| 1485 | result = PyUnicode_FromWideChar(buffer, size + 1); |
| 1486 | PyMem_Free(buffer); |
| 1487 | if (result == NULL) |
| 1488 | return NULL; |
| 1489 | return Py_BuildValue("(Nn)", result, size); |
| 1490 | } |
| 1491 | |
| 1492 | static PyObject * |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 1493 | unicode_encodedecimal(PyObject *self, PyObject *args) |
| 1494 | { |
| 1495 | Py_UNICODE *unicode; |
| 1496 | Py_ssize_t length; |
| 1497 | char *errors = NULL; |
| 1498 | PyObject *decimal; |
| 1499 | Py_ssize_t decimal_length, new_length; |
| 1500 | int res; |
| 1501 | |
| 1502 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) |
| 1503 | return NULL; |
| 1504 | |
| 1505 | decimal_length = length * 7; /* len('€') */ |
| 1506 | decimal = PyBytes_FromStringAndSize(NULL, decimal_length); |
| 1507 | if (decimal == NULL) |
| 1508 | return NULL; |
| 1509 | |
| 1510 | res = PyUnicode_EncodeDecimal(unicode, length, |
| 1511 | PyBytes_AS_STRING(decimal), |
| 1512 | errors); |
| 1513 | if (res < 0) { |
| 1514 | Py_DECREF(decimal); |
| 1515 | return NULL; |
| 1516 | } |
| 1517 | |
| 1518 | new_length = strlen(PyBytes_AS_STRING(decimal)); |
| 1519 | assert(new_length <= decimal_length); |
| 1520 | res = _PyBytes_Resize(&decimal, new_length); |
| 1521 | if (res < 0) |
| 1522 | return NULL; |
| 1523 | |
| 1524 | return decimal; |
| 1525 | } |
| 1526 | |
| 1527 | static PyObject * |
| 1528 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) |
| 1529 | { |
| 1530 | Py_UNICODE *unicode; |
| 1531 | Py_ssize_t length; |
| 1532 | if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) |
| 1533 | return NULL; |
| 1534 | return PyUnicode_TransformDecimalToASCII(unicode, length); |
| 1535 | } |
| 1536 | |
| 1537 | static PyObject * |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 1538 | unicode_legacy_string(PyObject *self, PyObject *args) |
| 1539 | { |
| 1540 | Py_UNICODE *data; |
| 1541 | Py_ssize_t len; |
| 1542 | PyObject *u; |
| 1543 | |
| 1544 | if (!PyArg_ParseTuple(args, "u#", &data, &len)) |
| 1545 | return NULL; |
| 1546 | |
| 1547 | u = PyUnicode_FromUnicode(NULL, len); |
| 1548 | if (u == NULL) |
| 1549 | return NULL; |
| 1550 | |
| 1551 | memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); |
| 1552 | |
| 1553 | if (len > 0) { /* The empty string is always ready. */ |
| 1554 | assert(!PyUnicode_IS_READY(u)); |
| 1555 | } |
| 1556 | |
| 1557 | return u; |
| 1558 | } |
| 1559 | |
| 1560 | static PyObject * |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1561 | getargs_w_star(PyObject *self, PyObject *args) |
| 1562 | { |
| 1563 | Py_buffer buffer; |
| 1564 | PyObject *result; |
| 1565 | char *str; |
| 1566 | |
| 1567 | if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) |
| 1568 | return NULL; |
| 1569 | |
| 1570 | if (2 <= buffer.len) { |
| 1571 | str = buffer.buf; |
| 1572 | str[0] = '['; |
| 1573 | str[buffer.len-1] = ']'; |
| 1574 | } |
| 1575 | |
| 1576 | result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 1577 | PyBuffer_Release(&buffer); |
| 1578 | return result; |
| 1579 | } |
| 1580 | |
| 1581 | |
| 1582 | static PyObject * |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1583 | test_empty_argparse(PyObject *self) |
| 1584 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1585 | /* Test that formats can begin with '|'. See issue #4720. */ |
| 1586 | PyObject *tuple, *dict = NULL; |
| 1587 | static char *kwlist[] = {NULL}; |
| 1588 | int result; |
| 1589 | tuple = PyTuple_New(0); |
| 1590 | if (!tuple) |
| 1591 | return NULL; |
| 1592 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) |
| 1593 | goto done; |
| 1594 | dict = PyDict_New(); |
| 1595 | if (!dict) |
| 1596 | goto done; |
| 1597 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1598 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | Py_DECREF(tuple); |
| 1600 | Py_XDECREF(dict); |
| 1601 | if (result < 0) |
| 1602 | return NULL; |
| 1603 | else { |
| 1604 | Py_RETURN_NONE; |
| 1605 | } |
Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1609 | codec_incrementalencoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | const char *encoding, *errors = NULL; |
| 1612 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", |
| 1613 | &encoding, &errors)) |
| 1614 | return NULL; |
| 1615 | return PyCodec_IncrementalEncoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1618 | static PyObject * |
| 1619 | codec_incrementaldecoder(PyObject *self, PyObject *args) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1620 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | const char *encoding, *errors = NULL; |
| 1622 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", |
| 1623 | &encoding, &errors)) |
| 1624 | return NULL; |
| 1625 | return PyCodec_IncrementalDecoder(encoding, errors); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1628 | |
Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1629 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1630 | static PyObject * |
| 1631 | test_long_numbits(PyObject *self) |
| 1632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | struct triple { |
| 1634 | long input; |
| 1635 | size_t nbits; |
| 1636 | int sign; |
| 1637 | } testcases[] = {{0, 0, 0}, |
| 1638 | {1L, 1, 1}, |
| 1639 | {-1L, 1, -1}, |
| 1640 | {2L, 2, 1}, |
| 1641 | {-2L, 2, -1}, |
| 1642 | {3L, 2, 1}, |
| 1643 | {-3L, 2, -1}, |
| 1644 | {4L, 3, 1}, |
| 1645 | {-4L, 3, -1}, |
| 1646 | {0x7fffL, 15, 1}, /* one Python long digit */ |
| 1647 | {-0x7fffL, 15, -1}, |
| 1648 | {0xffffL, 16, 1}, |
| 1649 | {-0xffffL, 16, -1}, |
| 1650 | {0xfffffffL, 28, 1}, |
| 1651 | {-0xfffffffL, 28, -1}}; |
| 1652 | int i; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1653 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 1654 | for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | PyObject *plong = PyLong_FromLong(testcases[i].input); |
| 1656 | size_t nbits = _PyLong_NumBits(plong); |
| 1657 | int sign = _PyLong_Sign(plong); |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | Py_DECREF(plong); |
| 1660 | if (nbits != testcases[i].nbits) |
| 1661 | return raiseTestError("test_long_numbits", |
| 1662 | "wrong result for _PyLong_NumBits"); |
| 1663 | if (sign != testcases[i].sign) |
| 1664 | return raiseTestError("test_long_numbits", |
| 1665 | "wrong result for _PyLong_Sign"); |
| 1666 | } |
| 1667 | Py_INCREF(Py_None); |
| 1668 | return Py_None; |
Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 1671 | /* Example passing NULLs to PyObject_Str(NULL). */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1672 | |
| 1673 | static PyObject * |
| 1674 | test_null_strings(PyObject *self) |
| 1675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); |
| 1677 | PyObject *tuple = PyTuple_Pack(2, o1, o2); |
| 1678 | Py_XDECREF(o1); |
| 1679 | Py_XDECREF(o2); |
| 1680 | return tuple; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1683 | static PyObject * |
| 1684 | raise_exception(PyObject *self, PyObject *args) |
| 1685 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | PyObject *exc; |
| 1687 | PyObject *exc_args, *v; |
| 1688 | int num_args, i; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | if (!PyArg_ParseTuple(args, "Oi:raise_exception", |
| 1691 | &exc, &num_args)) |
| 1692 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | exc_args = PyTuple_New(num_args); |
| 1695 | if (exc_args == NULL) |
| 1696 | return NULL; |
| 1697 | for (i = 0; i < num_args; ++i) { |
| 1698 | v = PyLong_FromLong(i); |
| 1699 | if (v == NULL) { |
| 1700 | Py_DECREF(exc_args); |
| 1701 | return NULL; |
| 1702 | } |
| 1703 | PyTuple_SET_ITEM(exc_args, i, v); |
| 1704 | } |
| 1705 | PyErr_SetObject(exc, exc_args); |
| 1706 | Py_DECREF(exc_args); |
| 1707 | return NULL; |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 1708 | } |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 1709 | |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 1710 | static PyObject * |
| 1711 | test_set_exc_info(PyObject *self, PyObject *args) |
| 1712 | { |
| 1713 | PyObject *orig_exc; |
| 1714 | PyObject *new_type, *new_value, *new_tb; |
| 1715 | PyObject *type, *value, *tb; |
| 1716 | if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", |
| 1717 | &new_type, &new_value, &new_tb)) |
| 1718 | return NULL; |
| 1719 | |
| 1720 | PyErr_GetExcInfo(&type, &value, &tb); |
| 1721 | |
| 1722 | Py_INCREF(new_type); |
| 1723 | Py_INCREF(new_value); |
| 1724 | Py_INCREF(new_tb); |
| 1725 | PyErr_SetExcInfo(new_type, new_value, new_tb); |
| 1726 | |
| 1727 | orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); |
| 1728 | Py_XDECREF(type); |
| 1729 | Py_XDECREF(value); |
| 1730 | Py_XDECREF(tb); |
| 1731 | return orig_exc; |
| 1732 | } |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 1733 | |
| 1734 | static int test_run_counter = 0; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1735 | |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1736 | static PyObject * |
| 1737 | test_datetime_capi(PyObject *self, PyObject *args) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | if (PyDateTimeAPI) { |
| 1739 | if (test_run_counter) { |
| 1740 | /* Probably regrtest.py -R */ |
| 1741 | Py_RETURN_NONE; |
| 1742 | } |
| 1743 | else { |
| 1744 | PyErr_SetString(PyExc_AssertionError, |
| 1745 | "PyDateTime_CAPI somehow initialized"); |
| 1746 | return NULL; |
| 1747 | } |
| 1748 | } |
| 1749 | test_run_counter++; |
| 1750 | PyDateTime_IMPORT; |
| 1751 | if (PyDateTimeAPI) |
| 1752 | Py_RETURN_NONE; |
| 1753 | else |
| 1754 | return NULL; |
Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 1757 | |
| 1758 | #ifdef WITH_THREAD |
| 1759 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1760 | /* test_thread_state spawns a thread of its own, and that thread releases |
| 1761 | * `thread_done` when it's finished. The driver code has to know when the |
| 1762 | * thread finishes, because the thread uses a PyObject (the callable) that |
| 1763 | * may go away when the driver finishes. The former lack of this explicit |
| 1764 | * synchronization caused rare segfaults, so rare that they were seen only |
| 1765 | * on a Mac buildbot (although they were possible on any box). |
| 1766 | */ |
| 1767 | static PyThread_type_lock thread_done = NULL; |
| 1768 | |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1769 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1770 | _make_call(void *callable) |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | PyObject *rc; |
| 1773 | int success; |
| 1774 | PyGILState_STATE s = PyGILState_Ensure(); |
| 1775 | rc = PyObject_CallFunction((PyObject *)callable, ""); |
| 1776 | success = (rc != NULL); |
| 1777 | Py_XDECREF(rc); |
| 1778 | PyGILState_Release(s); |
| 1779 | return success; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1782 | /* Same thing, but releases `thread_done` when it returns. This variant |
| 1783 | * should be called only from threads spawned by test_thread_state(). |
| 1784 | */ |
| 1785 | static void |
| 1786 | _make_call_from_thread(void *callable) |
| 1787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | _make_call(callable); |
| 1789 | PyThread_release_lock(thread_done); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1792 | static PyObject * |
| 1793 | test_thread_state(PyObject *self, PyObject *args) |
| 1794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | PyObject *fn; |
| 1796 | int success = 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) |
| 1799 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | if (!PyCallable_Check(fn)) { |
| 1802 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable", |
| 1803 | fn->ob_type->tp_name); |
| 1804 | return NULL; |
| 1805 | } |
Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 1806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1807 | /* Ensure Python is set up for threading */ |
| 1808 | PyEval_InitThreads(); |
| 1809 | thread_done = PyThread_allocate_lock(); |
| 1810 | if (thread_done == NULL) |
| 1811 | return PyErr_NoMemory(); |
| 1812 | PyThread_acquire_lock(thread_done, 1); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | /* Start a new thread with our callback. */ |
| 1815 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1816 | /* Make the callback with the thread lock held by this thread */ |
| 1817 | success &= _make_call(fn); |
| 1818 | /* Do it all again, but this time with the thread-lock released */ |
| 1819 | Py_BEGIN_ALLOW_THREADS |
| 1820 | success &= _make_call(fn); |
| 1821 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1822 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | /* And once more with and without a thread |
| 1825 | XXX - should use a lock and work out exactly what we are trying |
| 1826 | to test <wink> |
| 1827 | */ |
| 1828 | Py_BEGIN_ALLOW_THREADS |
| 1829 | PyThread_start_new_thread(_make_call_from_thread, fn); |
| 1830 | success &= _make_call(fn); |
| 1831 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ |
| 1832 | Py_END_ALLOW_THREADS |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1833 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | /* Release lock we acquired above. This is required on HP-UX. */ |
| 1835 | PyThread_release_lock(thread_done); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1837 | PyThread_free_lock(thread_done); |
| 1838 | if (!success) |
| 1839 | return NULL; |
| 1840 | Py_RETURN_NONE; |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1841 | } |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1842 | |
| 1843 | /* test Py_AddPendingCalls using threads */ |
| 1844 | static int _pending_callback(void *arg) |
| 1845 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | /* we assume the argument is callable object to which we own a reference */ |
| 1847 | PyObject *callable = (PyObject *)arg; |
| 1848 | PyObject *r = PyObject_CallObject(callable, NULL); |
| 1849 | Py_DECREF(callable); |
| 1850 | Py_XDECREF(r); |
| 1851 | return r != NULL ? 0 : -1; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /* The following requests n callbacks to _pending_callback. It can be |
| 1855 | * run from any python thread. |
| 1856 | */ |
| 1857 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) |
| 1858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1859 | PyObject *callable; |
| 1860 | int r; |
| 1861 | if (PyArg_ParseTuple(arg, "O", &callable) == 0) |
| 1862 | return NULL; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | /* create the reference for the callbackwhile we hold the lock */ |
| 1865 | Py_INCREF(callable); |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | Py_BEGIN_ALLOW_THREADS |
| 1868 | r = Py_AddPendingCall(&_pending_callback, callable); |
| 1869 | Py_END_ALLOW_THREADS |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | if (r<0) { |
| 1872 | Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ |
| 1873 | Py_INCREF(Py_False); |
| 1874 | return Py_False; |
| 1875 | } |
| 1876 | Py_INCREF(Py_True); |
| 1877 | return Py_True; |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1878 | } |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 1879 | #endif |
| 1880 | |
Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 1881 | /* Some tests of PyUnicode_FromFormat(). This needs more tests. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1882 | static PyObject * |
| 1883 | test_string_from_format(PyObject *self, PyObject *args) |
| 1884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | PyObject *result; |
| 1886 | char *msg; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1887 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1888 | #define CHECK_1_FORMAT(FORMAT, TYPE) \ |
| 1889 | result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \ |
| 1890 | if (result == NULL) \ |
| 1891 | return NULL; \ |
Victor Stinner | 2b979bf | 2011-11-20 19:32:09 +0100 | [diff] [blame] | 1892 | if (PyUnicode_CompareWithASCIIString(result, "1")) { \ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1893 | msg = FORMAT " failed at 1"; \ |
| 1894 | goto Fail; \ |
| 1895 | } \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | Py_DECREF(result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | CHECK_1_FORMAT("%d", int); |
| 1899 | CHECK_1_FORMAT("%ld", long); |
| 1900 | /* The z width modifier was added in Python 2.5. */ |
| 1901 | CHECK_1_FORMAT("%zd", Py_ssize_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | /* The u type code was added in Python 2.5. */ |
| 1904 | CHECK_1_FORMAT("%u", unsigned int); |
| 1905 | CHECK_1_FORMAT("%lu", unsigned long); |
| 1906 | CHECK_1_FORMAT("%zu", size_t); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | /* "%lld" and "%llu" support added in Python 2.7. */ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1909 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG); |
| 1911 | CHECK_1_FORMAT("%lld", PY_LONG_LONG); |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 1912 | #endif |
| 1913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | Py_RETURN_NONE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1915 | |
| 1916 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1917 | Py_XDECREF(result); |
| 1918 | return raiseTestError("test_string_from_format", msg); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1919 | |
| 1920 | #undef CHECK_1_FORMAT |
| 1921 | } |
| 1922 | |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 1923 | |
| 1924 | static PyObject * |
| 1925 | test_unicode_compare_with_ascii(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); |
| 1927 | int result; |
| 1928 | if (py_s == NULL) |
| 1929 | return NULL; |
| 1930 | result = PyUnicode_CompareWithASCIIString(py_s, "str"); |
| 1931 | Py_DECREF(py_s); |
| 1932 | if (!result) { |
| 1933 | PyErr_SetString(TestError, "Python string ending in NULL " |
| 1934 | "should not compare equal to c string."); |
| 1935 | return NULL; |
| 1936 | } |
| 1937 | Py_RETURN_NONE; |
Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 1938 | } |
Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 1939 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1940 | /* This is here to provide a docstring for test_descr. */ |
| 1941 | static PyObject * |
| 1942 | test_with_docstring(PyObject *self) |
| 1943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | Py_RETURN_NONE; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1947 | /* Test PyOS_string_to_double. */ |
| 1948 | static PyObject * |
| 1949 | test_string_to_double(PyObject *self) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | double result; |
| 1951 | char *msg; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | #define CHECK_STRING(STR, expected) \ |
| 1954 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1955 | if (result == -1.0 && PyErr_Occurred()) \ |
| 1956 | return NULL; \ |
| 1957 | if (result != expected) { \ |
| 1958 | msg = "conversion of " STR " to float failed"; \ |
| 1959 | goto fail; \ |
| 1960 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | #define CHECK_INVALID(STR) \ |
| 1963 | result = PyOS_string_to_double(STR, NULL, NULL); \ |
| 1964 | if (result == -1.0 && PyErr_Occurred()) { \ |
| 1965 | if (PyErr_ExceptionMatches(PyExc_ValueError)) \ |
| 1966 | PyErr_Clear(); \ |
| 1967 | else \ |
| 1968 | return NULL; \ |
| 1969 | } \ |
| 1970 | else { \ |
| 1971 | msg = "conversion of " STR " didn't raise ValueError"; \ |
| 1972 | goto fail; \ |
| 1973 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | CHECK_STRING("0.1", 0.1); |
| 1976 | CHECK_STRING("1.234", 1.234); |
| 1977 | CHECK_STRING("-1.35", -1.35); |
| 1978 | CHECK_STRING(".1e01", 1.0); |
| 1979 | CHECK_STRING("2.e-2", 0.02); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | CHECK_INVALID(" 0.1"); |
| 1982 | CHECK_INVALID("\t\n-3"); |
| 1983 | CHECK_INVALID(".123 "); |
| 1984 | CHECK_INVALID("3\n"); |
| 1985 | CHECK_INVALID("123abc"); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | Py_RETURN_NONE; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1988 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | return raiseTestError("test_string_to_double", msg); |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 1990 | #undef CHECK_STRING |
| 1991 | #undef CHECK_INVALID |
| 1992 | } |
| 1993 | |
| 1994 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1995 | /* Coverage testing of capsule objects. */ |
| 1996 | |
| 1997 | static const char *capsule_name = "capsule name"; |
| 1998 | static char *capsule_pointer = "capsule pointer"; |
| 1999 | static char *capsule_context = "capsule context"; |
| 2000 | static const char *capsule_error = NULL; |
| 2001 | static int |
| 2002 | capsule_destructor_call_count = 0; |
| 2003 | |
| 2004 | static void |
| 2005 | capsule_destructor(PyObject *o) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | capsule_destructor_call_count++; |
| 2007 | if (PyCapsule_GetContext(o) != capsule_context) { |
| 2008 | capsule_error = "context did not match in destructor!"; |
| 2009 | } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { |
| 2010 | capsule_error = "destructor did not match in destructor! (woah!)"; |
| 2011 | } else if (PyCapsule_GetName(o) != capsule_name) { |
| 2012 | capsule_error = "name did not match in destructor!"; |
| 2013 | } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { |
| 2014 | capsule_error = "pointer did not match in destructor!"; |
| 2015 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | char *name; |
| 2020 | char *module; |
| 2021 | char *attribute; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2022 | } known_capsule; |
| 2023 | |
| 2024 | static PyObject * |
| 2025 | test_capsule(PyObject *self, PyObject *args) |
| 2026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | PyObject *object; |
| 2028 | const char *error = NULL; |
| 2029 | void *pointer; |
| 2030 | void *pointer2; |
| 2031 | known_capsule known_capsules[] = { |
| 2032 | #define KNOWN_CAPSULE(module, name) { module "." name, module, name } |
| 2033 | KNOWN_CAPSULE("_socket", "CAPI"), |
| 2034 | KNOWN_CAPSULE("_curses", "_C_API"), |
| 2035 | KNOWN_CAPSULE("datetime", "datetime_CAPI"), |
| 2036 | { NULL, NULL }, |
| 2037 | }; |
| 2038 | known_capsule *known = &known_capsules[0]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2039 | |
| 2040 | #define FAIL(x) { error = (x); goto exit; } |
| 2041 | |
| 2042 | #define CHECK_DESTRUCTOR \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2043 | if (capsule_error) { \ |
| 2044 | FAIL(capsule_error); \ |
| 2045 | } \ |
| 2046 | else if (!capsule_destructor_call_count) { \ |
| 2047 | FAIL("destructor not called!"); \ |
| 2048 | } \ |
| 2049 | capsule_destructor_call_count = 0; \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); |
| 2052 | PyCapsule_SetContext(object, capsule_context); |
| 2053 | capsule_destructor(object); |
| 2054 | CHECK_DESTRUCTOR; |
| 2055 | Py_DECREF(object); |
| 2056 | CHECK_DESTRUCTOR; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | object = PyCapsule_New(known, "ignored", NULL); |
| 2059 | PyCapsule_SetPointer(object, capsule_pointer); |
| 2060 | PyCapsule_SetName(object, capsule_name); |
| 2061 | PyCapsule_SetDestructor(object, capsule_destructor); |
| 2062 | PyCapsule_SetContext(object, capsule_context); |
| 2063 | capsule_destructor(object); |
| 2064 | CHECK_DESTRUCTOR; |
| 2065 | /* intentionally access using the wrong name */ |
| 2066 | pointer2 = PyCapsule_GetPointer(object, "the wrong name"); |
| 2067 | if (!PyErr_Occurred()) { |
| 2068 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2069 | } |
| 2070 | PyErr_Clear(); |
| 2071 | if (pointer2) { |
| 2072 | if (pointer2 == capsule_pointer) { |
| 2073 | FAIL("PyCapsule_GetPointer should not have" |
| 2074 | " returned the internal pointer!"); |
| 2075 | } else { |
| 2076 | FAIL("PyCapsule_GetPointer should have " |
| 2077 | "returned NULL pointer but did not!"); |
| 2078 | } |
| 2079 | } |
| 2080 | PyCapsule_SetDestructor(object, NULL); |
| 2081 | Py_DECREF(object); |
| 2082 | if (capsule_destructor_call_count) { |
| 2083 | FAIL("destructor called when it should not have been!"); |
| 2084 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | for (known = &known_capsules[0]; known->module != NULL; known++) { |
| 2087 | /* yeah, ordinarily I wouldn't do this either, |
| 2088 | but it's fine for this test harness. |
| 2089 | */ |
| 2090 | static char buffer[256]; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2091 | #undef FAIL |
| 2092 | #define FAIL(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | { \ |
| 2094 | sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ |
| 2095 | x, known->module, known->attribute); \ |
| 2096 | error = buffer; \ |
| 2097 | goto exit; \ |
| 2098 | } \ |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | PyObject *module = PyImport_ImportModule(known->module); |
| 2101 | if (module) { |
| 2102 | pointer = PyCapsule_Import(known->name, 0); |
| 2103 | if (!pointer) { |
| 2104 | Py_DECREF(module); |
| 2105 | FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); |
| 2106 | } |
| 2107 | object = PyObject_GetAttrString(module, known->attribute); |
| 2108 | if (!object) { |
| 2109 | Py_DECREF(module); |
| 2110 | return NULL; |
| 2111 | } |
| 2112 | pointer2 = PyCapsule_GetPointer(object, |
| 2113 | "weebles wobble but they don't fall down"); |
| 2114 | if (!PyErr_Occurred()) { |
| 2115 | Py_DECREF(object); |
| 2116 | Py_DECREF(module); |
| 2117 | FAIL("PyCapsule_GetPointer should have failed but did not!"); |
| 2118 | } |
| 2119 | PyErr_Clear(); |
| 2120 | if (pointer2) { |
| 2121 | Py_DECREF(module); |
| 2122 | Py_DECREF(object); |
| 2123 | if (pointer2 == pointer) { |
| 2124 | FAIL("PyCapsule_GetPointer should not have" |
| 2125 | " returned its internal pointer!"); |
| 2126 | } else { |
| 2127 | FAIL("PyCapsule_GetPointer should have" |
| 2128 | " returned NULL pointer but did not!"); |
| 2129 | } |
| 2130 | } |
| 2131 | Py_DECREF(object); |
| 2132 | Py_DECREF(module); |
| 2133 | } |
| 2134 | else |
| 2135 | PyErr_Clear(); |
| 2136 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2137 | |
| 2138 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | if (error) { |
| 2140 | return raiseTestError("test_capsule", error); |
| 2141 | } |
| 2142 | Py_RETURN_NONE; |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2143 | #undef FAIL |
| 2144 | } |
| 2145 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2146 | #ifdef HAVE_GETTIMEOFDAY |
| 2147 | /* Profiling of integer performance */ |
Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 2148 | 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] | 2149 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2150 | e->tv_sec -= s->tv_sec; |
| 2151 | e->tv_usec -= s->tv_usec; |
| 2152 | if (e->tv_usec < 0) { |
| 2153 | e->tv_sec -=1; |
| 2154 | e->tv_usec += 1000000; |
| 2155 | } |
| 2156 | 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] | 2157 | } |
| 2158 | |
| 2159 | static PyObject * |
| 2160 | profile_int(PyObject *self, PyObject* args) |
| 2161 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | int i, k; |
| 2163 | struct timeval start, stop; |
| 2164 | PyObject *single, **multiple, *op1, *result; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | /* Test 1: Allocate and immediately deallocate |
| 2167 | many small integers */ |
| 2168 | gettimeofday(&start, NULL); |
| 2169 | for(k=0; k < 20000; k++) |
| 2170 | for(i=0; i < 1000; i++) { |
| 2171 | single = PyLong_FromLong(i); |
| 2172 | Py_DECREF(single); |
| 2173 | } |
| 2174 | gettimeofday(&stop, NULL); |
| 2175 | print_delta(1, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | /* Test 2: Allocate and immediately deallocate |
| 2178 | many large integers */ |
| 2179 | gettimeofday(&start, NULL); |
| 2180 | for(k=0; k < 20000; k++) |
| 2181 | for(i=0; i < 1000; i++) { |
| 2182 | single = PyLong_FromLong(i+1000000); |
| 2183 | Py_DECREF(single); |
| 2184 | } |
| 2185 | gettimeofday(&stop, NULL); |
| 2186 | print_delta(2, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2188 | /* Test 3: Allocate a few integers, then release |
| 2189 | them all simultaneously. */ |
| 2190 | multiple = malloc(sizeof(PyObject*) * 1000); |
| 2191 | gettimeofday(&start, NULL); |
| 2192 | for(k=0; k < 20000; k++) { |
| 2193 | for(i=0; i < 1000; i++) { |
| 2194 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2195 | } |
| 2196 | for(i=0; i < 1000; i++) { |
| 2197 | Py_DECREF(multiple[i]); |
| 2198 | } |
| 2199 | } |
| 2200 | gettimeofday(&stop, NULL); |
| 2201 | print_delta(3, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | /* Test 4: Allocate many integers, then release |
| 2204 | them all simultaneously. */ |
| 2205 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 2206 | gettimeofday(&start, NULL); |
| 2207 | for(k=0; k < 20; k++) { |
| 2208 | for(i=0; i < 1000000; i++) { |
| 2209 | multiple[i] = PyLong_FromLong(i+1000000); |
| 2210 | } |
| 2211 | for(i=0; i < 1000000; i++) { |
| 2212 | Py_DECREF(multiple[i]); |
| 2213 | } |
| 2214 | } |
| 2215 | gettimeofday(&stop, NULL); |
| 2216 | print_delta(4, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | /* Test 5: Allocate many integers < 32000 */ |
| 2219 | multiple = malloc(sizeof(PyObject*) * 1000000); |
| 2220 | gettimeofday(&start, NULL); |
| 2221 | for(k=0; k < 10; k++) { |
| 2222 | for(i=0; i < 1000000; i++) { |
| 2223 | multiple[i] = PyLong_FromLong(i+1000); |
| 2224 | } |
| 2225 | for(i=0; i < 1000000; i++) { |
| 2226 | Py_DECREF(multiple[i]); |
| 2227 | } |
| 2228 | } |
| 2229 | gettimeofday(&stop, NULL); |
| 2230 | print_delta(5, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | /* Test 6: Perform small int addition */ |
| 2233 | op1 = PyLong_FromLong(1); |
| 2234 | gettimeofday(&start, NULL); |
| 2235 | for(i=0; i < 10000000; i++) { |
| 2236 | result = PyNumber_Add(op1, op1); |
| 2237 | Py_DECREF(result); |
| 2238 | } |
| 2239 | gettimeofday(&stop, NULL); |
| 2240 | Py_DECREF(op1); |
| 2241 | print_delta(6, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | /* Test 7: Perform medium int addition */ |
| 2244 | op1 = PyLong_FromLong(1000); |
| 2245 | gettimeofday(&start, NULL); |
| 2246 | for(i=0; i < 10000000; i++) { |
| 2247 | result = PyNumber_Add(op1, op1); |
| 2248 | Py_DECREF(result); |
| 2249 | } |
| 2250 | gettimeofday(&stop, NULL); |
| 2251 | Py_DECREF(op1); |
| 2252 | print_delta(7, &start, &stop); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | Py_INCREF(Py_None); |
| 2255 | return Py_None; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2256 | } |
| 2257 | #endif |
| 2258 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2259 | /* To test the format of tracebacks as printed out. */ |
| 2260 | static PyObject * |
| 2261 | traceback_print(PyObject *self, PyObject *args) |
| 2262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | PyObject *file; |
| 2264 | PyObject *traceback; |
| 2265 | int result; |
| 2266 | |
| 2267 | if (!PyArg_ParseTuple(args, "OO:traceback_print", |
| 2268 | &traceback, &file)) |
| 2269 | return NULL; |
| 2270 | |
| 2271 | result = PyTraceBack_Print(traceback, file); |
| 2272 | if (result < 0) |
| 2273 | return NULL; |
| 2274 | Py_RETURN_NONE; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2277 | /* To test the format of exceptions as printed out. */ |
| 2278 | static PyObject * |
| 2279 | exception_print(PyObject *self, PyObject *args) |
| 2280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | PyObject *value; |
| 2282 | PyObject *tb; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | if (!PyArg_ParseTuple(args, "O:exception_print", |
| 2285 | &value)) |
| 2286 | return NULL; |
| 2287 | if (!PyExceptionInstance_Check(value)) { |
| 2288 | PyErr_Format(PyExc_TypeError, "an exception instance is required"); |
| 2289 | return NULL; |
| 2290 | } |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | tb = PyException_GetTraceback(value); |
| 2293 | PyErr_Display((PyObject *) Py_TYPE(value), value, tb); |
| 2294 | Py_XDECREF(tb); |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | Py_RETURN_NONE; |
Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | |
| 2300 | |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2301 | |
| 2302 | /* reliably raise a MemoryError */ |
| 2303 | static PyObject * |
| 2304 | raise_memoryerror(PyObject *self) |
| 2305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | PyErr_NoMemory(); |
| 2307 | return NULL; |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2310 | /* Issue 6012 */ |
| 2311 | static PyObject *str1, *str2; |
| 2312 | static int |
| 2313 | failing_converter(PyObject *obj, void *arg) |
| 2314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | /* Clone str1, then let the conversion fail. */ |
| 2316 | assert(str1); |
| 2317 | str2 = str1; |
| 2318 | Py_INCREF(str2); |
| 2319 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2320 | } |
| 2321 | static PyObject* |
| 2322 | argparsing(PyObject *o, PyObject *args) |
| 2323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | PyObject *res; |
| 2325 | str1 = str2 = NULL; |
| 2326 | if (!PyArg_ParseTuple(args, "O&O&", |
| 2327 | PyUnicode_FSConverter, &str1, |
| 2328 | failing_converter, &str2)) { |
| 2329 | if (!str2) |
| 2330 | /* argument converter not called? */ |
| 2331 | return NULL; |
| 2332 | /* Should be 1 */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2333 | res = PyLong_FromSsize_t(Py_REFCNT(str2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | Py_DECREF(str2); |
| 2335 | PyErr_Clear(); |
| 2336 | return res; |
| 2337 | } |
| 2338 | Py_RETURN_NONE; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2339 | } |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2340 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2341 | /* To test that the result of PyCode_NewEmpty has the right members. */ |
| 2342 | static PyObject * |
| 2343 | code_newempty(PyObject *self, PyObject *args) |
| 2344 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2345 | const char *filename; |
| 2346 | const char *funcname; |
| 2347 | int firstlineno; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | if (!PyArg_ParseTuple(args, "ssi:code_newempty", |
| 2350 | &filename, &funcname, &firstlineno)) |
| 2351 | return NULL; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2353 | return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2356 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). |
| 2357 | Run via Lib/test/test_exceptions.py */ |
| 2358 | static PyObject * |
| 2359 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) |
| 2360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | const char *name; |
| 2362 | const char *doc = NULL; |
| 2363 | PyObject *base = NULL; |
| 2364 | PyObject *dict = NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2369 | "s|sOO:make_exception_with_doc", kwlist, |
| 2370 | &name, &doc, &base, &dict)) |
| 2371 | return NULL; |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | return PyErr_NewExceptionWithDoc(name, doc, base, dict); |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2376 | static PyObject * |
| 2377 | make_memoryview_from_NULL_pointer(PyObject *self) |
| 2378 | { |
| 2379 | Py_buffer info; |
| 2380 | if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) |
| 2381 | return NULL; |
| 2382 | return PyMemoryView_FromBuffer(&info); |
| 2383 | } |
| 2384 | |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2385 | /* Test that the fatal error from not having a current thread doesn't |
| 2386 | cause an infinite loop. Run via Lib/test/test_capi.py */ |
| 2387 | static PyObject * |
| 2388 | crash_no_current_thread(PyObject *self) |
| 2389 | { |
| 2390 | Py_BEGIN_ALLOW_THREADS |
Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 2391 | /* Using PyThreadState_Get() directly allows the test to pass in |
| 2392 | !pydebug mode. However, the test only actually tests anything |
| 2393 | in pydebug mode, since that's where the infinite loop was in |
| 2394 | the first place. */ |
| 2395 | PyThreadState_Get(); |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2396 | Py_END_ALLOW_THREADS |
| 2397 | return NULL; |
| 2398 | } |
| 2399 | |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2400 | /* To run some code in a sub-interpreter. */ |
| 2401 | static PyObject * |
| 2402 | run_in_subinterp(PyObject *self, PyObject *args) |
| 2403 | { |
| 2404 | const char *code; |
| 2405 | int r; |
| 2406 | PyThreadState *substate, *mainstate; |
| 2407 | |
| 2408 | if (!PyArg_ParseTuple(args, "s:run_in_subinterp", |
| 2409 | &code)) |
| 2410 | return NULL; |
| 2411 | |
| 2412 | mainstate = PyThreadState_Get(); |
| 2413 | |
| 2414 | PyThreadState_Swap(NULL); |
| 2415 | |
| 2416 | substate = Py_NewInterpreter(); |
Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 2417 | if (substate == NULL) { |
| 2418 | /* Since no new thread state was created, there is no exception to |
| 2419 | propagate; raise a fresh one after swapping in the old thread |
| 2420 | state. */ |
| 2421 | PyThreadState_Swap(mainstate); |
| 2422 | PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); |
| 2423 | return NULL; |
| 2424 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2425 | r = PyRun_SimpleString(code); |
| 2426 | Py_EndInterpreter(substate); |
| 2427 | |
| 2428 | PyThreadState_Swap(mainstate); |
| 2429 | |
| 2430 | return PyLong_FromLong(r); |
| 2431 | } |
| 2432 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2433 | static PyObject * |
| 2434 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) |
| 2435 | { |
| 2436 | PyObject *obj; |
| 2437 | time_t sec; |
| 2438 | if (!PyArg_ParseTuple(args, "O:pytime_object_to_time_t", &obj)) |
| 2439 | return NULL; |
| 2440 | if (_PyTime_ObjectToTime_t(obj, &sec) == -1) |
| 2441 | return NULL; |
| 2442 | return _PyLong_FromTime_t(sec); |
| 2443 | } |
| 2444 | |
| 2445 | static PyObject * |
| 2446 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) |
| 2447 | { |
| 2448 | PyObject *obj; |
| 2449 | time_t sec; |
| 2450 | long usec; |
| 2451 | if (!PyArg_ParseTuple(args, "O:pytime_object_to_timeval", &obj)) |
| 2452 | return NULL; |
| 2453 | if (_PyTime_ObjectToTimeval(obj, &sec, &usec) == -1) |
| 2454 | return NULL; |
| 2455 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); |
| 2456 | } |
| 2457 | |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2458 | static PyObject * |
| 2459 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) |
| 2460 | { |
| 2461 | PyObject *obj; |
| 2462 | time_t sec; |
| 2463 | long nsec; |
| 2464 | if (!PyArg_ParseTuple(args, "O:pytime_object_to_timespec", &obj)) |
| 2465 | return NULL; |
| 2466 | if (_PyTime_ObjectToTimespec(obj, &sec, &nsec) == -1) |
| 2467 | return NULL; |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2468 | return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2469 | } |
| 2470 | |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 2471 | static PyObject * |
| 2472 | _test_incref(PyObject *ob) |
| 2473 | { |
| 2474 | Py_INCREF(ob); |
| 2475 | return ob; |
| 2476 | } |
| 2477 | |
| 2478 | static PyObject * |
| 2479 | test_xincref_doesnt_leak(PyObject *ob) |
| 2480 | { |
| 2481 | PyObject *obj = PyLong_FromLong(0); |
| 2482 | Py_XINCREF(_test_incref(obj)); |
| 2483 | Py_DECREF(obj); |
| 2484 | Py_DECREF(obj); |
| 2485 | Py_DECREF(obj); |
| 2486 | Py_RETURN_NONE; |
| 2487 | } |
| 2488 | |
| 2489 | static PyObject * |
| 2490 | test_incref_doesnt_leak(PyObject *ob) |
| 2491 | { |
| 2492 | PyObject *obj = PyLong_FromLong(0); |
| 2493 | Py_INCREF(_test_incref(obj)); |
| 2494 | Py_DECREF(obj); |
| 2495 | Py_DECREF(obj); |
| 2496 | Py_DECREF(obj); |
| 2497 | Py_RETURN_NONE; |
| 2498 | } |
| 2499 | |
| 2500 | static PyObject * |
| 2501 | test_xdecref_doesnt_leak(PyObject *ob) |
| 2502 | { |
| 2503 | Py_XDECREF(PyLong_FromLong(0)); |
| 2504 | Py_RETURN_NONE; |
| 2505 | } |
| 2506 | |
| 2507 | static PyObject * |
| 2508 | test_decref_doesnt_leak(PyObject *ob) |
| 2509 | { |
| 2510 | Py_DECREF(PyLong_FromLong(0)); |
| 2511 | Py_RETURN_NONE; |
| 2512 | } |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2513 | |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2514 | static PyMethodDef TestMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | {"raise_exception", raise_exception, METH_VARARGS}, |
| 2516 | {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS}, |
| 2517 | {"test_config", (PyCFunction)test_config, METH_NOARGS}, |
| 2518 | {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, |
| 2519 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, |
| 2520 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, |
| 2521 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 2523 | {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS}, |
| 2524 | {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak, METH_NOARGS}, |
| 2525 | {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak, METH_NOARGS}, |
| 2526 | {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak, METH_NOARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, |
| 2528 | METH_NOARGS}, |
Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 2529 | {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS}, |
| 2530 | {"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] | 2531 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS}, |
| 2532 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS}, |
| 2533 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, |
Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 2534 | {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS}, |
| 2536 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, |
| 2537 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, |
| 2538 | PyDoc_STR("This is a pretty normal docstring.")}, |
| 2539 | {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, |
| 2540 | {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, |
| 2541 | {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, |
| 2542 | {"getargs_tuple", getargs_tuple, METH_VARARGS}, |
| 2543 | {"getargs_keywords", (PyCFunction)getargs_keywords, |
| 2544 | METH_VARARGS|METH_KEYWORDS}, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 2545 | {"getargs_keyword_only", (PyCFunction)getargs_keyword_only, |
| 2546 | METH_VARARGS|METH_KEYWORDS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2547 | {"getargs_b", getargs_b, METH_VARARGS}, |
| 2548 | {"getargs_B", getargs_B, METH_VARARGS}, |
| 2549 | {"getargs_h", getargs_h, METH_VARARGS}, |
| 2550 | {"getargs_H", getargs_H, METH_VARARGS}, |
| 2551 | {"getargs_I", getargs_I, METH_VARARGS}, |
| 2552 | {"getargs_k", getargs_k, METH_VARARGS}, |
| 2553 | {"getargs_i", getargs_i, METH_VARARGS}, |
| 2554 | {"getargs_l", getargs_l, METH_VARARGS}, |
| 2555 | {"getargs_n", getargs_n, METH_VARARGS}, |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 2556 | {"getargs_p", getargs_p, METH_VARARGS}, |
Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2557 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2558 | {"getargs_L", getargs_L, METH_VARARGS}, |
| 2559 | {"getargs_K", getargs_K, METH_VARARGS}, |
| 2560 | {"test_longlong_api", test_longlong_api, METH_NOARGS}, |
| 2561 | {"test_long_long_and_overflow", |
| 2562 | (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, |
| 2563 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 2564 | #endif |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 2565 | {"getargs_c", getargs_c, METH_VARARGS}, |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 2566 | {"getargs_s", getargs_s, METH_VARARGS}, |
| 2567 | {"getargs_s_star", getargs_s_star, METH_VARARGS}, |
| 2568 | {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, |
| 2569 | {"getargs_z", getargs_z, METH_VARARGS}, |
| 2570 | {"getargs_z_star", getargs_z_star, METH_VARARGS}, |
| 2571 | {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, |
| 2572 | {"getargs_y", getargs_y, METH_VARARGS}, |
| 2573 | {"getargs_y_star", getargs_y_star, METH_VARARGS}, |
| 2574 | {"getargs_y_hash", getargs_y_hash, METH_VARARGS}, |
| 2575 | {"getargs_u", getargs_u, METH_VARARGS}, |
| 2576 | {"getargs_u_hash", getargs_u_hash, METH_VARARGS}, |
| 2577 | {"getargs_Z", getargs_Z, METH_VARARGS}, |
| 2578 | {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS}, |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 2579 | {"getargs_w_star", getargs_w_star, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | {"codec_incrementalencoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2581 | (PyCFunction)codec_incrementalencoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2582 | {"codec_incrementaldecoder", |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2583 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2584 | {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS}, |
| 2585 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS}, |
| 2586 | {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS}, |
| 2587 | {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS}, |
Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 2588 | {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS}, |
| 2589 | {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS}, |
| 2590 | {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS}, |
| 2591 | {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, |
Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 2592 | {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2593 | #ifdef WITH_THREAD |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2594 | {"_test_thread_state", test_thread_state, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | {"_pending_threadfunc", pending_threadfunc, METH_VARARGS}, |
Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2596 | #endif |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2597 | #ifdef HAVE_GETTIMEOFDAY |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2598 | {"profile_int", profile_int, METH_NOARGS}, |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2599 | #endif |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2600 | {"traceback_print", traceback_print, METH_VARARGS}, |
| 2601 | {"exception_print", exception_print, METH_VARARGS}, |
Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2602 | {"set_exc_info", test_set_exc_info, METH_VARARGS}, |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 2603 | {"argparsing", argparsing, METH_VARARGS}, |
| 2604 | {"code_newempty", code_newempty, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, |
| 2606 | METH_VARARGS | METH_KEYWORDS}, |
Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2607 | {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer, |
| 2608 | METH_NOARGS}, |
Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2609 | {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, |
Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2610 | {"run_in_subinterp", run_in_subinterp, METH_VARARGS}, |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2611 | {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS}, |
| 2612 | {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS}, |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2613 | {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | {NULL, NULL} /* sentinel */ |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2615 | }; |
| 2616 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2617 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} |
| 2618 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2619 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | char bool_member; |
| 2621 | char byte_member; |
| 2622 | unsigned char ubyte_member; |
| 2623 | short short_member; |
| 2624 | unsigned short ushort_member; |
| 2625 | int int_member; |
| 2626 | unsigned int uint_member; |
| 2627 | long long_member; |
| 2628 | unsigned long ulong_member; |
| 2629 | Py_ssize_t pyssizet_member; |
| 2630 | float float_member; |
| 2631 | double double_member; |
| 2632 | char inplace_member[6]; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2633 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | PY_LONG_LONG longlong_member; |
| 2635 | unsigned PY_LONG_LONG ulonglong_member; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2636 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2637 | } all_structmembers; |
| 2638 | |
| 2639 | typedef struct { |
| 2640 | PyObject_HEAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | all_structmembers structmembers; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2642 | } test_structmembers; |
| 2643 | |
| 2644 | static struct PyMemberDef test_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, |
| 2646 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, |
| 2647 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, |
| 2648 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, |
| 2649 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, |
| 2650 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, |
| 2651 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, |
| 2652 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, |
| 2653 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, |
| 2654 | {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, |
| 2655 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, |
| 2656 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, |
| 2657 | {"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] | 2658 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, |
| 2660 | {"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] | 2661 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | {NULL} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2663 | }; |
| 2664 | |
| 2665 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2666 | static PyObject * |
| 2667 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 2668 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | static char *keywords[] = { |
| 2670 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", |
| 2671 | "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", |
| 2672 | "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 2673 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | "T_LONGLONG", "T_ULONGLONG", |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2675 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | NULL}; |
| 2677 | static char *fmt = "|bbBhHiIlknfds#" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2678 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | "LK" |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2680 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | ; |
| 2682 | test_structmembers *ob; |
| 2683 | const char *s = NULL; |
| 2684 | Py_ssize_t string_len = 0; |
| 2685 | ob = PyObject_New(test_structmembers, type); |
| 2686 | if (ob == NULL) |
| 2687 | return NULL; |
| 2688 | memset(&ob->structmembers, 0, sizeof(all_structmembers)); |
| 2689 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 2690 | &ob->structmembers.bool_member, |
| 2691 | &ob->structmembers.byte_member, |
| 2692 | &ob->structmembers.ubyte_member, |
| 2693 | &ob->structmembers.short_member, |
| 2694 | &ob->structmembers.ushort_member, |
| 2695 | &ob->structmembers.int_member, |
| 2696 | &ob->structmembers.uint_member, |
| 2697 | &ob->structmembers.long_member, |
| 2698 | &ob->structmembers.ulong_member, |
| 2699 | &ob->structmembers.pyssizet_member, |
| 2700 | &ob->structmembers.float_member, |
| 2701 | &ob->structmembers.double_member, |
| 2702 | &s, &string_len |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2703 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2704 | , &ob->structmembers.longlong_member, |
| 2705 | &ob->structmembers.ulonglong_member |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2706 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | )) { |
| 2708 | Py_DECREF(ob); |
| 2709 | return NULL; |
| 2710 | } |
| 2711 | if (s != NULL) { |
| 2712 | if (string_len > 5) { |
| 2713 | Py_DECREF(ob); |
| 2714 | PyErr_SetString(PyExc_ValueError, "string too long"); |
| 2715 | return NULL; |
| 2716 | } |
| 2717 | strcpy(ob->structmembers.inplace_member, s); |
| 2718 | } |
| 2719 | else { |
| 2720 | strcpy(ob->structmembers.inplace_member, ""); |
| 2721 | } |
| 2722 | return (PyObject *)ob; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 2725 | static void |
| 2726 | test_structmembers_free(PyObject *ob) |
| 2727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | PyObject_FREE(ob); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | static PyTypeObject test_structmembersType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2732 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | "test_structmembersType", |
| 2734 | sizeof(test_structmembers), /* tp_basicsize */ |
| 2735 | 0, /* tp_itemsize */ |
| 2736 | test_structmembers_free, /* destructor tp_dealloc */ |
| 2737 | 0, /* tp_print */ |
| 2738 | 0, /* tp_getattr */ |
| 2739 | 0, /* tp_setattr */ |
| 2740 | 0, /* tp_reserved */ |
| 2741 | 0, /* tp_repr */ |
| 2742 | 0, /* tp_as_number */ |
| 2743 | 0, /* tp_as_sequence */ |
| 2744 | 0, /* tp_as_mapping */ |
| 2745 | 0, /* tp_hash */ |
| 2746 | 0, /* tp_call */ |
| 2747 | 0, /* tp_str */ |
| 2748 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2749 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 2750 | 0, /* tp_as_buffer */ |
| 2751 | 0, /* tp_flags */ |
| 2752 | "Type containing all structmember types", |
| 2753 | 0, /* traverseproc tp_traverse */ |
| 2754 | 0, /* tp_clear */ |
| 2755 | 0, /* tp_richcompare */ |
| 2756 | 0, /* tp_weaklistoffset */ |
| 2757 | 0, /* tp_iter */ |
| 2758 | 0, /* tp_iternext */ |
| 2759 | 0, /* tp_methods */ |
| 2760 | test_members, /* tp_members */ |
| 2761 | 0, |
| 2762 | 0, |
| 2763 | 0, |
| 2764 | 0, |
| 2765 | 0, |
| 2766 | 0, |
| 2767 | 0, |
| 2768 | 0, |
| 2769 | test_structmembers_new, /* tp_new */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2770 | }; |
| 2771 | |
| 2772 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2773 | |
| 2774 | static struct PyModuleDef _testcapimodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | PyModuleDef_HEAD_INIT, |
| 2776 | "_testcapi", |
| 2777 | NULL, |
| 2778 | -1, |
| 2779 | TestMethods, |
| 2780 | NULL, |
| 2781 | NULL, |
| 2782 | NULL, |
| 2783 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2784 | }; |
| 2785 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 2786 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2787 | PyInit__testcapi(void) |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | PyObject *m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | m = PyModule_Create(&_testcapimodule); |
| 2792 | if (m == NULL) |
| 2793 | return NULL; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; |
Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 2796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2797 | Py_TYPE(&test_structmembersType)=&PyType_Type; |
| 2798 | Py_INCREF(&test_structmembersType); |
| 2799 | /* don't use a name starting with "test", since we don't want |
| 2800 | test_capi to automatically call this */ |
| 2801 | PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); |
| 2804 | PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); |
| 2805 | PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); |
| 2806 | PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); |
| 2807 | PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); |
| 2808 | PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); |
| 2809 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX)); |
| 2810 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN)); |
| 2811 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX)); |
| 2812 | PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); |
| 2813 | PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); |
| 2814 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); |
| 2815 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); |
| 2816 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); |
| 2817 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); |
| 2818 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); |
| 2819 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); |
| 2820 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); |
| 2821 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); |
| 2822 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); |
| 2823 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); |
| 2824 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); |
| 2825 | Py_INCREF(&PyInstanceMethod_Type); |
| 2826 | PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 2827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2828 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL); |
| 2829 | Py_INCREF(TestError); |
| 2830 | PyModule_AddObject(m, "error", TestError); |
| 2831 | return m; |
Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 2832 | } |