| 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 |
|
|---|
| 8 | #include "Python.h"
|
|---|
| 9 | #include <float.h>
|
|---|
| 10 | #include "structmember.h"
|
|---|
| 11 |
|
|---|
| 12 | #ifdef WITH_THREAD
|
|---|
| 13 | #include "pythread.h"
|
|---|
| 14 | #endif /* WITH_THREAD */
|
|---|
| 15 | static PyObject *TestError; /* set to exception object in init */
|
|---|
| 16 |
|
|---|
| 17 | /* Raise TestError with test_name + ": " + msg, and return NULL. */
|
|---|
| 18 |
|
|---|
| 19 | static PyObject *
|
|---|
| 20 | raiseTestError(const char* test_name, const char* msg)
|
|---|
| 21 | {
|
|---|
| 22 | char buf[2048];
|
|---|
| 23 |
|
|---|
| 24 | if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
|
|---|
| 25 | PyErr_SetString(TestError, "internal error msg too large");
|
|---|
| 26 | else {
|
|---|
| 27 | PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
|
|---|
| 28 | PyErr_SetString(TestError, buf);
|
|---|
| 29 | }
|
|---|
| 30 | return NULL;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
|
|---|
| 34 |
|
|---|
| 35 | The ones derived from autoconf on the UNIX-like OSes can be relied
|
|---|
| 36 | upon (in the absence of sloppy cross-compiling), but the Windows
|
|---|
| 37 | platforms have these hardcoded. Better safe than sorry.
|
|---|
| 38 | */
|
|---|
| 39 | static PyObject*
|
|---|
| 40 | sizeof_error(const char* fatname, const char* typname,
|
|---|
| 41 | int expected, int got)
|
|---|
| 42 | {
|
|---|
| 43 | char buf[1024];
|
|---|
| 44 | PyOS_snprintf(buf, sizeof(buf),
|
|---|
| 45 | "%.200s #define == %d but sizeof(%.200s) == %d",
|
|---|
| 46 | fatname, expected, typname, got);
|
|---|
| 47 | PyErr_SetString(TestError, buf);
|
|---|
| 48 | return (PyObject*)NULL;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | static PyObject*
|
|---|
| 52 | test_config(PyObject *self)
|
|---|
| 53 | {
|
|---|
| 54 | #define CHECK_SIZEOF(FATNAME, TYPE) \
|
|---|
| 55 | if (FATNAME != sizeof(TYPE)) \
|
|---|
| 56 | return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
|
|---|
| 57 |
|
|---|
| 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);
|
|---|
| 63 | #ifdef HAVE_LONG_LONG
|
|---|
| 64 | CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
|
|---|
| 65 | #endif
|
|---|
| 66 |
|
|---|
| 67 | #undef CHECK_SIZEOF
|
|---|
| 68 |
|
|---|
| 69 | Py_INCREF(Py_None);
|
|---|
| 70 | return Py_None;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | static PyObject*
|
|---|
| 74 | test_list_api(PyObject *self)
|
|---|
| 75 | {
|
|---|
| 76 | PyObject* list;
|
|---|
| 77 | int i;
|
|---|
| 78 |
|
|---|
| 79 | /* SF bug 132008: PyList_Reverse segfaults */
|
|---|
| 80 | #define NLIST 30
|
|---|
| 81 | list = PyList_New(NLIST);
|
|---|
| 82 | if (list == (PyObject*)NULL)
|
|---|
| 83 | return (PyObject*)NULL;
|
|---|
| 84 | /* list = range(NLIST) */
|
|---|
| 85 | for (i = 0; i < NLIST; ++i) {
|
|---|
| 86 | PyObject* anint = PyInt_FromLong(i);
|
|---|
| 87 | if (anint == (PyObject*)NULL) {
|
|---|
| 88 | Py_DECREF(list);
|
|---|
| 89 | return (PyObject*)NULL;
|
|---|
| 90 | }
|
|---|
| 91 | PyList_SET_ITEM(list, i, anint);
|
|---|
| 92 | }
|
|---|
| 93 | /* list.reverse(), via PyList_Reverse() */
|
|---|
| 94 | i = PyList_Reverse(list); /* should not blow up! */
|
|---|
| 95 | if (i != 0) {
|
|---|
| 96 | Py_DECREF(list);
|
|---|
| 97 | return (PyObject*)NULL;
|
|---|
| 98 | }
|
|---|
| 99 | /* Check that list == range(29, -1, -1) now */
|
|---|
| 100 | for (i = 0; i < NLIST; ++i) {
|
|---|
| 101 | PyObject* anint = PyList_GET_ITEM(list, i);
|
|---|
| 102 | if (PyInt_AS_LONG(anint) != NLIST-1-i) {
|
|---|
| 103 | PyErr_SetString(TestError,
|
|---|
| 104 | "test_list_api: reverse screwed up");
|
|---|
| 105 | Py_DECREF(list);
|
|---|
| 106 | return (PyObject*)NULL;
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | Py_DECREF(list);
|
|---|
| 110 | #undef NLIST
|
|---|
| 111 |
|
|---|
| 112 | Py_INCREF(Py_None);
|
|---|
| 113 | return Py_None;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static int
|
|---|
| 117 | test_dict_inner(int count)
|
|---|
| 118 | {
|
|---|
| 119 | Py_ssize_t pos = 0, iterations = 0;
|
|---|
| 120 | int i;
|
|---|
| 121 | PyObject *dict = PyDict_New();
|
|---|
| 122 | PyObject *v, *k;
|
|---|
| 123 |
|
|---|
| 124 | if (dict == NULL)
|
|---|
| 125 | return -1;
|
|---|
| 126 |
|
|---|
| 127 | for (i = 0; i < count; i++) {
|
|---|
| 128 | v = PyInt_FromLong(i);
|
|---|
| 129 | PyDict_SetItem(dict, v, v);
|
|---|
| 130 | Py_DECREF(v);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | while (PyDict_Next(dict, &pos, &k, &v)) {
|
|---|
| 134 | PyObject *o;
|
|---|
| 135 | iterations++;
|
|---|
| 136 |
|
|---|
| 137 | i = PyInt_AS_LONG(v) + 1;
|
|---|
| 138 | o = PyInt_FromLong(i);
|
|---|
| 139 | if (o == NULL)
|
|---|
| 140 | return -1;
|
|---|
| 141 | if (PyDict_SetItem(dict, k, o) < 0) {
|
|---|
| 142 | Py_DECREF(o);
|
|---|
| 143 | return -1;
|
|---|
| 144 | }
|
|---|
| 145 | Py_DECREF(o);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | Py_DECREF(dict);
|
|---|
| 149 |
|
|---|
| 150 | if (iterations != count) {
|
|---|
| 151 | PyErr_SetString(
|
|---|
| 152 | TestError,
|
|---|
| 153 | "test_dict_iteration: dict iteration went wrong ");
|
|---|
| 154 | return -1;
|
|---|
| 155 | } else {
|
|---|
| 156 | return 0;
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | static PyObject*
|
|---|
| 161 | test_dict_iteration(PyObject* self)
|
|---|
| 162 | {
|
|---|
| 163 | int i;
|
|---|
| 164 |
|
|---|
| 165 | for (i = 0; i < 200; i++) {
|
|---|
| 166 | if (test_dict_inner(i) < 0) {
|
|---|
| 167 | return NULL;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | Py_INCREF(Py_None);
|
|---|
| 172 | return Py_None;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 | /* Issue #4701: Check that PyObject_Hash implicitly calls
|
|---|
| 177 | * PyType_Ready if it hasn't already been called
|
|---|
| 178 | */
|
|---|
| 179 | static PyTypeObject _HashInheritanceTester_Type = {
|
|---|
| 180 | PyObject_HEAD_INIT(NULL)
|
|---|
| 181 | 0, /* Number of items for varobject */
|
|---|
| 182 | "hashinheritancetester", /* Name of this type */
|
|---|
| 183 | sizeof(PyObject), /* Basic object size */
|
|---|
| 184 | 0, /* Item size for varobject */
|
|---|
| 185 | (destructor)PyObject_Del, /* tp_dealloc */
|
|---|
| 186 | 0, /* tp_print */
|
|---|
| 187 | 0, /* tp_getattr */
|
|---|
| 188 | 0, /* tp_setattr */
|
|---|
| 189 | 0, /* tp_compare */
|
|---|
| 190 | 0, /* tp_repr */
|
|---|
| 191 | 0, /* tp_as_number */
|
|---|
| 192 | 0, /* tp_as_sequence */
|
|---|
| 193 | 0, /* tp_as_mapping */
|
|---|
| 194 | 0, /* tp_hash */
|
|---|
| 195 | 0, /* tp_call */
|
|---|
| 196 | 0, /* tp_str */
|
|---|
| 197 | PyObject_GenericGetAttr, /* tp_getattro */
|
|---|
| 198 | 0, /* tp_setattro */
|
|---|
| 199 | 0, /* tp_as_buffer */
|
|---|
| 200 | Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|---|
| 201 | 0, /* tp_doc */
|
|---|
| 202 | 0, /* tp_traverse */
|
|---|
| 203 | 0, /* tp_clear */
|
|---|
| 204 | 0, /* tp_richcompare */
|
|---|
| 205 | 0, /* tp_weaklistoffset */
|
|---|
| 206 | 0, /* tp_iter */
|
|---|
| 207 | 0, /* tp_iternext */
|
|---|
| 208 | 0, /* tp_methods */
|
|---|
| 209 | 0, /* tp_members */
|
|---|
| 210 | 0, /* tp_getset */
|
|---|
| 211 | 0, /* tp_base */
|
|---|
| 212 | 0, /* tp_dict */
|
|---|
| 213 | 0, /* tp_descr_get */
|
|---|
| 214 | 0, /* tp_descr_set */
|
|---|
| 215 | 0, /* tp_dictoffset */
|
|---|
| 216 | 0, /* tp_init */
|
|---|
| 217 | 0, /* tp_alloc */
|
|---|
| 218 | PyType_GenericNew, /* tp_new */
|
|---|
| 219 | };
|
|---|
| 220 |
|
|---|
| 221 | static PyObject*
|
|---|
| 222 | test_lazy_hash_inheritance(PyObject* self)
|
|---|
| 223 | {
|
|---|
| 224 | PyTypeObject *type;
|
|---|
| 225 | PyObject *obj;
|
|---|
| 226 | long hash;
|
|---|
| 227 |
|
|---|
| 228 | type = &_HashInheritanceTester_Type;
|
|---|
| 229 |
|
|---|
| 230 | if (type->tp_dict != NULL)
|
|---|
| 231 | /* The type has already been initialized. This probably means
|
|---|
| 232 | -R is being used. */
|
|---|
| 233 | Py_RETURN_NONE;
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 | obj = PyObject_New(PyObject, type);
|
|---|
| 237 | if (obj == NULL) {
|
|---|
| 238 | PyErr_Clear();
|
|---|
| 239 | PyErr_SetString(
|
|---|
| 240 | TestError,
|
|---|
| 241 | "test_lazy_hash_inheritance: failed to create object");
|
|---|
| 242 | return NULL;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (type->tp_dict != NULL) {
|
|---|
| 246 | PyErr_SetString(
|
|---|
| 247 | TestError,
|
|---|
| 248 | "test_lazy_hash_inheritance: type initialised too soon");
|
|---|
| 249 | Py_DECREF(obj);
|
|---|
| 250 | return NULL;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | hash = PyObject_Hash(obj);
|
|---|
| 254 | if ((hash == -1) && PyErr_Occurred()) {
|
|---|
| 255 | PyErr_Clear();
|
|---|
| 256 | PyErr_SetString(
|
|---|
| 257 | TestError,
|
|---|
| 258 | "test_lazy_hash_inheritance: could not hash object");
|
|---|
| 259 | Py_DECREF(obj);
|
|---|
| 260 | return NULL;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | if (type->tp_dict == NULL) {
|
|---|
| 264 | PyErr_SetString(
|
|---|
| 265 | TestError,
|
|---|
| 266 | "test_lazy_hash_inheritance: type not initialised by hash()");
|
|---|
| 267 | Py_DECREF(obj);
|
|---|
| 268 | return NULL;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | if (type->tp_hash != PyType_Type.tp_hash) {
|
|---|
| 272 | PyErr_SetString(
|
|---|
| 273 | TestError,
|
|---|
| 274 | "test_lazy_hash_inheritance: unexpected hash function");
|
|---|
| 275 | Py_DECREF(obj);
|
|---|
| 276 | return NULL;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | Py_DECREF(obj);
|
|---|
| 280 |
|
|---|
| 281 | Py_RETURN_NONE;
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 | /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
|
|---|
| 286 | PyLong_{As, From}{Unsigned,}LongLong().
|
|---|
| 287 |
|
|---|
| 288 | Note that the meat of the test is contained in testcapi_long.h.
|
|---|
| 289 | This is revolting, but delicate code duplication is worse: "almost
|
|---|
| 290 | exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
|
|---|
| 291 | dependence on type names makes it impossible to use a parameterized
|
|---|
| 292 | function. A giant macro would be even worse than this. A C++ template
|
|---|
| 293 | would be perfect.
|
|---|
| 294 |
|
|---|
| 295 | The "report an error" functions are deliberately not part of the #include
|
|---|
| 296 | file: if the test fails, you can set a breakpoint in the appropriate
|
|---|
| 297 | error function directly, and crawl back from there in the debugger.
|
|---|
| 298 | */
|
|---|
| 299 |
|
|---|
| 300 | #define UNBIND(X) Py_DECREF(X); (X) = NULL
|
|---|
| 301 |
|
|---|
| 302 | static PyObject *
|
|---|
| 303 | raise_test_long_error(const char* msg)
|
|---|
| 304 | {
|
|---|
| 305 | return raiseTestError("test_long_api", msg);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | #define TESTNAME test_long_api_inner
|
|---|
| 309 | #define TYPENAME long
|
|---|
| 310 | #define F_S_TO_PY PyLong_FromLong
|
|---|
| 311 | #define F_PY_TO_S PyLong_AsLong
|
|---|
| 312 | #define F_U_TO_PY PyLong_FromUnsignedLong
|
|---|
| 313 | #define F_PY_TO_U PyLong_AsUnsignedLong
|
|---|
| 314 |
|
|---|
| 315 | #include "testcapi_long.h"
|
|---|
| 316 |
|
|---|
| 317 | static PyObject *
|
|---|
| 318 | test_long_api(PyObject* self)
|
|---|
| 319 | {
|
|---|
| 320 | return TESTNAME(raise_test_long_error);
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | #undef TESTNAME
|
|---|
| 324 | #undef TYPENAME
|
|---|
| 325 | #undef F_S_TO_PY
|
|---|
| 326 | #undef F_PY_TO_S
|
|---|
| 327 | #undef F_U_TO_PY
|
|---|
| 328 | #undef F_PY_TO_U
|
|---|
| 329 |
|
|---|
| 330 | #ifdef HAVE_LONG_LONG
|
|---|
| 331 |
|
|---|
| 332 | static PyObject *
|
|---|
| 333 | raise_test_longlong_error(const char* msg)
|
|---|
| 334 | {
|
|---|
| 335 | return raiseTestError("test_longlong_api", msg);
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | #define TESTNAME test_longlong_api_inner
|
|---|
| 339 | #define TYPENAME PY_LONG_LONG
|
|---|
| 340 | #define F_S_TO_PY PyLong_FromLongLong
|
|---|
| 341 | #define F_PY_TO_S PyLong_AsLongLong
|
|---|
| 342 | #define F_U_TO_PY PyLong_FromUnsignedLongLong
|
|---|
| 343 | #define F_PY_TO_U PyLong_AsUnsignedLongLong
|
|---|
| 344 |
|
|---|
| 345 | #include "testcapi_long.h"
|
|---|
| 346 |
|
|---|
| 347 | static PyObject *
|
|---|
| 348 | test_longlong_api(PyObject* self, PyObject *args)
|
|---|
| 349 | {
|
|---|
| 350 | return TESTNAME(raise_test_longlong_error);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | #undef TESTNAME
|
|---|
| 354 | #undef TYPENAME
|
|---|
| 355 | #undef F_S_TO_PY
|
|---|
| 356 | #undef F_PY_TO_S
|
|---|
| 357 | #undef F_U_TO_PY
|
|---|
| 358 | #undef F_PY_TO_U
|
|---|
| 359 |
|
|---|
| 360 | /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
|
|---|
| 361 | for both long and int arguments. The test may leak a little memory if
|
|---|
| 362 | it fails.
|
|---|
| 363 | */
|
|---|
| 364 | static PyObject *
|
|---|
| 365 | test_L_code(PyObject *self)
|
|---|
| 366 | {
|
|---|
| 367 | PyObject *tuple, *num;
|
|---|
| 368 | PY_LONG_LONG value;
|
|---|
| 369 |
|
|---|
| 370 | tuple = PyTuple_New(1);
|
|---|
| 371 | if (tuple == NULL)
|
|---|
| 372 | return NULL;
|
|---|
| 373 |
|
|---|
| 374 | num = PyLong_FromLong(42);
|
|---|
| 375 | if (num == NULL)
|
|---|
| 376 | return NULL;
|
|---|
| 377 |
|
|---|
| 378 | PyTuple_SET_ITEM(tuple, 0, num);
|
|---|
| 379 |
|
|---|
| 380 | value = -1;
|
|---|
| 381 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
|
|---|
| 382 | return NULL;
|
|---|
| 383 | if (value != 42)
|
|---|
| 384 | return raiseTestError("test_L_code",
|
|---|
| 385 | "L code returned wrong value for long 42");
|
|---|
| 386 |
|
|---|
| 387 | Py_DECREF(num);
|
|---|
| 388 | num = PyInt_FromLong(42);
|
|---|
| 389 | if (num == NULL)
|
|---|
| 390 | return NULL;
|
|---|
| 391 |
|
|---|
| 392 | PyTuple_SET_ITEM(tuple, 0, num);
|
|---|
| 393 |
|
|---|
| 394 | value = -1;
|
|---|
| 395 | if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
|
|---|
| 396 | return NULL;
|
|---|
| 397 | if (value != 42)
|
|---|
| 398 | return raiseTestError("test_L_code",
|
|---|
| 399 | "L code returned wrong value for int 42");
|
|---|
| 400 |
|
|---|
| 401 | Py_DECREF(tuple);
|
|---|
| 402 | Py_INCREF(Py_None);
|
|---|
| 403 | return Py_None;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | #endif /* ifdef HAVE_LONG_LONG */
|
|---|
| 407 |
|
|---|
| 408 | /* Test tuple argument processing */
|
|---|
| 409 | static PyObject *
|
|---|
| 410 | getargs_tuple(PyObject *self, PyObject *args)
|
|---|
| 411 | {
|
|---|
| 412 | int a, b, c;
|
|---|
| 413 | if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
|
|---|
| 414 | return NULL;
|
|---|
| 415 | return Py_BuildValue("iii", a, b, c);
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | /* test PyArg_ParseTupleAndKeywords */
|
|---|
| 419 | static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
|
|---|
| 420 | {
|
|---|
| 421 | static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
|
|---|
| 422 | static char *fmt="(ii)i|(i(ii))(iii)i";
|
|---|
| 423 | int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
|
|---|
| 424 |
|
|---|
| 425 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
|
|---|
| 426 | &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
|
|---|
| 427 | &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
|
|---|
| 428 | return NULL;
|
|---|
| 429 | return Py_BuildValue("iiiiiiiiii",
|
|---|
| 430 | int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
|
|---|
| 431 | int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | /* Functions to call PyArg_ParseTuple with integer format codes,
|
|---|
| 435 | and return the result.
|
|---|
| 436 | */
|
|---|
| 437 | static PyObject *
|
|---|
| 438 | getargs_b(PyObject *self, PyObject *args)
|
|---|
| 439 | {
|
|---|
| 440 | unsigned char value;
|
|---|
| 441 | if (!PyArg_ParseTuple(args, "b", &value))
|
|---|
| 442 | return NULL;
|
|---|
| 443 | return PyLong_FromUnsignedLong((unsigned long)value);
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | static PyObject *
|
|---|
| 447 | getargs_B(PyObject *self, PyObject *args)
|
|---|
| 448 | {
|
|---|
| 449 | unsigned char value;
|
|---|
| 450 | if (!PyArg_ParseTuple(args, "B", &value))
|
|---|
| 451 | return NULL;
|
|---|
| 452 | return PyLong_FromUnsignedLong((unsigned long)value);
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | static PyObject *
|
|---|
| 456 | getargs_H(PyObject *self, PyObject *args)
|
|---|
| 457 | {
|
|---|
| 458 | unsigned short value;
|
|---|
| 459 | if (!PyArg_ParseTuple(args, "H", &value))
|
|---|
| 460 | return NULL;
|
|---|
| 461 | return PyLong_FromUnsignedLong((unsigned long)value);
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | static PyObject *
|
|---|
| 465 | getargs_I(PyObject *self, PyObject *args)
|
|---|
| 466 | {
|
|---|
| 467 | unsigned int value;
|
|---|
| 468 | if (!PyArg_ParseTuple(args, "I", &value))
|
|---|
| 469 | return NULL;
|
|---|
| 470 | return PyLong_FromUnsignedLong((unsigned long)value);
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | static PyObject *
|
|---|
| 474 | getargs_k(PyObject *self, PyObject *args)
|
|---|
| 475 | {
|
|---|
| 476 | unsigned long value;
|
|---|
| 477 | if (!PyArg_ParseTuple(args, "k", &value))
|
|---|
| 478 | return NULL;
|
|---|
| 479 | return PyLong_FromUnsignedLong(value);
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | static PyObject *
|
|---|
| 483 | getargs_i(PyObject *self, PyObject *args)
|
|---|
| 484 | {
|
|---|
| 485 | int value;
|
|---|
| 486 | if (!PyArg_ParseTuple(args, "i", &value))
|
|---|
| 487 | return NULL;
|
|---|
| 488 | return PyLong_FromLong((long)value);
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | static PyObject *
|
|---|
| 492 | getargs_l(PyObject *self, PyObject *args)
|
|---|
| 493 | {
|
|---|
| 494 | long value;
|
|---|
| 495 | if (!PyArg_ParseTuple(args, "l", &value))
|
|---|
| 496 | return NULL;
|
|---|
| 497 | return PyLong_FromLong(value);
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | static PyObject *
|
|---|
| 501 | getargs_n(PyObject *self, PyObject *args)
|
|---|
| 502 | {
|
|---|
| 503 | Py_ssize_t value;
|
|---|
| 504 | if (!PyArg_ParseTuple(args, "n", &value))
|
|---|
| 505 | return NULL;
|
|---|
| 506 | return PyInt_FromSsize_t(value);
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | #ifdef HAVE_LONG_LONG
|
|---|
| 510 | static PyObject *
|
|---|
| 511 | getargs_L(PyObject *self, PyObject *args)
|
|---|
| 512 | {
|
|---|
| 513 | PY_LONG_LONG value;
|
|---|
| 514 | if (!PyArg_ParseTuple(args, "L", &value))
|
|---|
| 515 | return NULL;
|
|---|
| 516 | return PyLong_FromLongLong(value);
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | static PyObject *
|
|---|
| 520 | getargs_K(PyObject *self, PyObject *args)
|
|---|
| 521 | {
|
|---|
| 522 | unsigned PY_LONG_LONG value;
|
|---|
| 523 | if (!PyArg_ParseTuple(args, "K", &value))
|
|---|
| 524 | return NULL;
|
|---|
| 525 | return PyLong_FromUnsignedLongLong(value);
|
|---|
| 526 | }
|
|---|
| 527 | #endif
|
|---|
| 528 |
|
|---|
| 529 | /* This function not only tests the 'k' getargs code, but also the
|
|---|
| 530 | PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
|
|---|
| 531 | static PyObject *
|
|---|
| 532 | test_k_code(PyObject *self)
|
|---|
| 533 | {
|
|---|
| 534 | PyObject *tuple, *num;
|
|---|
| 535 | unsigned long value;
|
|---|
| 536 |
|
|---|
| 537 | tuple = PyTuple_New(1);
|
|---|
| 538 | if (tuple == NULL)
|
|---|
| 539 | return NULL;
|
|---|
| 540 |
|
|---|
| 541 | /* a number larger than ULONG_MAX even on 64-bit platforms */
|
|---|
| 542 | num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
|
|---|
| 543 | if (num == NULL)
|
|---|
| 544 | return NULL;
|
|---|
| 545 |
|
|---|
| 546 | value = PyInt_AsUnsignedLongMask(num);
|
|---|
| 547 | if (value != ULONG_MAX)
|
|---|
| 548 | return raiseTestError("test_k_code",
|
|---|
| 549 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
|
|---|
| 550 |
|
|---|
| 551 | PyTuple_SET_ITEM(tuple, 0, num);
|
|---|
| 552 |
|
|---|
| 553 | value = 0;
|
|---|
| 554 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
|
|---|
| 555 | return NULL;
|
|---|
| 556 | if (value != ULONG_MAX)
|
|---|
| 557 | return raiseTestError("test_k_code",
|
|---|
| 558 | "k code returned wrong value for long 0xFFF...FFF");
|
|---|
| 559 |
|
|---|
| 560 | Py_DECREF(num);
|
|---|
| 561 | num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
|
|---|
| 562 | if (num == NULL)
|
|---|
| 563 | return NULL;
|
|---|
| 564 |
|
|---|
| 565 | value = PyInt_AsUnsignedLongMask(num);
|
|---|
| 566 | if (value != (unsigned long)-0x42)
|
|---|
| 567 | return raiseTestError("test_k_code",
|
|---|
| 568 | "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
|
|---|
| 569 |
|
|---|
| 570 | PyTuple_SET_ITEM(tuple, 0, num);
|
|---|
| 571 |
|
|---|
| 572 | value = 0;
|
|---|
| 573 | if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
|
|---|
| 574 | return NULL;
|
|---|
| 575 | if (value != (unsigned long)-0x42)
|
|---|
| 576 | return raiseTestError("test_k_code",
|
|---|
| 577 | "k code returned wrong value for long -0xFFF..000042");
|
|---|
| 578 |
|
|---|
| 579 | Py_DECREF(tuple);
|
|---|
| 580 | Py_INCREF(Py_None);
|
|---|
| 581 | return Py_None;
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | #ifdef Py_USING_UNICODE
|
|---|
| 585 |
|
|---|
| 586 | static volatile int x;
|
|---|
| 587 |
|
|---|
| 588 | /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
|
|---|
| 589 | of an error.
|
|---|
| 590 | */
|
|---|
| 591 | static PyObject *
|
|---|
| 592 | test_u_code(PyObject *self)
|
|---|
| 593 | {
|
|---|
| 594 | PyObject *tuple, *obj;
|
|---|
| 595 | Py_UNICODE *value;
|
|---|
| 596 | int len;
|
|---|
| 597 |
|
|---|
| 598 | /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
|
|---|
| 599 | /* Just use the macro and check that it compiles */
|
|---|
| 600 | x = Py_UNICODE_ISSPACE(25);
|
|---|
| 601 |
|
|---|
| 602 | tuple = PyTuple_New(1);
|
|---|
| 603 | if (tuple == NULL)
|
|---|
| 604 | return NULL;
|
|---|
| 605 |
|
|---|
| 606 | obj = PyUnicode_Decode("test", strlen("test"),
|
|---|
| 607 | "ascii", NULL);
|
|---|
| 608 | if (obj == NULL)
|
|---|
| 609 | return NULL;
|
|---|
| 610 |
|
|---|
| 611 | PyTuple_SET_ITEM(tuple, 0, obj);
|
|---|
| 612 |
|
|---|
| 613 | value = 0;
|
|---|
| 614 | if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
|
|---|
| 615 | return NULL;
|
|---|
| 616 | if (value != PyUnicode_AS_UNICODE(obj))
|
|---|
| 617 | return raiseTestError("test_u_code",
|
|---|
| 618 | "u code returned wrong value for u'test'");
|
|---|
| 619 | value = 0;
|
|---|
| 620 | if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
|
|---|
| 621 | return NULL;
|
|---|
| 622 | if (value != PyUnicode_AS_UNICODE(obj) ||
|
|---|
| 623 | len != PyUnicode_GET_SIZE(obj))
|
|---|
| 624 | return raiseTestError("test_u_code",
|
|---|
| 625 | "u# code returned wrong values for u'test'");
|
|---|
| 626 |
|
|---|
| 627 | Py_DECREF(tuple);
|
|---|
| 628 | Py_INCREF(Py_None);
|
|---|
| 629 | return Py_None;
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | static PyObject *
|
|---|
| 633 | test_empty_argparse(PyObject *self)
|
|---|
| 634 | {
|
|---|
| 635 | /* Test that formats can begin with '|'. See issue #4720. */
|
|---|
| 636 | PyObject *tuple, *dict = NULL;
|
|---|
| 637 | static char *kwlist[] = {NULL};
|
|---|
| 638 | int result;
|
|---|
| 639 | tuple = PyTuple_New(0);
|
|---|
| 640 | if (!tuple)
|
|---|
| 641 | return NULL;
|
|---|
| 642 | if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
|
|---|
| 643 | goto done;
|
|---|
| 644 | dict = PyDict_New();
|
|---|
| 645 | if (!dict)
|
|---|
| 646 | goto done;
|
|---|
| 647 | result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
|
|---|
| 648 | done:
|
|---|
| 649 | Py_DECREF(tuple);
|
|---|
| 650 | Py_XDECREF(dict);
|
|---|
| 651 | if (result < 0)
|
|---|
| 652 | return NULL;
|
|---|
| 653 | else {
|
|---|
| 654 | Py_RETURN_NONE;
|
|---|
| 655 | }
|
|---|
| 656 | }
|
|---|
| 657 |
|
|---|
| 658 | static PyObject *
|
|---|
| 659 | codec_incrementalencoder(PyObject *self, PyObject *args)
|
|---|
| 660 | {
|
|---|
| 661 | const char *encoding, *errors = NULL;
|
|---|
| 662 | if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
|
|---|
| 663 | &encoding, &errors))
|
|---|
| 664 | return NULL;
|
|---|
| 665 | return PyCodec_IncrementalEncoder(encoding, errors);
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | static PyObject *
|
|---|
| 669 | codec_incrementaldecoder(PyObject *self, PyObject *args)
|
|---|
| 670 | {
|
|---|
| 671 | const char *encoding, *errors = NULL;
|
|---|
| 672 | if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
|
|---|
| 673 | &encoding, &errors))
|
|---|
| 674 | return NULL;
|
|---|
| 675 | return PyCodec_IncrementalDecoder(encoding, errors);
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | #endif
|
|---|
| 679 |
|
|---|
| 680 | /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
|
|---|
| 681 | static PyObject *
|
|---|
| 682 | test_long_numbits(PyObject *self)
|
|---|
| 683 | {
|
|---|
| 684 | struct triple {
|
|---|
| 685 | long input;
|
|---|
| 686 | size_t nbits;
|
|---|
| 687 | int sign;
|
|---|
| 688 | } testcases[] = {{0, 0, 0},
|
|---|
| 689 | {1L, 1, 1},
|
|---|
| 690 | {-1L, 1, -1},
|
|---|
| 691 | {2L, 2, 1},
|
|---|
| 692 | {-2L, 2, -1},
|
|---|
| 693 | {3L, 2, 1},
|
|---|
| 694 | {-3L, 2, -1},
|
|---|
| 695 | {4L, 3, 1},
|
|---|
| 696 | {-4L, 3, -1},
|
|---|
| 697 | {0x7fffL, 15, 1}, /* one Python long digit */
|
|---|
| 698 | {-0x7fffL, 15, -1},
|
|---|
| 699 | {0xffffL, 16, 1},
|
|---|
| 700 | {-0xffffL, 16, -1},
|
|---|
| 701 | {0xfffffffL, 28, 1},
|
|---|
| 702 | {-0xfffffffL, 28, -1}};
|
|---|
| 703 | int i;
|
|---|
| 704 |
|
|---|
| 705 | for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
|
|---|
| 706 | PyObject *plong = PyLong_FromLong(testcases[i].input);
|
|---|
| 707 | size_t nbits = _PyLong_NumBits(plong);
|
|---|
| 708 | int sign = _PyLong_Sign(plong);
|
|---|
| 709 |
|
|---|
| 710 | Py_DECREF(plong);
|
|---|
| 711 | if (nbits != testcases[i].nbits)
|
|---|
| 712 | return raiseTestError("test_long_numbits",
|
|---|
| 713 | "wrong result for _PyLong_NumBits");
|
|---|
| 714 | if (sign != testcases[i].sign)
|
|---|
| 715 | return raiseTestError("test_long_numbits",
|
|---|
| 716 | "wrong result for _PyLong_Sign");
|
|---|
| 717 | }
|
|---|
| 718 | Py_INCREF(Py_None);
|
|---|
| 719 | return Py_None;
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
|
|---|
| 723 |
|
|---|
| 724 | static PyObject *
|
|---|
| 725 | test_null_strings(PyObject *self)
|
|---|
| 726 | {
|
|---|
| 727 | PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
|
|---|
| 728 | PyObject *tuple = PyTuple_Pack(2, o1, o2);
|
|---|
| 729 | Py_XDECREF(o1);
|
|---|
| 730 | Py_XDECREF(o2);
|
|---|
| 731 | return tuple;
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | static PyObject *
|
|---|
| 735 | raise_exception(PyObject *self, PyObject *args)
|
|---|
| 736 | {
|
|---|
| 737 | PyObject *exc;
|
|---|
| 738 | PyObject *exc_args, *v;
|
|---|
| 739 | int num_args, i;
|
|---|
| 740 |
|
|---|
| 741 | if (!PyArg_ParseTuple(args, "Oi:raise_exception",
|
|---|
| 742 | &exc, &num_args))
|
|---|
| 743 | return NULL;
|
|---|
| 744 | if (!PyExceptionClass_Check(exc)) {
|
|---|
| 745 | PyErr_Format(PyExc_TypeError, "an exception class is required");
|
|---|
| 746 | return NULL;
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | exc_args = PyTuple_New(num_args);
|
|---|
| 750 | if (exc_args == NULL)
|
|---|
| 751 | return NULL;
|
|---|
| 752 | for (i = 0; i < num_args; ++i) {
|
|---|
| 753 | v = PyInt_FromLong(i);
|
|---|
| 754 | if (v == NULL) {
|
|---|
| 755 | Py_DECREF(exc_args);
|
|---|
| 756 | return NULL;
|
|---|
| 757 | }
|
|---|
| 758 | PyTuple_SET_ITEM(exc_args, i, v);
|
|---|
| 759 | }
|
|---|
| 760 | PyErr_SetObject(exc, exc_args);
|
|---|
| 761 | Py_DECREF(exc_args);
|
|---|
| 762 | return NULL;
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | #ifdef WITH_THREAD
|
|---|
| 766 |
|
|---|
| 767 | /* test_thread_state spawns a thread of its own, and that thread releases
|
|---|
| 768 | * `thread_done` when it's finished. The driver code has to know when the
|
|---|
| 769 | * thread finishes, because the thread uses a PyObject (the callable) that
|
|---|
| 770 | * may go away when the driver finishes. The former lack of this explicit
|
|---|
| 771 | * synchronization caused rare segfaults, so rare that they were seen only
|
|---|
| 772 | * on a Mac buildbot (although they were possible on any box).
|
|---|
| 773 | */
|
|---|
| 774 | static PyThread_type_lock thread_done = NULL;
|
|---|
| 775 |
|
|---|
| 776 | static int
|
|---|
| 777 | _make_call(void *callable)
|
|---|
| 778 | {
|
|---|
| 779 | PyObject *rc;
|
|---|
| 780 | int success;
|
|---|
| 781 | PyGILState_STATE s = PyGILState_Ensure();
|
|---|
| 782 | rc = PyObject_CallFunction((PyObject *)callable, "");
|
|---|
| 783 | success = (rc != NULL);
|
|---|
| 784 | Py_XDECREF(rc);
|
|---|
| 785 | PyGILState_Release(s);
|
|---|
| 786 | return success;
|
|---|
| 787 | }
|
|---|
| 788 |
|
|---|
| 789 | /* Same thing, but releases `thread_done` when it returns. This variant
|
|---|
| 790 | * should be called only from threads spawned by test_thread_state().
|
|---|
| 791 | */
|
|---|
| 792 | static void
|
|---|
| 793 | _make_call_from_thread(void *callable)
|
|---|
| 794 | {
|
|---|
| 795 | _make_call(callable);
|
|---|
| 796 | PyThread_release_lock(thread_done);
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 | static PyObject *
|
|---|
| 800 | test_thread_state(PyObject *self, PyObject *args)
|
|---|
| 801 | {
|
|---|
| 802 | PyObject *fn;
|
|---|
| 803 | int success = 1;
|
|---|
| 804 |
|
|---|
| 805 | if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
|
|---|
| 806 | return NULL;
|
|---|
| 807 |
|
|---|
| 808 | if (!PyCallable_Check(fn)) {
|
|---|
| 809 | PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
|
|---|
| 810 | fn->ob_type->tp_name);
|
|---|
| 811 | return NULL;
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | /* Ensure Python is set up for threading */
|
|---|
| 815 | PyEval_InitThreads();
|
|---|
| 816 | thread_done = PyThread_allocate_lock();
|
|---|
| 817 | if (thread_done == NULL)
|
|---|
| 818 | return PyErr_NoMemory();
|
|---|
| 819 | PyThread_acquire_lock(thread_done, 1);
|
|---|
| 820 |
|
|---|
| 821 | /* Start a new thread with our callback. */
|
|---|
| 822 | PyThread_start_new_thread(_make_call_from_thread, fn);
|
|---|
| 823 | /* Make the callback with the thread lock held by this thread */
|
|---|
| 824 | success &= _make_call(fn);
|
|---|
| 825 | /* Do it all again, but this time with the thread-lock released */
|
|---|
| 826 | Py_BEGIN_ALLOW_THREADS
|
|---|
| 827 | success &= _make_call(fn);
|
|---|
| 828 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
|
|---|
| 829 | Py_END_ALLOW_THREADS
|
|---|
| 830 |
|
|---|
| 831 | /* And once more with and without a thread
|
|---|
| 832 | XXX - should use a lock and work out exactly what we are trying
|
|---|
| 833 | to test <wink>
|
|---|
| 834 | */
|
|---|
| 835 | Py_BEGIN_ALLOW_THREADS
|
|---|
| 836 | PyThread_start_new_thread(_make_call_from_thread, fn);
|
|---|
| 837 | success &= _make_call(fn);
|
|---|
| 838 | PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
|
|---|
| 839 | Py_END_ALLOW_THREADS
|
|---|
| 840 |
|
|---|
| 841 | /* Release lock we acquired above. This is required on HP-UX. */
|
|---|
| 842 | PyThread_release_lock(thread_done);
|
|---|
| 843 |
|
|---|
| 844 | PyThread_free_lock(thread_done);
|
|---|
| 845 | if (!success)
|
|---|
| 846 | return NULL;
|
|---|
| 847 | Py_RETURN_NONE;
|
|---|
| 848 | }
|
|---|
| 849 | #endif
|
|---|
| 850 |
|
|---|
| 851 | /* Some tests of PyString_FromFormat(). This needs more tests. */
|
|---|
| 852 | static PyObject *
|
|---|
| 853 | test_string_from_format(PyObject *self, PyObject *args)
|
|---|
| 854 | {
|
|---|
| 855 | PyObject *result;
|
|---|
| 856 | char *msg;
|
|---|
| 857 |
|
|---|
| 858 | #define CHECK_1_FORMAT(FORMAT, TYPE) \
|
|---|
| 859 | result = PyString_FromFormat(FORMAT, (TYPE)1); \
|
|---|
| 860 | if (result == NULL) \
|
|---|
| 861 | return NULL; \
|
|---|
| 862 | if (strcmp(PyString_AsString(result), "1")) { \
|
|---|
| 863 | msg = FORMAT " failed at 1"; \
|
|---|
| 864 | goto Fail; \
|
|---|
| 865 | } \
|
|---|
| 866 | Py_DECREF(result)
|
|---|
| 867 |
|
|---|
| 868 | CHECK_1_FORMAT("%d", int);
|
|---|
| 869 | CHECK_1_FORMAT("%ld", long);
|
|---|
| 870 | /* The z width modifier was added in Python 2.5. */
|
|---|
| 871 | CHECK_1_FORMAT("%zd", Py_ssize_t);
|
|---|
| 872 |
|
|---|
| 873 | /* The u type code was added in Python 2.5. */
|
|---|
| 874 | CHECK_1_FORMAT("%u", unsigned int);
|
|---|
| 875 | CHECK_1_FORMAT("%lu", unsigned long);
|
|---|
| 876 | CHECK_1_FORMAT("%zu", size_t);
|
|---|
| 877 |
|
|---|
| 878 | Py_RETURN_NONE;
|
|---|
| 879 |
|
|---|
| 880 | Fail:
|
|---|
| 881 | Py_XDECREF(result);
|
|---|
| 882 | return raiseTestError("test_string_from_format", msg);
|
|---|
| 883 |
|
|---|
| 884 | #undef CHECK_1_FORMAT
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | /* This is here to provide a docstring for test_descr. */
|
|---|
| 888 | static PyObject *
|
|---|
| 889 | test_with_docstring(PyObject *self)
|
|---|
| 890 | {
|
|---|
| 891 | Py_RETURN_NONE;
|
|---|
| 892 | }
|
|---|
| 893 |
|
|---|
| 894 | /* To test the format of tracebacks as printed out. */
|
|---|
| 895 | static PyObject *
|
|---|
| 896 | traceback_print(PyObject *self, PyObject *args)
|
|---|
| 897 | {
|
|---|
| 898 | PyObject *file;
|
|---|
| 899 | PyObject *traceback;
|
|---|
| 900 | int result;
|
|---|
| 901 |
|
|---|
| 902 | if (!PyArg_ParseTuple(args, "OO:traceback_print",
|
|---|
| 903 | &traceback, &file))
|
|---|
| 904 | return NULL;
|
|---|
| 905 |
|
|---|
| 906 | result = PyTraceBack_Print(traceback, file);
|
|---|
| 907 | if (result < 0)
|
|---|
| 908 | return NULL;
|
|---|
| 909 | Py_RETURN_NONE;
|
|---|
| 910 | }
|
|---|
| 911 |
|
|---|
| 912 | static PyMethodDef TestMethods[] = {
|
|---|
| 913 | {"raise_exception", raise_exception, METH_VARARGS},
|
|---|
| 914 | {"test_config", (PyCFunction)test_config, METH_NOARGS},
|
|---|
| 915 | {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
|
|---|
| 916 | {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
|
|---|
| 917 | {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
|
|---|
| 918 | {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
|
|---|
| 919 | {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
|
|---|
| 920 | {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
|
|---|
| 921 | {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
|
|---|
| 922 | {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
|
|---|
| 923 | {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
|
|---|
| 924 | {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
|
|---|
| 925 | PyDoc_STR("This is a pretty normal docstring.")},
|
|---|
| 926 |
|
|---|
| 927 | {"getargs_tuple", getargs_tuple, METH_VARARGS},
|
|---|
| 928 | {"getargs_keywords", (PyCFunction)getargs_keywords,
|
|---|
| 929 | METH_VARARGS|METH_KEYWORDS},
|
|---|
| 930 | {"getargs_b", getargs_b, METH_VARARGS},
|
|---|
| 931 | {"getargs_B", getargs_B, METH_VARARGS},
|
|---|
| 932 | {"getargs_H", getargs_H, METH_VARARGS},
|
|---|
| 933 | {"getargs_I", getargs_I, METH_VARARGS},
|
|---|
| 934 | {"getargs_k", getargs_k, METH_VARARGS},
|
|---|
| 935 | {"getargs_i", getargs_i, METH_VARARGS},
|
|---|
| 936 | {"getargs_l", getargs_l, METH_VARARGS},
|
|---|
| 937 | {"getargs_n", getargs_n, METH_VARARGS},
|
|---|
| 938 | #ifdef HAVE_LONG_LONG
|
|---|
| 939 | {"getargs_L", getargs_L, METH_VARARGS},
|
|---|
| 940 | {"getargs_K", getargs_K, METH_VARARGS},
|
|---|
| 941 | {"test_longlong_api", test_longlong_api, METH_NOARGS},
|
|---|
| 942 | {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
|
|---|
| 943 | {"codec_incrementalencoder",
|
|---|
| 944 | (PyCFunction)codec_incrementalencoder, METH_VARARGS},
|
|---|
| 945 | {"codec_incrementaldecoder",
|
|---|
| 946 | (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
|
|---|
| 947 | #endif
|
|---|
| 948 | #ifdef Py_USING_UNICODE
|
|---|
| 949 | {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
|
|---|
| 950 | #endif
|
|---|
| 951 | #ifdef WITH_THREAD
|
|---|
| 952 | {"_test_thread_state", test_thread_state, METH_VARARGS},
|
|---|
| 953 | #endif
|
|---|
| 954 | {"traceback_print", traceback_print, METH_VARARGS},
|
|---|
| 955 | {NULL, NULL} /* sentinel */
|
|---|
| 956 | };
|
|---|
| 957 |
|
|---|
| 958 | #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
|
|---|
| 959 |
|
|---|
| 960 | typedef struct {
|
|---|
| 961 | char bool_member;
|
|---|
| 962 | char byte_member;
|
|---|
| 963 | unsigned char ubyte_member;
|
|---|
| 964 | short short_member;
|
|---|
| 965 | unsigned short ushort_member;
|
|---|
| 966 | int int_member;
|
|---|
| 967 | unsigned int uint_member;
|
|---|
| 968 | long long_member;
|
|---|
| 969 | unsigned long ulong_member;
|
|---|
| 970 | float float_member;
|
|---|
| 971 | double double_member;
|
|---|
| 972 | #ifdef HAVE_LONG_LONG
|
|---|
| 973 | PY_LONG_LONG longlong_member;
|
|---|
| 974 | unsigned PY_LONG_LONG ulonglong_member;
|
|---|
| 975 | #endif
|
|---|
| 976 | } all_structmembers;
|
|---|
| 977 |
|
|---|
| 978 | typedef struct {
|
|---|
| 979 | PyObject_HEAD
|
|---|
| 980 | all_structmembers structmembers;
|
|---|
| 981 | } test_structmembers;
|
|---|
| 982 |
|
|---|
| 983 | static struct PyMemberDef test_members[] = {
|
|---|
| 984 | {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
|
|---|
| 985 | {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
|
|---|
| 986 | {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
|
|---|
| 987 | {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
|
|---|
| 988 | {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
|
|---|
| 989 | {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
|
|---|
| 990 | {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
|
|---|
| 991 | {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
|
|---|
| 992 | {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
|
|---|
| 993 | {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
|
|---|
| 994 | {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
|
|---|
| 995 | #ifdef HAVE_LONG_LONG
|
|---|
| 996 | {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
|
|---|
| 997 | {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
|
|---|
| 998 | #endif
|
|---|
| 999 | {NULL}
|
|---|
| 1000 | };
|
|---|
| 1001 |
|
|---|
| 1002 |
|
|---|
| 1003 | static PyObject *
|
|---|
| 1004 | test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
|---|
| 1005 | {
|
|---|
| 1006 | static char *keywords[] = {
|
|---|
| 1007 | "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
|
|---|
| 1008 | "T_INT", "T_UINT", "T_LONG", "T_ULONG",
|
|---|
| 1009 | "T_FLOAT", "T_DOUBLE",
|
|---|
| 1010 | #ifdef HAVE_LONG_LONG
|
|---|
| 1011 | "T_LONGLONG", "T_ULONGLONG",
|
|---|
| 1012 | #endif
|
|---|
| 1013 | NULL};
|
|---|
| 1014 | static char *fmt = "|bbBhHiIlkfd"
|
|---|
| 1015 | #ifdef HAVE_LONG_LONG
|
|---|
| 1016 | "LK"
|
|---|
| 1017 | #endif
|
|---|
| 1018 | ;
|
|---|
| 1019 | test_structmembers *ob;
|
|---|
| 1020 | ob = PyObject_New(test_structmembers, type);
|
|---|
| 1021 | if (ob == NULL)
|
|---|
| 1022 | return NULL;
|
|---|
| 1023 | memset(&ob->structmembers, 0, sizeof(all_structmembers));
|
|---|
| 1024 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
|
|---|
| 1025 | &ob->structmembers.bool_member,
|
|---|
| 1026 | &ob->structmembers.byte_member,
|
|---|
| 1027 | &ob->structmembers.ubyte_member,
|
|---|
| 1028 | &ob->structmembers.short_member,
|
|---|
| 1029 | &ob->structmembers.ushort_member,
|
|---|
| 1030 | &ob->structmembers.int_member,
|
|---|
| 1031 | &ob->structmembers.uint_member,
|
|---|
| 1032 | &ob->structmembers.long_member,
|
|---|
| 1033 | &ob->structmembers.ulong_member,
|
|---|
| 1034 | &ob->structmembers.float_member,
|
|---|
| 1035 | &ob->structmembers.double_member
|
|---|
| 1036 | #ifdef HAVE_LONG_LONG
|
|---|
| 1037 | , &ob->structmembers.longlong_member,
|
|---|
| 1038 | &ob->structmembers.ulonglong_member
|
|---|
| 1039 | #endif
|
|---|
| 1040 | )) {
|
|---|
| 1041 | Py_DECREF(ob);
|
|---|
| 1042 | return NULL;
|
|---|
| 1043 | }
|
|---|
| 1044 | return (PyObject *)ob;
|
|---|
| 1045 | }
|
|---|
| 1046 |
|
|---|
| 1047 | static void
|
|---|
| 1048 | test_structmembers_free(PyObject *ob)
|
|---|
| 1049 | {
|
|---|
| 1050 | PyObject_FREE(ob);
|
|---|
| 1051 | }
|
|---|
| 1052 |
|
|---|
| 1053 | static PyTypeObject test_structmembersType = {
|
|---|
| 1054 | PyVarObject_HEAD_INIT(NULL, 0)
|
|---|
| 1055 | "test_structmembersType",
|
|---|
| 1056 | sizeof(test_structmembers), /* tp_basicsize */
|
|---|
| 1057 | 0, /* tp_itemsize */
|
|---|
| 1058 | test_structmembers_free, /* destructor tp_dealloc */
|
|---|
| 1059 | 0, /* tp_print */
|
|---|
| 1060 | 0, /* tp_getattr */
|
|---|
| 1061 | 0, /* tp_setattr */
|
|---|
| 1062 | 0, /* tp_compare */
|
|---|
| 1063 | 0, /* tp_repr */
|
|---|
| 1064 | 0, /* tp_as_number */
|
|---|
| 1065 | 0, /* tp_as_sequence */
|
|---|
| 1066 | 0, /* tp_as_mapping */
|
|---|
| 1067 | 0, /* tp_hash */
|
|---|
| 1068 | 0, /* tp_call */
|
|---|
| 1069 | 0, /* tp_str */
|
|---|
| 1070 | PyObject_GenericGetAttr, /* tp_getattro */
|
|---|
| 1071 | PyObject_GenericSetAttr, /* tp_setattro */
|
|---|
| 1072 | 0, /* tp_as_buffer */
|
|---|
| 1073 | 0, /* tp_flags */
|
|---|
| 1074 | "Type containing all structmember types",
|
|---|
| 1075 | 0, /* traverseproc tp_traverse */
|
|---|
| 1076 | 0, /* tp_clear */
|
|---|
| 1077 | 0, /* tp_richcompare */
|
|---|
| 1078 | 0, /* tp_weaklistoffset */
|
|---|
| 1079 | 0, /* tp_iter */
|
|---|
| 1080 | 0, /* tp_iternext */
|
|---|
| 1081 | 0, /* tp_methods */
|
|---|
| 1082 | test_members, /* tp_members */
|
|---|
| 1083 | 0,
|
|---|
| 1084 | 0,
|
|---|
| 1085 | 0,
|
|---|
| 1086 | 0,
|
|---|
| 1087 | 0,
|
|---|
| 1088 | 0,
|
|---|
| 1089 | 0,
|
|---|
| 1090 | 0,
|
|---|
| 1091 | test_structmembers_new, /* tp_new */
|
|---|
| 1092 | };
|
|---|
| 1093 |
|
|---|
| 1094 |
|
|---|
| 1095 | PyMODINIT_FUNC
|
|---|
| 1096 | init_testcapi(void)
|
|---|
| 1097 | {
|
|---|
| 1098 | PyObject *m;
|
|---|
| 1099 |
|
|---|
| 1100 | m = Py_InitModule("_testcapi", TestMethods);
|
|---|
| 1101 | if (m == NULL)
|
|---|
| 1102 | return;
|
|---|
| 1103 |
|
|---|
| 1104 | Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
|
|---|
| 1105 |
|
|---|
| 1106 | Py_TYPE(&test_structmembersType)=&PyType_Type;
|
|---|
| 1107 | Py_INCREF(&test_structmembersType);
|
|---|
| 1108 | PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType);
|
|---|
| 1109 |
|
|---|
| 1110 | PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
|
|---|
| 1111 | PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));
|
|---|
| 1112 | PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
|
|---|
| 1113 | PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX));
|
|---|
| 1114 | PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN));
|
|---|
| 1115 | PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
|
|---|
| 1116 | PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
|
|---|
| 1117 | PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
|
|---|
| 1118 | PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
|
|---|
| 1119 | PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
|
|---|
| 1120 | PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
|
|---|
| 1121 | PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
|
|---|
| 1122 | PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
|
|---|
| 1123 | PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
|
|---|
| 1124 | PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
|
|---|
| 1125 | PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
|
|---|
| 1126 | PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
|
|---|
| 1127 | PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
|
|---|
| 1128 | PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
|
|---|
| 1129 | PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
|
|---|
| 1130 | PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
|
|---|
| 1131 | PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head)));
|
|---|
| 1132 |
|
|---|
| 1133 | TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
|
|---|
| 1134 | Py_INCREF(TestError);
|
|---|
| 1135 | PyModule_AddObject(m, "error", TestError);
|
|---|
| 1136 | }
|
|---|