| 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" | 
| Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 14 | #include "marshal.h" | 
| Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 15 | #include <signal.h> | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 16 |  | 
| Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 17 | #ifdef MS_WINDOWS | 
| Victor Stinner | 09e5cf2 | 2015-03-30 00:09:18 +0200 | [diff] [blame] | 18 | #  include <winsock2.h>         /* struct timeval */ | 
| Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 19 | #endif | 
 | 20 |  | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 21 | #ifdef WITH_THREAD | 
 | 22 | #include "pythread.h" | 
 | 23 | #endif /* WITH_THREAD */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | static PyObject *TestError;     /* set to exception object in init */ | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 25 |  | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 26 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ | 
 | 27 |  | 
 | 28 | static PyObject * | 
 | 29 | raiseTestError(const char* test_name, const char* msg) | 
 | 30 | { | 
| Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 31 |     PyErr_Format(TestError, "%s: %s", test_name, msg); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 |     return NULL; | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 33 | } | 
 | 34 |  | 
| Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 35 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines). | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 36 |  | 
 | 37 |    The ones derived from autoconf on the UNIX-like OSes can be relied | 
 | 38 |    upon (in the absence of sloppy cross-compiling), but the Windows | 
 | 39 |    platforms have these hardcoded.  Better safe than sorry. | 
 | 40 | */ | 
 | 41 | static PyObject* | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 42 | sizeof_error(const char* fatname, const char* typname, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 |     int expected, int got) | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 44 | { | 
| Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 45 |     PyErr_Format(TestError, | 
 | 46 |         "%s #define == %d but sizeof(%s) == %d", | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 |         fatname, expected, typname, got); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 |     return (PyObject*)NULL; | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 49 | } | 
 | 50 |  | 
 | 51 | static PyObject* | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 52 | test_config(PyObject *self) | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 53 | { | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 54 | #define CHECK_SIZEOF(FATNAME, TYPE) \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 |             if (FATNAME != sizeof(TYPE)) \ | 
 | 56 |                 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE)) | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 57 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 |     CHECK_SIZEOF(SIZEOF_SHORT, short); | 
 | 59 |     CHECK_SIZEOF(SIZEOF_INT, int); | 
 | 60 |     CHECK_SIZEOF(SIZEOF_LONG, long); | 
 | 61 |     CHECK_SIZEOF(SIZEOF_VOID_P, void*); | 
 | 62 |     CHECK_SIZEOF(SIZEOF_TIME_T, time_t); | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 63 |     CHECK_SIZEOF(SIZEOF_LONG_LONG, long long); | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 64 |  | 
 | 65 | #undef CHECK_SIZEOF | 
 | 66 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 |     Py_INCREF(Py_None); | 
 | 68 |     return Py_None; | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 69 | } | 
 | 70 |  | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 71 | static PyObject* | 
| Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 72 | test_sizeof_c_types(PyObject *self) | 
 | 73 | { | 
| Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 74 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) | 
| Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 75 | #pragma GCC diagnostic push | 
 | 76 | #pragma GCC diagnostic ignored "-Wtype-limits" | 
 | 77 | #endif | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 78 | #define CHECK_SIZEOF(TYPE, EXPECTED)         \ | 
| Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 79 |     if (EXPECTED != sizeof(TYPE))  {         \ | 
 | 80 |         PyErr_Format(TestError,              \ | 
 | 81 |             "sizeof(%s) = %u instead of %u", \ | 
 | 82 |             #TYPE, sizeof(TYPE), EXPECTED);  \ | 
 | 83 |         return (PyObject*)NULL;              \ | 
 | 84 |     } | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 85 | #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0) | 
 | 86 | #define CHECK_SIGNNESS(TYPE, SIGNED)         \ | 
 | 87 |     if (IS_SIGNED(TYPE) != SIGNED) {         \ | 
 | 88 |         PyErr_Format(TestError,              \ | 
 | 89 |             "%s signness is, instead of %i",  \ | 
 | 90 |             #TYPE, IS_SIGNED(TYPE), SIGNED); \ | 
 | 91 |         return (PyObject*)NULL;              \ | 
 | 92 |     } | 
| Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 93 |  | 
 | 94 |     /* integer types */ | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 95 |     CHECK_SIZEOF(Py_UCS1, 1); | 
 | 96 |     CHECK_SIZEOF(Py_UCS2, 2); | 
 | 97 |     CHECK_SIZEOF(Py_UCS4, 4); | 
 | 98 |     CHECK_SIGNNESS(Py_UCS1, 0); | 
 | 99 |     CHECK_SIGNNESS(Py_UCS2, 0); | 
 | 100 |     CHECK_SIGNNESS(Py_UCS4, 0); | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 101 |     CHECK_SIZEOF(PY_INT32_T, 4); | 
 | 102 |     CHECK_SIGNNESS(PY_INT32_T, 1); | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 103 |     CHECK_SIZEOF(PY_UINT32_T, 4); | 
 | 104 |     CHECK_SIGNNESS(PY_UINT32_T, 0); | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 105 |     CHECK_SIZEOF(PY_INT64_T, 8); | 
 | 106 |     CHECK_SIGNNESS(PY_INT64_T, 1); | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 107 |     CHECK_SIZEOF(PY_UINT64_T, 8); | 
 | 108 |     CHECK_SIGNNESS(PY_UINT64_T, 0); | 
| Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 109 |  | 
 | 110 |     /* pointer/size types */ | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 111 |     CHECK_SIZEOF(size_t, sizeof(void *)); | 
 | 112 |     CHECK_SIGNNESS(size_t, 0); | 
 | 113 |     CHECK_SIZEOF(Py_ssize_t, sizeof(void *)); | 
 | 114 |     CHECK_SIGNNESS(Py_ssize_t, 1); | 
 | 115 |  | 
 | 116 |     CHECK_SIZEOF(Py_uintptr_t, sizeof(void *)); | 
 | 117 |     CHECK_SIGNNESS(Py_uintptr_t, 0); | 
 | 118 |     CHECK_SIZEOF(Py_intptr_t, sizeof(void *)); | 
 | 119 |     CHECK_SIGNNESS(Py_intptr_t, 1); | 
| Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 120 |  | 
 | 121 |     Py_INCREF(Py_None); | 
 | 122 |     return Py_None; | 
 | 123 |  | 
| Victor Stinner | f866f97 | 2013-10-29 19:59:31 +0100 | [diff] [blame] | 124 | #undef IS_SIGNED | 
 | 125 | #undef CHECK_SIGNESS | 
| Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 126 | #undef CHECK_SIZEOF | 
| Ned Deily | e37a194 | 2015-03-05 15:47:10 -0800 | [diff] [blame] | 127 | #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))) | 
| Serhiy Storchaka | b48af34 | 2015-02-26 15:27:57 +0200 | [diff] [blame] | 128 | #pragma GCC diagnostic pop | 
 | 129 | #endif | 
| Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 130 | } | 
 | 131 |  | 
 | 132 |  | 
 | 133 | static PyObject* | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 134 | test_list_api(PyObject *self) | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 135 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 |     PyObject* list; | 
 | 137 |     int i; | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 138 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 |     /* SF bug 132008:  PyList_Reverse segfaults */ | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 140 | #define NLIST 30 | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 |     list = PyList_New(NLIST); | 
 | 142 |     if (list == (PyObject*)NULL) | 
 | 143 |         return (PyObject*)NULL; | 
 | 144 |     /* list = range(NLIST) */ | 
 | 145 |     for (i = 0; i < NLIST; ++i) { | 
 | 146 |         PyObject* anint = PyLong_FromLong(i); | 
 | 147 |         if (anint == (PyObject*)NULL) { | 
 | 148 |             Py_DECREF(list); | 
 | 149 |             return (PyObject*)NULL; | 
 | 150 |         } | 
 | 151 |         PyList_SET_ITEM(list, i, anint); | 
 | 152 |     } | 
 | 153 |     /* list.reverse(), via PyList_Reverse() */ | 
 | 154 |     i = PyList_Reverse(list);   /* should not blow up! */ | 
 | 155 |     if (i != 0) { | 
 | 156 |         Py_DECREF(list); | 
 | 157 |         return (PyObject*)NULL; | 
 | 158 |     } | 
 | 159 |     /* Check that list == range(29, -1, -1) now */ | 
 | 160 |     for (i = 0; i < NLIST; ++i) { | 
 | 161 |         PyObject* anint = PyList_GET_ITEM(list, i); | 
 | 162 |         if (PyLong_AS_LONG(anint) != NLIST-1-i) { | 
 | 163 |             PyErr_SetString(TestError, | 
 | 164 |                             "test_list_api: reverse screwed up"); | 
 | 165 |             Py_DECREF(list); | 
 | 166 |             return (PyObject*)NULL; | 
 | 167 |         } | 
 | 168 |     } | 
 | 169 |     Py_DECREF(list); | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 170 | #undef NLIST | 
 | 171 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 |     Py_INCREF(Py_None); | 
 | 173 |     return Py_None; | 
| Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 174 | } | 
 | 175 |  | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 176 | static int | 
 | 177 | test_dict_inner(int count) | 
 | 178 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 |     Py_ssize_t pos = 0, iterations = 0; | 
 | 180 |     int i; | 
 | 181 |     PyObject *dict = PyDict_New(); | 
 | 182 |     PyObject *v, *k; | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 183 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 |     if (dict == NULL) | 
 | 185 |         return -1; | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 186 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 |     for (i = 0; i < count; i++) { | 
 | 188 |         v = PyLong_FromLong(i); | 
| Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 189 |         if (v == NULL) { | 
| Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 190 |             return -1; | 
 | 191 |         } | 
| Christian Heimes | 97cb67b | 2013-07-20 15:01:26 +0200 | [diff] [blame] | 192 |         if (PyDict_SetItem(dict, v, v) < 0) { | 
 | 193 |             Py_DECREF(v); | 
 | 194 |             return -1; | 
 | 195 |         } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 |         Py_DECREF(v); | 
 | 197 |     } | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 198 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 |     while (PyDict_Next(dict, &pos, &k, &v)) { | 
 | 200 |         PyObject *o; | 
 | 201 |         iterations++; | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 202 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 |         i = PyLong_AS_LONG(v) + 1; | 
 | 204 |         o = PyLong_FromLong(i); | 
 | 205 |         if (o == NULL) | 
 | 206 |             return -1; | 
 | 207 |         if (PyDict_SetItem(dict, k, o) < 0) { | 
 | 208 |             Py_DECREF(o); | 
 | 209 |             return -1; | 
 | 210 |         } | 
 | 211 |         Py_DECREF(o); | 
 | 212 |     } | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 213 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 |     Py_DECREF(dict); | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 215 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 |     if (iterations != count) { | 
 | 217 |         PyErr_SetString( | 
 | 218 |             TestError, | 
 | 219 |             "test_dict_iteration: dict iteration went wrong "); | 
 | 220 |         return -1; | 
 | 221 |     } else { | 
 | 222 |         return 0; | 
 | 223 |     } | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 224 | } | 
 | 225 |  | 
 | 226 | static PyObject* | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 227 | test_dict_iteration(PyObject* self) | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 228 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 |     int i; | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 230 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 |     for (i = 0; i < 200; i++) { | 
 | 232 |         if (test_dict_inner(i) < 0) { | 
 | 233 |             return NULL; | 
 | 234 |         } | 
 | 235 |     } | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 236 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 |     Py_INCREF(Py_None); | 
 | 238 |     return Py_None; | 
| Guido van Rossum | eb0d992 | 2001-04-13 17:08:15 +0000 | [diff] [blame] | 239 | } | 
 | 240 |  | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 241 |  | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 242 | /* Issue #4701: Check that PyObject_Hash implicitly calls | 
 | 243 |  *   PyType_Ready if it hasn't already been called | 
 | 244 |  */ | 
 | 245 | static PyTypeObject _HashInheritanceTester_Type = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 247 |     "hashinheritancetester",            /* Name of this type */ | 
 | 248 |     sizeof(PyObject),           /* Basic object size */ | 
 | 249 |     0,                          /* Item size for varobject */ | 
 | 250 |     (destructor)PyObject_Del, /* tp_dealloc */ | 
 | 251 |     0,                          /* tp_print */ | 
 | 252 |     0,                          /* tp_getattr */ | 
 | 253 |     0,                          /* tp_setattr */ | 
 | 254 |     0,                          /* tp_reserved */ | 
 | 255 |     0,                          /* tp_repr */ | 
 | 256 |     0,                          /* tp_as_number */ | 
 | 257 |     0,                          /* tp_as_sequence */ | 
 | 258 |     0,                          /* tp_as_mapping */ | 
 | 259 |     0,                          /* tp_hash */ | 
 | 260 |     0,                          /* tp_call */ | 
 | 261 |     0,                          /* tp_str */ | 
 | 262 |     PyObject_GenericGetAttr,  /* tp_getattro */ | 
 | 263 |     0,                          /* tp_setattro */ | 
 | 264 |     0,                          /* tp_as_buffer */ | 
 | 265 |     Py_TPFLAGS_DEFAULT,         /* tp_flags */ | 
 | 266 |     0,                          /* tp_doc */ | 
 | 267 |     0,                          /* tp_traverse */ | 
 | 268 |     0,                          /* tp_clear */ | 
 | 269 |     0,                          /* tp_richcompare */ | 
 | 270 |     0,                          /* tp_weaklistoffset */ | 
 | 271 |     0,                          /* tp_iter */ | 
 | 272 |     0,                          /* tp_iternext */ | 
 | 273 |     0,                          /* tp_methods */ | 
 | 274 |     0,                          /* tp_members */ | 
 | 275 |     0,                          /* tp_getset */ | 
 | 276 |     0,                          /* tp_base */ | 
 | 277 |     0,                          /* tp_dict */ | 
 | 278 |     0,                          /* tp_descr_get */ | 
 | 279 |     0,                          /* tp_descr_set */ | 
 | 280 |     0,                          /* tp_dictoffset */ | 
 | 281 |     0,                          /* tp_init */ | 
 | 282 |     0,                          /* tp_alloc */ | 
 | 283 |     PyType_GenericNew,                  /* tp_new */ | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 284 | }; | 
 | 285 |  | 
 | 286 | static PyObject* | 
 | 287 | test_lazy_hash_inheritance(PyObject* self) | 
 | 288 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 |     PyTypeObject *type; | 
 | 290 |     PyObject *obj; | 
| Antoine Pitrou | 29aad00 | 2010-10-23 19:42:38 +0000 | [diff] [blame] | 291 |     Py_hash_t hash; | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 292 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 |     type = &_HashInheritanceTester_Type; | 
| Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 294 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 |     if (type->tp_dict != NULL) | 
 | 296 |         /* The type has already been initialized. This probably means | 
 | 297 |            -R is being used. */ | 
 | 298 |         Py_RETURN_NONE; | 
| Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 299 |  | 
 | 300 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 |     obj = PyObject_New(PyObject, type); | 
 | 302 |     if (obj == NULL) { | 
 | 303 |         PyErr_Clear(); | 
 | 304 |         PyErr_SetString( | 
 | 305 |             TestError, | 
 | 306 |             "test_lazy_hash_inheritance: failed to create object"); | 
 | 307 |         return NULL; | 
 | 308 |     } | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 309 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 |     if (type->tp_dict != NULL) { | 
 | 311 |         PyErr_SetString( | 
 | 312 |             TestError, | 
 | 313 |             "test_lazy_hash_inheritance: type initialised too soon"); | 
 | 314 |         Py_DECREF(obj); | 
 | 315 |         return NULL; | 
 | 316 |     } | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 317 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 |     hash = PyObject_Hash(obj); | 
 | 319 |     if ((hash == -1) && PyErr_Occurred()) { | 
 | 320 |         PyErr_Clear(); | 
 | 321 |         PyErr_SetString( | 
 | 322 |             TestError, | 
 | 323 |             "test_lazy_hash_inheritance: could not hash object"); | 
 | 324 |         Py_DECREF(obj); | 
 | 325 |         return NULL; | 
 | 326 |     } | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 327 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 |     if (type->tp_dict == NULL) { | 
 | 329 |         PyErr_SetString( | 
 | 330 |             TestError, | 
 | 331 |             "test_lazy_hash_inheritance: type not initialised by hash()"); | 
 | 332 |         Py_DECREF(obj); | 
 | 333 |         return NULL; | 
 | 334 |     } | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 335 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 |     if (type->tp_hash != PyType_Type.tp_hash) { | 
 | 337 |         PyErr_SetString( | 
 | 338 |             TestError, | 
 | 339 |             "test_lazy_hash_inheritance: unexpected hash function"); | 
 | 340 |         Py_DECREF(obj); | 
 | 341 |         return NULL; | 
 | 342 |     } | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 343 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 |     Py_DECREF(obj); | 
| Benjamin Peterson | f172637 | 2009-05-05 21:11:54 +0000 | [diff] [blame] | 345 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 |     Py_RETURN_NONE; | 
| Nick Coghlan | f1f2f68 | 2008-12-30 07:29:12 +0000 | [diff] [blame] | 347 | } | 
 | 348 |  | 
 | 349 |  | 
| Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 350 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and | 
| Tim Peters | ff70d3c | 2001-06-14 01:11:03 +0000 | [diff] [blame] | 351 |    PyLong_{As, From}{Unsigned,}LongLong(). | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 352 |  | 
 | 353 |    Note that the meat of the test is contained in testcapi_long.h. | 
 | 354 |    This is revolting, but delicate code duplication is worse:  "almost | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 355 |    exactly the same" code is needed to test long long, but the ubiquitous | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 356 |    dependence on type names makes it impossible to use a parameterized | 
 | 357 |    function.  A giant macro would be even worse than this.  A C++ template | 
 | 358 |    would be perfect. | 
 | 359 |  | 
 | 360 |    The "report an error" functions are deliberately not part of the #include | 
 | 361 |    file:  if the test fails, you can set a breakpoint in the appropriate | 
 | 362 |    error function directly, and crawl back from there in the debugger. | 
 | 363 | */ | 
 | 364 |  | 
 | 365 | #define UNBIND(X)  Py_DECREF(X); (X) = NULL | 
 | 366 |  | 
 | 367 | static PyObject * | 
 | 368 | raise_test_long_error(const char* msg) | 
 | 369 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 |     return raiseTestError("test_long_api", msg); | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 371 | } | 
 | 372 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | #define TESTNAME        test_long_api_inner | 
 | 374 | #define TYPENAME        long | 
 | 375 | #define F_S_TO_PY       PyLong_FromLong | 
 | 376 | #define F_PY_TO_S       PyLong_AsLong | 
 | 377 | #define F_U_TO_PY       PyLong_FromUnsignedLong | 
 | 378 | #define F_PY_TO_U       PyLong_AsUnsignedLong | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 379 |  | 
 | 380 | #include "testcapi_long.h" | 
 | 381 |  | 
 | 382 | static PyObject * | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 383 | test_long_api(PyObject* self) | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 384 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 |     return TESTNAME(raise_test_long_error); | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 386 | } | 
 | 387 |  | 
 | 388 | #undef TESTNAME | 
 | 389 | #undef TYPENAME | 
 | 390 | #undef F_S_TO_PY | 
 | 391 | #undef F_PY_TO_S | 
 | 392 | #undef F_U_TO_PY | 
 | 393 | #undef F_PY_TO_U | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 394 |  | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 395 | static PyObject * | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 396 | raise_test_longlong_error(const char* msg) | 
 | 397 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 |     return raiseTestError("test_longlong_api", msg); | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 399 | } | 
 | 400 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | #define TESTNAME        test_longlong_api_inner | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 402 | #define TYPENAME        long long | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | #define F_S_TO_PY       PyLong_FromLongLong | 
 | 404 | #define F_PY_TO_S       PyLong_AsLongLong | 
 | 405 | #define F_U_TO_PY       PyLong_FromUnsignedLongLong | 
 | 406 | #define F_PY_TO_U       PyLong_AsUnsignedLongLong | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 407 |  | 
 | 408 | #include "testcapi_long.h" | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 409 |  | 
 | 410 | static PyObject * | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 411 | test_longlong_api(PyObject* self, PyObject *args) | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 412 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 |     return TESTNAME(raise_test_longlong_error); | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 414 | } | 
 | 415 |  | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 416 | #undef TESTNAME | 
 | 417 | #undef TYPENAME | 
 | 418 | #undef F_S_TO_PY | 
 | 419 | #undef F_PY_TO_S | 
 | 420 | #undef F_U_TO_PY | 
 | 421 | #undef F_PY_TO_U | 
| Tim Peters | e7c1f9b | 2001-06-14 00:55:41 +0000 | [diff] [blame] | 422 |  | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 423 | /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG | 
 | 424 |    is tested by test_long_api_inner. This test will concentrate on proper | 
 | 425 |    handling of overflow. | 
 | 426 | */ | 
 | 427 |  | 
 | 428 | static PyObject * | 
 | 429 | test_long_and_overflow(PyObject *self) | 
 | 430 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 |     PyObject *num, *one, *temp; | 
 | 432 |     long value; | 
 | 433 |     int overflow; | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 434 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 |     /* Test that overflow is set properly for a large value. */ | 
 | 436 |     /* num is a number larger than LONG_MAX even on 64-bit platforms */ | 
 | 437 |     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); | 
 | 438 |     if (num == NULL) | 
 | 439 |         return NULL; | 
 | 440 |     overflow = 1234; | 
 | 441 |     value = PyLong_AsLongAndOverflow(num, &overflow); | 
 | 442 |     Py_DECREF(num); | 
 | 443 |     if (value == -1 && PyErr_Occurred()) | 
 | 444 |         return NULL; | 
 | 445 |     if (value != -1) | 
 | 446 |         return raiseTestError("test_long_and_overflow", | 
 | 447 |             "return value was not set to -1"); | 
 | 448 |     if (overflow != 1) | 
 | 449 |         return raiseTestError("test_long_and_overflow", | 
 | 450 |             "overflow was not set to 1"); | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 451 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 |     /* Same again, with num = LONG_MAX + 1 */ | 
 | 453 |     num = PyLong_FromLong(LONG_MAX); | 
 | 454 |     if (num == NULL) | 
 | 455 |         return NULL; | 
 | 456 |     one = PyLong_FromLong(1L); | 
 | 457 |     if (one == NULL) { | 
 | 458 |         Py_DECREF(num); | 
 | 459 |         return NULL; | 
 | 460 |     } | 
 | 461 |     temp = PyNumber_Add(num, one); | 
 | 462 |     Py_DECREF(one); | 
 | 463 |     Py_DECREF(num); | 
 | 464 |     num = temp; | 
 | 465 |     if (num == NULL) | 
 | 466 |         return NULL; | 
 | 467 |     overflow = 0; | 
 | 468 |     value = PyLong_AsLongAndOverflow(num, &overflow); | 
 | 469 |     Py_DECREF(num); | 
 | 470 |     if (value == -1 && PyErr_Occurred()) | 
 | 471 |         return NULL; | 
 | 472 |     if (value != -1) | 
 | 473 |         return raiseTestError("test_long_and_overflow", | 
 | 474 |             "return value was not set to -1"); | 
 | 475 |     if (overflow != 1) | 
 | 476 |         return raiseTestError("test_long_and_overflow", | 
 | 477 |             "overflow was not set to 1"); | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 478 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 |     /* Test that overflow is set properly for a large negative value. */ | 
 | 480 |     /* num is a number smaller than LONG_MIN even on 64-bit platforms */ | 
 | 481 |     num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); | 
 | 482 |     if (num == NULL) | 
 | 483 |         return NULL; | 
 | 484 |     overflow = 1234; | 
 | 485 |     value = PyLong_AsLongAndOverflow(num, &overflow); | 
 | 486 |     Py_DECREF(num); | 
 | 487 |     if (value == -1 && PyErr_Occurred()) | 
 | 488 |         return NULL; | 
 | 489 |     if (value != -1) | 
 | 490 |         return raiseTestError("test_long_and_overflow", | 
 | 491 |             "return value was not set to -1"); | 
 | 492 |     if (overflow != -1) | 
 | 493 |         return raiseTestError("test_long_and_overflow", | 
 | 494 |             "overflow was not set to -1"); | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 495 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 |     /* Same again, with num = LONG_MIN - 1 */ | 
 | 497 |     num = PyLong_FromLong(LONG_MIN); | 
 | 498 |     if (num == NULL) | 
 | 499 |         return NULL; | 
 | 500 |     one = PyLong_FromLong(1L); | 
 | 501 |     if (one == NULL) { | 
 | 502 |         Py_DECREF(num); | 
 | 503 |         return NULL; | 
 | 504 |     } | 
 | 505 |     temp = PyNumber_Subtract(num, one); | 
 | 506 |     Py_DECREF(one); | 
 | 507 |     Py_DECREF(num); | 
 | 508 |     num = temp; | 
 | 509 |     if (num == NULL) | 
 | 510 |         return NULL; | 
 | 511 |     overflow = 0; | 
 | 512 |     value = PyLong_AsLongAndOverflow(num, &overflow); | 
 | 513 |     Py_DECREF(num); | 
 | 514 |     if (value == -1 && PyErr_Occurred()) | 
 | 515 |         return NULL; | 
 | 516 |     if (value != -1) | 
 | 517 |         return raiseTestError("test_long_and_overflow", | 
 | 518 |             "return value was not set to -1"); | 
 | 519 |     if (overflow != -1) | 
 | 520 |         return raiseTestError("test_long_and_overflow", | 
 | 521 |             "overflow was not set to -1"); | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 522 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 |     /* Test that overflow is cleared properly for small values. */ | 
 | 524 |     num = PyLong_FromString("FF", NULL, 16); | 
 | 525 |     if (num == NULL) | 
 | 526 |         return NULL; | 
 | 527 |     overflow = 1234; | 
 | 528 |     value = PyLong_AsLongAndOverflow(num, &overflow); | 
 | 529 |     Py_DECREF(num); | 
 | 530 |     if (value == -1 && PyErr_Occurred()) | 
 | 531 |         return NULL; | 
 | 532 |     if (value != 0xFF) | 
 | 533 |         return raiseTestError("test_long_and_overflow", | 
 | 534 |             "expected return value 0xFF"); | 
 | 535 |     if (overflow != 0) | 
 | 536 |         return raiseTestError("test_long_and_overflow", | 
 | 537 |             "overflow was not cleared"); | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 538 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 |     num = PyLong_FromString("-FF", NULL, 16); | 
 | 540 |     if (num == NULL) | 
 | 541 |         return NULL; | 
 | 542 |     overflow = 0; | 
 | 543 |     value = PyLong_AsLongAndOverflow(num, &overflow); | 
 | 544 |     Py_DECREF(num); | 
 | 545 |     if (value == -1 && PyErr_Occurred()) | 
 | 546 |         return NULL; | 
 | 547 |     if (value != -0xFF) | 
 | 548 |         return raiseTestError("test_long_and_overflow", | 
 | 549 |             "expected return value 0xFF"); | 
 | 550 |     if (overflow != 0) | 
 | 551 |         return raiseTestError("test_long_and_overflow", | 
 | 552 |             "overflow was set incorrectly"); | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 553 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 |     num = PyLong_FromLong(LONG_MAX); | 
 | 555 |     if (num == NULL) | 
 | 556 |         return NULL; | 
 | 557 |     overflow = 1234; | 
 | 558 |     value = PyLong_AsLongAndOverflow(num, &overflow); | 
 | 559 |     Py_DECREF(num); | 
 | 560 |     if (value == -1 && PyErr_Occurred()) | 
 | 561 |         return NULL; | 
 | 562 |     if (value != LONG_MAX) | 
 | 563 |         return raiseTestError("test_long_and_overflow", | 
 | 564 |             "expected return value LONG_MAX"); | 
 | 565 |     if (overflow != 0) | 
 | 566 |         return raiseTestError("test_long_and_overflow", | 
 | 567 |             "overflow was not cleared"); | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 568 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 |     num = PyLong_FromLong(LONG_MIN); | 
 | 570 |     if (num == NULL) | 
 | 571 |         return NULL; | 
 | 572 |     overflow = 0; | 
 | 573 |     value = PyLong_AsLongAndOverflow(num, &overflow); | 
 | 574 |     Py_DECREF(num); | 
 | 575 |     if (value == -1 && PyErr_Occurred()) | 
 | 576 |         return NULL; | 
 | 577 |     if (value != LONG_MIN) | 
 | 578 |         return raiseTestError("test_long_and_overflow", | 
 | 579 |             "expected return value LONG_MIN"); | 
 | 580 |     if (overflow != 0) | 
 | 581 |         return raiseTestError("test_long_and_overflow", | 
 | 582 |             "overflow was not cleared"); | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 583 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 |     Py_INCREF(Py_None); | 
 | 585 |     return Py_None; | 
