LCOV - code coverage report
Current view: top level - Modules - _testcapimodule.c (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 82 1627 5.0 %
Date: 2023-03-20 08:15:36 Functions: 1 154 0.6 %
Branches: 27 910 3.0 %

           Branch data     Line data    Source code
       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                 :            : /* This module tests the public (Include/ and Include/cpython/) C API.
       9                 :            :    The internal C API must not be used here: use _testinternalcapi for that.
      10                 :            : 
      11                 :            :    The Visual Studio projects builds _testcapi with Py_BUILD_CORE_MODULE
      12                 :            :    macro defined, but only the public C API must be tested here. */
      13                 :            : 
      14                 :            : #undef Py_BUILD_CORE_MODULE
      15                 :            : #undef Py_BUILD_CORE_BUILTIN
      16                 :            : 
      17                 :            : /* Always enable assertions */
      18                 :            : #undef NDEBUG
      19                 :            : 
      20                 :            : #define PY_SSIZE_T_CLEAN
      21                 :            : 
      22                 :            : #include "Python.h"
      23                 :            : #include "frameobject.h"          // PyFrame_New
      24                 :            : #include "marshal.h"              // PyMarshal_WriteLongToFile
      25                 :            : #include "structmember.h"         // for offsetof(), T_OBJECT
      26                 :            : #include <float.h>                // FLT_MAX
      27                 :            : #include <signal.h>
      28                 :            : #ifndef MS_WINDOWS
      29                 :            : #include <unistd.h>
      30                 :            : #endif
      31                 :            : 
      32                 :            : #ifdef HAVE_SYS_WAIT_H
      33                 :            : #include <sys/wait.h>             // W_STOPCODE
      34                 :            : #endif
      35                 :            : 
      36                 :            : #ifdef Py_BUILD_CORE
      37                 :            : #  error "_testcapi must test the public Python C API, not CPython internal C API"
      38                 :            : #endif
      39                 :            : 
      40                 :            : #ifdef bool
      41                 :            : #  error "The public headers should not include <stdbool.h>, see bpo-46748"
      42                 :            : #endif
      43                 :            : 
      44                 :            : // Several parts of this module are broken out into files in _testcapi/.
      45                 :            : // Include definitions from there.
      46                 :            : #include "_testcapi/parts.h"
      47                 :            : 
      48                 :            : // Forward declarations
      49                 :            : static struct PyModuleDef _testcapimodule;
      50                 :            : static PyObject *TestError;     /* set to exception object in init */
      51                 :            : 
      52                 :            : 
      53                 :            : /* Raise TestError with test_name + ": " + msg, and return NULL. */
      54                 :            : 
      55                 :            : static PyObject *
      56                 :          0 : raiseTestError(const char* test_name, const char* msg)
      57                 :            : {
      58                 :          0 :     PyErr_Format(TestError, "%s: %s", test_name, msg);
      59                 :          0 :     return NULL;
      60                 :            : }
      61                 :            : 
      62                 :            : /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
      63                 :            : 
      64                 :            :    The ones derived from autoconf on the UNIX-like OSes can be relied
      65                 :            :    upon (in the absence of sloppy cross-compiling), but the Windows
      66                 :            :    platforms have these hardcoded.  Better safe than sorry.
      67                 :            : */
      68                 :            : static PyObject*
      69                 :          0 : sizeof_error(const char* fatname, const char* typname,
      70                 :            :     int expected, int got)
      71                 :            : {
      72                 :          0 :     PyErr_Format(TestError,
      73                 :            :         "%s #define == %d but sizeof(%s) == %d",
      74                 :            :         fatname, expected, typname, got);
      75                 :          0 :     return (PyObject*)NULL;
      76                 :            : }
      77                 :            : 
      78                 :            : static PyObject*
      79                 :          0 : test_config(PyObject *self, PyObject *Py_UNUSED(ignored))
      80                 :            : {
      81                 :            : #define CHECK_SIZEOF(FATNAME, TYPE) \
      82                 :            :             if (FATNAME != sizeof(TYPE)) \
      83                 :            :                 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
      84                 :            : 
      85                 :            :     CHECK_SIZEOF(SIZEOF_SHORT, short);
      86                 :            :     CHECK_SIZEOF(SIZEOF_INT, int);
      87                 :            :     CHECK_SIZEOF(SIZEOF_LONG, long);
      88                 :            :     CHECK_SIZEOF(SIZEOF_VOID_P, void*);
      89                 :            :     CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
      90                 :            :     CHECK_SIZEOF(SIZEOF_LONG_LONG, long long);
      91                 :            : 
      92                 :            : #undef CHECK_SIZEOF
      93                 :            : 
      94                 :          0 :     Py_RETURN_NONE;
      95                 :            : }
      96                 :            : 
      97                 :            : static PyObject*
      98                 :          0 : test_sizeof_c_types(PyObject *self, PyObject *Py_UNUSED(ignored))
      99                 :            : {
     100                 :            : #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
     101                 :            : #pragma GCC diagnostic push
     102                 :            : #pragma GCC diagnostic ignored "-Wtype-limits"
     103                 :            : #endif
     104                 :            : #define CHECK_SIZEOF(TYPE, EXPECTED)         \
     105                 :            :     if (EXPECTED != sizeof(TYPE))  {         \
     106                 :            :         PyErr_Format(TestError,              \
     107                 :            :             "sizeof(%s) = %u instead of %u", \
     108                 :            :             #TYPE, sizeof(TYPE), EXPECTED);  \
     109                 :            :         return (PyObject*)NULL;              \
     110                 :            :     }
     111                 :            : #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0)
     112                 :            : #define CHECK_SIGNNESS(TYPE, SIGNED)         \
     113                 :            :     if (IS_SIGNED(TYPE) != SIGNED) {         \
     114                 :            :         PyErr_Format(TestError,              \
     115                 :            :             "%s signness is, instead of %i",  \
     116                 :            :             #TYPE, IS_SIGNED(TYPE), SIGNED); \
     117                 :            :         return (PyObject*)NULL;              \
     118                 :            :     }
     119                 :            : 
     120                 :            :     /* integer types */
     121                 :            :     CHECK_SIZEOF(Py_UCS1, 1);
     122                 :            :     CHECK_SIZEOF(Py_UCS2, 2);
     123                 :            :     CHECK_SIZEOF(Py_UCS4, 4);
     124                 :            :     CHECK_SIGNNESS(Py_UCS1, 0);
     125                 :            :     CHECK_SIGNNESS(Py_UCS2, 0);
     126                 :            :     CHECK_SIGNNESS(Py_UCS4, 0);
     127                 :            :     CHECK_SIZEOF(int32_t, 4);
     128                 :            :     CHECK_SIGNNESS(int32_t, 1);
     129                 :            :     CHECK_SIZEOF(uint32_t, 4);
     130                 :            :     CHECK_SIGNNESS(uint32_t, 0);
     131                 :            :     CHECK_SIZEOF(int64_t, 8);
     132                 :            :     CHECK_SIGNNESS(int64_t, 1);
     133                 :            :     CHECK_SIZEOF(uint64_t, 8);
     134                 :            :     CHECK_SIGNNESS(uint64_t, 0);
     135                 :            : 
     136                 :            :     /* pointer/size types */
     137                 :            :     CHECK_SIZEOF(size_t, sizeof(void *));
     138                 :            :     CHECK_SIGNNESS(size_t, 0);
     139                 :            :     CHECK_SIZEOF(Py_ssize_t, sizeof(void *));
     140                 :            :     CHECK_SIGNNESS(Py_ssize_t, 1);
     141                 :            : 
     142                 :            :     CHECK_SIZEOF(uintptr_t, sizeof(void *));
     143                 :            :     CHECK_SIGNNESS(uintptr_t, 0);
     144                 :            :     CHECK_SIZEOF(intptr_t, sizeof(void *));
     145                 :            :     CHECK_SIGNNESS(intptr_t, 1);
     146                 :            : 
     147                 :          0 :     Py_RETURN_NONE;
     148                 :            : 
     149                 :            : #undef IS_SIGNED
     150                 :            : #undef CHECK_SIGNESS
     151                 :            : #undef CHECK_SIZEOF
     152                 :            : #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
     153                 :            : #pragma GCC diagnostic pop
     154                 :            : #endif
     155                 :            : }
     156                 :            : 
     157                 :            : static PyObject*
     158                 :          0 : test_gc_control(PyObject *self, PyObject *Py_UNUSED(ignored))
     159                 :            : {
     160                 :          0 :     int orig_enabled = PyGC_IsEnabled();
     161                 :          0 :     const char* msg = "ok";
     162                 :            :     int old_state;
     163                 :            : 
     164                 :          0 :     old_state = PyGC_Enable();
     165                 :          0 :     msg = "Enable(1)";
     166         [ #  # ]:          0 :     if (old_state != orig_enabled) {
     167                 :          0 :         goto failed;
     168                 :            :     }
     169                 :          0 :     msg = "IsEnabled(1)";
     170         [ #  # ]:          0 :     if (!PyGC_IsEnabled()) {
     171                 :          0 :         goto failed;
     172                 :            :     }
     173                 :            : 
     174                 :          0 :     old_state = PyGC_Disable();
     175                 :          0 :     msg = "disable(2)";
     176         [ #  # ]:          0 :     if (!old_state) {
     177                 :          0 :         goto failed;
     178                 :            :     }
     179                 :          0 :     msg = "IsEnabled(2)";
     180         [ #  # ]:          0 :     if (PyGC_IsEnabled()) {
     181                 :          0 :         goto failed;
     182                 :            :     }
     183                 :            : 
     184                 :          0 :     old_state = PyGC_Enable();
     185                 :          0 :     msg = "enable(3)";
     186         [ #  # ]:          0 :     if (old_state) {
     187                 :          0 :         goto failed;
     188                 :            :     }
     189                 :          0 :     msg = "IsEnabled(3)";
     190         [ #  # ]:          0 :     if (!PyGC_IsEnabled()) {
     191                 :          0 :         goto failed;
     192                 :            :     }
     193                 :            : 
     194         [ #  # ]:          0 :     if (!orig_enabled) {
     195                 :          0 :         old_state = PyGC_Disable();
     196                 :          0 :         msg = "disable(4)";
     197         [ #  # ]:          0 :         if (old_state) {
     198                 :          0 :             goto failed;
     199                 :            :         }
     200                 :          0 :         msg = "IsEnabled(4)";
     201         [ #  # ]:          0 :         if (PyGC_IsEnabled()) {
     202                 :          0 :             goto failed;
     203                 :            :         }
     204                 :            :     }
     205                 :            : 
     206                 :          0 :     Py_RETURN_NONE;
     207                 :            : 
     208                 :          0 : failed:
     209                 :            :     /* Try to clean up if we can. */
     210         [ #  # ]:          0 :     if (orig_enabled) {
     211                 :          0 :         PyGC_Enable();
     212                 :            :     } else {
     213                 :          0 :         PyGC_Disable();
     214                 :            :     }
     215                 :          0 :     PyErr_Format(TestError, "GC control failed in %s", msg);
     216                 :          0 :     return NULL;
     217                 :            : }
     218                 :            : 
     219                 :            : static PyObject*
     220                 :          0 : test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored))
     221                 :            : {
     222                 :            :     PyObject* list;
     223                 :            :     int i;
     224                 :            : 
     225                 :            :     /* SF bug 132008:  PyList_Reverse segfaults */
     226                 :            : #define NLIST 30
     227                 :          0 :     list = PyList_New(NLIST);
     228         [ #  # ]:          0 :     if (list == (PyObject*)NULL)
     229                 :          0 :         return (PyObject*)NULL;
     230                 :            :     /* list = range(NLIST) */
     231         [ #  # ]:          0 :     for (i = 0; i < NLIST; ++i) {
     232                 :          0 :         PyObject* anint = PyLong_FromLong(i);
     233         [ #  # ]:          0 :         if (anint == (PyObject*)NULL) {
     234                 :          0 :             Py_DECREF(list);
     235                 :          0 :             return (PyObject*)NULL;
     236                 :            :         }
     237                 :          0 :         PyList_SET_ITEM(list, i, anint);
     238                 :            :     }
     239                 :            :     /* list.reverse(), via PyList_Reverse() */
     240                 :          0 :     i = PyList_Reverse(list);   /* should not blow up! */
     241         [ #  # ]:          0 :     if (i != 0) {
     242                 :          0 :         Py_DECREF(list);
     243                 :          0 :         return (PyObject*)NULL;
     244                 :            :     }
     245                 :            :     /* Check that list == range(29, -1, -1) now */
     246         [ #  # ]:          0 :     for (i = 0; i < NLIST; ++i) {
     247         [ #  # ]:          0 :         PyObject* anint = PyList_GET_ITEM(list, i);
     248         [ #  # ]:          0 :         if (PyLong_AS_LONG(anint) != NLIST-1-i) {
     249                 :          0 :             PyErr_SetString(TestError,
     250                 :            :                             "test_list_api: reverse screwed up");
     251                 :          0 :             Py_DECREF(list);
     252                 :          0 :             return (PyObject*)NULL;
     253                 :            :         }
     254                 :            :     }
     255                 :          0 :     Py_DECREF(list);
     256                 :            : #undef NLIST
     257                 :            : 
     258                 :          0 :     Py_RETURN_NONE;
     259                 :            : }
     260                 :            : 
     261                 :            : static int
     262                 :          0 : test_dict_inner(int count)
     263                 :            : {
     264                 :          0 :     Py_ssize_t pos = 0, iterations = 0;
     265                 :            :     int i;
     266                 :          0 :     PyObject *dict = PyDict_New();
     267                 :            :     PyObject *v, *k;
     268                 :            : 
     269         [ #  # ]:          0 :     if (dict == NULL)
     270                 :          0 :         return -1;
     271                 :            : 
     272         [ #  # ]:          0 :     for (i = 0; i < count; i++) {
     273                 :          0 :         v = PyLong_FromLong(i);
     274         [ #  # ]:          0 :         if (v == NULL) {
     275                 :          0 :             return -1;
     276                 :            :         }
     277         [ #  # ]:          0 :         if (PyDict_SetItem(dict, v, v) < 0) {
     278                 :          0 :             Py_DECREF(v);
     279                 :          0 :             return -1;
     280                 :            :         }
     281                 :          0 :         Py_DECREF(v);
     282                 :            :     }
     283                 :            : 
     284         [ #  # ]:          0 :     while (PyDict_Next(dict, &pos, &k, &v)) {
     285                 :            :         PyObject *o;
     286                 :          0 :         iterations++;
     287                 :            : 
     288                 :          0 :         i = PyLong_AS_LONG(v) + 1;
     289                 :          0 :         o = PyLong_FromLong(i);
     290         [ #  # ]:          0 :         if (o == NULL)
     291                 :          0 :             return -1;
     292         [ #  # ]:          0 :         if (PyDict_SetItem(dict, k, o) < 0) {
     293                 :          0 :             Py_DECREF(o);
     294                 :          0 :             return -1;
     295                 :            :         }
     296                 :          0 :         Py_DECREF(o);
     297                 :            :     }
     298                 :            : 
     299                 :          0 :     Py_DECREF(dict);
     300                 :            : 
     301         [ #  # ]:          0 :     if (iterations != count) {
     302                 :          0 :         PyErr_SetString(
     303                 :            :             TestError,
     304                 :            :             "test_dict_iteration: dict iteration went wrong ");
     305                 :          0 :         return -1;
     306                 :            :     } else {
     307                 :          0 :         return 0;
     308                 :            :     }
     309                 :            : }
     310                 :            : 
     311                 :            : 
     312                 :            : 
     313                 :            : static PyObject*
     314                 :          0 : test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored))
     315                 :            : {
     316                 :            :     int i;
     317                 :            : 
     318         [ #  # ]:          0 :     for (i = 0; i < 200; i++) {
     319         [ #  # ]:          0 :         if (test_dict_inner(i) < 0) {
     320                 :          0 :             return NULL;
     321                 :            :         }
     322                 :            :     }
     323                 :            : 
     324                 :          0 :     Py_RETURN_NONE;
     325                 :            : }
     326                 :            : 
     327                 :            : static PyObject*
     328                 :          0 : dict_getitem_knownhash(PyObject *self, PyObject *args)
     329                 :            : {
     330                 :            :     PyObject *mp, *key, *result;
     331                 :            :     Py_ssize_t hash;
     332                 :            : 
     333         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
     334                 :            :                           &mp, &key, &hash)) {
     335                 :          0 :         return NULL;
     336                 :            :     }
     337                 :            : 
     338                 :          0 :     result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
     339   [ #  #  #  # ]:          0 :     if (result == NULL && !PyErr_Occurred()) {
     340                 :          0 :         _PyErr_SetKeyError(key);
     341                 :          0 :         return NULL;
     342                 :            :     }
     343                 :            : 
     344                 :          0 :     return Py_XNewRef(result);
     345                 :            : }
     346                 :            : 
     347                 :            : /* Issue #4701: Check that PyObject_Hash implicitly calls
     348                 :            :  *   PyType_Ready if it hasn't already been called
     349                 :            :  */
     350                 :            : static PyTypeObject _HashInheritanceTester_Type = {
     351                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
     352                 :            :     "hashinheritancetester",            /* Name of this type */
     353                 :            :     sizeof(PyObject),           /* Basic object size */
     354                 :            :     0,                          /* Item size for varobject */
     355                 :            :     (destructor)PyObject_Del, /* tp_dealloc */
     356                 :            :     0,                          /* tp_vectorcall_offset */
     357                 :            :     0,                          /* tp_getattr */
     358                 :            :     0,                          /* tp_setattr */
     359                 :            :     0,                          /* tp_as_async */
     360                 :            :     0,                          /* tp_repr */
     361                 :            :     0,                          /* tp_as_number */
     362                 :            :     0,                          /* tp_as_sequence */
     363                 :            :     0,                          /* tp_as_mapping */
     364                 :            :     0,                          /* tp_hash */
     365                 :            :     0,                          /* tp_call */
     366                 :            :     0,                          /* tp_str */
     367                 :            :     PyObject_GenericGetAttr,  /* tp_getattro */
     368                 :            :     0,                          /* tp_setattro */
     369                 :            :     0,                          /* tp_as_buffer */
     370                 :            :     Py_TPFLAGS_DEFAULT,         /* tp_flags */
     371                 :            :     0,                          /* tp_doc */
     372                 :            :     0,                          /* tp_traverse */
     373                 :            :     0,                          /* tp_clear */
     374                 :            :     0,                          /* tp_richcompare */
     375                 :            :     0,                          /* tp_weaklistoffset */
     376                 :            :     0,                          /* tp_iter */
     377                 :            :     0,                          /* tp_iternext */
     378                 :            :     0,                          /* tp_methods */
     379                 :            :     0,                          /* tp_members */
     380                 :            :     0,                          /* tp_getset */
     381                 :            :     0,                          /* tp_base */
     382                 :            :     0,                          /* tp_dict */
     383                 :            :     0,                          /* tp_descr_get */
     384                 :            :     0,                          /* tp_descr_set */
     385                 :            :     0,                          /* tp_dictoffset */
     386                 :            :     0,                          /* tp_init */
     387                 :            :     0,                          /* tp_alloc */
     388                 :            :     PyType_GenericNew,                  /* tp_new */
     389                 :            : };
     390                 :            : 
     391                 :            : static PyObject*
     392                 :          0 : pycompilestring(PyObject* self, PyObject *obj) {
     393         [ #  # ]:          0 :     if (PyBytes_CheckExact(obj) == 0) {
     394                 :          0 :         PyErr_SetString(PyExc_ValueError, "Argument must be a bytes object");
     395                 :          0 :         return NULL;
     396                 :            :     }
     397                 :          0 :     const char *the_string = PyBytes_AsString(obj);
     398         [ #  # ]:          0 :     if (the_string == NULL) {
     399                 :          0 :         return NULL;
     400                 :            :     }
     401                 :          0 :     return Py_CompileString(the_string, "<string>", Py_file_input);
     402                 :            : }
     403                 :            : 
     404                 :            : static PyObject*
     405                 :          0 : test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
     406                 :            : {
     407                 :            :     PyTypeObject *type;
     408                 :            :     PyObject *obj;
     409                 :            :     Py_hash_t hash;
     410                 :            : 
     411                 :          0 :     type = &_HashInheritanceTester_Type;
     412                 :            : 
     413         [ #  # ]:          0 :     if (type->tp_dict != NULL)
     414                 :            :         /* The type has already been initialized. This probably means
     415                 :            :            -R is being used. */
     416                 :          0 :         Py_RETURN_NONE;
     417                 :            : 
     418                 :            : 
     419                 :          0 :     obj = PyObject_New(PyObject, type);
     420         [ #  # ]:          0 :     if (obj == NULL) {
     421                 :          0 :         PyErr_Clear();
     422                 :          0 :         PyErr_SetString(
     423                 :            :             TestError,
     424                 :            :             "test_lazy_hash_inheritance: failed to create object");
     425                 :          0 :         return NULL;
     426                 :            :     }
     427                 :            : 
     428         [ #  # ]:          0 :     if (type->tp_dict != NULL) {
     429                 :          0 :         PyErr_SetString(
     430                 :            :             TestError,
     431                 :            :             "test_lazy_hash_inheritance: type initialised too soon");
     432                 :          0 :         Py_DECREF(obj);
     433                 :          0 :         return NULL;
     434                 :            :     }
     435                 :            : 
     436                 :          0 :     hash = PyObject_Hash(obj);
     437   [ #  #  #  # ]:          0 :     if ((hash == -1) && PyErr_Occurred()) {
     438                 :          0 :         PyErr_Clear();
     439                 :          0 :         PyErr_SetString(
     440                 :            :             TestError,
     441                 :            :             "test_lazy_hash_inheritance: could not hash object");
     442                 :          0 :         Py_DECREF(obj);
     443                 :          0 :         return NULL;
     444                 :            :     }
     445                 :            : 
     446         [ #  # ]:          0 :     if (type->tp_dict == NULL) {
     447                 :          0 :         PyErr_SetString(
     448                 :            :             TestError,
     449                 :            :             "test_lazy_hash_inheritance: type not initialised by hash()");
     450                 :          0 :         Py_DECREF(obj);
     451                 :          0 :         return NULL;
     452                 :            :     }
     453                 :            : 
     454         [ #  # ]:          0 :     if (type->tp_hash != PyType_Type.tp_hash) {
     455                 :          0 :         PyErr_SetString(
     456                 :            :             TestError,
     457                 :            :             "test_lazy_hash_inheritance: unexpected hash function");
     458                 :          0 :         Py_DECREF(obj);
     459                 :          0 :         return NULL;
     460                 :            :     }
     461                 :            : 
     462                 :          0 :     Py_DECREF(obj);
     463                 :            : 
     464                 :          0 :     Py_RETURN_NONE;
     465                 :            : }
     466                 :            : 
     467                 :            : static PyObject *
     468                 :          0 : return_none(void *unused)
     469                 :            : {
     470                 :          0 :     Py_RETURN_NONE;
     471                 :            : }
     472                 :            : 
     473                 :            : static PyObject *
     474                 :          0 : raise_error(void *unused)
     475                 :            : {
     476                 :          0 :     PyErr_SetNone(PyExc_ValueError);
     477                 :          0 :     return NULL;
     478                 :            : }
     479                 :            : 
     480                 :            : static int
     481                 :          0 : test_buildvalue_N_error(const char *fmt)
     482                 :            : {
     483                 :            :     PyObject *arg, *res;
     484                 :            : 
     485                 :          0 :     arg = PyList_New(0);
     486         [ #  # ]:          0 :     if (arg == NULL) {
     487                 :          0 :         return -1;
     488                 :            :     }
     489                 :            : 
     490                 :          0 :     Py_INCREF(arg);
     491                 :          0 :     res = Py_BuildValue(fmt, return_none, NULL, arg);
     492         [ #  # ]:          0 :     if (res == NULL) {
     493                 :          0 :         return -1;
     494                 :            :     }
     495                 :          0 :     Py_DECREF(res);
     496         [ #  # ]:          0 :     if (Py_REFCNT(arg) != 1) {
     497                 :          0 :         PyErr_Format(TestError, "test_buildvalue_N: "
     498                 :            :                      "arg was not decrefed in successful "
     499                 :            :                      "Py_BuildValue(\"%s\")", fmt);
     500                 :          0 :         return -1;
     501                 :            :     }
     502                 :            : 
     503                 :          0 :     Py_INCREF(arg);
     504                 :          0 :     res = Py_BuildValue(fmt, raise_error, NULL, arg);
     505   [ #  #  #  # ]:          0 :     if (res != NULL || !PyErr_Occurred()) {
     506                 :          0 :         PyErr_Format(TestError, "test_buildvalue_N: "
     507                 :            :                      "Py_BuildValue(\"%s\") didn't complain", fmt);
     508                 :          0 :         return -1;
     509                 :            :     }
     510                 :          0 :     PyErr_Clear();
     511         [ #  # ]:          0 :     if (Py_REFCNT(arg) != 1) {
     512                 :          0 :         PyErr_Format(TestError, "test_buildvalue_N: "
     513                 :            :                      "arg was not decrefed in failed "
     514                 :            :                      "Py_BuildValue(\"%s\")", fmt);
     515                 :          0 :         return -1;
     516                 :            :     }
     517                 :          0 :     Py_DECREF(arg);
     518                 :          0 :     return 0;
     519                 :            : }
     520                 :            : 
     521                 :            : static PyObject *
     522                 :          0 : test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
     523                 :            : {
     524                 :            :     PyObject *arg, *res;
     525                 :            : 
     526                 :          0 :     arg = PyList_New(0);
     527         [ #  # ]:          0 :     if (arg == NULL) {
     528                 :          0 :         return NULL;
     529                 :            :     }
     530                 :          0 :     Py_INCREF(arg);
     531                 :          0 :     res = Py_BuildValue("N", arg);
     532         [ #  # ]:          0 :     if (res == NULL) {
     533                 :          0 :         return NULL;
     534                 :            :     }
     535         [ #  # ]:          0 :     if (res != arg) {
     536                 :          0 :         return raiseTestError("test_buildvalue_N",
     537                 :            :                               "Py_BuildValue(\"N\") returned wrong result");
     538                 :            :     }
     539         [ #  # ]:          0 :     if (Py_REFCNT(arg) != 2) {
     540                 :          0 :         return raiseTestError("test_buildvalue_N",
     541                 :            :                               "arg was not decrefed in Py_BuildValue(\"N\")");
     542                 :            :     }
     543                 :          0 :     Py_DECREF(res);
     544                 :          0 :     Py_DECREF(arg);
     545                 :            : 
     546         [ #  # ]:          0 :     if (test_buildvalue_N_error("O&N") < 0)
     547                 :          0 :         return NULL;
     548         [ #  # ]:          0 :     if (test_buildvalue_N_error("(O&N)") < 0)
     549                 :          0 :         return NULL;
     550         [ #  # ]:          0 :     if (test_buildvalue_N_error("[O&N]") < 0)
     551                 :          0 :         return NULL;
     552         [ #  # ]:          0 :     if (test_buildvalue_N_error("{O&N}") < 0)
     553                 :          0 :         return NULL;
     554         [ #  # ]:          0 :     if (test_buildvalue_N_error("{()O&(())N}") < 0)
     555                 :          0 :         return NULL;
     556                 :            : 
     557                 :          0 :     Py_RETURN_NONE;
     558                 :            : }
     559                 :            : 
     560                 :            : 
     561                 :            : static PyObject *
     562                 :          0 : test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored))
     563                 :            : {
     564                 :          0 :     newfunc tp_new = PyType_GetSlot(&PyLong_Type, Py_tp_new);
     565         [ #  # ]:          0 :     if (PyLong_Type.tp_new != tp_new) {
     566                 :          0 :         PyErr_SetString(PyExc_AssertionError, "mismatch: tp_new of long");
     567                 :          0 :         return NULL;
     568                 :            :     }
     569                 :            : 
     570                 :          0 :     reprfunc tp_repr = PyType_GetSlot(&PyLong_Type, Py_tp_repr);
     571         [ #  # ]:          0 :     if (PyLong_Type.tp_repr != tp_repr) {
     572                 :          0 :         PyErr_SetString(PyExc_AssertionError, "mismatch: tp_repr of long");
     573                 :          0 :         return NULL;
     574                 :            :     }
     575                 :            : 
     576                 :          0 :     ternaryfunc tp_call = PyType_GetSlot(&PyLong_Type, Py_tp_call);
     577         [ #  # ]:          0 :     if (tp_call != NULL) {
     578                 :          0 :         PyErr_SetString(PyExc_AssertionError, "mismatch: tp_call of long");
     579                 :          0 :         return NULL;
     580                 :            :     }
     581                 :            : 
     582                 :          0 :     binaryfunc nb_add = PyType_GetSlot(&PyLong_Type, Py_nb_add);
     583         [ #  # ]:          0 :     if (PyLong_Type.tp_as_number->nb_add != nb_add) {
     584                 :          0 :         PyErr_SetString(PyExc_AssertionError, "mismatch: nb_add of long");
     585                 :          0 :         return NULL;
     586                 :            :     }
     587                 :            : 
     588                 :          0 :     lenfunc mp_length = PyType_GetSlot(&PyLong_Type, Py_mp_length);
     589         [ #  # ]:          0 :     if (mp_length != NULL) {
     590                 :          0 :         PyErr_SetString(PyExc_AssertionError, "mismatch: mp_length of long");
     591                 :          0 :         return NULL;
     592                 :            :     }
     593                 :            : 
     594                 :          0 :     void *over_value = PyType_GetSlot(&PyLong_Type, Py_bf_releasebuffer + 1);
     595         [ #  # ]:          0 :     if (over_value != NULL) {
     596                 :          0 :         PyErr_SetString(PyExc_AssertionError, "mismatch: max+1 of long");
     597                 :          0 :         return NULL;
     598                 :            :     }
     599                 :            : 
     600                 :          0 :     tp_new = PyType_GetSlot(&PyLong_Type, 0);
     601         [ #  # ]:          0 :     if (tp_new != NULL) {
     602                 :          0 :         PyErr_SetString(PyExc_AssertionError, "mismatch: slot 0 of long");
     603                 :          0 :         return NULL;
     604                 :            :     }
     605         [ #  # ]:          0 :     if (PyErr_ExceptionMatches(PyExc_SystemError)) {
     606                 :            :         // This is the right exception
     607                 :          0 :         PyErr_Clear();
     608                 :            :     }
     609                 :            :     else {
     610                 :          0 :         return NULL;
     611                 :            :     }
     612                 :            : 
     613                 :          0 :     Py_RETURN_NONE;
     614                 :            : }
     615                 :            : 
     616                 :            : 
     617                 :            : static PyType_Slot HeapTypeNameType_slots[] = {
     618                 :            :     {0},
     619                 :            : };
     620                 :            : 
     621                 :            : static PyType_Spec HeapTypeNameType_Spec = {
     622                 :            :     .name = "_testcapi.HeapTypeNameType",
     623                 :            :     .basicsize = sizeof(PyObject),
     624                 :            :     .flags = Py_TPFLAGS_DEFAULT,
     625                 :            :     .slots = HeapTypeNameType_slots,
     626                 :            : };
     627                 :            : 
     628                 :            : static PyObject *
     629                 :          0 : test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
     630                 :            : {
     631                 :          0 :     PyObject *tp_name = PyType_GetName(&PyLong_Type);
     632         [ #  # ]:          0 :     assert(strcmp(PyUnicode_AsUTF8(tp_name), "int") == 0);
     633                 :          0 :     Py_DECREF(tp_name);
     634                 :            : 
     635                 :          0 :     tp_name = PyType_GetName(&PyModule_Type);
     636         [ #  # ]:          0 :     assert(strcmp(PyUnicode_AsUTF8(tp_name), "module") == 0);
     637                 :          0 :     Py_DECREF(tp_name);
     638                 :            : 
     639                 :          0 :     PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
     640         [ #  # ]:          0 :     if (HeapTypeNameType == NULL) {
     641                 :          0 :         Py_RETURN_NONE;
     642                 :            :     }
     643                 :          0 :     tp_name = PyType_GetName((PyTypeObject *)HeapTypeNameType);
     644         [ #  # ]:          0 :     assert(strcmp(PyUnicode_AsUTF8(tp_name), "HeapTypeNameType") == 0);
     645                 :          0 :     Py_DECREF(tp_name);
     646                 :            : 
     647                 :          0 :     PyObject *name = PyUnicode_FromString("test_name");
     648         [ #  # ]:          0 :     if (name == NULL) {
     649                 :          0 :         goto done;
     650                 :            :     }
     651         [ #  # ]:          0 :     if (PyObject_SetAttrString(HeapTypeNameType, "__name__", name) < 0) {
     652                 :          0 :         Py_DECREF(name);
     653                 :          0 :         goto done;
     654                 :            :     }
     655                 :          0 :     tp_name = PyType_GetName((PyTypeObject *)HeapTypeNameType);
     656         [ #  # ]:          0 :     assert(strcmp(PyUnicode_AsUTF8(tp_name), "test_name") == 0);
     657                 :          0 :     Py_DECREF(name);
     658                 :          0 :     Py_DECREF(tp_name);
     659                 :            : 
     660                 :          0 :   done:
     661                 :          0 :     Py_DECREF(HeapTypeNameType);
     662                 :          0 :     Py_RETURN_NONE;
     663                 :            : }
     664                 :            : 
     665                 :            : 
     666                 :            : static PyObject *
     667                 :          0 : test_get_type_qualname(PyObject *self, PyObject *Py_UNUSED(ignored))
     668                 :            : {
     669                 :          0 :     PyObject *tp_qualname = PyType_GetQualName(&PyLong_Type);
     670         [ #  # ]:          0 :     assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "int") == 0);
     671                 :          0 :     Py_DECREF(tp_qualname);
     672                 :            : 
     673                 :          0 :     tp_qualname = PyType_GetQualName(&PyODict_Type);
     674         [ #  # ]:          0 :     assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "OrderedDict") == 0);
     675                 :          0 :     Py_DECREF(tp_qualname);
     676                 :            : 
     677                 :          0 :     PyObject *HeapTypeNameType = PyType_FromSpec(&HeapTypeNameType_Spec);
     678         [ #  # ]:          0 :     if (HeapTypeNameType == NULL) {
     679                 :          0 :         Py_RETURN_NONE;
     680                 :            :     }
     681                 :          0 :     tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
     682         [ #  # ]:          0 :     assert(strcmp(PyUnicode_AsUTF8(tp_qualname), "HeapTypeNameType") == 0);
     683                 :          0 :     Py_DECREF(tp_qualname);
     684                 :            : 
     685                 :          0 :     PyObject *spec_name = PyUnicode_FromString(HeapTypeNameType_Spec.name);
     686         [ #  # ]:          0 :     if (spec_name == NULL) {
     687                 :          0 :         goto done;
     688                 :            :     }
     689         [ #  # ]:          0 :     if (PyObject_SetAttrString(HeapTypeNameType,
     690                 :            :                                "__qualname__", spec_name) < 0) {
     691                 :          0 :         Py_DECREF(spec_name);
     692                 :          0 :         goto done;
     693                 :            :     }
     694                 :          0 :     tp_qualname = PyType_GetQualName((PyTypeObject *)HeapTypeNameType);
     695         [ #  # ]:          0 :     assert(strcmp(PyUnicode_AsUTF8(tp_qualname),
     696                 :            :                   "_testcapi.HeapTypeNameType") == 0);
     697                 :          0 :     Py_DECREF(spec_name);
     698                 :          0 :     Py_DECREF(tp_qualname);
     699                 :            : 
     700                 :          0 :   done:
     701                 :          0 :     Py_DECREF(HeapTypeNameType);
     702                 :          0 :     Py_RETURN_NONE;
     703                 :            : }
     704                 :            : 
     705                 :            : static PyObject *
     706                 :          0 : pyobject_repr_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
     707                 :            : {
     708                 :          0 :     return PyObject_Repr(NULL);
     709                 :            : }
     710                 :            : 
     711                 :            : static PyObject *
     712                 :          0 : pyobject_str_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
     713                 :            : {
     714                 :          0 :     return PyObject_Str(NULL);
     715                 :            : }
     716                 :            : 
     717                 :            : static PyObject *
     718                 :          0 : pyobject_bytes_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
     719                 :            : {
     720                 :          0 :     return PyObject_Bytes(NULL);
     721                 :            : }
     722                 :            : 
     723                 :            : static PyObject *
     724                 :          0 : set_errno(PyObject *self, PyObject *args)
     725                 :            : {
     726                 :            :     int new_errno;
     727                 :            : 
     728         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno))
     729                 :          0 :         return NULL;
     730                 :            : 
     731                 :          0 :     errno = new_errno;
     732                 :          0 :     Py_RETURN_NONE;
     733                 :            : }
     734                 :            : 
     735                 :            : /* test_thread_state spawns a thread of its own, and that thread releases
     736                 :            :  * `thread_done` when it's finished.  The driver code has to know when the
     737                 :            :  * thread finishes, because the thread uses a PyObject (the callable) that
     738                 :            :  * may go away when the driver finishes.  The former lack of this explicit
     739                 :            :  * synchronization caused rare segfaults, so rare that they were seen only
     740                 :            :  * on a Mac buildbot (although they were possible on any box).
     741                 :            :  */
     742                 :            : static PyThread_type_lock thread_done = NULL;
     743                 :            : 
     744                 :            : static int
     745                 :          0 : _make_call(void *callable)
     746                 :            : {
     747                 :            :     PyObject *rc;
     748                 :            :     int success;
     749                 :          0 :     PyGILState_STATE s = PyGILState_Ensure();
     750                 :          0 :     rc = PyObject_CallNoArgs((PyObject *)callable);
     751                 :          0 :     success = (rc != NULL);
     752                 :          0 :     Py_XDECREF(rc);
     753                 :          0 :     PyGILState_Release(s);
     754                 :          0 :     return success;
     755                 :            : }
     756                 :            : 
     757                 :            : /* Same thing, but releases `thread_done` when it returns.  This variant
     758                 :            :  * should be called only from threads spawned by test_thread_state().
     759                 :            :  */
     760                 :            : static void
     761                 :          0 : _make_call_from_thread(void *callable)
     762                 :            : {
     763                 :          0 :     _make_call(callable);
     764                 :          0 :     PyThread_release_lock(thread_done);
     765                 :          0 : }
     766                 :            : 
     767                 :            : static PyObject *
     768                 :          0 : test_thread_state(PyObject *self, PyObject *args)
     769                 :            : {
     770                 :            :     PyObject *fn;
     771                 :          0 :     int success = 1;
     772                 :            : 
     773         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
     774                 :          0 :         return NULL;
     775                 :            : 
     776         [ #  # ]:          0 :     if (!PyCallable_Check(fn)) {
     777                 :          0 :         PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
     778                 :          0 :             Py_TYPE(fn)->tp_name);
     779                 :          0 :         return NULL;
     780                 :            :     }
     781                 :            : 
     782                 :          0 :     thread_done = PyThread_allocate_lock();
     783         [ #  # ]:          0 :     if (thread_done == NULL)
     784                 :          0 :         return PyErr_NoMemory();
     785                 :          0 :     PyThread_acquire_lock(thread_done, 1);
     786                 :            : 
     787                 :            :     /* Start a new thread with our callback. */
     788                 :          0 :     PyThread_start_new_thread(_make_call_from_thread, fn);
     789                 :            :     /* Make the callback with the thread lock held by this thread */
     790                 :          0 :     success &= _make_call(fn);
     791                 :            :     /* Do it all again, but this time with the thread-lock released */
     792                 :          0 :     Py_BEGIN_ALLOW_THREADS
     793                 :          0 :     success &= _make_call(fn);
     794                 :          0 :     PyThread_acquire_lock(thread_done, 1);  /* wait for thread to finish */
     795                 :          0 :     Py_END_ALLOW_THREADS
     796                 :            : 
     797                 :            :     /* And once more with and without a thread
     798                 :            :        XXX - should use a lock and work out exactly what we are trying
     799                 :            :        to test <wink>
     800                 :            :     */
     801                 :          0 :     Py_BEGIN_ALLOW_THREADS
     802                 :          0 :     PyThread_start_new_thread(_make_call_from_thread, fn);
     803                 :          0 :     success &= _make_call(fn);
     804                 :          0 :     PyThread_acquire_lock(thread_done, 1);  /* wait for thread to finish */
     805                 :          0 :     Py_END_ALLOW_THREADS
     806                 :            : 
     807                 :            :     /* Release lock we acquired above.  This is required on HP-UX. */
     808                 :          0 :     PyThread_release_lock(thread_done);
     809                 :            : 
     810                 :          0 :     PyThread_free_lock(thread_done);
     811         [ #  # ]:          0 :     if (!success)
     812                 :          0 :         return NULL;
     813                 :          0 :     Py_RETURN_NONE;
     814                 :            : }
     815                 :            : 
     816                 :            : #ifndef MS_WINDOWS
     817                 :            : static PyThread_type_lock wait_done = NULL;
     818                 :            : 
     819                 :          0 : static void wait_for_lock(void *unused) {
     820                 :          0 :     PyThread_acquire_lock(wait_done, 1);
     821                 :          0 :     PyThread_release_lock(wait_done);
     822                 :          0 :     PyThread_free_lock(wait_done);
     823                 :          0 :     wait_done = NULL;
     824                 :          0 : }
     825                 :            : 
     826                 :            : // These can be used to test things that care about the existence of another
     827                 :            : // thread that the threading module doesn't know about.
     828                 :            : 
     829                 :            : static PyObject *
     830                 :          0 : spawn_pthread_waiter(PyObject *self, PyObject *Py_UNUSED(ignored))
     831                 :            : {
     832         [ #  # ]:          0 :     if (wait_done) {
     833                 :          0 :         PyErr_SetString(PyExc_RuntimeError, "thread already running");
     834                 :          0 :         return NULL;
     835                 :            :     }
     836                 :          0 :     wait_done = PyThread_allocate_lock();
     837         [ #  # ]:          0 :     if (wait_done == NULL)
     838                 :          0 :         return PyErr_NoMemory();
     839                 :          0 :     PyThread_acquire_lock(wait_done, 1);
     840                 :          0 :     PyThread_start_new_thread(wait_for_lock, NULL);
     841                 :          0 :     Py_RETURN_NONE;
     842                 :            : }
     843                 :            : 
     844                 :            : static PyObject *
     845                 :          0 : end_spawned_pthread(PyObject *self, PyObject *Py_UNUSED(ignored))
     846                 :            : {
     847         [ #  # ]:          0 :     if (!wait_done) {
     848                 :          0 :         PyErr_SetString(PyExc_RuntimeError, "call _spawn_pthread_waiter 1st");
     849                 :          0 :         return NULL;
     850                 :            :     }
     851                 :          0 :     PyThread_release_lock(wait_done);
     852                 :          0 :     Py_RETURN_NONE;
     853                 :            : }
     854                 :            : #endif  // not MS_WINDOWS
     855                 :            : 
     856                 :            : /* test Py_AddPendingCalls using threads */
     857                 :          0 : static int _pending_callback(void *arg)
     858                 :            : {
     859                 :            :     /* we assume the argument is callable object to which we own a reference */
     860                 :          0 :     PyObject *callable = (PyObject *)arg;
     861                 :          0 :     PyObject *r = PyObject_CallNoArgs(callable);
     862                 :          0 :     Py_DECREF(callable);
     863                 :          0 :     Py_XDECREF(r);
     864         [ #  # ]:          0 :     return r != NULL ? 0 : -1;
     865                 :            : }
     866                 :            : 
     867                 :            : /* The following requests n callbacks to _pending_callback.  It can be
     868                 :            :  * run from any python thread.
     869                 :            :  */
     870                 :            : static PyObject *
     871                 :          0 : pending_threadfunc(PyObject *self, PyObject *arg)
     872                 :            : {
     873                 :            :     PyObject *callable;
     874                 :            :     int r;
     875         [ #  # ]:          0 :     if (PyArg_ParseTuple(arg, "O", &callable) == 0)
     876                 :          0 :         return NULL;
     877                 :            : 
     878                 :            :     /* create the reference for the callbackwhile we hold the lock */
     879                 :          0 :     Py_INCREF(callable);
     880                 :            : 
     881                 :          0 :     Py_BEGIN_ALLOW_THREADS
     882                 :          0 :     r = Py_AddPendingCall(&_pending_callback, callable);
     883                 :          0 :     Py_END_ALLOW_THREADS
     884                 :            : 
     885         [ #  # ]:          0 :     if (r<0) {
     886                 :          0 :         Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
     887                 :          0 :         Py_RETURN_FALSE;
     888                 :            :     }
     889                 :          0 :     Py_RETURN_TRUE;
     890                 :            : }
     891                 :            : 
     892                 :            : /* Test PyOS_string_to_double. */
     893                 :            : static PyObject *
     894                 :          0 : test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) {
     895                 :            :     double result;
     896                 :            :     const char *msg;
     897                 :            : 
     898                 :            : #define CHECK_STRING(STR, expected)                             \
     899                 :            :     result = PyOS_string_to_double(STR, NULL, NULL);            \
     900                 :            :     if (result == -1.0 && PyErr_Occurred())                     \
     901                 :            :         return NULL;                                            \
     902                 :            :     if (result != (double)expected) {                           \
     903                 :            :         msg = "conversion of " STR " to float failed";          \
     904                 :            :         goto fail;                                              \
     905                 :            :     }
     906                 :            : 
     907                 :            : #define CHECK_INVALID(STR)                                              \
     908                 :            :     result = PyOS_string_to_double(STR, NULL, NULL);                    \
     909                 :            :     if (result == -1.0 && PyErr_Occurred()) {                           \
     910                 :            :         if (PyErr_ExceptionMatches(PyExc_ValueError))                   \
     911                 :            :             PyErr_Clear();                                              \
     912                 :            :         else                                                            \
     913                 :            :             return NULL;                                                \
     914                 :            :     }                                                                   \
     915                 :            :     else {                                                              \
     916                 :            :         msg = "conversion of " STR " didn't raise ValueError";          \
     917                 :            :         goto fail;                                                      \
     918                 :            :     }
     919                 :            : 
     920   [ #  #  #  #  :          0 :     CHECK_STRING("0.1", 0.1);
                   #  # ]
     921   [ #  #  #  #  :          0 :     CHECK_STRING("1.234", 1.234);
                   #  # ]
     922   [ #  #  #  #  :          0 :     CHECK_STRING("-1.35", -1.35);
                   #  # ]
     923   [ #  #  #  #  :          0 :     CHECK_STRING(".1e01", 1.0);
                   #  # ]
     924   [ #  #  #  #  :          0 :     CHECK_STRING("2.e-2", 0.02);
                   #  # ]
     925                 :            : 
     926   [ #  #  #  #  :          0 :     CHECK_INVALID(" 0.1");
                   #  # ]
     927   [ #  #  #  #  :          0 :     CHECK_INVALID("\t\n-3");
                   #  # ]
     928   [ #  #  #  #  :          0 :     CHECK_INVALID(".123 ");
                   #  # ]
     929   [ #  #  #  #  :          0 :     CHECK_INVALID("3\n");
                   #  # ]
     930   [ #  #  #  #  :          0 :     CHECK_INVALID("123abc");
                   #  # ]
     931                 :            : 
     932                 :          0 :     Py_RETURN_NONE;
     933                 :          0 :   fail:
     934                 :          0 :     return raiseTestError("test_string_to_double", msg);
     935                 :            : #undef CHECK_STRING
     936                 :            : #undef CHECK_INVALID
     937                 :            : }
     938                 :            : 
     939                 :            : 
     940                 :            : /* Coverage testing of capsule objects. */
     941                 :            : 
     942                 :            : static const char *capsule_name = "capsule name";
     943                 :            : static       char *capsule_pointer = "capsule pointer";
     944                 :            : static       char *capsule_context = "capsule context";
     945                 :            : static const char *capsule_error = NULL;
     946                 :            : static int
     947                 :            : capsule_destructor_call_count = 0;
     948                 :            : 
     949                 :            : static void
     950                 :          0 : capsule_destructor(PyObject *o) {
     951                 :          0 :     capsule_destructor_call_count++;
     952         [ #  # ]:          0 :     if (PyCapsule_GetContext(o) != capsule_context) {
     953                 :          0 :         capsule_error = "context did not match in destructor!";
     954         [ #  # ]:          0 :     } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
     955                 :          0 :         capsule_error = "destructor did not match in destructor!  (woah!)";
     956         [ #  # ]:          0 :     } else if (PyCapsule_GetName(o) != capsule_name) {
     957                 :          0 :         capsule_error = "name did not match in destructor!";
     958         [ #  # ]:          0 :     } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
     959                 :          0 :         capsule_error = "pointer did not match in destructor!";
     960                 :            :     }
     961                 :          0 : }
     962                 :            : 
     963                 :            : typedef struct {
     964                 :            :     char *name;
     965                 :            :     char *module;
     966                 :            :     char *attribute;
     967                 :            : } known_capsule;
     968                 :            : 
     969                 :            : static PyObject *
     970                 :          0 : test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored))
     971                 :            : {
     972                 :            :     PyObject *object;
     973                 :          0 :     const char *error = NULL;
     974                 :            :     void *pointer;
     975                 :            :     void *pointer2;
     976                 :          0 :     known_capsule known_capsules[] = {
     977                 :            :         #define KNOWN_CAPSULE(module, name)             { module "." name, module, name }
     978                 :            :         KNOWN_CAPSULE("_socket", "CAPI"),
     979                 :            :         KNOWN_CAPSULE("_curses", "_C_API"),
     980                 :            :         KNOWN_CAPSULE("datetime", "datetime_CAPI"),
     981                 :            :         { NULL, NULL },
     982                 :            :     };
     983                 :          0 :     known_capsule *known = &known_capsules[0];
     984                 :            : 
     985                 :            : #define FAIL(x) { error = (x); goto exit; }
     986                 :            : 
     987                 :            : #define CHECK_DESTRUCTOR \
     988                 :            :     if (capsule_error) { \
     989                 :            :         FAIL(capsule_error); \
     990                 :            :     } \
     991                 :            :     else if (!capsule_destructor_call_count) {          \
     992                 :            :         FAIL("destructor not called!"); \
     993                 :            :     } \
     994                 :            :     capsule_destructor_call_count = 0; \
     995                 :            : 
     996                 :          0 :     object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
     997                 :          0 :     PyCapsule_SetContext(object, capsule_context);
     998                 :          0 :     capsule_destructor(object);
     999   [ #  #  #  # ]:          0 :     CHECK_DESTRUCTOR;
    1000                 :          0 :     Py_DECREF(object);
    1001   [ #  #  #  # ]:          0 :     CHECK_DESTRUCTOR;
    1002                 :            : 
    1003                 :          0 :     object = PyCapsule_New(known, "ignored", NULL);
    1004                 :          0 :     PyCapsule_SetPointer(object, capsule_pointer);
    1005                 :          0 :     PyCapsule_SetName(object, capsule_name);
    1006                 :          0 :     PyCapsule_SetDestructor(object, capsule_destructor);
    1007                 :          0 :     PyCapsule_SetContext(object, capsule_context);
    1008                 :          0 :     capsule_destructor(object);
    1009   [ #  #  #  # ]:          0 :     CHECK_DESTRUCTOR;
    1010                 :            :     /* intentionally access using the wrong name */
    1011                 :          0 :     pointer2 = PyCapsule_GetPointer(object, "the wrong name");
    1012         [ #  # ]:          0 :     if (!PyErr_Occurred()) {
    1013                 :          0 :         FAIL("PyCapsule_GetPointer should have failed but did not!");
    1014                 :            :     }
    1015                 :          0 :     PyErr_Clear();
    1016         [ #  # ]:          0 :     if (pointer2) {
    1017         [ #  # ]:          0 :         if (pointer2 == capsule_pointer) {
    1018                 :          0 :             FAIL("PyCapsule_GetPointer should not have"
    1019                 :            :                      " returned the internal pointer!");
    1020                 :            :         } else {
    1021                 :          0 :             FAIL("PyCapsule_GetPointer should have "
    1022                 :            :                      "returned NULL pointer but did not!");
    1023                 :            :         }
    1024                 :            :     }
    1025                 :          0 :     PyCapsule_SetDestructor(object, NULL);
    1026                 :          0 :     Py_DECREF(object);
    1027         [ #  # ]:          0 :     if (capsule_destructor_call_count) {
    1028                 :          0 :         FAIL("destructor called when it should not have been!");
    1029                 :            :     }
    1030                 :            : 
    1031         [ #  # ]:          0 :     for (known = &known_capsules[0]; known->module != NULL; known++) {
    1032                 :            :         /* yeah, ordinarily I wouldn't do this either,
    1033                 :            :            but it's fine for this test harness.
    1034                 :            :         */
    1035                 :            :         static char buffer[256];
    1036                 :            : #undef FAIL
    1037                 :            : #define FAIL(x) \
    1038                 :            :         { \
    1039                 :            :         sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
    1040                 :            :             x, known->module, known->attribute); \
    1041                 :            :         error = buffer; \
    1042                 :            :         goto exit; \
    1043                 :            :         } \
    1044                 :            : 
    1045                 :          0 :         PyObject *module = PyImport_ImportModule(known->module);
    1046         [ #  # ]:          0 :         if (module) {
    1047                 :          0 :             pointer = PyCapsule_Import(known->name, 0);
    1048         [ #  # ]:          0 :             if (!pointer) {
    1049                 :          0 :                 Py_DECREF(module);
    1050                 :          0 :                 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
    1051                 :            :             }
    1052                 :          0 :             object = PyObject_GetAttrString(module, known->attribute);
    1053         [ #  # ]:          0 :             if (!object) {
    1054                 :          0 :                 Py_DECREF(module);
    1055                 :          0 :                 return NULL;
    1056                 :            :             }
    1057                 :          0 :             pointer2 = PyCapsule_GetPointer(object,
    1058                 :            :                                     "weebles wobble but they don't fall down");
    1059         [ #  # ]:          0 :             if (!PyErr_Occurred()) {
    1060                 :          0 :                 Py_DECREF(object);
    1061                 :          0 :                 Py_DECREF(module);
    1062                 :          0 :                 FAIL("PyCapsule_GetPointer should have failed but did not!");
    1063                 :            :             }
    1064                 :          0 :             PyErr_Clear();
    1065         [ #  # ]:          0 :             if (pointer2) {
    1066                 :          0 :                 Py_DECREF(module);
    1067                 :          0 :                 Py_DECREF(object);
    1068         [ #  # ]:          0 :                 if (pointer2 == pointer) {
    1069                 :          0 :                     FAIL("PyCapsule_GetPointer should not have"
    1070                 :            :                              " returned its internal pointer!");
    1071                 :            :                 } else {
    1072                 :          0 :                     FAIL("PyCapsule_GetPointer should have"
    1073                 :            :                              " returned NULL pointer but did not!");
    1074                 :            :                 }
    1075                 :            :             }
    1076                 :          0 :             Py_DECREF(object);
    1077                 :          0 :             Py_DECREF(module);
    1078                 :            :         }
    1079                 :            :         else
    1080                 :          0 :             PyErr_Clear();
    1081                 :            :     }
    1082                 :            : 
    1083                 :          0 :   exit:
    1084         [ #  # ]:          0 :     if (error) {
    1085                 :          0 :         return raiseTestError("test_capsule", error);
    1086                 :            :     }
    1087                 :          0 :     Py_RETURN_NONE;
    1088                 :            : #undef FAIL
    1089                 :            : }
    1090                 :            : 
    1091                 :            : #ifdef HAVE_GETTIMEOFDAY
    1092                 :            : /* Profiling of integer performance */
    1093                 :            : static void print_delta(int test, struct timeval *s, struct timeval *e)
    1094                 :            : {
    1095                 :            :     e->tv_sec -= s->tv_sec;
    1096                 :            :     e->tv_usec -= s->tv_usec;
    1097                 :            :     if (e->tv_usec < 0) {
    1098                 :            :         e->tv_sec -=1;
    1099                 :            :         e->tv_usec += 1000000;
    1100                 :            :     }
    1101                 :            :     printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
    1102                 :            : }
    1103                 :            : 
    1104                 :            : static PyObject *
    1105                 :            : profile_int(PyObject *self, PyObject* args)
    1106                 :            : {
    1107                 :            :     int i, k;
    1108                 :            :     struct timeval start, stop;
    1109                 :            :     PyObject *single, **multiple, *op1, *result;
    1110                 :            : 
    1111                 :            :     /* Test 1: Allocate and immediately deallocate
    1112                 :            :        many small integers */
    1113                 :            :     gettimeofday(&start, NULL);
    1114                 :            :     for(k=0; k < 20000; k++)
    1115                 :            :         for(i=0; i < 1000; i++) {
    1116                 :            :             single = PyLong_FromLong(i);
    1117                 :            :             Py_DECREF(single);
    1118                 :            :         }
    1119                 :            :     gettimeofday(&stop, NULL);
    1120                 :            :     print_delta(1, &start, &stop);
    1121                 :            : 
    1122                 :            :     /* Test 2: Allocate and immediately deallocate
    1123                 :            :        many large integers */
    1124                 :            :     gettimeofday(&start, NULL);
    1125                 :            :     for(k=0; k < 20000; k++)
    1126                 :            :         for(i=0; i < 1000; i++) {
    1127                 :            :             single = PyLong_FromLong(i+1000000);
    1128                 :            :             Py_DECREF(single);
    1129                 :            :         }
    1130                 :            :     gettimeofday(&stop, NULL);
    1131                 :            :     print_delta(2, &start, &stop);
    1132                 :            : 
    1133                 :            :     /* Test 3: Allocate a few integers, then release
    1134                 :            :        them all simultaneously. */
    1135                 :            :     multiple = malloc(sizeof(PyObject*) * 1000);
    1136                 :            :     if (multiple == NULL)
    1137                 :            :         return PyErr_NoMemory();
    1138                 :            :     gettimeofday(&start, NULL);
    1139                 :            :     for(k=0; k < 20000; k++) {
    1140                 :            :         for(i=0; i < 1000; i++) {
    1141                 :            :             multiple[i] = PyLong_FromLong(i+1000000);
    1142                 :            :         }
    1143                 :            :         for(i=0; i < 1000; i++) {
    1144                 :            :             Py_DECREF(multiple[i]);
    1145                 :            :         }
    1146                 :            :     }
    1147                 :            :     gettimeofday(&stop, NULL);
    1148                 :            :     print_delta(3, &start, &stop);
    1149                 :            :     free(multiple);
    1150                 :            : 
    1151                 :            :     /* Test 4: Allocate many integers, then release
    1152                 :            :        them all simultaneously. */
    1153                 :            :     multiple = malloc(sizeof(PyObject*) * 1000000);
    1154                 :            :     if (multiple == NULL)
    1155                 :            :         return PyErr_NoMemory();
    1156                 :            :     gettimeofday(&start, NULL);
    1157                 :            :     for(k=0; k < 20; k++) {
    1158                 :            :         for(i=0; i < 1000000; i++) {
    1159                 :            :             multiple[i] = PyLong_FromLong(i+1000000);
    1160                 :            :         }
    1161                 :            :         for(i=0; i < 1000000; i++) {
    1162                 :            :             Py_DECREF(multiple[i]);
    1163                 :            :         }
    1164                 :            :     }
    1165                 :            :     gettimeofday(&stop, NULL);
    1166                 :            :     print_delta(4, &start, &stop);
    1167                 :            :     free(multiple);
    1168                 :            : 
    1169                 :            :     /* Test 5: Allocate many integers < 32000 */
    1170                 :            :     multiple = malloc(sizeof(PyObject*) * 1000000);
    1171                 :            :     if (multiple == NULL)
    1172                 :            :         return PyErr_NoMemory();
    1173                 :            :     gettimeofday(&start, NULL);
    1174                 :            :     for(k=0; k < 10; k++) {
    1175                 :            :         for(i=0; i < 1000000; i++) {
    1176                 :            :             multiple[i] = PyLong_FromLong(i+1000);
    1177                 :            :         }
    1178                 :            :         for(i=0; i < 1000000; i++) {
    1179                 :            :             Py_DECREF(multiple[i]);
    1180                 :            :         }
    1181                 :            :     }
    1182                 :            :     gettimeofday(&stop, NULL);
    1183                 :            :     print_delta(5, &start, &stop);
    1184                 :            :     free(multiple);
    1185                 :            : 
    1186                 :            :     /* Test 6: Perform small int addition */
    1187                 :            :     op1 = PyLong_FromLong(1);
    1188                 :            :     gettimeofday(&start, NULL);
    1189                 :            :     for(i=0; i < 10000000; i++) {
    1190                 :            :         result = PyNumber_Add(op1, op1);
    1191                 :            :         Py_DECREF(result);
    1192                 :            :     }
    1193                 :            :     gettimeofday(&stop, NULL);
    1194                 :            :     Py_DECREF(op1);
    1195                 :            :     print_delta(6, &start, &stop);
    1196                 :            : 
    1197                 :            :     /* Test 7: Perform medium int addition */
    1198                 :            :     op1 = PyLong_FromLong(1000);
    1199                 :            :     if (op1 == NULL)
    1200                 :            :         return NULL;
    1201                 :            :     gettimeofday(&start, NULL);
    1202                 :            :     for(i=0; i < 10000000; i++) {
    1203                 :            :         result = PyNumber_Add(op1, op1);
    1204                 :            :         Py_XDECREF(result);
    1205                 :            :     }
    1206                 :            :     gettimeofday(&stop, NULL);
    1207                 :            :     Py_DECREF(op1);
    1208                 :            :     print_delta(7, &start, &stop);
    1209                 :            : 
    1210                 :            :     Py_RETURN_NONE;
    1211                 :            : }
    1212                 :            : #endif
    1213                 :            : 
    1214                 :            : /* Issue 6012 */
    1215                 :            : static PyObject *str1, *str2;
    1216                 :            : static int
    1217                 :          0 : failing_converter(PyObject *obj, void *arg)
    1218                 :            : {
    1219                 :            :     /* Clone str1, then let the conversion fail. */
    1220         [ #  # ]:          0 :     assert(str1);
    1221                 :          0 :     str2 = Py_NewRef(str1);
    1222                 :          0 :     return 0;
    1223                 :            : }
    1224                 :            : static PyObject*
    1225                 :          0 : argparsing(PyObject *o, PyObject *args)
    1226                 :            : {
    1227                 :            :     PyObject *res;
    1228                 :          0 :     str1 = str2 = NULL;
    1229         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O&O&",
    1230                 :            :                           PyUnicode_FSConverter, &str1,
    1231                 :            :                           failing_converter, &str2)) {
    1232         [ #  # ]:          0 :         if (!str2)
    1233                 :            :             /* argument converter not called? */
    1234                 :          0 :             return NULL;
    1235                 :            :         /* Should be 1 */
    1236                 :          0 :         res = PyLong_FromSsize_t(Py_REFCNT(str2));
    1237                 :          0 :         Py_DECREF(str2);
    1238                 :          0 :         PyErr_Clear();
    1239                 :          0 :         return res;
    1240                 :            :     }
    1241                 :          0 :     Py_RETURN_NONE;
    1242                 :            : }
    1243                 :            : 
    1244                 :            : /* To test that the result of PyCode_NewEmpty has the right members. */
    1245                 :            : static PyObject *
    1246                 :          0 : code_newempty(PyObject *self, PyObject *args)
    1247                 :            : {
    1248                 :            :     const char *filename;
    1249                 :            :     const char *funcname;
    1250                 :            :     int firstlineno;
    1251                 :            : 
    1252         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "ssi:code_newempty",
    1253                 :            :                           &filename, &funcname, &firstlineno))
    1254                 :          0 :         return NULL;
    1255                 :            : 
    1256                 :          0 :     return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
    1257                 :            : }
    1258                 :            : 
    1259                 :            : static PyObject *
    1260                 :          0 : make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored))
    1261                 :            : {
    1262                 :            :     Py_buffer info;
    1263         [ #  # ]:          0 :     if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0)
    1264                 :          0 :         return NULL;
    1265                 :          0 :     return PyMemoryView_FromBuffer(&info);
    1266                 :            : }
    1267                 :            : 
    1268                 :            : static PyObject *
    1269                 :          0 : test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored))
    1270                 :            : {
    1271                 :          0 :     int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
    1272                 :          0 :     int init[5] = {0, 1, 2, 3, 4};
    1273                 :          0 :     Py_ssize_t itemsize = sizeof(int);
    1274                 :          0 :     Py_ssize_t shape = 5;
    1275                 :          0 :     Py_ssize_t strides = 2 * itemsize;
    1276                 :          0 :     Py_buffer view = {
    1277                 :            :         data,
    1278                 :            :         NULL,
    1279                 :          0 :         5 * itemsize,
    1280                 :            :         itemsize,
    1281                 :            :         1,
    1282                 :            :         1,
    1283                 :            :         NULL,
    1284                 :            :         &shape,
    1285                 :            :         &strides,
    1286                 :            :         NULL,
    1287                 :            :         NULL
    1288                 :            :     };
    1289                 :            :     int *ptr;
    1290                 :            :     int i;
    1291                 :            : 
    1292                 :          0 :     PyBuffer_FromContiguous(&view, init, view.len, 'C');
    1293                 :          0 :     ptr = view.buf;
    1294         [ #  # ]:          0 :     for (i = 0; i < 5; i++) {
    1295         [ #  # ]:          0 :         if (ptr[2*i] != i) {
    1296                 :          0 :             PyErr_SetString(TestError,
    1297                 :            :                 "test_from_contiguous: incorrect result");
    1298                 :          0 :             return NULL;
    1299                 :            :         }
    1300                 :            :     }
    1301                 :            : 
    1302                 :          0 :     view.buf = &data[8];
    1303                 :          0 :     view.strides[0] = -2 * itemsize;
    1304                 :            : 
    1305                 :          0 :     PyBuffer_FromContiguous(&view, init, view.len, 'C');
    1306                 :          0 :     ptr = view.buf;
    1307         [ #  # ]:          0 :     for (i = 0; i < 5; i++) {
    1308         [ #  # ]:          0 :         if (*(ptr-2*i) != i) {
    1309                 :          0 :             PyErr_SetString(TestError,
    1310                 :            :                 "test_from_contiguous: incorrect result");
    1311                 :          0 :             return NULL;
    1312                 :            :         }
    1313                 :            :     }
    1314                 :            : 
    1315                 :          0 :     Py_RETURN_NONE;
    1316                 :            : }
    1317                 :            : 
    1318                 :            : #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
    1319                 :            : 
    1320                 :            : static PyObject *
    1321                 :          0 : test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
    1322                 :            : {
    1323                 :            :     PyObject *b;
    1324                 :            :     char *dummy[1];
    1325                 :            :     int ret, match;
    1326                 :            : 
    1327                 :            :     /* PyBuffer_FillInfo() */
    1328                 :          0 :     ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE);
    1329   [ #  #  #  # ]:          0 :     match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
    1330                 :          0 :     PyErr_Clear();
    1331   [ #  #  #  # ]:          0 :     if (ret != -1 || match == 0)
    1332                 :          0 :         goto error;
    1333                 :            : 
    1334                 :            :     /* bytesiobuf_getbuffer() */
    1335                 :          0 :     PyTypeObject *type = (PyTypeObject *)_PyImport_GetModuleAttrString(
    1336                 :            :             "_io", "_BytesIOBuffer");
    1337         [ #  # ]:          0 :     if (type == NULL) {
    1338                 :          0 :         return NULL;
    1339                 :            :     }
    1340                 :          0 :     b = type->tp_alloc(type, 0);
    1341                 :          0 :     Py_DECREF(type);
    1342         [ #  # ]:          0 :     if (b == NULL) {
    1343                 :          0 :         return NULL;
    1344                 :            :     }
    1345                 :            : 
    1346                 :          0 :     ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE);
    1347                 :          0 :     Py_DECREF(b);
    1348   [ #  #  #  # ]:          0 :     match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
    1349                 :          0 :     PyErr_Clear();
    1350   [ #  #  #  # ]:          0 :     if (ret != -1 || match == 0)
    1351                 :          0 :         goto error;
    1352                 :            : 
    1353                 :          0 :     Py_RETURN_NONE;
    1354                 :            : 
    1355                 :          0 : error:
    1356                 :          0 :     PyErr_SetString(TestError,
    1357                 :            :         "test_pep3118_obsolete_write_locks: failure");
    1358                 :          0 :     return NULL;
    1359                 :            : }
    1360                 :            : #endif
    1361                 :            : 
    1362                 :            : /* This tests functions that historically supported write locks.  It is
    1363                 :            :    wrong to call getbuffer() with view==NULL and a compliant getbufferproc
    1364                 :            :    is entitled to segfault in that case. */
    1365                 :            : static PyObject *
    1366                 :          0 : getbuffer_with_null_view(PyObject* self, PyObject *obj)
    1367                 :            : {
    1368         [ #  # ]:          0 :     if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0)
    1369                 :          0 :         return NULL;
    1370                 :            : 
    1371                 :          0 :     Py_RETURN_NONE;
    1372                 :            : }
    1373                 :            : 
    1374                 :            : /* PyBuffer_SizeFromFormat() */
    1375                 :            : static PyObject *
    1376                 :          0 : test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args)
    1377                 :            : {
    1378                 :            :     const char *format;
    1379                 :            :     Py_ssize_t result;
    1380                 :            : 
    1381         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat",
    1382                 :            :                           &format)) {
    1383                 :          0 :         return NULL;
    1384                 :            :     }
    1385                 :            : 
    1386                 :          0 :     result = PyBuffer_SizeFromFormat(format);
    1387         [ #  # ]:          0 :     if (result == -1) {
    1388                 :          0 :         return NULL;
    1389                 :            :     }
    1390                 :            : 
    1391                 :          0 :     return PyLong_FromSsize_t(result);
    1392                 :            : }
    1393                 :            : 
    1394                 :            : /* Test that the fatal error from not having a current thread doesn't
    1395                 :            :    cause an infinite loop.  Run via Lib/test/test_capi.py */
    1396                 :            : static PyObject *
    1397                 :          0 : crash_no_current_thread(PyObject *self, PyObject *Py_UNUSED(ignored))
    1398                 :            : {
    1399                 :          0 :     Py_BEGIN_ALLOW_THREADS
    1400                 :            :     /* Using PyThreadState_Get() directly allows the test to pass in
    1401                 :            :        !pydebug mode. However, the test only actually tests anything
    1402                 :            :        in pydebug mode, since that's where the infinite loop was in
    1403                 :            :        the first place. */
    1404                 :          0 :     PyThreadState_Get();
    1405                 :          0 :     Py_END_ALLOW_THREADS
    1406                 :          0 :     return NULL;
    1407                 :            : }
    1408                 :            : 
    1409                 :            : /* Test that the GILState thread and the "current" thread match. */
    1410                 :            : static PyObject *
    1411                 :          0 : test_current_tstate_matches(PyObject *self, PyObject *Py_UNUSED(ignored))
    1412                 :            : {
    1413                 :          0 :     PyThreadState *orig_tstate = PyThreadState_Get();
    1414                 :            : 
    1415         [ #  # ]:          0 :     if (orig_tstate != PyGILState_GetThisThreadState()) {
    1416                 :          0 :         PyErr_SetString(PyExc_RuntimeError,
    1417                 :            :                         "current thread state doesn't match GILState");
    1418                 :          0 :         return NULL;
    1419                 :            :     }
    1420                 :            : 
    1421                 :          0 :     const char *err = NULL;
    1422                 :          0 :     PyThreadState_Swap(NULL);
    1423                 :          0 :     PyThreadState *substate = Py_NewInterpreter();
    1424                 :            : 
    1425         [ #  # ]:          0 :     if (substate != PyThreadState_Get()) {
    1426                 :          0 :         err = "subinterpreter thread state not current";
    1427                 :          0 :         goto finally;
    1428                 :            :     }
    1429         [ #  # ]:          0 :     if (substate != PyGILState_GetThisThreadState()) {
    1430                 :          0 :         err = "subinterpreter thread state doesn't match GILState";
    1431                 :          0 :         goto finally;
    1432                 :            :     }
    1433                 :            : 
    1434                 :          0 : finally:
    1435                 :          0 :     Py_EndInterpreter(substate);
    1436                 :          0 :     PyThreadState_Swap(orig_tstate);
    1437                 :            : 
    1438         [ #  # ]:          0 :     if (err != NULL) {
    1439                 :          0 :         PyErr_SetString(PyExc_RuntimeError, err);
    1440                 :          0 :         return NULL;
    1441                 :            :     }
    1442                 :          0 :     Py_RETURN_NONE;
    1443                 :            : }
    1444                 :            : 
    1445                 :            : /* To run some code in a sub-interpreter. */
    1446                 :            : static PyObject *
    1447                 :          0 : run_in_subinterp(PyObject *self, PyObject *args)
    1448                 :            : {
    1449                 :            :     const char *code;
    1450                 :            :     int r;
    1451                 :            :     PyThreadState *substate, *mainstate;
    1452                 :            :     /* only initialise 'cflags.cf_flags' to test backwards compatibility */
    1453                 :          0 :     PyCompilerFlags cflags = {0};
    1454                 :            : 
    1455         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
    1456                 :            :                           &code))
    1457                 :          0 :         return NULL;
    1458                 :            : 
    1459                 :          0 :     mainstate = PyThreadState_Get();
    1460                 :            : 
    1461                 :          0 :     PyThreadState_Swap(NULL);
    1462                 :            : 
    1463                 :          0 :     substate = Py_NewInterpreter();
    1464         [ #  # ]:          0 :     if (substate == NULL) {
    1465                 :            :         /* Since no new thread state was created, there is no exception to
    1466                 :            :            propagate; raise a fresh one after swapping in the old thread
    1467                 :            :            state. */
    1468                 :          0 :         PyThreadState_Swap(mainstate);
    1469                 :          0 :         PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
    1470                 :          0 :         return NULL;
    1471                 :            :     }
    1472                 :          0 :     r = PyRun_SimpleStringFlags(code, &cflags);
    1473                 :          0 :     Py_EndInterpreter(substate);
    1474                 :            : 
    1475                 :          0 :     PyThreadState_Swap(mainstate);
    1476                 :            : 
    1477                 :          0 :     return PyLong_FromLong(r);
    1478                 :            : }
    1479                 :            : 
    1480                 :            : /* To run some code in a sub-interpreter. */
    1481                 :            : static PyObject *
    1482                 :          0 : run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
    1483                 :            : {
    1484                 :            :     const char *code;
    1485                 :          0 :     int allow_fork = -1;
    1486                 :          0 :     int allow_exec = -1;
    1487                 :          0 :     int allow_threads = -1;
    1488                 :          0 :     int allow_daemon_threads = -1;
    1489                 :          0 :     int check_multi_interp_extensions = -1;
    1490                 :            :     int r;
    1491                 :            :     PyThreadState *substate, *mainstate;
    1492                 :            :     /* only initialise 'cflags.cf_flags' to test backwards compatibility */
    1493                 :          0 :     PyCompilerFlags cflags = {0};
    1494                 :            : 
    1495                 :            :     static char *kwlist[] = {"code",
    1496                 :            :                              "allow_fork",
    1497                 :            :                              "allow_exec",
    1498                 :            :                              "allow_threads",
    1499                 :            :                              "allow_daemon_threads",
    1500                 :            :                              "check_multi_interp_extensions",
    1501                 :            :                              NULL};
    1502         [ #  # ]:          0 :     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
    1503                 :            :                     "s$ppppp:run_in_subinterp_with_config", kwlist,
    1504                 :            :                     &code, &allow_fork, &allow_exec,
    1505                 :            :                     &allow_threads, &allow_daemon_threads,
    1506                 :            :                     &check_multi_interp_extensions)) {
    1507                 :          0 :         return NULL;
    1508                 :            :     }
    1509         [ #  # ]:          0 :     if (allow_fork < 0) {
    1510                 :          0 :         PyErr_SetString(PyExc_ValueError, "missing allow_fork");
    1511                 :          0 :         return NULL;
    1512                 :            :     }
    1513         [ #  # ]:          0 :     if (allow_exec < 0) {
    1514                 :          0 :         PyErr_SetString(PyExc_ValueError, "missing allow_exec");
    1515                 :          0 :         return NULL;
    1516                 :            :     }
    1517         [ #  # ]:          0 :     if (allow_threads < 0) {
    1518                 :          0 :         PyErr_SetString(PyExc_ValueError, "missing allow_threads");
    1519                 :          0 :         return NULL;
    1520                 :            :     }
    1521         [ #  # ]:          0 :     if (allow_daemon_threads < 0) {
    1522                 :          0 :         PyErr_SetString(PyExc_ValueError, "missing allow_daemon_threads");
    1523                 :          0 :         return NULL;
    1524                 :            :     }
    1525         [ #  # ]:          0 :     if (check_multi_interp_extensions < 0) {
    1526                 :          0 :         PyErr_SetString(PyExc_ValueError, "missing check_multi_interp_extensions");
    1527                 :          0 :         return NULL;
    1528                 :            :     }
    1529                 :            : 
    1530                 :          0 :     mainstate = PyThreadState_Get();
    1531                 :            : 
    1532                 :          0 :     PyThreadState_Swap(NULL);
    1533                 :            : 
    1534                 :          0 :     const _PyInterpreterConfig config = {
    1535                 :            :         .allow_fork = allow_fork,
    1536                 :            :         .allow_exec = allow_exec,
    1537                 :            :         .allow_threads = allow_threads,
    1538                 :            :         .allow_daemon_threads = allow_daemon_threads,
    1539                 :            :         .check_multi_interp_extensions = check_multi_interp_extensions,
    1540                 :            :     };
    1541                 :          0 :     substate = _Py_NewInterpreterFromConfig(&config);
    1542         [ #  # ]:          0 :     if (substate == NULL) {
    1543                 :            :         /* Since no new thread state was created, there is no exception to
    1544                 :            :            propagate; raise a fresh one after swapping in the old thread
    1545                 :            :            state. */
    1546                 :          0 :         PyThreadState_Swap(mainstate);
    1547                 :          0 :         PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
    1548                 :          0 :         return NULL;
    1549                 :            :     }
    1550                 :          0 :     r = PyRun_SimpleStringFlags(code, &cflags);
    1551                 :          0 :     Py_EndInterpreter(substate);
    1552                 :            : 
    1553                 :          0 :     PyThreadState_Swap(mainstate);
    1554                 :            : 
    1555                 :          0 :     return PyLong_FromLong(r);
    1556                 :            : }
    1557                 :            : 
    1558                 :            : static void
    1559                 :          0 : _xid_capsule_destructor(PyObject *capsule)
    1560                 :            : {
    1561                 :            :     _PyCrossInterpreterData *data = \
    1562                 :          0 :             (_PyCrossInterpreterData *)PyCapsule_GetPointer(capsule, NULL);
    1563         [ #  # ]:          0 :     if (data != NULL) {
    1564         [ #  # ]:          0 :         assert(_PyCrossInterpreterData_Release(data) == 0);
    1565                 :          0 :         PyMem_Free(data);
    1566                 :            :     }
    1567                 :          0 : }
    1568                 :            : 
    1569                 :            : static PyObject *
    1570                 :          0 : get_crossinterp_data(PyObject *self, PyObject *args)
    1571                 :            : {
    1572                 :          0 :     PyObject *obj = NULL;
    1573         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O:get_crossinterp_data", &obj)) {
    1574                 :          0 :         return NULL;
    1575                 :            :     }
    1576                 :            : 
    1577                 :          0 :     _PyCrossInterpreterData *data = PyMem_NEW(_PyCrossInterpreterData, 1);
    1578         [ #  # ]:          0 :     if (data == NULL) {
    1579                 :          0 :         PyErr_NoMemory();
    1580                 :          0 :         return NULL;
    1581                 :            :     }
    1582         [ #  # ]:          0 :     if (_PyObject_GetCrossInterpreterData(obj, data) != 0) {
    1583                 :          0 :         PyMem_Free(data);
    1584                 :          0 :         return NULL;
    1585                 :            :     }
    1586                 :          0 :     PyObject *capsule = PyCapsule_New(data, NULL, _xid_capsule_destructor);
    1587         [ #  # ]:          0 :     if (capsule == NULL) {
    1588         [ #  # ]:          0 :         assert(_PyCrossInterpreterData_Release(data) == 0);
    1589                 :          0 :         PyMem_Free(data);
    1590                 :            :     }
    1591                 :          0 :     return capsule;
    1592                 :            : }
    1593                 :            : 
    1594                 :            : static PyObject *
    1595                 :          0 : restore_crossinterp_data(PyObject *self, PyObject *args)
    1596                 :            : {
    1597                 :          0 :     PyObject *capsule = NULL;
    1598         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O:restore_crossinterp_data", &capsule)) {
    1599                 :          0 :         return NULL;
    1600                 :            :     }
    1601                 :            : 
    1602                 :            :     _PyCrossInterpreterData *data = \
    1603                 :          0 :             (_PyCrossInterpreterData *)PyCapsule_GetPointer(capsule, NULL);
    1604         [ #  # ]:          0 :     if (data == NULL) {
    1605                 :          0 :         return NULL;
    1606                 :            :     }
    1607                 :          0 :     return _PyCrossInterpreterData_NewObject(data);
    1608                 :            : }
    1609                 :            : 
    1610                 :            : static void
    1611                 :          0 : slot_tp_del(PyObject *self)
    1612                 :            : {
    1613                 :            :     PyObject *del, *res;
    1614                 :            : 
    1615                 :            :     /* Temporarily resurrect the object. */
    1616         [ #  # ]:          0 :     assert(Py_REFCNT(self) == 0);
    1617                 :          0 :     Py_SET_REFCNT(self, 1);
    1618                 :            : 
    1619                 :            :     /* Save the current exception, if any. */
    1620                 :          0 :     PyObject *exc = PyErr_GetRaisedException();
    1621                 :            : 
    1622                 :          0 :     PyObject *tp_del = PyUnicode_InternFromString("__tp_del__");
    1623         [ #  # ]:          0 :     if (tp_del == NULL) {
    1624                 :          0 :         PyErr_WriteUnraisable(NULL);
    1625                 :          0 :         PyErr_SetRaisedException(exc);
    1626                 :          0 :         return;
    1627                 :            :     }
    1628                 :            :     /* Execute __del__ method, if any. */
    1629                 :          0 :     del = _PyType_Lookup(Py_TYPE(self), tp_del);
    1630                 :          0 :     Py_DECREF(tp_del);
    1631         [ #  # ]:          0 :     if (del != NULL) {
    1632                 :          0 :         res = PyObject_CallOneArg(del, self);
    1633         [ #  # ]:          0 :         if (res == NULL)
    1634                 :          0 :             PyErr_WriteUnraisable(del);
    1635                 :            :         else
    1636                 :          0 :             Py_DECREF(res);
    1637                 :            :     }
    1638                 :            : 
    1639                 :            :     /* Restore the saved exception. */
    1640                 :          0 :     PyErr_SetRaisedException(exc);
    1641                 :            : 
    1642                 :            :     /* Undo the temporary resurrection; can't use DECREF here, it would
    1643                 :            :      * cause a recursive call.
    1644                 :            :      */
    1645         [ #  # ]:          0 :     assert(Py_REFCNT(self) > 0);
    1646                 :          0 :     Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
    1647         [ #  # ]:          0 :     if (Py_REFCNT(self) == 0) {
    1648                 :            :         /* this is the normal path out */
    1649                 :          0 :         return;
    1650                 :            :     }
    1651                 :            : 
    1652                 :            :     /* __del__ resurrected it!  Make it look like the original Py_DECREF
    1653                 :            :      * never happened.
    1654                 :            :      */
    1655                 :            :     {
    1656                 :          0 :         Py_ssize_t refcnt = Py_REFCNT(self);
    1657                 :          0 :         _Py_NewReferenceNoTotal(self);
    1658                 :          0 :         Py_SET_REFCNT(self, refcnt);
    1659                 :            :     }
    1660   [ #  #  #  # ]:          0 :     assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
    1661                 :            : }
    1662                 :            : 
    1663                 :            : static PyObject *
    1664                 :          0 : with_tp_del(PyObject *self, PyObject *args)
    1665                 :            : {
    1666                 :            :     PyObject *obj;
    1667                 :            :     PyTypeObject *tp;
    1668                 :            : 
    1669         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
    1670                 :          0 :         return NULL;
    1671                 :          0 :     tp = (PyTypeObject *) obj;
    1672   [ #  #  #  # ]:          0 :     if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
    1673                 :          0 :         PyErr_Format(PyExc_TypeError,
    1674                 :            :                      "heap type expected, got %R", obj);
    1675                 :          0 :         return NULL;
    1676                 :            :     }
    1677                 :          0 :     tp->tp_del = slot_tp_del;
    1678                 :          0 :     return Py_NewRef(obj);
    1679                 :            : }
    1680                 :            : 
    1681                 :            : static PyObject *
    1682                 :          0 : without_gc(PyObject *Py_UNUSED(self), PyObject *obj)
    1683                 :            : {
    1684                 :          0 :     PyTypeObject *tp = (PyTypeObject*)obj;
    1685   [ #  #  #  # ]:          0 :     if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
    1686                 :          0 :         return PyErr_Format(PyExc_TypeError, "heap type expected, got %R", obj);
    1687                 :            :     }
    1688         [ #  # ]:          0 :     if (PyType_IS_GC(tp)) {
    1689                 :            :         // Don't try this at home, kids:
    1690                 :          0 :         tp->tp_flags -= Py_TPFLAGS_HAVE_GC;
    1691                 :          0 :         tp->tp_free = PyObject_Del;
    1692                 :          0 :         tp->tp_traverse = NULL;
    1693                 :          0 :         tp->tp_clear = NULL;
    1694                 :            :     }
    1695         [ #  # ]:          0 :     assert(!PyType_IS_GC(tp));
    1696                 :          0 :     return Py_NewRef(obj);
    1697                 :            : }
    1698                 :            : 
    1699                 :            : static PyMethodDef ml;
    1700                 :            : 
    1701                 :            : static PyObject *
    1702                 :          0 : create_cfunction(PyObject *self, PyObject *args)
    1703                 :            : {
    1704                 :          0 :     return PyCFunction_NewEx(&ml, self, NULL);
    1705                 :            : }
    1706                 :            : 
    1707                 :            : static PyMethodDef ml = {
    1708                 :            :     "create_cfunction",
    1709                 :            :     create_cfunction,
    1710                 :            :     METH_NOARGS,
    1711                 :            :     NULL
    1712                 :            : };
    1713                 :            : 
    1714                 :            : static PyObject *
    1715                 :          0 : _test_incref(PyObject *ob)
    1716                 :            : {
    1717                 :          0 :     return Py_NewRef(ob);
    1718                 :            : }
    1719                 :            : 
    1720                 :            : static PyObject *
    1721                 :          0 : test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
    1722                 :            : {
    1723                 :          0 :     PyObject *obj = PyLong_FromLong(0);
    1724                 :          0 :     Py_XINCREF(_test_incref(obj));
    1725                 :          0 :     Py_DECREF(obj);
    1726                 :          0 :     Py_DECREF(obj);
    1727                 :          0 :     Py_DECREF(obj);
    1728                 :          0 :     Py_RETURN_NONE;
    1729                 :            : }
    1730                 :            : 
    1731                 :            : static PyObject *
    1732                 :          0 : test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
    1733                 :            : {
    1734                 :          0 :     PyObject *obj = PyLong_FromLong(0);
    1735                 :          0 :     Py_INCREF(_test_incref(obj));
    1736                 :          0 :     Py_DECREF(obj);
    1737                 :          0 :     Py_DECREF(obj);
    1738                 :          0 :     Py_DECREF(obj);
    1739                 :          0 :     Py_RETURN_NONE;
    1740                 :            : }
    1741                 :            : 
    1742                 :            : static PyObject *
    1743                 :          0 : test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
    1744                 :            : {
    1745                 :          0 :     Py_XDECREF(PyLong_FromLong(0));
    1746                 :          0 :     Py_RETURN_NONE;
    1747                 :            : }
    1748                 :            : 
    1749                 :            : static PyObject *
    1750                 :          0 : test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
    1751                 :            : {
    1752                 :          0 :     Py_DECREF(PyLong_FromLong(0));
    1753                 :          0 :     Py_RETURN_NONE;
    1754                 :            : }
    1755                 :            : 
    1756                 :            : static PyObject *
    1757                 :          0 : test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
    1758                 :            :                               PyObject *Py_UNUSED(args))
    1759                 :            : {
    1760                 :            :     PyStructSequence_Desc descr;
    1761                 :            :     PyStructSequence_Field descr_fields[3];
    1762                 :            : 
    1763                 :          0 :     descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"};
    1764                 :          0 :     descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"};
    1765                 :          0 :     descr_fields[2] = (PyStructSequence_Field){0, NULL};
    1766                 :            : 
    1767                 :          0 :     descr.name = "_testcapi.test_descr";
    1768                 :          0 :     descr.doc = "This is used to test for memory leaks in NewType";
    1769                 :          0 :     descr.fields = descr_fields;
    1770                 :          0 :     descr.n_in_sequence = 1;
    1771                 :            : 
    1772                 :          0 :     PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
    1773         [ #  # ]:          0 :     assert(structseq_type != NULL);
    1774         [ #  # ]:          0 :     assert(PyType_Check(structseq_type));
    1775         [ #  # ]:          0 :     assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
    1776                 :          0 :     Py_DECREF(structseq_type);
    1777                 :            : 
    1778                 :          0 :     Py_RETURN_NONE;
    1779                 :            : }
    1780                 :            : 
    1781                 :            : static PyObject *
    1782                 :          0 : test_structseq_newtype_null_descr_doc(PyObject *Py_UNUSED(self),
    1783                 :            :                               PyObject *Py_UNUSED(args))
    1784                 :            : {
    1785                 :          0 :     PyStructSequence_Field descr_fields[1] = {
    1786                 :            :         (PyStructSequence_Field){NULL, NULL}
    1787                 :            :     };
    1788                 :            :     // Test specifically for NULL .doc field.
    1789                 :          0 :     PyStructSequence_Desc descr = {"_testcapi.test_descr", NULL, &descr_fields[0], 0};
    1790                 :            : 
    1791                 :          0 :     PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
    1792         [ #  # ]:          0 :     assert(structseq_type != NULL);
    1793         [ #  # ]:          0 :     assert(PyType_Check(structseq_type));
    1794         [ #  # ]:          0 :     assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
    1795                 :          0 :     Py_DECREF(structseq_type);
    1796                 :            : 
    1797                 :          0 :     Py_RETURN_NONE;
    1798                 :            : }
    1799                 :            : 
    1800                 :            : static PyObject *
    1801                 :          0 : test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored))
    1802                 :            : {
    1803                 :          0 :     PyObject *obj = PyLong_FromLong(0);
    1804                 :          0 :     Py_IncRef(obj);
    1805                 :          0 :     Py_DecRef(obj);
    1806                 :          0 :     Py_DecRef(obj);
    1807                 :          0 :     Py_RETURN_NONE;
    1808                 :            : }
    1809                 :            : 
    1810                 :            : typedef struct {
    1811                 :            :     PyThread_type_lock start_event;
    1812                 :            :     PyThread_type_lock exit_event;
    1813                 :            :     PyObject *callback;
    1814                 :            : } test_c_thread_t;
    1815                 :            : 
    1816                 :            : static void
    1817                 :          0 : temporary_c_thread(void *data)
    1818                 :            : {
    1819                 :          0 :     test_c_thread_t *test_c_thread = data;
    1820                 :            :     PyGILState_STATE state;
    1821                 :            :     PyObject *res;
    1822                 :            : 
    1823                 :          0 :     PyThread_release_lock(test_c_thread->start_event);
    1824                 :            : 
    1825                 :            :     /* Allocate a Python thread state for this thread */
    1826                 :          0 :     state = PyGILState_Ensure();
    1827                 :            : 
    1828                 :          0 :     res = PyObject_CallNoArgs(test_c_thread->callback);
    1829         [ #  # ]:          0 :     Py_CLEAR(test_c_thread->callback);
    1830                 :            : 
    1831         [ #  # ]:          0 :     if (res == NULL) {
    1832                 :          0 :         PyErr_Print();
    1833                 :            :     }
    1834                 :            :     else {
    1835                 :          0 :         Py_DECREF(res);
    1836                 :            :     }
    1837                 :            : 
    1838                 :            :     /* Destroy the Python thread state for this thread */
    1839                 :          0 :     PyGILState_Release(state);
    1840                 :            : 
    1841                 :          0 :     PyThread_release_lock(test_c_thread->exit_event);
    1842                 :          0 : }
    1843                 :            : 
    1844                 :            : static test_c_thread_t test_c_thread;
    1845                 :            : 
    1846                 :            : static PyObject *
    1847                 :          0 : call_in_temporary_c_thread(PyObject *self, PyObject *args)
    1848                 :            : {
    1849                 :          0 :     PyObject *res = NULL;
    1850                 :          0 :     PyObject *callback = NULL;
    1851                 :            :     long thread;
    1852                 :          0 :     int wait = 1;
    1853         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O|i", &callback, &wait))
    1854                 :            :     {
    1855                 :          0 :         return NULL;
    1856                 :            :     }
    1857                 :            : 
    1858                 :          0 :     test_c_thread.start_event = PyThread_allocate_lock();
    1859                 :          0 :     test_c_thread.exit_event = PyThread_allocate_lock();
    1860                 :          0 :     test_c_thread.callback = NULL;
    1861   [ #  #  #  # ]:          0 :     if (!test_c_thread.start_event || !test_c_thread.exit_event) {
    1862                 :          0 :         PyErr_SetString(PyExc_RuntimeError, "could not allocate lock");
    1863                 :          0 :         goto exit;
    1864                 :            :     }
    1865                 :            : 
    1866                 :          0 :     test_c_thread.callback = Py_NewRef(callback);
    1867                 :            : 
    1868                 :          0 :     PyThread_acquire_lock(test_c_thread.start_event, 1);
    1869                 :          0 :     PyThread_acquire_lock(test_c_thread.exit_event, 1);
    1870                 :            : 
    1871                 :          0 :     thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread);
    1872         [ #  # ]:          0 :     if (thread == -1) {
    1873                 :          0 :         PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
    1874                 :          0 :         PyThread_release_lock(test_c_thread.start_event);
    1875                 :          0 :         PyThread_release_lock(test_c_thread.exit_event);
    1876                 :          0 :         goto exit;
    1877                 :            :     }
    1878                 :            : 
    1879                 :          0 :     PyThread_acquire_lock(test_c_thread.start_event, 1);
    1880                 :          0 :     PyThread_release_lock(test_c_thread.start_event);
    1881                 :            : 
    1882         [ #  # ]:          0 :     if (!wait) {
    1883                 :          0 :         Py_RETURN_NONE;
    1884                 :            :     }
    1885                 :            : 
    1886                 :          0 :     Py_BEGIN_ALLOW_THREADS
    1887                 :          0 :         PyThread_acquire_lock(test_c_thread.exit_event, 1);
    1888                 :          0 :         PyThread_release_lock(test_c_thread.exit_event);
    1889                 :          0 :     Py_END_ALLOW_THREADS
    1890                 :            : 
    1891                 :          0 :     res = Py_NewRef(Py_None);
    1892                 :            : 
    1893                 :          0 : exit:
    1894         [ #  # ]:          0 :     Py_CLEAR(test_c_thread.callback);
    1895         [ #  # ]:          0 :     if (test_c_thread.start_event) {
    1896                 :          0 :         PyThread_free_lock(test_c_thread.start_event);
    1897                 :          0 :         test_c_thread.start_event = NULL;
    1898                 :            :     }
    1899         [ #  # ]:          0 :     if (test_c_thread.exit_event) {
    1900                 :          0 :         PyThread_free_lock(test_c_thread.exit_event);
    1901                 :          0 :         test_c_thread.exit_event = NULL;
    1902                 :            :     }
    1903                 :          0 :     return res;
    1904                 :            : }
    1905                 :            : 
    1906                 :            : static PyObject *
    1907                 :          0 : join_temporary_c_thread(PyObject *self, PyObject *Py_UNUSED(ignored))
    1908                 :            : {
    1909                 :          0 :     Py_BEGIN_ALLOW_THREADS
    1910                 :          0 :         PyThread_acquire_lock(test_c_thread.exit_event, 1);
    1911                 :          0 :         PyThread_release_lock(test_c_thread.exit_event);
    1912                 :          0 :     Py_END_ALLOW_THREADS
    1913         [ #  # ]:          0 :     Py_CLEAR(test_c_thread.callback);
    1914                 :          0 :     PyThread_free_lock(test_c_thread.start_event);
    1915                 :          0 :     test_c_thread.start_event = NULL;
    1916                 :          0 :     PyThread_free_lock(test_c_thread.exit_event);
    1917                 :          0 :     test_c_thread.exit_event = NULL;
    1918                 :          0 :     Py_RETURN_NONE;
    1919                 :            : }
    1920                 :            : 
    1921                 :            : /* marshal */
    1922                 :            : 
    1923                 :            : static PyObject*
    1924                 :          0 : pymarshal_write_long_to_file(PyObject* self, PyObject *args)
    1925                 :            : {
    1926                 :            :     long value;
    1927                 :            :     PyObject *filename;
    1928                 :            :     int version;
    1929                 :            :     FILE *fp;
    1930                 :            : 
    1931         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "lOi:pymarshal_write_long_to_file",
    1932                 :            :                           &value, &filename, &version))
    1933                 :          0 :         return NULL;
    1934                 :            : 
    1935                 :          0 :     fp = _Py_fopen_obj(filename, "wb");
    1936         [ #  # ]:          0 :     if (fp == NULL) {
    1937                 :          0 :         PyErr_SetFromErrno(PyExc_OSError);
    1938                 :          0 :         return NULL;
    1939                 :            :     }
    1940                 :            : 
    1941                 :          0 :     PyMarshal_WriteLongToFile(value, fp, version);
    1942                 :            : 
    1943                 :          0 :     fclose(fp);
    1944         [ #  # ]:          0 :     if (PyErr_Occurred())
    1945                 :          0 :         return NULL;
    1946                 :          0 :     Py_RETURN_NONE;
    1947                 :            : }
    1948                 :            : 
    1949                 :            : static PyObject*
    1950                 :          0 : pymarshal_write_object_to_file(PyObject* self, PyObject *args)
    1951                 :            : {
    1952                 :            :     PyObject *obj;
    1953                 :            :     PyObject *filename;
    1954                 :            :     int version;
    1955                 :            :     FILE *fp;
    1956                 :            : 
    1957         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OOi:pymarshal_write_object_to_file",
    1958                 :            :                           &obj, &filename, &version))
    1959                 :          0 :         return NULL;
    1960                 :            : 
    1961                 :          0 :     fp = _Py_fopen_obj(filename, "wb");
    1962         [ #  # ]:          0 :     if (fp == NULL) {
    1963                 :          0 :         PyErr_SetFromErrno(PyExc_OSError);
    1964                 :          0 :         return NULL;
    1965                 :            :     }
    1966                 :            : 
    1967                 :          0 :     PyMarshal_WriteObjectToFile(obj, fp, version);
    1968                 :            : 
    1969                 :          0 :     fclose(fp);
    1970         [ #  # ]:          0 :     if (PyErr_Occurred())
    1971                 :          0 :         return NULL;
    1972                 :          0 :     Py_RETURN_NONE;
    1973                 :            : }
    1974                 :            : 
    1975                 :            : static PyObject*
    1976                 :          0 : pymarshal_read_short_from_file(PyObject* self, PyObject *args)
    1977                 :            : {
    1978                 :            :     int value;
    1979                 :            :     long pos;
    1980                 :            :     PyObject *filename;
    1981                 :            :     FILE *fp;
    1982                 :            : 
    1983         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O:pymarshal_read_short_from_file", &filename))
    1984                 :          0 :         return NULL;
    1985                 :            : 
    1986                 :          0 :     fp = _Py_fopen_obj(filename, "rb");
    1987         [ #  # ]:          0 :     if (fp == NULL) {
    1988                 :          0 :         PyErr_SetFromErrno(PyExc_OSError);
    1989                 :          0 :         return NULL;
    1990                 :            :     }
    1991                 :            : 
    1992                 :          0 :     value = PyMarshal_ReadShortFromFile(fp);
    1993                 :          0 :     pos = ftell(fp);
    1994                 :            : 
    1995                 :          0 :     fclose(fp);
    1996         [ #  # ]:          0 :     if (PyErr_Occurred())
    1997                 :          0 :         return NULL;
    1998                 :          0 :     return Py_BuildValue("il", value, pos);
    1999                 :            : }
    2000                 :            : 
    2001                 :            : static PyObject*
    2002                 :          0 : pymarshal_read_long_from_file(PyObject* self, PyObject *args)
    2003                 :            : {
    2004                 :            :     long value, pos;
    2005                 :            :     PyObject *filename;
    2006                 :            :     FILE *fp;
    2007                 :            : 
    2008         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O:pymarshal_read_long_from_file", &filename))
    2009                 :          0 :         return NULL;
    2010                 :            : 
    2011                 :          0 :     fp = _Py_fopen_obj(filename, "rb");
    2012         [ #  # ]:          0 :     if (fp == NULL) {
    2013                 :          0 :         PyErr_SetFromErrno(PyExc_OSError);
    2014                 :          0 :         return NULL;
    2015                 :            :     }
    2016                 :            : 
    2017                 :          0 :     value = PyMarshal_ReadLongFromFile(fp);
    2018                 :          0 :     pos = ftell(fp);
    2019                 :            : 
    2020                 :          0 :     fclose(fp);
    2021         [ #  # ]:          0 :     if (PyErr_Occurred())
    2022                 :          0 :         return NULL;
    2023                 :          0 :     return Py_BuildValue("ll", value, pos);
    2024                 :            : }
    2025                 :            : 
    2026                 :            : static PyObject*
    2027                 :          0 : pymarshal_read_last_object_from_file(PyObject* self, PyObject *args)
    2028                 :            : {
    2029                 :            :     PyObject *obj;
    2030                 :            :     long pos;
    2031                 :            :     PyObject *filename;
    2032                 :            :     FILE *fp;
    2033                 :            : 
    2034         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O:pymarshal_read_last_object_from_file", &filename))
    2035                 :          0 :         return NULL;
    2036                 :            : 
    2037                 :          0 :     fp = _Py_fopen_obj(filename, "rb");
    2038         [ #  # ]:          0 :     if (fp == NULL) {
    2039                 :          0 :         PyErr_SetFromErrno(PyExc_OSError);
    2040                 :          0 :         return NULL;
    2041                 :            :     }
    2042                 :            : 
    2043                 :          0 :     obj = PyMarshal_ReadLastObjectFromFile(fp);
    2044                 :          0 :     pos = ftell(fp);
    2045                 :            : 
    2046                 :          0 :     fclose(fp);
    2047                 :          0 :     return Py_BuildValue("Nl", obj, pos);
    2048                 :            : }
    2049                 :            : 
    2050                 :            : static PyObject*
    2051                 :          0 : pymarshal_read_object_from_file(PyObject* self, PyObject *args)
    2052                 :            : {
    2053                 :            :     PyObject *obj;
    2054                 :            :     long pos;
    2055                 :            :     PyObject *filename;
    2056                 :            :     FILE *fp;
    2057                 :            : 
    2058         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O:pymarshal_read_object_from_file", &filename))
    2059                 :          0 :         return NULL;
    2060                 :            : 
    2061                 :          0 :     fp = _Py_fopen_obj(filename, "rb");
    2062         [ #  # ]:          0 :     if (fp == NULL) {
    2063                 :          0 :         PyErr_SetFromErrno(PyExc_OSError);
    2064                 :          0 :         return NULL;
    2065                 :            :     }
    2066                 :            : 
    2067                 :          0 :     obj = PyMarshal_ReadObjectFromFile(fp);
    2068                 :          0 :     pos = ftell(fp);
    2069                 :            : 
    2070                 :          0 :     fclose(fp);
    2071                 :          0 :     return Py_BuildValue("Nl", obj, pos);
    2072                 :            : }
    2073                 :            : 
    2074                 :            : static PyObject*
    2075                 :          0 : return_null_without_error(PyObject *self, PyObject *args)
    2076                 :            : {
    2077                 :            :     /* invalid call: return NULL without setting an error,
    2078                 :            :      * _Py_CheckFunctionResult() must detect such bug at runtime. */
    2079                 :          0 :     PyErr_Clear();
    2080                 :          0 :     return NULL;
    2081                 :            : }
    2082                 :            : 
    2083                 :            : static PyObject*
    2084                 :          0 : return_result_with_error(PyObject *self, PyObject *args)
    2085                 :            : {
    2086                 :            :     /* invalid call: return a result with an error set,
    2087                 :            :      * _Py_CheckFunctionResult() must detect such bug at runtime. */
    2088                 :          0 :     PyErr_SetNone(PyExc_ValueError);
    2089                 :          0 :     Py_RETURN_NONE;
    2090                 :            : }
    2091                 :            : 
    2092                 :            : static PyObject*
    2093                 :          0 : getitem_with_error(PyObject *self, PyObject *args)
    2094                 :            : {
    2095                 :            :     PyObject *map, *key;
    2096         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OO", &map, &key)) {
    2097                 :          0 :         return NULL;
    2098                 :            :     }
    2099                 :            : 
    2100                 :          0 :     PyErr_SetString(PyExc_ValueError, "bug");
    2101                 :          0 :     return PyObject_GetItem(map, key);
    2102                 :            : }
    2103                 :            : 
    2104                 :            : static PyObject *
    2105                 :          0 : dict_get_version(PyObject *self, PyObject *args)
    2106                 :            : {
    2107                 :            :     PyDictObject *dict;
    2108                 :            :     uint64_t version;
    2109                 :            : 
    2110         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict))
    2111                 :          0 :         return NULL;
    2112                 :            : 
    2113                 :            :     _Py_COMP_DIAG_PUSH
    2114                 :            :     _Py_COMP_DIAG_IGNORE_DEPR_DECLS
    2115                 :          0 :     version = dict->ma_version_tag;
    2116                 :            :     _Py_COMP_DIAG_POP
    2117                 :            : 
    2118                 :            :     static_assert(sizeof(unsigned long long) >= sizeof(version),
    2119                 :            :                   "version is larger than unsigned long long");
    2120                 :          0 :     return PyLong_FromUnsignedLongLong((unsigned long long)version);
    2121                 :            : }
    2122                 :            : 
    2123                 :            : 
    2124                 :            : static PyObject *
    2125                 :          0 : raise_SIGINT_then_send_None(PyObject *self, PyObject *args)
    2126                 :            : {
    2127                 :            :     PyGenObject *gen;
    2128                 :            : 
    2129         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen))
    2130                 :          0 :         return NULL;
    2131                 :            : 
    2132                 :            :     /* This is used in a test to check what happens if a signal arrives just
    2133                 :            :        as we're in the process of entering a yield from chain (see
    2134                 :            :        bpo-30039).
    2135                 :            : 
    2136                 :            :        Needs to be done in C, because:
    2137                 :            :        - we don't have a Python wrapper for raise()
    2138                 :            :        - we need to make sure that the Python-level signal handler doesn't run
    2139                 :            :          *before* we enter the generator frame, which is impossible in Python
    2140                 :            :          because we check for signals before every bytecode operation.
    2141                 :            :      */
    2142                 :          0 :     raise(SIGINT);
    2143                 :          0 :     return PyObject_CallMethod((PyObject *)gen, "send", "O", Py_None);
    2144                 :            : }
    2145                 :            : 
    2146                 :            : 
    2147                 :            : static PyObject*
    2148                 :          0 : stack_pointer(PyObject *self, PyObject *args)
    2149                 :            : {
    2150                 :          0 :     int v = 5;
    2151                 :          0 :     return PyLong_FromVoidPtr(&v);
    2152                 :            : }
    2153                 :            : 
    2154                 :            : 
    2155                 :            : #ifdef W_STOPCODE
    2156                 :            : static PyObject*
    2157                 :          0 : py_w_stopcode(PyObject *self, PyObject *args)
    2158                 :            : {
    2159                 :            :     int sig, status;
    2160         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "i", &sig)) {
    2161                 :          0 :         return NULL;
    2162                 :            :     }
    2163                 :          0 :     status = W_STOPCODE(sig);
    2164                 :          0 :     return PyLong_FromLong(status);
    2165                 :            : }
    2166                 :            : #endif
    2167                 :            : 
    2168                 :            : 
    2169                 :            : static PyObject *
    2170                 :          0 : get_mapping_keys(PyObject* self, PyObject *obj)
    2171                 :            : {
    2172                 :          0 :     return PyMapping_Keys(obj);
    2173                 :            : }
    2174                 :            : 
    2175                 :            : static PyObject *
    2176                 :          0 : get_mapping_values(PyObject* self, PyObject *obj)
    2177                 :            : {
    2178                 :          0 :     return PyMapping_Values(obj);
    2179                 :            : }
    2180                 :            : 
    2181                 :            : static PyObject *
    2182                 :          0 : get_mapping_items(PyObject* self, PyObject *obj)
    2183                 :            : {
    2184                 :          0 :     return PyMapping_Items(obj);
    2185                 :            : }
    2186                 :            : 
    2187                 :            : static PyObject *
    2188                 :          0 : test_mapping_has_key_string(PyObject *self, PyObject *Py_UNUSED(args))
    2189                 :            : {
    2190                 :          0 :     PyObject *context = PyDict_New();
    2191                 :          0 :     PyObject *val = PyLong_FromLong(1);
    2192                 :            : 
    2193                 :            :     // Since this uses `const char*` it is easier to test this in C:
    2194                 :          0 :     PyDict_SetItemString(context, "a", val);
    2195         [ #  # ]:          0 :     if (!PyMapping_HasKeyString(context, "a")) {
    2196                 :          0 :         PyErr_SetString(PyExc_RuntimeError,
    2197                 :            :                         "Existing mapping key does not exist");
    2198                 :          0 :         return NULL;
    2199                 :            :     }
    2200         [ #  # ]:          0 :     if (PyMapping_HasKeyString(context, "b")) {
    2201                 :          0 :         PyErr_SetString(PyExc_RuntimeError,
    2202                 :            :                         "Missing mapping key exists");
    2203                 :          0 :         return NULL;
    2204                 :            :     }
    2205                 :            : 
    2206                 :          0 :     Py_DECREF(val);
    2207                 :          0 :     Py_DECREF(context);
    2208                 :          0 :     Py_RETURN_NONE;
    2209                 :            : }
    2210                 :            : 
    2211                 :            : static PyObject *
    2212                 :          0 : mapping_has_key(PyObject* self, PyObject *args)
    2213                 :            : {
    2214                 :            :     PyObject *context, *key;
    2215         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OO", &context, &key)) {
    2216                 :          0 :         return NULL;
    2217                 :            :     }
    2218                 :          0 :     return PyLong_FromLong(PyMapping_HasKey(context, key));
    2219                 :            : }
    2220                 :            : 
    2221                 :            : static PyObject *
    2222                 :          0 : sequence_set_slice(PyObject* self, PyObject *args)
    2223                 :            : {
    2224                 :            :     PyObject *sequence, *obj;
    2225                 :            :     Py_ssize_t i1, i2;
    2226         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OnnO", &sequence, &i1, &i2, &obj)) {
    2227                 :          0 :         return NULL;
    2228                 :            :     }
    2229                 :            : 
    2230                 :          0 :     int res = PySequence_SetSlice(sequence, i1, i2, obj);
    2231         [ #  # ]:          0 :     if (res == -1) {
    2232                 :          0 :         return NULL;
    2233                 :            :     }
    2234                 :          0 :     Py_RETURN_NONE;
    2235                 :            : }
    2236                 :            : 
    2237                 :            : static PyObject *
    2238                 :          0 : sequence_del_slice(PyObject* self, PyObject *args)
    2239                 :            : {
    2240                 :            :     PyObject *sequence;
    2241                 :            :     Py_ssize_t i1, i2;
    2242         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "Onn", &sequence, &i1, &i2)) {
    2243                 :          0 :         return NULL;
    2244                 :            :     }
    2245                 :            : 
    2246                 :          0 :     int res = PySequence_DelSlice(sequence, i1, i2);
    2247         [ #  # ]:          0 :     if (res == -1) {
    2248                 :          0 :         return NULL;
    2249                 :            :     }
    2250                 :          0 :     Py_RETURN_NONE;
    2251                 :            : }
    2252                 :            : 
    2253                 :            : static PyObject *
    2254                 :          0 : test_pythread_tss_key_state(PyObject *self, PyObject *args)
    2255                 :            : {
    2256                 :          0 :     Py_tss_t tss_key = Py_tss_NEEDS_INIT;
    2257         [ #  # ]:          0 :     if (PyThread_tss_is_created(&tss_key)) {
    2258                 :          0 :         return raiseTestError("test_pythread_tss_key_state",
    2259                 :            :                               "TSS key not in an uninitialized state at "
    2260                 :            :                               "creation time");
    2261                 :            :     }
    2262         [ #  # ]:          0 :     if (PyThread_tss_create(&tss_key) != 0) {
    2263                 :          0 :         PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed");
    2264                 :          0 :         return NULL;
    2265                 :            :     }
    2266         [ #  # ]:          0 :     if (!PyThread_tss_is_created(&tss_key)) {
    2267                 :          0 :         return raiseTestError("test_pythread_tss_key_state",
    2268                 :            :                               "PyThread_tss_create succeeded, "
    2269                 :            :                               "but with TSS key in an uninitialized state");
    2270                 :            :     }
    2271         [ #  # ]:          0 :     if (PyThread_tss_create(&tss_key) != 0) {
    2272                 :          0 :         return raiseTestError("test_pythread_tss_key_state",
    2273                 :            :                               "PyThread_tss_create unsuccessful with "
    2274                 :            :                               "an already initialized key");
    2275                 :            :     }
    2276                 :            : #define CHECK_TSS_API(expr) \
    2277                 :            :         (void)(expr); \
    2278                 :            :         if (!PyThread_tss_is_created(&tss_key)) { \
    2279                 :            :             return raiseTestError("test_pythread_tss_key_state", \
    2280                 :            :                                   "TSS key initialization state was not " \
    2281                 :            :                                   "preserved after calling " #expr); }
    2282         [ #  # ]:          0 :     CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL));
    2283         [ #  # ]:          0 :     CHECK_TSS_API(PyThread_tss_get(&tss_key));
    2284                 :            : #undef CHECK_TSS_API
    2285                 :          0 :     PyThread_tss_delete(&tss_key);
    2286         [ #  # ]:          0 :     if (PyThread_tss_is_created(&tss_key)) {
    2287                 :          0 :         return raiseTestError("test_pythread_tss_key_state",
    2288                 :            :                               "PyThread_tss_delete called, but did not "
    2289                 :            :                               "set the key state to uninitialized");
    2290                 :            :     }
    2291                 :            : 
    2292                 :          0 :     Py_tss_t *ptr_key = PyThread_tss_alloc();
    2293         [ #  # ]:          0 :     if (ptr_key == NULL) {
    2294                 :          0 :         PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed");
    2295                 :          0 :         return NULL;
    2296                 :            :     }
    2297         [ #  # ]:          0 :     if (PyThread_tss_is_created(ptr_key)) {
    2298                 :          0 :         return raiseTestError("test_pythread_tss_key_state",
    2299                 :            :                               "TSS key not in an uninitialized state at "
    2300                 :            :                               "allocation time");
    2301                 :            :     }
    2302                 :          0 :     PyThread_tss_free(ptr_key);
    2303                 :          0 :     ptr_key = NULL;
    2304                 :          0 :     Py_RETURN_NONE;
    2305                 :            : }
    2306                 :            : 
    2307                 :            : 
    2308                 :            : static PyObject*
    2309                 :          0 : new_hamt(PyObject *self, PyObject *args)
    2310                 :            : {
    2311                 :          0 :     return _PyContext_NewHamtForTests();
    2312                 :            : }
    2313                 :            : 
    2314                 :            : 
    2315                 :            : /* def bad_get(self, obj, cls):
    2316                 :            :        cls()
    2317                 :            :        return repr(self)
    2318                 :            : */
    2319                 :            : static PyObject*
    2320                 :          0 : bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
    2321                 :            : {
    2322                 :            :     PyObject *self, *obj, *cls;
    2323         [ #  # ]:          0 :     if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) {
    2324                 :          0 :         return NULL;
    2325                 :            :     }
    2326                 :            : 
    2327                 :          0 :     PyObject *res = PyObject_CallNoArgs(cls);
    2328         [ #  # ]:          0 :     if (res == NULL) {
    2329                 :          0 :         return NULL;
    2330                 :            :     }
    2331                 :          0 :     Py_DECREF(res);
    2332                 :            : 
    2333                 :          0 :     return PyObject_Repr(self);
    2334                 :            : }
    2335                 :            : 
    2336                 :            : 
    2337                 :            : #ifdef Py_REF_DEBUG
    2338                 :            : static PyObject *
    2339                 :            : negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
    2340                 :            : {
    2341                 :            :     PyObject *obj = PyUnicode_FromString("negative_refcount");
    2342                 :            :     if (obj == NULL) {
    2343                 :            :         return NULL;
    2344                 :            :     }
    2345                 :            :     assert(Py_REFCNT(obj) == 1);
    2346                 :            : 
    2347                 :            :     Py_SET_REFCNT(obj,  0);
    2348                 :            :     /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */
    2349                 :            :     Py_DECREF(obj);
    2350                 :            : 
    2351                 :            :     Py_RETURN_NONE;
    2352                 :            : }
    2353                 :            : #endif
    2354                 :            : 
    2355                 :            : 
    2356                 :            : static PyObject *
    2357                 :          0 : sequence_getitem(PyObject *self, PyObject *args)
    2358                 :            : {
    2359                 :            :     PyObject *seq;
    2360                 :            :     Py_ssize_t i;
    2361         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "On", &seq, &i)) {
    2362                 :          0 :         return NULL;
    2363                 :            :     }
    2364                 :          0 :     return PySequence_GetItem(seq, i);
    2365                 :            : }
    2366                 :            : 
    2367                 :            : 
    2368                 :            : static PyObject *
    2369                 :          0 : sequence_setitem(PyObject *self, PyObject *args)
    2370                 :            : {
    2371                 :            :     Py_ssize_t i;
    2372                 :            :     PyObject *seq, *val;
    2373         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OnO", &seq, &i, &val)) {
    2374                 :          0 :         return NULL;
    2375                 :            :     }
    2376         [ #  # ]:          0 :     if (PySequence_SetItem(seq, i, val)) {
    2377                 :          0 :         return NULL;
    2378                 :            :     }
    2379                 :          0 :     Py_RETURN_NONE;
    2380                 :            : }
    2381                 :            : 
    2382                 :            : 
    2383                 :            : static PyObject *
    2384                 :          0 : sequence_delitem(PyObject *self, PyObject *args)
    2385                 :            : {
    2386                 :            :     Py_ssize_t i;
    2387                 :            :     PyObject *seq;
    2388         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "On", &seq, &i)) {
    2389                 :          0 :         return NULL;
    2390                 :            :     }
    2391         [ #  # ]:          0 :     if (PySequence_DelItem(seq, i)) {
    2392                 :          0 :         return NULL;
    2393                 :            :     }
    2394                 :          0 :     Py_RETURN_NONE;
    2395                 :            : }
    2396                 :            : 
    2397                 :            : static PyObject *
    2398                 :          0 : hasattr_string(PyObject *self, PyObject* args)
    2399                 :            : {
    2400                 :            :     PyObject* obj;
    2401                 :            :     PyObject* attr_name;
    2402                 :            : 
    2403         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(args, "hasattr_string", 2, 2, &obj, &attr_name)) {
    2404                 :          0 :         return NULL;
    2405                 :            :     }
    2406                 :            : 
    2407         [ #  # ]:          0 :     if (!PyUnicode_Check(attr_name)) {
    2408                 :          0 :         PyErr_SetString(PyExc_TypeError, "attribute name must a be string");
    2409                 :          0 :         return PyErr_Occurred();
    2410                 :            :     }
    2411                 :            : 
    2412                 :          0 :     const char *name_str = PyUnicode_AsUTF8(attr_name);
    2413         [ #  # ]:          0 :     if (PyObject_HasAttrString(obj, name_str)) {
    2414                 :          0 :         Py_RETURN_TRUE;
    2415                 :            :     }
    2416                 :            :     else {
    2417                 :          0 :         Py_RETURN_FALSE;
    2418                 :            :     }
    2419                 :            : }
    2420                 :            : 
    2421                 :            : 
    2422                 :            : /* Functions for testing C calling conventions (METH_*) are named meth_*,
    2423                 :            :  * e.g. "meth_varargs" for METH_VARARGS.
    2424                 :            :  *
    2425                 :            :  * They all return a tuple of their C-level arguments, with None instead
    2426                 :            :  * of NULL and Python tuples instead of C arrays.
    2427                 :            :  */
    2428                 :            : 
    2429                 :            : 
    2430                 :            : static PyObject*
    2431                 :          0 : _null_to_none(PyObject* obj)
    2432                 :            : {
    2433         [ #  # ]:          0 :     if (obj == NULL) {
    2434                 :          0 :         Py_RETURN_NONE;
    2435                 :            :     }
    2436                 :          0 :     return Py_NewRef(obj);
    2437                 :            : }
    2438                 :            : 
    2439                 :            : static PyObject*
    2440                 :          0 : meth_varargs(PyObject* self, PyObject* args)
    2441                 :            : {
    2442                 :          0 :     return Py_BuildValue("NO", _null_to_none(self), args);
    2443                 :            : }
    2444                 :            : 
    2445                 :            : static PyObject*
    2446                 :          0 : meth_varargs_keywords(PyObject* self, PyObject* args, PyObject* kwargs)
    2447                 :            : {
    2448                 :          0 :     return Py_BuildValue("NON", _null_to_none(self), args, _null_to_none(kwargs));
    2449                 :            : }
    2450                 :            : 
    2451                 :            : static PyObject*
    2452                 :          0 : meth_o(PyObject* self, PyObject* obj)
    2453                 :            : {
    2454                 :          0 :     return Py_BuildValue("NO", _null_to_none(self), obj);
    2455                 :            : }
    2456                 :            : 
    2457                 :            : static PyObject*
    2458                 :          0 : meth_noargs(PyObject* self, PyObject* ignored)
    2459                 :            : {
    2460                 :          0 :     return _null_to_none(self);
    2461                 :            : }
    2462                 :            : 
    2463                 :            : static PyObject*
    2464                 :          0 : _fastcall_to_tuple(PyObject* const* args, Py_ssize_t nargs)
    2465                 :            : {
    2466                 :          0 :     PyObject *tuple = PyTuple_New(nargs);
    2467         [ #  # ]:          0 :     if (tuple == NULL) {
    2468                 :          0 :         return NULL;
    2469                 :            :     }
    2470         [ #  # ]:          0 :     for (Py_ssize_t i=0; i < nargs; i++) {
    2471                 :          0 :         Py_INCREF(args[i]);
    2472                 :          0 :         PyTuple_SET_ITEM(tuple, i, args[i]);
    2473                 :            :     }
    2474                 :          0 :     return tuple;
    2475                 :            : }
    2476                 :            : 
    2477                 :            : static PyObject*
    2478                 :          0 : meth_fastcall(PyObject* self, PyObject* const* args, Py_ssize_t nargs)
    2479                 :            : {
    2480                 :          0 :     return Py_BuildValue(
    2481                 :            :         "NN", _null_to_none(self), _fastcall_to_tuple(args, nargs)
    2482                 :            :     );
    2483                 :            : }
    2484                 :            : 
    2485                 :            : static PyObject*
    2486                 :          0 : meth_fastcall_keywords(PyObject* self, PyObject* const* args,
    2487                 :            :                        Py_ssize_t nargs, PyObject* kwargs)
    2488                 :            : {
    2489                 :          0 :     PyObject *pyargs = _fastcall_to_tuple(args, nargs);
    2490         [ #  # ]:          0 :     if (pyargs == NULL) {
    2491                 :          0 :         return NULL;
    2492                 :            :     }
    2493   [ #  #  #  # ]:          0 :     assert(args != NULL || nargs == 0);
    2494         [ #  # ]:          0 :     PyObject* const* args_offset = args == NULL ? NULL : args + nargs;
    2495                 :          0 :     PyObject *pykwargs = PyObject_Vectorcall((PyObject*)&PyDict_Type,
    2496                 :            :                                               args_offset, 0, kwargs);
    2497                 :          0 :     return Py_BuildValue("NNN", _null_to_none(self), pyargs, pykwargs);
    2498                 :            : }
    2499                 :            : 
    2500                 :            : static PyObject*
    2501                 :          0 : pynumber_tobase(PyObject *module, PyObject *args)
    2502                 :            : {
    2503                 :            :     PyObject *obj;
    2504                 :            :     int base;
    2505         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "Oi:pynumber_tobase",
    2506                 :            :                           &obj, &base)) {
    2507                 :          0 :         return NULL;
    2508                 :            :     }
    2509                 :          0 :     return PyNumber_ToBase(obj, base);
    2510                 :            : }
    2511                 :            : 
    2512                 :            : static PyObject*
    2513                 :          0 : test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
    2514                 :            : {
    2515                 :          0 :     PyObject *obj = PyList_New(0);
    2516         [ #  # ]:          0 :     if (obj == NULL) {
    2517                 :          0 :         return NULL;
    2518                 :            :     }
    2519                 :            : 
    2520                 :            :     // Ensure that following tests don't modify the object,
    2521                 :            :     // to ensure that Py_DECREF() will not crash.
    2522         [ #  # ]:          0 :     assert(Py_TYPE(obj) == &PyList_Type);
    2523         [ #  # ]:          0 :     assert(Py_SIZE(obj) == 0);
    2524                 :            : 
    2525                 :            :     // bpo-39573: Test Py_SET_TYPE() and Py_SET_SIZE() functions.
    2526                 :          0 :     Py_SET_TYPE(obj, &PyList_Type);
    2527                 :          0 :     Py_SET_SIZE(obj, 0);
    2528                 :            : 
    2529                 :          0 :     Py_DECREF(obj);
    2530                 :          0 :     Py_RETURN_NONE;
    2531                 :            : }
    2532                 :            : 
    2533                 :            : 
    2534                 :            : // Test Py_CLEAR() macro
    2535                 :            : static PyObject*
    2536                 :          0 : test_py_clear(PyObject *self, PyObject *Py_UNUSED(ignored))
    2537                 :            : {
    2538                 :            :     // simple case with a variable
    2539                 :          0 :     PyObject *obj = PyList_New(0);
    2540         [ #  # ]:          0 :     if (obj == NULL) {
    2541                 :          0 :         return NULL;
    2542                 :            :     }
    2543         [ #  # ]:          0 :     Py_CLEAR(obj);
    2544         [ #  # ]:          0 :     assert(obj == NULL);
    2545                 :            : 
    2546                 :            :     // gh-98724: complex case, Py_CLEAR() argument has a side effect
    2547                 :            :     PyObject* array[1];
    2548                 :          0 :     array[0] = PyList_New(0);
    2549         [ #  # ]:          0 :     if (array[0] == NULL) {
    2550                 :          0 :         return NULL;
    2551                 :            :     }
    2552                 :            : 
    2553                 :          0 :     PyObject **p = array;
    2554         [ #  # ]:          0 :     Py_CLEAR(*p++);
    2555         [ #  # ]:          0 :     assert(array[0] == NULL);
    2556         [ #  # ]:          0 :     assert(p == array + 1);
    2557                 :            : 
    2558                 :          0 :     Py_RETURN_NONE;
    2559                 :            : }
    2560                 :            : 
    2561                 :            : 
    2562                 :            : // Test Py_SETREF() and Py_XSETREF() macros, similar to test_py_clear()
    2563                 :            : static PyObject*
    2564                 :          0 : test_py_setref(PyObject *self, PyObject *Py_UNUSED(ignored))
    2565                 :            : {
    2566                 :            :     // Py_SETREF() simple case with a variable
    2567                 :          0 :     PyObject *obj = PyList_New(0);
    2568         [ #  # ]:          0 :     if (obj == NULL) {
    2569                 :          0 :         return NULL;
    2570                 :            :     }
    2571                 :          0 :     Py_SETREF(obj, NULL);
    2572         [ #  # ]:          0 :     assert(obj == NULL);
    2573                 :            : 
    2574                 :            :     // Py_XSETREF() simple case with a variable
    2575                 :          0 :     PyObject *obj2 = PyList_New(0);
    2576         [ #  # ]:          0 :     if (obj2 == NULL) {
    2577                 :          0 :         return NULL;
    2578                 :            :     }
    2579                 :          0 :     Py_XSETREF(obj2, NULL);
    2580         [ #  # ]:          0 :     assert(obj2 == NULL);
    2581                 :            :     // test Py_XSETREF() when the argument is NULL
    2582                 :          0 :     Py_XSETREF(obj2, NULL);
    2583         [ #  # ]:          0 :     assert(obj2 == NULL);
    2584                 :            : 
    2585                 :            :     // gh-98724: complex case, Py_SETREF() argument has a side effect
    2586                 :            :     PyObject* array[1];
    2587                 :          0 :     array[0] = PyList_New(0);
    2588         [ #  # ]:          0 :     if (array[0] == NULL) {
    2589                 :          0 :         return NULL;
    2590                 :            :     }
    2591                 :            : 
    2592                 :          0 :     PyObject **p = array;
    2593                 :          0 :     Py_SETREF(*p++, NULL);
    2594         [ #  # ]:          0 :     assert(array[0] == NULL);
    2595         [ #  # ]:          0 :     assert(p == array + 1);
    2596                 :            : 
    2597                 :            :     // gh-98724: complex case, Py_XSETREF() argument has a side effect
    2598                 :            :     PyObject* array2[1];
    2599                 :          0 :     array2[0] = PyList_New(0);
    2600         [ #  # ]:          0 :     if (array2[0] == NULL) {
    2601                 :          0 :         return NULL;
    2602                 :            :     }
    2603                 :            : 
    2604                 :          0 :     PyObject **p2 = array2;
    2605                 :          0 :     Py_XSETREF(*p2++, NULL);
    2606         [ #  # ]:          0 :     assert(array2[0] == NULL);
    2607         [ #  # ]:          0 :     assert(p2 == array2 + 1);
    2608                 :            : 
    2609                 :            :     // test Py_XSETREF() when the argument is NULL
    2610                 :          0 :     p2 = array2;
    2611                 :          0 :     Py_XSETREF(*p2++, NULL);
    2612         [ #  # ]:          0 :     assert(array2[0] == NULL);
    2613         [ #  # ]:          0 :     assert(p2 == array2 + 1);
    2614                 :            : 
    2615                 :          0 :     Py_RETURN_NONE;
    2616                 :            : }
    2617                 :            : 
    2618                 :            : 
    2619                 :            : #define TEST_REFCOUNT() \
    2620                 :            :     do { \
    2621                 :            :         PyObject *obj = PyList_New(0); \
    2622                 :            :         if (obj == NULL) { \
    2623                 :            :             return NULL; \
    2624                 :            :         } \
    2625                 :            :         assert(Py_REFCNT(obj) == 1); \
    2626                 :            :         \
    2627                 :            :         /* test Py_NewRef() */ \
    2628                 :            :         PyObject *ref = Py_NewRef(obj); \
    2629                 :            :         assert(ref == obj); \
    2630                 :            :         assert(Py_REFCNT(obj) == 2); \
    2631                 :            :         Py_DECREF(ref); \
    2632                 :            :         \
    2633                 :            :         /* test Py_XNewRef() */ \
    2634                 :            :         PyObject *xref = Py_XNewRef(obj); \
    2635                 :            :         assert(xref == obj); \
    2636                 :            :         assert(Py_REFCNT(obj) == 2); \
    2637                 :            :         Py_DECREF(xref); \
    2638                 :            :         \
    2639                 :            :         assert(Py_XNewRef(NULL) == NULL); \
    2640                 :            :         \
    2641                 :            :         Py_DECREF(obj); \
    2642                 :            :         Py_RETURN_NONE; \
    2643                 :            :     } while (0) \
    2644                 :            : 
    2645                 :            : 
    2646                 :            : // Test Py_NewRef() and Py_XNewRef() macros
    2647                 :            : static PyObject*
    2648                 :          0 : test_refcount_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
    2649                 :            : {
    2650   [ #  #  #  #  :          0 :     TEST_REFCOUNT();
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2651                 :            : }
    2652                 :            : 
    2653                 :            : #undef Py_NewRef
    2654                 :            : #undef Py_XNewRef
    2655                 :            : 
    2656                 :            : // Test Py_NewRef() and Py_XNewRef() functions, after undefining macros.
    2657                 :            : static PyObject*
    2658                 :          0 : test_refcount_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
    2659                 :            : {
    2660   [ #  #  #  #  :          0 :     TEST_REFCOUNT();
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2661                 :            : }
    2662                 :            : 
    2663                 :            : 
    2664                 :            : // Test Py_Is() function
    2665                 :            : #define TEST_PY_IS() \
    2666                 :            :     do { \
    2667                 :            :         PyObject *o_none = Py_None; \
    2668                 :            :         PyObject *o_true = Py_True; \
    2669                 :            :         PyObject *o_false = Py_False; \
    2670                 :            :         PyObject *obj = PyList_New(0); \
    2671                 :            :         if (obj == NULL) { \
    2672                 :            :             return NULL; \
    2673                 :            :         } \
    2674                 :            :         \
    2675                 :            :         /* test Py_Is() */ \
    2676                 :            :         assert(Py_Is(obj, obj)); \
    2677                 :            :         assert(!Py_Is(obj, o_none)); \
    2678                 :            :         \
    2679                 :            :         /* test Py_None */ \
    2680                 :            :         assert(Py_Is(o_none, o_none)); \
    2681                 :            :         assert(!Py_Is(obj, o_none)); \
    2682                 :            :         \
    2683                 :            :         /* test Py_True */ \
    2684                 :            :         assert(Py_Is(o_true, o_true)); \
    2685                 :            :         assert(!Py_Is(o_false, o_true)); \
    2686                 :            :         assert(!Py_Is(obj, o_true)); \
    2687                 :            :         \
    2688                 :            :         /* test Py_False */ \
    2689                 :            :         assert(Py_Is(o_false, o_false)); \
    2690                 :            :         assert(!Py_Is(o_true, o_false)); \
    2691                 :            :         assert(!Py_Is(obj, o_false)); \
    2692                 :            :         \
    2693                 :            :         Py_DECREF(obj); \
    2694                 :            :         Py_RETURN_NONE; \
    2695                 :            :     } while (0)
    2696                 :            : 
    2697                 :            : // Test Py_Is() macro
    2698                 :            : static PyObject*
    2699                 :          0 : test_py_is_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
    2700                 :            : {
    2701   [ #  #  #  #  :          0 :     TEST_PY_IS();
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2702                 :            : }
    2703                 :            : 
    2704                 :            : #undef Py_Is
    2705                 :            : 
    2706                 :            : // Test Py_Is() function, after undefining its macro.
    2707                 :            : static PyObject*
    2708                 :          0 : test_py_is_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
    2709                 :            : {
    2710   [ #  #  #  #  :          0 :     TEST_PY_IS();
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2711                 :            : }
    2712                 :            : 
    2713                 :            : 
    2714                 :            : // type->tp_version_tag
    2715                 :            : static PyObject *
    2716                 :          0 : type_get_version(PyObject *self, PyObject *type)
    2717                 :            : {
    2718         [ #  # ]:          0 :     if (!PyType_Check(type)) {
    2719                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a type");
    2720                 :          0 :         return NULL;
    2721                 :            :     }
    2722                 :          0 :     PyObject *res = PyLong_FromUnsignedLong(
    2723                 :          0 :         ((PyTypeObject *)type)->tp_version_tag);
    2724         [ #  # ]:          0 :     if (res == NULL) {
    2725         [ #  # ]:          0 :         assert(PyErr_Occurred());
    2726                 :          0 :         return NULL;
    2727                 :            :     }
    2728                 :          0 :     return res;
    2729                 :            : }
    2730                 :            : 
    2731                 :            : 
    2732                 :            : // Test PyThreadState C API
    2733                 :            : static PyObject *
    2734                 :          0 : test_tstate_capi(PyObject *self, PyObject *Py_UNUSED(args))
    2735                 :            : {
    2736                 :            :     // PyThreadState_Get()
    2737                 :          0 :     PyThreadState *tstate = PyThreadState_Get();
    2738         [ #  # ]:          0 :     assert(tstate != NULL);
    2739                 :            : 
    2740                 :            :     // PyThreadState_GET()
    2741                 :          0 :     PyThreadState *tstate2 = PyThreadState_Get();
    2742         [ #  # ]:          0 :     assert(tstate2 == tstate);
    2743                 :            : 
    2744                 :            :     // private _PyThreadState_UncheckedGet()
    2745                 :          0 :     PyThreadState *tstate3 = _PyThreadState_UncheckedGet();
    2746         [ #  # ]:          0 :     assert(tstate3 == tstate);
    2747                 :            : 
    2748                 :            :     // PyThreadState_EnterTracing(), PyThreadState_LeaveTracing()
    2749                 :          0 :     PyThreadState_EnterTracing(tstate);
    2750                 :          0 :     PyThreadState_LeaveTracing(tstate);
    2751                 :            : 
    2752                 :            :     // PyThreadState_GetDict(): no tstate argument
    2753                 :          0 :     PyObject *dict = PyThreadState_GetDict();
    2754                 :            :     // PyThreadState_GetDict() API can return NULL if PyDict_New() fails,
    2755                 :            :     // but it should not occur in practice.
    2756         [ #  # ]:          0 :     assert(dict != NULL);
    2757         [ #  # ]:          0 :     assert(PyDict_Check(dict));
    2758                 :            :     // dict is a borrowed reference
    2759                 :            : 
    2760                 :            :     // private _PyThreadState_GetDict()
    2761                 :          0 :     PyObject *dict2 = _PyThreadState_GetDict(tstate);
    2762         [ #  # ]:          0 :     assert(dict2 == dict);
    2763                 :            :     // dict2 is a borrowed reference
    2764                 :            : 
    2765                 :            :     // PyThreadState_GetInterpreter()
    2766                 :          0 :     PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate);
    2767         [ #  # ]:          0 :     assert(interp != NULL);
    2768                 :            : 
    2769                 :            :     // PyThreadState_GetFrame()
    2770                 :          0 :     PyFrameObject*frame = PyThreadState_GetFrame(tstate);
    2771         [ #  # ]:          0 :     assert(frame != NULL);
    2772         [ #  # ]:          0 :     assert(PyFrame_Check(frame));
    2773                 :          0 :     Py_DECREF(frame);
    2774                 :            : 
    2775                 :            :     // PyThreadState_GetID()
    2776                 :          0 :     uint64_t id = PyThreadState_GetID(tstate);
    2777         [ #  # ]:          0 :     assert(id >= 1);
    2778                 :            : 
    2779                 :          0 :     Py_RETURN_NONE;
    2780                 :            : }
    2781                 :            : 
    2782                 :            : static PyObject *
    2783                 :          0 : frame_getlocals(PyObject *self, PyObject *frame)
    2784                 :            : {
    2785         [ #  # ]:          0 :     if (!PyFrame_Check(frame)) {
    2786                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a frame");
    2787                 :          0 :         return NULL;
    2788                 :            :     }
    2789                 :          0 :     return PyFrame_GetLocals((PyFrameObject *)frame);
    2790                 :            : }
    2791                 :            : 
    2792                 :            : static PyObject *
    2793                 :          0 : frame_getglobals(PyObject *self, PyObject *frame)
    2794                 :            : {
    2795         [ #  # ]:          0 :     if (!PyFrame_Check(frame)) {
    2796                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a frame");
    2797                 :          0 :         return NULL;
    2798                 :            :     }
    2799                 :          0 :     return PyFrame_GetGlobals((PyFrameObject *)frame);
    2800                 :            : }
    2801                 :            : 
    2802                 :            : static PyObject *
    2803                 :          0 : frame_getgenerator(PyObject *self, PyObject *frame)
    2804                 :            : {
    2805         [ #  # ]:          0 :     if (!PyFrame_Check(frame)) {
    2806                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a frame");
    2807                 :          0 :         return NULL;
    2808                 :            :     }
    2809                 :          0 :     return PyFrame_GetGenerator((PyFrameObject *)frame);
    2810                 :            : }
    2811                 :            : 
    2812                 :            : static PyObject *
    2813                 :          0 : frame_getbuiltins(PyObject *self, PyObject *frame)
    2814                 :            : {
    2815         [ #  # ]:          0 :     if (!PyFrame_Check(frame)) {
    2816                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a frame");
    2817                 :          0 :         return NULL;
    2818                 :            :     }
    2819                 :          0 :     return PyFrame_GetBuiltins((PyFrameObject *)frame);
    2820                 :            : }
    2821                 :            : 
    2822                 :            : static PyObject *
    2823                 :          0 : frame_getlasti(PyObject *self, PyObject *frame)
    2824                 :            : {
    2825         [ #  # ]:          0 :     if (!PyFrame_Check(frame)) {
    2826                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a frame");
    2827                 :          0 :         return NULL;
    2828                 :            :     }
    2829                 :          0 :     int lasti = PyFrame_GetLasti((PyFrameObject *)frame);
    2830         [ #  # ]:          0 :     if (lasti < 0) {
    2831         [ #  # ]:          0 :         assert(lasti == -1);
    2832                 :          0 :         Py_RETURN_NONE;
    2833                 :            :     }
    2834                 :          0 :     return PyLong_FromLong(lasti);
    2835                 :            : }
    2836                 :            : 
    2837                 :            : static PyObject *
    2838                 :          0 : frame_new(PyObject *self, PyObject *args)
    2839                 :            : {
    2840                 :            :     PyObject *code, *globals, *locals;
    2841         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OOO", &code, &globals, &locals)) {
    2842                 :          0 :         return NULL;
    2843                 :            :     }
    2844         [ #  # ]:          0 :     if (!PyCode_Check(code)) {
    2845                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a code object");
    2846                 :          0 :         return NULL;
    2847                 :            :     }
    2848                 :          0 :     PyThreadState *tstate = PyThreadState_Get();
    2849                 :            : 
    2850                 :          0 :     return (PyObject *)PyFrame_New(tstate, (PyCodeObject *)code, globals, locals);
    2851                 :            : }
    2852                 :            : 
    2853                 :            : static PyObject *
    2854                 :          0 : test_frame_getvar(PyObject *self, PyObject *args)
    2855                 :            : {
    2856                 :            :     PyObject *frame, *name;
    2857         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OO", &frame, &name)) {
    2858                 :          0 :         return NULL;
    2859                 :            :     }
    2860         [ #  # ]:          0 :     if (!PyFrame_Check(frame)) {
    2861                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a frame");
    2862                 :          0 :         return NULL;
    2863                 :            :     }
    2864                 :            : 
    2865                 :          0 :     return PyFrame_GetVar((PyFrameObject *)frame, name);
    2866                 :            : }
    2867                 :            : 
    2868                 :            : static PyObject *
    2869                 :          0 : test_frame_getvarstring(PyObject *self, PyObject *args)
    2870                 :            : {
    2871                 :            :     PyObject *frame;
    2872                 :            :     const char *name;
    2873         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "Oy", &frame, &name)) {
    2874                 :          0 :         return NULL;
    2875                 :            :     }
    2876         [ #  # ]:          0 :     if (!PyFrame_Check(frame)) {
    2877                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a frame");
    2878                 :          0 :         return NULL;
    2879                 :            :     }
    2880                 :            : 
    2881                 :          0 :     return PyFrame_GetVarString((PyFrameObject *)frame, name);
    2882                 :            : }
    2883                 :            : 
    2884                 :            : 
    2885                 :            : static PyObject *
    2886                 :          0 : eval_get_func_name(PyObject *self, PyObject *func)
    2887                 :            : {
    2888                 :          0 :     return PyUnicode_FromString(PyEval_GetFuncName(func));
    2889                 :            : }
    2890                 :            : 
    2891                 :            : static PyObject *
    2892                 :          0 : eval_get_func_desc(PyObject *self, PyObject *func)
    2893                 :            : {
    2894                 :          0 :     return PyUnicode_FromString(PyEval_GetFuncDesc(func));
    2895                 :            : }
    2896                 :            : 
    2897                 :            : static PyObject *
    2898                 :          0 : gen_get_code(PyObject *self, PyObject *gen)
    2899                 :            : {
    2900         [ #  # ]:          0 :     if (!PyGen_Check(gen)) {
    2901                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a generator object");
    2902                 :          0 :         return NULL;
    2903                 :            :     }
    2904                 :          0 :     return (PyObject *)PyGen_GetCode((PyGenObject *)gen);
    2905                 :            : }
    2906                 :            : 
    2907                 :            : static PyObject *
    2908                 :          0 : eval_eval_code_ex(PyObject *mod, PyObject *pos_args)
    2909                 :            : {
    2910                 :          0 :     PyObject *result = NULL;
    2911                 :            :     PyObject *code;
    2912                 :            :     PyObject *globals;
    2913                 :          0 :     PyObject *locals = NULL;
    2914                 :          0 :     PyObject *args = NULL;
    2915                 :          0 :     PyObject *kwargs = NULL;
    2916                 :          0 :     PyObject *defaults = NULL;
    2917                 :          0 :     PyObject *kw_defaults = NULL;
    2918                 :          0 :     PyObject *closure = NULL;
    2919                 :            : 
    2920                 :          0 :     PyObject **c_kwargs = NULL;
    2921                 :            : 
    2922         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(pos_args,
    2923                 :            :                            "eval_code_ex",
    2924                 :            :                            2,
    2925                 :            :                            8,
    2926                 :            :                            &code,
    2927                 :            :                            &globals,
    2928                 :            :                            &locals,
    2929                 :            :                            &args,
    2930                 :            :                            &kwargs,
    2931                 :            :                            &defaults,
    2932                 :            :                            &kw_defaults,
    2933                 :            :                            &closure))
    2934                 :            :     {
    2935                 :          0 :         goto exit;
    2936                 :            :     }
    2937                 :            : 
    2938         [ #  # ]:          0 :     if (!PyCode_Check(code)) {
    2939                 :          0 :         PyErr_SetString(PyExc_TypeError,
    2940                 :            :                         "code must be a Python code object");
    2941                 :          0 :         goto exit;
    2942                 :            :     }
    2943                 :            : 
    2944         [ #  # ]:          0 :     if (!PyDict_Check(globals)) {
    2945                 :          0 :         PyErr_SetString(PyExc_TypeError, "globals must be a dict");
    2946                 :          0 :         goto exit;
    2947                 :            :     }
    2948                 :            : 
    2949   [ #  #  #  # ]:          0 :     if (locals && !PyMapping_Check(locals)) {
    2950                 :          0 :         PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
    2951                 :          0 :         goto exit;
    2952                 :            :     }
    2953         [ #  # ]:          0 :     if (locals == Py_None) {
    2954                 :          0 :         locals = NULL;
    2955                 :            :     }
    2956                 :            : 
    2957                 :          0 :     PyObject **c_args = NULL;
    2958                 :          0 :     Py_ssize_t c_args_len = 0;
    2959                 :            : 
    2960         [ #  # ]:          0 :     if (args)
    2961                 :            :     {
    2962         [ #  # ]:          0 :         if (!PyTuple_Check(args)) {
    2963                 :          0 :             PyErr_SetString(PyExc_TypeError, "args must be a tuple");
    2964                 :          0 :             goto exit;
    2965                 :            :         } else {
    2966         [ #  # ]:          0 :             c_args = &PyTuple_GET_ITEM(args, 0);
    2967                 :          0 :             c_args_len = PyTuple_Size(args);
    2968                 :            :         }
    2969                 :            :     }
    2970                 :            : 
    2971                 :          0 :     Py_ssize_t c_kwargs_len = 0;
    2972                 :            : 
    2973         [ #  # ]:          0 :     if (kwargs)
    2974                 :            :     {
    2975         [ #  # ]:          0 :         if (!PyDict_Check(kwargs)) {
    2976                 :          0 :             PyErr_SetString(PyExc_TypeError, "keywords must be a dict");
    2977                 :          0 :             goto exit;
    2978                 :            :         } else {
    2979                 :          0 :             c_kwargs_len = PyDict_Size(kwargs);
    2980         [ #  # ]:          0 :             if (c_kwargs_len > 0) {
    2981         [ #  # ]:          0 :                 c_kwargs = PyMem_NEW(PyObject*, 2 * c_kwargs_len);
    2982         [ #  # ]:          0 :                 if (!c_kwargs) {
    2983                 :          0 :                     PyErr_NoMemory();
    2984                 :          0 :                     goto exit;
    2985                 :            :                 }
    2986                 :            : 
    2987                 :          0 :                 Py_ssize_t i = 0;
    2988                 :          0 :                 Py_ssize_t pos = 0;
    2989                 :            : 
    2990         [ #  # ]:          0 :                 while (PyDict_Next(kwargs,
    2991                 :            :                                    &pos,
    2992                 :          0 :                                    &c_kwargs[i],
    2993                 :          0 :                                    &c_kwargs[i + 1]))
    2994                 :            :                 {
    2995                 :          0 :                     i += 2;
    2996                 :            :                 }
    2997                 :          0 :                 c_kwargs_len = i / 2;
    2998                 :            :                 /* XXX This is broken if the caller deletes dict items! */
    2999                 :            :             }
    3000                 :            :         }
    3001                 :            :     }
    3002                 :            : 
    3003                 :            : 
    3004                 :          0 :     PyObject **c_defaults = NULL;
    3005                 :          0 :     Py_ssize_t c_defaults_len = 0;
    3006                 :            : 
    3007   [ #  #  #  # ]:          0 :     if (defaults && PyTuple_Check(defaults)) {
    3008         [ #  # ]:          0 :         c_defaults = &PyTuple_GET_ITEM(defaults, 0);
    3009                 :          0 :         c_defaults_len = PyTuple_Size(defaults);
    3010                 :            :     }
    3011                 :            : 
    3012   [ #  #  #  # ]:          0 :     if (kw_defaults && !PyDict_Check(kw_defaults)) {
    3013                 :          0 :         PyErr_SetString(PyExc_TypeError, "kw_defaults must be a dict");
    3014                 :          0 :         goto exit;
    3015                 :            :     }
    3016                 :            : 
    3017   [ #  #  #  # ]:          0 :     if (closure && !PyTuple_Check(closure)) {
    3018                 :          0 :         PyErr_SetString(PyExc_TypeError, "closure must be a tuple of cells");
    3019                 :          0 :         goto exit;
    3020                 :            :     }
    3021                 :            : 
    3022                 :            : 
    3023                 :          0 :     result = PyEval_EvalCodeEx(
    3024                 :            :         code,
    3025                 :            :         globals,
    3026                 :            :         locals,
    3027                 :            :         c_args,
    3028                 :            :         (int)c_args_len,
    3029                 :            :         c_kwargs,
    3030                 :            :         (int)c_kwargs_len,
    3031                 :            :         c_defaults,
    3032                 :            :         (int)c_defaults_len,
    3033                 :            :         kw_defaults,
    3034                 :            :         closure
    3035                 :            :     );
    3036                 :            : 
    3037                 :          0 : exit:
    3038         [ #  # ]:          0 :     if (c_kwargs) {
    3039                 :          0 :         PyMem_DEL(c_kwargs);
    3040                 :            :     }
    3041                 :            : 
    3042                 :          0 :     return result;
    3043                 :            : }
    3044                 :            : 
    3045                 :            : static PyObject *
    3046                 :          0 : get_feature_macros(PyObject *self, PyObject *Py_UNUSED(args))
    3047                 :            : {
    3048                 :          0 :     PyObject *result = PyDict_New();
    3049         [ #  # ]:          0 :     if (!result) {
    3050                 :          0 :         return NULL;
    3051                 :            :     }
    3052                 :            :     int res;
    3053                 :            : #include "_testcapi_feature_macros.inc"
    3054                 :          0 :     return result;
    3055                 :            : }
    3056                 :            : 
    3057                 :            : static PyObject *
    3058                 :          0 : test_code_api(PyObject *self, PyObject *Py_UNUSED(args))
    3059                 :            : {
    3060                 :          0 :     PyCodeObject *co = PyCode_NewEmpty("_testcapi", "dummy", 1);
    3061         [ #  # ]:          0 :     if (co == NULL) {
    3062                 :          0 :         return NULL;
    3063                 :            :     }
    3064                 :            :     /* co_code */
    3065                 :            :     {
    3066                 :          0 :         PyObject *co_code = PyCode_GetCode(co);
    3067         [ #  # ]:          0 :         if (co_code == NULL) {
    3068                 :          0 :             goto fail;
    3069                 :            :         }
    3070         [ #  # ]:          0 :         assert(PyBytes_CheckExact(co_code));
    3071         [ #  # ]:          0 :         if (PyObject_Length(co_code) == 0) {
    3072                 :          0 :             PyErr_SetString(PyExc_ValueError, "empty co_code");
    3073                 :          0 :             Py_DECREF(co_code);
    3074                 :          0 :             goto fail;
    3075                 :            :         }
    3076                 :          0 :         Py_DECREF(co_code);
    3077                 :            :     }
    3078                 :            :     /* co_varnames */
    3079                 :            :     {
    3080                 :          0 :         PyObject *co_varnames = PyCode_GetVarnames(co);
    3081         [ #  # ]:          0 :         if (co_varnames == NULL) {
    3082                 :          0 :             goto fail;
    3083                 :            :         }
    3084         [ #  # ]:          0 :         if (!PyTuple_CheckExact(co_varnames)) {
    3085                 :          0 :             PyErr_SetString(PyExc_TypeError, "co_varnames not tuple");
    3086                 :          0 :             Py_DECREF(co_varnames);
    3087                 :          0 :             goto fail;
    3088                 :            :         }
    3089         [ #  # ]:          0 :         if (PyTuple_GET_SIZE(co_varnames) != 0) {
    3090                 :          0 :             PyErr_SetString(PyExc_ValueError, "non-empty co_varnames");
    3091                 :          0 :             Py_DECREF(co_varnames);
    3092                 :          0 :             goto fail;
    3093                 :            :         }
    3094                 :          0 :         Py_DECREF(co_varnames);
    3095                 :            :     }
    3096                 :            :     /* co_cellvars */
    3097                 :            :     {
    3098                 :          0 :         PyObject *co_cellvars = PyCode_GetCellvars(co);
    3099         [ #  # ]:          0 :         if (co_cellvars == NULL) {
    3100                 :          0 :             goto fail;
    3101                 :            :         }
    3102         [ #  # ]:          0 :         if (!PyTuple_CheckExact(co_cellvars)) {
    3103                 :          0 :             PyErr_SetString(PyExc_TypeError, "co_cellvars not tuple");
    3104                 :          0 :             Py_DECREF(co_cellvars);
    3105                 :          0 :             goto fail;
    3106                 :            :         }
    3107         [ #  # ]:          0 :         if (PyTuple_GET_SIZE(co_cellvars) != 0) {
    3108                 :          0 :             PyErr_SetString(PyExc_ValueError, "non-empty co_cellvars");
    3109                 :          0 :             Py_DECREF(co_cellvars);
    3110                 :          0 :             goto fail;
    3111                 :            :         }
    3112                 :          0 :         Py_DECREF(co_cellvars);
    3113                 :            :     }
    3114                 :            :     /* co_freevars */
    3115                 :            :     {
    3116                 :          0 :         PyObject *co_freevars = PyCode_GetFreevars(co);
    3117         [ #  # ]:          0 :         if (co_freevars == NULL) {
    3118                 :          0 :             goto fail;
    3119                 :            :         }
    3120         [ #  # ]:          0 :         if (!PyTuple_CheckExact(co_freevars)) {
    3121                 :          0 :             PyErr_SetString(PyExc_TypeError, "co_freevars not tuple");
    3122                 :          0 :             Py_DECREF(co_freevars);
    3123                 :          0 :             goto fail;
    3124                 :            :         }
    3125         [ #  # ]:          0 :         if (PyTuple_GET_SIZE(co_freevars) != 0) {
    3126                 :          0 :             PyErr_SetString(PyExc_ValueError, "non-empty co_freevars");
    3127                 :          0 :             Py_DECREF(co_freevars);
    3128                 :          0 :             goto fail;
    3129                 :            :         }
    3130                 :          0 :         Py_DECREF(co_freevars);
    3131                 :            :     }
    3132                 :          0 :     Py_DECREF(co);
    3133                 :          0 :     Py_RETURN_NONE;
    3134                 :          0 : fail:
    3135                 :          0 :     Py_DECREF(co);
    3136                 :          0 :     return NULL;
    3137                 :            : }
    3138                 :            : 
    3139                 :            : static int
    3140                 :          0 : record_func(PyObject *obj, PyFrameObject *f, int what, PyObject *arg)
    3141                 :            : {
    3142         [ #  # ]:          0 :     assert(PyList_Check(obj));
    3143                 :          0 :     PyObject *what_obj = NULL;
    3144                 :          0 :     PyObject *line_obj = NULL;
    3145                 :          0 :     PyObject *tuple = NULL;
    3146                 :          0 :     int res = -1;
    3147                 :          0 :     what_obj = PyLong_FromLong(what);
    3148         [ #  # ]:          0 :     if (what_obj == NULL) {
    3149                 :          0 :         goto error;
    3150                 :            :     }
    3151                 :          0 :     int line = PyFrame_GetLineNumber(f);
    3152                 :          0 :     line_obj = PyLong_FromLong(line);
    3153         [ #  # ]:          0 :     if (line_obj == NULL) {
    3154                 :          0 :         goto error;
    3155                 :            :     }
    3156                 :          0 :     tuple = PyTuple_Pack(3, what_obj, line_obj, arg);
    3157         [ #  # ]:          0 :     if (tuple == NULL) {
    3158                 :          0 :         goto error;
    3159                 :            :     }
    3160                 :          0 :     PyTuple_SET_ITEM(tuple, 0, what_obj);
    3161         [ #  # ]:          0 :     if (PyList_Append(obj, tuple)) {
    3162                 :          0 :         goto error;
    3163                 :            :     }
    3164                 :          0 :     res = 0;
    3165                 :          0 : error:
    3166                 :          0 :     Py_XDECREF(what_obj);
    3167                 :          0 :     Py_XDECREF(line_obj);
    3168                 :          0 :     Py_XDECREF(tuple);
    3169                 :          0 :     return res;
    3170                 :            : }
    3171                 :            : 
    3172                 :            : static PyObject *
    3173                 :          0 : settrace_to_record(PyObject *self, PyObject *list)
    3174                 :            : {
    3175                 :            : 
    3176         [ #  # ]:          0 :    if (!PyList_Check(list)) {
    3177                 :          0 :         PyErr_SetString(PyExc_TypeError, "argument must be a list");
    3178                 :          0 :         return NULL;
    3179                 :            :     }
    3180                 :          0 :     PyEval_SetTrace(record_func, list);
    3181                 :          0 :     Py_RETURN_NONE;
    3182                 :            : }
    3183                 :            : 
    3184                 :            : static PyObject *
    3185                 :          0 : clear_managed_dict(PyObject *self, PyObject *obj)
    3186                 :            : {
    3187                 :          0 :     _PyObject_ClearManagedDict(obj);
    3188                 :          0 :     Py_RETURN_NONE;
    3189                 :            : }
    3190                 :            : 
    3191                 :            : 
    3192                 :            : static PyObject *
    3193                 :          0 : test_macros(PyObject *self, PyObject *Py_UNUSED(args))
    3194                 :            : {
    3195                 :            :     struct MyStruct {
    3196                 :            :         int x;
    3197                 :            :     };
    3198                 :            :     wchar_t array[3];
    3199                 :            : 
    3200                 :            :     // static_assert(), Py_BUILD_ASSERT()
    3201                 :            :     static_assert(1 == 1, "bug");
    3202                 :            :     Py_BUILD_ASSERT(1 == 1);
    3203                 :            : 
    3204                 :            : 
    3205                 :            :     // Py_MIN(), Py_MAX(), Py_ABS()
    3206                 :            :     assert(Py_MIN(5, 11) == 5);
    3207                 :            :     assert(Py_MAX(5, 11) == 11);
    3208                 :            :     assert(Py_ABS(-5) == 5);
    3209                 :            : 
    3210                 :            :     // Py_STRINGIFY()
    3211                 :            :     assert(strcmp(Py_STRINGIFY(123), "123") == 0);
    3212                 :            : 
    3213                 :            :     // Py_MEMBER_SIZE(), Py_ARRAY_LENGTH()
    3214                 :            :     assert(Py_MEMBER_SIZE(struct MyStruct, x) == sizeof(int));
    3215                 :            :     assert(Py_ARRAY_LENGTH(array) == 3);
    3216                 :            : 
    3217                 :            :     // Py_CHARMASK()
    3218                 :          0 :     int c = 0xab00 | 7;
    3219         [ #  # ]:          0 :     assert(Py_CHARMASK(c) == 7);
    3220                 :            : 
    3221                 :            :     // _Py_IS_TYPE_SIGNED()
    3222                 :            :     assert(_Py_IS_TYPE_SIGNED(int));
    3223                 :            :     assert(!_Py_IS_TYPE_SIGNED(unsigned int));
    3224                 :            : 
    3225                 :          0 :     Py_RETURN_NONE;
    3226                 :            : }
    3227                 :            : 
    3228                 :            : static PyObject *
    3229                 :          0 : function_get_code(PyObject *self, PyObject *func)
    3230                 :            : {
    3231                 :          0 :     PyObject *code = PyFunction_GetCode(func);
    3232         [ #  # ]:          0 :     if (code != NULL) {
    3233                 :          0 :         return Py_NewRef(code);
    3234                 :            :     } else {
    3235                 :          0 :         return NULL;
    3236                 :            :     }
    3237                 :            : }
    3238                 :            : 
    3239                 :            : static PyObject *
    3240                 :          0 : function_get_globals(PyObject *self, PyObject *func)
    3241                 :            : {
    3242                 :          0 :     PyObject *globals = PyFunction_GetGlobals(func);
    3243         [ #  # ]:          0 :     if (globals != NULL) {
    3244                 :          0 :         return Py_NewRef(globals);
    3245                 :            :     } else {
    3246                 :          0 :         return NULL;
    3247                 :            :     }
    3248                 :            : }
    3249                 :            : 
    3250                 :            : static PyObject *
    3251                 :          0 : function_get_module(PyObject *self, PyObject *func)
    3252                 :            : {
    3253                 :          0 :     PyObject *module = PyFunction_GetModule(func);
    3254         [ #  # ]:          0 :     if (module != NULL) {
    3255                 :          0 :         return Py_NewRef(module);
    3256                 :            :     } else {
    3257                 :          0 :         return NULL;
    3258                 :            :     }
    3259                 :            : }
    3260                 :            : 
    3261                 :            : static PyObject *
    3262                 :          0 : function_get_defaults(PyObject *self, PyObject *func)
    3263                 :            : {
    3264                 :          0 :     PyObject *defaults = PyFunction_GetDefaults(func);
    3265         [ #  # ]:          0 :     if (defaults != NULL) {
    3266                 :          0 :         return Py_NewRef(defaults);
    3267         [ #  # ]:          0 :     } else if (PyErr_Occurred()) {
    3268                 :          0 :         return NULL;
    3269                 :            :     } else {
    3270                 :          0 :         Py_RETURN_NONE;  // This can happen when `defaults` are set to `None`
    3271                 :            :     }
    3272                 :            : }
    3273                 :            : 
    3274                 :            : static PyObject *
    3275                 :          0 : function_set_defaults(PyObject *self, PyObject *args)
    3276                 :            : {
    3277                 :          0 :     PyObject *func = NULL, *defaults = NULL;
    3278         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OO", &func, &defaults)) {
    3279                 :          0 :         return NULL;
    3280                 :            :     }
    3281                 :          0 :     int result = PyFunction_SetDefaults(func, defaults);
    3282         [ #  # ]:          0 :     if (result == -1)
    3283                 :          0 :         return NULL;
    3284                 :          0 :     Py_RETURN_NONE;
    3285                 :            : }
    3286                 :            : 
    3287                 :            : static PyObject *
    3288                 :          0 : function_get_kw_defaults(PyObject *self, PyObject *func)
    3289                 :            : {
    3290                 :          0 :     PyObject *defaults = PyFunction_GetKwDefaults(func);
    3291         [ #  # ]:          0 :     if (defaults != NULL) {
    3292                 :          0 :         return Py_NewRef(defaults);
    3293         [ #  # ]:          0 :     } else if (PyErr_Occurred()) {
    3294                 :          0 :         return NULL;
    3295                 :            :     } else {
    3296                 :          0 :         Py_RETURN_NONE;  // This can happen when `kwdefaults` are set to `None`
    3297                 :            :     }
    3298                 :            : }
    3299                 :            : 
    3300                 :            : static PyObject *
    3301                 :          0 : function_set_kw_defaults(PyObject *self, PyObject *args)
    3302                 :            : {
    3303                 :          0 :     PyObject *func = NULL, *defaults = NULL;
    3304         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "OO", &func, &defaults)) {
    3305                 :          0 :         return NULL;
    3306                 :            :     }
    3307                 :          0 :     int result = PyFunction_SetKwDefaults(func, defaults);
    3308         [ #  # ]:          0 :     if (result == -1)
    3309                 :          0 :         return NULL;
    3310                 :          0 :     Py_RETURN_NONE;
    3311                 :            : }
    3312                 :            : 
    3313                 :            : struct gc_visit_state_basic {
    3314                 :            :     PyObject *target;
    3315                 :            :     int found;
    3316                 :            : };
    3317                 :            : 
    3318                 :            : static int
    3319                 :          0 : gc_visit_callback_basic(PyObject *obj, void *arg)
    3320                 :            : {
    3321                 :          0 :     struct gc_visit_state_basic *state = (struct gc_visit_state_basic *)arg;
    3322         [ #  # ]:          0 :     if (obj == state->target) {
    3323                 :          0 :         state->found = 1;
    3324                 :          0 :         return 0;
    3325                 :            :     }
    3326                 :          0 :     return 1;
    3327                 :            : }
    3328                 :            : 
    3329                 :            : static PyObject *
    3330                 :          0 : test_gc_visit_objects_basic(PyObject *Py_UNUSED(self),
    3331                 :            :                             PyObject *Py_UNUSED(ignored))
    3332                 :            : {
    3333                 :            :     PyObject *obj;
    3334                 :            :     struct gc_visit_state_basic state;
    3335                 :            : 
    3336                 :          0 :     obj = PyList_New(0);
    3337         [ #  # ]:          0 :     if (obj == NULL) {
    3338                 :          0 :         return NULL;
    3339                 :            :     }
    3340                 :          0 :     state.target = obj;
    3341                 :          0 :     state.found = 0;
    3342                 :            :     
    3343                 :          0 :     PyUnstable_GC_VisitObjects(gc_visit_callback_basic, &state);
    3344                 :          0 :     Py_DECREF(obj);
    3345         [ #  # ]:          0 :     if (!state.found) {
    3346                 :          0 :         PyErr_SetString(
    3347                 :            :              PyExc_AssertionError,
    3348                 :            :              "test_gc_visit_objects_basic: Didn't find live list");
    3349                 :          0 :          return NULL;
    3350                 :            :     }
    3351                 :          0 :     Py_RETURN_NONE;
    3352                 :            : }
    3353                 :            : 
    3354                 :            : static int
    3355                 :          0 : gc_visit_callback_exit_early(PyObject *obj, void *arg)
    3356                 :            :  {
    3357                 :          0 :     int *visited_i = (int *)arg;
    3358                 :          0 :     (*visited_i)++;
    3359         [ #  # ]:          0 :     if (*visited_i == 2) {
    3360                 :          0 :         return 0;
    3361                 :            :     }
    3362                 :          0 :     return 1;
    3363                 :            : }
    3364                 :            : 
    3365                 :            : static PyObject *
    3366                 :          0 : test_gc_visit_objects_exit_early(PyObject *Py_UNUSED(self),
    3367                 :            :                                  PyObject *Py_UNUSED(ignored))
    3368                 :            : {
    3369                 :          0 :     int visited_i = 0;
    3370                 :          0 :     PyUnstable_GC_VisitObjects(gc_visit_callback_exit_early, &visited_i);
    3371         [ #  # ]:          0 :     if (visited_i != 2) {
    3372                 :          0 :         PyErr_SetString(
    3373                 :            :             PyExc_AssertionError,
    3374                 :            :             "test_gc_visit_objects_exit_early: did not exit when expected");
    3375                 :            :     }
    3376                 :          0 :     Py_RETURN_NONE;
    3377                 :            : }
    3378                 :            : 
    3379                 :            : 
    3380                 :            : static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
    3381                 :            : 
    3382                 :            : static PyMethodDef TestMethods[] = {
    3383                 :            :     {"set_errno",               set_errno,                       METH_VARARGS},
    3384                 :            :     {"test_config",             test_config,                     METH_NOARGS},
    3385                 :            :     {"test_sizeof_c_types",     test_sizeof_c_types,             METH_NOARGS},
    3386                 :            :     {"test_gc_control",         test_gc_control,                 METH_NOARGS},
    3387                 :            :     {"test_list_api",           test_list_api,                   METH_NOARGS},
    3388                 :            :     {"test_dict_iteration",     test_dict_iteration,             METH_NOARGS},
    3389                 :            :     {"dict_getitem_knownhash",  dict_getitem_knownhash,          METH_VARARGS},
    3390                 :            :     {"test_lazy_hash_inheritance",      test_lazy_hash_inheritance,METH_NOARGS},
    3391                 :            :     {"test_xincref_doesnt_leak",test_xincref_doesnt_leak,        METH_NOARGS},
    3392                 :            :     {"test_incref_doesnt_leak", test_incref_doesnt_leak,         METH_NOARGS},
    3393                 :            :     {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak,        METH_NOARGS},
    3394                 :            :     {"test_decref_doesnt_leak", test_decref_doesnt_leak,         METH_NOARGS},
    3395                 :            :     {"test_structseq_newtype_doesnt_leak",
    3396                 :            :         test_structseq_newtype_doesnt_leak, METH_NOARGS},
    3397                 :            :     {"test_structseq_newtype_null_descr_doc",
    3398                 :            :         test_structseq_newtype_null_descr_doc, METH_NOARGS},
    3399                 :            :     {"test_incref_decref_API",  test_incref_decref_API,          METH_NOARGS},
    3400                 :            :     {"pyobject_repr_from_null", pyobject_repr_from_null, METH_NOARGS},
    3401                 :            :     {"pyobject_str_from_null",  pyobject_str_from_null, METH_NOARGS},
    3402                 :            :     {"pyobject_bytes_from_null", pyobject_bytes_from_null, METH_NOARGS},
    3403                 :            :     {"test_string_to_double",   test_string_to_double,           METH_NOARGS},
    3404                 :            :     {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
    3405                 :            :     {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
    3406                 :            : #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
    3407                 :            :     {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
    3408                 :            : #endif
    3409                 :            :     {"getbuffer_with_null_view", getbuffer_with_null_view,       METH_O},
    3410                 :            :     {"PyBuffer_SizeFromFormat",  test_PyBuffer_SizeFromFormat,   METH_VARARGS},
    3411                 :            :     {"test_buildvalue_N",        test_buildvalue_N,              METH_NOARGS},
    3412                 :            :     {"test_buildvalue_issue38913", test_buildvalue_issue38913,   METH_NOARGS},
    3413                 :            :     {"test_get_statictype_slots", test_get_statictype_slots,     METH_NOARGS},
    3414                 :            :     {"test_get_type_name",        test_get_type_name,            METH_NOARGS},
    3415                 :            :     {"test_get_type_qualname",    test_get_type_qualname,        METH_NOARGS},
    3416                 :            :     {"_test_thread_state",      test_thread_state,               METH_VARARGS},
    3417                 :            : #ifndef MS_WINDOWS
    3418                 :            :     {"_spawn_pthread_waiter",   spawn_pthread_waiter,            METH_NOARGS},
    3419                 :            :     {"_end_spawned_pthread",    end_spawned_pthread,             METH_NOARGS},
    3420                 :            : #endif
    3421                 :            :     {"_pending_threadfunc",     pending_threadfunc,              METH_VARARGS},
    3422                 :            : #ifdef HAVE_GETTIMEOFDAY
    3423                 :            :     {"profile_int",             profile_int,                     METH_NOARGS},
    3424                 :            : #endif
    3425                 :            :     {"argparsing",              argparsing,                      METH_VARARGS},
    3426                 :            :     {"code_newempty",           code_newempty,                   METH_VARARGS},
    3427                 :            :     {"eval_code_ex",            eval_eval_code_ex,               METH_VARARGS},
    3428                 :            :     {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer,
    3429                 :            :      METH_NOARGS},
    3430                 :            :     {"crash_no_current_thread", crash_no_current_thread,         METH_NOARGS},
    3431                 :            :     {"test_current_tstate_matches", test_current_tstate_matches, METH_NOARGS},
    3432                 :            :     {"run_in_subinterp",        run_in_subinterp,                METH_VARARGS},
    3433                 :            :     {"run_in_subinterp_with_config",
    3434                 :            :      _PyCFunction_CAST(run_in_subinterp_with_config),
    3435                 :            :      METH_VARARGS | METH_KEYWORDS},
    3436                 :            :     {"get_crossinterp_data",    get_crossinterp_data,            METH_VARARGS},
    3437                 :            :     {"restore_crossinterp_data", restore_crossinterp_data,       METH_VARARGS},
    3438                 :            :     {"with_tp_del",             with_tp_del,                     METH_VARARGS},
    3439                 :            :     {"create_cfunction",        create_cfunction,                METH_NOARGS},
    3440                 :            :     {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_VARARGS,
    3441                 :            :      PyDoc_STR("set_error_class(error_class) -> None")},
    3442                 :            :     {"join_temporary_c_thread", join_temporary_c_thread, METH_NOARGS},
    3443                 :            :     {"pymarshal_write_long_to_file",
    3444                 :            :         pymarshal_write_long_to_file, METH_VARARGS},
    3445                 :            :     {"pymarshal_write_object_to_file",
    3446                 :            :         pymarshal_write_object_to_file, METH_VARARGS},
    3447                 :            :     {"pymarshal_read_short_from_file",
    3448                 :            :         pymarshal_read_short_from_file, METH_VARARGS},
    3449                 :            :     {"pymarshal_read_long_from_file",
    3450                 :            :         pymarshal_read_long_from_file, METH_VARARGS},
    3451                 :            :     {"pymarshal_read_last_object_from_file",
    3452                 :            :         pymarshal_read_last_object_from_file, METH_VARARGS},
    3453                 :            :     {"pymarshal_read_object_from_file",
    3454                 :            :         pymarshal_read_object_from_file, METH_VARARGS},
    3455                 :            :     {"return_null_without_error", return_null_without_error, METH_NOARGS},
    3456                 :            :     {"return_result_with_error", return_result_with_error, METH_NOARGS},
    3457                 :            :     {"getitem_with_error", getitem_with_error, METH_VARARGS},
    3458                 :            :     {"Py_CompileString",     pycompilestring, METH_O},
    3459                 :            :     {"dict_get_version", dict_get_version, METH_VARARGS},
    3460                 :            :     {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
    3461                 :            :     {"stack_pointer", stack_pointer, METH_NOARGS},
    3462                 :            : #ifdef W_STOPCODE
    3463                 :            :     {"W_STOPCODE", py_w_stopcode, METH_VARARGS},
    3464                 :            : #endif
    3465                 :            :     {"get_mapping_keys", get_mapping_keys, METH_O},
    3466                 :            :     {"get_mapping_values", get_mapping_values, METH_O},
    3467                 :            :     {"get_mapping_items", get_mapping_items, METH_O},
    3468                 :            :     {"test_mapping_has_key_string", test_mapping_has_key_string, METH_NOARGS},
    3469                 :            :     {"mapping_has_key", mapping_has_key, METH_VARARGS},
    3470                 :            :     {"sequence_set_slice", sequence_set_slice, METH_VARARGS},
    3471                 :            :     {"sequence_del_slice", sequence_del_slice, METH_VARARGS},
    3472                 :            :     {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS},
    3473                 :            :     {"hamt", new_hamt, METH_NOARGS},
    3474                 :            :     {"bad_get", _PyCFunction_CAST(bad_get), METH_FASTCALL},
    3475                 :            : #ifdef Py_REF_DEBUG
    3476                 :            :     {"negative_refcount", negative_refcount, METH_NOARGS},
    3477                 :            : #endif
    3478                 :            :     {"sequence_getitem", sequence_getitem, METH_VARARGS},
    3479                 :            :     {"sequence_setitem", sequence_setitem, METH_VARARGS},
    3480                 :            :     {"sequence_delitem", sequence_delitem, METH_VARARGS},
    3481                 :            :     {"hasattr_string", hasattr_string, METH_VARARGS},
    3482                 :            :     {"meth_varargs", meth_varargs, METH_VARARGS},
    3483                 :            :     {"meth_varargs_keywords", _PyCFunction_CAST(meth_varargs_keywords), METH_VARARGS|METH_KEYWORDS},
    3484                 :            :     {"meth_o", meth_o, METH_O},
    3485                 :            :     {"meth_noargs", meth_noargs, METH_NOARGS},
    3486                 :            :     {"meth_fastcall", _PyCFunction_CAST(meth_fastcall), METH_FASTCALL},
    3487                 :            :     {"meth_fastcall_keywords", _PyCFunction_CAST(meth_fastcall_keywords), METH_FASTCALL|METH_KEYWORDS},
    3488                 :            :     {"pynumber_tobase", pynumber_tobase, METH_VARARGS},
    3489                 :            :     {"without_gc", without_gc, METH_O},
    3490                 :            :     {"test_set_type_size", test_set_type_size, METH_NOARGS},
    3491                 :            :     {"test_py_clear", test_py_clear, METH_NOARGS},
    3492                 :            :     {"test_py_setref", test_py_setref, METH_NOARGS},
    3493                 :            :     {"test_refcount_macros", test_refcount_macros, METH_NOARGS},
    3494                 :            :     {"test_refcount_funcs", test_refcount_funcs, METH_NOARGS},
    3495                 :            :     {"test_py_is_macros", test_py_is_macros, METH_NOARGS},
    3496                 :            :     {"test_py_is_funcs", test_py_is_funcs, METH_NOARGS},
    3497                 :            :     {"type_get_version", type_get_version, METH_O, PyDoc_STR("type->tp_version_tag")},
    3498                 :            :     {"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL},
    3499                 :            :     {"frame_getlocals", frame_getlocals, METH_O, NULL},
    3500                 :            :     {"frame_getglobals", frame_getglobals, METH_O, NULL},
    3501                 :            :     {"frame_getgenerator", frame_getgenerator, METH_O, NULL},
    3502                 :            :     {"frame_getbuiltins", frame_getbuiltins, METH_O, NULL},
    3503                 :            :     {"frame_getlasti", frame_getlasti, METH_O, NULL},
    3504                 :            :     {"frame_new", frame_new, METH_VARARGS, NULL},
    3505                 :            :     {"frame_getvar", test_frame_getvar, METH_VARARGS, NULL},
    3506                 :            :     {"frame_getvarstring", test_frame_getvarstring, METH_VARARGS, NULL},
    3507                 :            :     {"eval_get_func_name", eval_get_func_name, METH_O, NULL},
    3508                 :            :     {"eval_get_func_desc", eval_get_func_desc, METH_O, NULL},
    3509                 :            :     {"gen_get_code", gen_get_code, METH_O, NULL},
    3510                 :            :     {"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
    3511                 :            :     {"test_code_api", test_code_api, METH_NOARGS, NULL},
    3512                 :            :     {"settrace_to_record", settrace_to_record, METH_O, NULL},
    3513                 :            :     {"test_macros", test_macros, METH_NOARGS, NULL},
    3514                 :            :     {"clear_managed_dict", clear_managed_dict, METH_O, NULL},
    3515                 :            :     {"function_get_code", function_get_code, METH_O, NULL},
    3516                 :            :     {"function_get_globals", function_get_globals, METH_O, NULL},
    3517                 :            :     {"function_get_module", function_get_module, METH_O, NULL},
    3518                 :            :     {"function_get_defaults", function_get_defaults, METH_O, NULL},
    3519                 :            :     {"function_set_defaults", function_set_defaults, METH_VARARGS, NULL},
    3520                 :            :     {"function_get_kw_defaults", function_get_kw_defaults, METH_O, NULL},
    3521                 :            :     {"function_set_kw_defaults", function_set_kw_defaults, METH_VARARGS, NULL},
    3522                 :            :     {"test_gc_visit_objects_basic", test_gc_visit_objects_basic, METH_NOARGS, NULL},
    3523                 :            :     {"test_gc_visit_objects_exit_early", test_gc_visit_objects_exit_early, METH_NOARGS, NULL},
    3524                 :            :     {NULL, NULL} /* sentinel */
    3525                 :            : };
    3526                 :            : 
    3527                 :            : 
    3528                 :            : typedef struct {
    3529                 :            :     PyObject_HEAD
    3530                 :            : } matmulObject;
    3531                 :            : 
    3532                 :            : static PyObject *
    3533                 :          0 : matmulType_matmul(PyObject *self, PyObject *other)
    3534                 :            : {
    3535                 :          0 :     return Py_BuildValue("(sOO)", "matmul", self, other);
    3536                 :            : }
    3537                 :            : 
    3538                 :            : static PyObject *
    3539                 :          0 : matmulType_imatmul(PyObject *self, PyObject *other)
    3540                 :            : {
    3541                 :          0 :     return Py_BuildValue("(sOO)", "imatmul", self, other);
    3542                 :            : }
    3543                 :            : 
    3544                 :            : static void
    3545                 :          0 : matmulType_dealloc(PyObject *self)
    3546                 :            : {
    3547                 :          0 :     Py_TYPE(self)->tp_free(self);
    3548                 :          0 : }
    3549                 :            : 
    3550                 :            : static PyNumberMethods matmulType_as_number = {
    3551                 :            :     0,                          /* nb_add */
    3552                 :            :     0,                          /* nb_subtract */
    3553                 :            :     0,                          /* nb_multiply */
    3554                 :            :     0,                          /* nb_remainde r*/
    3555                 :            :     0,                          /* nb_divmod */
    3556                 :            :     0,                          /* nb_power */
    3557                 :            :     0,                          /* nb_negative */
    3558                 :            :     0,                          /* tp_positive */
    3559                 :            :     0,                          /* tp_absolute */
    3560                 :            :     0,                          /* tp_bool */
    3561                 :            :     0,                          /* nb_invert */
    3562                 :            :     0,                          /* nb_lshift */
    3563                 :            :     0,                          /* nb_rshift */
    3564                 :            :     0,                          /* nb_and */
    3565                 :            :     0,                          /* nb_xor */
    3566                 :            :     0,                          /* nb_or */
    3567                 :            :     0,                          /* nb_int */
    3568                 :            :     0,                          /* nb_reserved */
    3569                 :            :     0,                          /* nb_float */
    3570                 :            :     0,                          /* nb_inplace_add */
    3571                 :            :     0,                          /* nb_inplace_subtract */
    3572                 :            :     0,                          /* nb_inplace_multiply */
    3573                 :            :     0,                          /* nb_inplace_remainder */
    3574                 :            :     0,                          /* nb_inplace_power */
    3575                 :            :     0,                          /* nb_inplace_lshift */
    3576                 :            :     0,                          /* nb_inplace_rshift */
    3577                 :            :     0,                          /* nb_inplace_and */
    3578                 :            :     0,                          /* nb_inplace_xor */
    3579                 :            :     0,                          /* nb_inplace_or */
    3580                 :            :     0,                          /* nb_floor_divide */
    3581                 :            :     0,                          /* nb_true_divide */
    3582                 :            :     0,                          /* nb_inplace_floor_divide */
    3583                 :            :     0,                          /* nb_inplace_true_divide */
    3584                 :            :     0,                          /* nb_index */
    3585                 :            :     matmulType_matmul,        /* nb_matrix_multiply */
    3586                 :            :     matmulType_imatmul        /* nb_matrix_inplace_multiply */
    3587                 :            : };
    3588                 :            : 
    3589                 :            : static PyTypeObject matmulType = {
    3590                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3591                 :            :     "matmulType",
    3592                 :            :     sizeof(matmulObject),               /* tp_basicsize */
    3593                 :            :     0,                                  /* tp_itemsize */
    3594                 :            :     matmulType_dealloc,                 /* destructor tp_dealloc */
    3595                 :            :     0,                                  /* tp_vectorcall_offset */
    3596                 :            :     0,                                  /* tp_getattr */
    3597                 :            :     0,                                  /* tp_setattr */
    3598                 :            :     0,                                  /* tp_as_async */
    3599                 :            :     0,                                  /* tp_repr */
    3600                 :            :     &matmulType_as_number,              /* tp_as_number */
    3601                 :            :     0,                                  /* tp_as_sequence */
    3602                 :            :     0,                                  /* tp_as_mapping */
    3603                 :            :     0,                                  /* tp_hash */
    3604                 :            :     0,                                  /* tp_call */
    3605                 :            :     0,                                  /* tp_str */
    3606                 :            :     PyObject_GenericGetAttr,            /* tp_getattro */
    3607                 :            :     PyObject_GenericSetAttr,            /* tp_setattro */
    3608                 :            :     0,                                  /* tp_as_buffer */
    3609                 :            :     0,                                  /* tp_flags */
    3610                 :            :     "C level type with matrix operations defined",
    3611                 :            :     0,                                  /* traverseproc tp_traverse */
    3612                 :            :     0,                                  /* tp_clear */
    3613                 :            :     0,                                  /* tp_richcompare */
    3614                 :            :     0,                                  /* tp_weaklistoffset */
    3615                 :            :     0,                                  /* tp_iter */
    3616                 :            :     0,                                  /* tp_iternext */
    3617                 :            :     0,                                  /* tp_methods */
    3618                 :            :     0,                                  /* tp_members */
    3619                 :            :     0,
    3620                 :            :     0,
    3621                 :            :     0,
    3622                 :            :     0,
    3623                 :            :     0,
    3624                 :            :     0,
    3625                 :            :     0,
    3626                 :            :     0,
    3627                 :            :     PyType_GenericNew,                  /* tp_new */
    3628                 :            :     PyObject_Del,                       /* tp_free */
    3629                 :            : };
    3630                 :            : 
    3631                 :            : typedef struct {
    3632                 :            :     PyObject_HEAD
    3633                 :            : } ipowObject;
    3634                 :            : 
    3635                 :            : static PyObject *
    3636                 :          0 : ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod)
    3637                 :            : {
    3638                 :          0 :     return Py_BuildValue("OO", other, mod);
    3639                 :            : }
    3640                 :            : 
    3641                 :            : static PyNumberMethods ipowType_as_number = {
    3642                 :            :     .nb_inplace_power = ipowType_ipow
    3643                 :            : };
    3644                 :            : 
    3645                 :            : static PyTypeObject ipowType = {
    3646                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3647                 :            :     .tp_name = "ipowType",
    3648                 :            :     .tp_basicsize = sizeof(ipowObject),
    3649                 :            :     .tp_as_number = &ipowType_as_number,
    3650                 :            :     .tp_new = PyType_GenericNew
    3651                 :            : };
    3652                 :            : 
    3653                 :            : typedef struct {
    3654                 :            :     PyObject_HEAD
    3655                 :            :     PyObject *ao_iterator;
    3656                 :            : } awaitObject;
    3657                 :            : 
    3658                 :            : 
    3659                 :            : static PyObject *
    3660                 :          0 : awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    3661                 :            : {
    3662                 :            :     PyObject *v;
    3663                 :            :     awaitObject *ao;
    3664                 :            : 
    3665         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v))
    3666                 :          0 :         return NULL;
    3667                 :            : 
    3668                 :          0 :     ao = (awaitObject *)type->tp_alloc(type, 0);
    3669         [ #  # ]:          0 :     if (ao == NULL) {
    3670                 :          0 :         return NULL;
    3671                 :            :     }
    3672                 :            : 
    3673                 :          0 :     ao->ao_iterator = Py_NewRef(v);
    3674                 :            : 
    3675                 :          0 :     return (PyObject *)ao;
    3676                 :            : }
    3677                 :            : 
    3678                 :            : 
    3679                 :            : static void
    3680                 :          0 : awaitObject_dealloc(awaitObject *ao)
    3681                 :            : {
    3682         [ #  # ]:          0 :     Py_CLEAR(ao->ao_iterator);
    3683                 :          0 :     Py_TYPE(ao)->tp_free(ao);
    3684                 :          0 : }
    3685                 :            : 
    3686                 :            : 
    3687                 :            : static PyObject *
    3688                 :          0 : awaitObject_await(awaitObject *ao)
    3689                 :            : {
    3690                 :          0 :     return Py_NewRef(ao->ao_iterator);
    3691                 :            : }
    3692                 :            : 
    3693                 :            : static PyAsyncMethods awaitType_as_async = {
    3694                 :            :     (unaryfunc)awaitObject_await,           /* am_await */
    3695                 :            :     0,                                      /* am_aiter */
    3696                 :            :     0,                                      /* am_anext */
    3697                 :            :     0,                                      /* am_send  */
    3698                 :            : };
    3699                 :            : 
    3700                 :            : 
    3701                 :            : static PyTypeObject awaitType = {
    3702                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3703                 :            :     "awaitType",
    3704                 :            :     sizeof(awaitObject),                /* tp_basicsize */
    3705                 :            :     0,                                  /* tp_itemsize */
    3706                 :            :     (destructor)awaitObject_dealloc,    /* destructor tp_dealloc */
    3707                 :            :     0,                                  /* tp_vectorcall_offset */
    3708                 :            :     0,                                  /* tp_getattr */
    3709                 :            :     0,                                  /* tp_setattr */
    3710                 :            :     &awaitType_as_async,                /* tp_as_async */
    3711                 :            :     0,                                  /* tp_repr */
    3712                 :            :     0,                                  /* tp_as_number */
    3713                 :            :     0,                                  /* tp_as_sequence */
    3714                 :            :     0,                                  /* tp_as_mapping */
    3715                 :            :     0,                                  /* tp_hash */
    3716                 :            :     0,                                  /* tp_call */
    3717                 :            :     0,                                  /* tp_str */
    3718                 :            :     PyObject_GenericGetAttr,            /* tp_getattro */
    3719                 :            :     PyObject_GenericSetAttr,            /* tp_setattro */
    3720                 :            :     0,                                  /* tp_as_buffer */
    3721                 :            :     0,                                  /* tp_flags */
    3722                 :            :     "C level type with tp_as_async",
    3723                 :            :     0,                                  /* traverseproc tp_traverse */
    3724                 :            :     0,                                  /* tp_clear */
    3725                 :            :     0,                                  /* tp_richcompare */
    3726                 :            :     0,                                  /* tp_weaklistoffset */
    3727                 :            :     0,                                  /* tp_iter */
    3728                 :            :     0,                                  /* tp_iternext */
    3729                 :            :     0,                                  /* tp_methods */
    3730                 :            :     0,                                  /* tp_members */
    3731                 :            :     0,
    3732                 :            :     0,
    3733                 :            :     0,
    3734                 :            :     0,
    3735                 :            :     0,
    3736                 :            :     0,
    3737                 :            :     0,
    3738                 :            :     0,
    3739                 :            :     awaitObject_new,                    /* tp_new */
    3740                 :            :     PyObject_Del,                       /* tp_free */
    3741                 :            : };
    3742                 :            : 
    3743                 :            : 
    3744                 :            : /* Test bpo-35983: create a subclass of "list" which checks that instances
    3745                 :            :  * are not deallocated twice */
    3746                 :            : 
    3747                 :            : typedef struct {
    3748                 :            :     PyListObject list;
    3749                 :            :     int deallocated;
    3750                 :            : } MyListObject;
    3751                 :            : 
    3752                 :            : static PyObject *
    3753                 :          0 : MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    3754                 :            : {
    3755                 :          0 :     PyObject* op = PyList_Type.tp_new(type, args, kwds);
    3756                 :          0 :     ((MyListObject*)op)->deallocated = 0;
    3757                 :          0 :     return op;
    3758                 :            : }
    3759                 :            : 
    3760                 :            : void
    3761                 :          0 : MyList_dealloc(MyListObject* op)
    3762                 :            : {
    3763         [ #  # ]:          0 :     if (op->deallocated) {
    3764                 :            :         /* We cannot raise exceptions here but we still want the testsuite
    3765                 :            :          * to fail when we hit this */
    3766                 :          0 :         Py_FatalError("MyList instance deallocated twice");
    3767                 :            :     }
    3768                 :          0 :     op->deallocated = 1;
    3769                 :          0 :     PyList_Type.tp_dealloc((PyObject *)op);
    3770                 :          0 : }
    3771                 :            : 
    3772                 :            : static PyTypeObject MyList_Type = {
    3773                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3774                 :            :     "MyList",
    3775                 :            :     sizeof(MyListObject),
    3776                 :            :     0,
    3777                 :            :     (destructor)MyList_dealloc,                 /* tp_dealloc */
    3778                 :            :     0,                                          /* tp_vectorcall_offset */
    3779                 :            :     0,                                          /* tp_getattr */
    3780                 :            :     0,                                          /* tp_setattr */
    3781                 :            :     0,                                          /* tp_as_async */
    3782                 :            :     0,                                          /* tp_repr */
    3783                 :            :     0,                                          /* tp_as_number */
    3784                 :            :     0,                                          /* tp_as_sequence */
    3785                 :            :     0,                                          /* tp_as_mapping */
    3786                 :            :     0,                                          /* tp_hash */
    3787                 :            :     0,                                          /* tp_call */
    3788                 :            :     0,                                          /* tp_str */
    3789                 :            :     0,                                          /* tp_getattro */
    3790                 :            :     0,                                          /* tp_setattro */
    3791                 :            :     0,                                          /* tp_as_buffer */
    3792                 :            :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
    3793                 :            :     0,                                          /* tp_doc */
    3794                 :            :     0,                                          /* tp_traverse */
    3795                 :            :     0,                                          /* tp_clear */
    3796                 :            :     0,                                          /* tp_richcompare */
    3797                 :            :     0,                                          /* tp_weaklistoffset */
    3798                 :            :     0,                                          /* tp_iter */
    3799                 :            :     0,                                          /* tp_iternext */
    3800                 :            :     0,                                          /* tp_methods */
    3801                 :            :     0,                                          /* tp_members */
    3802                 :            :     0,                                          /* tp_getset */
    3803                 :            :     0,  /* &PyList_Type */                      /* tp_base */
    3804                 :            :     0,                                          /* tp_dict */
    3805                 :            :     0,                                          /* tp_descr_get */
    3806                 :            :     0,                                          /* tp_descr_set */
    3807                 :            :     0,                                          /* tp_dictoffset */
    3808                 :            :     0,                                          /* tp_init */
    3809                 :            :     0,                                          /* tp_alloc */
    3810                 :            :     MyList_new,                                 /* tp_new */
    3811                 :            : };
    3812                 :            : 
    3813                 :            : 
    3814                 :            : /* Test PEP 560 */
    3815                 :            : 
    3816                 :            : typedef struct {
    3817                 :            :     PyObject_HEAD
    3818                 :            :     PyObject *item;
    3819                 :            : } PyGenericAliasObject;
    3820                 :            : 
    3821                 :            : static void
    3822                 :          0 : generic_alias_dealloc(PyGenericAliasObject *self)
    3823                 :            : {
    3824         [ #  # ]:          0 :     Py_CLEAR(self->item);
    3825                 :          0 :     Py_TYPE(self)->tp_free((PyObject *)self);
    3826                 :          0 : }
    3827                 :            : 
    3828                 :            : static PyObject *
    3829                 :          0 : generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases)
    3830                 :            : {
    3831                 :          0 :     return PyTuple_Pack(1, self->item);
    3832                 :            : }
    3833                 :            : 
    3834                 :            : static PyMethodDef generic_alias_methods[] = {
    3835                 :            :     {"__mro_entries__", _PyCFunction_CAST(generic_alias_mro_entries), METH_O, NULL},
    3836                 :            :     {NULL}  /* sentinel */
    3837                 :            : };
    3838                 :            : 
    3839                 :            : static PyTypeObject GenericAlias_Type = {
    3840                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3841                 :            :     "GenericAlias",
    3842                 :            :     sizeof(PyGenericAliasObject),
    3843                 :            :     0,
    3844                 :            :     .tp_dealloc = (destructor)generic_alias_dealloc,
    3845                 :            :     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    3846                 :            :     .tp_methods = generic_alias_methods,
    3847                 :            : };
    3848                 :            : 
    3849                 :            : static PyObject *
    3850                 :          0 : generic_alias_new(PyObject *item)
    3851                 :            : {
    3852                 :          0 :     PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type);
    3853         [ #  # ]:          0 :     if (o == NULL) {
    3854                 :          0 :         return NULL;
    3855                 :            :     }
    3856                 :          0 :     o->item = Py_NewRef(item);
    3857                 :          0 :     return (PyObject*) o;
    3858                 :            : }
    3859                 :            : 
    3860                 :            : typedef struct {
    3861                 :            :     PyObject_HEAD
    3862                 :            : } PyGenericObject;
    3863                 :            : 
    3864                 :            : static PyObject *
    3865                 :          0 : generic_class_getitem(PyObject *type, PyObject *item)
    3866                 :            : {
    3867                 :          0 :     return generic_alias_new(item);
    3868                 :            : }
    3869                 :            : 
    3870                 :            : static PyMethodDef generic_methods[] = {
    3871                 :            :     {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL},
    3872                 :            :     {NULL}  /* sentinel */
    3873                 :            : };
    3874                 :            : 
    3875                 :            : static PyTypeObject Generic_Type = {
    3876                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3877                 :            :     "Generic",
    3878                 :            :     sizeof(PyGenericObject),
    3879                 :            :     0,
    3880                 :            :     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    3881                 :            :     .tp_methods = generic_methods,
    3882                 :            : };
    3883                 :            : 
    3884                 :            : static PyMethodDef meth_instance_methods[] = {
    3885                 :            :     {"meth_varargs", meth_varargs, METH_VARARGS},
    3886                 :            :     {"meth_varargs_keywords", _PyCFunction_CAST(meth_varargs_keywords), METH_VARARGS|METH_KEYWORDS},
    3887                 :            :     {"meth_o", meth_o, METH_O},
    3888                 :            :     {"meth_noargs", meth_noargs, METH_NOARGS},
    3889                 :            :     {"meth_fastcall", _PyCFunction_CAST(meth_fastcall), METH_FASTCALL},
    3890                 :            :     {"meth_fastcall_keywords", _PyCFunction_CAST(meth_fastcall_keywords), METH_FASTCALL|METH_KEYWORDS},
    3891                 :            :     {NULL, NULL} /* sentinel */
    3892                 :            : };
    3893                 :            : 
    3894                 :            : 
    3895                 :            : static PyTypeObject MethInstance_Type = {
    3896                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3897                 :            :     "MethInstance",
    3898                 :            :     sizeof(PyObject),
    3899                 :            :     .tp_new = PyType_GenericNew,
    3900                 :            :     .tp_flags = Py_TPFLAGS_DEFAULT,
    3901                 :            :     .tp_methods = meth_instance_methods,
    3902                 :            :     .tp_doc = (char*)PyDoc_STR(
    3903                 :            :         "Class with normal (instance) methods to test calling conventions"),
    3904                 :            : };
    3905                 :            : 
    3906                 :            : static PyMethodDef meth_class_methods[] = {
    3907                 :            :     {"meth_varargs", meth_varargs, METH_VARARGS|METH_CLASS},
    3908                 :            :     {"meth_varargs_keywords", _PyCFunction_CAST(meth_varargs_keywords), METH_VARARGS|METH_KEYWORDS|METH_CLASS},
    3909                 :            :     {"meth_o", meth_o, METH_O|METH_CLASS},
    3910                 :            :     {"meth_noargs", meth_noargs, METH_NOARGS|METH_CLASS},
    3911                 :            :     {"meth_fastcall", _PyCFunction_CAST(meth_fastcall), METH_FASTCALL|METH_CLASS},
    3912                 :            :     {"meth_fastcall_keywords", _PyCFunction_CAST(meth_fastcall_keywords), METH_FASTCALL|METH_KEYWORDS|METH_CLASS},
    3913                 :            :     {NULL, NULL} /* sentinel */
    3914                 :            : };
    3915                 :            : 
    3916                 :            : 
    3917                 :            : static PyTypeObject MethClass_Type = {
    3918                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3919                 :            :     "MethClass",
    3920                 :            :     sizeof(PyObject),
    3921                 :            :     .tp_new = PyType_GenericNew,
    3922                 :            :     .tp_flags = Py_TPFLAGS_DEFAULT,
    3923                 :            :     .tp_methods = meth_class_methods,
    3924                 :            :     .tp_doc = PyDoc_STR(
    3925                 :            :         "Class with class methods to test calling conventions"),
    3926                 :            : };
    3927                 :            : 
    3928                 :            : static PyMethodDef meth_static_methods[] = {
    3929                 :            :     {"meth_varargs", meth_varargs, METH_VARARGS|METH_STATIC},
    3930                 :            :     {"meth_varargs_keywords", _PyCFunction_CAST(meth_varargs_keywords), METH_VARARGS|METH_KEYWORDS|METH_STATIC},
    3931                 :            :     {"meth_o", meth_o, METH_O|METH_STATIC},
    3932                 :            :     {"meth_noargs", meth_noargs, METH_NOARGS|METH_STATIC},
    3933                 :            :     {"meth_fastcall", _PyCFunction_CAST(meth_fastcall), METH_FASTCALL|METH_STATIC},
    3934                 :            :     {"meth_fastcall_keywords", _PyCFunction_CAST(meth_fastcall_keywords), METH_FASTCALL|METH_KEYWORDS|METH_STATIC},
    3935                 :            :     {NULL, NULL} /* sentinel */
    3936                 :            : };
    3937                 :            : 
    3938                 :            : 
    3939                 :            : static PyTypeObject MethStatic_Type = {
    3940                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3941                 :            :     "MethStatic",
    3942                 :            :     sizeof(PyObject),
    3943                 :            :     .tp_new = PyType_GenericNew,
    3944                 :            :     .tp_flags = Py_TPFLAGS_DEFAULT,
    3945                 :            :     .tp_methods = meth_static_methods,
    3946                 :            :     .tp_doc = PyDoc_STR(
    3947                 :            :         "Class with static methods to test calling conventions"),
    3948                 :            : };
    3949                 :            : 
    3950                 :            : /* ContainerNoGC -- a simple container without GC methods */
    3951                 :            : 
    3952                 :            : typedef struct {
    3953                 :            :     PyObject_HEAD
    3954                 :            :     PyObject *value;
    3955                 :            : } ContainerNoGCobject;
    3956                 :            : 
    3957                 :            : static PyObject *
    3958                 :          0 : ContainerNoGC_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
    3959                 :            : {
    3960                 :            :     PyObject *value;
    3961                 :          0 :     char *names[] = {"value", NULL};
    3962         [ #  # ]:          0 :     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", names, &value)) {
    3963                 :          0 :         return NULL;
    3964                 :            :     }
    3965                 :          0 :     PyObject *self = type->tp_alloc(type, 0);
    3966         [ #  # ]:          0 :     if (self == NULL) {
    3967                 :          0 :         return NULL;
    3968                 :            :     }
    3969                 :          0 :     Py_INCREF(value);
    3970                 :          0 :     ((ContainerNoGCobject *)self)->value = value;
    3971                 :          0 :     return self;
    3972                 :            : }
    3973                 :            : 
    3974                 :            : static void
    3975                 :          0 : ContainerNoGC_dealloc(ContainerNoGCobject *self)
    3976                 :            : {
    3977                 :          0 :     Py_DECREF(self->value);
    3978                 :          0 :     Py_TYPE(self)->tp_free((PyObject *)self);
    3979                 :          0 : }
    3980                 :            : 
    3981                 :            : static PyMemberDef ContainerNoGC_members[] = {
    3982                 :            :     {"value", T_OBJECT, offsetof(ContainerNoGCobject, value), READONLY,
    3983                 :            :      PyDoc_STR("a container value for test purposes")},
    3984                 :            :     {0}
    3985                 :            : };
    3986                 :            : 
    3987                 :            : static PyTypeObject ContainerNoGC_type = {
    3988                 :            :     PyVarObject_HEAD_INIT(NULL, 0)
    3989                 :            :     "_testcapi.ContainerNoGC",
    3990                 :            :     sizeof(ContainerNoGCobject),
    3991                 :            :     .tp_dealloc = (destructor)ContainerNoGC_dealloc,
    3992                 :            :     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    3993                 :            :     .tp_members = ContainerNoGC_members,
    3994                 :            :     .tp_new = ContainerNoGC_new,
    3995                 :            : };
    3996                 :            : 
    3997                 :            : 
    3998                 :            : static struct PyModuleDef _testcapimodule = {
    3999                 :            :     PyModuleDef_HEAD_INIT,
    4000                 :            :     "_testcapi",
    4001                 :            :     NULL,
    4002                 :            :     -1,
    4003                 :            :     TestMethods,
    4004                 :            :     NULL,
    4005                 :            :     NULL,
    4006                 :            :     NULL,
    4007                 :            :     NULL
    4008                 :            : };
    4009                 :            : 
    4010                 :            : /* Per PEP 489, this module will not be converted to multi-phase initialization
    4011                 :            :  */
    4012                 :            : 
    4013                 :            : PyMODINIT_FUNC
    4014                 :          2 : PyInit__testcapi(void)
    4015                 :            : {
    4016                 :            :     PyObject *m;
    4017                 :            : 
    4018                 :          2 :     m = PyModule_Create(&_testcapimodule);
    4019         [ -  + ]:          2 :     if (m == NULL)
    4020                 :          0 :         return NULL;
    4021                 :            : 
    4022                 :          2 :     Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type);
    4023                 :            : 
    4024         [ -  + ]:          2 :     if (PyType_Ready(&matmulType) < 0)
    4025                 :          0 :         return NULL;
    4026                 :          2 :     Py_INCREF(&matmulType);
    4027                 :          2 :     PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType);
    4028         [ -  + ]:          2 :     if (PyType_Ready(&ipowType) < 0) {
    4029                 :          0 :         return NULL;
    4030                 :            :     }
    4031                 :          2 :     Py_INCREF(&ipowType);
    4032                 :          2 :     PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType);
    4033                 :            : 
    4034         [ -  + ]:          2 :     if (PyType_Ready(&awaitType) < 0)
    4035                 :          0 :         return NULL;
    4036                 :          2 :     Py_INCREF(&awaitType);
    4037                 :          2 :     PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType);
    4038                 :            : 
    4039                 :          2 :     MyList_Type.tp_base = &PyList_Type;
    4040         [ -  + ]:          2 :     if (PyType_Ready(&MyList_Type) < 0)
    4041                 :          0 :         return NULL;
    4042                 :          2 :     Py_INCREF(&MyList_Type);
    4043                 :          2 :     PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type);
    4044                 :            : 
    4045         [ -  + ]:          2 :     if (PyType_Ready(&GenericAlias_Type) < 0)
    4046                 :          0 :         return NULL;
    4047                 :          2 :     Py_INCREF(&GenericAlias_Type);
    4048                 :          2 :     PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type);
    4049                 :            : 
    4050         [ -  + ]:          2 :     if (PyType_Ready(&Generic_Type) < 0)
    4051                 :          0 :         return NULL;
    4052                 :          2 :     Py_INCREF(&Generic_Type);
    4053                 :          2 :     PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type);
    4054                 :            : 
    4055         [ -  + ]:          2 :     if (PyType_Ready(&MethInstance_Type) < 0)
    4056                 :          0 :         return NULL;
    4057                 :          2 :     Py_INCREF(&MethInstance_Type);
    4058                 :          2 :     PyModule_AddObject(m, "MethInstance", (PyObject *)&MethInstance_Type);
    4059                 :            : 
    4060         [ -  + ]:          2 :     if (PyType_Ready(&MethClass_Type) < 0)
    4061                 :          0 :         return NULL;
    4062                 :          2 :     Py_INCREF(&MethClass_Type);
    4063                 :          2 :     PyModule_AddObject(m, "MethClass", (PyObject *)&MethClass_Type);
    4064                 :            : 
    4065         [ -  + ]:          2 :     if (PyType_Ready(&MethStatic_Type) < 0)
    4066                 :          0 :         return NULL;
    4067                 :          2 :     Py_INCREF(&MethStatic_Type);
    4068                 :          2 :     PyModule_AddObject(m, "MethStatic", (PyObject *)&MethStatic_Type);
    4069                 :            : 
    4070                 :          2 :     PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
    4071                 :          2 :     PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
    4072                 :          2 :     PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
    4073                 :          2 :     PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
    4074                 :          2 :     PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
    4075                 :          2 :     PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
    4076                 :          2 :     PyModule_AddObject(m, "INT_MAX",  PyLong_FromLong(INT_MAX));
    4077                 :          2 :     PyModule_AddObject(m, "INT_MIN",  PyLong_FromLong(INT_MIN));
    4078                 :          2 :     PyModule_AddObject(m, "UINT_MAX",  PyLong_FromUnsignedLong(UINT_MAX));
    4079                 :          2 :     PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
    4080                 :          2 :     PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
    4081                 :          2 :     PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
    4082                 :          2 :     PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
    4083                 :          2 :     PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
    4084                 :          2 :     PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
    4085                 :          2 :     PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
    4086                 :          2 :     PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX));
    4087                 :          2 :     PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN));
    4088                 :          2 :     PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX));
    4089                 :          2 :     PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
    4090                 :          2 :     PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
    4091                 :          2 :     PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
    4092                 :          2 :     PyModule_AddObject(m, "Py_Version", PyLong_FromUnsignedLong(Py_Version));
    4093                 :          2 :     Py_INCREF(&PyInstanceMethod_Type);
    4094                 :          2 :     PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
    4095                 :            : 
    4096                 :          2 :     PyModule_AddIntConstant(m, "the_number_three", 3);
    4097                 :            : 
    4098                 :          2 :     TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
    4099                 :          2 :     Py_INCREF(TestError);
    4100                 :          2 :     PyModule_AddObject(m, "error", TestError);
    4101                 :            : 
    4102         [ -  + ]:          2 :     if (PyType_Ready(&ContainerNoGC_type) < 0) {
    4103                 :          0 :         return NULL;
    4104                 :            :     }
    4105                 :          2 :     Py_INCREF(&ContainerNoGC_type);
    4106         [ -  + ]:          2 :     if (PyModule_AddObject(m, "ContainerNoGC",
    4107                 :            :                            (PyObject *) &ContainerNoGC_type) < 0)
    4108                 :          0 :         return NULL;
    4109                 :            : 
    4110                 :            :     /* Include tests from the _testcapi/ directory */
    4111         [ -  + ]:          2 :     if (_PyTestCapi_Init_Vectorcall(m) < 0) {
    4112                 :          0 :         return NULL;
    4113                 :            :     }
    4114         [ -  + ]:          2 :     if (_PyTestCapi_Init_Heaptype(m) < 0) {
    4115                 :          0 :         return NULL;
    4116                 :            :     }
    4117         [ -  + ]:          2 :     if (_PyTestCapi_Init_Unicode(m) < 0) {
    4118                 :          0 :         return NULL;
    4119                 :            :     }
    4120         [ -  + ]:          2 :     if (_PyTestCapi_Init_GetArgs(m) < 0) {
    4121                 :          0 :         return NULL;
    4122                 :            :     }
    4123         [ -  + ]:          2 :     if (_PyTestCapi_Init_PyTime(m) < 0) {
    4124                 :          0 :         return NULL;
    4125                 :            :     }
    4126         [ -  + ]:          2 :     if (_PyTestCapi_Init_DateTime(m) < 0) {
    4127                 :          0 :         return NULL;
    4128                 :            :     }
    4129         [ -  + ]:          2 :     if (_PyTestCapi_Init_Docstring(m) < 0) {
    4130                 :          0 :         return NULL;
    4131                 :            :     }
    4132         [ -  + ]:          2 :     if (_PyTestCapi_Init_Mem(m) < 0) {
    4133                 :          0 :         return NULL;
    4134                 :            :     }
    4135         [ -  + ]:          2 :     if (_PyTestCapi_Init_Watchers(m) < 0) {
    4136                 :          0 :         return NULL;
    4137                 :            :     }
    4138         [ -  + ]:          2 :     if (_PyTestCapi_Init_Long(m) < 0) {
    4139                 :          0 :         return NULL;
    4140                 :            :     }
    4141         [ -  + ]:          2 :     if (_PyTestCapi_Init_Float(m) < 0) {
    4142                 :          0 :         return NULL;
    4143                 :            :     }
    4144         [ -  + ]:          2 :     if (_PyTestCapi_Init_Structmember(m) < 0) {
    4145                 :          0 :         return NULL;
    4146                 :            :     }
    4147         [ -  + ]:          2 :     if (_PyTestCapi_Init_Exceptions(m) < 0) {
    4148                 :          0 :         return NULL;
    4149                 :            :     }
    4150         [ -  + ]:          2 :     if (_PyTestCapi_Init_Code(m) < 0) {
    4151                 :          0 :         return NULL;
    4152                 :            :     }
    4153                 :            : 
    4154                 :            : #ifndef LIMITED_API_AVAILABLE
    4155                 :            :     PyModule_AddObjectRef(m, "LIMITED_API_AVAILABLE", Py_False);
    4156                 :            : #else
    4157                 :          2 :     PyModule_AddObjectRef(m, "LIMITED_API_AVAILABLE", Py_True);
    4158         [ -  + ]:          2 :     if (_PyTestCapi_Init_VectorcallLimited(m) < 0) {
    4159                 :          0 :         return NULL;
    4160                 :            :     }
    4161                 :            : #endif
    4162                 :            : 
    4163                 :          2 :     PyState_AddModule(m, &_testcapimodule);
    4164                 :          2 :     return m;
    4165                 :            : }
    4166                 :            : 
    4167                 :            : /* Test the C API exposed when PY_SSIZE_T_CLEAN is not defined */
    4168                 :            : 
    4169                 :            : #undef Py_BuildValue
    4170                 :            : PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...);
    4171                 :            : 
    4172                 :            : static PyObject *
    4173                 :          0 : test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored))
    4174                 :            : {
    4175                 :            :     PyObject *res;
    4176                 :          0 :     const char str[] = "string";
    4177                 :          0 :     const Py_UNICODE unicode[] = L"unicode";
    4178         [ #  # ]:          0 :     assert(!PyErr_Occurred());
    4179                 :            : 
    4180                 :          0 :     res = Py_BuildValue("(s#O)", str, 1, Py_None);
    4181         [ #  # ]:          0 :     assert(res == NULL);
    4182         [ #  # ]:          0 :     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
    4183                 :          0 :         return NULL;
    4184                 :            :     }
    4185                 :          0 :     PyErr_Clear();
    4186                 :            : 
    4187                 :          0 :     res = Py_BuildValue("(z#O)", str, 1, Py_None);
    4188         [ #  # ]:          0 :     assert(res == NULL);
    4189         [ #  # ]:          0 :     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
    4190                 :          0 :         return NULL;
    4191                 :            :     }
    4192                 :          0 :     PyErr_Clear();
    4193                 :            : 
    4194                 :          0 :     res = Py_BuildValue("(y#O)", str, 1, Py_None);
    4195         [ #  # ]:          0 :     assert(res == NULL);
    4196         [ #  # ]:          0 :     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
    4197                 :          0 :         return NULL;
    4198                 :            :     }
    4199                 :          0 :     PyErr_Clear();
    4200                 :            : 
    4201                 :          0 :     res = Py_BuildValue("(u#O)", unicode, 1, Py_None);
    4202         [ #  # ]:          0 :     assert(res == NULL);
    4203         [ #  # ]:          0 :     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
    4204                 :          0 :         return NULL;
    4205                 :            :     }
    4206                 :          0 :     PyErr_Clear();
    4207                 :            : 
    4208                 :          0 :     Py_RETURN_NONE;
    4209                 :            : }

Generated by: LCOV version 1.14