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