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