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