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