• 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 /* This module tests the public (Include/ and Include/cpython/) C API.
9    The internal C API must not be used here: use _testinternalcapi for that.
10 
11    The Visual Studio projects builds _testcapi with Py_BUILD_CORE_MODULE
12    macro defined, but only the public C API must be tested here. */
13 
14 #undef Py_BUILD_CORE_MODULE
15 /* Always enable assertions */
16 #undef NDEBUG
17 
18 #define PY_SSIZE_T_CLEAN
19 
20 #include "Python.h"
21 #include "datetime.h"
22 #include "marshal.h"
23 #include "structmember.h"         // PyMemberDef
24 #include <float.h>
25 #include <signal.h>
26 
27 #ifdef MS_WINDOWS
28 #  include <winsock2.h>         /* struct timeval */
29 #endif
30 
31 #ifdef HAVE_SYS_WAIT_H
32 #include <sys/wait.h>           /* For W_STOPCODE */
33 #endif
34 
35 #ifdef Py_BUILD_CORE
36 #  error "_testcapi must test the public Python C API, not CPython internal C API"
37 #endif
38 
39 static struct PyModuleDef _testcapimodule;
40 
41 static PyObject *TestError;     /* set to exception object in init */
42 
43 /* Raise TestError with test_name + ": " + msg, and return NULL. */
44 
45 static PyObject *
raiseTestError(const char * test_name,const char * msg)46 raiseTestError(const char* test_name, const char* msg)
47 {
48     PyErr_Format(TestError, "%s: %s", test_name, msg);
49     return NULL;
50 }
51 
52 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
53 
54    The ones derived from autoconf on the UNIX-like OSes can be relied
55    upon (in the absence of sloppy cross-compiling), but the Windows
56    platforms have these hardcoded.  Better safe than sorry.
57 */
58 static PyObject*
sizeof_error(const char * fatname,const char * typname,int expected,int got)59 sizeof_error(const char* fatname, const char* typname,
60     int expected, int got)
61 {
62     PyErr_Format(TestError,
63         "%s #define == %d but sizeof(%s) == %d",
64         fatname, expected, typname, got);
65     return (PyObject*)NULL;
66 }
67 
68 static PyObject*
test_config(PyObject * self,PyObject * Py_UNUSED (ignored))69 test_config(PyObject *self, PyObject *Py_UNUSED(ignored))
70 {
71 #define CHECK_SIZEOF(FATNAME, TYPE) \
72             if (FATNAME != sizeof(TYPE)) \
73                 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
74 
75     CHECK_SIZEOF(SIZEOF_SHORT, short);
76     CHECK_SIZEOF(SIZEOF_INT, int);
77     CHECK_SIZEOF(SIZEOF_LONG, long);
78     CHECK_SIZEOF(SIZEOF_VOID_P, void*);
79     CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
80     CHECK_SIZEOF(SIZEOF_LONG_LONG, long long);
81 
82 #undef CHECK_SIZEOF
83 
84     Py_RETURN_NONE;
85 }
86 
87 static PyObject*
test_sizeof_c_types(PyObject * self,PyObject * Py_UNUSED (ignored))88 test_sizeof_c_types(PyObject *self, PyObject *Py_UNUSED(ignored))
89 {
90 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
91 #pragma GCC diagnostic push
92 #pragma GCC diagnostic ignored "-Wtype-limits"
93 #endif
94 #define CHECK_SIZEOF(TYPE, EXPECTED)         \
95     if (EXPECTED != sizeof(TYPE))  {         \
96         PyErr_Format(TestError,              \
97             "sizeof(%s) = %u instead of %u", \
98             #TYPE, sizeof(TYPE), EXPECTED);  \
99         return (PyObject*)NULL;              \
100     }
101 #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0)
102 #define CHECK_SIGNNESS(TYPE, SIGNED)         \
103     if (IS_SIGNED(TYPE) != SIGNED) {         \
104         PyErr_Format(TestError,              \
105             "%s signness is, instead of %i",  \
106             #TYPE, IS_SIGNED(TYPE), SIGNED); \
107         return (PyObject*)NULL;              \
108     }
109 
110     /* integer types */
111     CHECK_SIZEOF(Py_UCS1, 1);
112     CHECK_SIZEOF(Py_UCS2, 2);
113     CHECK_SIZEOF(Py_UCS4, 4);
114     CHECK_SIGNNESS(Py_UCS1, 0);
115     CHECK_SIGNNESS(Py_UCS2, 0);
116     CHECK_SIGNNESS(Py_UCS4, 0);
117     CHECK_SIZEOF(int32_t, 4);
118     CHECK_SIGNNESS(int32_t, 1);
119     CHECK_SIZEOF(uint32_t, 4);
120     CHECK_SIGNNESS(uint32_t, 0);
121     CHECK_SIZEOF(int64_t, 8);
122     CHECK_SIGNNESS(int64_t, 1);
123     CHECK_SIZEOF(uint64_t, 8);
124     CHECK_SIGNNESS(uint64_t, 0);
125 
126     /* pointer/size types */
127     CHECK_SIZEOF(size_t, sizeof(void *));
128     CHECK_SIGNNESS(size_t, 0);
129     CHECK_SIZEOF(Py_ssize_t, sizeof(void *));
130     CHECK_SIGNNESS(Py_ssize_t, 1);
131 
132     CHECK_SIZEOF(uintptr_t, sizeof(void *));
133     CHECK_SIGNNESS(uintptr_t, 0);
134     CHECK_SIZEOF(intptr_t, sizeof(void *));
135     CHECK_SIGNNESS(intptr_t, 1);
136 
137     Py_RETURN_NONE;
138 
139 #undef IS_SIGNED
140 #undef CHECK_SIGNESS
141 #undef CHECK_SIZEOF
142 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
143 #pragma GCC diagnostic pop
144 #endif
145 }
146 
147 static PyObject*
test_gc_control(PyObject * self,PyObject * Py_UNUSED (ignored))148 test_gc_control(PyObject *self, PyObject *Py_UNUSED(ignored))
149 {
150     int orig_enabled = PyGC_IsEnabled();
151     const char* msg = "ok";
152     int old_state;
153 
154     old_state = PyGC_Enable();
155     msg = "Enable(1)";
156     if (old_state != orig_enabled) {
157         goto failed;
158     }
159     msg = "IsEnabled(1)";
160     if (!PyGC_IsEnabled()) {
161         goto failed;
162     }
163 
164     old_state = PyGC_Disable();
165     msg = "disable(2)";
166     if (!old_state) {
167         goto failed;
168     }
169     msg = "IsEnabled(2)";
170     if (PyGC_IsEnabled()) {
171         goto failed;
172     }
173 
174     old_state = PyGC_Enable();
175     msg = "enable(3)";
176     if (old_state) {
177         goto failed;
178     }
179     msg = "IsEnabled(3)";
180     if (!PyGC_IsEnabled()) {
181         goto failed;
182     }
183 
184     if (!orig_enabled) {
185         old_state = PyGC_Disable();
186         msg = "disable(4)";
187         if (old_state) {
188             goto failed;
189         }
190         msg = "IsEnabled(4)";
191         if (PyGC_IsEnabled()) {
192             goto failed;
193         }
194     }
195 
196     Py_RETURN_NONE;
197 
198 failed:
199     /* Try to clean up if we can. */
200     if (orig_enabled) {
201         PyGC_Enable();
202     } else {
203         PyGC_Disable();
204     }
205     PyErr_Format(TestError, "GC control failed in %s", msg);
206     return NULL;
207 }
208 
209 static PyObject*
test_list_api(PyObject * self,PyObject * Py_UNUSED (ignored))210 test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored))
211 {
212     PyObject* list;
213     int i;
214 
215     /* SF bug 132008:  PyList_Reverse segfaults */
216 #define NLIST 30
217     list = PyList_New(NLIST);
218     if (list == (PyObject*)NULL)
219         return (PyObject*)NULL;
220     /* list = range(NLIST) */
221     for (i = 0; i < NLIST; ++i) {
222         PyObject* anint = PyLong_FromLong(i);
223         if (anint == (PyObject*)NULL) {
224             Py_DECREF(list);
225             return (PyObject*)NULL;
226         }
227         PyList_SET_ITEM(list, i, anint);
228     }
229     /* list.reverse(), via PyList_Reverse() */
230     i = PyList_Reverse(list);   /* should not blow up! */
231     if (i != 0) {
232         Py_DECREF(list);
233         return (PyObject*)NULL;
234     }
235     /* Check that list == range(29, -1, -1) now */
236     for (i = 0; i < NLIST; ++i) {
237         PyObject* anint = PyList_GET_ITEM(list, i);
238         if (PyLong_AS_LONG(anint) != NLIST-1-i) {
239             PyErr_SetString(TestError,
240                             "test_list_api: reverse screwed up");
241             Py_DECREF(list);
242             return (PyObject*)NULL;
243         }
244     }
245     Py_DECREF(list);
246 #undef NLIST
247 
248     Py_RETURN_NONE;
249 }
250 
251 static int
test_dict_inner(int count)252 test_dict_inner(int count)
253 {
254     Py_ssize_t pos = 0, iterations = 0;
255     int i;
256     PyObject *dict = PyDict_New();
257     PyObject *v, *k;
258 
259     if (dict == NULL)
260         return -1;
261 
262     for (i = 0; i < count; i++) {
263         v = PyLong_FromLong(i);
264         if (v == NULL) {
265             return -1;
266         }
267         if (PyDict_SetItem(dict, v, v) < 0) {
268             Py_DECREF(v);
269             return -1;
270         }
271         Py_DECREF(v);
272     }
273 
274     while (PyDict_Next(dict, &pos, &k, &v)) {
275         PyObject *o;
276         iterations++;
277 
278         i = PyLong_AS_LONG(v) + 1;
279         o = PyLong_FromLong(i);
280         if (o == NULL)
281             return -1;
282         if (PyDict_SetItem(dict, k, o) < 0) {
283             Py_DECREF(o);
284             return -1;
285         }
286         Py_DECREF(o);
287     }
288 
289     Py_DECREF(dict);
290 
291     if (iterations != count) {
292         PyErr_SetString(
293             TestError,
294             "test_dict_iteration: dict iteration went wrong ");
295         return -1;
296     } else {
297         return 0;
298     }
299 }
300 
301 static PyObject*
test_dict_iteration(PyObject * self,PyObject * Py_UNUSED (ignored))302 test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored))
303 {
304     int i;
305 
306     for (i = 0; i < 200; i++) {
307         if (test_dict_inner(i) < 0) {
308             return NULL;
309         }
310     }
311 
312     Py_RETURN_NONE;
313 }
314 
315 static PyObject*
dict_getitem_knownhash(PyObject * self,PyObject * args)316 dict_getitem_knownhash(PyObject *self, PyObject *args)
317 {
318     PyObject *mp, *key, *result;
319     Py_ssize_t hash;
320 
321     if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
322                           &mp, &key, &hash)) {
323         return NULL;
324     }
325 
326     result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
327     if (result == NULL && !PyErr_Occurred()) {
328         _PyErr_SetKeyError(key);
329         return NULL;
330     }
331 
332     Py_XINCREF(result);
333     return result;
334 }
335 
336 static PyObject*
dict_hassplittable(PyObject * self,PyObject * arg)337 dict_hassplittable(PyObject *self, PyObject *arg)
338 {
339     if (!PyDict_Check(arg)) {
340         PyErr_Format(PyExc_TypeError,
341                      "dict_hassplittable() argument must be dict, not '%s'",
342                      Py_TYPE(arg)->tp_name);
343         return NULL;
344     }
345 
346     return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg));
347 }
348 
349 /* Issue #4701: Check that PyObject_Hash implicitly calls
350  *   PyType_Ready if it hasn't already been called
351  */
352 static PyTypeObject _HashInheritanceTester_Type = {
353     PyVarObject_HEAD_INIT(NULL, 0)
354     "hashinheritancetester",            /* Name of this type */
355     sizeof(PyObject),           /* Basic object size */
356     0,                          /* Item size for varobject */
357     (destructor)PyObject_Del, /* tp_dealloc */
358     0,                          /* tp_vectorcall_offset */
359     0,                          /* tp_getattr */
360     0,                          /* tp_setattr */
361     0,                          /* tp_as_async */
362     0,                          /* tp_repr */
363     0,                          /* tp_as_number */
364     0,                          /* tp_as_sequence */
365     0,                          /* tp_as_mapping */
366     0,                          /* tp_hash */
367     0,                          /* tp_call */
368     0,                          /* tp_str */
369     PyObject_GenericGetAttr,  /* tp_getattro */
370     0,                          /* tp_setattro */
371     0,                          /* tp_as_buffer */
372     Py_TPFLAGS_DEFAULT,         /* tp_flags */
373     0,                          /* tp_doc */
374     0,                          /* tp_traverse */
375     0,                          /* tp_clear */
376     0,                          /* tp_richcompare */
377     0,                          /* tp_weaklistoffset */
378     0,                          /* tp_iter */
379     0,                          /* tp_iternext */
380     0,                          /* tp_methods */
381     0,                          /* tp_members */
382     0,                          /* tp_getset */
383     0,                          /* tp_base */
384     0,                          /* tp_dict */
385     0,                          /* tp_descr_get */
386     0,                          /* tp_descr_set */
387     0,                          /* tp_dictoffset */
388     0,                          /* tp_init */
389     0,                          /* tp_alloc */
390     PyType_GenericNew,                  /* tp_new */
391 };
392 
393 static PyObject*
pycompilestring(PyObject * self,PyObject * obj)394 pycompilestring(PyObject* self, PyObject *obj) {
395     if (PyBytes_CheckExact(obj) == 0) {
396         PyErr_SetString(PyExc_ValueError, "Argument must be a bytes object");
397         return NULL;
398     }
399     const char *the_string = PyBytes_AsString(obj);
400     if (the_string == NULL) {
401         return NULL;
402     }
403     return Py_CompileString(the_string, "<string>", Py_file_input);
404 }
405 
406 static PyObject*
test_lazy_hash_inheritance(PyObject * self,PyObject * Py_UNUSED (ignored))407 test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
408 {
409     PyTypeObject *type;
410     PyObject *obj;
411     Py_hash_t hash;
412 
413     type = &_HashInheritanceTester_Type;
414 
415     if (type->tp_dict != NULL)
416         /* The type has already been initialized. This probably means
417            -R is being used. */
418         Py_RETURN_NONE;
419 
420 
421     obj = PyObject_New(PyObject, type);
422     if (obj == NULL) {
423         PyErr_Clear();
424         PyErr_SetString(
425             TestError,
426             "test_lazy_hash_inheritance: failed to create object");
427         return NULL;
428     }
429 
430     if (type->tp_dict != NULL) {
431         PyErr_SetString(
432             TestError,
433             "test_lazy_hash_inheritance: type initialised too soon");
434         Py_DECREF(obj);
435         return NULL;
436     }
437 
438     hash = PyObject_Hash(obj);
439     if ((hash == -1) && PyErr_Occurred()) {
440         PyErr_Clear();
441         PyErr_SetString(
442             TestError,
443             "test_lazy_hash_inheritance: could not hash object");
444         Py_DECREF(obj);
445         return NULL;
446     }
447 
448     if (type->tp_dict == NULL) {
449         PyErr_SetString(
450             TestError,
451             "test_lazy_hash_inheritance: type not initialised by hash()");
452         Py_DECREF(obj);
453         return NULL;
454     }
455 
456     if (type->tp_hash != PyType_Type.tp_hash) {
457         PyErr_SetString(
458             TestError,
459             "test_lazy_hash_inheritance: unexpected hash function");
460         Py_DECREF(obj);
461         return NULL;
462     }
463 
464     Py_DECREF(obj);
465 
466     Py_RETURN_NONE;
467 }
468 
469 
470 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and
471    PyLong_{As, From}{Unsigned,}LongLong().
472 
473    Note that the meat of the test is contained in testcapi_long.h.
474    This is revolting, but delicate code duplication is worse:  "almost
475    exactly the same" code is needed to test long long, but the ubiquitous
476    dependence on type names makes it impossible to use a parameterized
477    function.  A giant macro would be even worse than this.  A C++ template
478    would be perfect.
479 
480    The "report an error" functions are deliberately not part of the #include
481    file:  if the test fails, you can set a breakpoint in the appropriate
482    error function directly, and crawl back from there in the debugger.
483 */
484 
485 #define UNBIND(X)  Py_DECREF(X); (X) = NULL
486 
487 static PyObject *
raise_test_long_error(const char * msg)488 raise_test_long_error(const char* msg)
489 {
490     return raiseTestError("test_long_api", msg);
491 }
492 
493 #define TESTNAME        test_long_api_inner
494 #define TYPENAME        long
495 #define F_S_TO_PY       PyLong_FromLong
496 #define F_PY_TO_S       PyLong_AsLong
497 #define F_U_TO_PY       PyLong_FromUnsignedLong
498 #define F_PY_TO_U       PyLong_AsUnsignedLong
499 
500 #include "testcapi_long.h"
501 
502 static PyObject *
test_long_api(PyObject * self,PyObject * Py_UNUSED (ignored))503 test_long_api(PyObject* self, PyObject *Py_UNUSED(ignored))
504 {
505     return TESTNAME(raise_test_long_error);
506 }
507 
508 #undef TESTNAME
509 #undef TYPENAME
510 #undef F_S_TO_PY
511 #undef F_PY_TO_S
512 #undef F_U_TO_PY
513 #undef F_PY_TO_U
514 
515 static PyObject *
raise_test_longlong_error(const char * msg)516 raise_test_longlong_error(const char* msg)
517 {
518     return raiseTestError("test_longlong_api", msg);
519 }
520 
521 #define TESTNAME        test_longlong_api_inner
522 #define TYPENAME        long long
523 #define F_S_TO_PY       PyLong_FromLongLong
524 #define F_PY_TO_S       PyLong_AsLongLong
525 #define F_U_TO_PY       PyLong_FromUnsignedLongLong
526 #define F_PY_TO_U       PyLong_AsUnsignedLongLong
527 
528 #include "testcapi_long.h"
529 
530 static PyObject *
test_longlong_api(PyObject * self,PyObject * args)531 test_longlong_api(PyObject* self, PyObject *args)
532 {
533     return TESTNAME(raise_test_longlong_error);
534 }
535 
536 #undef TESTNAME
537 #undef TYPENAME
538 #undef F_S_TO_PY
539 #undef F_PY_TO_S
540 #undef F_U_TO_PY
541 #undef F_PY_TO_U
542 
543 /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
544    is tested by test_long_api_inner. This test will concentrate on proper
545    handling of overflow.
546 */
547 
548 static PyObject *
test_long_and_overflow(PyObject * self,PyObject * Py_UNUSED (ignored))549 test_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
550 {
551     PyObject *num, *one, *temp;
552     long value;
553     int overflow;
554 
555     /* Test that overflow is set properly for a large value. */
556     /* num is a number larger than LONG_MAX even on 64-bit platforms */
557     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
558     if (num == NULL)
559         return NULL;
560     overflow = 1234;
561     value = PyLong_AsLongAndOverflow(num, &overflow);
562     Py_DECREF(num);
563     if (value == -1 && PyErr_Occurred())
564         return NULL;
565     if (value != -1)
566         return raiseTestError("test_long_and_overflow",
567             "return value was not set to -1");
568     if (overflow != 1)
569         return raiseTestError("test_long_and_overflow",
570             "overflow was not set to 1");
571 
572     /* Same again, with num = LONG_MAX + 1 */
573     num = PyLong_FromLong(LONG_MAX);
574     if (num == NULL)
575         return NULL;
576     one = PyLong_FromLong(1L);
577     if (one == NULL) {
578         Py_DECREF(num);
579         return NULL;
580     }
581     temp = PyNumber_Add(num, one);
582     Py_DECREF(one);
583     Py_DECREF(num);
584     num = temp;
585     if (num == NULL)
586         return NULL;
587     overflow = 0;
588     value = PyLong_AsLongAndOverflow(num, &overflow);
589     Py_DECREF(num);
590     if (value == -1 && PyErr_Occurred())
591         return NULL;
592     if (value != -1)
593         return raiseTestError("test_long_and_overflow",
594             "return value was not set to -1");
595     if (overflow != 1)
596         return raiseTestError("test_long_and_overflow",
597             "overflow was not set to 1");
598 
599     /* Test that overflow is set properly for a large negative value. */
600     /* num is a number smaller than LONG_MIN even on 64-bit platforms */
601     num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
602     if (num == NULL)
603         return NULL;
604     overflow = 1234;
605     value = PyLong_AsLongAndOverflow(num, &overflow);
606     Py_DECREF(num);
607     if (value == -1 && PyErr_Occurred())
608         return NULL;
609     if (value != -1)
610         return raiseTestError("test_long_and_overflow",
611             "return value was not set to -1");
612     if (overflow != -1)
613         return raiseTestError("test_long_and_overflow",
614             "overflow was not set to -1");
615 
616     /* Same again, with num = LONG_MIN - 1 */
617     num = PyLong_FromLong(LONG_MIN);
618     if (num == NULL)
619         return NULL;
620     one = PyLong_FromLong(1L);
621     if (one == NULL) {
622         Py_DECREF(num);
623         return NULL;
624     }
625     temp = PyNumber_Subtract(num, one);
626     Py_DECREF(one);
627     Py_DECREF(num);
628     num = temp;
629     if (num == NULL)
630         return NULL;
631     overflow = 0;
632     value = PyLong_AsLongAndOverflow(num, &overflow);
633     Py_DECREF(num);
634     if (value == -1 && PyErr_Occurred())
635         return NULL;
636     if (value != -1)
637         return raiseTestError("test_long_and_overflow",
638             "return value was not set to -1");
639     if (overflow != -1)
640         return raiseTestError("test_long_and_overflow",
641             "overflow was not set to -1");
642 
643     /* Test that overflow is cleared properly for small values. */
644     num = PyLong_FromString("FF", NULL, 16);
645     if (num == NULL)
646         return NULL;
647     overflow = 1234;
648     value = PyLong_AsLongAndOverflow(num, &overflow);
649     Py_DECREF(num);
650     if (value == -1 && PyErr_Occurred())
651         return NULL;
652     if (value != 0xFF)
653         return raiseTestError("test_long_and_overflow",
654             "expected return value 0xFF");
655     if (overflow != 0)
656         return raiseTestError("test_long_and_overflow",
657             "overflow was not cleared");
658 
659     num = PyLong_FromString("-FF", NULL, 16);
660     if (num == NULL)
661         return NULL;
662     overflow = 0;
663     value = PyLong_AsLongAndOverflow(num, &overflow);
664     Py_DECREF(num);
665     if (value == -1 && PyErr_Occurred())
666         return NULL;
667     if (value != -0xFF)
668         return raiseTestError("test_long_and_overflow",
669             "expected return value 0xFF");
670     if (overflow != 0)
671         return raiseTestError("test_long_and_overflow",
672             "overflow was set incorrectly");
673 
674     num = PyLong_FromLong(LONG_MAX);
675     if (num == NULL)
676         return NULL;
677     overflow = 1234;
678     value = PyLong_AsLongAndOverflow(num, &overflow);
679     Py_DECREF(num);
680     if (value == -1 && PyErr_Occurred())
681         return NULL;
682     if (value != LONG_MAX)
683         return raiseTestError("test_long_and_overflow",
684             "expected return value LONG_MAX");
685     if (overflow != 0)
686         return raiseTestError("test_long_and_overflow",
687             "overflow was not cleared");
688 
689     num = PyLong_FromLong(LONG_MIN);
690     if (num == NULL)
691         return NULL;
692     overflow = 0;
693     value = PyLong_AsLongAndOverflow(num, &overflow);
694     Py_DECREF(num);
695     if (value == -1 && PyErr_Occurred())
696         return NULL;
697     if (value != LONG_MIN)
698         return raiseTestError("test_long_and_overflow",
699             "expected return value LONG_MIN");
700     if (overflow != 0)
701         return raiseTestError("test_long_and_overflow",
702             "overflow was not cleared");
703 
704     Py_RETURN_NONE;
705 }
706 
707 /* Test the PyLong_AsLongLongAndOverflow API. General conversion to
708    long long is tested by test_long_api_inner. This test will
709    concentrate on proper handling of overflow.
710 */
711 
712 static PyObject *
test_long_long_and_overflow(PyObject * self,PyObject * Py_UNUSED (ignored))713 test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
714 {
715     PyObject *num, *one, *temp;
716     long long value;
717     int overflow;
718 
719     /* Test that overflow is set properly for a large value. */
720     /* num is a number larger than LLONG_MAX on a typical machine. */
721     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
722     if (num == NULL)
723         return NULL;
724     overflow = 1234;
725     value = PyLong_AsLongLongAndOverflow(num, &overflow);
726     Py_DECREF(num);
727     if (value == -1 && PyErr_Occurred())
728         return NULL;
729     if (value != -1)
730         return raiseTestError("test_long_long_and_overflow",
731             "return value was not set to -1");
732     if (overflow != 1)
733         return raiseTestError("test_long_long_and_overflow",
734             "overflow was not set to 1");
735 
736     /* Same again, with num = LLONG_MAX + 1 */
737     num = PyLong_FromLongLong(LLONG_MAX);
738     if (num == NULL)
739         return NULL;
740     one = PyLong_FromLong(1L);
741     if (one == NULL) {
742         Py_DECREF(num);
743         return NULL;
744     }
745     temp = PyNumber_Add(num, one);
746     Py_DECREF(one);
747     Py_DECREF(num);
748     num = temp;
749     if (num == NULL)
750         return NULL;
751     overflow = 0;
752     value = PyLong_AsLongLongAndOverflow(num, &overflow);
753     Py_DECREF(num);
754     if (value == -1 && PyErr_Occurred())
755         return NULL;
756     if (value != -1)
757         return raiseTestError("test_long_long_and_overflow",
758             "return value was not set to -1");
759     if (overflow != 1)
760         return raiseTestError("test_long_long_and_overflow",
761             "overflow was not set to 1");
762 
763     /* Test that overflow is set properly for a large negative value. */
764     /* num is a number smaller than LLONG_MIN on a typical platform */
765     num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
766     if (num == NULL)
767         return NULL;
768     overflow = 1234;
769     value = PyLong_AsLongLongAndOverflow(num, &overflow);
770     Py_DECREF(num);
771     if (value == -1 && PyErr_Occurred())
772         return NULL;
773     if (value != -1)
774         return raiseTestError("test_long_long_and_overflow",
775             "return value was not set to -1");
776     if (overflow != -1)
777         return raiseTestError("test_long_long_and_overflow",
778             "overflow was not set to -1");
779 
780     /* Same again, with num = LLONG_MIN - 1 */
781     num = PyLong_FromLongLong(LLONG_MIN);
782     if (num == NULL)
783         return NULL;
784     one = PyLong_FromLong(1L);
785     if (one == NULL) {
786         Py_DECREF(num);
787         return NULL;
788     }
789     temp = PyNumber_Subtract(num, one);
790     Py_DECREF(one);
791     Py_DECREF(num);
792     num = temp;
793     if (num == NULL)
794         return NULL;
795     overflow = 0;
796     value = PyLong_AsLongLongAndOverflow(num, &overflow);
797     Py_DECREF(num);
798     if (value == -1 && PyErr_Occurred())
799         return NULL;
800     if (value != -1)
801         return raiseTestError("test_long_long_and_overflow",
802             "return value was not set to -1");
803     if (overflow != -1)
804         return raiseTestError("test_long_long_and_overflow",
805             "overflow was not set to -1");
806 
807     /* Test that overflow is cleared properly for small values. */
808     num = PyLong_FromString("FF", NULL, 16);
809     if (num == NULL)
810         return NULL;
811     overflow = 1234;
812     value = PyLong_AsLongLongAndOverflow(num, &overflow);
813     Py_DECREF(num);
814     if (value == -1 && PyErr_Occurred())
815         return NULL;
816     if (value != 0xFF)
817         return raiseTestError("test_long_long_and_overflow",
818             "expected return value 0xFF");
819     if (overflow != 0)
820         return raiseTestError("test_long_long_and_overflow",
821             "overflow was not cleared");
822 
823     num = PyLong_FromString("-FF", NULL, 16);
824     if (num == NULL)
825         return NULL;
826     overflow = 0;
827     value = PyLong_AsLongLongAndOverflow(num, &overflow);
828     Py_DECREF(num);
829     if (value == -1 && PyErr_Occurred())
830         return NULL;
831     if (value != -0xFF)
832         return raiseTestError("test_long_long_and_overflow",
833             "expected return value 0xFF");
834     if (overflow != 0)
835         return raiseTestError("test_long_long_and_overflow",
836             "overflow was set incorrectly");
837 
838     num = PyLong_FromLongLong(LLONG_MAX);
839     if (num == NULL)
840         return NULL;
841     overflow = 1234;
842     value = PyLong_AsLongLongAndOverflow(num, &overflow);
843     Py_DECREF(num);
844     if (value == -1 && PyErr_Occurred())
845         return NULL;
846     if (value != LLONG_MAX)
847         return raiseTestError("test_long_long_and_overflow",
848             "expected return value LLONG_MAX");
849     if (overflow != 0)
850         return raiseTestError("test_long_long_and_overflow",
851             "overflow was not cleared");
852 
853     num = PyLong_FromLongLong(LLONG_MIN);
854     if (num == NULL)
855         return NULL;
856     overflow = 0;
857     value = PyLong_AsLongLongAndOverflow(num, &overflow);
858     Py_DECREF(num);
859     if (value == -1 && PyErr_Occurred())
860         return NULL;
861     if (value != LLONG_MIN)
862         return raiseTestError("test_long_long_and_overflow",
863             "expected return value LLONG_MIN");
864     if (overflow != 0)
865         return raiseTestError("test_long_long_and_overflow",
866             "overflow was not cleared");
867 
868     Py_RETURN_NONE;
869 }
870 
871 /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that
872    non-integer arguments are handled correctly. It should be extended to
873    test overflow handling.
874  */
875 
876 static PyObject *
test_long_as_size_t(PyObject * self,PyObject * Py_UNUSED (ignored))877 test_long_as_size_t(PyObject *self, PyObject *Py_UNUSED(ignored))
878 {
879     size_t out_u;
880     Py_ssize_t out_s;
881 
882     Py_INCREF(Py_None);
883 
884     out_u = PyLong_AsSize_t(Py_None);
885     if (out_u != (size_t)-1 || !PyErr_Occurred())
886         return raiseTestError("test_long_as_size_t",
887                               "PyLong_AsSize_t(None) didn't complain");
888     if (!PyErr_ExceptionMatches(PyExc_TypeError))
889         return raiseTestError("test_long_as_size_t",
890                               "PyLong_AsSize_t(None) raised "
891                               "something other than TypeError");
892     PyErr_Clear();
893 
894     out_s = PyLong_AsSsize_t(Py_None);
895     if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred())
896         return raiseTestError("test_long_as_size_t",
897                               "PyLong_AsSsize_t(None) didn't complain");
898     if (!PyErr_ExceptionMatches(PyExc_TypeError))
899         return raiseTestError("test_long_as_size_t",
900                               "PyLong_AsSsize_t(None) raised "
901                               "something other than TypeError");
902     PyErr_Clear();
903 
904     /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
905     return Py_None;
906 }
907 
908 static PyObject *
test_long_as_unsigned_long_long_mask(PyObject * self,PyObject * Py_UNUSED (ignored))909 test_long_as_unsigned_long_long_mask(PyObject *self,
910                                      PyObject *Py_UNUSED(ignored))
911 {
912     unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL);
913 
914     if (res != (unsigned long long)-1 || !PyErr_Occurred()) {
915         return raiseTestError("test_long_as_unsigned_long_long_mask",
916                               "PyLong_AsUnsignedLongLongMask(NULL) didn't "
917                               "complain");
918     }
919     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
920         return raiseTestError("test_long_as_unsigned_long_long_mask",
921                               "PyLong_AsUnsignedLongLongMask(NULL) raised "
922                               "something other than SystemError");
923     }
924     PyErr_Clear();
925     Py_RETURN_NONE;
926 }
927 
928 /* Test the PyLong_AsDouble API. At present this just tests that
929    non-integer arguments are handled correctly.
930  */
931 
932 static PyObject *
test_long_as_double(PyObject * self,PyObject * Py_UNUSED (ignored))933 test_long_as_double(PyObject *self, PyObject *Py_UNUSED(ignored))
934 {
935     double out;
936 
937     Py_INCREF(Py_None);
938 
939     out = PyLong_AsDouble(Py_None);
940     if (out != -1.0 || !PyErr_Occurred())
941         return raiseTestError("test_long_as_double",
942                               "PyLong_AsDouble(None) didn't complain");
943     if (!PyErr_ExceptionMatches(PyExc_TypeError))
944         return raiseTestError("test_long_as_double",
945                               "PyLong_AsDouble(None) raised "
946                               "something other than TypeError");
947     PyErr_Clear();
948 
949     /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
950     return Py_None;
951 }
952 
953 /* Test the L code for PyArg_ParseTuple.  This should deliver a long long
954    for both long and int arguments.  The test may leak a little memory if
955    it fails.
956 */
957 static PyObject *
test_L_code(PyObject * self,PyObject * Py_UNUSED (ignored))958 test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored))
959 {
960     PyObject *tuple, *num;
961     long long value;
962 
963     tuple = PyTuple_New(1);
964     if (tuple == NULL)
965         return NULL;
966 
967     num = PyLong_FromLong(42);
968     if (num == NULL)
969         return NULL;
970 
971     PyTuple_SET_ITEM(tuple, 0, num);
972 
973     value = -1;
974     if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
975         return NULL;
976     }
977     if (value != 42)
978         return raiseTestError("test_L_code",
979             "L code returned wrong value for long 42");
980 
981     Py_DECREF(num);
982     num = PyLong_FromLong(42);
983     if (num == NULL)
984         return NULL;
985 
986     PyTuple_SET_ITEM(tuple, 0, num);
987 
988     value = -1;
989     if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
990         return NULL;
991     }
992     if (value != 42)
993         return raiseTestError("test_L_code",
994             "L code returned wrong value for int 42");
995 
996     Py_DECREF(tuple);
997     Py_RETURN_NONE;
998 }
999 
1000 static PyObject *
return_none(void * unused)1001 return_none(void *unused)
1002 {
1003     Py_RETURN_NONE;
1004 }
1005 
1006 static PyObject *
raise_error(void * unused)1007 raise_error(void *unused)
1008 {
1009     PyErr_SetNone(PyExc_ValueError);
1010     return NULL;
1011 }
1012 
1013 static int
test_buildvalue_N_error(const char * fmt)1014 test_buildvalue_N_error(const char *fmt)
1015 {
1016     PyObject *arg, *res;
1017 
1018     arg = PyList_New(0);
1019     if (arg == NULL) {
1020         return -1;
1021     }
1022 
1023     Py_INCREF(arg);
1024     res = Py_BuildValue(fmt, return_none, NULL, arg);
1025     if (res == NULL) {
1026         return -1;
1027     }
1028     Py_DECREF(res);
1029     if (Py_REFCNT(arg) != 1) {
1030         PyErr_Format(TestError, "test_buildvalue_N: "
1031                      "arg was not decrefed in successful "
1032                      "Py_BuildValue(\"%s\")", fmt);
1033         return -1;
1034     }
1035 
1036     Py_INCREF(arg);
1037     res = Py_BuildValue(fmt, raise_error, NULL, arg);
1038     if (res != NULL || !PyErr_Occurred()) {
1039         PyErr_Format(TestError, "test_buildvalue_N: "
1040                      "Py_BuildValue(\"%s\") didn't complain", fmt);
1041         return -1;
1042     }
1043     PyErr_Clear();
1044     if (Py_REFCNT(arg) != 1) {
1045         PyErr_Format(TestError, "test_buildvalue_N: "
1046                      "arg was not decrefed in failed "
1047                      "Py_BuildValue(\"%s\")", fmt);
1048         return -1;
1049     }
1050     Py_DECREF(arg);
1051     return 0;
1052 }
1053 
1054 static PyObject *
test_buildvalue_N(PyObject * self,PyObject * Py_UNUSED (ignored))1055 test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
1056 {
1057     PyObject *arg, *res;
1058 
1059     arg = PyList_New(0);
1060     if (arg == NULL) {
1061         return NULL;
1062     }
1063     Py_INCREF(arg);
1064     res = Py_BuildValue("N", arg);
1065     if (res == NULL) {
1066         return NULL;
1067     }
1068     if (res != arg) {
1069         return raiseTestError("test_buildvalue_N",
1070                               "Py_BuildValue(\"N\") returned wrong result");
1071     }
1072     if (Py_REFCNT(arg) != 2) {
1073         return raiseTestError("test_buildvalue_N",
1074                               "arg was not decrefed in Py_BuildValue(\"N\")");
1075     }
1076     Py_DECREF(res);
1077     Py_DECREF(arg);
1078 
1079     if (test_buildvalue_N_error("O&N") < 0)
1080         return NULL;
1081     if (test_buildvalue_N_error("(O&N)") < 0)
1082         return NULL;
1083     if (test_buildvalue_N_error("[O&N]") < 0)
1084         return NULL;
1085     if (test_buildvalue_N_error("{O&N}") < 0)
1086         return NULL;
1087     if (test_buildvalue_N_error("{()O&(())N}") < 0)
1088         return NULL;
1089 
1090     Py_RETURN_NONE;
1091 }
1092 
1093 
1094 static PyObject *
test_get_statictype_slots(PyObject * self,PyObject * Py_UNUSED (ignored))1095 test_get_statictype_slots(PyObject *self, PyObject *Py_UNUSED(ignored))
1096 {
1097     newfunc tp_new = PyType_GetSlot(&PyLong_Type, Py_tp_new);
1098     if (PyLong_Type.tp_new != tp_new) {
1099         PyErr_SetString(PyExc_AssertionError, "mismatch: tp_new of long");
1100         return NULL;
1101     }
1102 
1103     reprfunc tp_repr = PyType_GetSlot(&PyLong_Type, Py_tp_repr);
1104     if (PyLong_Type.tp_repr != tp_repr) {
1105         PyErr_SetString(PyExc_AssertionError, "mismatch: tp_repr of long");
1106         return NULL;
1107     }
1108 
1109     ternaryfunc tp_call = PyType_GetSlot(&PyLong_Type, Py_tp_call);
1110     if (tp_call != NULL) {
1111         PyErr_SetString(PyExc_AssertionError, "mismatch: tp_call of long");
1112         return NULL;
1113     }
1114 
1115     binaryfunc nb_add = PyType_GetSlot(&PyLong_Type, Py_nb_add);
1116     if (PyLong_Type.tp_as_number->nb_add != nb_add) {
1117         PyErr_SetString(PyExc_AssertionError, "mismatch: nb_add of long");
1118         return NULL;
1119     }
1120 
1121     lenfunc mp_length = PyType_GetSlot(&PyLong_Type, Py_mp_length);
1122     if (mp_length != NULL) {
1123         PyErr_SetString(PyExc_AssertionError, "mismatch: mp_length of long");
1124         return NULL;
1125     }
1126 
1127     void *over_value = PyType_GetSlot(&PyLong_Type, Py_bf_releasebuffer + 1);
1128     if (over_value != NULL) {
1129         PyErr_SetString(PyExc_AssertionError, "mismatch: max+1 of long");
1130         return NULL;
1131     }
1132 
1133     tp_new = PyType_GetSlot(&PyLong_Type, 0);
1134     if (tp_new != NULL) {
1135         PyErr_SetString(PyExc_AssertionError, "mismatch: slot 0 of long");
1136         return NULL;
1137     }
1138     if (PyErr_ExceptionMatches(PyExc_SystemError)) {
1139         // This is the right exception
1140         PyErr_Clear();
1141     }
1142     else {
1143         return NULL;
1144     }
1145 
1146     Py_RETURN_NONE;
1147 }
1148 
1149 
1150 static PyObject *
get_args(PyObject * self,PyObject * args)1151 get_args(PyObject *self, PyObject *args)
1152 {
1153     if (args == NULL) {
1154         args = Py_None;
1155     }
1156     Py_INCREF(args);
1157     return args;
1158 }
1159 
1160 static PyObject *
get_kwargs(PyObject * self,PyObject * args,PyObject * kwargs)1161 get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
1162 {
1163     if (kwargs == NULL) {
1164         kwargs = Py_None;
1165     }
1166     Py_INCREF(kwargs);
1167     return kwargs;
1168 }
1169 
1170 /* Test tuple argument processing */
1171 static PyObject *
getargs_tuple(PyObject * self,PyObject * args)1172 getargs_tuple(PyObject *self, PyObject *args)
1173 {
1174     int a, b, c;
1175     if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
1176         return NULL;
1177     return Py_BuildValue("iii", a, b, c);
1178 }
1179 
1180 /* test PyArg_ParseTupleAndKeywords */
1181 static PyObject *
getargs_keywords(PyObject * self,PyObject * args,PyObject * kwargs)1182 getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
1183 {
1184     static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
1185     static const char fmt[] = "(ii)i|(i(ii))(iii)i";
1186     int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1187 
1188     if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
1189         &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
1190         &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
1191         return NULL;
1192     return Py_BuildValue("iiiiiiiiii",
1193         int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
1194         int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
1195 }
1196 
1197 /* test PyArg_ParseTupleAndKeywords keyword-only arguments */
1198 static PyObject *
getargs_keyword_only(PyObject * self,PyObject * args,PyObject * kwargs)1199 getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
1200 {
1201     static char *keywords[] = {"required", "optional", "keyword_only", NULL};
1202     int required = -1;
1203     int optional = -1;
1204     int keyword_only = -1;
1205 
1206     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
1207                                      &required, &optional, &keyword_only))
1208         return NULL;
1209     return Py_BuildValue("iii", required, optional, keyword_only);
1210 }
1211 
1212 /* test PyArg_ParseTupleAndKeywords positional-only arguments */
1213 static PyObject *
getargs_positional_only_and_keywords(PyObject * self,PyObject * args,PyObject * kwargs)1214 getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
1215 {
1216     static char *keywords[] = {"", "", "keyword", NULL};
1217     int required = -1;
1218     int optional = -1;
1219     int keyword = -1;
1220 
1221     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords,
1222                                      &required, &optional, &keyword))
1223         return NULL;
1224     return Py_BuildValue("iii", required, optional, keyword);
1225 }
1226 
1227 /* Functions to call PyArg_ParseTuple with integer format codes,
1228    and return the result.
1229 */
1230 static PyObject *
getargs_b(PyObject * self,PyObject * args)1231 getargs_b(PyObject *self, PyObject *args)
1232 {
1233     unsigned char value;
1234     if (!PyArg_ParseTuple(args, "b", &value))
1235         return NULL;
1236     return PyLong_FromUnsignedLong((unsigned long)value);
1237 }
1238 
1239 static PyObject *
getargs_B(PyObject * self,PyObject * args)1240 getargs_B(PyObject *self, PyObject *args)
1241 {
1242     unsigned char value;
1243     if (!PyArg_ParseTuple(args, "B", &value))
1244         return NULL;
1245     return PyLong_FromUnsignedLong((unsigned long)value);
1246 }
1247 
1248 static PyObject *
getargs_h(PyObject * self,PyObject * args)1249 getargs_h(PyObject *self, PyObject *args)
1250 {
1251     short value;
1252     if (!PyArg_ParseTuple(args, "h", &value))
1253         return NULL;
1254     return PyLong_FromLong((long)value);
1255 }
1256 
1257 static PyObject *
getargs_H(PyObject * self,PyObject * args)1258 getargs_H(PyObject *self, PyObject *args)
1259 {
1260     unsigned short value;
1261     if (!PyArg_ParseTuple(args, "H", &value))
1262         return NULL;
1263     return PyLong_FromUnsignedLong((unsigned long)value);
1264 }
1265 
1266 static PyObject *
getargs_I(PyObject * self,PyObject * args)1267 getargs_I(PyObject *self, PyObject *args)
1268 {
1269     unsigned int value;
1270     if (!PyArg_ParseTuple(args, "I", &value))
1271         return NULL;
1272     return PyLong_FromUnsignedLong((unsigned long)value);
1273 }
1274 
1275 static PyObject *
getargs_k(PyObject * self,PyObject * args)1276 getargs_k(PyObject *self, PyObject *args)
1277 {
1278     unsigned long value;
1279     if (!PyArg_ParseTuple(args, "k", &value))
1280         return NULL;
1281     return PyLong_FromUnsignedLong(value);
1282 }
1283 
1284 static PyObject *
getargs_i(PyObject * self,PyObject * args)1285 getargs_i(PyObject *self, PyObject *args)
1286 {
1287     int value;
1288     if (!PyArg_ParseTuple(args, "i", &value))
1289         return NULL;
1290     return PyLong_FromLong((long)value);
1291 }
1292 
1293 static PyObject *
getargs_l(PyObject * self,PyObject * args)1294 getargs_l(PyObject *self, PyObject *args)
1295 {
1296     long value;
1297     if (!PyArg_ParseTuple(args, "l", &value))
1298         return NULL;
1299     return PyLong_FromLong(value);
1300 }
1301 
1302 static PyObject *
getargs_n(PyObject * self,PyObject * args)1303 getargs_n(PyObject *self, PyObject *args)
1304 {
1305     Py_ssize_t value;
1306     if (!PyArg_ParseTuple(args, "n", &value))
1307         return NULL;
1308     return PyLong_FromSsize_t(value);
1309 }
1310 
1311 static PyObject *
getargs_p(PyObject * self,PyObject * args)1312 getargs_p(PyObject *self, PyObject *args)
1313 {
1314     int value;
1315     if (!PyArg_ParseTuple(args, "p", &value))
1316         return NULL;
1317     return PyLong_FromLong(value);
1318 }
1319 
1320 static PyObject *
getargs_L(PyObject * self,PyObject * args)1321 getargs_L(PyObject *self, PyObject *args)
1322 {
1323     long long value;
1324     if (!PyArg_ParseTuple(args, "L", &value))
1325         return NULL;
1326     return PyLong_FromLongLong(value);
1327 }
1328 
1329 static PyObject *
getargs_K(PyObject * self,PyObject * args)1330 getargs_K(PyObject *self, PyObject *args)
1331 {
1332     unsigned long long value;
1333     if (!PyArg_ParseTuple(args, "K", &value))
1334         return NULL;
1335     return PyLong_FromUnsignedLongLong(value);
1336 }
1337 
1338 /* This function not only tests the 'k' getargs code, but also the
1339    PyLong_AsUnsignedLongMask() function. */
1340 static PyObject *
test_k_code(PyObject * self,PyObject * Py_UNUSED (ignored))1341 test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored))
1342 {
1343     PyObject *tuple, *num;
1344     unsigned long value;
1345 
1346     tuple = PyTuple_New(1);
1347     if (tuple == NULL)
1348         return NULL;
1349 
1350     /* a number larger than ULONG_MAX even on 64-bit platforms */
1351     num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
1352     if (num == NULL)
1353         return NULL;
1354 
1355     value = PyLong_AsUnsignedLongMask(num);
1356     if (value != ULONG_MAX)
1357         return raiseTestError("test_k_code",
1358             "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
1359 
1360     PyTuple_SET_ITEM(tuple, 0, num);
1361 
1362     value = 0;
1363     if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
1364         return NULL;
1365     }
1366     if (value != ULONG_MAX)
1367         return raiseTestError("test_k_code",
1368             "k code returned wrong value for long 0xFFF...FFF");
1369 
1370     Py_DECREF(num);
1371     num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
1372     if (num == NULL)
1373         return NULL;
1374 
1375     value = PyLong_AsUnsignedLongMask(num);
1376     if (value != (unsigned long)-0x42)
1377         return raiseTestError("test_k_code",
1378                               "PyLong_AsUnsignedLongMask() returned wrong "
1379                               "value for long -0xFFF..000042");
1380 
1381     PyTuple_SET_ITEM(tuple, 0, num);
1382 
1383     value = 0;
1384     if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
1385         return NULL;
1386     }
1387     if (value != (unsigned long)-0x42)
1388         return raiseTestError("test_k_code",
1389             "k code returned wrong value for long -0xFFF..000042");
1390 
1391     Py_DECREF(tuple);
1392     Py_RETURN_NONE;
1393 }
1394 
1395 static PyObject *
getargs_f(PyObject * self,PyObject * args)1396 getargs_f(PyObject *self, PyObject *args)
1397 {
1398     float f;
1399     if (!PyArg_ParseTuple(args, "f", &f))
1400         return NULL;
1401     return PyFloat_FromDouble(f);
1402 }
1403 
1404 static PyObject *
getargs_d(PyObject * self,PyObject * args)1405 getargs_d(PyObject *self, PyObject *args)
1406 {
1407     double d;
1408     if (!PyArg_ParseTuple(args, "d", &d))
1409         return NULL;
1410     return PyFloat_FromDouble(d);
1411 }
1412 
1413 static PyObject *
getargs_D(PyObject * self,PyObject * args)1414 getargs_D(PyObject *self, PyObject *args)
1415 {
1416     Py_complex cval;
1417     if (!PyArg_ParseTuple(args, "D", &cval))
1418         return NULL;
1419     return PyComplex_FromCComplex(cval);
1420 }
1421 
1422 static PyObject *
getargs_S(PyObject * self,PyObject * args)1423 getargs_S(PyObject *self, PyObject *args)
1424 {
1425     PyObject *obj;
1426     if (!PyArg_ParseTuple(args, "S", &obj))
1427         return NULL;
1428     Py_INCREF(obj);
1429     return obj;
1430 }
1431 
1432 static PyObject *
getargs_Y(PyObject * self,PyObject * args)1433 getargs_Y(PyObject *self, PyObject *args)
1434 {
1435     PyObject *obj;
1436     if (!PyArg_ParseTuple(args, "Y", &obj))
1437         return NULL;
1438     Py_INCREF(obj);
1439     return obj;
1440 }
1441 
1442 static PyObject *
getargs_U(PyObject * self,PyObject * args)1443 getargs_U(PyObject *self, PyObject *args)
1444 {
1445     PyObject *obj;
1446     if (!PyArg_ParseTuple(args, "U", &obj))
1447         return NULL;
1448     Py_INCREF(obj);
1449     return obj;
1450 }
1451 
1452 static PyObject *
getargs_c(PyObject * self,PyObject * args)1453 getargs_c(PyObject *self, PyObject *args)
1454 {
1455     char c;
1456     if (!PyArg_ParseTuple(args, "c", &c))
1457         return NULL;
1458     return PyLong_FromLong((unsigned char)c);
1459 }
1460 
1461 static PyObject *
getargs_C(PyObject * self,PyObject * args)1462 getargs_C(PyObject *self, PyObject *args)
1463 {
1464     int c;
1465     if (!PyArg_ParseTuple(args, "C", &c))
1466         return NULL;
1467     return PyLong_FromLong(c);
1468 }
1469 
1470 static PyObject *
getargs_s(PyObject * self,PyObject * args)1471 getargs_s(PyObject *self, PyObject *args)
1472 {
1473     char *str;
1474     if (!PyArg_ParseTuple(args, "s", &str))
1475         return NULL;
1476     return PyBytes_FromString(str);
1477 }
1478 
1479 static PyObject *
getargs_s_star(PyObject * self,PyObject * args)1480 getargs_s_star(PyObject *self, PyObject *args)
1481 {
1482     Py_buffer buffer;
1483     PyObject *bytes;
1484     if (!PyArg_ParseTuple(args, "s*", &buffer))
1485         return NULL;
1486     bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1487     PyBuffer_Release(&buffer);
1488     return bytes;
1489 }
1490 
1491 static PyObject *
getargs_s_hash(PyObject * self,PyObject * args)1492 getargs_s_hash(PyObject *self, PyObject *args)
1493 {
1494     char *str;
1495     Py_ssize_t size;
1496     if (!PyArg_ParseTuple(args, "s#", &str, &size))
1497         return NULL;
1498     return PyBytes_FromStringAndSize(str, size);
1499 }
1500 
1501 static PyObject *
getargs_z(PyObject * self,PyObject * args)1502 getargs_z(PyObject *self, PyObject *args)
1503 {
1504     char *str;
1505     if (!PyArg_ParseTuple(args, "z", &str))
1506         return NULL;
1507     if (str != NULL)
1508         return PyBytes_FromString(str);
1509     else
1510         Py_RETURN_NONE;
1511 }
1512 
1513 static PyObject *
getargs_z_star(PyObject * self,PyObject * args)1514 getargs_z_star(PyObject *self, PyObject *args)
1515 {
1516     Py_buffer buffer;
1517     PyObject *bytes;
1518     if (!PyArg_ParseTuple(args, "z*", &buffer))
1519         return NULL;
1520     if (buffer.buf != NULL)
1521         bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1522     else {
1523         Py_INCREF(Py_None);
1524         bytes = Py_None;
1525     }
1526     PyBuffer_Release(&buffer);
1527     return bytes;
1528 }
1529 
1530 static PyObject *
getargs_z_hash(PyObject * self,PyObject * args)1531 getargs_z_hash(PyObject *self, PyObject *args)
1532 {
1533     char *str;
1534     Py_ssize_t size;
1535     if (!PyArg_ParseTuple(args, "z#", &str, &size))
1536         return NULL;
1537     if (str != NULL)
1538         return PyBytes_FromStringAndSize(str, size);
1539     else
1540         Py_RETURN_NONE;
1541 }
1542 
1543 static PyObject *
getargs_y(PyObject * self,PyObject * args)1544 getargs_y(PyObject *self, PyObject *args)
1545 {
1546     char *str;
1547     if (!PyArg_ParseTuple(args, "y", &str))
1548         return NULL;
1549     return PyBytes_FromString(str);
1550 }
1551 
1552 static PyObject *
getargs_y_star(PyObject * self,PyObject * args)1553 getargs_y_star(PyObject *self, PyObject *args)
1554 {
1555     Py_buffer buffer;
1556     PyObject *bytes;
1557     if (!PyArg_ParseTuple(args, "y*", &buffer))
1558         return NULL;
1559     bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1560     PyBuffer_Release(&buffer);
1561     return bytes;
1562 }
1563 
1564 static PyObject *
getargs_y_hash(PyObject * self,PyObject * args)1565 getargs_y_hash(PyObject *self, PyObject *args)
1566 {
1567     char *str;
1568     Py_ssize_t size;
1569     if (!PyArg_ParseTuple(args, "y#", &str, &size))
1570         return NULL;
1571     return PyBytes_FromStringAndSize(str, size);
1572 }
1573 
1574 static PyObject *
getargs_u(PyObject * self,PyObject * args)1575 getargs_u(PyObject *self, PyObject *args)
1576 {
1577     Py_UNICODE *str;
1578     if (!PyArg_ParseTuple(args, "u", &str))
1579         return NULL;
1580     return PyUnicode_FromWideChar(str, -1);
1581 }
1582 
1583 static PyObject *
getargs_u_hash(PyObject * self,PyObject * args)1584 getargs_u_hash(PyObject *self, PyObject *args)
1585 {
1586     Py_UNICODE *str;
1587     Py_ssize_t size;
1588     if (!PyArg_ParseTuple(args, "u#", &str, &size))
1589         return NULL;
1590     return PyUnicode_FromWideChar(str, size);
1591 }
1592 
1593 static PyObject *
getargs_Z(PyObject * self,PyObject * args)1594 getargs_Z(PyObject *self, PyObject *args)
1595 {
1596     Py_UNICODE *str;
1597     if (!PyArg_ParseTuple(args, "Z", &str))
1598         return NULL;
1599     if (str != NULL) {
1600         return PyUnicode_FromWideChar(str, -1);
1601     } else
1602         Py_RETURN_NONE;
1603 }
1604 
1605 static PyObject *
getargs_Z_hash(PyObject * self,PyObject * args)1606 getargs_Z_hash(PyObject *self, PyObject *args)
1607 {
1608     Py_UNICODE *str;
1609     Py_ssize_t size;
1610     if (!PyArg_ParseTuple(args, "Z#", &str, &size))
1611         return NULL;
1612     if (str != NULL)
1613         return PyUnicode_FromWideChar(str, size);
1614     else
1615         Py_RETURN_NONE;
1616 }
1617 
1618 static PyObject *
getargs_es(PyObject * self,PyObject * args)1619 getargs_es(PyObject *self, PyObject *args)
1620 {
1621     PyObject *arg, *result;
1622     const char *encoding = NULL;
1623     char *str;
1624 
1625     if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding))
1626         return NULL;
1627     if (!PyArg_Parse(arg, "es", encoding, &str))
1628         return NULL;
1629     result = PyBytes_FromString(str);
1630     PyMem_Free(str);
1631     return result;
1632 }
1633 
1634 static PyObject *
getargs_et(PyObject * self,PyObject * args)1635 getargs_et(PyObject *self, PyObject *args)
1636 {
1637     PyObject *arg, *result;
1638     const char *encoding = NULL;
1639     char *str;
1640 
1641     if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding))
1642         return NULL;
1643     if (!PyArg_Parse(arg, "et", encoding, &str))
1644         return NULL;
1645     result = PyBytes_FromString(str);
1646     PyMem_Free(str);
1647     return result;
1648 }
1649 
1650 static PyObject *
getargs_es_hash(PyObject * self,PyObject * args)1651 getargs_es_hash(PyObject *self, PyObject *args)
1652 {
1653     PyObject *arg, *result;
1654     const char *encoding = NULL;
1655     PyByteArrayObject *buffer = NULL;
1656     char *str = NULL;
1657     Py_ssize_t size;
1658 
1659     if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer))
1660         return NULL;
1661     if (buffer != NULL) {
1662         str = PyByteArray_AS_STRING(buffer);
1663         size = PyByteArray_GET_SIZE(buffer);
1664     }
1665     if (!PyArg_Parse(arg, "es#", encoding, &str, &size))
1666         return NULL;
1667     result = PyBytes_FromStringAndSize(str, size);
1668     if (buffer == NULL)
1669         PyMem_Free(str);
1670     return result;
1671 }
1672 
1673 static PyObject *
getargs_et_hash(PyObject * self,PyObject * args)1674 getargs_et_hash(PyObject *self, PyObject *args)
1675 {
1676     PyObject *arg, *result;
1677     const char *encoding = NULL;
1678     PyByteArrayObject *buffer = NULL;
1679     char *str = NULL;
1680     Py_ssize_t size;
1681 
1682     if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer))
1683         return NULL;
1684     if (buffer != NULL) {
1685         str = PyByteArray_AS_STRING(buffer);
1686         size = PyByteArray_GET_SIZE(buffer);
1687     }
1688     if (!PyArg_Parse(arg, "et#", encoding, &str, &size))
1689         return NULL;
1690     result = PyBytes_FromStringAndSize(str, size);
1691     if (buffer == NULL)
1692         PyMem_Free(str);
1693     return result;
1694 }
1695 
1696 /* Test the s and z codes for PyArg_ParseTuple.
1697 */
1698 static PyObject *
test_s_code(PyObject * self,PyObject * Py_UNUSED (ignored))1699 test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored))
1700 {
1701     /* Unicode strings should be accepted */
1702     PyObject *tuple, *obj;
1703     char *value;
1704 
1705     tuple = PyTuple_New(1);
1706     if (tuple == NULL)
1707     return NULL;
1708 
1709     obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
1710                            "latin-1", NULL);
1711     if (obj == NULL)
1712     return NULL;
1713 
1714     PyTuple_SET_ITEM(tuple, 0, obj);
1715 
1716     /* These two blocks used to raise a TypeError:
1717      * "argument must be string without null bytes, not str"
1718      */
1719     if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) {
1720         return NULL;
1721     }
1722 
1723     if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) {
1724         return NULL;
1725     }
1726 
1727     Py_DECREF(tuple);
1728     Py_RETURN_NONE;
1729 }
1730 
1731 static PyObject *
parse_tuple_and_keywords(PyObject * self,PyObject * args)1732 parse_tuple_and_keywords(PyObject *self, PyObject *args)
1733 {
1734     PyObject *sub_args;
1735     PyObject *sub_kwargs;
1736     const char *sub_format;
1737     PyObject *sub_keywords;
1738 
1739     Py_ssize_t i, size;
1740     char *keywords[8 + 1]; /* space for NULL at end */
1741     PyObject *o;
1742     PyObject *converted[8];
1743 
1744     int result;
1745     PyObject *return_value = NULL;
1746 
1747     double buffers[8][4]; /* double ensures alignment where necessary */
1748 
1749     if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords",
1750         &sub_args, &sub_kwargs,
1751         &sub_format, &sub_keywords))
1752         return NULL;
1753 
1754     if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) {
1755         PyErr_SetString(PyExc_ValueError,
1756             "parse_tuple_and_keywords: sub_keywords must be either list or tuple");
1757         return NULL;
1758     }
1759 
1760     memset(buffers, 0, sizeof(buffers));
1761     memset(converted, 0, sizeof(converted));
1762     memset(keywords, 0, sizeof(keywords));
1763 
1764     size = PySequence_Fast_GET_SIZE(sub_keywords);
1765     if (size > 8) {
1766         PyErr_SetString(PyExc_ValueError,
1767             "parse_tuple_and_keywords: too many keywords in sub_keywords");
1768         goto exit;
1769     }
1770 
1771     for (i = 0; i < size; i++) {
1772         o = PySequence_Fast_GET_ITEM(sub_keywords, i);
1773         if (!PyUnicode_FSConverter(o, (void *)(converted + i))) {
1774             PyErr_Format(PyExc_ValueError,
1775                 "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i);
1776             goto exit;
1777         }
1778         keywords[i] = PyBytes_AS_STRING(converted[i]);
1779     }
1780 
1781     result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs,
1782         sub_format, keywords,
1783         buffers + 0, buffers + 1, buffers + 2, buffers + 3,
1784         buffers + 4, buffers + 5, buffers + 6, buffers + 7);
1785 
1786     if (result) {
1787         return_value = Py_None;
1788         Py_INCREF(Py_None);
1789     }
1790 
1791 exit:
1792     size = sizeof(converted) / sizeof(converted[0]);
1793     for (i = 0; i < size; i++) {
1794         Py_XDECREF(converted[i]);
1795     }
1796     return return_value;
1797 }
1798 
1799 static volatile int x;
1800 
1801 #if USE_UNICODE_WCHAR_CACHE
1802 /* Ignore use of deprecated APIs */
1803 _Py_COMP_DIAG_PUSH
1804 _Py_COMP_DIAG_IGNORE_DEPR_DECLS
1805 
1806 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
1807    of an error.
1808 */
1809 static PyObject *
test_u_code(PyObject * self,PyObject * Py_UNUSED (ignored))1810 test_u_code(PyObject *self, PyObject *Py_UNUSED(ignored))
1811 {
1812     PyObject *tuple, *obj;
1813     Py_UNICODE *value;
1814     Py_ssize_t len;
1815 
1816     /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
1817     /* Just use the macro and check that it compiles */
1818     x = Py_UNICODE_ISSPACE(25);
1819 
1820     tuple = PyTuple_New(1);
1821     if (tuple == NULL)
1822         return NULL;
1823 
1824     obj = PyUnicode_Decode("test", strlen("test"),
1825                            "ascii", NULL);
1826     if (obj == NULL)
1827         return NULL;
1828 
1829     PyTuple_SET_ITEM(tuple, 0, obj);
1830 
1831     value = 0;
1832     if (!PyArg_ParseTuple(tuple, "u:test_u_code", &value)) {
1833         return NULL;
1834     }
1835     if (value != PyUnicode_AS_UNICODE(obj))
1836         return raiseTestError("test_u_code",
1837             "u code returned wrong value for u'test'");
1838     value = 0;
1839     if (!PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len)) {
1840         return NULL;
1841     }
1842     if (value != PyUnicode_AS_UNICODE(obj) ||
1843         len != PyUnicode_GET_SIZE(obj))
1844         return raiseTestError("test_u_code",
1845             "u# code returned wrong values for u'test'");
1846 
1847     Py_DECREF(tuple);
1848     Py_RETURN_NONE;
1849 }
1850 
1851 /* Test Z and Z# codes for PyArg_ParseTuple */
1852 static PyObject *
test_Z_code(PyObject * self,PyObject * Py_UNUSED (ignored))1853 test_Z_code(PyObject *self, PyObject *Py_UNUSED(ignored))
1854 {
1855     PyObject *tuple, *obj;
1856     const Py_UNICODE *value1, *value2;
1857     Py_ssize_t len1, len2;
1858 
1859     tuple = PyTuple_New(2);
1860     if (tuple == NULL)
1861         return NULL;
1862 
1863     obj = PyUnicode_FromString("test");
1864     PyTuple_SET_ITEM(tuple, 0, obj);
1865     Py_INCREF(Py_None);
1866     PyTuple_SET_ITEM(tuple, 1, Py_None);
1867 
1868     /* swap values on purpose */
1869     value1 = NULL;
1870     value2 = PyUnicode_AS_UNICODE(obj);
1871 
1872     /* Test Z for both values */
1873     if (!PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2)) {
1874         return NULL;
1875     }
1876     if (value1 != PyUnicode_AS_UNICODE(obj))
1877         return raiseTestError("test_Z_code",
1878             "Z code returned wrong value for 'test'");
1879     if (value2 != NULL)
1880         return raiseTestError("test_Z_code",
1881             "Z code returned wrong value for None");
1882 
1883     value1 = NULL;
1884     value2 = PyUnicode_AS_UNICODE(obj);
1885     len1 = -1;
1886     len2 = -1;
1887 
1888     /* Test Z# for both values */
1889     if (!PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
1890                           &value2, &len2))
1891     {
1892         return NULL;
1893     }
1894     if (value1 != PyUnicode_AS_UNICODE(obj) ||
1895         len1 != PyUnicode_GET_SIZE(obj))
1896         return raiseTestError("test_Z_code",
1897             "Z# code returned wrong values for 'test'");
1898     if (value2 != NULL ||
1899         len2 != 0)
1900         return raiseTestError("test_Z_code",
1901             "Z# code returned wrong values for None'");
1902 
1903     Py_DECREF(tuple);
1904     Py_RETURN_NONE;
1905 }
1906 _Py_COMP_DIAG_POP
1907 #endif /* USE_UNICODE_WCHAR_CACHE */
1908 
1909 static PyObject *
test_widechar(PyObject * self,PyObject * Py_UNUSED (ignored))1910 test_widechar(PyObject *self, PyObject *Py_UNUSED(ignored))
1911 {
1912 #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
1913     const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
1914     size_t wtextlen = 1;
1915     const wchar_t invalid[1] = {(wchar_t)0x110000u};
1916 #else
1917     const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
1918     size_t wtextlen = 2;
1919 #endif
1920     PyObject *wide, *utf8;
1921 
1922     wide = PyUnicode_FromWideChar(wtext, wtextlen);
1923     if (wide == NULL)
1924         return NULL;
1925 
1926     utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
1927     if (utf8 == NULL) {
1928         Py_DECREF(wide);
1929         return NULL;
1930     }
1931 
1932     if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) {
1933         Py_DECREF(wide);
1934         Py_DECREF(utf8);
1935         return raiseTestError("test_widechar",
1936                               "wide string and utf8 string "
1937                               "have different length");
1938     }
1939     if (PyUnicode_Compare(wide, utf8)) {
1940         Py_DECREF(wide);
1941         Py_DECREF(utf8);
1942         if (PyErr_Occurred())
1943             return NULL;
1944         return raiseTestError("test_widechar",
1945                               "wide string and utf8 string "
1946                               "are different");
1947     }
1948 
1949     Py_DECREF(wide);
1950     Py_DECREF(utf8);
1951 
1952 #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
1953     wide = PyUnicode_FromWideChar(invalid, 1);
1954     if (wide == NULL)
1955         PyErr_Clear();
1956     else
1957         return raiseTestError("test_widechar",
1958                               "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
1959 
1960 #if USE_UNICODE_WCHAR_CACHE
1961 /* Ignore use of deprecated APIs */
1962 _Py_COMP_DIAG_PUSH
1963 _Py_COMP_DIAG_IGNORE_DEPR_DECLS
1964     wide = PyUnicode_FromUnicode(invalid, 1);
1965     if (wide == NULL)
1966         PyErr_Clear();
1967     else
1968         return raiseTestError("test_widechar",
1969                               "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail");
1970 
1971     wide = PyUnicode_FromUnicode(NULL, 1);
1972     if (wide == NULL)
1973         return NULL;
1974     PyUnicode_AS_UNICODE(wide)[0] = invalid[0];
1975     if (_PyUnicode_Ready(wide) < 0) {
1976         Py_DECREF(wide);
1977         PyErr_Clear();
1978     }
1979     else {
1980         Py_DECREF(wide);
1981         return raiseTestError("test_widechar",
1982                               "PyUnicode_Ready() didn't fail");
1983     }
1984 _Py_COMP_DIAG_POP
1985 #endif /* USE_UNICODE_WCHAR_CACHE */
1986 #endif
1987 
1988     Py_RETURN_NONE;
1989 }
1990 
1991 static PyObject *
unicode_aswidechar(PyObject * self,PyObject * args)1992 unicode_aswidechar(PyObject *self, PyObject *args)
1993 {
1994     PyObject *unicode, *result;
1995     Py_ssize_t buflen, size;
1996     wchar_t *buffer;
1997 
1998     if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen))
1999         return NULL;
2000     buffer = PyMem_New(wchar_t, buflen);
2001     if (buffer == NULL)
2002         return PyErr_NoMemory();
2003 
2004     size = PyUnicode_AsWideChar(unicode, buffer, buflen);
2005     if (size == -1) {
2006         PyMem_Free(buffer);
2007         return NULL;
2008     }
2009 
2010     if (size < buflen)
2011         buflen = size + 1;
2012     else
2013         buflen = size;
2014     result = PyUnicode_FromWideChar(buffer, buflen);
2015     PyMem_Free(buffer);
2016     if (result == NULL)
2017         return NULL;
2018 
2019     return Py_BuildValue("(Nn)", result, size);
2020 }
2021 
2022 static PyObject *
unicode_aswidecharstring(PyObject * self,PyObject * args)2023 unicode_aswidecharstring(PyObject *self, PyObject *args)
2024 {
2025     PyObject *unicode, *result;
2026     Py_ssize_t size;
2027     wchar_t *buffer;
2028 
2029     if (!PyArg_ParseTuple(args, "U", &unicode))
2030         return NULL;
2031 
2032     buffer = PyUnicode_AsWideCharString(unicode, &size);
2033     if (buffer == NULL)
2034         return NULL;
2035 
2036     result = PyUnicode_FromWideChar(buffer, size + 1);
2037     PyMem_Free(buffer);
2038     if (result == NULL)
2039         return NULL;
2040     return Py_BuildValue("(Nn)", result, size);
2041 }
2042 
2043 static PyObject *
unicode_asucs4(PyObject * self,PyObject * args)2044 unicode_asucs4(PyObject *self, PyObject *args)
2045 {
2046     PyObject *unicode, *result;
2047     Py_UCS4 *buffer;
2048     int copy_null;
2049     Py_ssize_t str_len, buf_len;
2050 
2051     if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, &copy_null)) {
2052         return NULL;
2053     }
2054 
2055     buf_len = str_len + 1;
2056     buffer = PyMem_NEW(Py_UCS4, buf_len);
2057     if (buffer == NULL) {
2058         return PyErr_NoMemory();
2059     }
2060     memset(buffer, 0, sizeof(Py_UCS4)*buf_len);
2061     buffer[str_len] = 0xffffU;
2062 
2063     if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) {
2064         PyMem_Free(buffer);
2065         return NULL;
2066     }
2067 
2068     result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len);
2069     PyMem_Free(buffer);
2070     return result;
2071 }
2072 
2073 static PyObject *
unicode_asutf8(PyObject * self,PyObject * args)2074 unicode_asutf8(PyObject *self, PyObject *args)
2075 {
2076     PyObject *unicode;
2077     const char *buffer;
2078 
2079     if (!PyArg_ParseTuple(args, "U", &unicode)) {
2080         return NULL;
2081     }
2082 
2083     buffer = PyUnicode_AsUTF8(unicode);
2084     if (buffer == NULL) {
2085         return NULL;
2086     }
2087 
2088     return PyBytes_FromString(buffer);
2089 }
2090 
2091 static PyObject *
unicode_asutf8andsize(PyObject * self,PyObject * args)2092 unicode_asutf8andsize(PyObject *self, PyObject *args)
2093 {
2094     PyObject *unicode, *result;
2095     const char *buffer;
2096     Py_ssize_t utf8_len;
2097 
2098     if(!PyArg_ParseTuple(args, "U", &unicode)) {
2099         return NULL;
2100     }
2101 
2102     buffer = PyUnicode_AsUTF8AndSize(unicode, &utf8_len);
2103     if (buffer == NULL) {
2104         return NULL;
2105     }
2106 
2107     result = PyBytes_FromString(buffer);
2108     if (result == NULL) {
2109         return NULL;
2110     }
2111 
2112     return Py_BuildValue("(Nn)", result, utf8_len);
2113 }
2114 
2115 static PyObject *
unicode_findchar(PyObject * self,PyObject * args)2116 unicode_findchar(PyObject *self, PyObject *args)
2117 {
2118     PyObject *str;
2119     int direction;
2120     unsigned int ch;
2121     Py_ssize_t result;
2122     Py_ssize_t start, end;
2123 
2124     if (!PyArg_ParseTuple(args, "UInni:unicode_findchar", &str, &ch,
2125                           &start, &end, &direction)) {
2126         return NULL;
2127     }
2128 
2129     result = PyUnicode_FindChar(str, (Py_UCS4)ch, start, end, direction);
2130     if (result == -2)
2131         return NULL;
2132     else
2133         return PyLong_FromSsize_t(result);
2134 }
2135 
2136 static PyObject *
unicode_copycharacters(PyObject * self,PyObject * args)2137 unicode_copycharacters(PyObject *self, PyObject *args)
2138 {
2139     PyObject *from, *to, *to_copy;
2140     Py_ssize_t from_start, to_start, how_many, copied;
2141 
2142     if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start,
2143                           &from, &from_start, &how_many)) {
2144         return NULL;
2145     }
2146 
2147     if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to),
2148                                   PyUnicode_MAX_CHAR_VALUE(to)))) {
2149         return NULL;
2150     }
2151     if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) {
2152         Py_DECREF(to_copy);
2153         return NULL;
2154     }
2155 
2156     if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from,
2157                                            from_start, how_many)) < 0) {
2158         Py_DECREF(to_copy);
2159         return NULL;
2160     }
2161 
2162     return Py_BuildValue("(Nn)", to_copy, copied);
2163 }
2164 
2165 #if USE_UNICODE_WCHAR_CACHE
2166 /* Ignore use of deprecated APIs */
2167 _Py_COMP_DIAG_PUSH
2168 _Py_COMP_DIAG_IGNORE_DEPR_DECLS
2169 
2170 static PyObject *
unicode_encodedecimal(PyObject * self,PyObject * args)2171 unicode_encodedecimal(PyObject *self, PyObject *args)
2172 {
2173     Py_UNICODE *unicode;
2174     Py_ssize_t length;
2175     char *errors = NULL;
2176     PyObject *decimal;
2177     Py_ssize_t decimal_length, new_length;
2178     int res;
2179 
2180     if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
2181         return NULL;
2182 
2183     decimal_length = length * 7; /* len('&#8364;') */
2184     decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
2185     if (decimal == NULL)
2186         return NULL;
2187 
2188     res = PyUnicode_EncodeDecimal(unicode, length,
2189                                   PyBytes_AS_STRING(decimal),
2190                                   errors);
2191     if (res < 0) {
2192         Py_DECREF(decimal);
2193         return NULL;
2194     }
2195 
2196     new_length = strlen(PyBytes_AS_STRING(decimal));
2197     assert(new_length <= decimal_length);
2198     res = _PyBytes_Resize(&decimal, new_length);
2199     if (res < 0)
2200         return NULL;
2201 
2202     return decimal;
2203 }
2204 
2205 static PyObject *
unicode_transformdecimaltoascii(PyObject * self,PyObject * args)2206 unicode_transformdecimaltoascii(PyObject *self, PyObject *args)
2207 {
2208     Py_UNICODE *unicode;
2209     Py_ssize_t length;
2210     if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length))
2211         return NULL;
2212     return PyUnicode_TransformDecimalToASCII(unicode, length);
2213 }
2214 
2215 static PyObject *
unicode_legacy_string(PyObject * self,PyObject * args)2216 unicode_legacy_string(PyObject *self, PyObject *args)
2217 {
2218     Py_UNICODE *data;
2219     Py_ssize_t len;
2220     PyObject *u;
2221 
2222     if (!PyArg_ParseTuple(args, "u#", &data, &len))
2223         return NULL;
2224 
2225     u = PyUnicode_FromUnicode(NULL, len);
2226     if (u == NULL)
2227         return NULL;
2228 
2229     memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE));
2230 
2231     if (len > 0) { /* The empty string is always ready. */
2232         assert(!PyUnicode_IS_READY(u));
2233     }
2234 
2235     return u;
2236 }
2237 _Py_COMP_DIAG_POP
2238 #endif /* USE_UNICODE_WCHAR_CACHE */
2239 
2240 static PyObject *
getargs_w_star(PyObject * self,PyObject * args)2241 getargs_w_star(PyObject *self, PyObject *args)
2242 {
2243     Py_buffer buffer;
2244     PyObject *result;
2245     char *str;
2246 
2247     if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))
2248         return NULL;
2249 
2250     if (2 <= buffer.len) {
2251         str = buffer.buf;
2252         str[0] = '[';
2253         str[buffer.len-1] = ']';
2254     }
2255 
2256     result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
2257     PyBuffer_Release(&buffer);
2258     return result;
2259 }
2260 
2261 
2262 static PyObject *
test_empty_argparse(PyObject * self,PyObject * Py_UNUSED (ignored))2263 test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored))
2264 {
2265     /* Test that formats can begin with '|'. See issue #4720. */
2266     PyObject *tuple, *dict = NULL;
2267     static char *kwlist[] = {NULL};
2268     int result;
2269     tuple = PyTuple_New(0);
2270     if (!tuple)
2271         return NULL;
2272     if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) {
2273         goto done;
2274     }
2275     dict = PyDict_New();
2276     if (!dict)
2277         goto done;
2278     result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
2279   done:
2280     Py_DECREF(tuple);
2281     Py_XDECREF(dict);
2282     if (!result) {
2283         return NULL;
2284     }
2285     else {
2286         Py_RETURN_NONE;
2287     }
2288 }
2289 
2290 static PyObject *
codec_incrementalencoder(PyObject * self,PyObject * args)2291 codec_incrementalencoder(PyObject *self, PyObject *args)
2292 {
2293     const char *encoding, *errors = NULL;
2294     if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
2295                           &encoding, &errors))
2296         return NULL;
2297     return PyCodec_IncrementalEncoder(encoding, errors);
2298 }
2299 
2300 static PyObject *
codec_incrementaldecoder(PyObject * self,PyObject * args)2301 codec_incrementaldecoder(PyObject *self, PyObject *args)
2302 {
2303     const char *encoding, *errors = NULL;
2304     if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
2305                           &encoding, &errors))
2306         return NULL;
2307     return PyCodec_IncrementalDecoder(encoding, errors);
2308 }
2309 
2310 
2311 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
2312 static PyObject *
test_long_numbits(PyObject * self,PyObject * Py_UNUSED (ignored))2313 test_long_numbits(PyObject *self, PyObject *Py_UNUSED(ignored))
2314 {
2315     struct triple {
2316         long input;
2317         size_t nbits;
2318         int sign;
2319     } testcases[] = {{0, 0, 0},
2320                      {1L, 1, 1},
2321                      {-1L, 1, -1},
2322                      {2L, 2, 1},
2323                      {-2L, 2, -1},
2324                      {3L, 2, 1},
2325                      {-3L, 2, -1},
2326                      {4L, 3, 1},
2327                      {-4L, 3, -1},
2328                      {0x7fffL, 15, 1},          /* one Python int digit */
2329              {-0x7fffL, 15, -1},
2330              {0xffffL, 16, 1},
2331              {-0xffffL, 16, -1},
2332              {0xfffffffL, 28, 1},
2333              {-0xfffffffL, 28, -1}};
2334     size_t i;
2335 
2336     for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
2337         size_t nbits;
2338         int sign;
2339         PyObject *plong;
2340 
2341         plong = PyLong_FromLong(testcases[i].input);
2342         if (plong == NULL)
2343             return NULL;
2344         nbits = _PyLong_NumBits(plong);
2345         sign = _PyLong_Sign(plong);
2346 
2347         Py_DECREF(plong);
2348         if (nbits != testcases[i].nbits)
2349             return raiseTestError("test_long_numbits",
2350                             "wrong result for _PyLong_NumBits");
2351         if (sign != testcases[i].sign)
2352             return raiseTestError("test_long_numbits",
2353                             "wrong result for _PyLong_Sign");
2354     }
2355     Py_RETURN_NONE;
2356 }
2357 
2358 static PyObject *
pyobject_repr_from_null(PyObject * self,PyObject * Py_UNUSED (ignored))2359 pyobject_repr_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
2360 {
2361     return PyObject_Repr(NULL);
2362 }
2363 
2364 static PyObject *
pyobject_str_from_null(PyObject * self,PyObject * Py_UNUSED (ignored))2365 pyobject_str_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
2366 {
2367     return PyObject_Str(NULL);
2368 }
2369 
2370 static PyObject *
pyobject_bytes_from_null(PyObject * self,PyObject * Py_UNUSED (ignored))2371 pyobject_bytes_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
2372 {
2373     return PyObject_Bytes(NULL);
2374 }
2375 
2376 static PyObject *
raise_exception(PyObject * self,PyObject * args)2377 raise_exception(PyObject *self, PyObject *args)
2378 {
2379     PyObject *exc;
2380     PyObject *exc_args, *v;
2381     int num_args, i;
2382 
2383     if (!PyArg_ParseTuple(args, "Oi:raise_exception",
2384                           &exc, &num_args))
2385         return NULL;
2386 
2387     exc_args = PyTuple_New(num_args);
2388     if (exc_args == NULL)
2389         return NULL;
2390     for (i = 0; i < num_args; ++i) {
2391         v = PyLong_FromLong(i);
2392         if (v == NULL) {
2393             Py_DECREF(exc_args);
2394             return NULL;
2395         }
2396         PyTuple_SET_ITEM(exc_args, i, v);
2397     }
2398     PyErr_SetObject(exc, exc_args);
2399     Py_DECREF(exc_args);
2400     return NULL;
2401 }
2402 
2403 static PyObject *
set_errno(PyObject * self,PyObject * args)2404 set_errno(PyObject *self, PyObject *args)
2405 {
2406     int new_errno;
2407 
2408     if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno))
2409         return NULL;
2410 
2411     errno = new_errno;
2412     Py_RETURN_NONE;
2413 }
2414 
2415 static PyObject *
test_set_exc_info(PyObject * self,PyObject * args)2416 test_set_exc_info(PyObject *self, PyObject *args)
2417 {
2418     PyObject *orig_exc;
2419     PyObject *new_type, *new_value, *new_tb;
2420     PyObject *type, *value, *tb;
2421     if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info",
2422                           &new_type, &new_value, &new_tb))
2423         return NULL;
2424 
2425     PyErr_GetExcInfo(&type, &value, &tb);
2426 
2427     Py_INCREF(new_type);
2428     Py_INCREF(new_value);
2429     Py_INCREF(new_tb);
2430     PyErr_SetExcInfo(new_type, new_value, new_tb);
2431 
2432     orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None);
2433     Py_XDECREF(type);
2434     Py_XDECREF(value);
2435     Py_XDECREF(tb);
2436     return orig_exc;
2437 }
2438 
2439 static int test_run_counter = 0;
2440 
2441 static PyObject *
test_datetime_capi(PyObject * self,PyObject * args)2442 test_datetime_capi(PyObject *self, PyObject *args) {
2443     if (PyDateTimeAPI) {
2444         if (test_run_counter) {
2445             /* Probably regrtest.py -R */
2446             Py_RETURN_NONE;
2447         }
2448         else {
2449             PyErr_SetString(PyExc_AssertionError,
2450                             "PyDateTime_CAPI somehow initialized");
2451             return NULL;
2452         }
2453     }
2454     test_run_counter++;
2455     PyDateTime_IMPORT;
2456 
2457     if (PyDateTimeAPI)
2458         Py_RETURN_NONE;
2459     else
2460         return NULL;
2461 }
2462 
2463 /* Functions exposing the C API type checking for testing */
2464 #define MAKE_DATETIME_CHECK_FUNC(check_method, exact_method)    \
2465     PyObject *obj;                                              \
2466     int exact = 0;                                              \
2467     if (!PyArg_ParseTuple(args, "O|p", &obj, &exact)) {         \
2468         return NULL;                                            \
2469     }                                                           \
2470     int rv = exact?exact_method(obj):check_method(obj);         \
2471     if (rv) {                                                   \
2472         Py_RETURN_TRUE;                                         \
2473     } else {                                                    \
2474         Py_RETURN_FALSE;                                        \
2475     }
2476 
2477 static PyObject *
datetime_check_date(PyObject * self,PyObject * args)2478 datetime_check_date(PyObject *self, PyObject *args) {
2479     MAKE_DATETIME_CHECK_FUNC(PyDate_Check, PyDate_CheckExact)
2480 }
2481 
2482 static PyObject *
datetime_check_time(PyObject * self,PyObject * args)2483 datetime_check_time(PyObject *self, PyObject *args) {
2484     MAKE_DATETIME_CHECK_FUNC(PyTime_Check, PyTime_CheckExact)
2485 }
2486 
2487 static PyObject *
datetime_check_datetime(PyObject * self,PyObject * args)2488 datetime_check_datetime(PyObject *self, PyObject *args) {
2489     MAKE_DATETIME_CHECK_FUNC(PyDateTime_Check, PyDateTime_CheckExact)
2490 }
2491 
2492 static PyObject *
datetime_check_delta(PyObject * self,PyObject * args)2493 datetime_check_delta(PyObject *self, PyObject *args) {
2494     MAKE_DATETIME_CHECK_FUNC(PyDelta_Check, PyDelta_CheckExact)
2495 }
2496 
2497 static PyObject *
datetime_check_tzinfo(PyObject * self,PyObject * args)2498 datetime_check_tzinfo(PyObject *self, PyObject *args) {
2499     MAKE_DATETIME_CHECK_FUNC(PyTZInfo_Check, PyTZInfo_CheckExact)
2500 }
2501 
2502 
2503 /* Makes three variations on timezone representing UTC-5:
2504    1. timezone with offset and name from PyDateTimeAPI
2505    2. timezone with offset and name from PyTimeZone_FromOffsetAndName
2506    3. timezone with offset (no name) from PyTimeZone_FromOffset
2507 */
2508 static PyObject *
make_timezones_capi(PyObject * self,PyObject * args)2509 make_timezones_capi(PyObject *self, PyObject *args) {
2510     PyObject *offset = PyDelta_FromDSU(0, -18000, 0);
2511     PyObject *name = PyUnicode_FromString("EST");
2512 
2513     PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name);
2514     PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name);
2515     PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset);
2516 
2517     Py_DecRef(offset);
2518     Py_DecRef(name);
2519 
2520     PyObject *rv = PyTuple_New(3);
2521 
2522     PyTuple_SET_ITEM(rv, 0, est_zone_capi);
2523     PyTuple_SET_ITEM(rv, 1, est_zone_macro);
2524     PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname);
2525 
2526     return rv;
2527 }
2528 
2529 static PyObject *
get_timezones_offset_zero(PyObject * self,PyObject * args)2530 get_timezones_offset_zero(PyObject *self, PyObject *args) {
2531     PyObject *offset = PyDelta_FromDSU(0, 0, 0);
2532     PyObject *name = PyUnicode_FromString("");
2533 
2534     // These two should return the UTC singleton
2535     PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
2536     PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL);
2537 
2538     // This one will return +00:00 zone, but not the UTC singleton
2539     PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name);
2540 
2541     Py_DecRef(offset);
2542     Py_DecRef(name);
2543 
2544     PyObject *rv = PyTuple_New(3);
2545     PyTuple_SET_ITEM(rv, 0, utc_singleton_0);
2546     PyTuple_SET_ITEM(rv, 1, utc_singleton_1);
2547     PyTuple_SET_ITEM(rv, 2, non_utc_zone);
2548 
2549     return rv;
2550 }
2551 
2552 static PyObject *
get_timezone_utc_capi(PyObject * self,PyObject * args)2553 get_timezone_utc_capi(PyObject* self, PyObject *args) {
2554     int macro = 0;
2555     if (!PyArg_ParseTuple(args, "|p", &macro)) {
2556         return NULL;
2557     }
2558     if (macro) {
2559         Py_INCREF(PyDateTime_TimeZone_UTC);
2560         return PyDateTime_TimeZone_UTC;
2561     } else {
2562         Py_INCREF(PyDateTimeAPI->TimeZone_UTC);
2563         return PyDateTimeAPI->TimeZone_UTC;
2564     }
2565 }
2566 
2567 static PyObject *
get_date_fromdate(PyObject * self,PyObject * args)2568 get_date_fromdate(PyObject *self, PyObject *args)
2569 {
2570     PyObject *rv = NULL;
2571     int macro;
2572     int year, month, day;
2573 
2574     if (!PyArg_ParseTuple(args, "piii", &macro, &year, &month, &day)) {
2575         return NULL;
2576     }
2577 
2578     if (macro) {
2579         rv = PyDate_FromDate(year, month, day);
2580     }
2581     else {
2582         rv = PyDateTimeAPI->Date_FromDate(
2583             year, month, day,
2584             PyDateTimeAPI->DateType);
2585     }
2586     return rv;
2587 }
2588 
2589 static PyObject *
get_datetime_fromdateandtime(PyObject * self,PyObject * args)2590 get_datetime_fromdateandtime(PyObject *self, PyObject *args)
2591 {
2592     PyObject *rv = NULL;
2593     int macro;
2594     int year, month, day;
2595     int hour, minute, second, microsecond;
2596 
2597     if (!PyArg_ParseTuple(args, "piiiiiii",
2598                           &macro,
2599                           &year, &month, &day,
2600                           &hour, &minute, &second, &microsecond)) {
2601         return NULL;
2602     }
2603 
2604     if (macro) {
2605         rv = PyDateTime_FromDateAndTime(
2606             year, month, day,
2607             hour, minute, second, microsecond);
2608     }
2609     else {
2610         rv = PyDateTimeAPI->DateTime_FromDateAndTime(
2611             year, month, day,
2612             hour, minute, second, microsecond,
2613             Py_None,
2614             PyDateTimeAPI->DateTimeType);
2615     }
2616     return rv;
2617 }
2618 
2619 static PyObject *
get_datetime_fromdateandtimeandfold(PyObject * self,PyObject * args)2620 get_datetime_fromdateandtimeandfold(PyObject *self, PyObject *args)
2621 {
2622     PyObject *rv = NULL;
2623     int macro;
2624     int year, month, day;
2625     int hour, minute, second, microsecond, fold;
2626 
2627     if (!PyArg_ParseTuple(args, "piiiiiiii",
2628                           &macro,
2629                           &year, &month, &day,
2630                           &hour, &minute, &second, &microsecond,
2631                           &fold)) {
2632         return NULL;
2633     }
2634 
2635     if (macro) {
2636         rv = PyDateTime_FromDateAndTimeAndFold(
2637             year, month, day,
2638             hour, minute, second, microsecond,
2639             fold);
2640     }
2641     else {
2642         rv = PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(
2643             year, month, day,
2644             hour, minute, second, microsecond,
2645             Py_None,
2646             fold,
2647             PyDateTimeAPI->DateTimeType);
2648     }
2649     return rv;
2650 }
2651 
2652 static PyObject *
get_time_fromtime(PyObject * self,PyObject * args)2653 get_time_fromtime(PyObject *self, PyObject *args)
2654 {
2655     PyObject *rv = NULL;
2656     int macro;
2657     int hour, minute, second, microsecond;
2658 
2659     if (!PyArg_ParseTuple(args, "piiii",
2660                           &macro,
2661                           &hour, &minute, &second, &microsecond)) {
2662         return NULL;
2663     }
2664 
2665     if (macro) {
2666         rv = PyTime_FromTime(hour, minute, second, microsecond);
2667     }
2668     else {
2669         rv = PyDateTimeAPI->Time_FromTime(
2670             hour, minute, second, microsecond,
2671             Py_None,
2672             PyDateTimeAPI->TimeType);
2673     }
2674     return rv;
2675 }
2676 
2677 static PyObject *
get_time_fromtimeandfold(PyObject * self,PyObject * args)2678 get_time_fromtimeandfold(PyObject *self, PyObject *args)
2679 {
2680     PyObject *rv = NULL;
2681     int macro;
2682     int hour, minute, second, microsecond, fold;
2683 
2684     if (!PyArg_ParseTuple(args, "piiiii",
2685                           &macro,
2686                           &hour, &minute, &second, &microsecond,
2687                           &fold)) {
2688         return NULL;
2689     }
2690 
2691     if (macro) {
2692         rv = PyTime_FromTimeAndFold(hour, minute, second, microsecond, fold);
2693     }
2694     else {
2695         rv = PyDateTimeAPI->Time_FromTimeAndFold(
2696             hour, minute, second, microsecond,
2697             Py_None,
2698             fold,
2699             PyDateTimeAPI->TimeType);
2700     }
2701     return rv;
2702 }
2703 
2704 static PyObject *
get_delta_fromdsu(PyObject * self,PyObject * args)2705 get_delta_fromdsu(PyObject *self, PyObject *args)
2706 {
2707     PyObject *rv = NULL;
2708     int macro;
2709     int days, seconds, microseconds;
2710 
2711     if (!PyArg_ParseTuple(args, "piii",
2712                           &macro,
2713                           &days, &seconds, &microseconds)) {
2714         return NULL;
2715     }
2716 
2717     if (macro) {
2718         rv = PyDelta_FromDSU(days, seconds, microseconds);
2719     }
2720     else {
2721         rv = PyDateTimeAPI->Delta_FromDelta(
2722             days, seconds, microseconds, 1,
2723             PyDateTimeAPI->DeltaType);
2724     }
2725 
2726     return rv;
2727 }
2728 
2729 static PyObject *
get_date_fromtimestamp(PyObject * self,PyObject * args)2730 get_date_fromtimestamp(PyObject* self, PyObject *args)
2731 {
2732     PyObject *tsargs = NULL, *ts = NULL, *rv = NULL;
2733     int macro = 0;
2734 
2735     if (!PyArg_ParseTuple(args, "O|p", &ts, &macro)) {
2736         return NULL;
2737     }
2738 
2739     // Construct the argument tuple
2740     if ((tsargs = PyTuple_Pack(1, ts)) == NULL) {
2741         return NULL;
2742     }
2743 
2744     // Pass along to the API function
2745     if (macro) {
2746         rv = PyDate_FromTimestamp(tsargs);
2747     }
2748     else {
2749         rv = PyDateTimeAPI->Date_FromTimestamp(
2750                 (PyObject *)PyDateTimeAPI->DateType, tsargs
2751         );
2752     }
2753 
2754     Py_DECREF(tsargs);
2755     return rv;
2756 }
2757 
2758 static PyObject *
get_datetime_fromtimestamp(PyObject * self,PyObject * args)2759 get_datetime_fromtimestamp(PyObject* self, PyObject *args)
2760 {
2761     int macro = 0;
2762     int usetz = 0;
2763     PyObject *tsargs = NULL, *ts = NULL, *tzinfo = Py_None, *rv = NULL;
2764     if (!PyArg_ParseTuple(args, "OO|pp", &ts, &tzinfo, &usetz, &macro)) {
2765         return NULL;
2766     }
2767 
2768     // Construct the argument tuple
2769     if (usetz) {
2770         tsargs = PyTuple_Pack(2, ts, tzinfo);
2771     }
2772     else {
2773         tsargs = PyTuple_Pack(1, ts);
2774     }
2775 
2776     if (tsargs == NULL) {
2777         return NULL;
2778     }
2779 
2780     // Pass along to the API function
2781     if (macro) {
2782         rv = PyDateTime_FromTimestamp(tsargs);
2783     }
2784     else {
2785         rv = PyDateTimeAPI->DateTime_FromTimestamp(
2786                 (PyObject *)PyDateTimeAPI->DateTimeType, tsargs, NULL
2787         );
2788     }
2789 
2790     Py_DECREF(tsargs);
2791     return rv;
2792 }
2793 
2794 static PyObject *
test_PyDateTime_GET(PyObject * self,PyObject * obj)2795 test_PyDateTime_GET(PyObject *self, PyObject *obj)
2796 {
2797     int year, month, day;
2798 
2799     year = PyDateTime_GET_YEAR(obj);
2800     month = PyDateTime_GET_MONTH(obj);
2801     day = PyDateTime_GET_DAY(obj);
2802 
2803     return Py_BuildValue("(lll)", year, month, day);
2804 }
2805 
2806 static PyObject *
test_PyDateTime_DATE_GET(PyObject * self,PyObject * obj)2807 test_PyDateTime_DATE_GET(PyObject *self, PyObject *obj)
2808 {
2809     int hour, minute, second, microsecond;
2810 
2811     hour = PyDateTime_DATE_GET_HOUR(obj);
2812     minute = PyDateTime_DATE_GET_MINUTE(obj);
2813     second = PyDateTime_DATE_GET_SECOND(obj);
2814     microsecond = PyDateTime_DATE_GET_MICROSECOND(obj);
2815     PyObject *tzinfo = PyDateTime_DATE_GET_TZINFO(obj);
2816 
2817     return Py_BuildValue("(llllO)", hour, minute, second, microsecond, tzinfo);
2818 }
2819 
2820 static PyObject *
test_PyDateTime_TIME_GET(PyObject * self,PyObject * obj)2821 test_PyDateTime_TIME_GET(PyObject *self, PyObject *obj)
2822 {
2823     int hour, minute, second, microsecond;
2824 
2825     hour = PyDateTime_TIME_GET_HOUR(obj);
2826     minute = PyDateTime_TIME_GET_MINUTE(obj);
2827     second = PyDateTime_TIME_GET_SECOND(obj);
2828     microsecond = PyDateTime_TIME_GET_MICROSECOND(obj);
2829     PyObject *tzinfo = PyDateTime_TIME_GET_TZINFO(obj);
2830 
2831     return Py_BuildValue("(llllO)", hour, minute, second, microsecond, tzinfo);
2832 }
2833 
2834 static PyObject *
test_PyDateTime_DELTA_GET(PyObject * self,PyObject * obj)2835 test_PyDateTime_DELTA_GET(PyObject *self, PyObject *obj)
2836 {
2837     int days, seconds, microseconds;
2838 
2839     days = PyDateTime_DELTA_GET_DAYS(obj);
2840     seconds = PyDateTime_DELTA_GET_SECONDS(obj);
2841     microseconds = PyDateTime_DELTA_GET_MICROSECONDS(obj);
2842 
2843     return Py_BuildValue("(lll)", days, seconds, microseconds);
2844 }
2845 
2846 /* test_thread_state spawns a thread of its own, and that thread releases
2847  * `thread_done` when it's finished.  The driver code has to know when the
2848  * thread finishes, because the thread uses a PyObject (the callable) that
2849  * may go away when the driver finishes.  The former lack of this explicit
2850  * synchronization caused rare segfaults, so rare that they were seen only
2851  * on a Mac buildbot (although they were possible on any box).
2852  */
2853 static PyThread_type_lock thread_done = NULL;
2854 
2855 static int
_make_call(void * callable)2856 _make_call(void *callable)
2857 {
2858     PyObject *rc;
2859     int success;
2860     PyGILState_STATE s = PyGILState_Ensure();
2861     rc = _PyObject_CallNoArg((PyObject *)callable);
2862     success = (rc != NULL);
2863     Py_XDECREF(rc);
2864     PyGILState_Release(s);
2865     return success;
2866 }
2867 
2868 /* Same thing, but releases `thread_done` when it returns.  This variant
2869  * should be called only from threads spawned by test_thread_state().
2870  */
2871 static void
_make_call_from_thread(void * callable)2872 _make_call_from_thread(void *callable)
2873 {
2874     _make_call(callable);
2875     PyThread_release_lock(thread_done);
2876 }
2877 
2878 static PyObject *
test_thread_state(PyObject * self,PyObject * args)2879 test_thread_state(PyObject *self, PyObject *args)
2880 {
2881     PyObject *fn;
2882     int success = 1;
2883 
2884     if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
2885         return NULL;
2886 
2887     if (!PyCallable_Check(fn)) {
2888         PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
2889             Py_TYPE(fn)->tp_name);
2890         return NULL;
2891     }
2892 
2893     thread_done = PyThread_allocate_lock();
2894     if (thread_done == NULL)
2895         return PyErr_NoMemory();
2896     PyThread_acquire_lock(thread_done, 1);
2897 
2898     /* Start a new thread with our callback. */
2899     PyThread_start_new_thread(_make_call_from_thread, fn);
2900     /* Make the callback with the thread lock held by this thread */
2901     success &= _make_call(fn);
2902     /* Do it all again, but this time with the thread-lock released */
2903     Py_BEGIN_ALLOW_THREADS
2904     success &= _make_call(fn);
2905     PyThread_acquire_lock(thread_done, 1);  /* wait for thread to finish */
2906     Py_END_ALLOW_THREADS
2907 
2908     /* And once more with and without a thread
2909        XXX - should use a lock and work out exactly what we are trying
2910        to test <wink>
2911     */
2912     Py_BEGIN_ALLOW_THREADS
2913     PyThread_start_new_thread(_make_call_from_thread, fn);
2914     success &= _make_call(fn);
2915     PyThread_acquire_lock(thread_done, 1);  /* wait for thread to finish */
2916     Py_END_ALLOW_THREADS
2917 
2918     /* Release lock we acquired above.  This is required on HP-UX. */
2919     PyThread_release_lock(thread_done);
2920 
2921     PyThread_free_lock(thread_done);
2922     if (!success)
2923         return NULL;
2924     Py_RETURN_NONE;
2925 }
2926 
2927 /* test Py_AddPendingCalls using threads */
_pending_callback(void * arg)2928 static int _pending_callback(void *arg)
2929 {
2930     /* we assume the argument is callable object to which we own a reference */
2931     PyObject *callable = (PyObject *)arg;
2932     PyObject *r = _PyObject_CallNoArg(callable);
2933     Py_DECREF(callable);
2934     Py_XDECREF(r);
2935     return r != NULL ? 0 : -1;
2936 }
2937 
2938 /* The following requests n callbacks to _pending_callback.  It can be
2939  * run from any python thread.
2940  */
2941 static PyObject *
pending_threadfunc(PyObject * self,PyObject * arg)2942 pending_threadfunc(PyObject *self, PyObject *arg)
2943 {
2944     PyObject *callable;
2945     int r;
2946     if (PyArg_ParseTuple(arg, "O", &callable) == 0)
2947         return NULL;
2948 
2949     /* create the reference for the callbackwhile we hold the lock */
2950     Py_INCREF(callable);
2951 
2952     Py_BEGIN_ALLOW_THREADS
2953     r = Py_AddPendingCall(&_pending_callback, callable);
2954     Py_END_ALLOW_THREADS
2955 
2956     if (r<0) {
2957         Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
2958         Py_RETURN_FALSE;
2959     }
2960     Py_RETURN_TRUE;
2961 }
2962 
2963 /* Some tests of PyUnicode_FromFormat().  This needs more tests. */
2964 static PyObject *
test_string_from_format(PyObject * self,PyObject * Py_UNUSED (ignored))2965 test_string_from_format(PyObject *self, PyObject *Py_UNUSED(ignored))
2966 {
2967     PyObject *result;
2968     char *msg;
2969 
2970 #define CHECK_1_FORMAT(FORMAT, TYPE)                                \
2971     result = PyUnicode_FromFormat(FORMAT, (TYPE)1);                 \
2972     if (result == NULL)                                             \
2973         return NULL;                                                \
2974     if (!_PyUnicode_EqualToASCIIString(result, "1")) {              \
2975         msg = FORMAT " failed at 1";                                \
2976         goto Fail;                                                  \
2977     }                                                               \
2978     Py_DECREF(result)
2979 
2980     CHECK_1_FORMAT("%d", int);
2981     CHECK_1_FORMAT("%ld", long);
2982     /* The z width modifier was added in Python 2.5. */
2983     CHECK_1_FORMAT("%zd", Py_ssize_t);
2984 
2985     /* The u type code was added in Python 2.5. */
2986     CHECK_1_FORMAT("%u", unsigned int);
2987     CHECK_1_FORMAT("%lu", unsigned long);
2988     CHECK_1_FORMAT("%zu", size_t);
2989 
2990     /* "%lld" and "%llu" support added in Python 2.7. */
2991     CHECK_1_FORMAT("%llu", unsigned long long);
2992     CHECK_1_FORMAT("%lld", long long);
2993 
2994     Py_RETURN_NONE;
2995 
2996  Fail:
2997     Py_XDECREF(result);
2998     return raiseTestError("test_string_from_format", msg);
2999 
3000 #undef CHECK_1_FORMAT
3001 }
3002 
3003 
3004 static PyObject *
test_unicode_compare_with_ascii(PyObject * self,PyObject * Py_UNUSED (ignored))3005 test_unicode_compare_with_ascii(PyObject *self, PyObject *Py_UNUSED(ignored)) {
3006     PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
3007     int result;
3008     if (py_s == NULL)
3009         return NULL;
3010     result = PyUnicode_CompareWithASCIIString(py_s, "str");
3011     Py_DECREF(py_s);
3012     if (!result) {
3013         PyErr_SetString(TestError, "Python string ending in NULL "
3014                         "should not compare equal to c string.");
3015         return NULL;
3016     }
3017     Py_RETURN_NONE;
3018 }
3019 
3020 /* This is here to provide a docstring for test_descr. */
3021 static PyObject *
test_with_docstring(PyObject * self,PyObject * Py_UNUSED (ignored))3022 test_with_docstring(PyObject *self, PyObject *Py_UNUSED(ignored))
3023 {
3024     Py_RETURN_NONE;
3025 }
3026 
3027 /* Test PyOS_string_to_double. */
3028 static PyObject *
test_string_to_double(PyObject * self,PyObject * Py_UNUSED (ignored))3029 test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) {
3030     double result;
3031     const char *msg;
3032 
3033 #define CHECK_STRING(STR, expected)                             \
3034     result = PyOS_string_to_double(STR, NULL, NULL);            \
3035     if (result == -1.0 && PyErr_Occurred())                     \
3036         return NULL;                                            \
3037     if (result != (double)expected) {                           \
3038         msg = "conversion of " STR " to float failed";          \
3039         goto fail;                                              \
3040     }
3041 
3042 #define CHECK_INVALID(STR)                                              \
3043     result = PyOS_string_to_double(STR, NULL, NULL);                    \
3044     if (result == -1.0 && PyErr_Occurred()) {                           \
3045         if (PyErr_ExceptionMatches(PyExc_ValueError))                   \
3046             PyErr_Clear();                                              \
3047         else                                                            \
3048             return NULL;                                                \
3049     }                                                                   \
3050     else {                                                              \
3051         msg = "conversion of " STR " didn't raise ValueError";          \
3052         goto fail;                                                      \
3053     }
3054 
3055     CHECK_STRING("0.1", 0.1);
3056     CHECK_STRING("1.234", 1.234);
3057     CHECK_STRING("-1.35", -1.35);
3058     CHECK_STRING(".1e01", 1.0);
3059     CHECK_STRING("2.e-2", 0.02);
3060 
3061     CHECK_INVALID(" 0.1");
3062     CHECK_INVALID("\t\n-3");
3063     CHECK_INVALID(".123 ");
3064     CHECK_INVALID("3\n");
3065     CHECK_INVALID("123abc");
3066 
3067     Py_RETURN_NONE;
3068   fail:
3069     return raiseTestError("test_string_to_double", msg);
3070 #undef CHECK_STRING
3071 #undef CHECK_INVALID
3072 }
3073 
3074 
3075 /* Coverage testing of capsule objects. */
3076 
3077 static const char *capsule_name = "capsule name";
3078 static       char *capsule_pointer = "capsule pointer";
3079 static       char *capsule_context = "capsule context";
3080 static const char *capsule_error = NULL;
3081 static int
3082 capsule_destructor_call_count = 0;
3083 
3084 static void
capsule_destructor(PyObject * o)3085 capsule_destructor(PyObject *o) {
3086     capsule_destructor_call_count++;
3087     if (PyCapsule_GetContext(o) != capsule_context) {
3088         capsule_error = "context did not match in destructor!";
3089     } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
3090         capsule_error = "destructor did not match in destructor!  (woah!)";
3091     } else if (PyCapsule_GetName(o) != capsule_name) {
3092         capsule_error = "name did not match in destructor!";
3093     } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
3094         capsule_error = "pointer did not match in destructor!";
3095     }
3096 }
3097 
3098 typedef struct {
3099     char *name;
3100     char *module;
3101     char *attribute;
3102 } known_capsule;
3103 
3104 static PyObject *
test_capsule(PyObject * self,PyObject * Py_UNUSED (ignored))3105 test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored))
3106 {
3107     PyObject *object;
3108     const char *error = NULL;
3109     void *pointer;
3110     void *pointer2;
3111     known_capsule known_capsules[] = {
3112         #define KNOWN_CAPSULE(module, name)             { module "." name, module, name }
3113         KNOWN_CAPSULE("_socket", "CAPI"),
3114         KNOWN_CAPSULE("_curses", "_C_API"),
3115         KNOWN_CAPSULE("datetime", "datetime_CAPI"),
3116         { NULL, NULL },
3117     };
3118     known_capsule *known = &known_capsules[0];
3119 
3120 #define FAIL(x) { error = (x); goto exit; }
3121 
3122 #define CHECK_DESTRUCTOR \
3123     if (capsule_error) { \
3124         FAIL(capsule_error); \
3125     } \
3126     else if (!capsule_destructor_call_count) {          \
3127         FAIL("destructor not called!"); \
3128     } \
3129     capsule_destructor_call_count = 0; \
3130 
3131     object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
3132     PyCapsule_SetContext(object, capsule_context);
3133     capsule_destructor(object);
3134     CHECK_DESTRUCTOR;
3135     Py_DECREF(object);
3136     CHECK_DESTRUCTOR;
3137 
3138     object = PyCapsule_New(known, "ignored", NULL);
3139     PyCapsule_SetPointer(object, capsule_pointer);
3140     PyCapsule_SetName(object, capsule_name);
3141     PyCapsule_SetDestructor(object, capsule_destructor);
3142     PyCapsule_SetContext(object, capsule_context);
3143     capsule_destructor(object);
3144     CHECK_DESTRUCTOR;
3145     /* intentionally access using the wrong name */
3146     pointer2 = PyCapsule_GetPointer(object, "the wrong name");
3147     if (!PyErr_Occurred()) {
3148         FAIL("PyCapsule_GetPointer should have failed but did not!");
3149     }
3150     PyErr_Clear();
3151     if (pointer2) {
3152         if (pointer2 == capsule_pointer) {
3153             FAIL("PyCapsule_GetPointer should not have"
3154                      " returned the internal pointer!");
3155         } else {
3156             FAIL("PyCapsule_GetPointer should have "
3157                      "returned NULL pointer but did not!");
3158         }
3159     }
3160     PyCapsule_SetDestructor(object, NULL);
3161     Py_DECREF(object);
3162     if (capsule_destructor_call_count) {
3163         FAIL("destructor called when it should not have been!");
3164     }
3165 
3166     for (known = &known_capsules[0]; known->module != NULL; known++) {
3167         /* yeah, ordinarily I wouldn't do this either,
3168            but it's fine for this test harness.
3169         */
3170         static char buffer[256];
3171 #undef FAIL
3172 #define FAIL(x) \
3173         { \
3174         sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
3175             x, known->module, known->attribute); \
3176         error = buffer; \
3177         goto exit; \
3178         } \
3179 
3180         PyObject *module = PyImport_ImportModule(known->module);
3181         if (module) {
3182             pointer = PyCapsule_Import(known->name, 0);
3183             if (!pointer) {
3184                 Py_DECREF(module);
3185                 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
3186             }
3187             object = PyObject_GetAttrString(module, known->attribute);
3188             if (!object) {
3189                 Py_DECREF(module);
3190                 return NULL;
3191             }
3192             pointer2 = PyCapsule_GetPointer(object,
3193                                     "weebles wobble but they don't fall down");
3194             if (!PyErr_Occurred()) {
3195                 Py_DECREF(object);
3196                 Py_DECREF(module);
3197                 FAIL("PyCapsule_GetPointer should have failed but did not!");
3198             }
3199             PyErr_Clear();
3200             if (pointer2) {
3201                 Py_DECREF(module);
3202                 Py_DECREF(object);
3203                 if (pointer2 == pointer) {
3204                     FAIL("PyCapsule_GetPointer should not have"
3205                              " returned its internal pointer!");
3206                 } else {
3207                     FAIL("PyCapsule_GetPointer should have"
3208                              " returned NULL pointer but did not!");
3209                 }
3210             }
3211             Py_DECREF(object);
3212             Py_DECREF(module);
3213         }
3214         else
3215             PyErr_Clear();
3216     }
3217 
3218   exit:
3219     if (error) {
3220         return raiseTestError("test_capsule", error);
3221     }
3222     Py_RETURN_NONE;
3223 #undef FAIL
3224 }
3225 
3226 #ifdef HAVE_GETTIMEOFDAY
3227 /* Profiling of integer performance */
print_delta(int test,struct timeval * s,struct timeval * e)3228 static void print_delta(int test, struct timeval *s, struct timeval *e)
3229 {
3230     e->tv_sec -= s->tv_sec;
3231     e->tv_usec -= s->tv_usec;
3232     if (e->tv_usec < 0) {
3233         e->tv_sec -=1;
3234         e->tv_usec += 1000000;
3235     }
3236     printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
3237 }
3238 
3239 static PyObject *
profile_int(PyObject * self,PyObject * args)3240 profile_int(PyObject *self, PyObject* args)
3241 {
3242     int i, k;
3243     struct timeval start, stop;
3244     PyObject *single, **multiple, *op1, *result;
3245 
3246     /* Test 1: Allocate and immediately deallocate
3247        many small integers */
3248     gettimeofday(&start, NULL);
3249     for(k=0; k < 20000; k++)
3250         for(i=0; i < 1000; i++) {
3251             single = PyLong_FromLong(i);
3252             Py_DECREF(single);
3253         }
3254     gettimeofday(&stop, NULL);
3255     print_delta(1, &start, &stop);
3256 
3257     /* Test 2: Allocate and immediately deallocate
3258        many large integers */
3259     gettimeofday(&start, NULL);
3260     for(k=0; k < 20000; k++)
3261         for(i=0; i < 1000; i++) {
3262             single = PyLong_FromLong(i+1000000);
3263             Py_DECREF(single);
3264         }
3265     gettimeofday(&stop, NULL);
3266     print_delta(2, &start, &stop);
3267 
3268     /* Test 3: Allocate a few integers, then release
3269        them all simultaneously. */
3270     multiple = malloc(sizeof(PyObject*) * 1000);
3271     if (multiple == NULL)
3272         return PyErr_NoMemory();
3273     gettimeofday(&start, NULL);
3274     for(k=0; k < 20000; k++) {
3275         for(i=0; i < 1000; i++) {
3276             multiple[i] = PyLong_FromLong(i+1000000);
3277         }
3278         for(i=0; i < 1000; i++) {
3279             Py_DECREF(multiple[i]);
3280         }
3281     }
3282     gettimeofday(&stop, NULL);
3283     print_delta(3, &start, &stop);
3284     free(multiple);
3285 
3286     /* Test 4: Allocate many integers, then release
3287        them all simultaneously. */
3288     multiple = malloc(sizeof(PyObject*) * 1000000);
3289     if (multiple == NULL)
3290         return PyErr_NoMemory();
3291     gettimeofday(&start, NULL);
3292     for(k=0; k < 20; k++) {
3293         for(i=0; i < 1000000; i++) {
3294             multiple[i] = PyLong_FromLong(i+1000000);
3295         }
3296         for(i=0; i < 1000000; i++) {
3297             Py_DECREF(multiple[i]);
3298         }
3299     }
3300     gettimeofday(&stop, NULL);
3301     print_delta(4, &start, &stop);
3302     free(multiple);
3303 
3304     /* Test 5: Allocate many integers < 32000 */
3305     multiple = malloc(sizeof(PyObject*) * 1000000);
3306     if (multiple == NULL)
3307         return PyErr_NoMemory();
3308     gettimeofday(&start, NULL);
3309     for(k=0; k < 10; k++) {
3310         for(i=0; i < 1000000; i++) {
3311             multiple[i] = PyLong_FromLong(i+1000);
3312         }
3313         for(i=0; i < 1000000; i++) {
3314             Py_DECREF(multiple[i]);
3315         }
3316     }
3317     gettimeofday(&stop, NULL);
3318     print_delta(5, &start, &stop);
3319     free(multiple);
3320 
3321     /* Test 6: Perform small int addition */
3322     op1 = PyLong_FromLong(1);
3323     gettimeofday(&start, NULL);
3324     for(i=0; i < 10000000; i++) {
3325         result = PyNumber_Add(op1, op1);
3326         Py_DECREF(result);
3327     }
3328     gettimeofday(&stop, NULL);
3329     Py_DECREF(op1);
3330     print_delta(6, &start, &stop);
3331 
3332     /* Test 7: Perform medium int addition */
3333     op1 = PyLong_FromLong(1000);
3334     if (op1 == NULL)
3335         return NULL;
3336     gettimeofday(&start, NULL);
3337     for(i=0; i < 10000000; i++) {
3338         result = PyNumber_Add(op1, op1);
3339         Py_XDECREF(result);
3340     }
3341     gettimeofday(&stop, NULL);
3342     Py_DECREF(op1);
3343     print_delta(7, &start, &stop);
3344 
3345     Py_RETURN_NONE;
3346 }
3347 #endif
3348 
3349 /* To test the format of tracebacks as printed out. */
3350 static PyObject *
traceback_print(PyObject * self,PyObject * args)3351 traceback_print(PyObject *self, PyObject *args)
3352 {
3353     PyObject *file;
3354     PyObject *traceback;
3355     int result;
3356 
3357     if (!PyArg_ParseTuple(args, "OO:traceback_print",
3358                             &traceback, &file))
3359         return NULL;
3360 
3361     result = PyTraceBack_Print(traceback, file);
3362     if (result < 0)
3363         return NULL;
3364     Py_RETURN_NONE;
3365 }
3366 
3367 /* To test the format of exceptions as printed out. */
3368 static PyObject *
exception_print(PyObject * self,PyObject * args)3369 exception_print(PyObject *self, PyObject *args)
3370 {
3371     PyObject *value;
3372     PyObject *tb;
3373 
3374     if (!PyArg_ParseTuple(args, "O:exception_print",
3375                             &value))
3376         return NULL;
3377     if (!PyExceptionInstance_Check(value)) {
3378         PyErr_Format(PyExc_TypeError, "an exception instance is required");
3379         return NULL;
3380     }
3381 
3382     tb = PyException_GetTraceback(value);
3383     PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
3384     Py_XDECREF(tb);
3385 
3386     Py_RETURN_NONE;
3387 }
3388 
3389 
3390 
3391 
3392 /* reliably raise a MemoryError */
3393 static PyObject *
raise_memoryerror(PyObject * self,PyObject * Py_UNUSED (ignored))3394 raise_memoryerror(PyObject *self, PyObject *Py_UNUSED(ignored))
3395 {
3396     PyErr_NoMemory();
3397     return NULL;
3398 }
3399 
3400 /* Issue 6012 */
3401 static PyObject *str1, *str2;
3402 static int
failing_converter(PyObject * obj,void * arg)3403 failing_converter(PyObject *obj, void *arg)
3404 {
3405     /* Clone str1, then let the conversion fail. */
3406     assert(str1);
3407     str2 = str1;
3408     Py_INCREF(str2);
3409     return 0;
3410 }
3411 static PyObject*
argparsing(PyObject * o,PyObject * args)3412 argparsing(PyObject *o, PyObject *args)
3413 {
3414     PyObject *res;
3415     str1 = str2 = NULL;
3416     if (!PyArg_ParseTuple(args, "O&O&",
3417                           PyUnicode_FSConverter, &str1,
3418                           failing_converter, &str2)) {
3419         if (!str2)
3420             /* argument converter not called? */
3421             return NULL;
3422         /* Should be 1 */
3423         res = PyLong_FromSsize_t(Py_REFCNT(str2));
3424         Py_DECREF(str2);
3425         PyErr_Clear();
3426         return res;
3427     }
3428     Py_RETURN_NONE;
3429 }
3430 
3431 /* To test that the result of PyCode_NewEmpty has the right members. */
3432 static PyObject *
code_newempty(PyObject * self,PyObject * args)3433 code_newempty(PyObject *self, PyObject *args)
3434 {
3435     const char *filename;
3436     const char *funcname;
3437     int firstlineno;
3438 
3439     if (!PyArg_ParseTuple(args, "ssi:code_newempty",
3440                           &filename, &funcname, &firstlineno))
3441         return NULL;
3442 
3443     return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
3444 }
3445 
3446 /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
3447    Run via Lib/test/test_exceptions.py */
3448 static PyObject *
make_exception_with_doc(PyObject * self,PyObject * args,PyObject * kwargs)3449 make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
3450 {
3451     const char *name;
3452     const char *doc = NULL;
3453     PyObject *base = NULL;
3454     PyObject *dict = NULL;
3455 
3456     static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
3457 
3458     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
3459                     "s|sOO:make_exception_with_doc", kwlist,
3460                                      &name, &doc, &base, &dict))
3461         return NULL;
3462 
3463     return PyErr_NewExceptionWithDoc(name, doc, base, dict);
3464 }
3465 
3466 static PyObject *
make_memoryview_from_NULL_pointer(PyObject * self,PyObject * Py_UNUSED (ignored))3467 make_memoryview_from_NULL_pointer(PyObject *self, PyObject *Py_UNUSED(ignored))
3468 {
3469     Py_buffer info;
3470     if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0)
3471         return NULL;
3472     return PyMemoryView_FromBuffer(&info);
3473 }
3474 
3475 static PyObject *
test_from_contiguous(PyObject * self,PyObject * Py_UNUSED (ignored))3476 test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored))
3477 {
3478     int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
3479     int init[5] = {0, 1, 2, 3, 4};
3480     Py_ssize_t itemsize = sizeof(int);
3481     Py_ssize_t shape = 5;
3482     Py_ssize_t strides = 2 * itemsize;
3483     Py_buffer view = {
3484         data,
3485         NULL,
3486         5 * itemsize,
3487         itemsize,
3488         1,
3489         1,
3490         NULL,
3491         &shape,
3492         &strides,
3493         NULL,
3494         NULL
3495     };
3496     int *ptr;
3497     int i;
3498 
3499     PyBuffer_FromContiguous(&view, init, view.len, 'C');
3500     ptr = view.buf;
3501     for (i = 0; i < 5; i++) {
3502         if (ptr[2*i] != i) {
3503             PyErr_SetString(TestError,
3504                 "test_from_contiguous: incorrect result");
3505             return NULL;
3506         }
3507     }
3508 
3509     view.buf = &data[8];
3510     view.strides[0] = -2 * itemsize;
3511 
3512     PyBuffer_FromContiguous(&view, init, view.len, 'C');
3513     ptr = view.buf;
3514     for (i = 0; i < 5; i++) {
3515         if (*(ptr-2*i) != i) {
3516             PyErr_SetString(TestError,
3517                 "test_from_contiguous: incorrect result");
3518             return NULL;
3519         }
3520     }
3521 
3522     Py_RETURN_NONE;
3523 }
3524 
3525 #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
3526 extern PyTypeObject _PyBytesIOBuffer_Type;
3527 
3528 static PyObject *
test_pep3118_obsolete_write_locks(PyObject * self,PyObject * Py_UNUSED (ignored))3529 test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
3530 {
3531     PyTypeObject *type = &_PyBytesIOBuffer_Type;
3532     PyObject *b;
3533     char *dummy[1];
3534     int ret, match;
3535 
3536     /* PyBuffer_FillInfo() */
3537     ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE);
3538     match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
3539     PyErr_Clear();
3540     if (ret != -1 || match == 0)
3541         goto error;
3542 
3543     /* bytesiobuf_getbuffer() */
3544     b = type->tp_alloc(type, 0);
3545     if (b == NULL) {
3546         return NULL;
3547     }
3548 
3549     ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE);
3550     Py_DECREF(b);
3551     match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
3552     PyErr_Clear();
3553     if (ret != -1 || match == 0)
3554         goto error;
3555 
3556     Py_RETURN_NONE;
3557 
3558 error:
3559     PyErr_SetString(TestError,
3560         "test_pep3118_obsolete_write_locks: failure");
3561     return NULL;
3562 }
3563 #endif
3564 
3565 /* This tests functions that historically supported write locks.  It is
3566    wrong to call getbuffer() with view==NULL and a compliant getbufferproc
3567    is entitled to segfault in that case. */
3568 static PyObject *
getbuffer_with_null_view(PyObject * self,PyObject * obj)3569 getbuffer_with_null_view(PyObject* self, PyObject *obj)
3570 {
3571     if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0)
3572         return NULL;
3573 
3574     Py_RETURN_NONE;
3575 }
3576 
3577 /* PyBuffer_SizeFromFormat() */
3578 static PyObject *
test_PyBuffer_SizeFromFormat(PyObject * self,PyObject * args)3579 test_PyBuffer_SizeFromFormat(PyObject *self, PyObject *args)
3580 {
3581     const char *format;
3582     Py_ssize_t result;
3583 
3584     if (!PyArg_ParseTuple(args, "s:test_PyBuffer_SizeFromFormat",
3585                           &format)) {
3586         return NULL;
3587     }
3588 
3589     result = PyBuffer_SizeFromFormat(format);
3590     if (result == -1) {
3591         return NULL;
3592     }
3593 
3594     return PyLong_FromSsize_t(result);
3595 }
3596 
3597 /* Test that the fatal error from not having a current thread doesn't
3598    cause an infinite loop.  Run via Lib/test/test_capi.py */
3599 static PyObject *
crash_no_current_thread(PyObject * self,PyObject * Py_UNUSED (ignored))3600 crash_no_current_thread(PyObject *self, PyObject *Py_UNUSED(ignored))
3601 {
3602     Py_BEGIN_ALLOW_THREADS
3603     /* Using PyThreadState_Get() directly allows the test to pass in
3604        !pydebug mode. However, the test only actually tests anything
3605        in pydebug mode, since that's where the infinite loop was in
3606        the first place. */
3607     PyThreadState_Get();
3608     Py_END_ALLOW_THREADS
3609     return NULL;
3610 }
3611 
3612 /* To run some code in a sub-interpreter. */
3613 static PyObject *
run_in_subinterp(PyObject * self,PyObject * args)3614 run_in_subinterp(PyObject *self, PyObject *args)
3615 {
3616     const char *code;
3617     int r;
3618     PyThreadState *substate, *mainstate;
3619     /* only initialise 'cflags.cf_flags' to test backwards compatibility */
3620     PyCompilerFlags cflags = {0};
3621 
3622     if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
3623                           &code))
3624         return NULL;
3625 
3626     mainstate = PyThreadState_Get();
3627 
3628     PyThreadState_Swap(NULL);
3629 
3630     substate = Py_NewInterpreter();
3631     if (substate == NULL) {
3632         /* Since no new thread state was created, there is no exception to
3633            propagate; raise a fresh one after swapping in the old thread
3634            state. */
3635         PyThreadState_Swap(mainstate);
3636         PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
3637         return NULL;
3638     }
3639     r = PyRun_SimpleStringFlags(code, &cflags);
3640     Py_EndInterpreter(substate);
3641 
3642     PyThreadState_Swap(mainstate);
3643 
3644     return PyLong_FromLong(r);
3645 }
3646 
3647 static int
check_time_rounding(int round)3648 check_time_rounding(int round)
3649 {
3650     if (round != _PyTime_ROUND_FLOOR
3651         && round != _PyTime_ROUND_CEILING
3652         && round != _PyTime_ROUND_HALF_EVEN
3653         && round != _PyTime_ROUND_UP) {
3654         PyErr_SetString(PyExc_ValueError, "invalid rounding");
3655         return -1;
3656     }
3657     return 0;
3658 }
3659 
3660 static PyObject *
test_pytime_object_to_time_t(PyObject * self,PyObject * args)3661 test_pytime_object_to_time_t(PyObject *self, PyObject *args)
3662 {
3663     PyObject *obj;
3664     time_t sec;
3665     int round;
3666     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round))
3667         return NULL;
3668     if (check_time_rounding(round) < 0)
3669         return NULL;
3670     if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1)
3671         return NULL;
3672     return _PyLong_FromTime_t(sec);
3673 }
3674 
3675 static PyObject *
test_pytime_object_to_timeval(PyObject * self,PyObject * args)3676 test_pytime_object_to_timeval(PyObject *self, PyObject *args)
3677 {
3678     PyObject *obj;
3679     time_t sec;
3680     long usec;
3681     int round;
3682     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round))
3683         return NULL;
3684     if (check_time_rounding(round) < 0)
3685         return NULL;
3686     if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1)
3687         return NULL;
3688     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
3689 }
3690 
3691 static PyObject *
test_pytime_object_to_timespec(PyObject * self,PyObject * args)3692 test_pytime_object_to_timespec(PyObject *self, PyObject *args)
3693 {
3694     PyObject *obj;
3695     time_t sec;
3696     long nsec;
3697     int round;
3698     if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round))
3699         return NULL;
3700     if (check_time_rounding(round) < 0)
3701         return NULL;
3702     if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1)
3703         return NULL;
3704     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
3705 }
3706 
3707 static void
slot_tp_del(PyObject * self)3708 slot_tp_del(PyObject *self)
3709 {
3710     _Py_IDENTIFIER(__tp_del__);
3711     PyObject *del, *res;
3712     PyObject *error_type, *error_value, *error_traceback;
3713 
3714     /* Temporarily resurrect the object. */
3715     assert(Py_REFCNT(self) == 0);
3716     Py_SET_REFCNT(self, 1);
3717 
3718     /* Save the current exception, if any. */
3719     PyErr_Fetch(&error_type, &error_value, &error_traceback);
3720 
3721     /* Execute __del__ method, if any. */
3722     del = _PyObject_LookupSpecial(self, &PyId___tp_del__);
3723     if (del != NULL) {
3724         res = _PyObject_CallNoArg(del);
3725         if (res == NULL)
3726             PyErr_WriteUnraisable(del);
3727         else
3728             Py_DECREF(res);
3729         Py_DECREF(del);
3730     }
3731 
3732     /* Restore the saved exception. */
3733     PyErr_Restore(error_type, error_value, error_traceback);
3734 
3735     /* Undo the temporary resurrection; can't use DECREF here, it would
3736      * cause a recursive call.
3737      */
3738     assert(Py_REFCNT(self) > 0);
3739     Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
3740     if (Py_REFCNT(self) == 0) {
3741         /* this is the normal path out */
3742         return;
3743     }
3744 
3745     /* __del__ resurrected it!  Make it look like the original Py_DECREF
3746      * never happened.
3747      */
3748     {
3749         Py_ssize_t refcnt = Py_REFCNT(self);
3750         _Py_NewReference(self);
3751         Py_SET_REFCNT(self, refcnt);
3752     }
3753     assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
3754     /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
3755        _Py_RefTotal, so we need to undo that. */
3756 #ifdef Py_REF_DEBUG
3757     _Py_RefTotal--;
3758 #endif
3759 }
3760 
3761 static PyObject *
with_tp_del(PyObject * self,PyObject * args)3762 with_tp_del(PyObject *self, PyObject *args)
3763 {
3764     PyObject *obj;
3765     PyTypeObject *tp;
3766 
3767     if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
3768         return NULL;
3769     tp = (PyTypeObject *) obj;
3770     if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
3771         PyErr_Format(PyExc_TypeError,
3772                      "heap type expected, got %R", obj);
3773         return NULL;
3774     }
3775     tp->tp_del = slot_tp_del;
3776     Py_INCREF(obj);
3777     return obj;
3778 }
3779 
3780 static PyObject *
without_gc(PyObject * Py_UNUSED (self),PyObject * obj)3781 without_gc(PyObject *Py_UNUSED(self), PyObject *obj)
3782 {
3783     PyTypeObject *tp = (PyTypeObject*)obj;
3784     if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
3785         return PyErr_Format(PyExc_TypeError, "heap type expected, got %R", obj);
3786     }
3787     if (PyType_IS_GC(tp)) {
3788         // Don't try this at home, kids:
3789         tp->tp_flags -= Py_TPFLAGS_HAVE_GC;
3790         tp->tp_free = PyObject_Del;
3791         tp->tp_traverse = NULL;
3792         tp->tp_clear = NULL;
3793     }
3794     assert(!PyType_IS_GC(tp));
3795     Py_INCREF(obj);
3796     return obj;
3797 }
3798 
3799 static PyMethodDef ml;
3800 
3801 static PyObject *
create_cfunction(PyObject * self,PyObject * args)3802 create_cfunction(PyObject *self, PyObject *args)
3803 {
3804     return PyCFunction_NewEx(&ml, self, NULL);
3805 }
3806 
3807 static PyMethodDef ml = {
3808     "create_cfunction",
3809     create_cfunction,
3810     METH_NOARGS,
3811     NULL
3812 };
3813 
3814 static PyObject *
_test_incref(PyObject * ob)3815 _test_incref(PyObject *ob)
3816 {
3817     Py_INCREF(ob);
3818     return ob;
3819 }
3820 
3821 static PyObject *
test_xincref_doesnt_leak(PyObject * ob,PyObject * Py_UNUSED (ignored))3822 test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
3823 {
3824     PyObject *obj = PyLong_FromLong(0);
3825     Py_XINCREF(_test_incref(obj));
3826     Py_DECREF(obj);
3827     Py_DECREF(obj);
3828     Py_DECREF(obj);
3829     Py_RETURN_NONE;
3830 }
3831 
3832 static PyObject *
test_incref_doesnt_leak(PyObject * ob,PyObject * Py_UNUSED (ignored))3833 test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
3834 {
3835     PyObject *obj = PyLong_FromLong(0);
3836     Py_INCREF(_test_incref(obj));
3837     Py_DECREF(obj);
3838     Py_DECREF(obj);
3839     Py_DECREF(obj);
3840     Py_RETURN_NONE;
3841 }
3842 
3843 static PyObject *
test_xdecref_doesnt_leak(PyObject * ob,PyObject * Py_UNUSED (ignored))3844 test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
3845 {
3846     Py_XDECREF(PyLong_FromLong(0));
3847     Py_RETURN_NONE;
3848 }
3849 
3850 static PyObject *
test_decref_doesnt_leak(PyObject * ob,PyObject * Py_UNUSED (ignored))3851 test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
3852 {
3853     Py_DECREF(PyLong_FromLong(0));
3854     Py_RETURN_NONE;
3855 }
3856 
3857 static PyObject *
test_structseq_newtype_doesnt_leak(PyObject * Py_UNUSED (self),PyObject * Py_UNUSED (args))3858 test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
3859                               PyObject *Py_UNUSED(args))
3860 {
3861     PyStructSequence_Desc descr;
3862     PyStructSequence_Field descr_fields[3];
3863 
3864     descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"};
3865     descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"};
3866     descr_fields[2] = (PyStructSequence_Field){0, NULL};
3867 
3868     descr.name = "_testcapi.test_descr";
3869     descr.doc = "This is used to test for memory leaks in NewType";
3870     descr.fields = descr_fields;
3871     descr.n_in_sequence = 1;
3872 
3873     PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
3874     assert(structseq_type != NULL);
3875     assert(PyType_Check(structseq_type));
3876     assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
3877     Py_DECREF(structseq_type);
3878 
3879     Py_RETURN_NONE;
3880 }
3881 
3882 static PyObject *
test_structseq_newtype_null_descr_doc(PyObject * Py_UNUSED (self),PyObject * Py_UNUSED (args))3883 test_structseq_newtype_null_descr_doc(PyObject *Py_UNUSED(self),
3884                               PyObject *Py_UNUSED(args))
3885 {
3886     PyStructSequence_Field descr_fields[1] = {
3887         (PyStructSequence_Field){NULL, NULL}
3888     };
3889     // Test specifically for NULL .doc field.
3890     PyStructSequence_Desc descr = {"_testcapi.test_descr", NULL, &descr_fields[0], 0};
3891 
3892     PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
3893     assert(structseq_type != NULL);
3894     assert(PyType_Check(structseq_type));
3895     assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
3896     Py_DECREF(structseq_type);
3897 
3898     Py_RETURN_NONE;
3899 }
3900 
3901 static PyObject *
test_incref_decref_API(PyObject * ob,PyObject * Py_UNUSED (ignored))3902 test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored))
3903 {
3904     PyObject *obj = PyLong_FromLong(0);
3905     Py_IncRef(obj);
3906     Py_DecRef(obj);
3907     Py_DecRef(obj);
3908     Py_RETURN_NONE;
3909 }
3910 
3911 static PyObject *
test_pymem_alloc0(PyObject * self,PyObject * Py_UNUSED (ignored))3912 test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
3913 {
3914     void *ptr;
3915 
3916     ptr = PyMem_RawMalloc(0);
3917     if (ptr == NULL) {
3918         PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL");
3919         return NULL;
3920     }
3921     PyMem_RawFree(ptr);
3922 
3923     ptr = PyMem_RawCalloc(0, 0);
3924     if (ptr == NULL) {
3925         PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL");
3926         return NULL;
3927     }
3928     PyMem_RawFree(ptr);
3929 
3930     ptr = PyMem_Malloc(0);
3931     if (ptr == NULL) {
3932         PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL");
3933         return NULL;
3934     }
3935     PyMem_Free(ptr);
3936 
3937     ptr = PyMem_Calloc(0, 0);
3938     if (ptr == NULL) {
3939         PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL");
3940         return NULL;
3941     }
3942     PyMem_Free(ptr);
3943 
3944     ptr = PyObject_Malloc(0);
3945     if (ptr == NULL) {
3946         PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL");
3947         return NULL;
3948     }
3949     PyObject_Free(ptr);
3950 
3951     ptr = PyObject_Calloc(0, 0);
3952     if (ptr == NULL) {
3953         PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL");
3954         return NULL;
3955     }
3956     PyObject_Free(ptr);
3957 
3958     Py_RETURN_NONE;
3959 }
3960 
3961 typedef struct {
3962     PyMemAllocatorEx alloc;
3963 
3964     size_t malloc_size;
3965     size_t calloc_nelem;
3966     size_t calloc_elsize;
3967     void *realloc_ptr;
3968     size_t realloc_new_size;
3969     void *free_ptr;
3970     void *ctx;
3971 } alloc_hook_t;
3972 
hook_malloc(void * ctx,size_t size)3973 static void* hook_malloc(void* ctx, size_t size)
3974 {
3975     alloc_hook_t *hook = (alloc_hook_t *)ctx;
3976     hook->ctx = ctx;
3977     hook->malloc_size = size;
3978     return hook->alloc.malloc(hook->alloc.ctx, size);
3979 }
3980 
hook_calloc(void * ctx,size_t nelem,size_t elsize)3981 static void* hook_calloc(void* ctx, size_t nelem, size_t elsize)
3982 {
3983     alloc_hook_t *hook = (alloc_hook_t *)ctx;
3984     hook->ctx = ctx;
3985     hook->calloc_nelem = nelem;
3986     hook->calloc_elsize = elsize;
3987     return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize);
3988 }
3989 
hook_realloc(void * ctx,void * ptr,size_t new_size)3990 static void* hook_realloc(void* ctx, void* ptr, size_t new_size)
3991 {
3992     alloc_hook_t *hook = (alloc_hook_t *)ctx;
3993     hook->ctx = ctx;
3994     hook->realloc_ptr = ptr;
3995     hook->realloc_new_size = new_size;
3996     return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size);
3997 }
3998 
hook_free(void * ctx,void * ptr)3999 static void hook_free(void *ctx, void *ptr)
4000 {
4001     alloc_hook_t *hook = (alloc_hook_t *)ctx;
4002     hook->ctx = ctx;
4003     hook->free_ptr = ptr;
4004     hook->alloc.free(hook->alloc.ctx, ptr);
4005 }
4006 
4007 static PyObject *
test_setallocators(PyMemAllocatorDomain domain)4008 test_setallocators(PyMemAllocatorDomain domain)
4009 {
4010     PyObject *res = NULL;
4011     const char *error_msg;
4012     alloc_hook_t hook;
4013     PyMemAllocatorEx alloc;
4014     size_t size, size2, nelem, elsize;
4015     void *ptr, *ptr2;
4016 
4017     memset(&hook, 0, sizeof(hook));
4018 
4019     alloc.ctx = &hook;
4020     alloc.malloc = &hook_malloc;
4021     alloc.calloc = &hook_calloc;
4022     alloc.realloc = &hook_realloc;
4023     alloc.free = &hook_free;
4024     PyMem_GetAllocator(domain, &hook.alloc);
4025     PyMem_SetAllocator(domain, &alloc);
4026 
4027     /* malloc, realloc, free */
4028     size = 42;
4029     hook.ctx = NULL;
4030     switch(domain)
4031     {
4032     case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break;
4033     case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break;
4034     case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break;
4035     default: ptr = NULL; break;
4036     }
4037 
4038 #define CHECK_CTX(FUNC) \
4039     if (hook.ctx != &hook) { \
4040         error_msg = FUNC " wrong context"; \
4041         goto fail; \
4042     } \
4043     hook.ctx = NULL;  /* reset for next check */
4044 
4045     if (ptr == NULL) {
4046         error_msg = "malloc failed";
4047         goto fail;
4048     }
4049     CHECK_CTX("malloc");
4050     if (hook.malloc_size != size) {
4051         error_msg = "malloc invalid size";
4052         goto fail;
4053     }
4054 
4055     size2 = 200;
4056     switch(domain)
4057     {
4058     case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break;
4059     case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break;
4060     case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break;
4061     default: ptr2 = NULL; break;
4062     }
4063 
4064     if (ptr2 == NULL) {
4065         error_msg = "realloc failed";
4066         goto fail;
4067     }
4068     CHECK_CTX("realloc");
4069     if (hook.realloc_ptr != ptr
4070         || hook.realloc_new_size != size2) {
4071         error_msg = "realloc invalid parameters";
4072         goto fail;
4073     }
4074 
4075     switch(domain)
4076     {
4077     case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break;
4078     case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break;
4079     case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break;
4080     }
4081 
4082     CHECK_CTX("free");
4083     if (hook.free_ptr != ptr2) {
4084         error_msg = "free invalid pointer";
4085         goto fail;
4086     }
4087 
4088     /* calloc, free */
4089     nelem = 2;
4090     elsize = 5;
4091     switch(domain)
4092     {
4093     case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break;
4094     case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break;
4095     case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break;
4096     default: ptr = NULL; break;
4097     }
4098 
4099     if (ptr == NULL) {
4100         error_msg = "calloc failed";
4101         goto fail;
4102     }
4103     CHECK_CTX("calloc");
4104     if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) {
4105         error_msg = "calloc invalid nelem or elsize";
4106         goto fail;
4107     }
4108 
4109     hook.free_ptr = NULL;
4110     switch(domain)
4111     {
4112     case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break;
4113     case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break;
4114     case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break;
4115     }
4116 
4117     CHECK_CTX("calloc free");
4118     if (hook.free_ptr != ptr) {
4119         error_msg = "calloc free invalid pointer";
4120         goto fail;
4121     }
4122 
4123     Py_INCREF(Py_None);
4124     res = Py_None;
4125     goto finally;
4126 
4127 fail:
4128     PyErr_SetString(PyExc_RuntimeError, error_msg);
4129 
4130 finally:
4131     PyMem_SetAllocator(domain, &hook.alloc);
4132     return res;
4133 
4134 #undef CHECK_CTX
4135 }
4136 
4137 static PyObject *
test_pymem_setrawallocators(PyObject * self,PyObject * Py_UNUSED (ignored))4138 test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
4139 {
4140     return test_setallocators(PYMEM_DOMAIN_RAW);
4141 }
4142 
4143 static PyObject *
test_pymem_setallocators(PyObject * self,PyObject * Py_UNUSED (ignored))4144 test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
4145 {
4146     return test_setallocators(PYMEM_DOMAIN_MEM);
4147 }
4148 
4149 static PyObject *
test_pyobject_setallocators(PyObject * self,PyObject * Py_UNUSED (ignored))4150 test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
4151 {
4152     return test_setallocators(PYMEM_DOMAIN_OBJ);
4153 }
4154 
4155 /* Most part of the following code is inherited from the pyfailmalloc project
4156  * written by Victor Stinner. */
4157 static struct {
4158     int installed;
4159     PyMemAllocatorEx raw;
4160     PyMemAllocatorEx mem;
4161     PyMemAllocatorEx obj;
4162 } FmHook;
4163 
4164 static struct {
4165     int start;
4166     int stop;
4167     Py_ssize_t count;
4168 } FmData;
4169 
4170 static int
fm_nomemory(void)4171 fm_nomemory(void)
4172 {
4173     FmData.count++;
4174     if (FmData.count > FmData.start &&
4175             (FmData.stop <= 0 || FmData.count <= FmData.stop)) {
4176         return 1;
4177     }
4178     return 0;
4179 }
4180 
4181 static void *
hook_fmalloc(void * ctx,size_t size)4182 hook_fmalloc(void *ctx, size_t size)
4183 {
4184     PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
4185     if (fm_nomemory()) {
4186         return NULL;
4187     }
4188     return alloc->malloc(alloc->ctx, size);
4189 }
4190 
4191 static void *
hook_fcalloc(void * ctx,size_t nelem,size_t elsize)4192 hook_fcalloc(void *ctx, size_t nelem, size_t elsize)
4193 {
4194     PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
4195     if (fm_nomemory()) {
4196         return NULL;
4197     }
4198     return alloc->calloc(alloc->ctx, nelem, elsize);
4199 }
4200 
4201 static void *
hook_frealloc(void * ctx,void * ptr,size_t new_size)4202 hook_frealloc(void *ctx, void *ptr, size_t new_size)
4203 {
4204     PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
4205     if (fm_nomemory()) {
4206         return NULL;
4207     }
4208     return alloc->realloc(alloc->ctx, ptr, new_size);
4209 }
4210 
4211 static void
hook_ffree(void * ctx,void * ptr)4212 hook_ffree(void *ctx, void *ptr)
4213 {
4214     PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
4215     alloc->free(alloc->ctx, ptr);
4216 }
4217 
4218 static void
fm_setup_hooks(void)4219 fm_setup_hooks(void)
4220 {
4221     PyMemAllocatorEx alloc;
4222 
4223     if (FmHook.installed) {
4224         return;
4225     }
4226     FmHook.installed = 1;
4227 
4228     alloc.malloc = hook_fmalloc;
4229     alloc.calloc = hook_fcalloc;
4230     alloc.realloc = hook_frealloc;
4231     alloc.free = hook_ffree;
4232     PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
4233     PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
4234     PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
4235 
4236     alloc.ctx = &FmHook.raw;
4237     PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
4238 
4239     alloc.ctx = &FmHook.mem;
4240     PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
4241 
4242     alloc.ctx = &FmHook.obj;
4243     PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc);
4244 }
4245 
4246 static void
fm_remove_hooks(void)4247 fm_remove_hooks(void)
4248 {
4249     if (FmHook.installed) {
4250         FmHook.installed = 0;
4251         PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
4252         PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
4253         PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
4254     }
4255 }
4256 
4257 static PyObject*
set_nomemory(PyObject * self,PyObject * args)4258 set_nomemory(PyObject *self, PyObject *args)
4259 {
4260     /* Memory allocation fails after 'start' allocation requests, and until
4261      * 'stop' allocation requests except when 'stop' is negative or equal
4262      * to 0 (default) in which case allocation failures never stop. */
4263     FmData.count = 0;
4264     FmData.stop = 0;
4265     if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) {
4266         return NULL;
4267     }
4268     fm_setup_hooks();
4269     Py_RETURN_NONE;
4270 }
4271 
4272 static PyObject*
remove_mem_hooks(PyObject * self,PyObject * Py_UNUSED (ignored))4273 remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored))
4274 {
4275     fm_remove_hooks();
4276     Py_RETURN_NONE;
4277 }
4278 
4279 PyDoc_STRVAR(docstring_empty,
4280 ""
4281 );
4282 
4283 PyDoc_STRVAR(docstring_no_signature,
4284 "This docstring has no signature."
4285 );
4286 
4287 PyDoc_STRVAR(docstring_with_invalid_signature,
4288 "docstring_with_invalid_signature($module, /, boo)\n"
4289 "\n"
4290 "This docstring has an invalid signature."
4291 );
4292 
4293 PyDoc_STRVAR(docstring_with_invalid_signature2,
4294 "docstring_with_invalid_signature2($module, /, boo)\n"
4295 "\n"
4296 "--\n"
4297 "\n"
4298 "This docstring also has an invalid signature."
4299 );
4300 
4301 PyDoc_STRVAR(docstring_with_signature,
4302 "docstring_with_signature($module, /, sig)\n"
4303 "--\n"
4304 "\n"
4305 "This docstring has a valid signature."
4306 );
4307 
4308 PyDoc_STRVAR(docstring_with_signature_but_no_doc,
4309 "docstring_with_signature_but_no_doc($module, /, sig)\n"
4310 "--\n"
4311 "\n"
4312 );
4313 
4314 PyDoc_STRVAR(docstring_with_signature_and_extra_newlines,
4315 "docstring_with_signature_and_extra_newlines($module, /, parameter)\n"
4316 "--\n"
4317 "\n"
4318 "\n"
4319 "This docstring has a valid signature and some extra newlines."
4320 );
4321 
4322 PyDoc_STRVAR(docstring_with_signature_with_defaults,
4323 "docstring_with_signature_with_defaults(module, s='avocado',\n"
4324 "        b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n"
4325 "        local=the_number_three, sys=sys.maxsize,\n"
4326 "        exp=sys.maxsize - 1)\n"
4327 "--\n"
4328 "\n"
4329 "\n"
4330 "\n"
4331 "This docstring has a valid signature with parameters,\n"
4332 "and the parameters take defaults of varying types."
4333 );
4334 
4335 typedef struct {
4336     PyThread_type_lock start_event;
4337     PyThread_type_lock exit_event;
4338     PyObject *callback;
4339 } test_c_thread_t;
4340 
4341 static void
temporary_c_thread(void * data)4342 temporary_c_thread(void *data)
4343 {
4344     test_c_thread_t *test_c_thread = data;
4345     PyGILState_STATE state;
4346     PyObject *res;
4347 
4348     PyThread_release_lock(test_c_thread->start_event);
4349 
4350     /* Allocate a Python thread state for this thread */
4351     state = PyGILState_Ensure();
4352 
4353     res = _PyObject_CallNoArg(test_c_thread->callback);
4354     Py_CLEAR(test_c_thread->callback);
4355 
4356     if (res == NULL) {
4357         PyErr_Print();
4358     }
4359     else {
4360         Py_DECREF(res);
4361     }
4362 
4363     /* Destroy the Python thread state for this thread */
4364     PyGILState_Release(state);
4365 
4366     PyThread_release_lock(test_c_thread->exit_event);
4367 
4368     PyThread_exit_thread();
4369 }
4370 
4371 static PyObject *
call_in_temporary_c_thread(PyObject * self,PyObject * callback)4372 call_in_temporary_c_thread(PyObject *self, PyObject *callback)
4373 {
4374     PyObject *res = NULL;
4375     test_c_thread_t test_c_thread;
4376     long thread;
4377 
4378     test_c_thread.start_event = PyThread_allocate_lock();
4379     test_c_thread.exit_event = PyThread_allocate_lock();
4380     test_c_thread.callback = NULL;
4381     if (!test_c_thread.start_event || !test_c_thread.exit_event) {
4382         PyErr_SetString(PyExc_RuntimeError, "could not allocate lock");
4383         goto exit;
4384     }
4385 
4386     Py_INCREF(callback);
4387     test_c_thread.callback = callback;
4388 
4389     PyThread_acquire_lock(test_c_thread.start_event, 1);
4390     PyThread_acquire_lock(test_c_thread.exit_event, 1);
4391 
4392     thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread);
4393     if (thread == -1) {
4394         PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
4395         PyThread_release_lock(test_c_thread.start_event);
4396         PyThread_release_lock(test_c_thread.exit_event);
4397         goto exit;
4398     }
4399 
4400     PyThread_acquire_lock(test_c_thread.start_event, 1);
4401     PyThread_release_lock(test_c_thread.start_event);
4402 
4403     Py_BEGIN_ALLOW_THREADS
4404         PyThread_acquire_lock(test_c_thread.exit_event, 1);
4405         PyThread_release_lock(test_c_thread.exit_event);
4406     Py_END_ALLOW_THREADS
4407 
4408     Py_INCREF(Py_None);
4409     res = Py_None;
4410 
4411 exit:
4412     Py_CLEAR(test_c_thread.callback);
4413     if (test_c_thread.start_event)
4414         PyThread_free_lock(test_c_thread.start_event);
4415     if (test_c_thread.exit_event)
4416         PyThread_free_lock(test_c_thread.exit_event);
4417     return res;
4418 }
4419 
4420 /* marshal */
4421 
4422 static PyObject*
pymarshal_write_long_to_file(PyObject * self,PyObject * args)4423 pymarshal_write_long_to_file(PyObject* self, PyObject *args)
4424 {
4425     long value;
4426     PyObject *filename;
4427     int version;
4428     FILE *fp;
4429 
4430     if (!PyArg_ParseTuple(args, "lOi:pymarshal_write_long_to_file",
4431                           &value, &filename, &version))
4432         return NULL;
4433 
4434     fp = _Py_fopen_obj(filename, "wb");
4435     if (fp == NULL) {
4436         PyErr_SetFromErrno(PyExc_OSError);
4437         return NULL;
4438     }
4439 
4440     PyMarshal_WriteLongToFile(value, fp, version);
4441 
4442     fclose(fp);
4443     if (PyErr_Occurred())
4444         return NULL;
4445     Py_RETURN_NONE;
4446 }
4447 
4448 static PyObject*
pymarshal_write_object_to_file(PyObject * self,PyObject * args)4449 pymarshal_write_object_to_file(PyObject* self, PyObject *args)
4450 {
4451     PyObject *obj;
4452     PyObject *filename;
4453     int version;
4454     FILE *fp;
4455 
4456     if (!PyArg_ParseTuple(args, "OOi:pymarshal_write_object_to_file",
4457                           &obj, &filename, &version))
4458         return NULL;
4459 
4460     fp = _Py_fopen_obj(filename, "wb");
4461     if (fp == NULL) {
4462         PyErr_SetFromErrno(PyExc_OSError);
4463         return NULL;
4464     }
4465 
4466     PyMarshal_WriteObjectToFile(obj, fp, version);
4467 
4468     fclose(fp);
4469     if (PyErr_Occurred())
4470         return NULL;
4471     Py_RETURN_NONE;
4472 }
4473 
4474 static PyObject*
pymarshal_read_short_from_file(PyObject * self,PyObject * args)4475 pymarshal_read_short_from_file(PyObject* self, PyObject *args)
4476 {
4477     int value;
4478     long pos;
4479     PyObject *filename;
4480     FILE *fp;
4481 
4482     if (!PyArg_ParseTuple(args, "O:pymarshal_read_short_from_file", &filename))
4483         return NULL;
4484 
4485     fp = _Py_fopen_obj(filename, "rb");
4486     if (fp == NULL) {
4487         PyErr_SetFromErrno(PyExc_OSError);
4488         return NULL;
4489     }
4490 
4491     value = PyMarshal_ReadShortFromFile(fp);
4492     pos = ftell(fp);
4493 
4494     fclose(fp);
4495     if (PyErr_Occurred())
4496         return NULL;
4497     return Py_BuildValue("il", value, pos);
4498 }
4499 
4500 static PyObject*
pymarshal_read_long_from_file(PyObject * self,PyObject * args)4501 pymarshal_read_long_from_file(PyObject* self, PyObject *args)
4502 {
4503     long value, pos;
4504     PyObject *filename;
4505     FILE *fp;
4506 
4507     if (!PyArg_ParseTuple(args, "O:pymarshal_read_long_from_file", &filename))
4508         return NULL;
4509 
4510     fp = _Py_fopen_obj(filename, "rb");
4511     if (fp == NULL) {
4512         PyErr_SetFromErrno(PyExc_OSError);
4513         return NULL;
4514     }
4515 
4516     value = PyMarshal_ReadLongFromFile(fp);
4517     pos = ftell(fp);
4518 
4519     fclose(fp);
4520     if (PyErr_Occurred())
4521         return NULL;
4522     return Py_BuildValue("ll", value, pos);
4523 }
4524 
4525 static PyObject*
pymarshal_read_last_object_from_file(PyObject * self,PyObject * args)4526 pymarshal_read_last_object_from_file(PyObject* self, PyObject *args)
4527 {
4528     PyObject *obj;
4529     long pos;
4530     PyObject *filename;
4531     FILE *fp;
4532 
4533     if (!PyArg_ParseTuple(args, "O:pymarshal_read_last_object_from_file", &filename))
4534         return NULL;
4535 
4536     fp = _Py_fopen_obj(filename, "rb");
4537     if (fp == NULL) {
4538         PyErr_SetFromErrno(PyExc_OSError);
4539         return NULL;
4540     }
4541 
4542     obj = PyMarshal_ReadLastObjectFromFile(fp);
4543     pos = ftell(fp);
4544 
4545     fclose(fp);
4546     return Py_BuildValue("Nl", obj, pos);
4547 }
4548 
4549 static PyObject*
pymarshal_read_object_from_file(PyObject * self,PyObject * args)4550 pymarshal_read_object_from_file(PyObject* self, PyObject *args)
4551 {
4552     PyObject *obj;
4553     long pos;
4554     PyObject *filename;
4555     FILE *fp;
4556 
4557     if (!PyArg_ParseTuple(args, "O:pymarshal_read_object_from_file", &filename))
4558         return NULL;
4559 
4560     fp = _Py_fopen_obj(filename, "rb");
4561     if (fp == NULL) {
4562         PyErr_SetFromErrno(PyExc_OSError);
4563         return NULL;
4564     }
4565 
4566     obj = PyMarshal_ReadObjectFromFile(fp);
4567     pos = ftell(fp);
4568 
4569     fclose(fp);
4570     return Py_BuildValue("Nl", obj, pos);
4571 }
4572 
4573 static PyObject*
return_null_without_error(PyObject * self,PyObject * args)4574 return_null_without_error(PyObject *self, PyObject *args)
4575 {
4576     /* invalid call: return NULL without setting an error,
4577      * _Py_CheckFunctionResult() must detect such bug at runtime. */
4578     PyErr_Clear();
4579     return NULL;
4580 }
4581 
4582 static PyObject*
return_result_with_error(PyObject * self,PyObject * args)4583 return_result_with_error(PyObject *self, PyObject *args)
4584 {
4585     /* invalid call: return a result with an error set,
4586      * _Py_CheckFunctionResult() must detect such bug at runtime. */
4587     PyErr_SetNone(PyExc_ValueError);
4588     Py_RETURN_NONE;
4589 }
4590 
4591 static PyObject*
getitem_with_error(PyObject * self,PyObject * args)4592 getitem_with_error(PyObject *self, PyObject *args)
4593 {
4594     PyObject *map, *key;
4595     if (!PyArg_ParseTuple(args, "OO", &map, &key)) {
4596         return NULL;
4597     }
4598 
4599     PyErr_SetString(PyExc_ValueError, "bug");
4600     return PyObject_GetItem(map, key);
4601 }
4602 
4603 static PyObject *
test_pytime_fromseconds(PyObject * self,PyObject * args)4604 test_pytime_fromseconds(PyObject *self, PyObject *args)
4605 {
4606     int seconds;
4607     _PyTime_t ts;
4608 
4609     if (!PyArg_ParseTuple(args, "i", &seconds))
4610         return NULL;
4611     ts = _PyTime_FromSeconds(seconds);
4612     return _PyTime_AsNanosecondsObject(ts);
4613 }
4614 
4615 static PyObject *
test_pytime_fromsecondsobject(PyObject * self,PyObject * args)4616 test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
4617 {
4618     PyObject *obj;
4619     int round;
4620     _PyTime_t ts;
4621 
4622     if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
4623         return NULL;
4624     if (check_time_rounding(round) < 0)
4625         return NULL;
4626     if (_PyTime_FromSecondsObject(&ts, obj, round) == -1)
4627         return NULL;
4628     return _PyTime_AsNanosecondsObject(ts);
4629 }
4630 
4631 static PyObject *
test_pytime_assecondsdouble(PyObject * self,PyObject * args)4632 test_pytime_assecondsdouble(PyObject *self, PyObject *args)
4633 {
4634     PyObject *obj;
4635     _PyTime_t ts;
4636     double d;
4637 
4638     if (!PyArg_ParseTuple(args, "O", &obj)) {
4639         return NULL;
4640     }
4641     if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) {
4642         return NULL;
4643     }
4644     d = _PyTime_AsSecondsDouble(ts);
4645     return PyFloat_FromDouble(d);
4646 }
4647 
4648 static PyObject *
test_PyTime_AsTimeval(PyObject * self,PyObject * args)4649 test_PyTime_AsTimeval(PyObject *self, PyObject *args)
4650 {
4651     PyObject *obj;
4652     int round;
4653     _PyTime_t t;
4654     struct timeval tv;
4655     PyObject *seconds;
4656 
4657     if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
4658         return NULL;
4659     if (check_time_rounding(round) < 0) {
4660         return NULL;
4661     }
4662     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
4663         return NULL;
4664     }
4665     if (_PyTime_AsTimeval(t, &tv, round) < 0) {
4666         return NULL;
4667     }
4668 
4669     seconds = PyLong_FromLongLong(tv.tv_sec);
4670     if (seconds == NULL) {
4671         return NULL;
4672     }
4673     return Py_BuildValue("Nl", seconds, tv.tv_usec);
4674 }
4675 
4676 #ifdef HAVE_CLOCK_GETTIME
4677 static PyObject *
test_PyTime_AsTimespec(PyObject * self,PyObject * args)4678 test_PyTime_AsTimespec(PyObject *self, PyObject *args)
4679 {
4680     PyObject *obj;
4681     _PyTime_t t;
4682     struct timespec ts;
4683 
4684     if (!PyArg_ParseTuple(args, "O", &obj)) {
4685         return NULL;
4686     }
4687     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
4688         return NULL;
4689     }
4690     if (_PyTime_AsTimespec(t, &ts) == -1) {
4691         return NULL;
4692     }
4693     return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
4694 }
4695 #endif
4696 
4697 static PyObject *
test_PyTime_AsMilliseconds(PyObject * self,PyObject * args)4698 test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
4699 {
4700     PyObject *obj;
4701     int round;
4702     _PyTime_t t, ms;
4703 
4704     if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
4705         return NULL;
4706     }
4707     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
4708         return NULL;
4709     }
4710     if (check_time_rounding(round) < 0) {
4711         return NULL;
4712     }
4713     ms = _PyTime_AsMilliseconds(t, round);
4714     /* This conversion rely on the fact that _PyTime_t is a number of
4715        nanoseconds */
4716     return _PyTime_AsNanosecondsObject(ms);
4717 }
4718 
4719 static PyObject *
test_PyTime_AsMicroseconds(PyObject * self,PyObject * args)4720 test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
4721 {
4722     PyObject *obj;
4723     int round;
4724     _PyTime_t t, ms;
4725 
4726     if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
4727         return NULL;
4728     if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
4729         return NULL;
4730     }
4731     if (check_time_rounding(round) < 0) {
4732         return NULL;
4733     }
4734     ms = _PyTime_AsMicroseconds(t, round);
4735     /* This conversion rely on the fact that _PyTime_t is a number of
4736        nanoseconds */
4737     return _PyTime_AsNanosecondsObject(ms);
4738 }
4739 
4740 static PyObject*
pymem_buffer_overflow(PyObject * self,PyObject * args)4741 pymem_buffer_overflow(PyObject *self, PyObject *args)
4742 {
4743     char *buffer;
4744 
4745     /* Deliberate buffer overflow to check that PyMem_Free() detects
4746        the overflow when debug hooks are installed. */
4747     buffer = PyMem_Malloc(16);
4748     if (buffer == NULL) {
4749         PyErr_NoMemory();
4750         return NULL;
4751     }
4752     buffer[16] = 'x';
4753     PyMem_Free(buffer);
4754 
4755     Py_RETURN_NONE;
4756 }
4757 
4758 static PyObject*
pymem_api_misuse(PyObject * self,PyObject * args)4759 pymem_api_misuse(PyObject *self, PyObject *args)
4760 {
4761     char *buffer;
4762 
4763     /* Deliberate misusage of Python allocators:
4764        allococate with PyMem but release with PyMem_Raw. */
4765     buffer = PyMem_Malloc(16);
4766     PyMem_RawFree(buffer);
4767 
4768     Py_RETURN_NONE;
4769 }
4770 
4771 static PyObject*
pymem_malloc_without_gil(PyObject * self,PyObject * args)4772 pymem_malloc_without_gil(PyObject *self, PyObject *args)
4773 {
4774     char *buffer;
4775 
4776     /* Deliberate bug to test debug hooks on Python memory allocators:
4777        call PyMem_Malloc() without holding the GIL */
4778     Py_BEGIN_ALLOW_THREADS
4779     buffer = PyMem_Malloc(10);
4780     Py_END_ALLOW_THREADS
4781 
4782     PyMem_Free(buffer);
4783 
4784     Py_RETURN_NONE;
4785 }
4786 
4787 
4788 static PyObject*
test_pymem_getallocatorsname(PyObject * self,PyObject * args)4789 test_pymem_getallocatorsname(PyObject *self, PyObject *args)
4790 {
4791     const char *name = _PyMem_GetCurrentAllocatorName();
4792     if (name == NULL) {
4793         PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name");
4794         return NULL;
4795     }
4796     return PyUnicode_FromString(name);
4797 }
4798 
4799 
4800 static PyObject*
test_pyobject_is_freed(const char * test_name,PyObject * op)4801 test_pyobject_is_freed(const char *test_name, PyObject *op)
4802 {
4803     if (!_PyObject_IsFreed(op)) {
4804         return raiseTestError(test_name, "object is not seen as freed");
4805     }
4806     Py_RETURN_NONE;
4807 }
4808 
4809 
4810 static PyObject*
check_pyobject_null_is_freed(PyObject * self,PyObject * Py_UNUSED (args))4811 check_pyobject_null_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
4812 {
4813     PyObject *op = NULL;
4814     return test_pyobject_is_freed("check_pyobject_null_is_freed", op);
4815 }
4816 
4817 
4818 static PyObject*
check_pyobject_uninitialized_is_freed(PyObject * self,PyObject * Py_UNUSED (args))4819 check_pyobject_uninitialized_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
4820 {
4821     PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject));
4822     if (op == NULL) {
4823         return NULL;
4824     }
4825     /* Initialize reference count to avoid early crash in ceval or GC */
4826     Py_SET_REFCNT(op, 1);
4827     /* object fields like ob_type are uninitialized! */
4828     return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op);
4829 }
4830 
4831 
4832 static PyObject*
check_pyobject_forbidden_bytes_is_freed(PyObject * self,PyObject * Py_UNUSED (args))4833 check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
4834 {
4835     /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */
4836     PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type));
4837     if (op == NULL) {
4838         return NULL;
4839     }
4840     /* Initialize reference count to avoid early crash in ceval or GC */
4841     Py_SET_REFCNT(op, 1);
4842     /* ob_type field is after the memory block: part of "forbidden bytes"
4843        when using debug hooks on memory allocators! */
4844     return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op);
4845 }
4846 
4847 
4848 static PyObject*
check_pyobject_freed_is_freed(PyObject * self,PyObject * Py_UNUSED (args))4849 check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
4850 {
4851     /* This test would fail if run with the address sanitizer */
4852 #ifdef _Py_ADDRESS_SANITIZER
4853     Py_RETURN_NONE;
4854 #else
4855     PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
4856     if (op == NULL) {
4857         return NULL;
4858     }
4859     Py_TYPE(op)->tp_dealloc(op);
4860     /* Reset reference count to avoid early crash in ceval or GC */
4861     Py_SET_REFCNT(op, 1);
4862     /* object memory is freed! */
4863     return test_pyobject_is_freed("check_pyobject_freed_is_freed", op);
4864 #endif
4865 }
4866 
4867 
4868 static PyObject*
pyobject_malloc_without_gil(PyObject * self,PyObject * args)4869 pyobject_malloc_without_gil(PyObject *self, PyObject *args)
4870 {
4871     char *buffer;
4872 
4873     /* Deliberate bug to test debug hooks on Python memory allocators:
4874        call PyObject_Malloc() without holding the GIL */
4875     Py_BEGIN_ALLOW_THREADS
4876     buffer = PyObject_Malloc(10);
4877     Py_END_ALLOW_THREADS
4878 
4879     PyObject_Free(buffer);
4880 
4881     Py_RETURN_NONE;
4882 }
4883 
4884 static PyObject *
tracemalloc_track(PyObject * self,PyObject * args)4885 tracemalloc_track(PyObject *self, PyObject *args)
4886 {
4887     unsigned int domain;
4888     PyObject *ptr_obj;
4889     void *ptr;
4890     Py_ssize_t size;
4891     int release_gil = 0;
4892     int res;
4893 
4894     if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil))
4895         return NULL;
4896     ptr = PyLong_AsVoidPtr(ptr_obj);
4897     if (PyErr_Occurred())
4898         return NULL;
4899 
4900     if (release_gil) {
4901         Py_BEGIN_ALLOW_THREADS
4902         res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
4903         Py_END_ALLOW_THREADS
4904     }
4905     else {
4906         res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
4907     }
4908 
4909     if (res < 0) {
4910         PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error");
4911         return NULL;
4912     }
4913 
4914     Py_RETURN_NONE;
4915 }
4916 
4917 static PyObject *
tracemalloc_untrack(PyObject * self,PyObject * args)4918 tracemalloc_untrack(PyObject *self, PyObject *args)
4919 {
4920     unsigned int domain;
4921     PyObject *ptr_obj;
4922     void *ptr;
4923     int res;
4924 
4925     if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4926         return NULL;
4927     ptr = PyLong_AsVoidPtr(ptr_obj);
4928     if (PyErr_Occurred())
4929         return NULL;
4930 
4931     res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr);
4932     if (res < 0) {
4933         PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error");
4934         return NULL;
4935     }
4936 
4937     Py_RETURN_NONE;
4938 }
4939 
4940 static PyObject *
tracemalloc_get_traceback(PyObject * self,PyObject * args)4941 tracemalloc_get_traceback(PyObject *self, PyObject *args)
4942 {
4943     unsigned int domain;
4944     PyObject *ptr_obj;
4945     void *ptr;
4946 
4947     if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4948         return NULL;
4949     ptr = PyLong_AsVoidPtr(ptr_obj);
4950     if (PyErr_Occurred())
4951         return NULL;
4952 
4953     return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
4954 }
4955 
4956 static PyObject *
dict_get_version(PyObject * self,PyObject * args)4957 dict_get_version(PyObject *self, PyObject *args)
4958 {
4959     PyDictObject *dict;
4960     uint64_t version;
4961 
4962     if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict))
4963         return NULL;
4964 
4965     version = dict->ma_version_tag;
4966 
4967     Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(version));
4968     return PyLong_FromUnsignedLongLong((unsigned long long)version);
4969 }
4970 
4971 
4972 static PyObject *
raise_SIGINT_then_send_None(PyObject * self,PyObject * args)4973 raise_SIGINT_then_send_None(PyObject *self, PyObject *args)
4974 {
4975     _Py_IDENTIFIER(send);
4976     PyGenObject *gen;
4977 
4978     if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen))
4979         return NULL;
4980 
4981     /* This is used in a test to check what happens if a signal arrives just
4982        as we're in the process of entering a yield from chain (see
4983        bpo-30039).
4984 
4985        Needs to be done in C, because:
4986        - we don't have a Python wrapper for raise()
4987        - we need to make sure that the Python-level signal handler doesn't run
4988          *before* we enter the generator frame, which is impossible in Python
4989          because we check for signals before every bytecode operation.
4990      */
4991     raise(SIGINT);
4992     return _PyObject_CallMethodIdOneArg((PyObject *)gen, &PyId_send, Py_None);
4993 }
4994 
4995 
4996 static int
fastcall_args(PyObject * args,PyObject *** stack,Py_ssize_t * nargs)4997 fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs)
4998 {
4999     if (args == Py_None) {
5000         *stack = NULL;
5001         *nargs = 0;
5002     }
5003     else if (PyTuple_Check(args)) {
5004         *stack = ((PyTupleObject *)args)->ob_item;
5005         *nargs = PyTuple_GET_SIZE(args);
5006     }
5007     else {
5008         PyErr_SetString(PyExc_TypeError, "args must be None or a tuple");
5009         return -1;
5010     }
5011     return 0;
5012 }
5013 
5014 
5015 static PyObject *
test_pyobject_fastcall(PyObject * self,PyObject * args)5016 test_pyobject_fastcall(PyObject *self, PyObject *args)
5017 {
5018     PyObject *func, *func_args;
5019     PyObject **stack;
5020     Py_ssize_t nargs;
5021 
5022     if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) {
5023         return NULL;
5024     }
5025 
5026     if (fastcall_args(func_args, &stack, &nargs) < 0) {
5027         return NULL;
5028     }
5029     return _PyObject_FastCall(func, stack, nargs);
5030 }
5031 
5032 
5033 static PyObject *
test_pyobject_fastcalldict(PyObject * self,PyObject * args)5034 test_pyobject_fastcalldict(PyObject *self, PyObject *args)
5035 {
5036     PyObject *func, *func_args, *kwargs;
5037     PyObject **stack;
5038     Py_ssize_t nargs;
5039 
5040     if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) {
5041         return NULL;
5042     }
5043 
5044     if (fastcall_args(func_args, &stack, &nargs) < 0) {
5045         return NULL;
5046     }
5047 
5048     if (kwargs == Py_None) {
5049         kwargs = NULL;
5050     }
5051     else if (!PyDict_Check(kwargs)) {
5052         PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict");
5053         return NULL;
5054     }
5055 
5056     return PyObject_VectorcallDict(func, stack, nargs, kwargs);
5057 }
5058 
5059 
5060 static PyObject *
test_pyobject_vectorcall(PyObject * self,PyObject * args)5061 test_pyobject_vectorcall(PyObject *self, PyObject *args)
5062 {
5063     PyObject *func, *func_args, *kwnames = NULL;
5064     PyObject **stack;
5065     Py_ssize_t nargs, nkw;
5066 
5067     if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) {
5068         return NULL;
5069     }
5070 
5071     if (fastcall_args(func_args, &stack, &nargs) < 0) {
5072         return NULL;
5073     }
5074 
5075     if (kwnames == Py_None) {
5076         kwnames = NULL;
5077     }
5078     else if (PyTuple_Check(kwnames)) {
5079         nkw = PyTuple_GET_SIZE(kwnames);
5080         if (nargs < nkw) {
5081             PyErr_SetString(PyExc_ValueError, "kwnames longer than args");
5082             return NULL;
5083         }
5084         nargs -= nkw;
5085     }
5086     else {
5087         PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple");
5088         return NULL;
5089     }
5090     return PyObject_Vectorcall(func, stack, nargs, kwnames);
5091 }
5092 
5093 
5094 static PyObject *
test_pyvectorcall_call(PyObject * self,PyObject * args)5095 test_pyvectorcall_call(PyObject *self, PyObject *args)
5096 {
5097     PyObject *func;
5098     PyObject *argstuple;
5099     PyObject *kwargs = NULL;
5100 
5101     if (!PyArg_ParseTuple(args, "OO|O", &func, &argstuple, &kwargs)) {
5102         return NULL;
5103     }
5104 
5105     if (!PyTuple_Check(argstuple)) {
5106         PyErr_SetString(PyExc_TypeError, "args must be a tuple");
5107         return NULL;
5108     }
5109     if (kwargs != NULL && !PyDict_Check(kwargs)) {
5110         PyErr_SetString(PyExc_TypeError, "kwargs must be a dict");
5111         return NULL;
5112     }
5113 
5114     return PyVectorcall_Call(func, argstuple, kwargs);
5115 }
5116 
5117 
5118 static PyObject*
stack_pointer(PyObject * self,PyObject * args)5119 stack_pointer(PyObject *self, PyObject *args)
5120 {
5121     int v = 5;
5122     return PyLong_FromVoidPtr(&v);
5123 }
5124 
5125 
5126 #ifdef W_STOPCODE
5127 static PyObject*
py_w_stopcode(PyObject * self,PyObject * args)5128 py_w_stopcode(PyObject *self, PyObject *args)
5129 {
5130     int sig, status;
5131     if (!PyArg_ParseTuple(args, "i", &sig)) {
5132         return NULL;
5133     }
5134     status = W_STOPCODE(sig);
5135     return PyLong_FromLong(status);
5136 }
5137 #endif
5138 
5139 
5140 static PyObject *
get_mapping_keys(PyObject * self,PyObject * obj)5141 get_mapping_keys(PyObject* self, PyObject *obj)
5142 {
5143     return PyMapping_Keys(obj);
5144 }
5145 
5146 static PyObject *
get_mapping_values(PyObject * self,PyObject * obj)5147 get_mapping_values(PyObject* self, PyObject *obj)
5148 {
5149     return PyMapping_Values(obj);
5150 }
5151 
5152 static PyObject *
get_mapping_items(PyObject * self,PyObject * obj)5153 get_mapping_items(PyObject* self, PyObject *obj)
5154 {
5155     return PyMapping_Items(obj);
5156 }
5157 
5158 
5159 static PyObject *
test_pythread_tss_key_state(PyObject * self,PyObject * args)5160 test_pythread_tss_key_state(PyObject *self, PyObject *args)
5161 {
5162     Py_tss_t tss_key = Py_tss_NEEDS_INIT;
5163     if (PyThread_tss_is_created(&tss_key)) {
5164         return raiseTestError("test_pythread_tss_key_state",
5165                               "TSS key not in an uninitialized state at "
5166                               "creation time");
5167     }
5168     if (PyThread_tss_create(&tss_key) != 0) {
5169         PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed");
5170         return NULL;
5171     }
5172     if (!PyThread_tss_is_created(&tss_key)) {
5173         return raiseTestError("test_pythread_tss_key_state",
5174                               "PyThread_tss_create succeeded, "
5175                               "but with TSS key in an uninitialized state");
5176     }
5177     if (PyThread_tss_create(&tss_key) != 0) {
5178         return raiseTestError("test_pythread_tss_key_state",
5179                               "PyThread_tss_create unsuccessful with "
5180                               "an already initialized key");
5181     }
5182 #define CHECK_TSS_API(expr) \
5183         (void)(expr); \
5184         if (!PyThread_tss_is_created(&tss_key)) { \
5185             return raiseTestError("test_pythread_tss_key_state", \
5186                                   "TSS key initialization state was not " \
5187                                   "preserved after calling " #expr); }
5188     CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL));
5189     CHECK_TSS_API(PyThread_tss_get(&tss_key));
5190 #undef CHECK_TSS_API
5191     PyThread_tss_delete(&tss_key);
5192     if (PyThread_tss_is_created(&tss_key)) {
5193         return raiseTestError("test_pythread_tss_key_state",
5194                               "PyThread_tss_delete called, but did not "
5195                               "set the key state to uninitialized");
5196     }
5197 
5198     Py_tss_t *ptr_key = PyThread_tss_alloc();
5199     if (ptr_key == NULL) {
5200         PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed");
5201         return NULL;
5202     }
5203     if (PyThread_tss_is_created(ptr_key)) {
5204         return raiseTestError("test_pythread_tss_key_state",
5205                               "TSS key not in an uninitialized state at "
5206                               "allocation time");
5207     }
5208     PyThread_tss_free(ptr_key);
5209     ptr_key = NULL;
5210     Py_RETURN_NONE;
5211 }
5212 
5213 
5214 static PyObject*
new_hamt(PyObject * self,PyObject * args)5215 new_hamt(PyObject *self, PyObject *args)
5216 {
5217     return _PyContext_NewHamtForTests();
5218 }
5219 
5220 
5221 /* def bad_get(self, obj, cls):
5222        cls()
5223        return repr(self)
5224 */
5225 static PyObject*
bad_get(PyObject * module,PyObject * const * args,Py_ssize_t nargs)5226 bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
5227 {
5228     PyObject *self, *obj, *cls;
5229     if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) {
5230         return NULL;
5231     }
5232 
5233     PyObject *res = _PyObject_CallNoArg(cls);
5234     if (res == NULL) {
5235         return NULL;
5236     }
5237     Py_DECREF(res);
5238 
5239     return PyObject_Repr(self);
5240 }
5241 
5242 
5243 static PyObject *
encode_locale_ex(PyObject * self,PyObject * args)5244 encode_locale_ex(PyObject *self, PyObject *args)
5245 {
5246     PyObject *unicode;
5247     int current_locale = 0;
5248     wchar_t *wstr;
5249     PyObject *res = NULL;
5250     const char *errors = NULL;
5251 
5252     if (!PyArg_ParseTuple(args, "U|is", &unicode, &current_locale, &errors)) {
5253         return NULL;
5254     }
5255     wstr = PyUnicode_AsWideCharString(unicode, NULL);
5256     if (wstr == NULL) {
5257         return NULL;
5258     }
5259     _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
5260 
5261     char *str = NULL;
5262     size_t error_pos;
5263     const char *reason = NULL;
5264     int ret = _Py_EncodeLocaleEx(wstr,
5265                                  &str, &error_pos, &reason,
5266                                  current_locale, error_handler);
5267     PyMem_Free(wstr);
5268 
5269     switch(ret) {
5270     case 0:
5271         res = PyBytes_FromString(str);
5272         PyMem_RawFree(str);
5273         break;
5274     case -1:
5275         PyErr_NoMemory();
5276         break;
5277     case -2:
5278         PyErr_Format(PyExc_RuntimeError, "encode error: pos=%zu, reason=%s",
5279                      error_pos, reason);
5280         break;
5281     case -3:
5282         PyErr_SetString(PyExc_ValueError, "unsupported error handler");
5283         break;
5284     default:
5285         PyErr_SetString(PyExc_ValueError, "unknown error code");
5286         break;
5287     }
5288     return res;
5289 }
5290 
5291 
5292 static PyObject *
decode_locale_ex(PyObject * self,PyObject * args)5293 decode_locale_ex(PyObject *self, PyObject *args)
5294 {
5295     char *str;
5296     int current_locale = 0;
5297     PyObject *res = NULL;
5298     const char *errors = NULL;
5299 
5300     if (!PyArg_ParseTuple(args, "y|is", &str, &current_locale, &errors)) {
5301         return NULL;
5302     }
5303     _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
5304 
5305     wchar_t *wstr = NULL;
5306     size_t wlen = 0;
5307     const char *reason = NULL;
5308     int ret = _Py_DecodeLocaleEx(str,
5309                                  &wstr, &wlen, &reason,
5310                                  current_locale, error_handler);
5311 
5312     switch(ret) {
5313     case 0:
5314         res = PyUnicode_FromWideChar(wstr, wlen);
5315         PyMem_RawFree(wstr);
5316         break;
5317     case -1:
5318         PyErr_NoMemory();
5319         break;
5320     case -2:
5321         PyErr_Format(PyExc_RuntimeError, "decode error: pos=%zu, reason=%s",
5322                      wlen, reason);
5323         break;
5324     case -3:
5325         PyErr_SetString(PyExc_ValueError, "unsupported error handler");
5326         break;
5327     default:
5328         PyErr_SetString(PyExc_ValueError, "unknown error code");
5329         break;
5330     }
5331     return res;
5332 }
5333 
5334 
5335 #ifdef Py_REF_DEBUG
5336 static PyObject *
negative_refcount(PyObject * self,PyObject * Py_UNUSED (args))5337 negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
5338 {
5339     PyObject *obj = PyUnicode_FromString("negative_refcount");
5340     if (obj == NULL) {
5341         return NULL;
5342     }
5343     assert(Py_REFCNT(obj) == 1);
5344 
5345     Py_SET_REFCNT(obj,  0);
5346     /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */
5347     Py_DECREF(obj);
5348 
5349     Py_RETURN_NONE;
5350 }
5351 #endif
5352 
5353 
5354 static PyObject*
test_write_unraisable_exc(PyObject * self,PyObject * args)5355 test_write_unraisable_exc(PyObject *self, PyObject *args)
5356 {
5357     PyObject *exc, *err_msg, *obj;
5358     if (!PyArg_ParseTuple(args, "OOO", &exc, &err_msg, &obj)) {
5359         return NULL;
5360     }
5361 
5362     const char *err_msg_utf8;
5363     if (err_msg != Py_None) {
5364         err_msg_utf8 = PyUnicode_AsUTF8(err_msg);
5365         if (err_msg_utf8 == NULL) {
5366             return NULL;
5367         }
5368     }
5369     else {
5370         err_msg_utf8 = NULL;
5371     }
5372 
5373     PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
5374     _PyErr_WriteUnraisableMsg(err_msg_utf8, obj);
5375     Py_RETURN_NONE;
5376 }
5377 
5378 
5379 static PyObject *
sequence_getitem(PyObject * self,PyObject * args)5380 sequence_getitem(PyObject *self, PyObject *args)
5381 {
5382     PyObject *seq;
5383     Py_ssize_t i;
5384     if (!PyArg_ParseTuple(args, "On", &seq, &i)) {
5385         return NULL;
5386     }
5387     return PySequence_GetItem(seq, i);
5388 }
5389 
5390 
5391 /* Functions for testing C calling conventions (METH_*) are named meth_*,
5392  * e.g. "meth_varargs" for METH_VARARGS.
5393  *
5394  * They all return a tuple of their C-level arguments, with None instead
5395  * of NULL and Python tuples instead of C arrays.
5396  */
5397 
5398 
5399 static PyObject*
_null_to_none(PyObject * obj)5400 _null_to_none(PyObject* obj)
5401 {
5402     if (obj == NULL) {
5403         Py_RETURN_NONE;
5404     }
5405     Py_INCREF(obj);
5406     return obj;
5407 }
5408 
5409 static PyObject*
meth_varargs(PyObject * self,PyObject * args)5410 meth_varargs(PyObject* self, PyObject* args)
5411 {
5412     return Py_BuildValue("NO", _null_to_none(self), args);
5413 }
5414 
5415 static PyObject*
meth_varargs_keywords(PyObject * self,PyObject * args,PyObject * kwargs)5416 meth_varargs_keywords(PyObject* self, PyObject* args, PyObject* kwargs)
5417 {
5418     return Py_BuildValue("NON", _null_to_none(self), args, _null_to_none(kwargs));
5419 }
5420 
5421 static PyObject*
meth_o(PyObject * self,PyObject * obj)5422 meth_o(PyObject* self, PyObject* obj)
5423 {
5424     return Py_BuildValue("NO", _null_to_none(self), obj);
5425 }
5426 
5427 static PyObject*
meth_noargs(PyObject * self,PyObject * ignored)5428 meth_noargs(PyObject* self, PyObject* ignored)
5429 {
5430     return _null_to_none(self);
5431 }
5432 
5433 static PyObject*
_fastcall_to_tuple(PyObject * const * args,Py_ssize_t nargs)5434 _fastcall_to_tuple(PyObject* const* args, Py_ssize_t nargs)
5435 {
5436     PyObject *tuple = PyTuple_New(nargs);
5437     if (tuple == NULL) {
5438         return NULL;
5439     }
5440     for (Py_ssize_t i=0; i < nargs; i++) {
5441         Py_INCREF(args[i]);
5442         PyTuple_SET_ITEM(tuple, i, args[i]);
5443     }
5444     return tuple;
5445 }
5446 
5447 static PyObject*
meth_fastcall(PyObject * self,PyObject * const * args,Py_ssize_t nargs)5448 meth_fastcall(PyObject* self, PyObject* const* args, Py_ssize_t nargs)
5449 {
5450     return Py_BuildValue(
5451         "NN", _null_to_none(self), _fastcall_to_tuple(args, nargs)
5452     );
5453 }
5454 
5455 static PyObject*
meth_fastcall_keywords(PyObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwargs)5456 meth_fastcall_keywords(PyObject* self, PyObject* const* args,
5457                        Py_ssize_t nargs, PyObject* kwargs)
5458 {
5459     PyObject *pyargs = _fastcall_to_tuple(args, nargs);
5460     if (pyargs == NULL) {
5461         return NULL;
5462     }
5463     PyObject *pykwargs = PyObject_Vectorcall((PyObject*)&PyDict_Type,
5464                                               args + nargs, 0, kwargs);
5465     return Py_BuildValue("NNN", _null_to_none(self), pyargs, pykwargs);
5466 }
5467 
5468 
5469 static PyObject*
pynumber_tobase(PyObject * module,PyObject * args)5470 pynumber_tobase(PyObject *module, PyObject *args)
5471 {
5472     PyObject *obj;
5473     int base;
5474     if (!PyArg_ParseTuple(args, "Oi:pynumber_tobase",
5475                           &obj, &base)) {
5476         return NULL;
5477     }
5478     return PyNumber_ToBase(obj, base);
5479 }
5480 
5481 
5482 static PyObject*
test_set_type_size(PyObject * self,PyObject * Py_UNUSED (ignored))5483 test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
5484 {
5485     PyObject *obj = PyList_New(0);
5486     if (obj == NULL) {
5487         return NULL;
5488     }
5489 
5490     // Ensure that following tests don't modify the object,
5491     // to ensure that Py_DECREF() will not crash.
5492     assert(Py_TYPE(obj) == &PyList_Type);
5493     assert(Py_SIZE(obj) == 0);
5494 
5495     // bpo-39573: Check that Py_TYPE() and Py_SIZE() can be used
5496     // as l-values to set an object type and size.
5497     Py_TYPE(obj) = &PyList_Type;
5498     Py_SIZE(obj) = 0;
5499 
5500     Py_DECREF(obj);
5501     Py_RETURN_NONE;
5502 }
5503 
5504 
5505 #define TEST_REFCOUNT() \
5506     do { \
5507         PyObject *obj = PyList_New(0); \
5508         if (obj == NULL) { \
5509             return NULL; \
5510         } \
5511         assert(Py_REFCNT(obj) == 1); \
5512         \
5513         /* test Py_NewRef() */ \
5514         PyObject *ref = Py_NewRef(obj); \
5515         assert(ref == obj); \
5516         assert(Py_REFCNT(obj) == 2); \
5517         Py_DECREF(ref); \
5518         \
5519         /* test Py_XNewRef() */ \
5520         PyObject *xref = Py_XNewRef(obj); \
5521         assert(xref == obj); \
5522         assert(Py_REFCNT(obj) == 2); \
5523         Py_DECREF(xref); \
5524         \
5525         assert(Py_XNewRef(NULL) == NULL); \
5526         \
5527         Py_DECREF(obj); \
5528         Py_RETURN_NONE; \
5529     } while (0) \
5530 
5531 
5532 // Test Py_NewRef() and Py_XNewRef() macros
5533 static PyObject*
test_refcount_macros(PyObject * self,PyObject * Py_UNUSED (ignored))5534 test_refcount_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
5535 {
5536     TEST_REFCOUNT();
5537 }
5538 
5539 #undef Py_NewRef
5540 #undef Py_XNewRef
5541 
5542 // Test Py_NewRef() and Py_XNewRef() functions, after undefining macros.
5543 static PyObject*
test_refcount_funcs(PyObject * self,PyObject * Py_UNUSED (ignored))5544 test_refcount_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
5545 {
5546     TEST_REFCOUNT();
5547 }
5548 
5549 
5550 // Test Py_Is() function
5551 #define TEST_PY_IS() \
5552     do { \
5553         PyObject *o_none = Py_None; \
5554         PyObject *o_true = Py_True; \
5555         PyObject *o_false = Py_False; \
5556         PyObject *obj = PyList_New(0); \
5557         if (obj == NULL) { \
5558             return NULL; \
5559         } \
5560         \
5561         /* test Py_Is() */ \
5562         assert(Py_Is(obj, obj)); \
5563         assert(!Py_Is(obj, o_none)); \
5564         \
5565         /* test Py_None */ \
5566         assert(Py_Is(o_none, o_none)); \
5567         assert(!Py_Is(obj, o_none)); \
5568         \
5569         /* test Py_True */ \
5570         assert(Py_Is(o_true, o_true)); \
5571         assert(!Py_Is(o_false, o_true)); \
5572         assert(!Py_Is(obj, o_true)); \
5573         \
5574         /* test Py_False */ \
5575         assert(Py_Is(o_false, o_false)); \
5576         assert(!Py_Is(o_true, o_false)); \
5577         assert(!Py_Is(obj, o_false)); \
5578         \
5579         Py_DECREF(obj); \
5580         Py_RETURN_NONE; \
5581     } while (0)
5582 
5583 // Test Py_Is() macro
5584 static PyObject*
test_py_is_macros(PyObject * self,PyObject * Py_UNUSED (ignored))5585 test_py_is_macros(PyObject *self, PyObject *Py_UNUSED(ignored))
5586 {
5587     TEST_PY_IS();
5588 }
5589 
5590 #undef Py_Is
5591 
5592 // Test Py_Is() function, after undefining its macro.
5593 static PyObject*
test_py_is_funcs(PyObject * self,PyObject * Py_UNUSED (ignored))5594 test_py_is_funcs(PyObject *self, PyObject *Py_UNUSED(ignored))
5595 {
5596     TEST_PY_IS();
5597 }
5598 
5599 
5600 static PyObject *
test_fatal_error(PyObject * self,PyObject * args)5601 test_fatal_error(PyObject *self, PyObject *args)
5602 {
5603     char *message;
5604     int release_gil = 0;
5605     if (!PyArg_ParseTuple(args, "y|i:fatal_error", &message, &release_gil))
5606         return NULL;
5607     if (release_gil) {
5608         Py_BEGIN_ALLOW_THREADS
5609         Py_FatalError(message);
5610         Py_END_ALLOW_THREADS
5611     }
5612     else {
5613         Py_FatalError(message);
5614     }
5615     // Py_FatalError() does not return, but exits the process.
5616     Py_RETURN_NONE;
5617 }
5618 
5619 
5620 static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
5621 static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
5622 
5623 static PyMethodDef TestMethods[] = {
5624     {"raise_exception",         raise_exception,                 METH_VARARGS},
5625     {"raise_memoryerror",       raise_memoryerror,               METH_NOARGS},
5626     {"set_errno",               set_errno,                       METH_VARARGS},
5627     {"test_config",             test_config,                     METH_NOARGS},
5628     {"test_sizeof_c_types",     test_sizeof_c_types,             METH_NOARGS},
5629     {"test_datetime_capi",      test_datetime_capi,              METH_NOARGS},
5630     {"datetime_check_date",     datetime_check_date,             METH_VARARGS},
5631     {"datetime_check_time",     datetime_check_time,             METH_VARARGS},
5632     {"datetime_check_datetime",     datetime_check_datetime,     METH_VARARGS},
5633     {"datetime_check_delta",     datetime_check_delta,           METH_VARARGS},
5634     {"datetime_check_tzinfo",     datetime_check_tzinfo,         METH_VARARGS},
5635     {"make_timezones_capi",     make_timezones_capi,             METH_NOARGS},
5636     {"get_timezones_offset_zero",   get_timezones_offset_zero,   METH_NOARGS},
5637     {"get_timezone_utc_capi",    get_timezone_utc_capi,          METH_VARARGS},
5638     {"get_date_fromdate",        get_date_fromdate,              METH_VARARGS},
5639     {"get_datetime_fromdateandtime", get_datetime_fromdateandtime, METH_VARARGS},
5640     {"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS},
5641     {"get_time_fromtime",        get_time_fromtime,              METH_VARARGS},
5642     {"get_time_fromtimeandfold", get_time_fromtimeandfold,       METH_VARARGS},
5643     {"get_delta_fromdsu",        get_delta_fromdsu,              METH_VARARGS},
5644     {"get_date_fromtimestamp",   get_date_fromtimestamp,         METH_VARARGS},
5645     {"get_datetime_fromtimestamp", get_datetime_fromtimestamp,   METH_VARARGS},
5646     {"PyDateTime_GET",             test_PyDateTime_GET,           METH_O},
5647     {"PyDateTime_DATE_GET",        test_PyDateTime_DATE_GET,      METH_O},
5648     {"PyDateTime_TIME_GET",        test_PyDateTime_TIME_GET,      METH_O},
5649     {"PyDateTime_DELTA_GET",       test_PyDateTime_DELTA_GET,     METH_O},
5650     {"test_gc_control",         test_gc_control,                 METH_NOARGS},
5651     {"test_list_api",           test_list_api,                   METH_NOARGS},
5652     {"test_dict_iteration",     test_dict_iteration,             METH_NOARGS},
5653     {"dict_getitem_knownhash",  dict_getitem_knownhash,          METH_VARARGS},
5654     {"dict_hassplittable",      dict_hassplittable,              METH_O},
5655     {"test_lazy_hash_inheritance",      test_lazy_hash_inheritance,METH_NOARGS},
5656     {"test_long_api",           test_long_api,                   METH_NOARGS},
5657     {"test_xincref_doesnt_leak",test_xincref_doesnt_leak,        METH_NOARGS},
5658     {"test_incref_doesnt_leak", test_incref_doesnt_leak,         METH_NOARGS},
5659     {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak,        METH_NOARGS},
5660     {"test_decref_doesnt_leak", test_decref_doesnt_leak,         METH_NOARGS},
5661     {"test_structseq_newtype_doesnt_leak",
5662         test_structseq_newtype_doesnt_leak, METH_NOARGS},
5663     {"test_structseq_newtype_null_descr_doc",
5664         test_structseq_newtype_null_descr_doc, METH_NOARGS},
5665     {"test_incref_decref_API",  test_incref_decref_API,          METH_NOARGS},
5666     {"test_long_and_overflow",  test_long_and_overflow,          METH_NOARGS},
5667     {"test_long_as_double",     test_long_as_double,             METH_NOARGS},
5668     {"test_long_as_size_t",     test_long_as_size_t,             METH_NOARGS},
5669     {"test_long_as_unsigned_long_long_mask",
5670         test_long_as_unsigned_long_long_mask, METH_NOARGS},
5671     {"test_long_numbits",       test_long_numbits,               METH_NOARGS},
5672     {"test_k_code",             test_k_code,                     METH_NOARGS},
5673     {"test_empty_argparse",     test_empty_argparse,             METH_NOARGS},
5674     {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
5675     {"pyobject_repr_from_null", pyobject_repr_from_null, METH_NOARGS},
5676     {"pyobject_str_from_null",  pyobject_str_from_null, METH_NOARGS},
5677     {"pyobject_bytes_from_null", pyobject_bytes_from_null, METH_NOARGS},
5678     {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
5679     {"test_with_docstring",     test_with_docstring,             METH_NOARGS,
5680      PyDoc_STR("This is a pretty normal docstring.")},
5681     {"test_string_to_double",   test_string_to_double,           METH_NOARGS},
5682     {"test_unicode_compare_with_ascii", test_unicode_compare_with_ascii,
5683      METH_NOARGS},
5684     {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
5685     {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
5686 #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
5687     {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
5688 #endif
5689     {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
5690     {"PyBuffer_SizeFromFormat",  test_PyBuffer_SizeFromFormat, METH_VARARGS},
5691     {"test_buildvalue_N",       test_buildvalue_N,               METH_NOARGS},
5692     {"test_buildvalue_issue38913", test_buildvalue_issue38913,   METH_NOARGS},
5693     {"get_args",                get_args,                        METH_VARARGS},
5694     {"test_get_statictype_slots", test_get_statictype_slots,     METH_NOARGS},
5695     {"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs,
5696       METH_VARARGS|METH_KEYWORDS},
5697     {"getargs_tuple",           getargs_tuple,                   METH_VARARGS},
5698     {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords,
5699       METH_VARARGS|METH_KEYWORDS},
5700     {"getargs_keyword_only", (PyCFunction)(void(*)(void))getargs_keyword_only,
5701       METH_VARARGS|METH_KEYWORDS},
5702     {"getargs_positional_only_and_keywords",
5703       (PyCFunction)(void(*)(void))getargs_positional_only_and_keywords,
5704       METH_VARARGS|METH_KEYWORDS},
5705     {"getargs_b",               getargs_b,                       METH_VARARGS},
5706     {"getargs_B",               getargs_B,                       METH_VARARGS},
5707     {"getargs_h",               getargs_h,                       METH_VARARGS},
5708     {"getargs_H",               getargs_H,                       METH_VARARGS},
5709     {"getargs_I",               getargs_I,                       METH_VARARGS},
5710     {"getargs_k",               getargs_k,                       METH_VARARGS},
5711     {"getargs_i",               getargs_i,                       METH_VARARGS},
5712     {"getargs_l",               getargs_l,                       METH_VARARGS},
5713     {"getargs_n",               getargs_n,                       METH_VARARGS},
5714     {"getargs_p",               getargs_p,                       METH_VARARGS},
5715     {"getargs_L",               getargs_L,                       METH_VARARGS},
5716     {"getargs_K",               getargs_K,                       METH_VARARGS},
5717     {"test_longlong_api",       test_longlong_api,               METH_NOARGS},
5718     {"test_long_long_and_overflow",test_long_long_and_overflow,  METH_NOARGS},
5719     {"test_L_code",             test_L_code,                     METH_NOARGS},
5720     {"getargs_f",               getargs_f,                       METH_VARARGS},
5721     {"getargs_d",               getargs_d,                       METH_VARARGS},
5722     {"getargs_D",               getargs_D,                       METH_VARARGS},
5723     {"getargs_S",               getargs_S,                       METH_VARARGS},
5724     {"getargs_Y",               getargs_Y,                       METH_VARARGS},
5725     {"getargs_U",               getargs_U,                       METH_VARARGS},
5726     {"getargs_c",               getargs_c,                       METH_VARARGS},
5727     {"getargs_C",               getargs_C,                       METH_VARARGS},
5728     {"getargs_s",               getargs_s,                       METH_VARARGS},
5729     {"getargs_s_star",          getargs_s_star,                  METH_VARARGS},
5730     {"getargs_s_hash",          getargs_s_hash,                  METH_VARARGS},
5731     {"getargs_s_hash_int",      (PyCFunction)(void(*)(void))getargs_s_hash_int,
5732       METH_VARARGS|METH_KEYWORDS},
5733     {"getargs_z",               getargs_z,                       METH_VARARGS},
5734     {"getargs_z_star",          getargs_z_star,                  METH_VARARGS},
5735     {"getargs_z_hash",          getargs_z_hash,                  METH_VARARGS},
5736     {"getargs_y",               getargs_y,                       METH_VARARGS},
5737     {"getargs_y_star",          getargs_y_star,                  METH_VARARGS},
5738     {"getargs_y_hash",          getargs_y_hash,                  METH_VARARGS},
5739     {"getargs_u",               getargs_u,                       METH_VARARGS},
5740     {"getargs_u_hash",          getargs_u_hash,                  METH_VARARGS},
5741     {"getargs_Z",               getargs_Z,                       METH_VARARGS},
5742     {"getargs_Z_hash",          getargs_Z_hash,                  METH_VARARGS},
5743     {"getargs_w_star",          getargs_w_star,                  METH_VARARGS},
5744     {"getargs_es",              getargs_es,                      METH_VARARGS},
5745     {"getargs_et",              getargs_et,                      METH_VARARGS},
5746     {"getargs_es_hash",         getargs_es_hash,                 METH_VARARGS},
5747     {"getargs_et_hash",         getargs_et_hash,                 METH_VARARGS},
5748     {"codec_incrementalencoder",
5749      (PyCFunction)codec_incrementalencoder,                      METH_VARARGS},
5750     {"codec_incrementaldecoder",
5751      (PyCFunction)codec_incrementaldecoder,                      METH_VARARGS},
5752     {"test_s_code",             test_s_code,                     METH_NOARGS},
5753 #if USE_UNICODE_WCHAR_CACHE
5754     {"test_u_code",             test_u_code,                     METH_NOARGS},
5755     {"test_Z_code",             test_Z_code,                     METH_NOARGS},
5756 #endif /* USE_UNICODE_WCHAR_CACHE */
5757     {"test_widechar",           test_widechar,                   METH_NOARGS},
5758     {"unicode_aswidechar",      unicode_aswidechar,              METH_VARARGS},
5759     {"unicode_aswidecharstring",unicode_aswidecharstring,        METH_VARARGS},
5760     {"unicode_asucs4",          unicode_asucs4,                  METH_VARARGS},
5761     {"unicode_asutf8",          unicode_asutf8,                  METH_VARARGS},
5762     {"unicode_asutf8andsize",   unicode_asutf8andsize,           METH_VARARGS},
5763     {"unicode_findchar",        unicode_findchar,                METH_VARARGS},
5764     {"unicode_copycharacters",  unicode_copycharacters,          METH_VARARGS},
5765 #if USE_UNICODE_WCHAR_CACHE
5766     {"unicode_encodedecimal",   unicode_encodedecimal,           METH_VARARGS},
5767     {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
5768     {"unicode_legacy_string",   unicode_legacy_string,           METH_VARARGS},
5769 #endif /* USE_UNICODE_WCHAR_CACHE */
5770     {"_test_thread_state",      test_thread_state,               METH_VARARGS},
5771     {"_pending_threadfunc",     pending_threadfunc,              METH_VARARGS},
5772 #ifdef HAVE_GETTIMEOFDAY
5773     {"profile_int",             profile_int,                     METH_NOARGS},
5774 #endif
5775     {"traceback_print",         traceback_print,                 METH_VARARGS},
5776     {"exception_print",         exception_print,                 METH_VARARGS},
5777     {"set_exc_info",            test_set_exc_info,               METH_VARARGS},
5778     {"argparsing",              argparsing,                      METH_VARARGS},
5779     {"code_newempty",           code_newempty,                   METH_VARARGS},
5780     {"make_exception_with_doc", (PyCFunction)(void(*)(void))make_exception_with_doc,
5781      METH_VARARGS | METH_KEYWORDS},
5782     {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer,
5783      METH_NOARGS},
5784     {"crash_no_current_thread", crash_no_current_thread,         METH_NOARGS},
5785     {"run_in_subinterp",        run_in_subinterp,                METH_VARARGS},
5786     {"pytime_object_to_time_t", test_pytime_object_to_time_t,  METH_VARARGS},
5787     {"pytime_object_to_timeval", test_pytime_object_to_timeval,  METH_VARARGS},
5788     {"pytime_object_to_timespec", test_pytime_object_to_timespec,  METH_VARARGS},
5789     {"with_tp_del",             with_tp_del,                     METH_VARARGS},
5790     {"create_cfunction",        create_cfunction,                METH_NOARGS},
5791     {"test_pymem_alloc0",       test_pymem_alloc0,               METH_NOARGS},
5792     {"test_pymem_setrawallocators",test_pymem_setrawallocators,  METH_NOARGS},
5793     {"test_pymem_setallocators",test_pymem_setallocators,        METH_NOARGS},
5794     {"test_pyobject_setallocators",test_pyobject_setallocators,  METH_NOARGS},
5795     {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS,
5796      PyDoc_STR("set_nomemory(start:int, stop:int = 0)")},
5797     {"remove_mem_hooks",        remove_mem_hooks,                METH_NOARGS,
5798      PyDoc_STR("Remove memory hooks.")},
5799     {"no_docstring",
5800         (PyCFunction)test_with_docstring, METH_NOARGS},
5801     {"docstring_empty",
5802         (PyCFunction)test_with_docstring, METH_NOARGS,
5803         docstring_empty},
5804     {"docstring_no_signature",
5805         (PyCFunction)test_with_docstring, METH_NOARGS,
5806         docstring_no_signature},
5807     {"docstring_with_invalid_signature",
5808         (PyCFunction)test_with_docstring, METH_NOARGS,
5809         docstring_with_invalid_signature},
5810     {"docstring_with_invalid_signature2",
5811         (PyCFunction)test_with_docstring, METH_NOARGS,
5812         docstring_with_invalid_signature2},
5813     {"docstring_with_signature",
5814         (PyCFunction)test_with_docstring, METH_NOARGS,
5815         docstring_with_signature},
5816     {"docstring_with_signature_but_no_doc",
5817         (PyCFunction)test_with_docstring, METH_NOARGS,
5818         docstring_with_signature_but_no_doc},
5819     {"docstring_with_signature_and_extra_newlines",
5820         (PyCFunction)test_with_docstring, METH_NOARGS,
5821         docstring_with_signature_and_extra_newlines},
5822     {"docstring_with_signature_with_defaults",
5823         (PyCFunction)test_with_docstring, METH_NOARGS,
5824         docstring_with_signature_with_defaults},
5825     {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O,
5826      PyDoc_STR("set_error_class(error_class) -> None")},
5827     {"pymarshal_write_long_to_file",
5828         pymarshal_write_long_to_file, METH_VARARGS},
5829     {"pymarshal_write_object_to_file",
5830         pymarshal_write_object_to_file, METH_VARARGS},
5831     {"pymarshal_read_short_from_file",
5832         pymarshal_read_short_from_file, METH_VARARGS},
5833     {"pymarshal_read_long_from_file",
5834         pymarshal_read_long_from_file, METH_VARARGS},
5835     {"pymarshal_read_last_object_from_file",
5836         pymarshal_read_last_object_from_file, METH_VARARGS},
5837     {"pymarshal_read_object_from_file",
5838         pymarshal_read_object_from_file, METH_VARARGS},
5839     {"return_null_without_error", return_null_without_error, METH_NOARGS},
5840     {"return_result_with_error", return_result_with_error, METH_NOARGS},
5841     {"getitem_with_error", getitem_with_error, METH_VARARGS},
5842     {"Py_CompileString",     pycompilestring, METH_O},
5843     {"PyTime_FromSeconds", test_pytime_fromseconds,  METH_VARARGS},
5844     {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject,  METH_VARARGS},
5845     {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
5846     {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
5847 #ifdef HAVE_CLOCK_GETTIME
5848     {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS},
5849 #endif
5850     {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
5851     {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
5852     {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
5853     {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
5854     {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},
5855     {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS},
5856     {"check_pyobject_null_is_freed", check_pyobject_null_is_freed, METH_NOARGS},
5857     {"check_pyobject_uninitialized_is_freed", check_pyobject_uninitialized_is_freed, METH_NOARGS},
5858     {"check_pyobject_forbidden_bytes_is_freed", check_pyobject_forbidden_bytes_is_freed, METH_NOARGS},
5859     {"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS},
5860     {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
5861     {"tracemalloc_track", tracemalloc_track, METH_VARARGS},
5862     {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
5863     {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
5864     {"dict_get_version", dict_get_version, METH_VARARGS},
5865     {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
5866     {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS},
5867     {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
5868     {"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS},
5869     {"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS},
5870     {"stack_pointer", stack_pointer, METH_NOARGS},
5871 #ifdef W_STOPCODE
5872     {"W_STOPCODE", py_w_stopcode, METH_VARARGS},
5873 #endif
5874     {"get_mapping_keys", get_mapping_keys, METH_O},
5875     {"get_mapping_values", get_mapping_values, METH_O},
5876     {"get_mapping_items", get_mapping_items, METH_O},
5877     {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS},
5878     {"hamt", new_hamt, METH_NOARGS},
5879     {"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL},
5880     {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},
5881     {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS},
5882 #ifdef Py_REF_DEBUG
5883     {"negative_refcount", negative_refcount, METH_NOARGS},
5884 #endif
5885     {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS},
5886     {"sequence_getitem", sequence_getitem, METH_VARARGS},
5887     {"meth_varargs", meth_varargs, METH_VARARGS},
5888     {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS},
5889     {"meth_o", meth_o, METH_O},
5890     {"meth_noargs", meth_noargs, METH_NOARGS},
5891     {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL},
5892     {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS},
5893     {"pynumber_tobase", pynumber_tobase, METH_VARARGS},
5894     {"without_gc", without_gc, METH_O},
5895     {"test_set_type_size", test_set_type_size, METH_NOARGS},
5896     {"test_refcount_macros", test_refcount_macros, METH_NOARGS},
5897     {"test_refcount_funcs", test_refcount_funcs, METH_NOARGS},
5898     {"test_py_is_macros", test_py_is_macros, METH_NOARGS},
5899     {"test_py_is_funcs", test_py_is_funcs, METH_NOARGS},
5900     {"fatal_error", test_fatal_error, METH_VARARGS,
5901      PyDoc_STR("fatal_error(message, release_gil=False): call Py_FatalError(message)")},
5902     {NULL, NULL} /* sentinel */
5903 };
5904 
5905 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
5906 
5907 typedef struct {
5908     char bool_member;
5909     char byte_member;
5910     unsigned char ubyte_member;
5911     short short_member;
5912     unsigned short ushort_member;
5913     int int_member;
5914     unsigned int uint_member;
5915     long long_member;
5916     unsigned long ulong_member;
5917     Py_ssize_t pyssizet_member;
5918     float float_member;
5919     double double_member;
5920     char inplace_member[6];
5921     long long longlong_member;
5922     unsigned long long ulonglong_member;
5923 } all_structmembers;
5924 
5925 typedef struct {
5926     PyObject_HEAD
5927     all_structmembers structmembers;
5928 } test_structmembers;
5929 
5930 static struct PyMemberDef test_members[] = {
5931     {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
5932     {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
5933     {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
5934     {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
5935     {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
5936     {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
5937     {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
5938     {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
5939     {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
5940     {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
5941     {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
5942     {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
5943     {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
5944     {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
5945     {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
5946     {NULL}
5947 };
5948 
5949 
5950 static PyObject *
test_structmembers_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)5951 test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5952 {
5953     static char *keywords[] = {
5954         "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
5955         "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
5956         "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
5957         "T_LONGLONG", "T_ULONGLONG",
5958         NULL};
5959     static const char fmt[] = "|bbBhHiIlknfds#LK";
5960     test_structmembers *ob;
5961     const char *s = NULL;
5962     Py_ssize_t string_len = 0;
5963     ob = PyObject_New(test_structmembers, type);
5964     if (ob == NULL)
5965         return NULL;
5966     memset(&ob->structmembers, 0, sizeof(all_structmembers));
5967     if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
5968                                      &ob->structmembers.bool_member,
5969                                      &ob->structmembers.byte_member,
5970                                      &ob->structmembers.ubyte_member,
5971                                      &ob->structmembers.short_member,
5972                                      &ob->structmembers.ushort_member,
5973                                      &ob->structmembers.int_member,
5974                                      &ob->structmembers.uint_member,
5975                                      &ob->structmembers.long_member,
5976                                      &ob->structmembers.ulong_member,
5977                                      &ob->structmembers.pyssizet_member,
5978                                      &ob->structmembers.float_member,
5979                                      &ob->structmembers.double_member,
5980                                      &s, &string_len
5981                                      , &ob->structmembers.longlong_member,
5982                                      &ob->structmembers.ulonglong_member
5983         )) {
5984         Py_DECREF(ob);
5985         return NULL;
5986     }
5987     if (s != NULL) {
5988         if (string_len > 5) {
5989             Py_DECREF(ob);
5990             PyErr_SetString(PyExc_ValueError, "string too long");
5991             return NULL;
5992         }
5993         strcpy(ob->structmembers.inplace_member, s);
5994     }
5995     else {
5996         strcpy(ob->structmembers.inplace_member, "");
5997     }
5998     return (PyObject *)ob;
5999 }
6000 
6001 static void
test_structmembers_free(PyObject * ob)6002 test_structmembers_free(PyObject *ob)
6003 {
6004     PyObject_Free(ob);
6005 }
6006 
6007 static PyTypeObject test_structmembersType = {
6008     PyVarObject_HEAD_INIT(NULL, 0)
6009     "test_structmembersType",
6010     sizeof(test_structmembers),         /* tp_basicsize */
6011     0,                                  /* tp_itemsize */
6012     test_structmembers_free,            /* destructor tp_dealloc */
6013     0,                                  /* tp_vectorcall_offset */
6014     0,                                  /* tp_getattr */
6015     0,                                  /* tp_setattr */
6016     0,                                  /* tp_as_async */
6017     0,                                  /* tp_repr */
6018     0,                                  /* tp_as_number */
6019     0,                                  /* tp_as_sequence */
6020     0,                                  /* tp_as_mapping */
6021     0,                                  /* tp_hash */
6022     0,                                  /* tp_call */
6023     0,                                  /* tp_str */
6024     PyObject_GenericGetAttr,            /* tp_getattro */
6025     PyObject_GenericSetAttr,            /* tp_setattro */
6026     0,                                  /* tp_as_buffer */
6027     0,                                  /* tp_flags */
6028     "Type containing all structmember types",
6029     0,                                  /* traverseproc tp_traverse */
6030     0,                                  /* tp_clear */
6031     0,                                  /* tp_richcompare */
6032     0,                                  /* tp_weaklistoffset */
6033     0,                                  /* tp_iter */
6034     0,                                  /* tp_iternext */
6035     0,                                  /* tp_methods */
6036     test_members,                       /* tp_members */
6037     0,
6038     0,
6039     0,
6040     0,
6041     0,
6042     0,
6043     0,
6044     0,
6045     test_structmembers_new,             /* tp_new */
6046 };
6047 
6048 
6049 typedef struct {
6050     PyObject_HEAD
6051 } matmulObject;
6052 
6053 static PyObject *
matmulType_matmul(PyObject * self,PyObject * other)6054 matmulType_matmul(PyObject *self, PyObject *other)
6055 {
6056     return Py_BuildValue("(sOO)", "matmul", self, other);
6057 }
6058 
6059 static PyObject *
matmulType_imatmul(PyObject * self,PyObject * other)6060 matmulType_imatmul(PyObject *self, PyObject *other)
6061 {
6062     return Py_BuildValue("(sOO)", "imatmul", self, other);
6063 }
6064 
6065 static void
matmulType_dealloc(PyObject * self)6066 matmulType_dealloc(PyObject *self)
6067 {
6068     Py_TYPE(self)->tp_free(self);
6069 }
6070 
6071 static PyNumberMethods matmulType_as_number = {
6072     0,                          /* nb_add */
6073     0,                          /* nb_subtract */
6074     0,                          /* nb_multiply */
6075     0,                          /* nb_remainde r*/
6076     0,                          /* nb_divmod */
6077     0,                          /* nb_power */
6078     0,                          /* nb_negative */
6079     0,                          /* tp_positive */
6080     0,                          /* tp_absolute */
6081     0,                          /* tp_bool */
6082     0,                          /* nb_invert */
6083     0,                          /* nb_lshift */
6084     0,                          /* nb_rshift */
6085     0,                          /* nb_and */
6086     0,                          /* nb_xor */
6087     0,                          /* nb_or */
6088     0,                          /* nb_int */
6089     0,                          /* nb_reserved */
6090     0,                          /* nb_float */
6091     0,                          /* nb_inplace_add */
6092     0,                          /* nb_inplace_subtract */
6093     0,                          /* nb_inplace_multiply */
6094     0,                          /* nb_inplace_remainder */
6095     0,                          /* nb_inplace_power */
6096     0,                          /* nb_inplace_lshift */
6097     0,                          /* nb_inplace_rshift */
6098     0,                          /* nb_inplace_and */
6099     0,                          /* nb_inplace_xor */
6100     0,                          /* nb_inplace_or */
6101     0,                          /* nb_floor_divide */
6102     0,                          /* nb_true_divide */
6103     0,                          /* nb_inplace_floor_divide */
6104     0,                          /* nb_inplace_true_divide */
6105     0,                          /* nb_index */
6106     matmulType_matmul,        /* nb_matrix_multiply */
6107     matmulType_imatmul        /* nb_matrix_inplace_multiply */
6108 };
6109 
6110 static PyTypeObject matmulType = {
6111     PyVarObject_HEAD_INIT(NULL, 0)
6112     "matmulType",
6113     sizeof(matmulObject),               /* tp_basicsize */
6114     0,                                  /* tp_itemsize */
6115     matmulType_dealloc,                 /* destructor tp_dealloc */
6116     0,                                  /* tp_vectorcall_offset */
6117     0,                                  /* tp_getattr */
6118     0,                                  /* tp_setattr */
6119     0,                                  /* tp_as_async */
6120     0,                                  /* tp_repr */
6121     &matmulType_as_number,              /* tp_as_number */
6122     0,                                  /* tp_as_sequence */
6123     0,                                  /* tp_as_mapping */
6124     0,                                  /* tp_hash */
6125     0,                                  /* tp_call */
6126     0,                                  /* tp_str */
6127     PyObject_GenericGetAttr,            /* tp_getattro */
6128     PyObject_GenericSetAttr,            /* tp_setattro */
6129     0,                                  /* tp_as_buffer */
6130     0,                                  /* tp_flags */
6131     "C level type with matrix operations defined",
6132     0,                                  /* traverseproc tp_traverse */
6133     0,                                  /* tp_clear */
6134     0,                                  /* tp_richcompare */
6135     0,                                  /* tp_weaklistoffset */
6136     0,                                  /* tp_iter */
6137     0,                                  /* tp_iternext */
6138     0,                                  /* tp_methods */
6139     0,                                  /* tp_members */
6140     0,
6141     0,
6142     0,
6143     0,
6144     0,
6145     0,
6146     0,
6147     0,
6148     PyType_GenericNew,                  /* tp_new */
6149     PyObject_Del,                       /* tp_free */
6150 };
6151 
6152 typedef struct {
6153     PyObject_HEAD
6154 } ipowObject;
6155 
6156 static PyObject *
ipowType_ipow(PyObject * self,PyObject * other,PyObject * mod)6157 ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod)
6158 {
6159     return Py_BuildValue("OO", other, mod);
6160 }
6161 
6162 static PyNumberMethods ipowType_as_number = {
6163     .nb_inplace_power = ipowType_ipow
6164 };
6165 
6166 static PyTypeObject ipowType = {
6167     PyVarObject_HEAD_INIT(NULL, 0)
6168     .tp_name = "ipowType",
6169     .tp_basicsize = sizeof(ipowObject),
6170     .tp_as_number = &ipowType_as_number,
6171     .tp_new = PyType_GenericNew
6172 };
6173 
6174 typedef struct {
6175     PyObject_HEAD
6176     PyObject *ao_iterator;
6177 } awaitObject;
6178 
6179 
6180 static PyObject *
awaitObject_new(PyTypeObject * type,PyObject * args,PyObject * kwds)6181 awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6182 {
6183     PyObject *v;
6184     awaitObject *ao;
6185 
6186     if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v))
6187         return NULL;
6188 
6189     ao = (awaitObject *)type->tp_alloc(type, 0);
6190     if (ao == NULL) {
6191         return NULL;
6192     }
6193 
6194     Py_INCREF(v);
6195     ao->ao_iterator = v;
6196 
6197     return (PyObject *)ao;
6198 }
6199 
6200 
6201 static void
awaitObject_dealloc(awaitObject * ao)6202 awaitObject_dealloc(awaitObject *ao)
6203 {
6204     Py_CLEAR(ao->ao_iterator);
6205     Py_TYPE(ao)->tp_free(ao);
6206 }
6207 
6208 
6209 static PyObject *
awaitObject_await(awaitObject * ao)6210 awaitObject_await(awaitObject *ao)
6211 {
6212     Py_INCREF(ao->ao_iterator);
6213     return ao->ao_iterator;
6214 }
6215 
6216 static PyAsyncMethods awaitType_as_async = {
6217     (unaryfunc)awaitObject_await,           /* am_await */
6218     0,                                      /* am_aiter */
6219     0,                                      /* am_anext */
6220     0,                                      /* am_send  */
6221 };
6222 
6223 
6224 static PyTypeObject awaitType = {
6225     PyVarObject_HEAD_INIT(NULL, 0)
6226     "awaitType",
6227     sizeof(awaitObject),                /* tp_basicsize */
6228     0,                                  /* tp_itemsize */
6229     (destructor)awaitObject_dealloc,    /* destructor tp_dealloc */
6230     0,                                  /* tp_vectorcall_offset */
6231     0,                                  /* tp_getattr */
6232     0,                                  /* tp_setattr */
6233     &awaitType_as_async,                /* tp_as_async */
6234     0,                                  /* tp_repr */
6235     0,                                  /* tp_as_number */
6236     0,                                  /* tp_as_sequence */
6237     0,                                  /* tp_as_mapping */
6238     0,                                  /* tp_hash */
6239     0,                                  /* tp_call */
6240     0,                                  /* tp_str */
6241     PyObject_GenericGetAttr,            /* tp_getattro */
6242     PyObject_GenericSetAttr,            /* tp_setattro */
6243     0,                                  /* tp_as_buffer */
6244     0,                                  /* tp_flags */
6245     "C level type with tp_as_async",
6246     0,                                  /* traverseproc tp_traverse */
6247     0,                                  /* tp_clear */
6248     0,                                  /* tp_richcompare */
6249     0,                                  /* tp_weaklistoffset */
6250     0,                                  /* tp_iter */
6251     0,                                  /* tp_iternext */
6252     0,                                  /* tp_methods */
6253     0,                                  /* tp_members */
6254     0,
6255     0,
6256     0,
6257     0,
6258     0,
6259     0,
6260     0,
6261     0,
6262     awaitObject_new,                    /* tp_new */
6263     PyObject_Del,                       /* tp_free */
6264 };
6265 
6266 
6267 static int recurse_infinitely_error_init(PyObject *, PyObject *, PyObject *);
6268 
6269 static PyTypeObject PyRecursingInfinitelyError_Type = {
6270     PyVarObject_HEAD_INIT(NULL, 0)
6271     "RecursingInfinitelyError",   /* tp_name */
6272     sizeof(PyBaseExceptionObject), /* tp_basicsize */
6273     0,                          /* tp_itemsize */
6274     0,                          /* tp_dealloc */
6275     0,                          /* tp_vectorcall_offset */
6276     0,                          /* tp_getattr */
6277     0,                          /* tp_setattr */
6278     0,                          /* tp_as_async */
6279     0,                          /* tp_repr */
6280     0,                          /* tp_as_number */
6281     0,                          /* tp_as_sequence */
6282     0,                          /* tp_as_mapping */
6283     0,                          /* tp_hash */
6284     0,                          /* tp_call */
6285     0,                          /* tp_str */
6286     0,                          /* tp_getattro */
6287     0,                          /* tp_setattro */
6288     0,                          /* tp_as_buffer */
6289     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
6290     "Instantiating this exception starts infinite recursion.", /* tp_doc */
6291     0,                          /* tp_traverse */
6292     0,                          /* tp_clear */
6293     0,                          /* tp_richcompare */
6294     0,                          /* tp_weaklistoffset */
6295     0,                          /* tp_iter */
6296     0,                          /* tp_iternext */
6297     0,                          /* tp_methods */
6298     0,                          /* tp_members */
6299     0,                          /* tp_getset */
6300     0,                          /* tp_base */
6301     0,                          /* tp_dict */
6302     0,                          /* tp_descr_get */
6303     0,                          /* tp_descr_set */
6304     0,                          /* tp_dictoffset */
6305     (initproc)recurse_infinitely_error_init, /* tp_init */
6306     0,                          /* tp_alloc */
6307     0,                          /* tp_new */
6308 };
6309 
6310 static int
recurse_infinitely_error_init(PyObject * self,PyObject * args,PyObject * kwds)6311 recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds)
6312 {
6313     PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type;
6314 
6315     /* Instantiating this exception starts infinite recursion. */
6316     Py_INCREF(type);
6317     PyErr_SetObject(type, NULL);
6318     return -1;
6319 }
6320 
6321 
6322 /* Test bpo-35983: create a subclass of "list" which checks that instances
6323  * are not deallocated twice */
6324 
6325 typedef struct {
6326     PyListObject list;
6327     int deallocated;
6328 } MyListObject;
6329 
6330 static PyObject *
MyList_new(PyTypeObject * type,PyObject * args,PyObject * kwds)6331 MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6332 {
6333     PyObject* op = PyList_Type.tp_new(type, args, kwds);
6334     ((MyListObject*)op)->deallocated = 0;
6335     return op;
6336 }
6337 
6338 void
MyList_dealloc(MyListObject * op)6339 MyList_dealloc(MyListObject* op)
6340 {
6341     if (op->deallocated) {
6342         /* We cannot raise exceptions here but we still want the testsuite
6343          * to fail when we hit this */
6344         Py_FatalError("MyList instance deallocated twice");
6345     }
6346     op->deallocated = 1;
6347     PyList_Type.tp_dealloc((PyObject *)op);
6348 }
6349 
6350 static PyTypeObject MyList_Type = {
6351     PyVarObject_HEAD_INIT(NULL, 0)
6352     "MyList",
6353     sizeof(MyListObject),
6354     0,
6355     (destructor)MyList_dealloc,                 /* tp_dealloc */
6356     0,                                          /* tp_vectorcall_offset */
6357     0,                                          /* tp_getattr */
6358     0,                                          /* tp_setattr */
6359     0,                                          /* tp_as_async */
6360     0,                                          /* tp_repr */
6361     0,                                          /* tp_as_number */
6362     0,                                          /* tp_as_sequence */
6363     0,                                          /* tp_as_mapping */
6364     0,                                          /* tp_hash */
6365     0,                                          /* tp_call */
6366     0,                                          /* tp_str */
6367     0,                                          /* tp_getattro */
6368     0,                                          /* tp_setattro */
6369     0,                                          /* tp_as_buffer */
6370     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
6371     0,                                          /* tp_doc */
6372     0,                                          /* tp_traverse */
6373     0,                                          /* tp_clear */
6374     0,                                          /* tp_richcompare */
6375     0,                                          /* tp_weaklistoffset */
6376     0,                                          /* tp_iter */
6377     0,                                          /* tp_iternext */
6378     0,                                          /* tp_methods */
6379     0,                                          /* tp_members */
6380     0,                                          /* tp_getset */
6381     0,  /* &PyList_Type */                      /* tp_base */
6382     0,                                          /* tp_dict */
6383     0,                                          /* tp_descr_get */
6384     0,                                          /* tp_descr_set */
6385     0,                                          /* tp_dictoffset */
6386     0,                                          /* tp_init */
6387     0,                                          /* tp_alloc */
6388     MyList_new,                                 /* tp_new */
6389 };
6390 
6391 
6392 /* Test PEP 560 */
6393 
6394 typedef struct {
6395     PyObject_HEAD
6396     PyObject *item;
6397 } PyGenericAliasObject;
6398 
6399 static void
generic_alias_dealloc(PyGenericAliasObject * self)6400 generic_alias_dealloc(PyGenericAliasObject *self)
6401 {
6402     Py_CLEAR(self->item);
6403     Py_TYPE(self)->tp_free((PyObject *)self);
6404 }
6405 
6406 static PyObject *
generic_alias_mro_entries(PyGenericAliasObject * self,PyObject * bases)6407 generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases)
6408 {
6409     return PyTuple_Pack(1, self->item);
6410 }
6411 
6412 static PyMethodDef generic_alias_methods[] = {
6413     {"__mro_entries__", (PyCFunction)(void(*)(void))generic_alias_mro_entries, METH_O, NULL},
6414     {NULL}  /* sentinel */
6415 };
6416 
6417 static PyTypeObject GenericAlias_Type = {
6418     PyVarObject_HEAD_INIT(NULL, 0)
6419     "GenericAlias",
6420     sizeof(PyGenericAliasObject),
6421     0,
6422     .tp_dealloc = (destructor)generic_alias_dealloc,
6423     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6424     .tp_methods = generic_alias_methods,
6425 };
6426 
6427 static PyObject *
generic_alias_new(PyObject * item)6428 generic_alias_new(PyObject *item)
6429 {
6430     PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type);
6431     if (o == NULL) {
6432         return NULL;
6433     }
6434     Py_INCREF(item);
6435     o->item = item;
6436     return (PyObject*) o;
6437 }
6438 
6439 typedef struct {
6440     PyObject_HEAD
6441 } PyGenericObject;
6442 
6443 static PyObject *
generic_class_getitem(PyObject * type,PyObject * item)6444 generic_class_getitem(PyObject *type, PyObject *item)
6445 {
6446     return generic_alias_new(item);
6447 }
6448 
6449 static PyMethodDef generic_methods[] = {
6450     {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL},
6451     {NULL}  /* sentinel */
6452 };
6453 
6454 static PyTypeObject Generic_Type = {
6455     PyVarObject_HEAD_INIT(NULL, 0)
6456     "Generic",
6457     sizeof(PyGenericObject),
6458     0,
6459     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6460     .tp_methods = generic_methods,
6461 };
6462 
6463 
6464 /* Test PEP 590 */
6465 
6466 typedef struct {
6467     PyObject_HEAD
6468     vectorcallfunc vectorcall;
6469 } MethodDescriptorObject;
6470 
6471 static PyObject *
MethodDescriptor_vectorcall(PyObject * callable,PyObject * const * args,size_t nargsf,PyObject * kwnames)6472 MethodDescriptor_vectorcall(PyObject *callable, PyObject *const *args,
6473                             size_t nargsf, PyObject *kwnames)
6474 {
6475     /* True if using the vectorcall function in MethodDescriptorObject
6476      * but False for MethodDescriptor2Object */
6477     MethodDescriptorObject *md = (MethodDescriptorObject *)callable;
6478     return PyBool_FromLong(md->vectorcall != NULL);
6479 }
6480 
6481 static PyObject *
MethodDescriptor_new(PyTypeObject * type,PyObject * args,PyObject * kw)6482 MethodDescriptor_new(PyTypeObject* type, PyObject* args, PyObject *kw)
6483 {
6484     MethodDescriptorObject *op = (MethodDescriptorObject *)type->tp_alloc(type, 0);
6485     op->vectorcall = MethodDescriptor_vectorcall;
6486     return (PyObject *)op;
6487 }
6488 
6489 static PyObject *
func_descr_get(PyObject * func,PyObject * obj,PyObject * type)6490 func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
6491 {
6492     if (obj == Py_None || obj == NULL) {
6493         Py_INCREF(func);
6494         return func;
6495     }
6496     return PyMethod_New(func, obj);
6497 }
6498 
6499 static PyObject *
nop_descr_get(PyObject * func,PyObject * obj,PyObject * type)6500 nop_descr_get(PyObject *func, PyObject *obj, PyObject *type)
6501 {
6502     Py_INCREF(func);
6503     return func;
6504 }
6505 
6506 static PyObject *
call_return_args(PyObject * self,PyObject * args,PyObject * kwargs)6507 call_return_args(PyObject *self, PyObject *args, PyObject *kwargs)
6508 {
6509     Py_INCREF(args);
6510     return args;
6511 }
6512 
6513 static PyTypeObject MethodDescriptorBase_Type = {
6514     PyVarObject_HEAD_INIT(NULL, 0)
6515     "MethodDescriptorBase",
6516     sizeof(MethodDescriptorObject),
6517     .tp_new = MethodDescriptor_new,
6518     .tp_call = PyVectorcall_Call,
6519     .tp_vectorcall_offset = offsetof(MethodDescriptorObject, vectorcall),
6520     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
6521                 Py_TPFLAGS_METHOD_DESCRIPTOR | Py_TPFLAGS_HAVE_VECTORCALL,
6522     .tp_descr_get = func_descr_get,
6523 };
6524 
6525 static PyTypeObject MethodDescriptorDerived_Type = {
6526     PyVarObject_HEAD_INIT(NULL, 0)
6527     "MethodDescriptorDerived",
6528     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6529 };
6530 
6531 static PyTypeObject MethodDescriptorNopGet_Type = {
6532     PyVarObject_HEAD_INIT(NULL, 0)
6533     "MethodDescriptorNopGet",
6534     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6535     .tp_call = call_return_args,
6536     .tp_descr_get = nop_descr_get,
6537 };
6538 
6539 typedef struct {
6540     MethodDescriptorObject base;
6541     vectorcallfunc vectorcall;
6542 } MethodDescriptor2Object;
6543 
6544 static PyObject *
MethodDescriptor2_new(PyTypeObject * type,PyObject * args,PyObject * kw)6545 MethodDescriptor2_new(PyTypeObject* type, PyObject* args, PyObject *kw)
6546 {
6547     MethodDescriptor2Object *op = PyObject_New(MethodDescriptor2Object, type);
6548     op->base.vectorcall = NULL;
6549     op->vectorcall = MethodDescriptor_vectorcall;
6550     return (PyObject *)op;
6551 }
6552 
6553 static PyTypeObject MethodDescriptor2_Type = {
6554     PyVarObject_HEAD_INIT(NULL, 0)
6555     "MethodDescriptor2",
6556     sizeof(MethodDescriptor2Object),
6557     .tp_new = MethodDescriptor2_new,
6558     .tp_call = PyVectorcall_Call,
6559     .tp_vectorcall_offset = offsetof(MethodDescriptor2Object, vectorcall),
6560     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_VECTORCALL,
6561 };
6562 
6563 PyDoc_STRVAR(heapdocctype__doc__,
6564 "HeapDocCType(arg1, arg2)\n"
6565 "--\n"
6566 "\n"
6567 "somedoc");
6568 
6569 typedef struct {
6570     PyObject_HEAD
6571 } HeapDocCTypeObject;
6572 
6573 static PyType_Slot HeapDocCType_slots[] = {
6574     {Py_tp_doc, (char*)heapdocctype__doc__},
6575     {0},
6576 };
6577 
6578 static PyType_Spec HeapDocCType_spec = {
6579     "_testcapi.HeapDocCType",
6580     sizeof(HeapDocCTypeObject),
6581     0,
6582     Py_TPFLAGS_DEFAULT,
6583     HeapDocCType_slots
6584 };
6585 
6586 typedef struct {
6587     PyObject_HEAD
6588 } NullTpDocTypeObject;
6589 
6590 static PyType_Slot NullTpDocType_slots[] = {
6591     {Py_tp_doc, NULL},
6592     {0, 0},
6593 };
6594 
6595 static PyType_Spec NullTpDocType_spec = {
6596     "_testcapi.NullTpDocType",
6597     sizeof(NullTpDocTypeObject),
6598     0,
6599     Py_TPFLAGS_DEFAULT,
6600     NullTpDocType_slots
6601 };
6602 
6603 
6604 PyDoc_STRVAR(heapgctype__doc__,
6605 "A heap type with GC, and with overridden dealloc.\n\n"
6606 "The 'value' attribute is set to 10 in __init__.");
6607 
6608 typedef struct {
6609     PyObject_HEAD
6610     int value;
6611 } HeapCTypeObject;
6612 
6613 static struct PyMemberDef heapctype_members[] = {
6614     {"value", T_INT, offsetof(HeapCTypeObject, value)},
6615     {NULL} /* Sentinel */
6616 };
6617 
6618 static int
heapctype_init(PyObject * self,PyObject * args,PyObject * kwargs)6619 heapctype_init(PyObject *self, PyObject *args, PyObject *kwargs)
6620 {
6621     ((HeapCTypeObject *)self)->value = 10;
6622     return 0;
6623 }
6624 
6625 static int
heapgcctype_traverse(HeapCTypeObject * self,visitproc visit,void * arg)6626 heapgcctype_traverse(HeapCTypeObject *self, visitproc visit, void *arg)
6627 {
6628     Py_VISIT(Py_TYPE(self));
6629     return 0;
6630 }
6631 
6632 static void
heapgcctype_dealloc(HeapCTypeObject * self)6633 heapgcctype_dealloc(HeapCTypeObject *self)
6634 {
6635     PyTypeObject *tp = Py_TYPE(self);
6636     PyObject_GC_UnTrack(self);
6637     PyObject_GC_Del(self);
6638     Py_DECREF(tp);
6639 }
6640 
6641 static PyType_Slot HeapGcCType_slots[] = {
6642     {Py_tp_init, heapctype_init},
6643     {Py_tp_members, heapctype_members},
6644     {Py_tp_dealloc, heapgcctype_dealloc},
6645     {Py_tp_traverse, heapgcctype_traverse},
6646     {Py_tp_doc, (char*)heapgctype__doc__},
6647     {0, 0},
6648 };
6649 
6650 static PyType_Spec HeapGcCType_spec = {
6651     "_testcapi.HeapGcCType",
6652     sizeof(HeapCTypeObject),
6653     0,
6654     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6655     HeapGcCType_slots
6656 };
6657 
6658 PyDoc_STRVAR(heapctype__doc__,
6659 "A heap type without GC, but with overridden dealloc.\n\n"
6660 "The 'value' attribute is set to 10 in __init__.");
6661 
6662 static void
heapctype_dealloc(HeapCTypeObject * self)6663 heapctype_dealloc(HeapCTypeObject *self)
6664 {
6665     PyTypeObject *tp = Py_TYPE(self);
6666     PyObject_Free(self);
6667     Py_DECREF(tp);
6668 }
6669 
6670 static PyType_Slot HeapCType_slots[] = {
6671     {Py_tp_init, heapctype_init},
6672     {Py_tp_members, heapctype_members},
6673     {Py_tp_dealloc, heapctype_dealloc},
6674     {Py_tp_doc, (char*)heapctype__doc__},
6675     {0, 0},
6676 };
6677 
6678 static PyType_Spec HeapCType_spec = {
6679     "_testcapi.HeapCType",
6680     sizeof(HeapCTypeObject),
6681     0,
6682     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6683     HeapCType_slots
6684 };
6685 
6686 PyDoc_STRVAR(heapctypesubclass__doc__,
6687 "Subclass of HeapCType, without GC.\n\n"
6688 "__init__ sets the 'value' attribute to 10 and 'value2' to 20.");
6689 
6690 typedef struct {
6691     HeapCTypeObject base;
6692     int value2;
6693 } HeapCTypeSubclassObject;
6694 
6695 static int
heapctypesubclass_init(PyObject * self,PyObject * args,PyObject * kwargs)6696 heapctypesubclass_init(PyObject *self, PyObject *args, PyObject *kwargs)
6697 {
6698     /* Call __init__ of the superclass */
6699     if (heapctype_init(self, args, kwargs) < 0) {
6700         return -1;
6701     }
6702     /* Initialize additional element */
6703     ((HeapCTypeSubclassObject *)self)->value2 = 20;
6704     return 0;
6705 }
6706 
6707 static struct PyMemberDef heapctypesubclass_members[] = {
6708     {"value2", T_INT, offsetof(HeapCTypeSubclassObject, value2)},
6709     {NULL} /* Sentinel */
6710 };
6711 
6712 static PyType_Slot HeapCTypeSubclass_slots[] = {
6713     {Py_tp_init, heapctypesubclass_init},
6714     {Py_tp_members, heapctypesubclass_members},
6715     {Py_tp_doc, (char*)heapctypesubclass__doc__},
6716     {0, 0},
6717 };
6718 
6719 static PyType_Spec HeapCTypeSubclass_spec = {
6720     "_testcapi.HeapCTypeSubclass",
6721     sizeof(HeapCTypeSubclassObject),
6722     0,
6723     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6724     HeapCTypeSubclass_slots
6725 };
6726 
6727 PyDoc_STRVAR(heapctypewithbuffer__doc__,
6728 "Heap type with buffer support.\n\n"
6729 "The buffer is set to [b'1', b'2', b'3', b'4']");
6730 
6731 typedef struct {
6732     HeapCTypeObject base;
6733     char buffer[4];
6734 } HeapCTypeWithBufferObject;
6735 
6736 static int
heapctypewithbuffer_getbuffer(HeapCTypeWithBufferObject * self,Py_buffer * view,int flags)6737 heapctypewithbuffer_getbuffer(HeapCTypeWithBufferObject *self, Py_buffer *view, int flags)
6738 {
6739     self->buffer[0] = '1';
6740     self->buffer[1] = '2';
6741     self->buffer[2] = '3';
6742     self->buffer[3] = '4';
6743     return PyBuffer_FillInfo(
6744         view, (PyObject*)self, (void *)self->buffer, 4, 1, flags);
6745 }
6746 
6747 static void
heapctypewithbuffer_releasebuffer(HeapCTypeWithBufferObject * self,Py_buffer * view)6748 heapctypewithbuffer_releasebuffer(HeapCTypeWithBufferObject *self, Py_buffer *view)
6749 {
6750     assert(view->obj == (void*) self);
6751 }
6752 
6753 static PyType_Slot HeapCTypeWithBuffer_slots[] = {
6754     {Py_bf_getbuffer, heapctypewithbuffer_getbuffer},
6755     {Py_bf_releasebuffer, heapctypewithbuffer_releasebuffer},
6756     {Py_tp_doc, (char*)heapctypewithbuffer__doc__},
6757     {0, 0},
6758 };
6759 
6760 static PyType_Spec HeapCTypeWithBuffer_spec = {
6761     "_testcapi.HeapCTypeWithBuffer",
6762     sizeof(HeapCTypeWithBufferObject),
6763     0,
6764     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6765     HeapCTypeWithBuffer_slots
6766 };
6767 
6768 PyDoc_STRVAR(heapctypesubclasswithfinalizer__doc__,
6769 "Subclass of HeapCType with a finalizer that reassigns __class__.\n\n"
6770 "__class__ is set to plain HeapCTypeSubclass during finalization.\n"
6771 "__init__ sets the 'value' attribute to 10 and 'value2' to 20.");
6772 
6773 static int
heapctypesubclasswithfinalizer_init(PyObject * self,PyObject * args,PyObject * kwargs)6774 heapctypesubclasswithfinalizer_init(PyObject *self, PyObject *args, PyObject *kwargs)
6775 {
6776     PyTypeObject *base = (PyTypeObject *)PyType_GetSlot(Py_TYPE(self), Py_tp_base);
6777     initproc base_init = PyType_GetSlot(base, Py_tp_init);
6778     base_init(self, args, kwargs);
6779     return 0;
6780 }
6781 
6782 static void
heapctypesubclasswithfinalizer_finalize(PyObject * self)6783 heapctypesubclasswithfinalizer_finalize(PyObject *self)
6784 {
6785     PyObject *error_type, *error_value, *error_traceback, *m;
6786     PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL;
6787 
6788     /* Save the current exception, if any. */
6789     PyErr_Fetch(&error_type, &error_value, &error_traceback);
6790 
6791     m = PyState_FindModule(&_testcapimodule);
6792     if (m == NULL) {
6793         goto cleanup_finalize;
6794     }
6795     oldtype = PyObject_GetAttrString(m, "HeapCTypeSubclassWithFinalizer");
6796     newtype = PyObject_GetAttrString(m, "HeapCTypeSubclass");
6797     if (oldtype == NULL || newtype == NULL) {
6798         goto cleanup_finalize;
6799     }
6800 
6801     if (PyObject_SetAttrString(self, "__class__", newtype) < 0) {
6802         goto cleanup_finalize;
6803     }
6804     refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype));
6805     if (refcnt == NULL) {
6806         goto cleanup_finalize;
6807     }
6808     if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) {
6809         goto cleanup_finalize;
6810     }
6811     Py_DECREF(refcnt);
6812     refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype));
6813     if (refcnt == NULL) {
6814         goto cleanup_finalize;
6815     }
6816     if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) {
6817         goto cleanup_finalize;
6818     }
6819 
6820 cleanup_finalize:
6821     Py_XDECREF(oldtype);
6822     Py_XDECREF(newtype);
6823     Py_XDECREF(refcnt);
6824 
6825     /* Restore the saved exception. */
6826     PyErr_Restore(error_type, error_value, error_traceback);
6827 }
6828 
6829 static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = {
6830     {Py_tp_init, heapctypesubclasswithfinalizer_init},
6831     {Py_tp_members, heapctypesubclass_members},
6832     {Py_tp_finalize, heapctypesubclasswithfinalizer_finalize},
6833     {Py_tp_doc, (char*)heapctypesubclasswithfinalizer__doc__},
6834     {0, 0},
6835 };
6836 
6837 static PyType_Spec HeapCTypeSubclassWithFinalizer_spec = {
6838     "_testcapi.HeapCTypeSubclassWithFinalizer",
6839     sizeof(HeapCTypeSubclassObject),
6840     0,
6841     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
6842     HeapCTypeSubclassWithFinalizer_slots
6843 };
6844 
6845 typedef struct {
6846     PyObject_HEAD
6847     PyObject *dict;
6848 } HeapCTypeWithDictObject;
6849 
6850 static void
heapctypewithdict_dealloc(HeapCTypeWithDictObject * self)6851 heapctypewithdict_dealloc(HeapCTypeWithDictObject* self)
6852 {
6853 
6854     PyTypeObject *tp = Py_TYPE(self);
6855     Py_XDECREF(self->dict);
6856     PyObject_Free(self);
6857     Py_DECREF(tp);
6858 }
6859 
6860 static PyGetSetDef heapctypewithdict_getsetlist[] = {
6861     {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
6862     {NULL} /* Sentinel */
6863 };
6864 
6865 static struct PyMemberDef heapctypewithdict_members[] = {
6866     {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)},
6867     {"__dictoffset__", T_PYSSIZET, offsetof(HeapCTypeWithDictObject, dict), READONLY},
6868     {NULL} /* Sentinel */
6869 };
6870 
6871 static PyType_Slot HeapCTypeWithDict_slots[] = {
6872     {Py_tp_members, heapctypewithdict_members},
6873     {Py_tp_getset, heapctypewithdict_getsetlist},
6874     {Py_tp_dealloc, heapctypewithdict_dealloc},
6875     {0, 0},
6876 };
6877 
6878 static PyType_Spec HeapCTypeWithDict_spec = {
6879     "_testcapi.HeapCTypeWithDict",
6880     sizeof(HeapCTypeWithDictObject),
6881     0,
6882     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6883     HeapCTypeWithDict_slots
6884 };
6885 
6886 static struct PyMemberDef heapctypewithnegativedict_members[] = {
6887     {"dictobj", T_OBJECT, offsetof(HeapCTypeWithDictObject, dict)},
6888     {"__dictoffset__", T_PYSSIZET, -(Py_ssize_t)sizeof(void*), READONLY},
6889     {NULL} /* Sentinel */
6890 };
6891 
6892 static PyType_Slot HeapCTypeWithNegativeDict_slots[] = {
6893     {Py_tp_members, heapctypewithnegativedict_members},
6894     {Py_tp_getset, heapctypewithdict_getsetlist},
6895     {Py_tp_dealloc, heapctypewithdict_dealloc},
6896     {0, 0},
6897 };
6898 
6899 static PyType_Spec HeapCTypeWithNegativeDict_spec = {
6900     "_testcapi.HeapCTypeWithNegativeDict",
6901     sizeof(HeapCTypeWithDictObject),
6902     0,
6903     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6904     HeapCTypeWithNegativeDict_slots
6905 };
6906 
6907 typedef struct {
6908     PyObject_HEAD
6909     PyObject *weakreflist;
6910 } HeapCTypeWithWeakrefObject;
6911 
6912 static struct PyMemberDef heapctypewithweakref_members[] = {
6913     {"weakreflist", T_OBJECT, offsetof(HeapCTypeWithWeakrefObject, weakreflist)},
6914     {"__weaklistoffset__", T_PYSSIZET,
6915       offsetof(HeapCTypeWithWeakrefObject, weakreflist), READONLY},
6916     {NULL} /* Sentinel */
6917 };
6918 
6919 static void
heapctypewithweakref_dealloc(HeapCTypeWithWeakrefObject * self)6920 heapctypewithweakref_dealloc(HeapCTypeWithWeakrefObject* self)
6921 {
6922 
6923     PyTypeObject *tp = Py_TYPE(self);
6924     if (self->weakreflist != NULL)
6925         PyObject_ClearWeakRefs((PyObject *) self);
6926     Py_XDECREF(self->weakreflist);
6927     PyObject_Free(self);
6928     Py_DECREF(tp);
6929 }
6930 
6931 static PyType_Slot HeapCTypeWithWeakref_slots[] = {
6932     {Py_tp_members, heapctypewithweakref_members},
6933     {Py_tp_dealloc, heapctypewithweakref_dealloc},
6934     {0, 0},
6935 };
6936 
6937 static PyType_Spec HeapCTypeWithWeakref_spec = {
6938     "_testcapi.HeapCTypeWithWeakref",
6939     sizeof(HeapCTypeWithWeakrefObject),
6940     0,
6941     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6942     HeapCTypeWithWeakref_slots
6943 };
6944 
6945 PyDoc_STRVAR(heapctypesetattr__doc__,
6946 "A heap type without GC, but with overridden __setattr__.\n\n"
6947 "The 'value' attribute is set to 10 in __init__ and updated via attribute setting.");
6948 
6949 typedef struct {
6950     PyObject_HEAD
6951     long value;
6952 } HeapCTypeSetattrObject;
6953 
6954 static struct PyMemberDef heapctypesetattr_members[] = {
6955     {"pvalue", T_LONG, offsetof(HeapCTypeSetattrObject, value)},
6956     {NULL} /* Sentinel */
6957 };
6958 
6959 static int
heapctypesetattr_init(PyObject * self,PyObject * args,PyObject * kwargs)6960 heapctypesetattr_init(PyObject *self, PyObject *args, PyObject *kwargs)
6961 {
6962     ((HeapCTypeSetattrObject *)self)->value = 10;
6963     return 0;
6964 }
6965 
6966 static void
heapctypesetattr_dealloc(HeapCTypeSetattrObject * self)6967 heapctypesetattr_dealloc(HeapCTypeSetattrObject *self)
6968 {
6969     PyTypeObject *tp = Py_TYPE(self);
6970     PyObject_Free(self);
6971     Py_DECREF(tp);
6972 }
6973 
6974 static int
heapctypesetattr_setattro(HeapCTypeSetattrObject * self,PyObject * attr,PyObject * value)6975 heapctypesetattr_setattro(HeapCTypeSetattrObject *self, PyObject *attr, PyObject *value)
6976 {
6977     PyObject *svalue = PyUnicode_FromString("value");
6978     if (svalue == NULL)
6979         return -1;
6980     int eq = PyObject_RichCompareBool(svalue, attr, Py_EQ);
6981     Py_DECREF(svalue);
6982     if (eq < 0)
6983         return -1;
6984     if (!eq) {
6985         return PyObject_GenericSetAttr((PyObject*) self, attr, value);
6986     }
6987     if (value == NULL) {
6988         self->value = 0;
6989         return 0;
6990     }
6991     PyObject *ivalue = PyNumber_Long(value);
6992     if (ivalue == NULL)
6993         return -1;
6994     long v = PyLong_AsLong(ivalue);
6995     Py_DECREF(ivalue);
6996     if (v == -1 && PyErr_Occurred())
6997         return -1;
6998     self->value = v;
6999     return 0;
7000 }
7001 
7002 static PyType_Slot HeapCTypeSetattr_slots[] = {
7003     {Py_tp_init, heapctypesetattr_init},
7004     {Py_tp_members, heapctypesetattr_members},
7005     {Py_tp_setattro, heapctypesetattr_setattro},
7006     {Py_tp_dealloc, heapctypesetattr_dealloc},
7007     {Py_tp_doc, (char*)heapctypesetattr__doc__},
7008     {0, 0},
7009 };
7010 
7011 static PyType_Spec HeapCTypeSetattr_spec = {
7012     "_testcapi.HeapCTypeSetattr",
7013     sizeof(HeapCTypeSetattrObject),
7014     0,
7015     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
7016     HeapCTypeSetattr_slots
7017 };
7018 
7019 static PyMethodDef meth_instance_methods[] = {
7020     {"meth_varargs", meth_varargs, METH_VARARGS},
7021     {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS},
7022     {"meth_o", meth_o, METH_O},
7023     {"meth_noargs", meth_noargs, METH_NOARGS},
7024     {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL},
7025     {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS},
7026     {NULL, NULL} /* sentinel */
7027 };
7028 
7029 
7030 static PyTypeObject MethInstance_Type = {
7031     PyVarObject_HEAD_INIT(NULL, 0)
7032     "MethInstance",
7033     sizeof(PyObject),
7034     .tp_new = PyType_GenericNew,
7035     .tp_flags = Py_TPFLAGS_DEFAULT,
7036     .tp_methods = meth_instance_methods,
7037     .tp_doc = (char*)PyDoc_STR(
7038         "Class with normal (instance) methods to test calling conventions"),
7039 };
7040 
7041 static PyMethodDef meth_class_methods[] = {
7042     {"meth_varargs", meth_varargs, METH_VARARGS|METH_CLASS},
7043     {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_CLASS},
7044     {"meth_o", meth_o, METH_O|METH_CLASS},
7045     {"meth_noargs", meth_noargs, METH_NOARGS|METH_CLASS},
7046     {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_CLASS},
7047     {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_CLASS},
7048     {NULL, NULL} /* sentinel */
7049 };
7050 
7051 
7052 static PyTypeObject MethClass_Type = {
7053     PyVarObject_HEAD_INIT(NULL, 0)
7054     "MethClass",
7055     sizeof(PyObject),
7056     .tp_new = PyType_GenericNew,
7057     .tp_flags = Py_TPFLAGS_DEFAULT,
7058     .tp_methods = meth_class_methods,
7059     .tp_doc = PyDoc_STR(
7060         "Class with class methods to test calling conventions"),
7061 };
7062 
7063 static PyMethodDef meth_static_methods[] = {
7064     {"meth_varargs", meth_varargs, METH_VARARGS|METH_STATIC},
7065     {"meth_varargs_keywords", (PyCFunction)(void(*)(void))meth_varargs_keywords, METH_VARARGS|METH_KEYWORDS|METH_STATIC},
7066     {"meth_o", meth_o, METH_O|METH_STATIC},
7067     {"meth_noargs", meth_noargs, METH_NOARGS|METH_STATIC},
7068     {"meth_fastcall", (PyCFunction)(void(*)(void))meth_fastcall, METH_FASTCALL|METH_STATIC},
7069     {"meth_fastcall_keywords", (PyCFunction)(void(*)(void))meth_fastcall_keywords, METH_FASTCALL|METH_KEYWORDS|METH_STATIC},
7070     {NULL, NULL} /* sentinel */
7071 };
7072 
7073 
7074 static PyTypeObject MethStatic_Type = {
7075     PyVarObject_HEAD_INIT(NULL, 0)
7076     "MethStatic",
7077     sizeof(PyObject),
7078     .tp_new = PyType_GenericNew,
7079     .tp_flags = Py_TPFLAGS_DEFAULT,
7080     .tp_methods = meth_static_methods,
7081     .tp_doc = PyDoc_STR(
7082         "Class with static methods to test calling conventions"),
7083 };
7084 
7085 /* ContainerNoGC -- a simple container without GC methods */
7086 
7087 typedef struct {
7088     PyObject_HEAD
7089     PyObject *value;
7090 } ContainerNoGCobject;
7091 
7092 static PyObject *
ContainerNoGC_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)7093 ContainerNoGC_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
7094 {
7095     PyObject *value;
7096     char *names[] = {"value", NULL};
7097     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", names, &value)) {
7098         return NULL;
7099     }
7100     PyObject *self = type->tp_alloc(type, 0);
7101     if (self == NULL) {
7102         return NULL;
7103     }
7104     Py_INCREF(value);
7105     ((ContainerNoGCobject *)self)->value = value;
7106     return self;
7107 }
7108 
7109 static void
ContainerNoGC_dealloc(ContainerNoGCobject * self)7110 ContainerNoGC_dealloc(ContainerNoGCobject *self)
7111 {
7112     Py_DECREF(self->value);
7113     Py_TYPE(self)->tp_free((PyObject *)self);
7114 }
7115 
7116 static PyMemberDef ContainerNoGC_members[] = {
7117     {"value", T_OBJECT, offsetof(ContainerNoGCobject, value), READONLY,
7118      PyDoc_STR("a container value for test purposes")},
7119     {0}
7120 };
7121 
7122 static PyTypeObject ContainerNoGC_type = {
7123     PyVarObject_HEAD_INIT(NULL, 0)
7124     "_testcapi.ContainerNoGC",
7125     sizeof(ContainerNoGCobject),
7126     .tp_dealloc = (destructor)ContainerNoGC_dealloc,
7127     .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
7128     .tp_members = ContainerNoGC_members,
7129     .tp_new = ContainerNoGC_new,
7130 };
7131 
7132 
7133 static struct PyModuleDef _testcapimodule = {
7134     PyModuleDef_HEAD_INIT,
7135     "_testcapi",
7136     NULL,
7137     -1,
7138     TestMethods,
7139     NULL,
7140     NULL,
7141     NULL,
7142     NULL
7143 };
7144 
7145 /* Per PEP 489, this module will not be converted to multi-phase initialization
7146  */
7147 
7148 PyMODINIT_FUNC
PyInit__testcapi(void)7149 PyInit__testcapi(void)
7150 {
7151     PyObject *m;
7152 
7153     m = PyModule_Create(&_testcapimodule);
7154     if (m == NULL)
7155         return NULL;
7156 
7157     Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type);
7158 
7159     Py_SET_TYPE(&test_structmembersType, &PyType_Type);
7160     Py_INCREF(&test_structmembersType);
7161     /* don't use a name starting with "test", since we don't want
7162        test_capi to automatically call this */
7163     PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
7164     if (PyType_Ready(&matmulType) < 0)
7165         return NULL;
7166     Py_INCREF(&matmulType);
7167     PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType);
7168     if (PyType_Ready(&ipowType) < 0) {
7169         return NULL;
7170     }
7171     Py_INCREF(&ipowType);
7172     PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType);
7173 
7174     if (PyType_Ready(&awaitType) < 0)
7175         return NULL;
7176     Py_INCREF(&awaitType);
7177     PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType);
7178 
7179     MyList_Type.tp_base = &PyList_Type;
7180     if (PyType_Ready(&MyList_Type) < 0)
7181         return NULL;
7182     Py_INCREF(&MyList_Type);
7183     PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type);
7184 
7185     if (PyType_Ready(&MethodDescriptorBase_Type) < 0)
7186         return NULL;
7187     Py_INCREF(&MethodDescriptorBase_Type);
7188     PyModule_AddObject(m, "MethodDescriptorBase", (PyObject *)&MethodDescriptorBase_Type);
7189 
7190     MethodDescriptorDerived_Type.tp_base = &MethodDescriptorBase_Type;
7191     if (PyType_Ready(&MethodDescriptorDerived_Type) < 0)
7192         return NULL;
7193     Py_INCREF(&MethodDescriptorDerived_Type);
7194     PyModule_AddObject(m, "MethodDescriptorDerived", (PyObject *)&MethodDescriptorDerived_Type);
7195 
7196     MethodDescriptorNopGet_Type.tp_base = &MethodDescriptorBase_Type;
7197     if (PyType_Ready(&MethodDescriptorNopGet_Type) < 0)
7198         return NULL;
7199     Py_INCREF(&MethodDescriptorNopGet_Type);
7200     PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type);
7201 
7202     MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type;
7203     if (PyType_Ready(&MethodDescriptor2_Type) < 0)
7204         return NULL;
7205     Py_INCREF(&MethodDescriptor2_Type);
7206     PyModule_AddObject(m, "MethodDescriptor2", (PyObject *)&MethodDescriptor2_Type);
7207 
7208     if (PyType_Ready(&GenericAlias_Type) < 0)
7209         return NULL;
7210     Py_INCREF(&GenericAlias_Type);
7211     PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type);
7212 
7213     if (PyType_Ready(&Generic_Type) < 0)
7214         return NULL;
7215     Py_INCREF(&Generic_Type);
7216     PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type);
7217 
7218     if (PyType_Ready(&MethInstance_Type) < 0)
7219         return NULL;
7220     Py_INCREF(&MethInstance_Type);
7221     PyModule_AddObject(m, "MethInstance", (PyObject *)&MethInstance_Type);
7222 
7223     if (PyType_Ready(&MethClass_Type) < 0)
7224         return NULL;
7225     Py_INCREF(&MethClass_Type);
7226     PyModule_AddObject(m, "MethClass", (PyObject *)&MethClass_Type);
7227 
7228     if (PyType_Ready(&MethStatic_Type) < 0)
7229         return NULL;
7230     Py_INCREF(&MethStatic_Type);
7231     PyModule_AddObject(m, "MethStatic", (PyObject *)&MethStatic_Type);
7232 
7233     PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception;
7234     if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) {
7235         return NULL;
7236     }
7237     Py_INCREF(&PyRecursingInfinitelyError_Type);
7238     PyModule_AddObject(m, "RecursingInfinitelyError",
7239                        (PyObject *)&PyRecursingInfinitelyError_Type);
7240 
7241     PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
7242     PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
7243     PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
7244     PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
7245     PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
7246     PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
7247     PyModule_AddObject(m, "INT_MAX",  PyLong_FromLong(INT_MAX));
7248     PyModule_AddObject(m, "INT_MIN",  PyLong_FromLong(INT_MIN));
7249     PyModule_AddObject(m, "UINT_MAX",  PyLong_FromUnsignedLong(UINT_MAX));
7250     PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
7251     PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
7252     PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
7253     PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
7254     PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
7255     PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
7256     PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
7257     PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX));
7258     PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN));
7259     PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX));
7260     PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
7261     PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
7262     PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
7263     Py_INCREF(&PyInstanceMethod_Type);
7264     PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
7265 
7266     PyModule_AddIntConstant(m, "the_number_three", 3);
7267     PyObject *v;
7268 #ifdef WITH_PYMALLOC
7269     v = Py_True;
7270 #else
7271     v = Py_False;
7272 #endif
7273     Py_INCREF(v);
7274     PyModule_AddObject(m, "WITH_PYMALLOC", v);
7275 
7276     TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
7277     Py_INCREF(TestError);
7278     PyModule_AddObject(m, "error", TestError);
7279 
7280     PyObject *HeapDocCType = PyType_FromSpec(&HeapDocCType_spec);
7281     if (HeapDocCType == NULL) {
7282         return NULL;
7283     }
7284     PyModule_AddObject(m, "HeapDocCType", HeapDocCType);
7285 
7286     /* bpo-41832: Add a new type to test PyType_FromSpec()
7287        now can accept a NULL tp_doc slot. */
7288     PyObject *NullTpDocType = PyType_FromSpec(&NullTpDocType_spec);
7289     if (NullTpDocType == NULL) {
7290         return NULL;
7291     }
7292     PyModule_AddObject(m, "NullTpDocType", NullTpDocType);
7293 
7294     PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec);
7295     if (HeapGcCType == NULL) {
7296         return NULL;
7297     }
7298     PyModule_AddObject(m, "HeapGcCType", HeapGcCType);
7299 
7300     PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec);
7301     if (HeapCType == NULL) {
7302         return NULL;
7303     }
7304     PyObject *subclass_bases = PyTuple_Pack(1, HeapCType);
7305     if (subclass_bases == NULL) {
7306         return NULL;
7307     }
7308     PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases);
7309     if (HeapCTypeSubclass == NULL) {
7310         return NULL;
7311     }
7312     Py_DECREF(subclass_bases);
7313     PyModule_AddObject(m, "HeapCTypeSubclass", HeapCTypeSubclass);
7314 
7315     PyObject *HeapCTypeWithDict = PyType_FromSpec(&HeapCTypeWithDict_spec);
7316     if (HeapCTypeWithDict == NULL) {
7317         return NULL;
7318     }
7319     PyModule_AddObject(m, "HeapCTypeWithDict", HeapCTypeWithDict);
7320 
7321     PyObject *HeapCTypeWithNegativeDict = PyType_FromSpec(&HeapCTypeWithNegativeDict_spec);
7322     if (HeapCTypeWithNegativeDict == NULL) {
7323         return NULL;
7324     }
7325     PyModule_AddObject(m, "HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict);
7326 
7327     PyObject *HeapCTypeWithWeakref = PyType_FromSpec(&HeapCTypeWithWeakref_spec);
7328     if (HeapCTypeWithWeakref == NULL) {
7329         return NULL;
7330     }
7331     PyModule_AddObject(m, "HeapCTypeWithWeakref", HeapCTypeWithWeakref);
7332 
7333     PyObject *HeapCTypeWithBuffer = PyType_FromSpec(&HeapCTypeWithBuffer_spec);
7334     if (HeapCTypeWithBuffer == NULL) {
7335         return NULL;
7336     }
7337     PyModule_AddObject(m, "HeapCTypeWithBuffer", HeapCTypeWithBuffer);
7338 
7339     PyObject *HeapCTypeSetattr = PyType_FromSpec(&HeapCTypeSetattr_spec);
7340     if (HeapCTypeSetattr == NULL) {
7341         return NULL;
7342     }
7343     PyModule_AddObject(m, "HeapCTypeSetattr", HeapCTypeSetattr);
7344 
7345     PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass);
7346     if (subclass_with_finalizer_bases == NULL) {
7347         return NULL;
7348     }
7349     PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases(
7350         &HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases);
7351     if (HeapCTypeSubclassWithFinalizer == NULL) {
7352         return NULL;
7353     }
7354     Py_DECREF(subclass_with_finalizer_bases);
7355     PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer);
7356 
7357     if (PyType_Ready(&ContainerNoGC_type) < 0) {
7358         return NULL;
7359     }
7360     Py_INCREF(&ContainerNoGC_type);
7361     if (PyModule_AddObject(m, "ContainerNoGC",
7362                            (PyObject *) &ContainerNoGC_type) < 0)
7363         return NULL;
7364 
7365     PyState_AddModule(m, &_testcapimodule);
7366     return m;
7367 }
7368 
7369 
7370 /* Test the C API exposed when PY_SSIZE_T_CLEAN is not defined */
7371 
7372 #undef Py_BuildValue
7373 PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...);
7374 
7375 static PyObject *
test_buildvalue_issue38913(PyObject * self,PyObject * Py_UNUSED (ignored))7376 test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored))
7377 {
7378     PyObject *res;
7379     const char str[] = "string";
7380     const Py_UNICODE unicode[] = L"unicode";
7381     assert(!PyErr_Occurred());
7382 
7383     res = Py_BuildValue("(s#O)", str, 1, Py_None);
7384     assert(res == NULL);
7385     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
7386         return NULL;
7387     }
7388     PyErr_Clear();
7389 
7390     res = Py_BuildValue("(z#O)", str, 1, Py_None);
7391     assert(res == NULL);
7392     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
7393         return NULL;
7394     }
7395     PyErr_Clear();
7396 
7397     res = Py_BuildValue("(y#O)", str, 1, Py_None);
7398     assert(res == NULL);
7399     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
7400         return NULL;
7401     }
7402     PyErr_Clear();
7403 
7404     res = Py_BuildValue("(u#O)", unicode, 1, Py_None);
7405     assert(res == NULL);
7406     if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
7407         return NULL;
7408     }
7409     PyErr_Clear();
7410 
7411 
7412     Py_RETURN_NONE;
7413 }
7414 
7415 #undef PyArg_ParseTupleAndKeywords
7416 PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
7417                                             const char *, char **, ...);
7418 
7419 static PyObject *
getargs_s_hash_int(PyObject * self,PyObject * args,PyObject * kwargs)7420 getargs_s_hash_int(PyObject *self, PyObject *args, PyObject *kwargs)
7421 {
7422     static char *keywords[] = {"", "x", NULL};
7423     const char *s;
7424     int len;
7425     int i = 0;
7426     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s#i", keywords, &s, &len, &i))
7427         return NULL;
7428     Py_RETURN_NONE;
7429 }
7430