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