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