| Mark Dickinson | 309aa2d | 2009-12-21 12:37:06 +0000 | [diff] [blame] | 586 | } | 
 | 587 |  | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 588 | /* Test the PyLong_AsLongLongAndOverflow API. General conversion to | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 589 |    long long is tested by test_long_api_inner. This test will | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 590 |    concentrate on proper handling of overflow. | 
 | 591 | */ | 
 | 592 |  | 
 | 593 | static PyObject * | 
 | 594 | test_long_long_and_overflow(PyObject *self) | 
 | 595 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 |     PyObject *num, *one, *temp; | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 597 |     long long value; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 |     int overflow; | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 599 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 |     /* Test that overflow is set properly for a large value. */ | 
 | 601 |     /* num is a number larger than PY_LLONG_MAX on a typical machine. */ | 
 | 602 |     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); | 
 | 603 |     if (num == NULL) | 
 | 604 |         return NULL; | 
 | 605 |     overflow = 1234; | 
 | 606 |     value = PyLong_AsLongLongAndOverflow(num, &overflow); | 
 | 607 |     Py_DECREF(num); | 
 | 608 |     if (value == -1 && PyErr_Occurred()) | 
 | 609 |         return NULL; | 
 | 610 |     if (value != -1) | 
 | 611 |         return raiseTestError("test_long_long_and_overflow", | 
 | 612 |             "return value was not set to -1"); | 
 | 613 |     if (overflow != 1) | 
 | 614 |         return raiseTestError("test_long_long_and_overflow", | 
 | 615 |             "overflow was not set to 1"); | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 616 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 |     /* Same again, with num = PY_LLONG_MAX + 1 */ | 
 | 618 |     num = PyLong_FromLongLong(PY_LLONG_MAX); | 
 | 619 |     if (num == NULL) | 
 | 620 |         return NULL; | 
 | 621 |     one = PyLong_FromLong(1L); | 
 | 622 |     if (one == NULL) { | 
 | 623 |         Py_DECREF(num); | 
 | 624 |         return NULL; | 
 | 625 |     } | 
 | 626 |     temp = PyNumber_Add(num, one); | 
 | 627 |     Py_DECREF(one); | 
 | 628 |     Py_DECREF(num); | 
 | 629 |     num = temp; | 
 | 630 |     if (num == NULL) | 
 | 631 |         return NULL; | 
 | 632 |     overflow = 0; | 
 | 633 |     value = PyLong_AsLongLongAndOverflow(num, &overflow); | 
 | 634 |     Py_DECREF(num); | 
 | 635 |     if (value == -1 && PyErr_Occurred()) | 
 | 636 |         return NULL; | 
 | 637 |     if (value != -1) | 
 | 638 |         return raiseTestError("test_long_long_and_overflow", | 
 | 639 |             "return value was not set to -1"); | 
 | 640 |     if (overflow != 1) | 
 | 641 |         return raiseTestError("test_long_long_and_overflow", | 
 | 642 |             "overflow was not set to 1"); | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 643 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 |     /* Test that overflow is set properly for a large negative value. */ | 
 | 645 |     /* num is a number smaller than PY_LLONG_MIN on a typical platform */ | 
 | 646 |     num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); | 
 | 647 |     if (num == NULL) | 
 | 648 |         return NULL; | 
 | 649 |     overflow = 1234; | 
 | 650 |     value = PyLong_AsLongLongAndOverflow(num, &overflow); | 
 | 651 |     Py_DECREF(num); | 
 | 652 |     if (value == -1 && PyErr_Occurred()) | 
 | 653 |         return NULL; | 
 | 654 |     if (value != -1) | 
 | 655 |         return raiseTestError("test_long_long_and_overflow", | 
 | 656 |             "return value was not set to -1"); | 
 | 657 |     if (overflow != -1) | 
 | 658 |         return raiseTestError("test_long_long_and_overflow", | 
 | 659 |             "overflow was not set to -1"); | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 660 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 |     /* Same again, with num = PY_LLONG_MIN - 1 */ | 
 | 662 |     num = PyLong_FromLongLong(PY_LLONG_MIN); | 
 | 663 |     if (num == NULL) | 
 | 664 |         return NULL; | 
 | 665 |     one = PyLong_FromLong(1L); | 
 | 666 |     if (one == NULL) { | 
 | 667 |         Py_DECREF(num); | 
 | 668 |         return NULL; | 
 | 669 |     } | 
 | 670 |     temp = PyNumber_Subtract(num, one); | 
 | 671 |     Py_DECREF(one); | 
 | 672 |     Py_DECREF(num); | 
 | 673 |     num = temp; | 
 | 674 |     if (num == NULL) | 
 | 675 |         return NULL; | 
 | 676 |     overflow = 0; | 
 | 677 |     value = PyLong_AsLongLongAndOverflow(num, &overflow); | 
 | 678 |     Py_DECREF(num); | 
 | 679 |     if (value == -1 && PyErr_Occurred()) | 
 | 680 |         return NULL; | 
 | 681 |     if (value != -1) | 
 | 682 |         return raiseTestError("test_long_long_and_overflow", | 
 | 683 |             "return value was not set to -1"); | 
 | 684 |     if (overflow != -1) | 
 | 685 |         return raiseTestError("test_long_long_and_overflow", | 
 | 686 |             "overflow was not set to -1"); | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 687 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 |     /* Test that overflow is cleared properly for small values. */ | 
 | 689 |     num = PyLong_FromString("FF", NULL, 16); | 
 | 690 |     if (num == NULL) | 
 | 691 |         return NULL; | 
 | 692 |     overflow = 1234; | 
 | 693 |     value = PyLong_AsLongLongAndOverflow(num, &overflow); | 
 | 694 |     Py_DECREF(num); | 
 | 695 |     if (value == -1 && PyErr_Occurred()) | 
 | 696 |         return NULL; | 
 | 697 |     if (value != 0xFF) | 
 | 698 |         return raiseTestError("test_long_long_and_overflow", | 
 | 699 |             "expected return value 0xFF"); | 
 | 700 |     if (overflow != 0) | 
 | 701 |         return raiseTestError("test_long_long_and_overflow", | 
 | 702 |             "overflow was not cleared"); | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 703 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 |     num = PyLong_FromString("-FF", NULL, 16); | 
 | 705 |     if (num == NULL) | 
 | 706 |         return NULL; | 
 | 707 |     overflow = 0; | 
 | 708 |     value = PyLong_AsLongLongAndOverflow(num, &overflow); | 
 | 709 |     Py_DECREF(num); | 
 | 710 |     if (value == -1 && PyErr_Occurred()) | 
 | 711 |         return NULL; | 
 | 712 |     if (value != -0xFF) | 
 | 713 |         return raiseTestError("test_long_long_and_overflow", | 
 | 714 |             "expected return value 0xFF"); | 
 | 715 |     if (overflow != 0) | 
 | 716 |         return raiseTestError("test_long_long_and_overflow", | 
 | 717 |             "overflow was set incorrectly"); | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 718 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 |     num = PyLong_FromLongLong(PY_LLONG_MAX); | 
 | 720 |     if (num == NULL) | 
 | 721 |         return NULL; | 
 | 722 |     overflow = 1234; | 
 | 723 |     value = PyLong_AsLongLongAndOverflow(num, &overflow); | 
 | 724 |     Py_DECREF(num); | 
 | 725 |     if (value == -1 && PyErr_Occurred()) | 
 | 726 |         return NULL; | 
 | 727 |     if (value != PY_LLONG_MAX) | 
 | 728 |         return raiseTestError("test_long_long_and_overflow", | 
 | 729 |             "expected return value PY_LLONG_MAX"); | 
 | 730 |     if (overflow != 0) | 
 | 731 |         return raiseTestError("test_long_long_and_overflow", | 
 | 732 |             "overflow was not cleared"); | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 733 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 |     num = PyLong_FromLongLong(PY_LLONG_MIN); | 
 | 735 |     if (num == NULL) | 
 | 736 |         return NULL; | 
 | 737 |     overflow = 0; | 
 | 738 |     value = PyLong_AsLongLongAndOverflow(num, &overflow); | 
 | 739 |     Py_DECREF(num); | 
 | 740 |     if (value == -1 && PyErr_Occurred()) | 
 | 741 |         return NULL; | 
 | 742 |     if (value != PY_LLONG_MIN) | 
 | 743 |         return raiseTestError("test_long_long_and_overflow", | 
 | 744 |             "expected return value PY_LLONG_MIN"); | 
 | 745 |     if (overflow != 0) | 
 | 746 |         return raiseTestError("test_long_long_and_overflow", | 
 | 747 |             "overflow was not cleared"); | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 748 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 |     Py_INCREF(Py_None); | 
 | 750 |     return Py_None; | 
| Mark Dickinson | 93f562c | 2010-01-30 10:30:15 +0000 | [diff] [blame] | 751 | } | 
 | 752 |  | 
| Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 753 | /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that | 
 | 754 |    non-integer arguments are handled correctly. It should be extended to | 
 | 755 |    test overflow handling. | 
 | 756 |  */ | 
 | 757 |  | 
 | 758 | static PyObject * | 
 | 759 | test_long_as_size_t(PyObject *self) | 
 | 760 | { | 
 | 761 |     size_t out_u; | 
 | 762 |     Py_ssize_t out_s; | 
 | 763 |  | 
 | 764 |     Py_INCREF(Py_None); | 
 | 765 |  | 
 | 766 |     out_u = PyLong_AsSize_t(Py_None); | 
 | 767 |     if (out_u != (size_t)-1 || !PyErr_Occurred()) | 
 | 768 |         return raiseTestError("test_long_as_size_t", | 
 | 769 |                               "PyLong_AsSize_t(None) didn't complain"); | 
 | 770 |     if (!PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 771 |         return raiseTestError("test_long_as_size_t", | 
 | 772 |                               "PyLong_AsSize_t(None) raised " | 
 | 773 |                               "something other than TypeError"); | 
 | 774 |     PyErr_Clear(); | 
 | 775 |  | 
 | 776 |     out_s = PyLong_AsSsize_t(Py_None); | 
 | 777 |     if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred()) | 
 | 778 |         return raiseTestError("test_long_as_size_t", | 
 | 779 |                               "PyLong_AsSsize_t(None) didn't complain"); | 
 | 780 |     if (!PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 781 |         return raiseTestError("test_long_as_size_t", | 
 | 782 |                               "PyLong_AsSsize_t(None) raised " | 
 | 783 |                               "something other than TypeError"); | 
 | 784 |     PyErr_Clear(); | 
 | 785 |  | 
 | 786 |     /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ | 
 | 787 |     return Py_None; | 
 | 788 | } | 
 | 789 |  | 
 | 790 | /* Test the PyLong_AsDouble API. At present this just tests that | 
 | 791 |    non-integer arguments are handled correctly. | 
 | 792 |  */ | 
 | 793 |  | 
 | 794 | static PyObject * | 
 | 795 | test_long_as_double(PyObject *self) | 
 | 796 | { | 
 | 797 |     double out; | 
 | 798 |  | 
 | 799 |     Py_INCREF(Py_None); | 
 | 800 |  | 
 | 801 |     out = PyLong_AsDouble(Py_None); | 
 | 802 |     if (out != -1.0 || !PyErr_Occurred()) | 
 | 803 |         return raiseTestError("test_long_as_double", | 
 | 804 |                               "PyLong_AsDouble(None) didn't complain"); | 
 | 805 |     if (!PyErr_ExceptionMatches(PyExc_TypeError)) | 
 | 806 |         return raiseTestError("test_long_as_double", | 
 | 807 |                               "PyLong_AsDouble(None) raised " | 
 | 808 |                               "something other than TypeError"); | 
 | 809 |     PyErr_Clear(); | 
 | 810 |  | 
 | 811 |     /* Py_INCREF(Py_None) omitted - we already have a reference to it. */ | 
 | 812 |     return Py_None; | 
 | 813 | } | 
 | 814 |  | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 815 | /* Test the L code for PyArg_ParseTuple.  This should deliver a long long | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 816 |    for both long and int arguments.  The test may leak a little memory if | 
 | 817 |    it fails. | 
 | 818 | */ | 
 | 819 | static PyObject * | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 820 | test_L_code(PyObject *self) | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 821 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 |     PyObject *tuple, *num; | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 823 |     long long value; | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 824 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 |     tuple = PyTuple_New(1); | 
 | 826 |     if (tuple == NULL) | 
 | 827 |         return NULL; | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 828 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 |     num = PyLong_FromLong(42); | 
 | 830 |     if (num == NULL) | 
 | 831 |         return NULL; | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 832 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 833 |     PyTuple_SET_ITEM(tuple, 0, num); | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 834 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 |     value = -1; | 
 | 836 |     if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) | 
 | 837 |         return NULL; | 
 | 838 |     if (value != 42) | 
 | 839 |         return raiseTestError("test_L_code", | 
 | 840 |             "L code returned wrong value for long 42"); | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 841 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 |     Py_DECREF(num); | 
 | 843 |     num = PyLong_FromLong(42); | 
 | 844 |     if (num == NULL) | 
 | 845 |         return NULL; | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 846 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 |     PyTuple_SET_ITEM(tuple, 0, num); | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 848 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 |     value = -1; | 
 | 850 |     if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0) | 
 | 851 |         return NULL; | 
 | 852 |     if (value != 42) | 
 | 853 |         return raiseTestError("test_L_code", | 
 | 854 |             "L code returned wrong value for int 42"); | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 855 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 |     Py_DECREF(tuple); | 
 | 857 |     Py_INCREF(Py_None); | 
 | 858 |     return Py_None; | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 859 | } | 
 | 860 |  | 
| Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 861 | static PyObject * | 
| Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 862 | return_none(void *unused) | 
 | 863 | { | 
 | 864 |     Py_RETURN_NONE; | 
 | 865 | } | 
 | 866 |  | 
 | 867 | static PyObject * | 
 | 868 | raise_error(void *unused) | 
 | 869 | { | 
 | 870 |     PyErr_SetNone(PyExc_ValueError); | 
 | 871 |     return NULL; | 
 | 872 | } | 
 | 873 |  | 
 | 874 | static int | 
 | 875 | test_buildvalue_N_error(const char *fmt) | 
 | 876 | { | 
 | 877 |     PyObject *arg, *res; | 
 | 878 |  | 
 | 879 |     arg = PyList_New(0); | 
 | 880 |     if (arg == NULL) { | 
 | 881 |         return -1; | 
 | 882 |     } | 
 | 883 |  | 
 | 884 |     Py_INCREF(arg); | 
 | 885 |     res = Py_BuildValue(fmt, return_none, NULL, arg); | 
 | 886 |     if (res == NULL) { | 
 | 887 |         return -1; | 
 | 888 |     } | 
 | 889 |     Py_DECREF(res); | 
 | 890 |     if (Py_REFCNT(arg) != 1) { | 
 | 891 |         PyErr_Format(TestError, "test_buildvalue_N: " | 
 | 892 |                      "arg was not decrefed in successful " | 
 | 893 |                      "Py_BuildValue(\"%s\")", fmt); | 
 | 894 |         return -1; | 
 | 895 |     } | 
 | 896 |  | 
 | 897 |     Py_INCREF(arg); | 
 | 898 |     res = Py_BuildValue(fmt, raise_error, NULL, arg); | 
 | 899 |     if (res != NULL || !PyErr_Occurred()) { | 
 | 900 |         PyErr_Format(TestError, "test_buildvalue_N: " | 
 | 901 |                      "Py_BuildValue(\"%s\") didn't complain", fmt); | 
 | 902 |         return -1; | 
 | 903 |     } | 
 | 904 |     PyErr_Clear(); | 
 | 905 |     if (Py_REFCNT(arg) != 1) { | 
 | 906 |         PyErr_Format(TestError, "test_buildvalue_N: " | 
 | 907 |                      "arg was not decrefed in failed " | 
 | 908 |                      "Py_BuildValue(\"%s\")", fmt); | 
 | 909 |         return -1; | 
 | 910 |     } | 
 | 911 |     Py_DECREF(arg); | 
 | 912 |     return 0; | 
 | 913 | } | 
 | 914 |  | 
 | 915 | static PyObject * | 
 | 916 | test_buildvalue_N(PyObject *self, PyObject *noargs) | 
 | 917 | { | 
 | 918 |     PyObject *arg, *res; | 
 | 919 |  | 
 | 920 |     arg = PyList_New(0); | 
 | 921 |     if (arg == NULL) { | 
 | 922 |         return NULL; | 
 | 923 |     } | 
 | 924 |     Py_INCREF(arg); | 
 | 925 |     res = Py_BuildValue("N", arg); | 
 | 926 |     if (res == NULL) { | 
 | 927 |         return NULL; | 
 | 928 |     } | 
 | 929 |     if (res != arg) { | 
 | 930 |         return raiseTestError("test_buildvalue_N", | 
 | 931 |                               "Py_BuildValue(\"N\") returned wrong result"); | 
 | 932 |     } | 
 | 933 |     if (Py_REFCNT(arg) != 2) { | 
 | 934 |         return raiseTestError("test_buildvalue_N", | 
 | 935 |                               "arg was not decrefed in Py_BuildValue(\"N\")"); | 
 | 936 |     } | 
 | 937 |     Py_DECREF(res); | 
 | 938 |     Py_DECREF(arg); | 
 | 939 |  | 
 | 940 |     if (test_buildvalue_N_error("O&N") < 0) | 
 | 941 |         return NULL; | 
 | 942 |     if (test_buildvalue_N_error("(O&N)") < 0) | 
 | 943 |         return NULL; | 
 | 944 |     if (test_buildvalue_N_error("[O&N]") < 0) | 
 | 945 |         return NULL; | 
 | 946 |     if (test_buildvalue_N_error("{O&N}") < 0) | 
 | 947 |         return NULL; | 
 | 948 |     if (test_buildvalue_N_error("{()O&(())N}") < 0) | 
 | 949 |         return NULL; | 
 | 950 |  | 
 | 951 |     Py_RETURN_NONE; | 
 | 952 | } | 
 | 953 |  | 
 | 954 |  | 
 | 955 | static PyObject * | 
| Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 956 | get_args(PyObject *self, PyObject *args) | 
 | 957 | { | 
 | 958 |     if (args == NULL) { | 
 | 959 |         args = Py_None; | 
 | 960 |     } | 
 | 961 |     Py_INCREF(args); | 
 | 962 |     return args; | 
 | 963 | } | 
 | 964 |  | 
 | 965 | static PyObject * | 
 | 966 | get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs) | 
 | 967 | { | 
 | 968 |     if (kwargs == NULL) { | 
 | 969 |         kwargs = Py_None; | 
 | 970 |     } | 
 | 971 |     Py_INCREF(kwargs); | 
 | 972 |     return kwargs; | 
 | 973 | } | 
 | 974 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 975 | /* Test tuple argument processing */ | 
 | 976 | static PyObject * | 
 | 977 | getargs_tuple(PyObject *self, PyObject *args) | 
 | 978 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 |     int a, b, c; | 
 | 980 |     if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) | 
 | 981 |         return NULL; | 
 | 982 |     return Py_BuildValue("iii", a, b, c); | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 983 | } | 
 | 984 |  | 
| Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 985 | /* test PyArg_ParseTupleAndKeywords */ | 
| Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 986 | static PyObject * | 
 | 987 | getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs) | 
| Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 988 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 |     static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; | 
| Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 990 |     static const char fmt[] = "(ii)i|(i(ii))(iii)i"; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 |     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] | 992 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, | 
 | 994 |         &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4], | 
 | 995 |         &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9])) | 
 | 996 |         return NULL; | 
 | 997 |     return Py_BuildValue("iiiiiiiiii", | 
 | 998 |         int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], | 
 | 999 |         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] | 1000 | } | 
 | 1001 |  | 
| Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1002 | /* test PyArg_ParseTupleAndKeywords keyword-only arguments */ | 
 | 1003 | static PyObject * | 
 | 1004 | getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs) | 
 | 1005 | { | 
 | 1006 |     static char *keywords[] = {"required", "optional", "keyword_only", NULL}; | 
 | 1007 |     int required = -1; | 
 | 1008 |     int optional = -1; | 
 | 1009 |     int keyword_only = -1; | 
 | 1010 |  | 
 | 1011 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords, | 
 | 1012 |                                      &required, &optional, &keyword_only)) | 
 | 1013 |         return NULL; | 
 | 1014 |     return Py_BuildValue("iii", required, optional, keyword_only); | 
 | 1015 | } | 
 | 1016 |  | 
| Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1017 | /* test PyArg_ParseTupleAndKeywords positional-only arguments */ | 
 | 1018 | static PyObject * | 
 | 1019 | getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs) | 
 | 1020 | { | 
 | 1021 |     static char *keywords[] = {"", "", "keyword", NULL}; | 
 | 1022 |     int required = -1; | 
 | 1023 |     int optional = -1; | 
 | 1024 |     int keyword = -1; | 
 | 1025 |  | 
 | 1026 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords, | 
 | 1027 |                                      &required, &optional, &keyword)) | 
 | 1028 |         return NULL; | 
 | 1029 |     return Py_BuildValue("iii", required, optional, keyword); | 
 | 1030 | } | 
 | 1031 |  | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1032 | /* Functions to call PyArg_ParseTuple with integer format codes, | 
 | 1033 |    and return the result. | 
 | 1034 | */ | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1035 | static PyObject * | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1036 | getargs_b(PyObject *self, PyObject *args) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1037 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 |     unsigned char value; | 
 | 1039 |     if (!PyArg_ParseTuple(args, "b", &value)) | 
 | 1040 |         return NULL; | 
 | 1041 |     return PyLong_FromUnsignedLong((unsigned long)value); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1042 | } | 
 | 1043 |  | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1044 | static PyObject * | 
 | 1045 | getargs_B(PyObject *self, PyObject *args) | 
 | 1046 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 |     unsigned char value; | 
 | 1048 |     if (!PyArg_ParseTuple(args, "B", &value)) | 
 | 1049 |         return NULL; | 
 | 1050 |     return PyLong_FromUnsignedLong((unsigned long)value); | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1051 | } | 
 | 1052 |  | 
 | 1053 | static PyObject * | 
| Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1054 | getargs_h(PyObject *self, PyObject *args) | 
 | 1055 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 |     short value; | 
 | 1057 |     if (!PyArg_ParseTuple(args, "h", &value)) | 
 | 1058 |         return NULL; | 
 | 1059 |     return PyLong_FromLong((long)value); | 
| Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 1060 | } | 
 | 1061 |  | 
 | 1062 | static PyObject * | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1063 | getargs_H(PyObject *self, PyObject *args) | 
 | 1064 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1065 |     unsigned short value; | 
 | 1066 |     if (!PyArg_ParseTuple(args, "H", &value)) | 
 | 1067 |         return NULL; | 
 | 1068 |     return PyLong_FromUnsignedLong((unsigned long)value); | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1069 | } | 
 | 1070 |  | 
 | 1071 | static PyObject * | 
 | 1072 | getargs_I(PyObject *self, PyObject *args) | 
 | 1073 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 |     unsigned int value; | 
 | 1075 |     if (!PyArg_ParseTuple(args, "I", &value)) | 
 | 1076 |         return NULL; | 
 | 1077 |     return PyLong_FromUnsignedLong((unsigned long)value); | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1078 | } | 
 | 1079 |  | 
 | 1080 | static PyObject * | 
 | 1081 | getargs_k(PyObject *self, PyObject *args) | 
 | 1082 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 |     unsigned long value; | 
 | 1084 |     if (!PyArg_ParseTuple(args, "k", &value)) | 
 | 1085 |         return NULL; | 
 | 1086 |     return PyLong_FromUnsignedLong(value); | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1087 | } | 
 | 1088 |  | 
 | 1089 | static PyObject * | 
 | 1090 | getargs_i(PyObject *self, PyObject *args) | 
 | 1091 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 |     int value; | 
 | 1093 |     if (!PyArg_ParseTuple(args, "i", &value)) | 
 | 1094 |         return NULL; | 
 | 1095 |     return PyLong_FromLong((long)value); | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1096 | } | 
 | 1097 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1098 | static PyObject * | 
 | 1099 | getargs_l(PyObject *self, PyObject *args) | 
 | 1100 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 |     long value; | 
 | 1102 |     if (!PyArg_ParseTuple(args, "l", &value)) | 
 | 1103 |         return NULL; | 
 | 1104 |     return PyLong_FromLong(value); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1105 | } | 
 | 1106 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1107 | static PyObject * | 
 | 1108 | getargs_n(PyObject *self, PyObject *args) | 
 | 1109 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 |     Py_ssize_t value; | 
 | 1111 |     if (!PyArg_ParseTuple(args, "n", &value)) | 
 | 1112 |         return NULL; | 
 | 1113 |     return PyLong_FromSsize_t(value); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1114 | } | 
 | 1115 |  | 
| Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 1116 | static PyObject * | 
 | 1117 | getargs_p(PyObject *self, PyObject *args) | 
 | 1118 | { | 
 | 1119 |     int value; | 
 | 1120 |     if (!PyArg_ParseTuple(args, "p", &value)) | 
 | 1121 |         return NULL; | 
 | 1122 |     return PyLong_FromLong(value); | 
 | 1123 | } | 
 | 1124 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1125 | static PyObject * | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1126 | getargs_L(PyObject *self, PyObject *args) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1127 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1128 |     long long value; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 |     if (!PyArg_ParseTuple(args, "L", &value)) | 
 | 1130 |         return NULL; | 
 | 1131 |     return PyLong_FromLongLong(value); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1132 | } | 
 | 1133 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1134 | static PyObject * | 
| Thomas Heller | 3457e4b | 2003-04-24 16:14:27 +0000 | [diff] [blame] | 1135 | getargs_K(PyObject *self, PyObject *args) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1136 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1137 |     unsigned long long value; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 |     if (!PyArg_ParseTuple(args, "K", &value)) | 
 | 1139 |         return NULL; | 
 | 1140 |     return PyLong_FromUnsignedLongLong(value); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1141 | } | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1142 |  | 
 | 1143 | /* This function not only tests the 'k' getargs code, but also the | 
| Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1144 |    PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */ | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1145 | static PyObject * | 
 | 1146 | test_k_code(PyObject *self) | 
 | 1147 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 |     PyObject *tuple, *num; | 
 | 1149 |     unsigned long value; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1150 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 |     tuple = PyTuple_New(1); | 
 | 1152 |     if (tuple == NULL) | 
 | 1153 |         return NULL; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1154 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 |     /* a number larger than ULONG_MAX even on 64-bit platforms */ | 
 | 1156 |     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16); | 
 | 1157 |     if (num == NULL) | 
 | 1158 |         return NULL; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1159 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 |     value = PyLong_AsUnsignedLongMask(num); | 
 | 1161 |     if (value != ULONG_MAX) | 
 | 1162 |         return raiseTestError("test_k_code", | 
| Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 1163 |             "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1164 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 |     PyTuple_SET_ITEM(tuple, 0, num); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1166 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 |     value = 0; | 
 | 1168 |     if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) | 
 | 1169 |         return NULL; | 
 | 1170 |     if (value != ULONG_MAX) | 
 | 1171 |         return raiseTestError("test_k_code", | 
 | 1172 |             "k code returned wrong value for long 0xFFF...FFF"); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1173 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 |     Py_DECREF(num); | 
 | 1175 |     num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16); | 
 | 1176 |     if (num == NULL) | 
 | 1177 |         return NULL; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1178 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 |     value = PyLong_AsUnsignedLongMask(num); | 
 | 1180 |     if (value != (unsigned long)-0x42) | 
 | 1181 |         return raiseTestError("test_k_code", | 
| Georg Brandl | 4b5b062 | 2016-01-18 08:00:15 +0100 | [diff] [blame] | 1182 |             "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF"); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1183 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 |     PyTuple_SET_ITEM(tuple, 0, num); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1185 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 |     value = 0; | 
 | 1187 |     if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0) | 
 | 1188 |         return NULL; | 
 | 1189 |     if (value != (unsigned long)-0x42) | 
 | 1190 |         return raiseTestError("test_k_code", | 
 | 1191 |             "k code returned wrong value for long -0xFFF..000042"); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1192 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 |     Py_DECREF(tuple); | 
 | 1194 |     Py_INCREF(Py_None); | 
 | 1195 |     return Py_None; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1196 | } | 
 | 1197 |  | 
| Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1198 | static PyObject * | 
| Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 1199 | getargs_f(PyObject *self, PyObject *args) | 
 | 1200 | { | 
 | 1201 |     float f; | 
 | 1202 |     if (!PyArg_ParseTuple(args, "f", &f)) | 
 | 1203 |         return NULL; | 
 | 1204 |     return PyFloat_FromDouble(f); | 
 | 1205 | } | 
 | 1206 |  | 
 | 1207 | static PyObject * | 
 | 1208 | getargs_d(PyObject *self, PyObject *args) | 
 | 1209 | { | 
 | 1210 |     double d; | 
 | 1211 |     if (!PyArg_ParseTuple(args, "d", &d)) | 
 | 1212 |         return NULL; | 
 | 1213 |     return PyFloat_FromDouble(d); | 
 | 1214 | } | 
 | 1215 |  | 
 | 1216 | static PyObject * | 
 | 1217 | getargs_D(PyObject *self, PyObject *args) | 
 | 1218 | { | 
 | 1219 |     Py_complex cval; | 
 | 1220 |     if (!PyArg_ParseTuple(args, "D", &cval)) | 
 | 1221 |         return NULL; | 
 | 1222 |     return PyComplex_FromCComplex(cval); | 
 | 1223 | } | 
 | 1224 |  | 
 | 1225 | static PyObject * | 
 | 1226 | getargs_S(PyObject *self, PyObject *args) | 
 | 1227 | { | 
 | 1228 |     PyObject *obj; | 
 | 1229 |     if (!PyArg_ParseTuple(args, "S", &obj)) | 
 | 1230 |         return NULL; | 
 | 1231 |     Py_INCREF(obj); | 
 | 1232 |     return obj; | 
 | 1233 | } | 
 | 1234 |  | 
 | 1235 | static PyObject * | 
 | 1236 | getargs_Y(PyObject *self, PyObject *args) | 
 | 1237 | { | 
 | 1238 |     PyObject *obj; | 
 | 1239 |     if (!PyArg_ParseTuple(args, "Y", &obj)) | 
 | 1240 |         return NULL; | 
 | 1241 |     Py_INCREF(obj); | 
 | 1242 |     return obj; | 
 | 1243 | } | 
 | 1244 |  | 
 | 1245 | static PyObject * | 
 | 1246 | getargs_U(PyObject *self, PyObject *args) | 
 | 1247 | { | 
 | 1248 |     PyObject *obj; | 
 | 1249 |     if (!PyArg_ParseTuple(args, "U", &obj)) | 
 | 1250 |         return NULL; | 
 | 1251 |     Py_INCREF(obj); | 
 | 1252 |     return obj; | 
 | 1253 | } | 
 | 1254 |  | 
 | 1255 | static PyObject * | 
| Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1256 | getargs_c(PyObject *self, PyObject *args) | 
 | 1257 | { | 
 | 1258 |     char c; | 
 | 1259 |     if (!PyArg_ParseTuple(args, "c", &c)) | 
 | 1260 |         return NULL; | 
| Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1261 |     return PyLong_FromLong((unsigned char)c); | 
 | 1262 | } | 
 | 1263 |  | 
 | 1264 | static PyObject * | 
 | 1265 | getargs_C(PyObject *self, PyObject *args) | 
 | 1266 | { | 
 | 1267 |     int c; | 
 | 1268 |     if (!PyArg_ParseTuple(args, "C", &c)) | 
 | 1269 |         return NULL; | 
 | 1270 |     return PyLong_FromLong(c); | 
| Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 1271 | } | 
 | 1272 |  | 
 | 1273 | static PyObject * | 
| Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1274 | getargs_s(PyObject *self, PyObject *args) | 
 | 1275 | { | 
 | 1276 |     char *str; | 
 | 1277 |     if (!PyArg_ParseTuple(args, "s", &str)) | 
 | 1278 |         return NULL; | 
 | 1279 |     return PyBytes_FromString(str); | 
 | 1280 | } | 
 | 1281 |  | 
 | 1282 | static PyObject * | 
 | 1283 | getargs_s_star(PyObject *self, PyObject *args) | 
 | 1284 | { | 
 | 1285 |     Py_buffer buffer; | 
 | 1286 |     PyObject *bytes; | 
 | 1287 |     if (!PyArg_ParseTuple(args, "s*", &buffer)) | 
 | 1288 |         return NULL; | 
 | 1289 |     bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); | 
 | 1290 |     PyBuffer_Release(&buffer); | 
 | 1291 |     return bytes; | 
 | 1292 | } | 
 | 1293 |  | 
 | 1294 | static PyObject * | 
 | 1295 | getargs_s_hash(PyObject *self, PyObject *args) | 
 | 1296 | { | 
 | 1297 |     char *str; | 
 | 1298 |     Py_ssize_t size; | 
 | 1299 |     if (!PyArg_ParseTuple(args, "s#", &str, &size)) | 
 | 1300 |         return NULL; | 
 | 1301 |     return PyBytes_FromStringAndSize(str, size); | 
 | 1302 | } | 
 | 1303 |  | 
 | 1304 | static PyObject * | 
 | 1305 | getargs_z(PyObject *self, PyObject *args) | 
 | 1306 | { | 
 | 1307 |     char *str; | 
 | 1308 |     if (!PyArg_ParseTuple(args, "z", &str)) | 
 | 1309 |         return NULL; | 
 | 1310 |     if (str != NULL) | 
 | 1311 |         return PyBytes_FromString(str); | 
 | 1312 |     else | 
 | 1313 |         Py_RETURN_NONE; | 
 | 1314 | } | 
 | 1315 |  | 
 | 1316 | static PyObject * | 
 | 1317 | getargs_z_star(PyObject *self, PyObject *args) | 
 | 1318 | { | 
 | 1319 |     Py_buffer buffer; | 
 | 1320 |     PyObject *bytes; | 
 | 1321 |     if (!PyArg_ParseTuple(args, "z*", &buffer)) | 
 | 1322 |         return NULL; | 
 | 1323 |     if (buffer.buf != NULL) | 
 | 1324 |         bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); | 
 | 1325 |     else { | 
 | 1326 |         Py_INCREF(Py_None); | 
 | 1327 |         bytes = Py_None; | 
 | 1328 |     } | 
 | 1329 |     PyBuffer_Release(&buffer); | 
 | 1330 |     return bytes; | 
 | 1331 | } | 
 | 1332 |  | 
 | 1333 | static PyObject * | 
 | 1334 | getargs_z_hash(PyObject *self, PyObject *args) | 
 | 1335 | { | 
 | 1336 |     char *str; | 
 | 1337 |     Py_ssize_t size; | 
 | 1338 |     if (!PyArg_ParseTuple(args, "z#", &str, &size)) | 
 | 1339 |         return NULL; | 
 | 1340 |     if (str != NULL) | 
 | 1341 |         return PyBytes_FromStringAndSize(str, size); | 
 | 1342 |     else | 
 | 1343 |         Py_RETURN_NONE; | 
 | 1344 | } | 
 | 1345 |  | 
 | 1346 | static PyObject * | 
 | 1347 | getargs_y(PyObject *self, PyObject *args) | 
 | 1348 | { | 
 | 1349 |     char *str; | 
 | 1350 |     if (!PyArg_ParseTuple(args, "y", &str)) | 
 | 1351 |         return NULL; | 
 | 1352 |     return PyBytes_FromString(str); | 
 | 1353 | } | 
 | 1354 |  | 
 | 1355 | static PyObject * | 
 | 1356 | getargs_y_star(PyObject *self, PyObject *args) | 
 | 1357 | { | 
 | 1358 |     Py_buffer buffer; | 
 | 1359 |     PyObject *bytes; | 
 | 1360 |     if (!PyArg_ParseTuple(args, "y*", &buffer)) | 
 | 1361 |         return NULL; | 
 | 1362 |     bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); | 
 | 1363 |     PyBuffer_Release(&buffer); | 
 | 1364 |     return bytes; | 
 | 1365 | } | 
 | 1366 |  | 
 | 1367 | static PyObject * | 
 | 1368 | getargs_y_hash(PyObject *self, PyObject *args) | 
 | 1369 | { | 
 | 1370 |     char *str; | 
 | 1371 |     Py_ssize_t size; | 
 | 1372 |     if (!PyArg_ParseTuple(args, "y#", &str, &size)) | 
 | 1373 |         return NULL; | 
 | 1374 |     return PyBytes_FromStringAndSize(str, size); | 
 | 1375 | } | 
 | 1376 |  | 
 | 1377 | static PyObject * | 
 | 1378 | getargs_u(PyObject *self, PyObject *args) | 
 | 1379 | { | 
 | 1380 |     Py_UNICODE *str; | 
 | 1381 |     Py_ssize_t size; | 
 | 1382 |     if (!PyArg_ParseTuple(args, "u", &str)) | 
 | 1383 |         return NULL; | 
 | 1384 |     size = Py_UNICODE_strlen(str); | 
 | 1385 |     return PyUnicode_FromUnicode(str, size); | 
 | 1386 | } | 
 | 1387 |  | 
 | 1388 | static PyObject * | 
 | 1389 | getargs_u_hash(PyObject *self, PyObject *args) | 
 | 1390 | { | 
 | 1391 |     Py_UNICODE *str; | 
 | 1392 |     Py_ssize_t size; | 
 | 1393 |     if (!PyArg_ParseTuple(args, "u#", &str, &size)) | 
 | 1394 |         return NULL; | 
 | 1395 |     return PyUnicode_FromUnicode(str, size); | 
 | 1396 | } | 
 | 1397 |  | 
 | 1398 | static PyObject * | 
 | 1399 | getargs_Z(PyObject *self, PyObject *args) | 
 | 1400 | { | 
 | 1401 |     Py_UNICODE *str; | 
 | 1402 |     Py_ssize_t size; | 
 | 1403 |     if (!PyArg_ParseTuple(args, "Z", &str)) | 
 | 1404 |         return NULL; | 
 | 1405 |     if (str != NULL) { | 
 | 1406 |         size = Py_UNICODE_strlen(str); | 
 | 1407 |         return PyUnicode_FromUnicode(str, size); | 
 | 1408 |     } else | 
 | 1409 |         Py_RETURN_NONE; | 
 | 1410 | } | 
 | 1411 |  | 
 | 1412 | static PyObject * | 
 | 1413 | getargs_Z_hash(PyObject *self, PyObject *args) | 
 | 1414 | { | 
 | 1415 |     Py_UNICODE *str; | 
 | 1416 |     Py_ssize_t size; | 
 | 1417 |     if (!PyArg_ParseTuple(args, "Z#", &str, &size)) | 
 | 1418 |         return NULL; | 
 | 1419 |     if (str != NULL) | 
 | 1420 |         return PyUnicode_FromUnicode(str, size); | 
 | 1421 |     else | 
 | 1422 |         Py_RETURN_NONE; | 
 | 1423 | } | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1424 |  | 
| Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 1425 | static PyObject * | 
 | 1426 | getargs_es(PyObject *self, PyObject *args) | 
 | 1427 | { | 
 | 1428 |     PyObject *arg, *result; | 
 | 1429 |     const char *encoding = NULL; | 
 | 1430 |     char *str; | 
 | 1431 |  | 
 | 1432 |     if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) | 
 | 1433 |         return NULL; | 
 | 1434 |     if (!PyArg_Parse(arg, "es", encoding, &str)) | 
 | 1435 |         return NULL; | 
 | 1436 |     result = PyBytes_FromString(str); | 
 | 1437 |     PyMem_Free(str); | 
 | 1438 |     return result; | 
 | 1439 | } | 
 | 1440 |  | 
 | 1441 | static PyObject * | 
 | 1442 | getargs_et(PyObject *self, PyObject *args) | 
 | 1443 | { | 
 | 1444 |     PyObject *arg, *result; | 
 | 1445 |     const char *encoding = NULL; | 
 | 1446 |     char *str; | 
 | 1447 |  | 
 | 1448 |     if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) | 
 | 1449 |         return NULL; | 
 | 1450 |     if (!PyArg_Parse(arg, "et", encoding, &str)) | 
 | 1451 |         return NULL; | 
 | 1452 |     result = PyBytes_FromString(str); | 
 | 1453 |     PyMem_Free(str); | 
 | 1454 |     return result; | 
 | 1455 | } | 
 | 1456 |  | 
 | 1457 | static PyObject * | 
 | 1458 | getargs_es_hash(PyObject *self, PyObject *args) | 
 | 1459 | { | 
 | 1460 |     PyObject *arg, *result; | 
 | 1461 |     const char *encoding = NULL; | 
 | 1462 |     PyByteArrayObject *buffer = NULL; | 
 | 1463 |     char *str = NULL; | 
 | 1464 |     Py_ssize_t size; | 
 | 1465 |  | 
 | 1466 |     if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) | 
 | 1467 |         return NULL; | 
 | 1468 |     if (buffer != NULL) { | 
 | 1469 |         str = PyByteArray_AS_STRING(buffer); | 
 | 1470 |         size = PyByteArray_GET_SIZE(buffer); | 
 | 1471 |     } | 
 | 1472 |     if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) | 
 | 1473 |         return NULL; | 
 | 1474 |     result = PyBytes_FromStringAndSize(str, size); | 
 | 1475 |     if (buffer == NULL) | 
 | 1476 |         PyMem_Free(str); | 
 | 1477 |     return result; | 
 | 1478 | } | 
 | 1479 |  | 
 | 1480 | static PyObject * | 
 | 1481 | getargs_et_hash(PyObject *self, PyObject *args) | 
 | 1482 | { | 
 | 1483 |     PyObject *arg, *result; | 
 | 1484 |     const char *encoding = NULL; | 
 | 1485 |     PyByteArrayObject *buffer = NULL; | 
 | 1486 |     char *str = NULL; | 
 | 1487 |     Py_ssize_t size; | 
 | 1488 |  | 
 | 1489 |     if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) | 
 | 1490 |         return NULL; | 
 | 1491 |     if (buffer != NULL) { | 
 | 1492 |         str = PyByteArray_AS_STRING(buffer); | 
 | 1493 |         size = PyByteArray_GET_SIZE(buffer); | 
 | 1494 |     } | 
 | 1495 |     if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) | 
 | 1496 |         return NULL; | 
 | 1497 |     result = PyBytes_FromStringAndSize(str, size); | 
 | 1498 |     if (buffer == NULL) | 
 | 1499 |         PyMem_Free(str); | 
 | 1500 |     return result; | 
 | 1501 | } | 
 | 1502 |  | 
| Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1503 | /* Test the s and z codes for PyArg_ParseTuple. | 
 | 1504 | */ | 
 | 1505 | static PyObject * | 
 | 1506 | test_s_code(PyObject *self) | 
 | 1507 | { | 
 | 1508 |     /* Unicode strings should be accepted */ | 
 | 1509 |     PyObject *tuple, *obj; | 
 | 1510 |     char *value; | 
 | 1511 |  | 
 | 1512 |     tuple = PyTuple_New(1); | 
 | 1513 |     if (tuple == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 |     return NULL; | 
| Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1515 |  | 
 | 1516 |     obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"), | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 |                            "latin-1", NULL); | 
| Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1518 |     if (obj == NULL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 |     return NULL; | 
| Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1520 |  | 
 | 1521 |     PyTuple_SET_ITEM(tuple, 0, obj); | 
 | 1522 |  | 
 | 1523 |     /* These two blocks used to raise a TypeError: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 |      * "argument must be string without null bytes, not str" | 
| Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1525 |      */ | 
 | 1526 |     if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 |     return NULL; | 
| Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1528 |  | 
 | 1529 |     if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 |     return NULL; | 
| Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1531 |  | 
| Alexandre Vassalotti | b645bc7 | 2008-05-15 22:06:59 +0000 | [diff] [blame] | 1532 |     Py_DECREF(tuple); | 
| Amaury Forgeot d'Arc | 0740459 | 2008-05-12 13:19:07 +0000 | [diff] [blame] | 1533 |     Py_RETURN_NONE; | 
 | 1534 | } | 
 | 1535 |  | 
| Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1536 | static PyObject * | 
| Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1537 | parse_tuple_and_keywords(PyObject *self, PyObject *args) | 
| Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1538 | { | 
| Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1539 |     PyObject *sub_args; | 
 | 1540 |     PyObject *sub_kwargs; | 
 | 1541 |     char *sub_format; | 
 | 1542 |     PyObject *sub_keywords; | 
| Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1543 |  | 
| Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1544 |     Py_ssize_t i, size; | 
 | 1545 |     char *keywords[8 + 1]; /* space for NULL at end */ | 
 | 1546 |     PyObject *o; | 
 | 1547 |     PyObject *converted[8]; | 
| Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1548 |  | 
| Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1549 |     int result; | 
 | 1550 |     PyObject *return_value = NULL; | 
| Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1551 |  | 
| Larry Hastings | 22701e8 | 2012-08-08 14:52:22 -0700 | [diff] [blame] | 1552 |     double buffers[8][4]; /* double ensures alignment where necessary */ | 
| Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1553 |  | 
| Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1554 |     if (!PyArg_ParseTuple(args, "OOyO:parse_tuple_and_keywords", | 
 | 1555 |         &sub_args, &sub_kwargs, | 
 | 1556 |         &sub_format, &sub_keywords)) | 
 | 1557 |         return NULL; | 
| Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1558 |  | 
| Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1559 |     if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) { | 
 | 1560 |         PyErr_SetString(PyExc_ValueError, | 
 | 1561 |             "parse_tuple_and_keywords: sub_keywords must be either list or tuple"); | 
 | 1562 |         return NULL; | 
 | 1563 |     } | 
 | 1564 |  | 
 | 1565 |     memset(buffers, 0, sizeof(buffers)); | 
 | 1566 |     memset(converted, 0, sizeof(converted)); | 
 | 1567 |     memset(keywords, 0, sizeof(keywords)); | 
 | 1568 |  | 
 | 1569 |     size = PySequence_Fast_GET_SIZE(sub_keywords); | 
 | 1570 |     if (size > 8) { | 
 | 1571 |         PyErr_SetString(PyExc_ValueError, | 
 | 1572 |             "parse_tuple_and_keywords: too many keywords in sub_keywords"); | 
 | 1573 |         goto exit; | 
 | 1574 |     } | 
 | 1575 |  | 
 | 1576 |     for (i = 0; i < size; i++) { | 
 | 1577 |         o = PySequence_Fast_GET_ITEM(sub_keywords, i); | 
 | 1578 |         if (!PyUnicode_FSConverter(o, (void *)(converted + i))) { | 
 | 1579 |             PyErr_Format(PyExc_ValueError, | 
| Jesus Cea | 6e1d2b6 | 2012-10-04 16:06:30 +0200 | [diff] [blame] | 1580 |                 "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i); | 
| Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 1581 |             goto exit; | 
 | 1582 |         } | 
 | 1583 |         keywords[i] = PyBytes_AS_STRING(converted[i]); | 
 | 1584 |     } | 
 | 1585 |  | 
 | 1586 |     result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs, | 
 | 1587 |         sub_format, keywords, | 
 | 1588 |         buffers + 0, buffers + 1, buffers + 2, buffers + 3, | 
 | 1589 |         buffers + 4, buffers + 5, buffers + 6, buffers + 7); | 
 | 1590 |  | 
 | 1591 |     if (result) { | 
 | 1592 |         return_value = Py_None; | 
 | 1593 |         Py_INCREF(Py_None); | 
 | 1594 |     } | 
 | 1595 |  | 
 | 1596 | exit: | 
 | 1597 |     size = sizeof(converted) / sizeof(converted[0]); | 
 | 1598 |     for (i = 0; i < size; i++) { | 
 | 1599 |         Py_XDECREF(converted[i]); | 
 | 1600 |     } | 
 | 1601 |     return return_value; | 
| Mark Dickinson | f08173b | 2009-12-03 10:59:46 +0000 | [diff] [blame] | 1602 | } | 
 | 1603 |  | 
| Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1604 | static volatile int x; | 
 | 1605 |  | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1606 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case | 
 | 1607 |    of an error. | 
 | 1608 | */ | 
 | 1609 | static PyObject * | 
| Fred Drake | acee69f | 2002-04-01 14:28:58 +0000 | [diff] [blame] | 1610 | test_u_code(PyObject *self) | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1611 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 |     PyObject *tuple, *obj; | 
 | 1613 |     Py_UNICODE *value; | 
 | 1614 |     Py_ssize_t len; | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1615 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 |     /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */ | 
 | 1617 |     /* Just use the macro and check that it compiles */ | 
 | 1618 |     x = Py_UNICODE_ISSPACE(25); | 
| Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1619 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 |     tuple = PyTuple_New(1); | 
 | 1621 |     if (tuple == NULL) | 
 | 1622 |         return NULL; | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1623 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 |     obj = PyUnicode_Decode("test", strlen("test"), | 
 | 1625 |                            "ascii", NULL); | 
 | 1626 |     if (obj == NULL) | 
 | 1627 |         return NULL; | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1628 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 |     PyTuple_SET_ITEM(tuple, 0, obj); | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1630 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 |     value = 0; | 
 | 1632 |     if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0) | 
 | 1633 |         return NULL; | 
 | 1634 |     if (value != PyUnicode_AS_UNICODE(obj)) | 
 | 1635 |         return raiseTestError("test_u_code", | 
 | 1636 |             "u code returned wrong value for u'test'"); | 
 | 1637 |     value = 0; | 
 | 1638 |     if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0) | 
 | 1639 |         return NULL; | 
 | 1640 |     if (value != PyUnicode_AS_UNICODE(obj) || | 
 | 1641 |         len != PyUnicode_GET_SIZE(obj)) | 
 | 1642 |         return raiseTestError("test_u_code", | 
 | 1643 |             "u# code returned wrong values for u'test'"); | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1644 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 |     Py_DECREF(tuple); | 
 | 1646 |     Py_INCREF(Py_None); | 
 | 1647 |     return Py_None; | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1648 | } | 
 | 1649 |  | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1650 | /* Test Z and Z# codes for PyArg_ParseTuple */ | 
 | 1651 | static PyObject * | 
 | 1652 | test_Z_code(PyObject *self) | 
 | 1653 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 |     PyObject *tuple, *obj; | 
| Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1655 |     const Py_UNICODE *value1, *value2; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 |     Py_ssize_t len1, len2; | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1657 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 |     tuple = PyTuple_New(2); | 
 | 1659 |     if (tuple == NULL) | 
 | 1660 |         return NULL; | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1661 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 |     obj = PyUnicode_FromString("test"); | 
 | 1663 |     PyTuple_SET_ITEM(tuple, 0, obj); | 
 | 1664 |     Py_INCREF(Py_None); | 
 | 1665 |     PyTuple_SET_ITEM(tuple, 1, Py_None); | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1666 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 |     /* swap values on purpose */ | 
 | 1668 |     value1 = NULL; | 
 | 1669 |     value2 = PyUnicode_AS_UNICODE(obj); | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1670 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 |     /* Test Z for both values */ | 
 | 1672 |     if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0) | 
 | 1673 |         return NULL; | 
 | 1674 |     if (value1 != PyUnicode_AS_UNICODE(obj)) | 
 | 1675 |         return raiseTestError("test_Z_code", | 
 | 1676 |             "Z code returned wrong value for 'test'"); | 
 | 1677 |     if (value2 != NULL) | 
 | 1678 |         return raiseTestError("test_Z_code", | 
 | 1679 |             "Z code returned wrong value for None"); | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1680 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1681 |     value1 = NULL; | 
 | 1682 |     value2 = PyUnicode_AS_UNICODE(obj); | 
 | 1683 |     len1 = -1; | 
 | 1684 |     len2 = -1; | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1685 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 |     /* Test Z# for both values */ | 
 | 1687 |     if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1, | 
 | 1688 |                          &value2, &len2) < 0) | 
 | 1689 |         return NULL; | 
 | 1690 |     if (value1 != PyUnicode_AS_UNICODE(obj) || | 
 | 1691 |         len1 != PyUnicode_GET_SIZE(obj)) | 
 | 1692 |         return raiseTestError("test_Z_code", | 
 | 1693 |             "Z# code returned wrong values for 'test'"); | 
 | 1694 |     if (value2 != NULL || | 
 | 1695 |         len2 != 0) | 
 | 1696 |         return raiseTestError("test_Z_code", | 
 | 1697 |             "Z# code returned wrong values for None'"); | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1698 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 |     Py_DECREF(tuple); | 
 | 1700 |     Py_RETURN_NONE; | 
| Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 1701 | } | 
 | 1702 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1703 | static PyObject * | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1704 | test_widechar(PyObject *self) | 
 | 1705 | { | 
 | 1706 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 |     const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; | 
 | 1708 |     size_t wtextlen = 1; | 
| Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1709 |     const wchar_t invalid[1] = {(wchar_t)0x110000u}; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1710 | #else | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1711 |     const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; | 
 | 1712 |     size_t wtextlen = 2; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1713 | #endif | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 |     PyObject *wide, *utf8; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1715 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1716 |     wide = PyUnicode_FromWideChar(wtext, wtextlen); | 
 | 1717 |     if (wide == NULL) | 
 | 1718 |         return NULL; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1719 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 |     utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d"); | 
 | 1721 |     if (utf8 == NULL) { | 
 | 1722 |         Py_DECREF(wide); | 
 | 1723 |         return NULL; | 
 | 1724 |     } | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1725 |  | 
| Victor Stinner | 8ef1887 | 2011-11-21 02:06:57 +0100 | [diff] [blame] | 1726 |     if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 |         Py_DECREF(wide); | 
 | 1728 |         Py_DECREF(utf8); | 
 | 1729 |         return raiseTestError("test_widechar", | 
 | 1730 |                               "wide string and utf8 string " | 
 | 1731 |                               "have different length"); | 
 | 1732 |     } | 
 | 1733 |     if (PyUnicode_Compare(wide, utf8)) { | 
 | 1734 |         Py_DECREF(wide); | 
 | 1735 |         Py_DECREF(utf8); | 
 | 1736 |         if (PyErr_Occurred()) | 
 | 1737 |             return NULL; | 
 | 1738 |         return raiseTestError("test_widechar", | 
 | 1739 |                               "wide string and utf8 string " | 
 | 1740 |                               "are different"); | 
 | 1741 |     } | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1742 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 |     Py_DECREF(wide); | 
 | 1744 |     Py_DECREF(utf8); | 
| Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1745 |  | 
 | 1746 | #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) | 
 | 1747 |     wide = PyUnicode_FromWideChar(invalid, 1); | 
 | 1748 |     if (wide == NULL) | 
 | 1749 |         PyErr_Clear(); | 
 | 1750 |     else | 
 | 1751 |         return raiseTestError("test_widechar", | 
 | 1752 |                               "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail"); | 
 | 1753 |  | 
 | 1754 |     wide = PyUnicode_FromUnicode(invalid, 1); | 
 | 1755 |     if (wide == NULL) | 
 | 1756 |         PyErr_Clear(); | 
 | 1757 |     else | 
 | 1758 |         return raiseTestError("test_widechar", | 
 | 1759 |                               "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail"); | 
| Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1760 |  | 
 | 1761 |     wide = PyUnicode_FromUnicode(NULL, 1); | 
 | 1762 |     if (wide == NULL) | 
 | 1763 |         return NULL; | 
 | 1764 |     PyUnicode_AS_UNICODE(wide)[0] = invalid[0]; | 
| Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1765 |     if (_PyUnicode_Ready(wide) < 0) { | 
 | 1766 |         Py_DECREF(wide); | 
| Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1767 |         PyErr_Clear(); | 
| Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1768 |     } | 
 | 1769 |     else { | 
 | 1770 |         Py_DECREF(wide); | 
| Victor Stinner | e5c0533 | 2013-03-06 00:39:03 +0100 | [diff] [blame] | 1771 |         return raiseTestError("test_widechar", | 
 | 1772 |                               "PyUnicode_Ready() didn't fail"); | 
| Ezio Melotti | 03e667d | 2013-03-07 21:18:45 +0200 | [diff] [blame] | 1773 |     } | 
| Victor Stinner | e3b4715 | 2011-12-09 20:49:49 +0100 | [diff] [blame] | 1774 | #endif | 
 | 1775 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 |     Py_RETURN_NONE; | 
| Mark Dickinson | 081dfee | 2009-03-18 14:47:41 +0000 | [diff] [blame] | 1777 | } | 
 | 1778 |  | 
 | 1779 | static PyObject * | 
| Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1780 | unicode_aswidechar(PyObject *self, PyObject *args) | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1781 | { | 
 | 1782 |     PyObject *unicode, *result; | 
 | 1783 |     Py_ssize_t buflen, size; | 
 | 1784 |     wchar_t *buffer; | 
 | 1785 |  | 
 | 1786 |     if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen)) | 
 | 1787 |         return NULL; | 
| Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 1788 |     buffer = PyMem_New(wchar_t, buflen); | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1789 |     if (buffer == NULL) | 
 | 1790 |         return PyErr_NoMemory(); | 
 | 1791 |  | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1792 |     size = PyUnicode_AsWideChar(unicode, buffer, buflen); | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1793 |     if (size == -1) { | 
 | 1794 |         PyMem_Free(buffer); | 
 | 1795 |         return NULL; | 
 | 1796 |     } | 
 | 1797 |  | 
 | 1798 |     if (size < buflen) | 
 | 1799 |         buflen = size + 1; | 
 | 1800 |     else | 
 | 1801 |         buflen = size; | 
 | 1802 |     result = PyUnicode_FromWideChar(buffer, buflen); | 
 | 1803 |     PyMem_Free(buffer); | 
 | 1804 |     if (result == NULL) | 
 | 1805 |         return NULL; | 
 | 1806 |  | 
 | 1807 |     return Py_BuildValue("(Nn)", result, size); | 
 | 1808 | } | 
 | 1809 |  | 
 | 1810 | static PyObject * | 
| Victor Stinner | 46c7b3b | 2010-10-02 11:49:31 +0000 | [diff] [blame] | 1811 | unicode_aswidecharstring(PyObject *self, PyObject *args) | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1812 | { | 
 | 1813 |     PyObject *unicode, *result; | 
 | 1814 |     Py_ssize_t size; | 
 | 1815 |     wchar_t *buffer; | 
 | 1816 |  | 
 | 1817 |     if (!PyArg_ParseTuple(args, "U", &unicode)) | 
 | 1818 |         return NULL; | 
 | 1819 |  | 
| Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 1820 |     buffer = PyUnicode_AsWideCharString(unicode, &size); | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 1821 |     if (buffer == NULL) | 
 | 1822 |         return NULL; | 
 | 1823 |  | 
 | 1824 |     result = PyUnicode_FromWideChar(buffer, size + 1); | 
 | 1825 |     PyMem_Free(buffer); | 
 | 1826 |     if (result == NULL) | 
 | 1827 |         return NULL; | 
 | 1828 |     return Py_BuildValue("(Nn)", result, size); | 
 | 1829 | } | 
 | 1830 |  | 
 | 1831 | static PyObject * | 
| Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 1832 | unicode_encodedecimal(PyObject *self, PyObject *args) | 
 | 1833 | { | 
 | 1834 |     Py_UNICODE *unicode; | 
 | 1835 |     Py_ssize_t length; | 
 | 1836 |     char *errors = NULL; | 
 | 1837 |     PyObject *decimal; | 
 | 1838 |     Py_ssize_t decimal_length, new_length; | 
 | 1839 |     int res; | 
 | 1840 |  | 
 | 1841 |     if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors)) | 
 | 1842 |         return NULL; | 
 | 1843 |  | 
 | 1844 |     decimal_length = length * 7; /* len('€') */ | 
 | 1845 |     decimal = PyBytes_FromStringAndSize(NULL, decimal_length); | 
 | 1846 |     if (decimal == NULL) | 
 | 1847 |         return NULL; | 
 | 1848 |  | 
 | 1849 |     res = PyUnicode_EncodeDecimal(unicode, length, | 
 | 1850 |                                   PyBytes_AS_STRING(decimal), | 
 | 1851 |                                   errors); | 
 | 1852 |     if (res < 0) { | 
 | 1853 |         Py_DECREF(decimal); | 
 | 1854 |         return NULL; | 
 | 1855 |     } | 
 | 1856 |  | 
 | 1857 |     new_length = strlen(PyBytes_AS_STRING(decimal)); | 
 | 1858 |     assert(new_length <= decimal_length); | 
 | 1859 |     res = _PyBytes_Resize(&decimal, new_length); | 
 | 1860 |     if (res < 0) | 
 | 1861 |         return NULL; | 
 | 1862 |  | 
 | 1863 |     return decimal; | 
 | 1864 | } | 
 | 1865 |  | 
 | 1866 | static PyObject * | 
 | 1867 | unicode_transformdecimaltoascii(PyObject *self, PyObject *args) | 
 | 1868 | { | 
 | 1869 |     Py_UNICODE *unicode; | 
 | 1870 |     Py_ssize_t length; | 
 | 1871 |     if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length)) | 
 | 1872 |         return NULL; | 
 | 1873 |     return PyUnicode_TransformDecimalToASCII(unicode, length); | 
 | 1874 | } | 
 | 1875 |  | 
 | 1876 | static PyObject * | 
| Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 1877 | unicode_legacy_string(PyObject *self, PyObject *args) | 
 | 1878 | { | 
 | 1879 |     Py_UNICODE *data; | 
 | 1880 |     Py_ssize_t len; | 
 | 1881 |     PyObject *u; | 
 | 1882 |  | 
 | 1883 |     if (!PyArg_ParseTuple(args, "u#", &data, &len)) | 
 | 1884 |         return NULL; | 
 | 1885 |  | 
 | 1886 |     u = PyUnicode_FromUnicode(NULL, len); | 
 | 1887 |     if (u == NULL) | 
 | 1888 |         return NULL; | 
 | 1889 |  | 
 | 1890 |     memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE)); | 
 | 1891 |  | 
 | 1892 |     if (len > 0) { /* The empty string is always ready. */ | 
 | 1893 |         assert(!PyUnicode_IS_READY(u)); | 
 | 1894 |     } | 
 | 1895 |  | 
 | 1896 |     return u; | 
 | 1897 | } | 
 | 1898 |  | 
 | 1899 | static PyObject * | 
| Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1900 | getargs_w_star(PyObject *self, PyObject *args) | 
 | 1901 | { | 
 | 1902 |     Py_buffer buffer; | 
 | 1903 |     PyObject *result; | 
 | 1904 |     char *str; | 
 | 1905 |  | 
 | 1906 |     if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) | 
 | 1907 |         return NULL; | 
 | 1908 |  | 
 | 1909 |     if (2 <= buffer.len) { | 
 | 1910 |         str = buffer.buf; | 
 | 1911 |         str[0] = '['; | 
 | 1912 |         str[buffer.len-1] = ']'; | 
 | 1913 |     } | 
 | 1914 |  | 
 | 1915 |     result = PyBytes_FromStringAndSize(buffer.buf, buffer.len); | 
 | 1916 |     PyBuffer_Release(&buffer); | 
 | 1917 |     return result; | 
 | 1918 | } | 
 | 1919 |  | 
 | 1920 |  | 
 | 1921 | static PyObject * | 
| Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1922 | test_empty_argparse(PyObject *self) | 
 | 1923 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 |     /* Test that formats can begin with '|'. See issue #4720. */ | 
 | 1925 |     PyObject *tuple, *dict = NULL; | 
 | 1926 |     static char *kwlist[] = {NULL}; | 
 | 1927 |     int result; | 
 | 1928 |     tuple = PyTuple_New(0); | 
 | 1929 |     if (!tuple) | 
 | 1930 |         return NULL; | 
 | 1931 |     if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0) | 
 | 1932 |         goto done; | 
 | 1933 |     dict = PyDict_New(); | 
 | 1934 |     if (!dict) | 
 | 1935 |         goto done; | 
 | 1936 |     result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist); | 
| Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1937 |   done: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 |     Py_DECREF(tuple); | 
 | 1939 |     Py_XDECREF(dict); | 
 | 1940 |     if (result < 0) | 
 | 1941 |         return NULL; | 
 | 1942 |     else { | 
 | 1943 |         Py_RETURN_NONE; | 
 | 1944 |     } | 
| Benjamin Peterson | 9203501 | 2008-12-27 16:00:54 +0000 | [diff] [blame] | 1945 | } | 
 | 1946 |  | 
 | 1947 | static PyObject * | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1948 | codec_incrementalencoder(PyObject *self, PyObject *args) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1949 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 |     const char *encoding, *errors = NULL; | 
 | 1951 |     if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder", | 
 | 1952 |                           &encoding, &errors)) | 
 | 1953 |         return NULL; | 
 | 1954 |     return PyCodec_IncrementalEncoder(encoding, errors); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1955 | } | 
 | 1956 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1957 | static PyObject * | 
 | 1958 | codec_incrementaldecoder(PyObject *self, PyObject *args) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1959 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 |     const char *encoding, *errors = NULL; | 
 | 1961 |     if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder", | 
 | 1962 |                           &encoding, &errors)) | 
 | 1963 |         return NULL; | 
 | 1964 |     return PyCodec_IncrementalDecoder(encoding, errors); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1965 | } | 
 | 1966 |  | 
| Marc-André Lemburg | 3e3eacb | 2002-01-09 16:21:27 +0000 | [diff] [blame] | 1967 |  | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 1968 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */ | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1969 | static PyObject * | 
 | 1970 | test_long_numbits(PyObject *self) | 
 | 1971 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1972 |     struct triple { | 
 | 1973 |         long input; | 
 | 1974 |         size_t nbits; | 
 | 1975 |         int sign; | 
 | 1976 |     } testcases[] = {{0, 0, 0}, | 
 | 1977 |                      {1L, 1, 1}, | 
 | 1978 |                      {-1L, 1, -1}, | 
 | 1979 |                      {2L, 2, 1}, | 
 | 1980 |                      {-2L, 2, -1}, | 
 | 1981 |                      {3L, 2, 1}, | 
 | 1982 |                      {-3L, 2, -1}, | 
 | 1983 |                      {4L, 3, 1}, | 
 | 1984 |                      {-4L, 3, -1}, | 
| Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1985 |                      {0x7fffL, 15, 1},          /* one Python int digit */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 |              {-0x7fffL, 15, -1}, | 
 | 1987 |              {0xffffL, 16, 1}, | 
 | 1988 |              {-0xffffL, 16, -1}, | 
 | 1989 |              {0xfffffffL, 28, 1}, | 
 | 1990 |              {-0xfffffffL, 28, -1}}; | 
| Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1991 |     size_t i; | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 1992 |  | 
| Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 1993 |     for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) { | 
| Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 1994 |         size_t nbits; | 
 | 1995 |         int sign; | 
 | 1996 |         PyObject *plong; | 
 | 1997 |  | 
 | 1998 |         plong = PyLong_FromLong(testcases[i].input); | 
| Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 1999 |         if (plong == NULL) | 
 | 2000 |             return NULL; | 
| Christian Heimes | 3205e74 | 2013-07-26 15:06:48 +0200 | [diff] [blame] | 2001 |         nbits = _PyLong_NumBits(plong); | 
 | 2002 |         sign = _PyLong_Sign(plong); | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2003 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 |         Py_DECREF(plong); | 
 | 2005 |         if (nbits != testcases[i].nbits) | 
 | 2006 |             return raiseTestError("test_long_numbits", | 
 | 2007 |                             "wrong result for _PyLong_NumBits"); | 
 | 2008 |         if (sign != testcases[i].sign) | 
 | 2009 |             return raiseTestError("test_long_numbits", | 
 | 2010 |                             "wrong result for _PyLong_Sign"); | 
 | 2011 |     } | 
 | 2012 |     Py_INCREF(Py_None); | 
 | 2013 |     return Py_None; | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 2014 | } | 
 | 2015 |  | 
| Thomas Heller | 519a042 | 2007-11-15 20:48:54 +0000 | [diff] [blame] | 2016 | /* Example passing NULLs to PyObject_Str(NULL). */ | 
| Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2017 |  | 
 | 2018 | static PyObject * | 
 | 2019 | test_null_strings(PyObject *self) | 
 | 2020 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 |     PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL); | 
 | 2022 |     PyObject *tuple = PyTuple_Pack(2, o1, o2); | 
 | 2023 |     Py_XDECREF(o1); | 
 | 2024 |     Py_XDECREF(o2); | 
 | 2025 |     return tuple; | 
| Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2026 | } | 
 | 2027 |  | 
| Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2028 | static PyObject * | 
 | 2029 | raise_exception(PyObject *self, PyObject *args) | 
 | 2030 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 |     PyObject *exc; | 
 | 2032 |     PyObject *exc_args, *v; | 
 | 2033 |     int num_args, i; | 
| Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2034 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 |     if (!PyArg_ParseTuple(args, "Oi:raise_exception", | 
 | 2036 |                           &exc, &num_args)) | 
 | 2037 |         return NULL; | 
| Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2038 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 |     exc_args = PyTuple_New(num_args); | 
 | 2040 |     if (exc_args == NULL) | 
 | 2041 |         return NULL; | 
 | 2042 |     for (i = 0; i < num_args; ++i) { | 
 | 2043 |         v = PyLong_FromLong(i); | 
 | 2044 |         if (v == NULL) { | 
 | 2045 |             Py_DECREF(exc_args); | 
 | 2046 |             return NULL; | 
 | 2047 |         } | 
 | 2048 |         PyTuple_SET_ITEM(exc_args, i, v); | 
 | 2049 |     } | 
 | 2050 |     PyErr_SetObject(exc, exc_args); | 
 | 2051 |     Py_DECREF(exc_args); | 
 | 2052 |     return NULL; | 
| Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 2053 | } | 
| Tim Peters | 91621db | 2001-06-12 20:10:01 +0000 | [diff] [blame] | 2054 |  | 
| Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2055 | static PyObject * | 
| Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 2056 | set_errno(PyObject *self, PyObject *args) | 
 | 2057 | { | 
 | 2058 |     int new_errno; | 
 | 2059 |  | 
 | 2060 |     if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno)) | 
 | 2061 |         return NULL; | 
 | 2062 |  | 
 | 2063 |     errno = new_errno; | 
 | 2064 |     Py_RETURN_NONE; | 
 | 2065 | } | 
 | 2066 |  | 
 | 2067 | static PyObject * | 
| Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 2068 | test_set_exc_info(PyObject *self, PyObject *args) | 
 | 2069 | { | 
 | 2070 |     PyObject *orig_exc; | 
 | 2071 |     PyObject *new_type, *new_value, *new_tb; | 
 | 2072 |     PyObject *type, *value, *tb; | 
 | 2073 |     if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info", | 
 | 2074 |                           &new_type, &new_value, &new_tb)) | 
 | 2075 |         return NULL; | 
 | 2076 |  | 
 | 2077 |     PyErr_GetExcInfo(&type, &value, &tb); | 
 | 2078 |  | 
 | 2079 |     Py_INCREF(new_type); | 
 | 2080 |     Py_INCREF(new_value); | 
 | 2081 |     Py_INCREF(new_tb); | 
 | 2082 |     PyErr_SetExcInfo(new_type, new_value, new_tb); | 
 | 2083 |  | 
 | 2084 |     orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None); | 
 | 2085 |     Py_XDECREF(type); | 
 | 2086 |     Py_XDECREF(value); | 
 | 2087 |     Py_XDECREF(tb); | 
 | 2088 |     return orig_exc; | 
 | 2089 | } | 
| Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2090 |  | 
 | 2091 | static int test_run_counter = 0; | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2092 |  | 
| Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2093 | static PyObject * | 
 | 2094 | test_datetime_capi(PyObject *self, PyObject *args) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 |     if (PyDateTimeAPI) { | 
 | 2096 |         if (test_run_counter) { | 
 | 2097 |             /* Probably regrtest.py -R */ | 
 | 2098 |             Py_RETURN_NONE; | 
 | 2099 |         } | 
 | 2100 |         else { | 
 | 2101 |             PyErr_SetString(PyExc_AssertionError, | 
 | 2102 |                             "PyDateTime_CAPI somehow initialized"); | 
 | 2103 |             return NULL; | 
 | 2104 |         } | 
 | 2105 |     } | 
 | 2106 |     test_run_counter++; | 
 | 2107 |     PyDateTime_IMPORT; | 
 | 2108 |     if (PyDateTimeAPI) | 
 | 2109 |         Py_RETURN_NONE; | 
 | 2110 |     else | 
 | 2111 |         return NULL; | 
| Benjamin Peterson | 91d58bd | 2009-12-13 21:30:54 +0000 | [diff] [blame] | 2112 | } | 
 | 2113 |  | 
| Benjamin Peterson | 1632398 | 2010-02-03 01:13:41 +0000 | [diff] [blame] | 2114 |  | 
 | 2115 | #ifdef WITH_THREAD | 
 | 2116 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2117 | /* test_thread_state spawns a thread of its own, and that thread releases | 
 | 2118 |  * `thread_done` when it's finished.  The driver code has to know when the | 
 | 2119 |  * thread finishes, because the thread uses a PyObject (the callable) that | 
 | 2120 |  * may go away when the driver finishes.  The former lack of this explicit | 
 | 2121 |  * synchronization caused rare segfaults, so rare that they were seen only | 
 | 2122 |  * on a Mac buildbot (although they were possible on any box). | 
 | 2123 |  */ | 
 | 2124 | static PyThread_type_lock thread_done = NULL; | 
 | 2125 |  | 
| Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2126 | static int | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2127 | _make_call(void *callable) | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2128 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 |     PyObject *rc; | 
 | 2130 |     int success; | 
 | 2131 |     PyGILState_STATE s = PyGILState_Ensure(); | 
| Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 2132 |     rc = _PyObject_CallNoArg((PyObject *)callable); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 |     success = (rc != NULL); | 
 | 2134 |     Py_XDECREF(rc); | 
 | 2135 |     PyGILState_Release(s); | 
 | 2136 |     return success; | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2137 | } | 
 | 2138 |  | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2139 | /* Same thing, but releases `thread_done` when it returns.  This variant | 
 | 2140 |  * should be called only from threads spawned by test_thread_state(). | 
 | 2141 |  */ | 
 | 2142 | static void | 
 | 2143 | _make_call_from_thread(void *callable) | 
 | 2144 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 |     _make_call(callable); | 
 | 2146 |     PyThread_release_lock(thread_done); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2147 | } | 
 | 2148 |  | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2149 | static PyObject * | 
 | 2150 | test_thread_state(PyObject *self, PyObject *args) | 
 | 2151 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 |     PyObject *fn; | 
 | 2153 |     int success = 1; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2154 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 |     if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) | 
 | 2156 |         return NULL; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2157 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2158 |     if (!PyCallable_Check(fn)) { | 
 | 2159 |         PyErr_Format(PyExc_TypeError, "'%s' object is not callable", | 
 | 2160 |             fn->ob_type->tp_name); | 
 | 2161 |         return NULL; | 
 | 2162 |     } | 
| Benjamin Peterson | a786b02 | 2008-08-25 21:05:21 +0000 | [diff] [blame] | 2163 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 |     /* Ensure Python is set up for threading */ | 
 | 2165 |     PyEval_InitThreads(); | 
 | 2166 |     thread_done = PyThread_allocate_lock(); | 
 | 2167 |     if (thread_done == NULL) | 
 | 2168 |         return PyErr_NoMemory(); | 
 | 2169 |     PyThread_acquire_lock(thread_done, 1); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2170 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2171 |     /* Start a new thread with our callback. */ | 
 | 2172 |     PyThread_start_new_thread(_make_call_from_thread, fn); | 
 | 2173 |     /* Make the callback with the thread lock held by this thread */ | 
 | 2174 |     success &= _make_call(fn); | 
 | 2175 |     /* Do it all again, but this time with the thread-lock released */ | 
 | 2176 |     Py_BEGIN_ALLOW_THREADS | 
 | 2177 |     success &= _make_call(fn); | 
 | 2178 |     PyThread_acquire_lock(thread_done, 1);  /* wait for thread to finish */ | 
 | 2179 |     Py_END_ALLOW_THREADS | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2180 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2181 |     /* And once more with and without a thread | 
 | 2182 |        XXX - should use a lock and work out exactly what we are trying | 
 | 2183 |        to test <wink> | 
 | 2184 |     */ | 
 | 2185 |     Py_BEGIN_ALLOW_THREADS | 
 | 2186 |     PyThread_start_new_thread(_make_call_from_thread, fn); | 
 | 2187 |     success &= _make_call(fn); | 
 | 2188 |     PyThread_acquire_lock(thread_done, 1);  /* wait for thread to finish */ | 
 | 2189 |     Py_END_ALLOW_THREADS | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2190 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2191 |     /* Release lock we acquired above.  This is required on HP-UX. */ | 
 | 2192 |     PyThread_release_lock(thread_done); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2193 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 |     PyThread_free_lock(thread_done); | 
 | 2195 |     if (!success) | 
 | 2196 |         return NULL; | 
 | 2197 |     Py_RETURN_NONE; | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2198 | } | 
| Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2199 |  | 
 | 2200 | /* test Py_AddPendingCalls using threads */ | 
 | 2201 | static int _pending_callback(void *arg) | 
 | 2202 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 |     /* we assume the argument is callable object to which we own a reference */ | 
 | 2204 |     PyObject *callable = (PyObject *)arg; | 
 | 2205 |     PyObject *r = PyObject_CallObject(callable, NULL); | 
 | 2206 |     Py_DECREF(callable); | 
 | 2207 |     Py_XDECREF(r); | 
 | 2208 |     return r != NULL ? 0 : -1; | 
| Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2209 | } | 
 | 2210 |  | 
 | 2211 | /* The following requests n callbacks to _pending_callback.  It can be | 
 | 2212 |  * run from any python thread. | 
 | 2213 |  */ | 
 | 2214 | PyObject *pending_threadfunc(PyObject *self, PyObject *arg) | 
 | 2215 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 |     PyObject *callable; | 
 | 2217 |     int r; | 
 | 2218 |     if (PyArg_ParseTuple(arg, "O", &callable) == 0) | 
 | 2219 |         return NULL; | 
| Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2220 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 |     /* create the reference for the callbackwhile we hold the lock */ | 
 | 2222 |     Py_INCREF(callable); | 
| Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2223 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 |     Py_BEGIN_ALLOW_THREADS | 
 | 2225 |     r = Py_AddPendingCall(&_pending_callback, callable); | 
 | 2226 |     Py_END_ALLOW_THREADS | 
| Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2227 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 |     if (r<0) { | 
 | 2229 |         Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */ | 
 | 2230 |         Py_INCREF(Py_False); | 
 | 2231 |         return Py_False; | 
 | 2232 |     } | 
 | 2233 |     Py_INCREF(Py_True); | 
 | 2234 |     return Py_True; | 
| Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 2235 | } | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 2236 | #endif | 
 | 2237 |  | 
| Neal Norwitz | b0d2633 | 2007-08-25 00:49:05 +0000 | [diff] [blame] | 2238 | /* Some tests of PyUnicode_FromFormat().  This needs more tests. */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2239 | static PyObject * | 
 | 2240 | test_string_from_format(PyObject *self, PyObject *args) | 
 | 2241 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 |     PyObject *result; | 
 | 2243 |     char *msg; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2244 |  | 
| Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2245 | #define CHECK_1_FORMAT(FORMAT, TYPE)                                \ | 
 | 2246 |     result = PyUnicode_FromFormat(FORMAT, (TYPE)1);                 \ | 
 | 2247 |     if (result == NULL)                                             \ | 
 | 2248 |         return NULL;                                                \ | 
| Victor Stinner | 2b979bf | 2011-11-20 19:32:09 +0100 | [diff] [blame] | 2249 |     if (PyUnicode_CompareWithASCIIString(result, "1")) {     \ | 
| Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2250 |         msg = FORMAT " failed at 1";                                \ | 
 | 2251 |         goto Fail;                                                  \ | 
 | 2252 |     }                                                               \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 |     Py_DECREF(result) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2254 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 |     CHECK_1_FORMAT("%d", int); | 
 | 2256 |     CHECK_1_FORMAT("%ld", long); | 
 | 2257 |     /* The z width modifier was added in Python 2.5. */ | 
 | 2258 |     CHECK_1_FORMAT("%zd", Py_ssize_t); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2259 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2260 |     /* The u type code was added in Python 2.5. */ | 
 | 2261 |     CHECK_1_FORMAT("%u", unsigned int); | 
 | 2262 |     CHECK_1_FORMAT("%lu", unsigned long); | 
 | 2263 |     CHECK_1_FORMAT("%zu", size_t); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2264 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 |     /* "%lld" and "%llu" support added in Python 2.7. */ | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 2266 |     CHECK_1_FORMAT("%llu", unsigned long long); | 
 | 2267 |     CHECK_1_FORMAT("%lld", long long); | 
| Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 2268 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 |     Py_RETURN_NONE; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2270 |  | 
 | 2271 |  Fail: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 |     Py_XDECREF(result); | 
 | 2273 |     return raiseTestError("test_string_from_format", msg); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2274 |  | 
 | 2275 | #undef CHECK_1_FORMAT | 
 | 2276 | } | 
 | 2277 |  | 
| Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2278 |  | 
 | 2279 | static PyObject * | 
 | 2280 | test_unicode_compare_with_ascii(PyObject *self) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 |     PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4); | 
 | 2282 |     int result; | 
 | 2283 |     if (py_s == NULL) | 
 | 2284 |         return NULL; | 
 | 2285 |     result = PyUnicode_CompareWithASCIIString(py_s, "str"); | 
 | 2286 |     Py_DECREF(py_s); | 
 | 2287 |     if (!result) { | 
 | 2288 |         PyErr_SetString(TestError, "Python string ending in NULL " | 
 | 2289 |                         "should not compare equal to c string."); | 
 | 2290 |         return NULL; | 
 | 2291 |     } | 
 | 2292 |     Py_RETURN_NONE; | 
| Victor Stinner | 3e2b717 | 2010-11-09 09:32:19 +0000 | [diff] [blame] | 2293 | } | 
| Benjamin Peterson | 8667a9b | 2010-01-09 21:45:28 +0000 | [diff] [blame] | 2294 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2295 | /* This is here to provide a docstring for test_descr. */ | 
 | 2296 | static PyObject * | 
 | 2297 | test_with_docstring(PyObject *self) | 
 | 2298 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 |     Py_RETURN_NONE; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2300 | } | 
 | 2301 |  | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2302 | /* Test PyOS_string_to_double. */ | 
 | 2303 | static PyObject * | 
 | 2304 | test_string_to_double(PyObject *self) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 |     double result; | 
 | 2306 |     char *msg; | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2307 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | #define CHECK_STRING(STR, expected)                             \ | 
 | 2309 |     result = PyOS_string_to_double(STR, NULL, NULL);            \ | 
 | 2310 |     if (result == -1.0 && PyErr_Occurred())                     \ | 
 | 2311 |         return NULL;                                            \ | 
 | 2312 |     if (result != expected) {                                   \ | 
 | 2313 |         msg = "conversion of " STR " to float failed";          \ | 
 | 2314 |         goto fail;                                              \ | 
 | 2315 |     } | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2316 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | #define CHECK_INVALID(STR)                                              \ | 
 | 2318 |     result = PyOS_string_to_double(STR, NULL, NULL);                    \ | 
 | 2319 |     if (result == -1.0 && PyErr_Occurred()) {                           \ | 
 | 2320 |         if (PyErr_ExceptionMatches(PyExc_ValueError))                   \ | 
 | 2321 |             PyErr_Clear();                                              \ | 
 | 2322 |         else                                                            \ | 
 | 2323 |             return NULL;                                                \ | 
 | 2324 |     }                                                                   \ | 
 | 2325 |     else {                                                              \ | 
 | 2326 |         msg = "conversion of " STR " didn't raise ValueError";          \ | 
 | 2327 |         goto fail;                                                      \ | 
 | 2328 |     } | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2329 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 |     CHECK_STRING("0.1", 0.1); | 
 | 2331 |     CHECK_STRING("1.234", 1.234); | 
 | 2332 |     CHECK_STRING("-1.35", -1.35); | 
 | 2333 |     CHECK_STRING(".1e01", 1.0); | 
 | 2334 |     CHECK_STRING("2.e-2", 0.02); | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2335 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2336 |     CHECK_INVALID(" 0.1"); | 
 | 2337 |     CHECK_INVALID("\t\n-3"); | 
 | 2338 |     CHECK_INVALID(".123 "); | 
 | 2339 |     CHECK_INVALID("3\n"); | 
 | 2340 |     CHECK_INVALID("123abc"); | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2341 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 |     Py_RETURN_NONE; | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2343 |   fail: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 |     return raiseTestError("test_string_to_double", msg); | 
| Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 2345 | #undef CHECK_STRING | 
 | 2346 | #undef CHECK_INVALID | 
 | 2347 | } | 
 | 2348 |  | 
 | 2349 |  | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2350 | /* Coverage testing of capsule objects. */ | 
 | 2351 |  | 
 | 2352 | static const char *capsule_name = "capsule name"; | 
 | 2353 | static       char *capsule_pointer = "capsule pointer"; | 
 | 2354 | static       char *capsule_context = "capsule context"; | 
 | 2355 | static const char *capsule_error = NULL; | 
 | 2356 | static int | 
 | 2357 | capsule_destructor_call_count = 0; | 
 | 2358 |  | 
 | 2359 | static void | 
 | 2360 | capsule_destructor(PyObject *o) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 |     capsule_destructor_call_count++; | 
 | 2362 |     if (PyCapsule_GetContext(o) != capsule_context) { | 
 | 2363 |         capsule_error = "context did not match in destructor!"; | 
 | 2364 |     } else if (PyCapsule_GetDestructor(o) != capsule_destructor) { | 
 | 2365 |         capsule_error = "destructor did not match in destructor!  (woah!)"; | 
 | 2366 |     } else if (PyCapsule_GetName(o) != capsule_name) { | 
 | 2367 |         capsule_error = "name did not match in destructor!"; | 
 | 2368 |     } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) { | 
 | 2369 |         capsule_error = "pointer did not match in destructor!"; | 
 | 2370 |     } | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2371 | } | 
 | 2372 |  | 
 | 2373 | typedef struct { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 |     char *name; | 
 | 2375 |     char *module; | 
 | 2376 |     char *attribute; | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2377 | } known_capsule; | 
 | 2378 |  | 
 | 2379 | static PyObject * | 
 | 2380 | test_capsule(PyObject *self, PyObject *args) | 
 | 2381 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 |     PyObject *object; | 
 | 2383 |     const char *error = NULL; | 
 | 2384 |     void *pointer; | 
 | 2385 |     void *pointer2; | 
 | 2386 |     known_capsule known_capsules[] = { | 
 | 2387 |         #define KNOWN_CAPSULE(module, name)             { module "." name, module, name } | 
 | 2388 |         KNOWN_CAPSULE("_socket", "CAPI"), | 
 | 2389 |         KNOWN_CAPSULE("_curses", "_C_API"), | 
 | 2390 |         KNOWN_CAPSULE("datetime", "datetime_CAPI"), | 
 | 2391 |         { NULL, NULL }, | 
 | 2392 |     }; | 
 | 2393 |     known_capsule *known = &known_capsules[0]; | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2394 |  | 
 | 2395 | #define FAIL(x) { error = (x); goto exit; } | 
 | 2396 |  | 
 | 2397 | #define CHECK_DESTRUCTOR \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 |     if (capsule_error) { \ | 
 | 2399 |         FAIL(capsule_error); \ | 
 | 2400 |     } \ | 
 | 2401 |     else if (!capsule_destructor_call_count) {          \ | 
 | 2402 |         FAIL("destructor not called!"); \ | 
 | 2403 |     } \ | 
 | 2404 |     capsule_destructor_call_count = 0; \ | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2405 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 |     object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor); | 
 | 2407 |     PyCapsule_SetContext(object, capsule_context); | 
 | 2408 |     capsule_destructor(object); | 
 | 2409 |     CHECK_DESTRUCTOR; | 
 | 2410 |     Py_DECREF(object); | 
 | 2411 |     CHECK_DESTRUCTOR; | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2412 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 |     object = PyCapsule_New(known, "ignored", NULL); | 
 | 2414 |     PyCapsule_SetPointer(object, capsule_pointer); | 
 | 2415 |     PyCapsule_SetName(object, capsule_name); | 
 | 2416 |     PyCapsule_SetDestructor(object, capsule_destructor); | 
 | 2417 |     PyCapsule_SetContext(object, capsule_context); | 
 | 2418 |     capsule_destructor(object); | 
 | 2419 |     CHECK_DESTRUCTOR; | 
 | 2420 |     /* intentionally access using the wrong name */ | 
 | 2421 |     pointer2 = PyCapsule_GetPointer(object, "the wrong name"); | 
 | 2422 |     if (!PyErr_Occurred()) { | 
 | 2423 |         FAIL("PyCapsule_GetPointer should have failed but did not!"); | 
 | 2424 |     } | 
 | 2425 |     PyErr_Clear(); | 
 | 2426 |     if (pointer2) { | 
 | 2427 |         if (pointer2 == capsule_pointer) { | 
 | 2428 |             FAIL("PyCapsule_GetPointer should not have" | 
 | 2429 |                      " returned the internal pointer!"); | 
 | 2430 |         } else { | 
 | 2431 |             FAIL("PyCapsule_GetPointer should have " | 
 | 2432 |                      "returned NULL pointer but did not!"); | 
 | 2433 |         } | 
 | 2434 |     } | 
 | 2435 |     PyCapsule_SetDestructor(object, NULL); | 
 | 2436 |     Py_DECREF(object); | 
 | 2437 |     if (capsule_destructor_call_count) { | 
 | 2438 |         FAIL("destructor called when it should not have been!"); | 
 | 2439 |     } | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2440 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2441 |     for (known = &known_capsules[0]; known->module != NULL; known++) { | 
 | 2442 |         /* yeah, ordinarily I wouldn't do this either, | 
 | 2443 |            but it's fine for this test harness. | 
 | 2444 |         */ | 
 | 2445 |         static char buffer[256]; | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2446 | #undef FAIL | 
 | 2447 | #define FAIL(x) \ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2448 |         { \ | 
 | 2449 |         sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \ | 
 | 2450 |             x, known->module, known->attribute); \ | 
 | 2451 |         error = buffer; \ | 
 | 2452 |         goto exit; \ | 
 | 2453 |         } \ | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2454 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 |         PyObject *module = PyImport_ImportModule(known->module); | 
 | 2456 |         if (module) { | 
 | 2457 |             pointer = PyCapsule_Import(known->name, 0); | 
 | 2458 |             if (!pointer) { | 
 | 2459 |                 Py_DECREF(module); | 
 | 2460 |                 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!"); | 
 | 2461 |             } | 
 | 2462 |             object = PyObject_GetAttrString(module, known->attribute); | 
 | 2463 |             if (!object) { | 
 | 2464 |                 Py_DECREF(module); | 
 | 2465 |                 return NULL; | 
 | 2466 |             } | 
 | 2467 |             pointer2 = PyCapsule_GetPointer(object, | 
 | 2468 |                                     "weebles wobble but they don't fall down"); | 
 | 2469 |             if (!PyErr_Occurred()) { | 
 | 2470 |                 Py_DECREF(object); | 
 | 2471 |                 Py_DECREF(module); | 
 | 2472 |                 FAIL("PyCapsule_GetPointer should have failed but did not!"); | 
 | 2473 |             } | 
 | 2474 |             PyErr_Clear(); | 
 | 2475 |             if (pointer2) { | 
 | 2476 |                 Py_DECREF(module); | 
 | 2477 |                 Py_DECREF(object); | 
 | 2478 |                 if (pointer2 == pointer) { | 
 | 2479 |                     FAIL("PyCapsule_GetPointer should not have" | 
 | 2480 |                              " returned its internal pointer!"); | 
 | 2481 |                 } else { | 
 | 2482 |                     FAIL("PyCapsule_GetPointer should have" | 
 | 2483 |                              " returned NULL pointer but did not!"); | 
 | 2484 |                 } | 
 | 2485 |             } | 
 | 2486 |             Py_DECREF(object); | 
 | 2487 |             Py_DECREF(module); | 
 | 2488 |         } | 
 | 2489 |         else | 
 | 2490 |             PyErr_Clear(); | 
 | 2491 |     } | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2492 |  | 
 | 2493 |   exit: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2494 |     if (error) { | 
 | 2495 |         return raiseTestError("test_capsule", error); | 
 | 2496 |     } | 
 | 2497 |     Py_RETURN_NONE; | 
| Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 2498 | #undef FAIL | 
 | 2499 | } | 
 | 2500 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2501 | #ifdef HAVE_GETTIMEOFDAY | 
 | 2502 | /* Profiling of integer performance */ | 
| Martin v. Löwis | 1c95155 | 2008-06-13 07:48:19 +0000 | [diff] [blame] | 2503 | 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] | 2504 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2505 |     e->tv_sec -= s->tv_sec; | 
 | 2506 |     e->tv_usec -= s->tv_usec; | 
 | 2507 |     if (e->tv_usec < 0) { | 
 | 2508 |         e->tv_sec -=1; | 
 | 2509 |         e->tv_usec += 1000000; | 
 | 2510 |     } | 
 | 2511 |     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] | 2512 | } | 
 | 2513 |  | 
 | 2514 | static PyObject * | 
 | 2515 | profile_int(PyObject *self, PyObject* args) | 
 | 2516 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2517 |     int i, k; | 
 | 2518 |     struct timeval start, stop; | 
 | 2519 |     PyObject *single, **multiple, *op1, *result; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2520 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 |     /* Test 1: Allocate and immediately deallocate | 
 | 2522 |        many small integers */ | 
 | 2523 |     gettimeofday(&start, NULL); | 
 | 2524 |     for(k=0; k < 20000; k++) | 
 | 2525 |         for(i=0; i < 1000; i++) { | 
 | 2526 |             single = PyLong_FromLong(i); | 
 | 2527 |             Py_DECREF(single); | 
 | 2528 |         } | 
 | 2529 |     gettimeofday(&stop, NULL); | 
 | 2530 |     print_delta(1, &start, &stop); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2531 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2532 |     /* Test 2: Allocate and immediately deallocate | 
 | 2533 |        many large integers */ | 
 | 2534 |     gettimeofday(&start, NULL); | 
 | 2535 |     for(k=0; k < 20000; k++) | 
 | 2536 |         for(i=0; i < 1000; i++) { | 
 | 2537 |             single = PyLong_FromLong(i+1000000); | 
 | 2538 |             Py_DECREF(single); | 
 | 2539 |         } | 
 | 2540 |     gettimeofday(&stop, NULL); | 
 | 2541 |     print_delta(2, &start, &stop); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2542 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 |     /* Test 3: Allocate a few integers, then release | 
 | 2544 |        them all simultaneously. */ | 
 | 2545 |     multiple = malloc(sizeof(PyObject*) * 1000); | 
| Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2546 |     if (multiple == NULL) | 
 | 2547 |         return PyErr_NoMemory(); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 |     gettimeofday(&start, NULL); | 
 | 2549 |     for(k=0; k < 20000; k++) { | 
 | 2550 |         for(i=0; i < 1000; i++) { | 
 | 2551 |             multiple[i] = PyLong_FromLong(i+1000000); | 
 | 2552 |         } | 
 | 2553 |         for(i=0; i < 1000; i++) { | 
 | 2554 |             Py_DECREF(multiple[i]); | 
 | 2555 |         } | 
 | 2556 |     } | 
 | 2557 |     gettimeofday(&stop, NULL); | 
 | 2558 |     print_delta(3, &start, &stop); | 
| Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2559 |     free(multiple); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2560 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 |     /* Test 4: Allocate many integers, then release | 
 | 2562 |        them all simultaneously. */ | 
 | 2563 |     multiple = malloc(sizeof(PyObject*) * 1000000); | 
| Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2564 |     if (multiple == NULL) | 
 | 2565 |         return PyErr_NoMemory(); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 |     gettimeofday(&start, NULL); | 
 | 2567 |     for(k=0; k < 20; k++) { | 
 | 2568 |         for(i=0; i < 1000000; i++) { | 
 | 2569 |             multiple[i] = PyLong_FromLong(i+1000000); | 
 | 2570 |         } | 
 | 2571 |         for(i=0; i < 1000000; i++) { | 
 | 2572 |             Py_DECREF(multiple[i]); | 
 | 2573 |         } | 
 | 2574 |     } | 
 | 2575 |     gettimeofday(&stop, NULL); | 
 | 2576 |     print_delta(4, &start, &stop); | 
| Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2577 |     free(multiple); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2578 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 |     /* Test 5: Allocate many integers < 32000 */ | 
 | 2580 |     multiple = malloc(sizeof(PyObject*) * 1000000); | 
| Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2581 |     if (multiple == NULL) | 
 | 2582 |         return PyErr_NoMemory(); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 |     gettimeofday(&start, NULL); | 
 | 2584 |     for(k=0; k < 10; k++) { | 
 | 2585 |         for(i=0; i < 1000000; i++) { | 
 | 2586 |             multiple[i] = PyLong_FromLong(i+1000); | 
 | 2587 |         } | 
 | 2588 |         for(i=0; i < 1000000; i++) { | 
 | 2589 |             Py_DECREF(multiple[i]); | 
 | 2590 |         } | 
 | 2591 |     } | 
 | 2592 |     gettimeofday(&stop, NULL); | 
 | 2593 |     print_delta(5, &start, &stop); | 
| Christian Heimes | 7e13802 | 2013-07-26 15:03:50 +0200 | [diff] [blame] | 2594 |     free(multiple); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2595 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 |     /* Test 6: Perform small int addition */ | 
 | 2597 |     op1 = PyLong_FromLong(1); | 
 | 2598 |     gettimeofday(&start, NULL); | 
 | 2599 |     for(i=0; i < 10000000; i++) { | 
 | 2600 |         result = PyNumber_Add(op1, op1); | 
 | 2601 |         Py_DECREF(result); | 
 | 2602 |     } | 
 | 2603 |     gettimeofday(&stop, NULL); | 
 | 2604 |     Py_DECREF(op1); | 
 | 2605 |     print_delta(6, &start, &stop); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2606 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2607 |     /* Test 7: Perform medium int addition */ | 
 | 2608 |     op1 = PyLong_FromLong(1000); | 
| Christian Heimes | 66eda26 | 2013-07-26 15:54:07 +0200 | [diff] [blame] | 2609 |     if (op1 == NULL) | 
 | 2610 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2611 |     gettimeofday(&start, NULL); | 
 | 2612 |     for(i=0; i < 10000000; i++) { | 
 | 2613 |         result = PyNumber_Add(op1, op1); | 
| Christian Heimes | ff369a5 | 2013-07-26 14:52:18 +0200 | [diff] [blame] | 2614 |         Py_XDECREF(result); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2615 |     } | 
 | 2616 |     gettimeofday(&stop, NULL); | 
 | 2617 |     Py_DECREF(op1); | 
 | 2618 |     print_delta(7, &start, &stop); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2619 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 |     Py_INCREF(Py_None); | 
 | 2621 |     return Py_None; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2622 | } | 
 | 2623 | #endif | 
 | 2624 |  | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2625 | /* To test the format of tracebacks as printed out. */ | 
 | 2626 | static PyObject * | 
 | 2627 | traceback_print(PyObject *self, PyObject *args) | 
 | 2628 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 |     PyObject *file; | 
 | 2630 |     PyObject *traceback; | 
 | 2631 |     int result; | 
 | 2632 |  | 
 | 2633 |     if (!PyArg_ParseTuple(args, "OO:traceback_print", | 
 | 2634 |                             &traceback, &file)) | 
 | 2635 |         return NULL; | 
 | 2636 |  | 
 | 2637 |     result = PyTraceBack_Print(traceback, file); | 
 | 2638 |     if (result < 0) | 
 | 2639 |         return NULL; | 
 | 2640 |     Py_RETURN_NONE; | 
| Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 2641 | } | 
 | 2642 |  | 
| Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2643 | /* To test the format of exceptions as printed out. */ | 
 | 2644 | static PyObject * | 
 | 2645 | exception_print(PyObject *self, PyObject *args) | 
 | 2646 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 |     PyObject *value; | 
 | 2648 |     PyObject *tb; | 
| Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2649 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2650 |     if (!PyArg_ParseTuple(args, "O:exception_print", | 
 | 2651 |                             &value)) | 
 | 2652 |         return NULL; | 
 | 2653 |     if (!PyExceptionInstance_Check(value)) { | 
 | 2654 |         PyErr_Format(PyExc_TypeError, "an exception instance is required"); | 
 | 2655 |         return NULL; | 
 | 2656 |     } | 
| Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2657 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2658 |     tb = PyException_GetTraceback(value); | 
 | 2659 |     PyErr_Display((PyObject *) Py_TYPE(value), value, tb); | 
 | 2660 |     Py_XDECREF(tb); | 
| Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2661 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 |     Py_RETURN_NONE; | 
| Benjamin Peterson | e652821 | 2008-07-15 15:32:09 +0000 | [diff] [blame] | 2663 | } | 
 | 2664 |  | 
 | 2665 |  | 
 | 2666 |  | 
| Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2667 |  | 
 | 2668 | /* reliably raise a MemoryError */ | 
 | 2669 | static PyObject * | 
 | 2670 | raise_memoryerror(PyObject *self) | 
 | 2671 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 |     PyErr_NoMemory(); | 
 | 2673 |     return NULL; | 
| Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2674 | } | 
 | 2675 |  | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2676 | /* Issue 6012 */ | 
 | 2677 | static PyObject *str1, *str2; | 
 | 2678 | static int | 
 | 2679 | failing_converter(PyObject *obj, void *arg) | 
 | 2680 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 |     /* Clone str1, then let the conversion fail. */ | 
 | 2682 |     assert(str1); | 
 | 2683 |     str2 = str1; | 
 | 2684 |     Py_INCREF(str2); | 
 | 2685 |     return 0; | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2686 | } | 
 | 2687 | static PyObject* | 
 | 2688 | argparsing(PyObject *o, PyObject *args) | 
 | 2689 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 |     PyObject *res; | 
 | 2691 |     str1 = str2 = NULL; | 
 | 2692 |     if (!PyArg_ParseTuple(args, "O&O&", | 
 | 2693 |                           PyUnicode_FSConverter, &str1, | 
 | 2694 |                           failing_converter, &str2)) { | 
 | 2695 |         if (!str2) | 
 | 2696 |             /* argument converter not called? */ | 
 | 2697 |             return NULL; | 
 | 2698 |         /* Should be 1 */ | 
| Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 2699 |         res = PyLong_FromSsize_t(Py_REFCNT(str2)); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2700 |         Py_DECREF(str2); | 
 | 2701 |         PyErr_Clear(); | 
 | 2702 |         return res; | 
 | 2703 |     } | 
 | 2704 |     Py_RETURN_NONE; | 
| Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 2705 | } | 
| Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 2706 |  | 
| Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2707 | /* To test that the result of PyCode_NewEmpty has the right members. */ | 
 | 2708 | static PyObject * | 
 | 2709 | code_newempty(PyObject *self, PyObject *args) | 
 | 2710 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 |     const char *filename; | 
 | 2712 |     const char *funcname; | 
 | 2713 |     int firstlineno; | 
| Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2714 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 |     if (!PyArg_ParseTuple(args, "ssi:code_newempty", | 
 | 2716 |                           &filename, &funcname, &firstlineno)) | 
 | 2717 |         return NULL; | 
| Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2718 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 |     return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno); | 
| Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 2720 | } | 
 | 2721 |  | 
| Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2722 | /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException). | 
 | 2723 |    Run via Lib/test/test_exceptions.py */ | 
 | 2724 | static PyObject * | 
 | 2725 | make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs) | 
 | 2726 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 |     const char *name; | 
 | 2728 |     const char *doc = NULL; | 
 | 2729 |     PyObject *base = NULL; | 
 | 2730 |     PyObject *dict = NULL; | 
| Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2731 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2732 |     static char *kwlist[] = {"name", "doc", "base", "dict", NULL}; | 
| Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2733 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2734 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, | 
 | 2735 |                     "s|sOO:make_exception_with_doc", kwlist, | 
 | 2736 |                                      &name, &doc, &base, &dict)) | 
 | 2737 |         return NULL; | 
| Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2738 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 |     return PyErr_NewExceptionWithDoc(name, doc, base, dict); | 
| Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 2740 | } | 
 | 2741 |  | 
| Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2742 | static PyObject * | 
 | 2743 | make_memoryview_from_NULL_pointer(PyObject *self) | 
 | 2744 | { | 
 | 2745 |     Py_buffer info; | 
 | 2746 |     if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0) | 
 | 2747 |         return NULL; | 
 | 2748 |     return PyMemoryView_FromBuffer(&info); | 
 | 2749 | } | 
| Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 2750 |  | 
| Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 2751 | static PyObject * | 
 | 2752 | test_from_contiguous(PyObject* self, PyObject *noargs) | 
 | 2753 | { | 
 | 2754 |     int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; | 
 | 2755 |     int init[5] = {0, 1, 2, 3, 4}; | 
 | 2756 |     Py_ssize_t itemsize = sizeof(int); | 
 | 2757 |     Py_ssize_t shape = 5; | 
 | 2758 |     Py_ssize_t strides = 2 * itemsize; | 
 | 2759 |     Py_buffer view = { | 
 | 2760 |         data, | 
 | 2761 |         NULL, | 
 | 2762 |         5 * itemsize, | 
 | 2763 |         itemsize, | 
 | 2764 |         1, | 
 | 2765 |         1, | 
 | 2766 |         NULL, | 
 | 2767 |         &shape, | 
 | 2768 |         &strides, | 
 | 2769 |         NULL, | 
 | 2770 |         NULL | 
 | 2771 |     }; | 
 | 2772 |     int *ptr; | 
 | 2773 |     int i; | 
 | 2774 |  | 
 | 2775 |     PyBuffer_FromContiguous(&view, init, view.len, 'C'); | 
 | 2776 |     ptr = view.buf; | 
 | 2777 |     for (i = 0; i < 5; i++) { | 
 | 2778 |         if (ptr[2*i] != i) { | 
 | 2779 |             PyErr_SetString(TestError, | 
 | 2780 |                 "test_from_contiguous: incorrect result"); | 
 | 2781 |             return NULL; | 
 | 2782 |         } | 
 | 2783 |     } | 
 | 2784 |  | 
 | 2785 |     view.buf = &data[8]; | 
 | 2786 |     view.strides[0] = -2 * itemsize; | 
 | 2787 |  | 
 | 2788 |     PyBuffer_FromContiguous(&view, init, view.len, 'C'); | 
 | 2789 |     ptr = view.buf; | 
 | 2790 |     for (i = 0; i < 5; i++) { | 
 | 2791 |         if (*(ptr-2*i) != i) { | 
 | 2792 |             PyErr_SetString(TestError, | 
 | 2793 |                 "test_from_contiguous: incorrect result"); | 
 | 2794 |             return NULL; | 
 | 2795 |         } | 
 | 2796 |     } | 
 | 2797 |  | 
 | 2798 |     Py_RETURN_NONE; | 
 | 2799 | } | 
| Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2800 |  | 
| Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 2801 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) | 
| Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2802 | extern PyTypeObject _PyBytesIOBuffer_Type; | 
 | 2803 |  | 
| Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2804 | static PyObject * | 
 | 2805 | test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs) | 
 | 2806 | { | 
| Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2807 |     PyTypeObject *type = &_PyBytesIOBuffer_Type; | 
| Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2808 |     PyObject *b; | 
 | 2809 |     char *dummy[1]; | 
 | 2810 |     int ret, match; | 
 | 2811 |  | 
| Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2812 |     /* PyBuffer_FillInfo() */ | 
| Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2813 |     ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE); | 
 | 2814 |     match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); | 
 | 2815 |     PyErr_Clear(); | 
 | 2816 |     if (ret != -1 || match == 0) | 
 | 2817 |         goto error; | 
 | 2818 |  | 
| Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2819 |     /* bytesiobuf_getbuffer() */ | 
 | 2820 |     b = type->tp_alloc(type, 0); | 
| Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 2821 |     if (b == NULL) { | 
 | 2822 |         return NULL; | 
 | 2823 |     } | 
 | 2824 |  | 
 | 2825 |     ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE); | 
 | 2826 |     Py_DECREF(b); | 
 | 2827 |     match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError); | 
 | 2828 |     PyErr_Clear(); | 
 | 2829 |     if (ret != -1 || match == 0) | 
 | 2830 |         goto error; | 
 | 2831 |  | 
 | 2832 |     Py_RETURN_NONE; | 
 | 2833 |  | 
 | 2834 | error: | 
 | 2835 |     PyErr_SetString(TestError, | 
 | 2836 |         "test_pep3118_obsolete_write_locks: failure"); | 
 | 2837 |     return NULL; | 
 | 2838 | } | 
| Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 2839 | #endif | 
| Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 2840 |  | 
| Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 2841 | /* This tests functions that historically supported write locks.  It is | 
 | 2842 |    wrong to call getbuffer() with view==NULL and a compliant getbufferproc | 
 | 2843 |    is entitled to segfault in that case. */ | 
 | 2844 | static PyObject * | 
 | 2845 | getbuffer_with_null_view(PyObject* self, PyObject *obj) | 
 | 2846 | { | 
 | 2847 |     if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0) | 
 | 2848 |         return NULL; | 
 | 2849 |  | 
 | 2850 |     Py_RETURN_NONE; | 
 | 2851 | } | 
 | 2852 |  | 
| Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2853 | /* Test that the fatal error from not having a current thread doesn't | 
 | 2854 |    cause an infinite loop.  Run via Lib/test/test_capi.py */ | 
 | 2855 | static PyObject * | 
 | 2856 | crash_no_current_thread(PyObject *self) | 
 | 2857 | { | 
 | 2858 |     Py_BEGIN_ALLOW_THREADS | 
| Jeffrey Yasskin | ea7b748 | 2010-05-17 16:59:23 +0000 | [diff] [blame] | 2859 |     /* Using PyThreadState_Get() directly allows the test to pass in | 
 | 2860 |        !pydebug mode. However, the test only actually tests anything | 
 | 2861 |        in pydebug mode, since that's where the infinite loop was in | 
 | 2862 |        the first place. */ | 
 | 2863 |     PyThreadState_Get(); | 
| Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 2864 |     Py_END_ALLOW_THREADS | 
 | 2865 |     return NULL; | 
 | 2866 | } | 
 | 2867 |  | 
| Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2868 | /* To run some code in a sub-interpreter. */ | 
 | 2869 | static PyObject * | 
 | 2870 | run_in_subinterp(PyObject *self, PyObject *args) | 
 | 2871 | { | 
 | 2872 |     const char *code; | 
 | 2873 |     int r; | 
 | 2874 |     PyThreadState *substate, *mainstate; | 
 | 2875 |  | 
 | 2876 |     if (!PyArg_ParseTuple(args, "s:run_in_subinterp", | 
 | 2877 |                           &code)) | 
 | 2878 |         return NULL; | 
 | 2879 |  | 
 | 2880 |     mainstate = PyThreadState_Get(); | 
 | 2881 |  | 
 | 2882 |     PyThreadState_Swap(NULL); | 
 | 2883 |  | 
 | 2884 |     substate = Py_NewInterpreter(); | 
| Brett Cannon | b685568 | 2012-02-03 12:08:03 -0500 | [diff] [blame] | 2885 |     if (substate == NULL) { | 
 | 2886 |         /* Since no new thread state was created, there is no exception to | 
 | 2887 |            propagate; raise a fresh one after swapping in the old thread | 
 | 2888 |            state. */ | 
 | 2889 |         PyThreadState_Swap(mainstate); | 
 | 2890 |         PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed"); | 
 | 2891 |         return NULL; | 
 | 2892 |     } | 
| Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 2893 |     r = PyRun_SimpleString(code); | 
 | 2894 |     Py_EndInterpreter(substate); | 
 | 2895 |  | 
 | 2896 |     PyThreadState_Swap(mainstate); | 
 | 2897 |  | 
 | 2898 |     return PyLong_FromLong(r); | 
 | 2899 | } | 
 | 2900 |  | 
| Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2901 | static int | 
 | 2902 | check_time_rounding(int round) | 
 | 2903 | { | 
| Victor Stinner | 7447423 | 2015-09-02 01:43:56 +0200 | [diff] [blame] | 2904 |     if (round != _PyTime_ROUND_FLOOR | 
 | 2905 |         && round != _PyTime_ROUND_CEILING | 
| Victor Stinner | 7667f58 | 2015-09-09 01:02:23 +0200 | [diff] [blame] | 2906 |         && round != _PyTime_ROUND_HALF_EVEN) { | 
| Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2907 |         PyErr_SetString(PyExc_ValueError, "invalid rounding"); | 
 | 2908 |         return -1; | 
 | 2909 |     } | 
 | 2910 |     return 0; | 
 | 2911 | } | 
 | 2912 |  | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2913 | static PyObject * | 
 | 2914 | test_pytime_object_to_time_t(PyObject *self, PyObject *args) | 
 | 2915 | { | 
 | 2916 |     PyObject *obj; | 
 | 2917 |     time_t sec; | 
| Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2918 |     int round; | 
 | 2919 |     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round)) | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2920 |         return NULL; | 
| Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2921 |     if (check_time_rounding(round) < 0) | 
 | 2922 |         return NULL; | 
 | 2923 |     if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2924 |         return NULL; | 
 | 2925 |     return _PyLong_FromTime_t(sec); | 
 | 2926 | } | 
 | 2927 |  | 
 | 2928 | static PyObject * | 
 | 2929 | test_pytime_object_to_timeval(PyObject *self, PyObject *args) | 
 | 2930 | { | 
 | 2931 |     PyObject *obj; | 
 | 2932 |     time_t sec; | 
 | 2933 |     long usec; | 
| Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2934 |     int round; | 
 | 2935 |     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round)) | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2936 |         return NULL; | 
| Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2937 |     if (check_time_rounding(round) < 0) | 
 | 2938 |         return NULL; | 
 | 2939 |     if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2940 |         return NULL; | 
 | 2941 |     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec); | 
 | 2942 | } | 
 | 2943 |  | 
| Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2944 | static PyObject * | 
 | 2945 | test_pytime_object_to_timespec(PyObject *self, PyObject *args) | 
 | 2946 | { | 
 | 2947 |     PyObject *obj; | 
 | 2948 |     time_t sec; | 
 | 2949 |     long nsec; | 
| Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2950 |     int round; | 
 | 2951 |     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round)) | 
| Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2952 |         return NULL; | 
| Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2953 |     if (check_time_rounding(round) < 0) | 
 | 2954 |         return NULL; | 
 | 2955 |     if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) | 
| Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2956 |         return NULL; | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2957 |     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec); | 
| Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 2958 | } | 
 | 2959 |  | 
| Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 2960 | static void | 
 | 2961 | slot_tp_del(PyObject *self) | 
 | 2962 | { | 
 | 2963 |     _Py_IDENTIFIER(__tp_del__); | 
 | 2964 |     PyObject *del, *res; | 
 | 2965 |     PyObject *error_type, *error_value, *error_traceback; | 
 | 2966 |  | 
 | 2967 |     /* Temporarily resurrect the object. */ | 
 | 2968 |     assert(self->ob_refcnt == 0); | 
 | 2969 |     self->ob_refcnt = 1; | 
 | 2970 |  | 
 | 2971 |     /* Save the current exception, if any. */ | 
 | 2972 |     PyErr_Fetch(&error_type, &error_value, &error_traceback); | 
 | 2973 |  | 
 | 2974 |     /* Execute __del__ method, if any. */ | 
 | 2975 |     del = _PyObject_LookupSpecial(self, &PyId___tp_del__); | 
 | 2976 |     if (del != NULL) { | 
 | 2977 |         res = PyEval_CallObject(del, NULL); | 
 | 2978 |         if (res == NULL) | 
 | 2979 |             PyErr_WriteUnraisable(del); | 
 | 2980 |         else | 
 | 2981 |             Py_DECREF(res); | 
 | 2982 |         Py_DECREF(del); | 
 | 2983 |     } | 
 | 2984 |  | 
 | 2985 |     /* Restore the saved exception. */ | 
 | 2986 |     PyErr_Restore(error_type, error_value, error_traceback); | 
 | 2987 |  | 
 | 2988 |     /* Undo the temporary resurrection; can't use DECREF here, it would | 
 | 2989 |      * cause a recursive call. | 
 | 2990 |      */ | 
 | 2991 |     assert(self->ob_refcnt > 0); | 
 | 2992 |     if (--self->ob_refcnt == 0) | 
 | 2993 |         return;         /* this is the normal path out */ | 
 | 2994 |  | 
 | 2995 |     /* __del__ resurrected it!  Make it look like the original Py_DECREF | 
 | 2996 |      * never happened. | 
 | 2997 |      */ | 
 | 2998 |     { | 
 | 2999 |         Py_ssize_t refcnt = self->ob_refcnt; | 
 | 3000 |         _Py_NewReference(self); | 
 | 3001 |         self->ob_refcnt = refcnt; | 
 | 3002 |     } | 
 | 3003 |     assert(!PyType_IS_GC(Py_TYPE(self)) || | 
 | 3004 |            _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); | 
 | 3005 |     /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so | 
 | 3006 |      * we need to undo that. */ | 
 | 3007 |     _Py_DEC_REFTOTAL; | 
 | 3008 |     /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object | 
 | 3009 |      * chain, so no more to do there. | 
 | 3010 |      * If COUNT_ALLOCS, the original decref bumped tp_frees, and | 
 | 3011 |      * _Py_NewReference bumped tp_allocs:  both of those need to be | 
 | 3012 |      * undone. | 
 | 3013 |      */ | 
 | 3014 | #ifdef COUNT_ALLOCS | 
 | 3015 |     --Py_TYPE(self)->tp_frees; | 
 | 3016 |     --Py_TYPE(self)->tp_allocs; | 
 | 3017 | #endif | 
 | 3018 | } | 
 | 3019 |  | 
 | 3020 | static PyObject * | 
 | 3021 | with_tp_del(PyObject *self, PyObject *args) | 
 | 3022 | { | 
 | 3023 |     PyObject *obj; | 
 | 3024 |     PyTypeObject *tp; | 
 | 3025 |  | 
 | 3026 |     if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj)) | 
 | 3027 |         return NULL; | 
 | 3028 |     tp = (PyTypeObject *) obj; | 
 | 3029 |     if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) { | 
 | 3030 |         PyErr_Format(PyExc_TypeError, | 
 | 3031 |                      "heap type expected, got %R", obj); | 
 | 3032 |         return NULL; | 
 | 3033 |     } | 
 | 3034 |     tp->tp_del = slot_tp_del; | 
 | 3035 |     Py_INCREF(obj); | 
 | 3036 |     return obj; | 
 | 3037 | } | 
 | 3038 |  | 
| Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 3039 | static PyMethodDef ml; | 
 | 3040 |  | 
 | 3041 | static PyObject * | 
 | 3042 | create_cfunction(PyObject *self, PyObject *args) | 
 | 3043 | { | 
 | 3044 |     return PyCFunction_NewEx(&ml, self, NULL); | 
 | 3045 | } | 
 | 3046 |  | 
 | 3047 | static PyMethodDef ml = { | 
 | 3048 |     "create_cfunction", | 
 | 3049 |     create_cfunction, | 
 | 3050 |     METH_NOARGS, | 
 | 3051 |     NULL | 
 | 3052 | }; | 
 | 3053 |  | 
| Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3054 | static PyObject * | 
 | 3055 | _test_incref(PyObject *ob) | 
 | 3056 | { | 
 | 3057 |     Py_INCREF(ob); | 
 | 3058 |     return ob; | 
 | 3059 | } | 
 | 3060 |  | 
 | 3061 | static PyObject * | 
 | 3062 | test_xincref_doesnt_leak(PyObject *ob) | 
 | 3063 | { | 
 | 3064 |     PyObject *obj = PyLong_FromLong(0); | 
 | 3065 |     Py_XINCREF(_test_incref(obj)); | 
 | 3066 |     Py_DECREF(obj); | 
 | 3067 |     Py_DECREF(obj); | 
 | 3068 |     Py_DECREF(obj); | 
 | 3069 |     Py_RETURN_NONE; | 
 | 3070 | } | 
 | 3071 |  | 
 | 3072 | static PyObject * | 
 | 3073 | test_incref_doesnt_leak(PyObject *ob) | 
 | 3074 | { | 
 | 3075 |     PyObject *obj = PyLong_FromLong(0); | 
 | 3076 |     Py_INCREF(_test_incref(obj)); | 
 | 3077 |     Py_DECREF(obj); | 
 | 3078 |     Py_DECREF(obj); | 
 | 3079 |     Py_DECREF(obj); | 
 | 3080 |     Py_RETURN_NONE; | 
 | 3081 | } | 
 | 3082 |  | 
 | 3083 | static PyObject * | 
 | 3084 | test_xdecref_doesnt_leak(PyObject *ob) | 
 | 3085 | { | 
 | 3086 |     Py_XDECREF(PyLong_FromLong(0)); | 
 | 3087 |     Py_RETURN_NONE; | 
 | 3088 | } | 
 | 3089 |  | 
 | 3090 | static PyObject * | 
 | 3091 | test_decref_doesnt_leak(PyObject *ob) | 
 | 3092 | { | 
 | 3093 |     Py_DECREF(PyLong_FromLong(0)); | 
 | 3094 |     Py_RETURN_NONE; | 
 | 3095 | } | 
| Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 3096 |  | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3097 | static PyObject * | 
| Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3098 | test_incref_decref_API(PyObject *ob) | 
 | 3099 | { | 
 | 3100 |     PyObject *obj = PyLong_FromLong(0); | 
| Victor Stinner | fc6a90a | 2014-10-09 22:15:41 +0200 | [diff] [blame] | 3101 |     Py_IncRef(obj); | 
| Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3102 |     Py_DecRef(obj); | 
 | 3103 |     Py_DecRef(obj); | 
 | 3104 |     Py_RETURN_NONE; | 
 | 3105 | } | 
 | 3106 |  | 
 | 3107 | static PyObject * | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3108 | test_pymem_alloc0(PyObject *self) | 
 | 3109 | { | 
 | 3110 |     void *ptr; | 
 | 3111 |  | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3112 |     ptr = PyMem_RawMalloc(0); | 
 | 3113 |     if (ptr == NULL) { | 
 | 3114 |         PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL"); | 
 | 3115 |         return NULL; | 
 | 3116 |     } | 
 | 3117 |     PyMem_RawFree(ptr); | 
 | 3118 |  | 
 | 3119 |     ptr = PyMem_RawCalloc(0, 0); | 
 | 3120 |     if (ptr == NULL) { | 
 | 3121 |         PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL"); | 
 | 3122 |         return NULL; | 
 | 3123 |     } | 
 | 3124 |     PyMem_RawFree(ptr); | 
 | 3125 |  | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3126 |     ptr = PyMem_Malloc(0); | 
 | 3127 |     if (ptr == NULL) { | 
 | 3128 |         PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL"); | 
 | 3129 |         return NULL; | 
 | 3130 |     } | 
 | 3131 |     PyMem_Free(ptr); | 
 | 3132 |  | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3133 |     ptr = PyMem_Calloc(0, 0); | 
 | 3134 |     if (ptr == NULL) { | 
 | 3135 |         PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL"); | 
 | 3136 |         return NULL; | 
 | 3137 |     } | 
 | 3138 |     PyMem_Free(ptr); | 
 | 3139 |  | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3140 |     ptr = PyObject_Malloc(0); | 
 | 3141 |     if (ptr == NULL) { | 
 | 3142 |         PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL"); | 
 | 3143 |         return NULL; | 
 | 3144 |     } | 
 | 3145 |     PyObject_Free(ptr); | 
 | 3146 |  | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3147 |     ptr = PyObject_Calloc(0, 0); | 
 | 3148 |     if (ptr == NULL) { | 
 | 3149 |         PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL"); | 
 | 3150 |         return NULL; | 
 | 3151 |     } | 
 | 3152 |     PyObject_Free(ptr); | 
 | 3153 |  | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3154 |     Py_RETURN_NONE; | 
 | 3155 | } | 
 | 3156 |  | 
 | 3157 | typedef struct { | 
| Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3158 |     PyMemAllocatorEx alloc; | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3159 |  | 
 | 3160 |     size_t malloc_size; | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3161 |     size_t calloc_nelem; | 
 | 3162 |     size_t calloc_elsize; | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3163 |     void *realloc_ptr; | 
 | 3164 |     size_t realloc_new_size; | 
 | 3165 |     void *free_ptr; | 
 | 3166 | } alloc_hook_t; | 
 | 3167 |  | 
 | 3168 | static void* hook_malloc (void* ctx, size_t size) | 
 | 3169 | { | 
 | 3170 |     alloc_hook_t *hook = (alloc_hook_t *)ctx; | 
 | 3171 |     hook->malloc_size = size; | 
 | 3172 |     return hook->alloc.malloc(hook->alloc.ctx, size); | 
 | 3173 | } | 
 | 3174 |  | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3175 | static void* hook_calloc (void* ctx, size_t nelem, size_t elsize) | 
 | 3176 | { | 
 | 3177 |     alloc_hook_t *hook = (alloc_hook_t *)ctx; | 
 | 3178 |     hook->calloc_nelem = nelem; | 
 | 3179 |     hook->calloc_elsize = elsize; | 
 | 3180 |     return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize); | 
 | 3181 | } | 
 | 3182 |  | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3183 | static void* hook_realloc (void* ctx, void* ptr, size_t new_size) | 
 | 3184 | { | 
 | 3185 |     alloc_hook_t *hook = (alloc_hook_t *)ctx; | 
 | 3186 |     hook->realloc_ptr = ptr; | 
 | 3187 |     hook->realloc_new_size = new_size; | 
 | 3188 |     return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size); | 
 | 3189 | } | 
 | 3190 |  | 
 | 3191 | static void hook_free (void *ctx, void *ptr) | 
 | 3192 | { | 
 | 3193 |     alloc_hook_t *hook = (alloc_hook_t *)ctx; | 
 | 3194 |     hook->free_ptr = ptr; | 
 | 3195 |     hook->alloc.free(hook->alloc.ctx, ptr); | 
 | 3196 | } | 
 | 3197 |  | 
 | 3198 | static PyObject * | 
 | 3199 | test_setallocators(PyMemAllocatorDomain domain) | 
 | 3200 | { | 
 | 3201 |     PyObject *res = NULL; | 
 | 3202 |     const char *error_msg; | 
 | 3203 |     alloc_hook_t hook; | 
| Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 3204 |     PyMemAllocatorEx alloc; | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3205 |     size_t size, size2, nelem, elsize; | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3206 |     void *ptr, *ptr2; | 
 | 3207 |  | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3208 |     memset(&hook, 0, sizeof(hook)); | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3209 |  | 
 | 3210 |     alloc.ctx = &hook; | 
 | 3211 |     alloc.malloc = &hook_malloc; | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3212 |     alloc.calloc = &hook_calloc; | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3213 |     alloc.realloc = &hook_realloc; | 
 | 3214 |     alloc.free = &hook_free; | 
 | 3215 |     PyMem_GetAllocator(domain, &hook.alloc); | 
 | 3216 |     PyMem_SetAllocator(domain, &alloc); | 
 | 3217 |  | 
 | 3218 |     size = 42; | 
 | 3219 |     switch(domain) | 
 | 3220 |     { | 
 | 3221 |     case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break; | 
 | 3222 |     case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break; | 
 | 3223 |     case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break; | 
 | 3224 |     default: ptr = NULL; break; | 
 | 3225 |     } | 
 | 3226 |  | 
 | 3227 |     if (ptr == NULL) { | 
 | 3228 |         error_msg = "malloc failed"; | 
 | 3229 |         goto fail; | 
 | 3230 |     } | 
 | 3231 |  | 
 | 3232 |     if (hook.malloc_size != size) { | 
 | 3233 |         error_msg = "malloc invalid size"; | 
 | 3234 |         goto fail; | 
 | 3235 |     } | 
 | 3236 |  | 
 | 3237 |     size2 = 200; | 
 | 3238 |     switch(domain) | 
 | 3239 |     { | 
 | 3240 |     case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break; | 
 | 3241 |     case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break; | 
 | 3242 |     case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break; | 
| Christian Heimes | 865d12a | 2013-08-02 11:10:51 +0200 | [diff] [blame] | 3243 |     default: ptr2 = NULL; break; | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3244 |     } | 
 | 3245 |  | 
 | 3246 |     if (ptr2 == NULL) { | 
 | 3247 |         error_msg = "realloc failed"; | 
 | 3248 |         goto fail; | 
 | 3249 |     } | 
 | 3250 |  | 
 | 3251 |     if (hook.realloc_ptr != ptr | 
 | 3252 |         || hook.realloc_new_size != size2) { | 
 | 3253 |         error_msg = "realloc invalid parameters"; | 
 | 3254 |         goto fail; | 
 | 3255 |     } | 
 | 3256 |  | 
 | 3257 |     switch(domain) | 
 | 3258 |     { | 
 | 3259 |     case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break; | 
 | 3260 |     case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break; | 
 | 3261 |     case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break; | 
 | 3262 |     } | 
 | 3263 |  | 
 | 3264 |     if (hook.free_ptr != ptr2) { | 
 | 3265 |         error_msg = "free invalid pointer"; | 
 | 3266 |         goto fail; | 
 | 3267 |     } | 
 | 3268 |  | 
| Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3269 |     nelem = 2; | 
 | 3270 |     elsize = 5; | 
 | 3271 |     switch(domain) | 
 | 3272 |     { | 
 | 3273 |     case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break; | 
 | 3274 |     case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break; | 
 | 3275 |     case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break; | 
 | 3276 |     default: ptr = NULL; break; | 
 | 3277 |     } | 
 | 3278 |  | 
 | 3279 |     if (ptr == NULL) { | 
 | 3280 |         error_msg = "calloc failed"; | 
 | 3281 |         goto fail; | 
 | 3282 |     } | 
 | 3283 |  | 
 | 3284 |     if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) { | 
 | 3285 |         error_msg = "calloc invalid nelem or elsize"; | 
 | 3286 |         goto fail; | 
 | 3287 |     } | 
 | 3288 |  | 
 | 3289 |     switch(domain) | 
 | 3290 |     { | 
 | 3291 |     case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break; | 
 | 3292 |     case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break; | 
 | 3293 |     case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break; | 
 | 3294 |     } | 
 | 3295 |  | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3296 |     Py_INCREF(Py_None); | 
 | 3297 |     res = Py_None; | 
 | 3298 |     goto finally; | 
 | 3299 |  | 
 | 3300 | fail: | 
 | 3301 |     PyErr_SetString(PyExc_RuntimeError, error_msg); | 
 | 3302 |  | 
 | 3303 | finally: | 
 | 3304 |     PyMem_SetAllocator(domain, &hook.alloc); | 
 | 3305 |     return res; | 
 | 3306 | } | 
 | 3307 |  | 
 | 3308 | static PyObject * | 
 | 3309 | test_pymem_setrawallocators(PyObject *self) | 
 | 3310 | { | 
 | 3311 |     return test_setallocators(PYMEM_DOMAIN_RAW); | 
 | 3312 | } | 
 | 3313 |  | 
 | 3314 | static PyObject * | 
 | 3315 | test_pymem_setallocators(PyObject *self) | 
 | 3316 | { | 
 | 3317 |     return test_setallocators(PYMEM_DOMAIN_MEM); | 
 | 3318 | } | 
 | 3319 |  | 
 | 3320 | static PyObject * | 
 | 3321 | test_pyobject_setallocators(PyObject *self) | 
 | 3322 | { | 
 | 3323 |     return test_setallocators(PYMEM_DOMAIN_OBJ); | 
 | 3324 | } | 
 | 3325 |  | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3326 | PyDoc_STRVAR(docstring_empty, | 
 | 3327 | "" | 
 | 3328 | ); | 
 | 3329 |  | 
 | 3330 | PyDoc_STRVAR(docstring_no_signature, | 
 | 3331 | "This docstring has no signature." | 
 | 3332 | ); | 
 | 3333 |  | 
 | 3334 | PyDoc_STRVAR(docstring_with_invalid_signature, | 
| Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3335 | "docstring_with_invalid_signature($module, /, boo)\n" | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3336 | "\n" | 
 | 3337 | "This docstring has an invalid signature." | 
 | 3338 | ); | 
 | 3339 |  | 
| Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3340 | PyDoc_STRVAR(docstring_with_invalid_signature2, | 
 | 3341 | "docstring_with_invalid_signature2($module, /, boo)\n" | 
 | 3342 | "\n" | 
 | 3343 | "--\n" | 
 | 3344 | "\n" | 
 | 3345 | "This docstring also has an invalid signature." | 
 | 3346 | ); | 
 | 3347 |  | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3348 | PyDoc_STRVAR(docstring_with_signature, | 
| Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3349 | "docstring_with_signature($module, /, sig)\n" | 
 | 3350 | "--\n" | 
 | 3351 | "\n" | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3352 | "This docstring has a valid signature." | 
 | 3353 | ); | 
 | 3354 |  | 
| Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 3355 | PyDoc_STRVAR(docstring_with_signature_but_no_doc, | 
 | 3356 | "docstring_with_signature_but_no_doc($module, /, sig)\n" | 
 | 3357 | "--\n" | 
 | 3358 | "\n" | 
 | 3359 | ); | 
 | 3360 |  | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3361 | PyDoc_STRVAR(docstring_with_signature_and_extra_newlines, | 
| Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3362 | "docstring_with_signature_and_extra_newlines($module, /, parameter)\n" | 
 | 3363 | "--\n" | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 3364 | "\n" | 
 | 3365 | "\n" | 
 | 3366 | "This docstring has a valid signature and some extra newlines." | 
 | 3367 | ); | 
 | 3368 |  | 
| Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 3369 | PyDoc_STRVAR(docstring_with_signature_with_defaults, | 
| Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 3370 | "docstring_with_signature_with_defaults(module, s='avocado',\n" | 
 | 3371 | "        b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n" | 
 | 3372 | "        local=the_number_three, sys=sys.maxsize,\n" | 
 | 3373 | "        exp=sys.maxsize - 1)\n" | 
 | 3374 | "--\n" | 
| Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 3375 | "\n" | 
 | 3376 | "\n" | 
 | 3377 | "\n" | 
 | 3378 | "This docstring has a valid signature with parameters,\n" | 
 | 3379 | "and the parameters take defaults of varying types." | 
 | 3380 | ); | 
 | 3381 |  | 
| Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 3382 | #ifdef WITH_THREAD | 
| Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3383 | typedef struct { | 
 | 3384 |     PyThread_type_lock start_event; | 
 | 3385 |     PyThread_type_lock exit_event; | 
 | 3386 |     PyObject *callback; | 
 | 3387 | } test_c_thread_t; | 
 | 3388 |  | 
 | 3389 | static void | 
 | 3390 | temporary_c_thread(void *data) | 
 | 3391 | { | 
 | 3392 |     test_c_thread_t *test_c_thread = data; | 
 | 3393 |     PyGILState_STATE state; | 
 | 3394 |     PyObject *res; | 
 | 3395 |  | 
 | 3396 |     PyThread_release_lock(test_c_thread->start_event); | 
 | 3397 |  | 
 | 3398 |     /* Allocate a Python thread state for this thread */ | 
 | 3399 |     state = PyGILState_Ensure(); | 
 | 3400 |  | 
| Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 3401 |     res = _PyObject_CallNoArg(test_c_thread->callback); | 
| Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3402 |     Py_CLEAR(test_c_thread->callback); | 
 | 3403 |  | 
 | 3404 |     if (res == NULL) { | 
 | 3405 |         PyErr_Print(); | 
 | 3406 |     } | 
 | 3407 |     else { | 
 | 3408 |         Py_DECREF(res); | 
 | 3409 |     } | 
 | 3410 |  | 
 | 3411 |     /* Destroy the Python thread state for this thread */ | 
 | 3412 |     PyGILState_Release(state); | 
 | 3413 |  | 
 | 3414 |     PyThread_release_lock(test_c_thread->exit_event); | 
 | 3415 |  | 
 | 3416 |     PyThread_exit_thread(); | 
 | 3417 | } | 
 | 3418 |  | 
 | 3419 | static PyObject * | 
 | 3420 | call_in_temporary_c_thread(PyObject *self, PyObject *callback) | 
 | 3421 | { | 
 | 3422 |     PyObject *res = NULL; | 
 | 3423 |     test_c_thread_t test_c_thread; | 
 | 3424 |     long thread; | 
 | 3425 |  | 
 | 3426 |     PyEval_InitThreads(); | 
 | 3427 |  | 
 | 3428 |     test_c_thread.start_event = PyThread_allocate_lock(); | 
 | 3429 |     test_c_thread.exit_event = PyThread_allocate_lock(); | 
 | 3430 |     test_c_thread.callback = NULL; | 
 | 3431 |     if (!test_c_thread.start_event || !test_c_thread.exit_event) { | 
 | 3432 |         PyErr_SetString(PyExc_RuntimeError, "could not allocate lock"); | 
 | 3433 |         goto exit; | 
 | 3434 |     } | 
 | 3435 |  | 
 | 3436 |     Py_INCREF(callback); | 
 | 3437 |     test_c_thread.callback = callback; | 
 | 3438 |  | 
 | 3439 |     PyThread_acquire_lock(test_c_thread.start_event, 1); | 
 | 3440 |     PyThread_acquire_lock(test_c_thread.exit_event, 1); | 
 | 3441 |  | 
 | 3442 |     thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread); | 
 | 3443 |     if (thread == -1) { | 
 | 3444 |         PyErr_SetString(PyExc_RuntimeError, "unable to start the thread"); | 
 | 3445 |         PyThread_release_lock(test_c_thread.start_event); | 
 | 3446 |         PyThread_release_lock(test_c_thread.exit_event); | 
 | 3447 |         goto exit; | 
 | 3448 |     } | 
 | 3449 |  | 
 | 3450 |     PyThread_acquire_lock(test_c_thread.start_event, 1); | 
 | 3451 |     PyThread_release_lock(test_c_thread.start_event); | 
 | 3452 |  | 
 | 3453 |     Py_BEGIN_ALLOW_THREADS | 
 | 3454 |         PyThread_acquire_lock(test_c_thread.exit_event, 1); | 
 | 3455 |         PyThread_release_lock(test_c_thread.exit_event); | 
 | 3456 |     Py_END_ALLOW_THREADS | 
 | 3457 |  | 
 | 3458 |     Py_INCREF(Py_None); | 
 | 3459 |     res = Py_None; | 
 | 3460 |  | 
 | 3461 | exit: | 
 | 3462 |     Py_CLEAR(test_c_thread.callback); | 
 | 3463 |     if (test_c_thread.start_event) | 
 | 3464 |         PyThread_free_lock(test_c_thread.start_event); | 
 | 3465 |     if (test_c_thread.exit_event) | 
 | 3466 |         PyThread_free_lock(test_c_thread.exit_event); | 
 | 3467 |     return res; | 
 | 3468 | } | 
| Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 3469 | #endif   /* WITH_THREAD */ | 
| Victor Stinner | 1310510 | 2013-12-13 02:17:29 +0100 | [diff] [blame] | 3470 |  | 
| Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 3471 | static PyObject* | 
 | 3472 | test_raise_signal(PyObject* self, PyObject *args) | 
 | 3473 | { | 
 | 3474 |     int signum, err; | 
 | 3475 |  | 
 | 3476 |     if (PyArg_ParseTuple(args, "i:raise_signal", &signum) < 0) | 
 | 3477 |         return NULL; | 
 | 3478 |  | 
 | 3479 |     err = raise(signum); | 
 | 3480 |     if (err) | 
 | 3481 |         return PyErr_SetFromErrno(PyExc_OSError); | 
 | 3482 |  | 
 | 3483 |     if (PyErr_CheckSignals() < 0) | 
 | 3484 |         return NULL; | 
 | 3485 |  | 
 | 3486 |     Py_RETURN_NONE; | 
 | 3487 | } | 
 | 3488 |  | 
| Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 3489 | /* marshal */ | 
 | 3490 |  | 
 | 3491 | static PyObject* | 
 | 3492 | pymarshal_write_long_to_file(PyObject* self, PyObject *args) | 
 | 3493 | { | 
 | 3494 |     long value; | 
 | 3495 |     char *filename; | 
 | 3496 |     int version; | 
 | 3497 |     FILE *fp; | 
 | 3498 |  | 
 | 3499 |     if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file", | 
 | 3500 |                           &value, &filename, &version)) | 
 | 3501 |         return NULL; | 
 | 3502 |  | 
 | 3503 |     fp = fopen(filename, "wb"); | 
 | 3504 |     if (fp == NULL) { | 
 | 3505 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 3506 |         return NULL; | 
 | 3507 |     } | 
 | 3508 |  | 
 | 3509 |     PyMarshal_WriteLongToFile(value, fp, version); | 
 | 3510 |  | 
 | 3511 |     fclose(fp); | 
 | 3512 |     if (PyErr_Occurred()) | 
 | 3513 |         return NULL; | 
 | 3514 |     Py_RETURN_NONE; | 
 | 3515 | } | 
 | 3516 |  | 
 | 3517 | static PyObject* | 
 | 3518 | pymarshal_write_object_to_file(PyObject* self, PyObject *args) | 
 | 3519 | { | 
 | 3520 |     PyObject *obj; | 
 | 3521 |     char *filename; | 
 | 3522 |     int version; | 
 | 3523 |     FILE *fp; | 
 | 3524 |  | 
 | 3525 |     if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file", | 
 | 3526 |                           &obj, &filename, &version)) | 
 | 3527 |         return NULL; | 
 | 3528 |  | 
 | 3529 |     fp = fopen(filename, "wb"); | 
 | 3530 |     if (fp == NULL) { | 
 | 3531 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 3532 |         return NULL; | 
 | 3533 |     } | 
 | 3534 |  | 
 | 3535 |     PyMarshal_WriteObjectToFile(obj, fp, version); | 
 | 3536 |  | 
 | 3537 |     fclose(fp); | 
 | 3538 |     if (PyErr_Occurred()) | 
 | 3539 |         return NULL; | 
 | 3540 |     Py_RETURN_NONE; | 
 | 3541 | } | 
 | 3542 |  | 
 | 3543 | static PyObject* | 
 | 3544 | pymarshal_read_short_from_file(PyObject* self, PyObject *args) | 
 | 3545 | { | 
 | 3546 |     int value; | 
 | 3547 |     long pos; | 
 | 3548 |     char *filename; | 
 | 3549 |     FILE *fp; | 
 | 3550 |  | 
 | 3551 |     if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename)) | 
 | 3552 |         return NULL; | 
 | 3553 |  | 
 | 3554 |     fp = fopen(filename, "rb"); | 
 | 3555 |     if (fp == NULL) { | 
 | 3556 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 3557 |         return NULL; | 
 | 3558 |     } | 
 | 3559 |  | 
 | 3560 |     value = PyMarshal_ReadShortFromFile(fp); | 
 | 3561 |     pos = ftell(fp); | 
 | 3562 |  | 
 | 3563 |     fclose(fp); | 
 | 3564 |     if (PyErr_Occurred()) | 
 | 3565 |         return NULL; | 
 | 3566 |     return Py_BuildValue("il", value, pos); | 
 | 3567 | } | 
 | 3568 |  | 
 | 3569 | static PyObject* | 
 | 3570 | pymarshal_read_long_from_file(PyObject* self, PyObject *args) | 
 | 3571 | { | 
 | 3572 |     long value, pos; | 
 | 3573 |     char *filename; | 
 | 3574 |     FILE *fp; | 
 | 3575 |  | 
 | 3576 |     if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename)) | 
 | 3577 |         return NULL; | 
 | 3578 |  | 
 | 3579 |     fp = fopen(filename, "rb"); | 
 | 3580 |     if (fp == NULL) { | 
 | 3581 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 3582 |         return NULL; | 
 | 3583 |     } | 
 | 3584 |  | 
 | 3585 |     value = PyMarshal_ReadLongFromFile(fp); | 
 | 3586 |     pos = ftell(fp); | 
 | 3587 |  | 
 | 3588 |     fclose(fp); | 
 | 3589 |     if (PyErr_Occurred()) | 
 | 3590 |         return NULL; | 
 | 3591 |     return Py_BuildValue("ll", value, pos); | 
 | 3592 | } | 
 | 3593 |  | 
 | 3594 | static PyObject* | 
 | 3595 | pymarshal_read_last_object_from_file(PyObject* self, PyObject *args) | 
 | 3596 | { | 
 | 3597 |     PyObject *obj; | 
 | 3598 |     long pos; | 
 | 3599 |     char *filename; | 
 | 3600 |     FILE *fp; | 
 | 3601 |  | 
 | 3602 |     if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename)) | 
 | 3603 |         return NULL; | 
 | 3604 |  | 
 | 3605 |     fp = fopen(filename, "rb"); | 
 | 3606 |     if (fp == NULL) { | 
 | 3607 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 3608 |         return NULL; | 
 | 3609 |     } | 
 | 3610 |  | 
 | 3611 |     obj = PyMarshal_ReadLastObjectFromFile(fp); | 
 | 3612 |     pos = ftell(fp); | 
 | 3613 |  | 
 | 3614 |     fclose(fp); | 
 | 3615 |     return Py_BuildValue("Nl", obj, pos); | 
 | 3616 | } | 
 | 3617 |  | 
 | 3618 | static PyObject* | 
 | 3619 | pymarshal_read_object_from_file(PyObject* self, PyObject *args) | 
 | 3620 | { | 
 | 3621 |     PyObject *obj; | 
 | 3622 |     long pos; | 
 | 3623 |     char *filename; | 
 | 3624 |     FILE *fp; | 
 | 3625 |  | 
 | 3626 |     if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename)) | 
 | 3627 |         return NULL; | 
 | 3628 |  | 
 | 3629 |     fp = fopen(filename, "rb"); | 
 | 3630 |     if (fp == NULL) { | 
 | 3631 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 3632 |         return NULL; | 
 | 3633 |     } | 
 | 3634 |  | 
 | 3635 |     obj = PyMarshal_ReadObjectFromFile(fp); | 
 | 3636 |     pos = ftell(fp); | 
 | 3637 |  | 
 | 3638 |     fclose(fp); | 
 | 3639 |     return Py_BuildValue("Nl", obj, pos); | 
 | 3640 | } | 
 | 3641 |  | 
| Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 3642 | static PyObject* | 
 | 3643 | return_null_without_error(PyObject *self, PyObject *args) | 
 | 3644 | { | 
 | 3645 |     /* invalid call: return NULL without setting an error, | 
 | 3646 |      * _Py_CheckFunctionResult() must detect such bug at runtime. */ | 
 | 3647 |     PyErr_Clear(); | 
 | 3648 |     return NULL; | 
 | 3649 | } | 
 | 3650 |  | 
 | 3651 | static PyObject* | 
 | 3652 | return_result_with_error(PyObject *self, PyObject *args) | 
 | 3653 | { | 
 | 3654 |     /* invalid call: return a result with an error set, | 
 | 3655 |      * _Py_CheckFunctionResult() must detect such bug at runtime. */ | 
 | 3656 |     PyErr_SetNone(PyExc_ValueError); | 
 | 3657 |     Py_RETURN_NONE; | 
 | 3658 | } | 
 | 3659 |  | 
| Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 3660 | static PyObject * | 
| Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 3661 | test_pytime_fromseconds(PyObject *self, PyObject *args) | 
 | 3662 | { | 
 | 3663 |     int seconds; | 
 | 3664 |     _PyTime_t ts; | 
 | 3665 |  | 
 | 3666 |     if (!PyArg_ParseTuple(args, "i", &seconds)) | 
 | 3667 |         return NULL; | 
 | 3668 |     ts = _PyTime_FromSeconds(seconds); | 
 | 3669 |     return _PyTime_AsNanosecondsObject(ts); | 
 | 3670 | } | 
 | 3671 |  | 
 | 3672 | static PyObject * | 
| Victor Stinner | 992c43f | 2015-03-27 17:12:45 +0100 | [diff] [blame] | 3673 | test_pytime_fromsecondsobject(PyObject *self, PyObject *args) | 
 | 3674 | { | 
 | 3675 |     PyObject *obj; | 
 | 3676 |     int round; | 
 | 3677 |     _PyTime_t ts; | 
 | 3678 |  | 
 | 3679 |     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) | 
 | 3680 |         return NULL; | 
 | 3681 |     if (check_time_rounding(round) < 0) | 
 | 3682 |         return NULL; | 
 | 3683 |     if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) | 
 | 3684 |         return NULL; | 
 | 3685 |     return _PyTime_AsNanosecondsObject(ts); | 
 | 3686 | } | 
 | 3687 |  | 
| Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 3688 | static PyObject * | 
 | 3689 | test_pytime_assecondsdouble(PyObject *self, PyObject *args) | 
 | 3690 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3691 |     long long ns; | 
| Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 3692 |     _PyTime_t ts; | 
 | 3693 |     double d; | 
 | 3694 |  | 
 | 3695 |     if (!PyArg_ParseTuple(args, "L", &ns)) | 
 | 3696 |         return NULL; | 
 | 3697 |     ts = _PyTime_FromNanoseconds(ns); | 
 | 3698 |     d = _PyTime_AsSecondsDouble(ts); | 
 | 3699 |     return PyFloat_FromDouble(d); | 
 | 3700 | } | 
 | 3701 |  | 
| Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3702 | static PyObject * | 
 | 3703 | test_PyTime_AsTimeval(PyObject *self, PyObject *args) | 
 | 3704 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3705 |     long long ns; | 
| Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3706 |     int round; | 
 | 3707 |     _PyTime_t t; | 
 | 3708 |     struct timeval tv; | 
 | 3709 |     PyObject *seconds; | 
 | 3710 |  | 
 | 3711 |     if (!PyArg_ParseTuple(args, "Li", &ns, &round)) | 
 | 3712 |         return NULL; | 
 | 3713 |     if (check_time_rounding(round) < 0) | 
 | 3714 |         return NULL; | 
 | 3715 |     t = _PyTime_FromNanoseconds(ns); | 
| Victor Stinner | ea9c0dd | 2015-03-30 02:51:13 +0200 | [diff] [blame] | 3716 |     if (_PyTime_AsTimeval(t, &tv, round) < 0) | 
| Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3717 |         return NULL; | 
| Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3718 |  | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3719 |     seconds = PyLong_FromLong((long long)tv.tv_sec); | 
| Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 3720 |     if (seconds == NULL) | 
 | 3721 |         return NULL; | 
 | 3722 |     return Py_BuildValue("Nl", seconds, tv.tv_usec); | 
 | 3723 | } | 
 | 3724 |  | 
| Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 3725 | #ifdef HAVE_CLOCK_GETTIME | 
 | 3726 | static PyObject * | 
 | 3727 | test_PyTime_AsTimespec(PyObject *self, PyObject *args) | 
 | 3728 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3729 |     long long ns; | 
| Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 3730 |     _PyTime_t t; | 
 | 3731 |     struct timespec ts; | 
 | 3732 |  | 
 | 3733 |     if (!PyArg_ParseTuple(args, "L", &ns)) | 
 | 3734 |         return NULL; | 
 | 3735 |     t = _PyTime_FromNanoseconds(ns); | 
 | 3736 |     if (_PyTime_AsTimespec(t, &ts) == -1) | 
 | 3737 |         return NULL; | 
 | 3738 |     return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); | 
 | 3739 | } | 
 | 3740 | #endif | 
 | 3741 |  | 
| Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 3742 | static PyObject * | 
 | 3743 | test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) | 
 | 3744 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3745 |     long long ns; | 
| Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 3746 |     int round; | 
 | 3747 |     _PyTime_t t, ms; | 
 | 3748 |  | 
 | 3749 |     if (!PyArg_ParseTuple(args, "Li", &ns, &round)) | 
 | 3750 |         return NULL; | 
 | 3751 |     if (check_time_rounding(round) < 0) | 
 | 3752 |         return NULL; | 
 | 3753 |     t = _PyTime_FromNanoseconds(ns); | 
 | 3754 |     ms = _PyTime_AsMilliseconds(t, round); | 
 | 3755 |     /* This conversion rely on the fact that _PyTime_t is a number of | 
 | 3756 |        nanoseconds */ | 
 | 3757 |     return _PyTime_AsNanosecondsObject(ms); | 
 | 3758 | } | 
 | 3759 |  | 
 | 3760 | static PyObject * | 
 | 3761 | test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) | 
 | 3762 | { | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 3763 |     long long ns; | 
| Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 3764 |     int round; | 
 | 3765 |     _PyTime_t t, ms; | 
 | 3766 |  | 
 | 3767 |     if (!PyArg_ParseTuple(args, "Li", &ns, &round)) | 
 | 3768 |         return NULL; | 
 | 3769 |     if (check_time_rounding(round) < 0) | 
 | 3770 |         return NULL; | 
 | 3771 |     t = _PyTime_FromNanoseconds(ns); | 
 | 3772 |     ms = _PyTime_AsMicroseconds(t, round); | 
 | 3773 |     /* This conversion rely on the fact that _PyTime_t is a number of | 
 | 3774 |        nanoseconds */ | 
 | 3775 |     return _PyTime_AsNanosecondsObject(ms); | 
 | 3776 | } | 
 | 3777 |  | 
| Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 3778 | static PyObject* | 
 | 3779 | get_recursion_depth(PyObject *self, PyObject *args) | 
 | 3780 | { | 
 | 3781 |     PyThreadState *tstate = PyThreadState_GET(); | 
 | 3782 |  | 
| Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 3783 |     /* subtract one to ignore the frame of the get_recursion_depth() call */ | 
| Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 3784 |     return PyLong_FromLong(tstate->recursion_depth - 1); | 
 | 3785 | } | 
 | 3786 |  | 
| Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 3787 | static PyObject* | 
 | 3788 | pymem_buffer_overflow(PyObject *self, PyObject *args) | 
 | 3789 | { | 
 | 3790 |     char *buffer; | 
 | 3791 |  | 
 | 3792 |     /* Deliberate buffer overflow to check that PyMem_Free() detects | 
 | 3793 |        the overflow when debug hooks are installed. */ | 
 | 3794 |     buffer = PyMem_Malloc(16); | 
 | 3795 |     buffer[16] = 'x'; | 
 | 3796 |     PyMem_Free(buffer); | 
 | 3797 |  | 
 | 3798 |     Py_RETURN_NONE; | 
 | 3799 | } | 
 | 3800 |  | 
 | 3801 | static PyObject* | 
 | 3802 | pymem_api_misuse(PyObject *self, PyObject *args) | 
 | 3803 | { | 
 | 3804 |     char *buffer; | 
 | 3805 |  | 
 | 3806 |     /* Deliberate misusage of Python allocators: | 
 | 3807 |        allococate with PyMem but release with PyMem_Raw. */ | 
 | 3808 |     buffer = PyMem_Malloc(16); | 
 | 3809 |     PyMem_RawFree(buffer); | 
 | 3810 |  | 
 | 3811 |     Py_RETURN_NONE; | 
 | 3812 | } | 
 | 3813 |  | 
| Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 3814 | static PyObject* | 
| Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 3815 | pymem_malloc_without_gil(PyObject *self, PyObject *args) | 
 | 3816 | { | 
 | 3817 |     char *buffer; | 
 | 3818 |  | 
 | 3819 |     /* Deliberate bug to test debug hooks on Python memory allocators: | 
 | 3820 |        call PyMem_Malloc() without holding the GIL */ | 
 | 3821 |     Py_BEGIN_ALLOW_THREADS | 
 | 3822 |     buffer = PyMem_Malloc(10); | 
 | 3823 |     Py_END_ALLOW_THREADS | 
 | 3824 |  | 
 | 3825 |     PyMem_Free(buffer); | 
 | 3826 |  | 
 | 3827 |     Py_RETURN_NONE; | 
 | 3828 | } | 
 | 3829 |  | 
 | 3830 | static PyObject* | 
| Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 3831 | pyobject_malloc_without_gil(PyObject *self, PyObject *args) | 
 | 3832 | { | 
 | 3833 |     char *buffer; | 
 | 3834 |  | 
| Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 3835 |     /* Deliberate bug to test debug hooks on Python memory allocators: | 
 | 3836 |        call PyObject_Malloc() without holding the GIL */ | 
| Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 3837 |     Py_BEGIN_ALLOW_THREADS | 
 | 3838 |     buffer = PyObject_Malloc(10); | 
 | 3839 |     Py_END_ALLOW_THREADS | 
 | 3840 |  | 
 | 3841 |     PyObject_Free(buffer); | 
 | 3842 |  | 
 | 3843 |     Py_RETURN_NONE; | 
 | 3844 | } | 
 | 3845 |  | 
| Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 3846 | static PyObject * | 
 | 3847 | tracemalloc_track(PyObject *self, PyObject *args) | 
 | 3848 | { | 
 | 3849 |     unsigned int domain; | 
 | 3850 |     PyObject *ptr_obj; | 
 | 3851 |     void *ptr; | 
 | 3852 |     Py_ssize_t size; | 
 | 3853 |     int release_gil = 0; | 
 | 3854 |     int res; | 
 | 3855 |  | 
 | 3856 |     if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil)) | 
 | 3857 |         return NULL; | 
 | 3858 |     ptr = PyLong_AsVoidPtr(ptr_obj); | 
 | 3859 |     if (PyErr_Occurred()) | 
 | 3860 |         return NULL; | 
 | 3861 |  | 
 | 3862 |     if (release_gil) { | 
 | 3863 |         Py_BEGIN_ALLOW_THREADS | 
 | 3864 |         res = _PyTraceMalloc_Track(domain, (Py_uintptr_t)ptr, size); | 
 | 3865 |         Py_END_ALLOW_THREADS | 
 | 3866 |     } | 
 | 3867 |     else { | 
 | 3868 |         res = _PyTraceMalloc_Track(domain, (Py_uintptr_t)ptr, size); | 
 | 3869 |     } | 
 | 3870 |  | 
 | 3871 |     if (res < 0) { | 
 | 3872 |         PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error"); | 
 | 3873 |         return NULL; | 
 | 3874 |     } | 
 | 3875 |  | 
 | 3876 |     Py_RETURN_NONE; | 
 | 3877 | } | 
 | 3878 |  | 
 | 3879 | static PyObject * | 
 | 3880 | tracemalloc_untrack(PyObject *self, PyObject *args) | 
 | 3881 | { | 
 | 3882 |     unsigned int domain; | 
 | 3883 |     PyObject *ptr_obj; | 
 | 3884 |     void *ptr; | 
 | 3885 |     int res; | 
 | 3886 |  | 
 | 3887 |     if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) | 
 | 3888 |         return NULL; | 
 | 3889 |     ptr = PyLong_AsVoidPtr(ptr_obj); | 
 | 3890 |     if (PyErr_Occurred()) | 
 | 3891 |         return NULL; | 
 | 3892 |  | 
 | 3893 |     res = _PyTraceMalloc_Untrack(domain, (Py_uintptr_t)ptr); | 
 | 3894 |     if (res < 0) { | 
 | 3895 |         PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error"); | 
 | 3896 |         return NULL; | 
 | 3897 |     } | 
 | 3898 |  | 
 | 3899 |     Py_RETURN_NONE; | 
 | 3900 | } | 
 | 3901 |  | 
 | 3902 | static PyObject * | 
 | 3903 | tracemalloc_get_traceback(PyObject *self, PyObject *args) | 
 | 3904 | { | 
 | 3905 |     unsigned int domain; | 
 | 3906 |     PyObject *ptr_obj; | 
 | 3907 |     void *ptr; | 
 | 3908 |  | 
 | 3909 |     if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) | 
 | 3910 |         return NULL; | 
 | 3911 |     ptr = PyLong_AsVoidPtr(ptr_obj); | 
 | 3912 |     if (PyErr_Occurred()) | 
 | 3913 |         return NULL; | 
 | 3914 |  | 
 | 3915 |     return _PyTraceMalloc_GetTraceback(domain, (Py_uintptr_t)ptr); | 
 | 3916 | } | 
 | 3917 |  | 
| Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 3918 |  | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 3919 | static PyMethodDef TestMethods[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3920 |     {"raise_exception",         raise_exception,                 METH_VARARGS}, | 
 | 3921 |     {"raise_memoryerror",   (PyCFunction)raise_memoryerror,  METH_NOARGS}, | 
| Antoine Pitrou | 6bc217d | 2015-06-23 14:31:11 +0200 | [diff] [blame] | 3922 |     {"set_errno",               set_errno,                       METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3923 |     {"test_config",             (PyCFunction)test_config,        METH_NOARGS}, | 
| Victor Stinner | 0107655 | 2013-10-29 19:39:52 +0100 | [diff] [blame] | 3924 |     {"test_sizeof_c_types",     (PyCFunction)test_sizeof_c_types, METH_NOARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3925 |     {"test_datetime_capi",  test_datetime_capi,              METH_NOARGS}, | 
 | 3926 |     {"test_list_api",           (PyCFunction)test_list_api,      METH_NOARGS}, | 
 | 3927 |     {"test_dict_iteration",     (PyCFunction)test_dict_iteration,METH_NOARGS}, | 
 | 3928 |     {"test_lazy_hash_inheritance",      (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3929 |     {"test_long_api",           (PyCFunction)test_long_api,      METH_NOARGS}, | 
| Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 3930 |     {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak,      METH_NOARGS}, | 
 | 3931 |     {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak,      METH_NOARGS}, | 
 | 3932 |     {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak,      METH_NOARGS}, | 
 | 3933 |     {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak,      METH_NOARGS}, | 
| Christian Heimes | 4efdb41 | 2013-07-31 02:36:43 +0200 | [diff] [blame] | 3934 |     {"test_incref_decref_API",  (PyCFunction)test_incref_decref_API,       METH_NOARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3935 |     {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, | 
 | 3936 |      METH_NOARGS}, | 
| Nadeem Vawda | 3d5881e | 2011-09-07 21:40:26 +0200 | [diff] [blame] | 3937 |     {"test_long_as_double",     (PyCFunction)test_long_as_double,METH_NOARGS}, | 
 | 3938 |     {"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] | 3939 |     {"test_long_numbits",       (PyCFunction)test_long_numbits,  METH_NOARGS}, | 
 | 3940 |     {"test_k_code",             (PyCFunction)test_k_code,        METH_NOARGS}, | 
 | 3941 |     {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS}, | 
| Larry Hastings | 8f904da | 2012-06-22 03:56:29 -0700 | [diff] [blame] | 3942 |     {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3943 |     {"test_null_strings",       (PyCFunction)test_null_strings,  METH_NOARGS}, | 
 | 3944 |     {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS}, | 
 | 3945 |     {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 3946 |      PyDoc_STR("This is a pretty normal docstring.")}, | 
 | 3947 |     {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS}, | 
 | 3948 |     {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS}, | 
 | 3949 |     {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS}, | 
| Stefan Krah | 7213fcc | 2015-02-01 16:19:23 +0100 | [diff] [blame] | 3950 |     {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS}, | 
| Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3951 | #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__) | 
| Stefan Krah | 5178d91 | 2015-02-03 16:57:21 +0100 | [diff] [blame] | 3952 |     {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS}, | 
| Stefan Krah | a7559c0 | 2015-02-03 22:27:21 +0100 | [diff] [blame] | 3953 | #endif | 
| Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 3954 |     {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O}, | 
| Serhiy Storchaka | 13e602e | 2016-05-20 22:31:14 +0300 | [diff] [blame] | 3955 |     {"test_buildvalue_N",       test_buildvalue_N,               METH_NOARGS}, | 
| Serhiy Storchaka | ce41287 | 2016-05-08 23:36:44 +0300 | [diff] [blame] | 3956 |     {"get_args", get_args, METH_VARARGS}, | 
 | 3957 |     {"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 |     {"getargs_tuple",           getargs_tuple,                   METH_VARARGS}, | 
 | 3959 |     {"getargs_keywords", (PyCFunction)getargs_keywords, | 
 | 3960 |       METH_VARARGS|METH_KEYWORDS}, | 
| Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 3961 |     {"getargs_keyword_only", (PyCFunction)getargs_keyword_only, | 
 | 3962 |       METH_VARARGS|METH_KEYWORDS}, | 
| Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 3963 |     {"getargs_positional_only_and_keywords", | 
 | 3964 |       (PyCFunction)getargs_positional_only_and_keywords, | 
 | 3965 |       METH_VARARGS|METH_KEYWORDS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3966 |     {"getargs_b",               getargs_b,                       METH_VARARGS}, | 
 | 3967 |     {"getargs_B",               getargs_B,                       METH_VARARGS}, | 
 | 3968 |     {"getargs_h",               getargs_h,                       METH_VARARGS}, | 
 | 3969 |     {"getargs_H",               getargs_H,                       METH_VARARGS}, | 
 | 3970 |     {"getargs_I",               getargs_I,                       METH_VARARGS}, | 
 | 3971 |     {"getargs_k",               getargs_k,                       METH_VARARGS}, | 
 | 3972 |     {"getargs_i",               getargs_i,                       METH_VARARGS}, | 
 | 3973 |     {"getargs_l",               getargs_l,                       METH_VARARGS}, | 
 | 3974 |     {"getargs_n",               getargs_n,                       METH_VARARGS}, | 
| Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 3975 |     {"getargs_p",               getargs_p,                       METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3976 |     {"getargs_L",               getargs_L,                       METH_VARARGS}, | 
 | 3977 |     {"getargs_K",               getargs_K,                       METH_VARARGS}, | 
 | 3978 |     {"test_longlong_api",       test_longlong_api,               METH_NOARGS}, | 
 | 3979 |     {"test_long_long_and_overflow", | 
 | 3980 |         (PyCFunction)test_long_long_and_overflow, METH_NOARGS}, | 
 | 3981 |     {"test_L_code",             (PyCFunction)test_L_code,        METH_NOARGS}, | 
| Serhiy Storchaka | f95455d | 2016-05-16 10:11:47 +0300 | [diff] [blame] | 3982 |     {"getargs_f",               getargs_f,                       METH_VARARGS}, | 
 | 3983 |     {"getargs_d",               getargs_d,                       METH_VARARGS}, | 
 | 3984 |     {"getargs_D",               getargs_D,                       METH_VARARGS}, | 
 | 3985 |     {"getargs_S",               getargs_S,                       METH_VARARGS}, | 
 | 3986 |     {"getargs_Y",               getargs_Y,                       METH_VARARGS}, | 
 | 3987 |     {"getargs_U",               getargs_U,                       METH_VARARGS}, | 
| Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 3988 |     {"getargs_c",               getargs_c,                       METH_VARARGS}, | 
| Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 3989 |     {"getargs_C",               getargs_C,                       METH_VARARGS}, | 
| Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 3990 |     {"getargs_s",               getargs_s,                       METH_VARARGS}, | 
 | 3991 |     {"getargs_s_star",          getargs_s_star,                  METH_VARARGS}, | 
 | 3992 |     {"getargs_s_hash",          getargs_s_hash,                  METH_VARARGS}, | 
 | 3993 |     {"getargs_z",               getargs_z,                       METH_VARARGS}, | 
 | 3994 |     {"getargs_z_star",          getargs_z_star,                  METH_VARARGS}, | 
 | 3995 |     {"getargs_z_hash",          getargs_z_hash,                  METH_VARARGS}, | 
 | 3996 |     {"getargs_y",               getargs_y,                       METH_VARARGS}, | 
 | 3997 |     {"getargs_y_star",          getargs_y_star,                  METH_VARARGS}, | 
 | 3998 |     {"getargs_y_hash",          getargs_y_hash,                  METH_VARARGS}, | 
 | 3999 |     {"getargs_u",               getargs_u,                       METH_VARARGS}, | 
 | 4000 |     {"getargs_u_hash",          getargs_u_hash,                  METH_VARARGS}, | 
 | 4001 |     {"getargs_Z",               getargs_Z,                       METH_VARARGS}, | 
 | 4002 |     {"getargs_Z_hash",          getargs_Z_hash,                  METH_VARARGS}, | 
| Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 4003 |     {"getargs_w_star",          getargs_w_star,                  METH_VARARGS}, | 
| Serhiy Storchaka | c8241fd | 2016-01-28 19:49:54 +0200 | [diff] [blame] | 4004 |     {"getargs_es",              getargs_es,                      METH_VARARGS}, | 
 | 4005 |     {"getargs_et",              getargs_et,                      METH_VARARGS}, | 
 | 4006 |     {"getargs_es_hash",         getargs_es_hash,                 METH_VARARGS}, | 
 | 4007 |     {"getargs_et_hash",         getargs_et_hash,                 METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4008 |     {"codec_incrementalencoder", | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4009 |      (PyCFunction)codec_incrementalencoder,                      METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4010 |     {"codec_incrementaldecoder", | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4011 |      (PyCFunction)codec_incrementaldecoder,                      METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4012 |     {"test_s_code",             (PyCFunction)test_s_code,        METH_NOARGS}, | 
 | 4013 |     {"test_u_code",             (PyCFunction)test_u_code,        METH_NOARGS}, | 
 | 4014 |     {"test_Z_code",             (PyCFunction)test_Z_code,        METH_NOARGS}, | 
 | 4015 |     {"test_widechar",           (PyCFunction)test_widechar,      METH_NOARGS}, | 
| Victor Stinner | 42bf775 | 2011-11-21 22:52:58 +0100 | [diff] [blame] | 4016 |     {"unicode_aswidechar",      unicode_aswidechar,              METH_VARARGS}, | 
 | 4017 |     {"unicode_aswidecharstring",unicode_aswidecharstring,        METH_VARARGS}, | 
 | 4018 |     {"unicode_encodedecimal",   unicode_encodedecimal,           METH_VARARGS}, | 
 | 4019 |     {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS}, | 
| Stefan Krah | e6996ed | 2012-11-02 14:44:20 +0100 | [diff] [blame] | 4020 |     {"unicode_legacy_string",   unicode_legacy_string,           METH_VARARGS}, | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4021 | #ifdef WITH_THREAD | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4022 |     {"_test_thread_state",      test_thread_state,               METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4023 |     {"_pending_threadfunc",     pending_threadfunc,              METH_VARARGS}, | 
| Mark Hammond | 8d98d2c | 2003-04-19 15:41:53 +0000 | [diff] [blame] | 4024 | #endif | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4025 | #ifdef HAVE_GETTIMEOFDAY | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4026 |     {"profile_int",             profile_int,                     METH_NOARGS}, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4027 | #endif | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4028 |     {"traceback_print",         traceback_print,                 METH_VARARGS}, | 
 | 4029 |     {"exception_print",         exception_print,                 METH_VARARGS}, | 
| Martin v. Löwis | aa2efcb | 2012-04-19 14:33:43 +0200 | [diff] [blame] | 4030 |     {"set_exc_info",            test_set_exc_info,               METH_VARARGS}, | 
| Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 4031 |     {"argparsing",              argparsing,                      METH_VARARGS}, | 
 | 4032 |     {"code_newempty",           code_newempty,                   METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4033 |     {"make_exception_with_doc", (PyCFunction)make_exception_with_doc, | 
 | 4034 |      METH_VARARGS | METH_KEYWORDS}, | 
| Antoine Pitrou | 5bffa79 | 2011-02-24 20:50:49 +0000 | [diff] [blame] | 4035 |     {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer, | 
 | 4036 |      METH_NOARGS}, | 
| Jeffrey Yasskin | 8e0bdfd | 2010-05-13 18:31:05 +0000 | [diff] [blame] | 4037 |     {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS}, | 
| Antoine Pitrou | 2f828f2 | 2012-01-18 00:21:11 +0100 | [diff] [blame] | 4038 |     {"run_in_subinterp",        run_in_subinterp,                METH_VARARGS}, | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 4039 |     {"pytime_object_to_time_t", test_pytime_object_to_time_t,  METH_VARARGS}, | 
 | 4040 |     {"pytime_object_to_timeval", test_pytime_object_to_timeval,  METH_VARARGS}, | 
| Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 4041 |     {"pytime_object_to_timespec", test_pytime_object_to_timespec,  METH_VARARGS}, | 
| Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 4042 |     {"with_tp_del",             with_tp_del,                     METH_VARARGS}, | 
| Antoine Pitrou | b349e4c | 2014-08-06 19:31:40 -0400 | [diff] [blame] | 4043 |     {"create_cfunction",        create_cfunction,                METH_NOARGS}, | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4044 |     {"test_pymem_alloc0", | 
| Victor Stinner | 797bcb5 | 2014-06-02 21:29:07 +0200 | [diff] [blame] | 4045 |      (PyCFunction)test_pymem_alloc0, METH_NOARGS}, | 
 | 4046 |     {"test_pymem_setrawallocators", | 
| Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 4047 |      (PyCFunction)test_pymem_setrawallocators, METH_NOARGS}, | 
 | 4048 |     {"test_pymem_setallocators", | 
 | 4049 |      (PyCFunction)test_pymem_setallocators, METH_NOARGS}, | 
 | 4050 |     {"test_pyobject_setallocators", | 
 | 4051 |      (PyCFunction)test_pyobject_setallocators, METH_NOARGS}, | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4052 |     {"no_docstring", | 
 | 4053 |         (PyCFunction)test_with_docstring, METH_NOARGS}, | 
 | 4054 |     {"docstring_empty", | 
 | 4055 |         (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 4056 |         docstring_empty}, | 
 | 4057 |     {"docstring_no_signature", | 
 | 4058 |         (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 4059 |         docstring_no_signature}, | 
 | 4060 |     {"docstring_with_invalid_signature", | 
 | 4061 |         (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 4062 |         docstring_with_invalid_signature}, | 
| Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 4063 |     {"docstring_with_invalid_signature2", | 
 | 4064 |         (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 4065 |         docstring_with_invalid_signature2}, | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4066 |     {"docstring_with_signature", | 
 | 4067 |         (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 4068 |         docstring_with_signature}, | 
| Zachary Ware | 8ef887c | 2015-04-13 18:22:35 -0500 | [diff] [blame] | 4069 |     {"docstring_with_signature_but_no_doc", | 
 | 4070 |         (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 4071 |         docstring_with_signature_but_no_doc}, | 
| Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 4072 |     {"docstring_with_signature_and_extra_newlines", | 
 | 4073 |         (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 4074 |         docstring_with_signature_and_extra_newlines}, | 
| Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 4075 |     {"docstring_with_signature_with_defaults", | 
 | 4076 |         (PyCFunction)test_with_docstring, METH_NOARGS, | 
 | 4077 |         docstring_with_signature_with_defaults}, | 
| Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 4078 |     {"raise_signal", | 
 | 4079 |      (PyCFunction)test_raise_signal, METH_VARARGS}, | 
| Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 4080 | #ifdef WITH_THREAD | 
| Victor Stinner | fdeb6ec | 2013-12-13 02:01:38 +0100 | [diff] [blame] | 4081 |     {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O, | 
 | 4082 |      PyDoc_STR("set_error_class(error_class) -> None")}, | 
| Victor Stinner | 258e4d3 | 2013-12-13 02:30:12 +0100 | [diff] [blame] | 4083 | #endif | 
| Serhiy Storchaka | b518134 | 2015-02-06 08:58:56 +0200 | [diff] [blame] | 4084 |     {"pymarshal_write_long_to_file", | 
 | 4085 |         pymarshal_write_long_to_file, METH_VARARGS}, | 
 | 4086 |     {"pymarshal_write_object_to_file", | 
 | 4087 |         pymarshal_write_object_to_file, METH_VARARGS}, | 
 | 4088 |     {"pymarshal_read_short_from_file", | 
 | 4089 |         pymarshal_read_short_from_file, METH_VARARGS}, | 
 | 4090 |     {"pymarshal_read_long_from_file", | 
 | 4091 |         pymarshal_read_long_from_file, METH_VARARGS}, | 
 | 4092 |     {"pymarshal_read_last_object_from_file", | 
 | 4093 |         pymarshal_read_last_object_from_file, METH_VARARGS}, | 
 | 4094 |     {"pymarshal_read_object_from_file", | 
 | 4095 |         pymarshal_read_object_from_file, METH_VARARGS}, | 
| Victor Stinner | efde146 | 2015-03-21 15:04:43 +0100 | [diff] [blame] | 4096 |     {"return_null_without_error", | 
 | 4097 |         return_null_without_error, METH_NOARGS}, | 
 | 4098 |     {"return_result_with_error", | 
 | 4099 |         return_result_with_error, METH_NOARGS}, | 
| Victor Stinner | 13019fd | 2015-04-03 13:10:54 +0200 | [diff] [blame] | 4100 |     {"PyTime_FromSeconds", test_pytime_fromseconds,  METH_VARARGS}, | 
| Victor Stinner | 4bfb460 | 2015-03-27 22:27:24 +0100 | [diff] [blame] | 4101 |     {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject,  METH_VARARGS}, | 
 | 4102 |     {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, | 
| Victor Stinner | 95e9cef | 2015-03-28 01:26:47 +0100 | [diff] [blame] | 4103 |     {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, | 
| Victor Stinner | 34dc0f4 | 2015-03-27 18:19:03 +0100 | [diff] [blame] | 4104 | #ifdef HAVE_CLOCK_GETTIME | 
 | 4105 |     {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, | 
 | 4106 | #endif | 
| Victor Stinner | 62d1c70 | 2015-04-01 17:47:07 +0200 | [diff] [blame] | 4107 |     {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS}, | 
 | 4108 |     {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS}, | 
| Victor Stinner | 50856d5 | 2015-10-13 00:11:21 +0200 | [diff] [blame] | 4109 |     {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, | 
| Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 4110 |     {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS}, | 
 | 4111 |     {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, | 
| Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 4112 |     {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, | 
| Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 4113 |     {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, | 
| Victor Stinner | 10b73e1 | 2016-03-22 13:39:05 +0100 | [diff] [blame] | 4114 |     {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, | 
 | 4115 |     {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, | 
 | 4116 |     {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4117 |     {NULL, NULL} /* sentinel */ | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4118 | }; | 
 | 4119 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 4120 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} | 
 | 4121 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4122 | typedef struct { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4123 |     char bool_member; | 
 | 4124 |     char byte_member; | 
 | 4125 |     unsigned char ubyte_member; | 
 | 4126 |     short short_member; | 
 | 4127 |     unsigned short ushort_member; | 
 | 4128 |     int int_member; | 
 | 4129 |     unsigned int uint_member; | 
 | 4130 |     long long_member; | 
 | 4131 |     unsigned long ulong_member; | 
 | 4132 |     Py_ssize_t pyssizet_member; | 
 | 4133 |     float float_member; | 
 | 4134 |     double double_member; | 
 | 4135 |     char inplace_member[6]; | 
| Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 4136 |     long long longlong_member; | 
 | 4137 |     unsigned long long ulonglong_member; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4138 | } all_structmembers; | 
 | 4139 |  | 
 | 4140 | typedef struct { | 
 | 4141 |     PyObject_HEAD | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4142 |     all_structmembers structmembers; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4143 | } test_structmembers; | 
 | 4144 |  | 
 | 4145 | static struct PyMemberDef test_members[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4146 |     {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, | 
 | 4147 |     {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, | 
 | 4148 |     {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, | 
 | 4149 |     {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, | 
 | 4150 |     {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL}, | 
 | 4151 |     {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL}, | 
 | 4152 |     {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, | 
 | 4153 |     {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, | 
 | 4154 |     {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, | 
 | 4155 |     {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, | 
 | 4156 |     {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, | 
 | 4157 |     {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, | 
 | 4158 |     {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4159 |     {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL}, | 
 | 4160 |     {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4161 |     {NULL} | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4162 | }; | 
 | 4163 |  | 
 | 4164 |  | 
| Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4165 | static PyObject * | 
 | 4166 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) | 
 | 4167 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4168 |     static char *keywords[] = { | 
 | 4169 |         "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", | 
 | 4170 |         "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", | 
 | 4171 |         "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 |         "T_LONGLONG", "T_ULONGLONG", | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4173 |         NULL}; | 
| Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 4174 |     static const char fmt[] = "|bbBhHiIlknfds#LK"; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4175 |     test_structmembers *ob; | 
 | 4176 |     const char *s = NULL; | 
 | 4177 |     Py_ssize_t string_len = 0; | 
 | 4178 |     ob = PyObject_New(test_structmembers, type); | 
 | 4179 |     if (ob == NULL) | 
 | 4180 |         return NULL; | 
 | 4181 |     memset(&ob->structmembers, 0, sizeof(all_structmembers)); | 
 | 4182 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, | 
 | 4183 |                                      &ob->structmembers.bool_member, | 
 | 4184 |                                      &ob->structmembers.byte_member, | 
 | 4185 |                                      &ob->structmembers.ubyte_member, | 
 | 4186 |                                      &ob->structmembers.short_member, | 
 | 4187 |                                      &ob->structmembers.ushort_member, | 
 | 4188 |                                      &ob->structmembers.int_member, | 
 | 4189 |                                      &ob->structmembers.uint_member, | 
 | 4190 |                                      &ob->structmembers.long_member, | 
 | 4191 |                                      &ob->structmembers.ulong_member, | 
 | 4192 |                                      &ob->structmembers.pyssizet_member, | 
 | 4193 |                                      &ob->structmembers.float_member, | 
 | 4194 |                                      &ob->structmembers.double_member, | 
 | 4195 |                                      &s, &string_len | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4196 |                                      , &ob->structmembers.longlong_member, | 
 | 4197 |                                      &ob->structmembers.ulonglong_member | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4198 |         )) { | 
 | 4199 |         Py_DECREF(ob); | 
 | 4200 |         return NULL; | 
 | 4201 |     } | 
 | 4202 |     if (s != NULL) { | 
 | 4203 |         if (string_len > 5) { | 
 | 4204 |             Py_DECREF(ob); | 
 | 4205 |             PyErr_SetString(PyExc_ValueError, "string too long"); | 
 | 4206 |             return NULL; | 
 | 4207 |         } | 
 | 4208 |         strcpy(ob->structmembers.inplace_member, s); | 
 | 4209 |     } | 
 | 4210 |     else { | 
 | 4211 |         strcpy(ob->structmembers.inplace_member, ""); | 
 | 4212 |     } | 
 | 4213 |     return (PyObject *)ob; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4214 | } | 
 | 4215 |  | 
| Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 4216 | static void | 
 | 4217 | test_structmembers_free(PyObject *ob) | 
 | 4218 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 |     PyObject_FREE(ob); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4220 | } | 
 | 4221 |  | 
 | 4222 | static PyTypeObject test_structmembersType = { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 4223 |     PyVarObject_HEAD_INIT(NULL, 0) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4224 |     "test_structmembersType", | 
 | 4225 |     sizeof(test_structmembers),         /* tp_basicsize */ | 
 | 4226 |     0,                                  /* tp_itemsize */ | 
 | 4227 |     test_structmembers_free,            /* destructor tp_dealloc */ | 
 | 4228 |     0,                                  /* tp_print */ | 
 | 4229 |     0,                                  /* tp_getattr */ | 
 | 4230 |     0,                                  /* tp_setattr */ | 
 | 4231 |     0,                                  /* tp_reserved */ | 
 | 4232 |     0,                                  /* tp_repr */ | 
 | 4233 |     0,                                  /* tp_as_number */ | 
 | 4234 |     0,                                  /* tp_as_sequence */ | 
 | 4235 |     0,                                  /* tp_as_mapping */ | 
 | 4236 |     0,                                  /* tp_hash */ | 
 | 4237 |     0,                                  /* tp_call */ | 
 | 4238 |     0,                                  /* tp_str */ | 
 | 4239 |     PyObject_GenericGetAttr,            /* tp_getattro */ | 
 | 4240 |     PyObject_GenericSetAttr,            /* tp_setattro */ | 
 | 4241 |     0,                                  /* tp_as_buffer */ | 
 | 4242 |     0,                                  /* tp_flags */ | 
 | 4243 |     "Type containing all structmember types", | 
 | 4244 |     0,                                  /* traverseproc tp_traverse */ | 
 | 4245 |     0,                                  /* tp_clear */ | 
 | 4246 |     0,                                  /* tp_richcompare */ | 
 | 4247 |     0,                                  /* tp_weaklistoffset */ | 
 | 4248 |     0,                                  /* tp_iter */ | 
 | 4249 |     0,                                  /* tp_iternext */ | 
 | 4250 |     0,                                  /* tp_methods */ | 
 | 4251 |     test_members,                       /* tp_members */ | 
 | 4252 |     0, | 
 | 4253 |     0, | 
 | 4254 |     0, | 
 | 4255 |     0, | 
 | 4256 |     0, | 
 | 4257 |     0, | 
 | 4258 |     0, | 
 | 4259 |     0, | 
 | 4260 |     test_structmembers_new,             /* tp_new */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4261 | }; | 
 | 4262 |  | 
 | 4263 |  | 
| Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4264 | typedef struct { | 
 | 4265 |     PyObject_HEAD | 
 | 4266 | } matmulObject; | 
 | 4267 |  | 
 | 4268 | static PyObject * | 
 | 4269 | matmulType_matmul(PyObject *self, PyObject *other) | 
 | 4270 | { | 
 | 4271 |     return Py_BuildValue("(sOO)", "matmul", self, other); | 
 | 4272 | } | 
 | 4273 |  | 
 | 4274 | static PyObject * | 
 | 4275 | matmulType_imatmul(PyObject *self, PyObject *other) | 
 | 4276 | { | 
 | 4277 |     return Py_BuildValue("(sOO)", "imatmul", self, other); | 
 | 4278 | } | 
 | 4279 |  | 
 | 4280 | static void | 
 | 4281 | matmulType_dealloc(PyObject *self) | 
 | 4282 | { | 
| Zachary Ware | 420dc56 | 2014-04-23 13:51:27 -0500 | [diff] [blame] | 4283 |     Py_TYPE(self)->tp_free(self); | 
| Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4284 | } | 
 | 4285 |  | 
 | 4286 | static PyNumberMethods matmulType_as_number = { | 
 | 4287 |     0,                          /* nb_add */ | 
 | 4288 |     0,                          /* nb_subtract */ | 
 | 4289 |     0,                          /* nb_multiply */ | 
 | 4290 |     0,                          /* nb_remainde r*/ | 
 | 4291 |     0,                          /* nb_divmod */ | 
 | 4292 |     0,                          /* nb_power */ | 
 | 4293 |     0,                          /* nb_negative */ | 
 | 4294 |     0,                          /* tp_positive */ | 
 | 4295 |     0,                          /* tp_absolute */ | 
 | 4296 |     0,                          /* tp_bool */ | 
 | 4297 |     0,                          /* nb_invert */ | 
 | 4298 |     0,                          /* nb_lshift */ | 
 | 4299 |     0,                          /* nb_rshift */ | 
 | 4300 |     0,                          /* nb_and */ | 
 | 4301 |     0,                          /* nb_xor */ | 
 | 4302 |     0,                          /* nb_or */ | 
 | 4303 |     0,                          /* nb_int */ | 
 | 4304 |     0,                          /* nb_reserved */ | 
 | 4305 |     0,                          /* nb_float */ | 
 | 4306 |     0,                          /* nb_inplace_add */ | 
 | 4307 |     0,                          /* nb_inplace_subtract */ | 
 | 4308 |     0,                          /* nb_inplace_multiply */ | 
 | 4309 |     0,                          /* nb_inplace_remainder */ | 
 | 4310 |     0,                          /* nb_inplace_power */ | 
 | 4311 |     0,                          /* nb_inplace_lshift */ | 
 | 4312 |     0,                          /* nb_inplace_rshift */ | 
 | 4313 |     0,                          /* nb_inplace_and */ | 
 | 4314 |     0,                          /* nb_inplace_xor */ | 
 | 4315 |     0,                          /* nb_inplace_or */ | 
 | 4316 |     0,                          /* nb_floor_divide */ | 
 | 4317 |     0,                          /* nb_true_divide */ | 
 | 4318 |     0,                          /* nb_inplace_floor_divide */ | 
 | 4319 |     0,                          /* nb_inplace_true_divide */ | 
 | 4320 |     0,                          /* nb_index */ | 
 | 4321 |     matmulType_matmul,        /* nb_matrix_multiply */ | 
 | 4322 |     matmulType_imatmul        /* nb_matrix_inplace_multiply */ | 
 | 4323 | }; | 
 | 4324 |  | 
 | 4325 | static PyTypeObject matmulType = { | 
 | 4326 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 4327 |     "matmulType", | 
 | 4328 |     sizeof(matmulObject),               /* tp_basicsize */ | 
 | 4329 |     0,                                  /* tp_itemsize */ | 
 | 4330 |     matmulType_dealloc,                 /* destructor tp_dealloc */ | 
 | 4331 |     0,                                  /* tp_print */ | 
 | 4332 |     0,                                  /* tp_getattr */ | 
 | 4333 |     0,                                  /* tp_setattr */ | 
 | 4334 |     0,                                  /* tp_reserved */ | 
 | 4335 |     0,                                  /* tp_repr */ | 
 | 4336 |     &matmulType_as_number,              /* tp_as_number */ | 
 | 4337 |     0,                                  /* tp_as_sequence */ | 
 | 4338 |     0,                                  /* tp_as_mapping */ | 
 | 4339 |     0,                                  /* tp_hash */ | 
 | 4340 |     0,                                  /* tp_call */ | 
 | 4341 |     0,                                  /* tp_str */ | 
 | 4342 |     PyObject_GenericGetAttr,            /* tp_getattro */ | 
 | 4343 |     PyObject_GenericSetAttr,            /* tp_setattro */ | 
 | 4344 |     0,                                  /* tp_as_buffer */ | 
 | 4345 |     0,                                  /* tp_flags */ | 
 | 4346 |     "C level type with matrix operations defined", | 
 | 4347 |     0,                                  /* traverseproc tp_traverse */ | 
 | 4348 |     0,                                  /* tp_clear */ | 
 | 4349 |     0,                                  /* tp_richcompare */ | 
 | 4350 |     0,                                  /* tp_weaklistoffset */ | 
 | 4351 |     0,                                  /* tp_iter */ | 
 | 4352 |     0,                                  /* tp_iternext */ | 
 | 4353 |     0,                                  /* tp_methods */ | 
 | 4354 |     0,                                  /* tp_members */ | 
 | 4355 |     0, | 
 | 4356 |     0, | 
 | 4357 |     0, | 
 | 4358 |     0, | 
 | 4359 |     0, | 
 | 4360 |     0, | 
 | 4361 |     0, | 
 | 4362 |     0, | 
 | 4363 |     PyType_GenericNew,                  /* tp_new */ | 
 | 4364 |     PyObject_Del,                       /* tp_free */ | 
 | 4365 | }; | 
 | 4366 |  | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4367 |  | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4368 | typedef struct { | 
 | 4369 |     PyObject_HEAD | 
 | 4370 |     PyObject *ao_iterator; | 
 | 4371 | } awaitObject; | 
 | 4372 |  | 
 | 4373 |  | 
 | 4374 | static PyObject * | 
 | 4375 | awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 4376 | { | 
 | 4377 |     PyObject *v; | 
 | 4378 |     awaitObject *ao; | 
 | 4379 |  | 
 | 4380 |     if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v)) | 
 | 4381 |         return NULL; | 
 | 4382 |  | 
 | 4383 |     ao = (awaitObject *)type->tp_alloc(type, 0); | 
 | 4384 |     if (ao == NULL) { | 
 | 4385 |         return NULL; | 
 | 4386 |     } | 
 | 4387 |  | 
 | 4388 |     Py_INCREF(v); | 
 | 4389 |     ao->ao_iterator = v; | 
 | 4390 |  | 
 | 4391 |     return (PyObject *)ao; | 
 | 4392 | } | 
 | 4393 |  | 
 | 4394 |  | 
 | 4395 | static void | 
 | 4396 | awaitObject_dealloc(awaitObject *ao) | 
 | 4397 | { | 
 | 4398 |     Py_CLEAR(ao->ao_iterator); | 
 | 4399 |     Py_TYPE(ao)->tp_free(ao); | 
 | 4400 | } | 
 | 4401 |  | 
 | 4402 |  | 
 | 4403 | static PyObject * | 
 | 4404 | awaitObject_await(awaitObject *ao) | 
 | 4405 | { | 
 | 4406 |     Py_INCREF(ao->ao_iterator); | 
 | 4407 |     return ao->ao_iterator; | 
 | 4408 | } | 
 | 4409 |  | 
 | 4410 | static PyAsyncMethods awaitType_as_async = { | 
| Yury Selivanov | 6ef0590 | 2015-05-28 11:21:31 -0400 | [diff] [blame] | 4411 |     (unaryfunc)awaitObject_await,           /* am_await */ | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4412 |     0,                                      /* am_aiter */ | 
 | 4413 |     0                                       /* am_anext */ | 
 | 4414 | }; | 
 | 4415 |  | 
 | 4416 |  | 
 | 4417 | static PyTypeObject awaitType = { | 
 | 4418 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 4419 |     "awaitType", | 
 | 4420 |     sizeof(awaitObject),                /* tp_basicsize */ | 
 | 4421 |     0,                                  /* tp_itemsize */ | 
 | 4422 |     (destructor)awaitObject_dealloc,    /* destructor tp_dealloc */ | 
 | 4423 |     0,                                  /* tp_print */ | 
 | 4424 |     0,                                  /* tp_getattr */ | 
 | 4425 |     0,                                  /* tp_setattr */ | 
 | 4426 |     &awaitType_as_async,                /* tp_as_async */ | 
 | 4427 |     0,                                  /* tp_repr */ | 
 | 4428 |     0,                                  /* tp_as_number */ | 
 | 4429 |     0,                                  /* tp_as_sequence */ | 
 | 4430 |     0,                                  /* tp_as_mapping */ | 
 | 4431 |     0,                                  /* tp_hash */ | 
 | 4432 |     0,                                  /* tp_call */ | 
 | 4433 |     0,                                  /* tp_str */ | 
 | 4434 |     PyObject_GenericGetAttr,            /* tp_getattro */ | 
 | 4435 |     PyObject_GenericSetAttr,            /* tp_setattro */ | 
 | 4436 |     0,                                  /* tp_as_buffer */ | 
 | 4437 |     0,                                  /* tp_flags */ | 
 | 4438 |     "C level type with tp_as_async", | 
 | 4439 |     0,                                  /* traverseproc tp_traverse */ | 
 | 4440 |     0,                                  /* tp_clear */ | 
 | 4441 |     0,                                  /* tp_richcompare */ | 
 | 4442 |     0,                                  /* tp_weaklistoffset */ | 
 | 4443 |     0,                                  /* tp_iter */ | 
 | 4444 |     0,                                  /* tp_iternext */ | 
 | 4445 |     0,                                  /* tp_methods */ | 
 | 4446 |     0,                                  /* tp_members */ | 
 | 4447 |     0, | 
 | 4448 |     0, | 
 | 4449 |     0, | 
 | 4450 |     0, | 
 | 4451 |     0, | 
 | 4452 |     0, | 
 | 4453 |     0, | 
 | 4454 |     0, | 
 | 4455 |     awaitObject_new,                    /* tp_new */ | 
 | 4456 |     PyObject_Del,                       /* tp_free */ | 
 | 4457 | }; | 
 | 4458 |  | 
 | 4459 |  | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4460 | static struct PyModuleDef _testcapimodule = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4461 |     PyModuleDef_HEAD_INIT, | 
 | 4462 |     "_testcapi", | 
 | 4463 |     NULL, | 
 | 4464 |     -1, | 
 | 4465 |     TestMethods, | 
 | 4466 |     NULL, | 
 | 4467 |     NULL, | 
 | 4468 |     NULL, | 
 | 4469 |     NULL | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4470 | }; | 
 | 4471 |  | 
| Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 4472 | /* Per PEP 489, this module will not be converted to multi-phase initialization | 
 | 4473 |  */ | 
 | 4474 |  | 
| Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 4475 | PyMODINIT_FUNC | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4476 | PyInit__testcapi(void) | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4477 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4478 |     PyObject *m; | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4479 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4480 |     m = PyModule_Create(&_testcapimodule); | 
 | 4481 |     if (m == NULL) | 
 | 4482 |         return NULL; | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4483 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4484 |     Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type; | 
| Hirokazu Yamamoto | 8ebab5d | 2008-12-31 06:05:46 +0000 | [diff] [blame] | 4485 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4486 |     Py_TYPE(&test_structmembersType)=&PyType_Type; | 
 | 4487 |     Py_INCREF(&test_structmembersType); | 
 | 4488 |     /* don't use a name starting with "test", since we don't want | 
 | 4489 |        test_capi to automatically call this */ | 
 | 4490 |     PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType); | 
| Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 4491 |     if (PyType_Ready(&matmulType) < 0) | 
 | 4492 |         return NULL; | 
 | 4493 |     Py_INCREF(&matmulType); | 
 | 4494 |     PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4495 |  | 
| Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4496 |     if (PyType_Ready(&awaitType) < 0) | 
 | 4497 |         return NULL; | 
 | 4498 |     Py_INCREF(&awaitType); | 
 | 4499 |     PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType); | 
 | 4500 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 |     PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX)); | 
 | 4502 |     PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN)); | 
 | 4503 |     PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX)); | 
 | 4504 |     PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX)); | 
 | 4505 |     PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN)); | 
 | 4506 |     PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX)); | 
 | 4507 |     PyModule_AddObject(m, "INT_MAX",  PyLong_FromLong(INT_MAX)); | 
 | 4508 |     PyModule_AddObject(m, "INT_MIN",  PyLong_FromLong(INT_MIN)); | 
 | 4509 |     PyModule_AddObject(m, "UINT_MAX",  PyLong_FromUnsignedLong(UINT_MAX)); | 
 | 4510 |     PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX)); | 
 | 4511 |     PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN)); | 
 | 4512 |     PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX)); | 
 | 4513 |     PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX)); | 
 | 4514 |     PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN)); | 
 | 4515 |     PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX)); | 
 | 4516 |     PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN)); | 
 | 4517 |     PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX)); | 
 | 4518 |     PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN)); | 
 | 4519 |     PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX)); | 
 | 4520 |     PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); | 
 | 4521 |     PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); | 
 | 4522 |     PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); | 
| Victor Stinner | 4237d34 | 2015-09-10 10:10:39 +0200 | [diff] [blame] | 4523 |     PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t))); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 |     Py_INCREF(&PyInstanceMethod_Type); | 
 | 4525 |     PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 4526 |  | 
| Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 4527 |     PyModule_AddIntConstant(m, "the_number_three", 3); | 
 | 4528 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4529 |     TestError = PyErr_NewException("_testcapi.error", NULL, NULL); | 
 | 4530 |     Py_INCREF(TestError); | 
 | 4531 |     PyModule_AddObject(m, "error", TestError); | 
 | 4532 |     return m; | 
| Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 4533 | } |