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