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