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 /* only initialise 'cflags.cf_flags' to test backwards compatibility */
3349 PyCompilerFlags cflags = {0};
3350
3351 if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
3352 &code))
3353 return NULL;
3354
3355 mainstate = PyThreadState_Get();
3356
3357 PyThreadState_Swap(NULL);
3358
3359 substate = Py_NewInterpreter();
3360 if (substate == NULL) {
3361 /* Since no new thread state was created, there is no exception to
3362 propagate; raise a fresh one after swapping in the old thread
3363 state. */
3364 PyThreadState_Swap(mainstate);
3365 PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
3366 return NULL;
3367 }
3368 r = PyRun_SimpleStringFlags(code, &cflags);
3369 Py_EndInterpreter(substate);
3370
3371 PyThreadState_Swap(mainstate);
3372
3373 return PyLong_FromLong(r);
3374 }
3375
3376 static int
check_time_rounding(int round)3377 check_time_rounding(int round)
3378 {
3379 if (round != _PyTime_ROUND_FLOOR
3380 && round != _PyTime_ROUND_CEILING
3381 && round != _PyTime_ROUND_HALF_EVEN
3382 && round != _PyTime_ROUND_UP) {
3383 PyErr_SetString(PyExc_ValueError, "invalid rounding");
3384 return -1;
3385 }
3386 return 0;
3387 }
3388
3389 static PyObject *
test_pytime_object_to_time_t(PyObject * self,PyObject * args)3390 test_pytime_object_to_time_t(PyObject *self, PyObject *args)
3391 {
3392 PyObject *obj;
3393 time_t sec;
3394 int round;
3395 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round))
3396 return NULL;
3397 if (check_time_rounding(round) < 0)
3398 return NULL;
3399 if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1)
3400 return NULL;
3401 return _PyLong_FromTime_t(sec);
3402 }
3403
3404 static PyObject *
test_pytime_object_to_timeval(PyObject * self,PyObject * args)3405 test_pytime_object_to_timeval(PyObject *self, PyObject *args)
3406 {
3407 PyObject *obj;
3408 time_t sec;
3409 long usec;
3410 int round;
3411 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round))
3412 return NULL;
3413 if (check_time_rounding(round) < 0)
3414 return NULL;
3415 if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1)
3416 return NULL;
3417 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
3418 }
3419
3420 static PyObject *
test_pytime_object_to_timespec(PyObject * self,PyObject * args)3421 test_pytime_object_to_timespec(PyObject *self, PyObject *args)
3422 {
3423 PyObject *obj;
3424 time_t sec;
3425 long nsec;
3426 int round;
3427 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round))
3428 return NULL;
3429 if (check_time_rounding(round) < 0)
3430 return NULL;
3431 if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1)
3432 return NULL;
3433 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
3434 }
3435
3436 static void
slot_tp_del(PyObject * self)3437 slot_tp_del(PyObject *self)
3438 {
3439 _Py_IDENTIFIER(__tp_del__);
3440 PyObject *del, *res;
3441 PyObject *error_type, *error_value, *error_traceback;
3442
3443 /* Temporarily resurrect the object. */
3444 assert(self->ob_refcnt == 0);
3445 self->ob_refcnt = 1;
3446
3447 /* Save the current exception, if any. */
3448 PyErr_Fetch(&error_type, &error_value, &error_traceback);
3449
3450 /* Execute __del__ method, if any. */
3451 del = _PyObject_LookupSpecial(self, &PyId___tp_del__);
3452 if (del != NULL) {
3453 res = _PyObject_CallNoArg(del);
3454 if (res == NULL)
3455 PyErr_WriteUnraisable(del);
3456 else
3457 Py_DECREF(res);
3458 Py_DECREF(del);
3459 }
3460
3461 /* Restore the saved exception. */
3462 PyErr_Restore(error_type, error_value, error_traceback);
3463
3464 /* Undo the temporary resurrection; can't use DECREF here, it would
3465 * cause a recursive call.
3466 */
3467 assert(self->ob_refcnt > 0);
3468 if (--self->ob_refcnt == 0)
3469 return; /* this is the normal path out */
3470
3471 /* __del__ resurrected it! Make it look like the original Py_DECREF
3472 * never happened.
3473 */
3474 {
3475 Py_ssize_t refcnt = self->ob_refcnt;
3476 _Py_NewReference(self);
3477 self->ob_refcnt = refcnt;
3478 }
3479 assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
3480 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
3481 * we need to undo that. */
3482 _Py_DEC_REFTOTAL;
3483 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
3484 * chain, so no more to do there.
3485 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
3486 * _Py_NewReference bumped tp_allocs: both of those need to be
3487 * undone.
3488 */
3489 #ifdef COUNT_ALLOCS
3490 --Py_TYPE(self)->tp_frees;
3491 --Py_TYPE(self)->tp_allocs;
3492 #endif
3493 }
3494
3495 static PyObject *
with_tp_del(PyObject * self,PyObject * args)3496 with_tp_del(PyObject *self, PyObject *args)
3497 {
3498 PyObject *obj;
3499 PyTypeObject *tp;
3500
3501 if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
3502 return NULL;
3503 tp = (PyTypeObject *) obj;
3504 if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
3505 PyErr_Format(PyExc_TypeError,
3506 "heap type expected, got %R", obj);
3507 return NULL;
3508 }
3509 tp->tp_del = slot_tp_del;
3510 Py_INCREF(obj);
3511 return obj;
3512 }
3513
3514 static PyMethodDef ml;
3515
3516 static PyObject *
create_cfunction(PyObject * self,PyObject * args)3517 create_cfunction(PyObject *self, PyObject *args)
3518 {
3519 return PyCFunction_NewEx(&ml, self, NULL);
3520 }
3521
3522 static PyMethodDef ml = {
3523 "create_cfunction",
3524 create_cfunction,
3525 METH_NOARGS,
3526 NULL
3527 };
3528
3529 static PyObject *
_test_incref(PyObject * ob)3530 _test_incref(PyObject *ob)
3531 {
3532 Py_INCREF(ob);
3533 return ob;
3534 }
3535
3536 static PyObject *
test_xincref_doesnt_leak(PyObject * ob,PyObject * Py_UNUSED (ignored))3537 test_xincref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
3538 {
3539 PyObject *obj = PyLong_FromLong(0);
3540 Py_XINCREF(_test_incref(obj));
3541 Py_DECREF(obj);
3542 Py_DECREF(obj);
3543 Py_DECREF(obj);
3544 Py_RETURN_NONE;
3545 }
3546
3547 static PyObject *
test_incref_doesnt_leak(PyObject * ob,PyObject * Py_UNUSED (ignored))3548 test_incref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
3549 {
3550 PyObject *obj = PyLong_FromLong(0);
3551 Py_INCREF(_test_incref(obj));
3552 Py_DECREF(obj);
3553 Py_DECREF(obj);
3554 Py_DECREF(obj);
3555 Py_RETURN_NONE;
3556 }
3557
3558 static PyObject *
test_xdecref_doesnt_leak(PyObject * ob,PyObject * Py_UNUSED (ignored))3559 test_xdecref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
3560 {
3561 Py_XDECREF(PyLong_FromLong(0));
3562 Py_RETURN_NONE;
3563 }
3564
3565 static PyObject *
test_decref_doesnt_leak(PyObject * ob,PyObject * Py_UNUSED (ignored))3566 test_decref_doesnt_leak(PyObject *ob, PyObject *Py_UNUSED(ignored))
3567 {
3568 Py_DECREF(PyLong_FromLong(0));
3569 Py_RETURN_NONE;
3570 }
3571
3572 static PyObject *
test_structseq_newtype_doesnt_leak(PyObject * Py_UNUSED (self),PyObject * Py_UNUSED (args))3573 test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
3574 PyObject *Py_UNUSED(args))
3575 {
3576 PyStructSequence_Desc descr;
3577 PyStructSequence_Field descr_fields[3];
3578
3579 descr_fields[0] = (PyStructSequence_Field){"foo", "foo value"};
3580 descr_fields[1] = (PyStructSequence_Field){NULL, "some hidden value"};
3581 descr_fields[2] = (PyStructSequence_Field){0, NULL};
3582
3583 descr.name = "_testcapi.test_descr";
3584 descr.doc = "This is used to test for memory leaks in NewType";
3585 descr.fields = descr_fields;
3586 descr.n_in_sequence = 1;
3587
3588 PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
3589 assert(structseq_type != NULL);
3590 assert(PyType_Check(structseq_type));
3591 assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
3592 Py_DECREF(structseq_type);
3593
3594 Py_RETURN_NONE;
3595 }
3596
3597 static PyObject *
test_incref_decref_API(PyObject * ob,PyObject * Py_UNUSED (ignored))3598 test_incref_decref_API(PyObject *ob, PyObject *Py_UNUSED(ignored))
3599 {
3600 PyObject *obj = PyLong_FromLong(0);
3601 Py_IncRef(obj);
3602 Py_DecRef(obj);
3603 Py_DecRef(obj);
3604 Py_RETURN_NONE;
3605 }
3606
3607 static PyObject *
test_pymem_alloc0(PyObject * self,PyObject * Py_UNUSED (ignored))3608 test_pymem_alloc0(PyObject *self, PyObject *Py_UNUSED(ignored))
3609 {
3610 void *ptr;
3611
3612 ptr = PyMem_RawMalloc(0);
3613 if (ptr == NULL) {
3614 PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL");
3615 return NULL;
3616 }
3617 PyMem_RawFree(ptr);
3618
3619 ptr = PyMem_RawCalloc(0, 0);
3620 if (ptr == NULL) {
3621 PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL");
3622 return NULL;
3623 }
3624 PyMem_RawFree(ptr);
3625
3626 ptr = PyMem_Malloc(0);
3627 if (ptr == NULL) {
3628 PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL");
3629 return NULL;
3630 }
3631 PyMem_Free(ptr);
3632
3633 ptr = PyMem_Calloc(0, 0);
3634 if (ptr == NULL) {
3635 PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL");
3636 return NULL;
3637 }
3638 PyMem_Free(ptr);
3639
3640 ptr = PyObject_Malloc(0);
3641 if (ptr == NULL) {
3642 PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL");
3643 return NULL;
3644 }
3645 PyObject_Free(ptr);
3646
3647 ptr = PyObject_Calloc(0, 0);
3648 if (ptr == NULL) {
3649 PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL");
3650 return NULL;
3651 }
3652 PyObject_Free(ptr);
3653
3654 Py_RETURN_NONE;
3655 }
3656
3657 typedef struct {
3658 PyMemAllocatorEx alloc;
3659
3660 size_t malloc_size;
3661 size_t calloc_nelem;
3662 size_t calloc_elsize;
3663 void *realloc_ptr;
3664 size_t realloc_new_size;
3665 void *free_ptr;
3666 void *ctx;
3667 } alloc_hook_t;
3668
hook_malloc(void * ctx,size_t size)3669 static void* hook_malloc(void* ctx, size_t size)
3670 {
3671 alloc_hook_t *hook = (alloc_hook_t *)ctx;
3672 hook->ctx = ctx;
3673 hook->malloc_size = size;
3674 return hook->alloc.malloc(hook->alloc.ctx, size);
3675 }
3676
hook_calloc(void * ctx,size_t nelem,size_t elsize)3677 static void* hook_calloc(void* ctx, size_t nelem, size_t elsize)
3678 {
3679 alloc_hook_t *hook = (alloc_hook_t *)ctx;
3680 hook->ctx = ctx;
3681 hook->calloc_nelem = nelem;
3682 hook->calloc_elsize = elsize;
3683 return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize);
3684 }
3685
hook_realloc(void * ctx,void * ptr,size_t new_size)3686 static void* hook_realloc(void* ctx, void* ptr, size_t new_size)
3687 {
3688 alloc_hook_t *hook = (alloc_hook_t *)ctx;
3689 hook->ctx = ctx;
3690 hook->realloc_ptr = ptr;
3691 hook->realloc_new_size = new_size;
3692 return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size);
3693 }
3694
hook_free(void * ctx,void * ptr)3695 static void hook_free(void *ctx, void *ptr)
3696 {
3697 alloc_hook_t *hook = (alloc_hook_t *)ctx;
3698 hook->ctx = ctx;
3699 hook->free_ptr = ptr;
3700 hook->alloc.free(hook->alloc.ctx, ptr);
3701 }
3702
3703 static PyObject *
test_setallocators(PyMemAllocatorDomain domain)3704 test_setallocators(PyMemAllocatorDomain domain)
3705 {
3706 PyObject *res = NULL;
3707 const char *error_msg;
3708 alloc_hook_t hook;
3709 PyMemAllocatorEx alloc;
3710 size_t size, size2, nelem, elsize;
3711 void *ptr, *ptr2;
3712
3713 memset(&hook, 0, sizeof(hook));
3714
3715 alloc.ctx = &hook;
3716 alloc.malloc = &hook_malloc;
3717 alloc.calloc = &hook_calloc;
3718 alloc.realloc = &hook_realloc;
3719 alloc.free = &hook_free;
3720 PyMem_GetAllocator(domain, &hook.alloc);
3721 PyMem_SetAllocator(domain, &alloc);
3722
3723 /* malloc, realloc, free */
3724 size = 42;
3725 hook.ctx = NULL;
3726 switch(domain)
3727 {
3728 case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break;
3729 case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break;
3730 case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break;
3731 default: ptr = NULL; break;
3732 }
3733
3734 #define CHECK_CTX(FUNC) \
3735 if (hook.ctx != &hook) { \
3736 error_msg = FUNC " wrong context"; \
3737 goto fail; \
3738 } \
3739 hook.ctx = NULL; /* reset for next check */
3740
3741 if (ptr == NULL) {
3742 error_msg = "malloc failed";
3743 goto fail;
3744 }
3745 CHECK_CTX("malloc");
3746 if (hook.malloc_size != size) {
3747 error_msg = "malloc invalid size";
3748 goto fail;
3749 }
3750
3751 size2 = 200;
3752 switch(domain)
3753 {
3754 case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break;
3755 case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break;
3756 case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break;
3757 default: ptr2 = NULL; break;
3758 }
3759
3760 if (ptr2 == NULL) {
3761 error_msg = "realloc failed";
3762 goto fail;
3763 }
3764 CHECK_CTX("realloc");
3765 if (hook.realloc_ptr != ptr
3766 || hook.realloc_new_size != size2) {
3767 error_msg = "realloc invalid parameters";
3768 goto fail;
3769 }
3770
3771 switch(domain)
3772 {
3773 case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break;
3774 case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break;
3775 case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break;
3776 }
3777
3778 CHECK_CTX("free");
3779 if (hook.free_ptr != ptr2) {
3780 error_msg = "free invalid pointer";
3781 goto fail;
3782 }
3783
3784 /* calloc, free */
3785 nelem = 2;
3786 elsize = 5;
3787 switch(domain)
3788 {
3789 case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break;
3790 case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break;
3791 case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break;
3792 default: ptr = NULL; break;
3793 }
3794
3795 if (ptr == NULL) {
3796 error_msg = "calloc failed";
3797 goto fail;
3798 }
3799 CHECK_CTX("calloc");
3800 if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) {
3801 error_msg = "calloc invalid nelem or elsize";
3802 goto fail;
3803 }
3804
3805 hook.free_ptr = NULL;
3806 switch(domain)
3807 {
3808 case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break;
3809 case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break;
3810 case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break;
3811 }
3812
3813 CHECK_CTX("calloc free");
3814 if (hook.free_ptr != ptr) {
3815 error_msg = "calloc free invalid pointer";
3816 goto fail;
3817 }
3818
3819 Py_INCREF(Py_None);
3820 res = Py_None;
3821 goto finally;
3822
3823 fail:
3824 PyErr_SetString(PyExc_RuntimeError, error_msg);
3825
3826 finally:
3827 PyMem_SetAllocator(domain, &hook.alloc);
3828 return res;
3829
3830 #undef CHECK_CTX
3831 }
3832
3833 static PyObject *
test_pymem_setrawallocators(PyObject * self,PyObject * Py_UNUSED (ignored))3834 test_pymem_setrawallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
3835 {
3836 return test_setallocators(PYMEM_DOMAIN_RAW);
3837 }
3838
3839 static PyObject *
test_pymem_setallocators(PyObject * self,PyObject * Py_UNUSED (ignored))3840 test_pymem_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
3841 {
3842 return test_setallocators(PYMEM_DOMAIN_MEM);
3843 }
3844
3845 static PyObject *
test_pyobject_setallocators(PyObject * self,PyObject * Py_UNUSED (ignored))3846 test_pyobject_setallocators(PyObject *self, PyObject *Py_UNUSED(ignored))
3847 {
3848 return test_setallocators(PYMEM_DOMAIN_OBJ);
3849 }
3850
3851 /* Most part of the following code is inherited from the pyfailmalloc project
3852 * written by Victor Stinner. */
3853 static struct {
3854 int installed;
3855 PyMemAllocatorEx raw;
3856 PyMemAllocatorEx mem;
3857 PyMemAllocatorEx obj;
3858 } FmHook;
3859
3860 static struct {
3861 int start;
3862 int stop;
3863 Py_ssize_t count;
3864 } FmData;
3865
3866 static int
fm_nomemory(void)3867 fm_nomemory(void)
3868 {
3869 FmData.count++;
3870 if (FmData.count > FmData.start &&
3871 (FmData.stop <= 0 || FmData.count <= FmData.stop)) {
3872 return 1;
3873 }
3874 return 0;
3875 }
3876
3877 static void *
hook_fmalloc(void * ctx,size_t size)3878 hook_fmalloc(void *ctx, size_t size)
3879 {
3880 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3881 if (fm_nomemory()) {
3882 return NULL;
3883 }
3884 return alloc->malloc(alloc->ctx, size);
3885 }
3886
3887 static void *
hook_fcalloc(void * ctx,size_t nelem,size_t elsize)3888 hook_fcalloc(void *ctx, size_t nelem, size_t elsize)
3889 {
3890 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3891 if (fm_nomemory()) {
3892 return NULL;
3893 }
3894 return alloc->calloc(alloc->ctx, nelem, elsize);
3895 }
3896
3897 static void *
hook_frealloc(void * ctx,void * ptr,size_t new_size)3898 hook_frealloc(void *ctx, void *ptr, size_t new_size)
3899 {
3900 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3901 if (fm_nomemory()) {
3902 return NULL;
3903 }
3904 return alloc->realloc(alloc->ctx, ptr, new_size);
3905 }
3906
3907 static void
hook_ffree(void * ctx,void * ptr)3908 hook_ffree(void *ctx, void *ptr)
3909 {
3910 PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
3911 alloc->free(alloc->ctx, ptr);
3912 }
3913
3914 static void
fm_setup_hooks(void)3915 fm_setup_hooks(void)
3916 {
3917 PyMemAllocatorEx alloc;
3918
3919 if (FmHook.installed) {
3920 return;
3921 }
3922 FmHook.installed = 1;
3923
3924 alloc.malloc = hook_fmalloc;
3925 alloc.calloc = hook_fcalloc;
3926 alloc.realloc = hook_frealloc;
3927 alloc.free = hook_ffree;
3928 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
3929 PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
3930 PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
3931
3932 alloc.ctx = &FmHook.raw;
3933 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
3934
3935 alloc.ctx = &FmHook.mem;
3936 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
3937
3938 alloc.ctx = &FmHook.obj;
3939 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc);
3940 }
3941
3942 static void
fm_remove_hooks(void)3943 fm_remove_hooks(void)
3944 {
3945 if (FmHook.installed) {
3946 FmHook.installed = 0;
3947 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &FmHook.raw);
3948 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &FmHook.mem);
3949 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &FmHook.obj);
3950 }
3951 }
3952
3953 static PyObject*
set_nomemory(PyObject * self,PyObject * args)3954 set_nomemory(PyObject *self, PyObject *args)
3955 {
3956 /* Memory allocation fails after 'start' allocation requests, and until
3957 * 'stop' allocation requests except when 'stop' is negative or equal
3958 * to 0 (default) in which case allocation failures never stop. */
3959 FmData.count = 0;
3960 FmData.stop = 0;
3961 if (!PyArg_ParseTuple(args, "i|i", &FmData.start, &FmData.stop)) {
3962 return NULL;
3963 }
3964 fm_setup_hooks();
3965 Py_RETURN_NONE;
3966 }
3967
3968 static PyObject*
remove_mem_hooks(PyObject * self,PyObject * Py_UNUSED (ignored))3969 remove_mem_hooks(PyObject *self, PyObject *Py_UNUSED(ignored))
3970 {
3971 fm_remove_hooks();
3972 Py_RETURN_NONE;
3973 }
3974
3975 PyDoc_STRVAR(docstring_empty,
3976 ""
3977 );
3978
3979 PyDoc_STRVAR(docstring_no_signature,
3980 "This docstring has no signature."
3981 );
3982
3983 PyDoc_STRVAR(docstring_with_invalid_signature,
3984 "docstring_with_invalid_signature($module, /, boo)\n"
3985 "\n"
3986 "This docstring has an invalid signature."
3987 );
3988
3989 PyDoc_STRVAR(docstring_with_invalid_signature2,
3990 "docstring_with_invalid_signature2($module, /, boo)\n"
3991 "\n"
3992 "--\n"
3993 "\n"
3994 "This docstring also has an invalid signature."
3995 );
3996
3997 PyDoc_STRVAR(docstring_with_signature,
3998 "docstring_with_signature($module, /, sig)\n"
3999 "--\n"
4000 "\n"
4001 "This docstring has a valid signature."
4002 );
4003
4004 PyDoc_STRVAR(docstring_with_signature_but_no_doc,
4005 "docstring_with_signature_but_no_doc($module, /, sig)\n"
4006 "--\n"
4007 "\n"
4008 );
4009
4010 PyDoc_STRVAR(docstring_with_signature_and_extra_newlines,
4011 "docstring_with_signature_and_extra_newlines($module, /, parameter)\n"
4012 "--\n"
4013 "\n"
4014 "\n"
4015 "This docstring has a valid signature and some extra newlines."
4016 );
4017
4018 PyDoc_STRVAR(docstring_with_signature_with_defaults,
4019 "docstring_with_signature_with_defaults(module, s='avocado',\n"
4020 " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n"
4021 " local=the_number_three, sys=sys.maxsize,\n"
4022 " exp=sys.maxsize - 1)\n"
4023 "--\n"
4024 "\n"
4025 "\n"
4026 "\n"
4027 "This docstring has a valid signature with parameters,\n"
4028 "and the parameters take defaults of varying types."
4029 );
4030
4031 typedef struct {
4032 PyThread_type_lock start_event;
4033 PyThread_type_lock exit_event;
4034 PyObject *callback;
4035 } test_c_thread_t;
4036
4037 static void
temporary_c_thread(void * data)4038 temporary_c_thread(void *data)
4039 {
4040 test_c_thread_t *test_c_thread = data;
4041 PyGILState_STATE state;
4042 PyObject *res;
4043
4044 PyThread_release_lock(test_c_thread->start_event);
4045
4046 /* Allocate a Python thread state for this thread */
4047 state = PyGILState_Ensure();
4048
4049 res = _PyObject_CallNoArg(test_c_thread->callback);
4050 Py_CLEAR(test_c_thread->callback);
4051
4052 if (res == NULL) {
4053 PyErr_Print();
4054 }
4055 else {
4056 Py_DECREF(res);
4057 }
4058
4059 /* Destroy the Python thread state for this thread */
4060 PyGILState_Release(state);
4061
4062 PyThread_release_lock(test_c_thread->exit_event);
4063
4064 PyThread_exit_thread();
4065 }
4066
4067 static PyObject *
call_in_temporary_c_thread(PyObject * self,PyObject * callback)4068 call_in_temporary_c_thread(PyObject *self, PyObject *callback)
4069 {
4070 PyObject *res = NULL;
4071 test_c_thread_t test_c_thread;
4072 long thread;
4073
4074 PyEval_InitThreads();
4075
4076 test_c_thread.start_event = PyThread_allocate_lock();
4077 test_c_thread.exit_event = PyThread_allocate_lock();
4078 test_c_thread.callback = NULL;
4079 if (!test_c_thread.start_event || !test_c_thread.exit_event) {
4080 PyErr_SetString(PyExc_RuntimeError, "could not allocate lock");
4081 goto exit;
4082 }
4083
4084 Py_INCREF(callback);
4085 test_c_thread.callback = callback;
4086
4087 PyThread_acquire_lock(test_c_thread.start_event, 1);
4088 PyThread_acquire_lock(test_c_thread.exit_event, 1);
4089
4090 thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread);
4091 if (thread == -1) {
4092 PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
4093 PyThread_release_lock(test_c_thread.start_event);
4094 PyThread_release_lock(test_c_thread.exit_event);
4095 goto exit;
4096 }
4097
4098 PyThread_acquire_lock(test_c_thread.start_event, 1);
4099 PyThread_release_lock(test_c_thread.start_event);
4100
4101 Py_BEGIN_ALLOW_THREADS
4102 PyThread_acquire_lock(test_c_thread.exit_event, 1);
4103 PyThread_release_lock(test_c_thread.exit_event);
4104 Py_END_ALLOW_THREADS
4105
4106 Py_INCREF(Py_None);
4107 res = Py_None;
4108
4109 exit:
4110 Py_CLEAR(test_c_thread.callback);
4111 if (test_c_thread.start_event)
4112 PyThread_free_lock(test_c_thread.start_event);
4113 if (test_c_thread.exit_event)
4114 PyThread_free_lock(test_c_thread.exit_event);
4115 return res;
4116 }
4117
4118 /* marshal */
4119
4120 static PyObject*
pymarshal_write_long_to_file(PyObject * self,PyObject * args)4121 pymarshal_write_long_to_file(PyObject* self, PyObject *args)
4122 {
4123 long value;
4124 char *filename;
4125 int version;
4126 FILE *fp;
4127
4128 if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file",
4129 &value, &filename, &version))
4130 return NULL;
4131
4132 fp = fopen(filename, "wb");
4133 if (fp == NULL) {
4134 PyErr_SetFromErrno(PyExc_OSError);
4135 return NULL;
4136 }
4137
4138 PyMarshal_WriteLongToFile(value, fp, version);
4139
4140 fclose(fp);
4141 if (PyErr_Occurred())
4142 return NULL;
4143 Py_RETURN_NONE;
4144 }
4145
4146 static PyObject*
pymarshal_write_object_to_file(PyObject * self,PyObject * args)4147 pymarshal_write_object_to_file(PyObject* self, PyObject *args)
4148 {
4149 PyObject *obj;
4150 char *filename;
4151 int version;
4152 FILE *fp;
4153
4154 if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file",
4155 &obj, &filename, &version))
4156 return NULL;
4157
4158 fp = fopen(filename, "wb");
4159 if (fp == NULL) {
4160 PyErr_SetFromErrno(PyExc_OSError);
4161 return NULL;
4162 }
4163
4164 PyMarshal_WriteObjectToFile(obj, fp, version);
4165
4166 fclose(fp);
4167 if (PyErr_Occurred())
4168 return NULL;
4169 Py_RETURN_NONE;
4170 }
4171
4172 static PyObject*
pymarshal_read_short_from_file(PyObject * self,PyObject * args)4173 pymarshal_read_short_from_file(PyObject* self, PyObject *args)
4174 {
4175 int value;
4176 long pos;
4177 char *filename;
4178 FILE *fp;
4179
4180 if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename))
4181 return NULL;
4182
4183 fp = fopen(filename, "rb");
4184 if (fp == NULL) {
4185 PyErr_SetFromErrno(PyExc_OSError);
4186 return NULL;
4187 }
4188
4189 value = PyMarshal_ReadShortFromFile(fp);
4190 pos = ftell(fp);
4191
4192 fclose(fp);
4193 if (PyErr_Occurred())
4194 return NULL;
4195 return Py_BuildValue("il", value, pos);
4196 }
4197
4198 static PyObject*
pymarshal_read_long_from_file(PyObject * self,PyObject * args)4199 pymarshal_read_long_from_file(PyObject* self, PyObject *args)
4200 {
4201 long value, pos;
4202 char *filename;
4203 FILE *fp;
4204
4205 if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename))
4206 return NULL;
4207
4208 fp = fopen(filename, "rb");
4209 if (fp == NULL) {
4210 PyErr_SetFromErrno(PyExc_OSError);
4211 return NULL;
4212 }
4213
4214 value = PyMarshal_ReadLongFromFile(fp);
4215 pos = ftell(fp);
4216
4217 fclose(fp);
4218 if (PyErr_Occurred())
4219 return NULL;
4220 return Py_BuildValue("ll", value, pos);
4221 }
4222
4223 static PyObject*
pymarshal_read_last_object_from_file(PyObject * self,PyObject * args)4224 pymarshal_read_last_object_from_file(PyObject* self, PyObject *args)
4225 {
4226 PyObject *obj;
4227 long pos;
4228 char *filename;
4229 FILE *fp;
4230
4231 if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename))
4232 return NULL;
4233
4234 fp = fopen(filename, "rb");
4235 if (fp == NULL) {
4236 PyErr_SetFromErrno(PyExc_OSError);
4237 return NULL;
4238 }
4239
4240 obj = PyMarshal_ReadLastObjectFromFile(fp);
4241 pos = ftell(fp);
4242
4243 fclose(fp);
4244 return Py_BuildValue("Nl", obj, pos);
4245 }
4246
4247 static PyObject*
pymarshal_read_object_from_file(PyObject * self,PyObject * args)4248 pymarshal_read_object_from_file(PyObject* self, PyObject *args)
4249 {
4250 PyObject *obj;
4251 long pos;
4252 char *filename;
4253 FILE *fp;
4254
4255 if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename))
4256 return NULL;
4257
4258 fp = fopen(filename, "rb");
4259 if (fp == NULL) {
4260 PyErr_SetFromErrno(PyExc_OSError);
4261 return NULL;
4262 }
4263
4264 obj = PyMarshal_ReadObjectFromFile(fp);
4265 pos = ftell(fp);
4266
4267 fclose(fp);
4268 return Py_BuildValue("Nl", obj, pos);
4269 }
4270
4271 static PyObject*
return_null_without_error(PyObject * self,PyObject * args)4272 return_null_without_error(PyObject *self, PyObject *args)
4273 {
4274 /* invalid call: return NULL without setting an error,
4275 * _Py_CheckFunctionResult() must detect such bug at runtime. */
4276 PyErr_Clear();
4277 return NULL;
4278 }
4279
4280 static PyObject*
return_result_with_error(PyObject * self,PyObject * args)4281 return_result_with_error(PyObject *self, PyObject *args)
4282 {
4283 /* invalid call: return a result with an error set,
4284 * _Py_CheckFunctionResult() must detect such bug at runtime. */
4285 PyErr_SetNone(PyExc_ValueError);
4286 Py_RETURN_NONE;
4287 }
4288
4289 static PyObject *
test_pytime_fromseconds(PyObject * self,PyObject * args)4290 test_pytime_fromseconds(PyObject *self, PyObject *args)
4291 {
4292 int seconds;
4293 _PyTime_t ts;
4294
4295 if (!PyArg_ParseTuple(args, "i", &seconds))
4296 return NULL;
4297 ts = _PyTime_FromSeconds(seconds);
4298 return _PyTime_AsNanosecondsObject(ts);
4299 }
4300
4301 static PyObject *
test_pytime_fromsecondsobject(PyObject * self,PyObject * args)4302 test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
4303 {
4304 PyObject *obj;
4305 int round;
4306 _PyTime_t ts;
4307
4308 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
4309 return NULL;
4310 if (check_time_rounding(round) < 0)
4311 return NULL;
4312 if (_PyTime_FromSecondsObject(&ts, obj, round) == -1)
4313 return NULL;
4314 return _PyTime_AsNanosecondsObject(ts);
4315 }
4316
4317 static PyObject *
test_pytime_assecondsdouble(PyObject * self,PyObject * args)4318 test_pytime_assecondsdouble(PyObject *self, PyObject *args)
4319 {
4320 PyObject *obj;
4321 _PyTime_t ts;
4322 double d;
4323
4324 if (!PyArg_ParseTuple(args, "O", &obj)) {
4325 return NULL;
4326 }
4327 if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) {
4328 return NULL;
4329 }
4330 d = _PyTime_AsSecondsDouble(ts);
4331 return PyFloat_FromDouble(d);
4332 }
4333
4334 static PyObject *
test_PyTime_AsTimeval(PyObject * self,PyObject * args)4335 test_PyTime_AsTimeval(PyObject *self, PyObject *args)
4336 {
4337 PyObject *obj;
4338 int round;
4339 _PyTime_t t;
4340 struct timeval tv;
4341 PyObject *seconds;
4342
4343 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
4344 return NULL;
4345 if (check_time_rounding(round) < 0) {
4346 return NULL;
4347 }
4348 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
4349 return NULL;
4350 }
4351 if (_PyTime_AsTimeval(t, &tv, round) < 0) {
4352 return NULL;
4353 }
4354
4355 seconds = PyLong_FromLongLong(tv.tv_sec);
4356 if (seconds == NULL) {
4357 return NULL;
4358 }
4359 return Py_BuildValue("Nl", seconds, tv.tv_usec);
4360 }
4361
4362 #ifdef HAVE_CLOCK_GETTIME
4363 static PyObject *
test_PyTime_AsTimespec(PyObject * self,PyObject * args)4364 test_PyTime_AsTimespec(PyObject *self, PyObject *args)
4365 {
4366 PyObject *obj;
4367 _PyTime_t t;
4368 struct timespec ts;
4369
4370 if (!PyArg_ParseTuple(args, "O", &obj)) {
4371 return NULL;
4372 }
4373 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
4374 return NULL;
4375 }
4376 if (_PyTime_AsTimespec(t, &ts) == -1) {
4377 return NULL;
4378 }
4379 return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
4380 }
4381 #endif
4382
4383 static PyObject *
test_PyTime_AsMilliseconds(PyObject * self,PyObject * args)4384 test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
4385 {
4386 PyObject *obj;
4387 int round;
4388 _PyTime_t t, ms;
4389
4390 if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
4391 return NULL;
4392 }
4393 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
4394 return NULL;
4395 }
4396 if (check_time_rounding(round) < 0) {
4397 return NULL;
4398 }
4399 ms = _PyTime_AsMilliseconds(t, round);
4400 /* This conversion rely on the fact that _PyTime_t is a number of
4401 nanoseconds */
4402 return _PyTime_AsNanosecondsObject(ms);
4403 }
4404
4405 static PyObject *
test_PyTime_AsMicroseconds(PyObject * self,PyObject * args)4406 test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
4407 {
4408 PyObject *obj;
4409 int round;
4410 _PyTime_t t, ms;
4411
4412 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
4413 return NULL;
4414 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
4415 return NULL;
4416 }
4417 if (check_time_rounding(round) < 0) {
4418 return NULL;
4419 }
4420 ms = _PyTime_AsMicroseconds(t, round);
4421 /* This conversion rely on the fact that _PyTime_t is a number of
4422 nanoseconds */
4423 return _PyTime_AsNanosecondsObject(ms);
4424 }
4425
4426 static PyObject*
get_recursion_depth(PyObject * self,PyObject * args)4427 get_recursion_depth(PyObject *self, PyObject *args)
4428 {
4429 PyThreadState *tstate = PyThreadState_Get();
4430
4431 /* subtract one to ignore the frame of the get_recursion_depth() call */
4432 return PyLong_FromLong(tstate->recursion_depth - 1);
4433 }
4434
4435 static PyObject*
pymem_buffer_overflow(PyObject * self,PyObject * args)4436 pymem_buffer_overflow(PyObject *self, PyObject *args)
4437 {
4438 char *buffer;
4439
4440 /* Deliberate buffer overflow to check that PyMem_Free() detects
4441 the overflow when debug hooks are installed. */
4442 buffer = PyMem_Malloc(16);
4443 if (buffer == NULL) {
4444 PyErr_NoMemory();
4445 return NULL;
4446 }
4447 buffer[16] = 'x';
4448 PyMem_Free(buffer);
4449
4450 Py_RETURN_NONE;
4451 }
4452
4453 static PyObject*
pymem_api_misuse(PyObject * self,PyObject * args)4454 pymem_api_misuse(PyObject *self, PyObject *args)
4455 {
4456 char *buffer;
4457
4458 /* Deliberate misusage of Python allocators:
4459 allococate with PyMem but release with PyMem_Raw. */
4460 buffer = PyMem_Malloc(16);
4461 PyMem_RawFree(buffer);
4462
4463 Py_RETURN_NONE;
4464 }
4465
4466 static PyObject*
pymem_malloc_without_gil(PyObject * self,PyObject * args)4467 pymem_malloc_without_gil(PyObject *self, PyObject *args)
4468 {
4469 char *buffer;
4470
4471 /* Deliberate bug to test debug hooks on Python memory allocators:
4472 call PyMem_Malloc() without holding the GIL */
4473 Py_BEGIN_ALLOW_THREADS
4474 buffer = PyMem_Malloc(10);
4475 Py_END_ALLOW_THREADS
4476
4477 PyMem_Free(buffer);
4478
4479 Py_RETURN_NONE;
4480 }
4481
4482
4483 static PyObject*
test_pymem_getallocatorsname(PyObject * self,PyObject * args)4484 test_pymem_getallocatorsname(PyObject *self, PyObject *args)
4485 {
4486 const char *name = _PyMem_GetCurrentAllocatorName();
4487 if (name == NULL) {
4488 PyErr_SetString(PyExc_RuntimeError, "cannot get allocators name");
4489 return NULL;
4490 }
4491 return PyUnicode_FromString(name);
4492 }
4493
4494
4495 static PyObject*
test_pyobject_is_freed(const char * test_name,PyObject * op)4496 test_pyobject_is_freed(const char *test_name, PyObject *op)
4497 {
4498 if (!_PyObject_IsFreed(op)) {
4499 return raiseTestError(test_name, "object is not seen as freed");
4500 }
4501 Py_RETURN_NONE;
4502 }
4503
4504
4505 static PyObject*
check_pyobject_null_is_freed(PyObject * self,PyObject * Py_UNUSED (args))4506 check_pyobject_null_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
4507 {
4508 PyObject *op = NULL;
4509 return test_pyobject_is_freed("check_pyobject_null_is_freed", op);
4510 }
4511
4512
4513 static PyObject*
check_pyobject_uninitialized_is_freed(PyObject * self,PyObject * Py_UNUSED (args))4514 check_pyobject_uninitialized_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
4515 {
4516 PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject));
4517 if (op == NULL) {
4518 return NULL;
4519 }
4520 /* Initialize reference count to avoid early crash in ceval or GC */
4521 Py_REFCNT(op) = 1;
4522 /* object fields like ob_type are uninitialized! */
4523 return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op);
4524 }
4525
4526
4527 static PyObject*
check_pyobject_forbidden_bytes_is_freed(PyObject * self,PyObject * Py_UNUSED (args))4528 check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
4529 {
4530 /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */
4531 PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type));
4532 if (op == NULL) {
4533 return NULL;
4534 }
4535 /* Initialize reference count to avoid early crash in ceval or GC */
4536 Py_REFCNT(op) = 1;
4537 /* ob_type field is after the memory block: part of "forbidden bytes"
4538 when using debug hooks on memory allocators! */
4539 return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op);
4540 }
4541
4542
4543 static PyObject*
check_pyobject_freed_is_freed(PyObject * self,PyObject * Py_UNUSED (args))4544 check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
4545 {
4546 PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
4547 if (op == NULL) {
4548 return NULL;
4549 }
4550 Py_TYPE(op)->tp_dealloc(op);
4551 /* Reset reference count to avoid early crash in ceval or GC */
4552 Py_REFCNT(op) = 1;
4553 /* object memory is freed! */
4554 return test_pyobject_is_freed("check_pyobject_freed_is_freed", op);
4555 }
4556
4557
4558 static PyObject*
pyobject_malloc_without_gil(PyObject * self,PyObject * args)4559 pyobject_malloc_without_gil(PyObject *self, PyObject *args)
4560 {
4561 char *buffer;
4562
4563 /* Deliberate bug to test debug hooks on Python memory allocators:
4564 call PyObject_Malloc() without holding the GIL */
4565 Py_BEGIN_ALLOW_THREADS
4566 buffer = PyObject_Malloc(10);
4567 Py_END_ALLOW_THREADS
4568
4569 PyObject_Free(buffer);
4570
4571 Py_RETURN_NONE;
4572 }
4573
4574 static PyObject *
tracemalloc_track(PyObject * self,PyObject * args)4575 tracemalloc_track(PyObject *self, PyObject *args)
4576 {
4577 unsigned int domain;
4578 PyObject *ptr_obj;
4579 void *ptr;
4580 Py_ssize_t size;
4581 int release_gil = 0;
4582 int res;
4583
4584 if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil))
4585 return NULL;
4586 ptr = PyLong_AsVoidPtr(ptr_obj);
4587 if (PyErr_Occurred())
4588 return NULL;
4589
4590 if (release_gil) {
4591 Py_BEGIN_ALLOW_THREADS
4592 res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
4593 Py_END_ALLOW_THREADS
4594 }
4595 else {
4596 res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
4597 }
4598
4599 if (res < 0) {
4600 PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error");
4601 return NULL;
4602 }
4603
4604 Py_RETURN_NONE;
4605 }
4606
4607 static PyObject *
tracemalloc_untrack(PyObject * self,PyObject * args)4608 tracemalloc_untrack(PyObject *self, PyObject *args)
4609 {
4610 unsigned int domain;
4611 PyObject *ptr_obj;
4612 void *ptr;
4613 int res;
4614
4615 if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4616 return NULL;
4617 ptr = PyLong_AsVoidPtr(ptr_obj);
4618 if (PyErr_Occurred())
4619 return NULL;
4620
4621 res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr);
4622 if (res < 0) {
4623 PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error");
4624 return NULL;
4625 }
4626
4627 Py_RETURN_NONE;
4628 }
4629
4630 static PyObject *
tracemalloc_get_traceback(PyObject * self,PyObject * args)4631 tracemalloc_get_traceback(PyObject *self, PyObject *args)
4632 {
4633 unsigned int domain;
4634 PyObject *ptr_obj;
4635 void *ptr;
4636
4637 if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4638 return NULL;
4639 ptr = PyLong_AsVoidPtr(ptr_obj);
4640 if (PyErr_Occurred())
4641 return NULL;
4642
4643 return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
4644 }
4645
4646 static PyObject *
dict_get_version(PyObject * self,PyObject * args)4647 dict_get_version(PyObject *self, PyObject *args)
4648 {
4649 PyDictObject *dict;
4650 uint64_t version;
4651
4652 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict))
4653 return NULL;
4654
4655 version = dict->ma_version_tag;
4656
4657 Py_BUILD_ASSERT(sizeof(unsigned PY_LONG_LONG) >= sizeof(version));
4658 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)version);
4659 }
4660
4661
4662 static PyObject *
raise_SIGINT_then_send_None(PyObject * self,PyObject * args)4663 raise_SIGINT_then_send_None(PyObject *self, PyObject *args)
4664 {
4665 PyGenObject *gen;
4666
4667 if (!PyArg_ParseTuple(args, "O!", &PyGen_Type, &gen))
4668 return NULL;
4669
4670 /* This is used in a test to check what happens if a signal arrives just
4671 as we're in the process of entering a yield from chain (see
4672 bpo-30039).
4673
4674 Needs to be done in C, because:
4675 - we don't have a Python wrapper for raise()
4676 - we need to make sure that the Python-level signal handler doesn't run
4677 *before* we enter the generator frame, which is impossible in Python
4678 because we check for signals before every bytecode operation.
4679 */
4680 raise(SIGINT);
4681 return _PyGen_Send(gen, Py_None);
4682 }
4683
4684
4685 static int
fastcall_args(PyObject * args,PyObject *** stack,Py_ssize_t * nargs)4686 fastcall_args(PyObject *args, PyObject ***stack, Py_ssize_t *nargs)
4687 {
4688 if (args == Py_None) {
4689 *stack = NULL;
4690 *nargs = 0;
4691 }
4692 else if (PyTuple_Check(args)) {
4693 *stack = ((PyTupleObject *)args)->ob_item;
4694 *nargs = PyTuple_GET_SIZE(args);
4695 }
4696 else {
4697 PyErr_SetString(PyExc_TypeError, "args must be None or a tuple");
4698 return -1;
4699 }
4700 return 0;
4701 }
4702
4703
4704 static PyObject *
test_pyobject_fastcall(PyObject * self,PyObject * args)4705 test_pyobject_fastcall(PyObject *self, PyObject *args)
4706 {
4707 PyObject *func, *func_args;
4708 PyObject **stack;
4709 Py_ssize_t nargs;
4710
4711 if (!PyArg_ParseTuple(args, "OO", &func, &func_args)) {
4712 return NULL;
4713 }
4714
4715 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4716 return NULL;
4717 }
4718 return _PyObject_FastCall(func, stack, nargs);
4719 }
4720
4721
4722 static PyObject *
test_pyobject_fastcalldict(PyObject * self,PyObject * args)4723 test_pyobject_fastcalldict(PyObject *self, PyObject *args)
4724 {
4725 PyObject *func, *func_args, *kwargs;
4726 PyObject **stack;
4727 Py_ssize_t nargs;
4728
4729 if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwargs)) {
4730 return NULL;
4731 }
4732
4733 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4734 return NULL;
4735 }
4736
4737 if (kwargs == Py_None) {
4738 kwargs = NULL;
4739 }
4740 else if (!PyDict_Check(kwargs)) {
4741 PyErr_SetString(PyExc_TypeError, "kwnames must be None or a dict");
4742 return NULL;
4743 }
4744
4745 return _PyObject_FastCallDict(func, stack, nargs, kwargs);
4746 }
4747
4748
4749 static PyObject *
test_pyobject_vectorcall(PyObject * self,PyObject * args)4750 test_pyobject_vectorcall(PyObject *self, PyObject *args)
4751 {
4752 PyObject *func, *func_args, *kwnames = NULL;
4753 PyObject **stack;
4754 Py_ssize_t nargs, nkw;
4755
4756 if (!PyArg_ParseTuple(args, "OOO", &func, &func_args, &kwnames)) {
4757 return NULL;
4758 }
4759
4760 if (fastcall_args(func_args, &stack, &nargs) < 0) {
4761 return NULL;
4762 }
4763
4764 if (kwnames == Py_None) {
4765 kwnames = NULL;
4766 }
4767 else if (PyTuple_Check(kwnames)) {
4768 nkw = PyTuple_GET_SIZE(kwnames);
4769 if (nargs < nkw) {
4770 PyErr_SetString(PyExc_ValueError, "kwnames longer than args");
4771 return NULL;
4772 }
4773 nargs -= nkw;
4774 }
4775 else {
4776 PyErr_SetString(PyExc_TypeError, "kwnames must be None or a tuple");
4777 return NULL;
4778 }
4779 return _PyObject_Vectorcall(func, stack, nargs, kwnames);
4780 }
4781
4782
4783 static PyObject *
test_pyvectorcall_call(PyObject * self,PyObject * args)4784 test_pyvectorcall_call(PyObject *self, PyObject *args)
4785 {
4786 PyObject *func;
4787 PyObject *argstuple;
4788 PyObject *kwargs = NULL;
4789
4790 if (!PyArg_ParseTuple(args, "OO|O", &func, &argstuple, &kwargs)) {
4791 return NULL;
4792 }
4793
4794 if (!PyTuple_Check(argstuple)) {
4795 PyErr_SetString(PyExc_TypeError, "args must be a tuple");
4796 return NULL;
4797 }
4798 if (kwargs != NULL && !PyDict_Check(kwargs)) {
4799 PyErr_SetString(PyExc_TypeError, "kwargs must be a dict");
4800 return NULL;
4801 }
4802
4803 return PyVectorcall_Call(func, argstuple, kwargs);
4804 }
4805
4806
4807 static PyObject*
stack_pointer(PyObject * self,PyObject * args)4808 stack_pointer(PyObject *self, PyObject *args)
4809 {
4810 int v = 5;
4811 return PyLong_FromVoidPtr(&v);
4812 }
4813
4814
4815 #ifdef W_STOPCODE
4816 static PyObject*
py_w_stopcode(PyObject * self,PyObject * args)4817 py_w_stopcode(PyObject *self, PyObject *args)
4818 {
4819 int sig, status;
4820 if (!PyArg_ParseTuple(args, "i", &sig)) {
4821 return NULL;
4822 }
4823 status = W_STOPCODE(sig);
4824 return PyLong_FromLong(status);
4825 }
4826 #endif
4827
4828
4829 static PyObject *
get_mapping_keys(PyObject * self,PyObject * obj)4830 get_mapping_keys(PyObject* self, PyObject *obj)
4831 {
4832 return PyMapping_Keys(obj);
4833 }
4834
4835 static PyObject *
get_mapping_values(PyObject * self,PyObject * obj)4836 get_mapping_values(PyObject* self, PyObject *obj)
4837 {
4838 return PyMapping_Values(obj);
4839 }
4840
4841 static PyObject *
get_mapping_items(PyObject * self,PyObject * obj)4842 get_mapping_items(PyObject* self, PyObject *obj)
4843 {
4844 return PyMapping_Items(obj);
4845 }
4846
4847
4848 static PyObject *
test_pythread_tss_key_state(PyObject * self,PyObject * args)4849 test_pythread_tss_key_state(PyObject *self, PyObject *args)
4850 {
4851 Py_tss_t tss_key = Py_tss_NEEDS_INIT;
4852 if (PyThread_tss_is_created(&tss_key)) {
4853 return raiseTestError("test_pythread_tss_key_state",
4854 "TSS key not in an uninitialized state at "
4855 "creation time");
4856 }
4857 if (PyThread_tss_create(&tss_key) != 0) {
4858 PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_create failed");
4859 return NULL;
4860 }
4861 if (!PyThread_tss_is_created(&tss_key)) {
4862 return raiseTestError("test_pythread_tss_key_state",
4863 "PyThread_tss_create succeeded, "
4864 "but with TSS key in an uninitialized state");
4865 }
4866 if (PyThread_tss_create(&tss_key) != 0) {
4867 return raiseTestError("test_pythread_tss_key_state",
4868 "PyThread_tss_create unsuccessful with "
4869 "an already initialized key");
4870 }
4871 #define CHECK_TSS_API(expr) \
4872 (void)(expr); \
4873 if (!PyThread_tss_is_created(&tss_key)) { \
4874 return raiseTestError("test_pythread_tss_key_state", \
4875 "TSS key initialization state was not " \
4876 "preserved after calling " #expr); }
4877 CHECK_TSS_API(PyThread_tss_set(&tss_key, NULL));
4878 CHECK_TSS_API(PyThread_tss_get(&tss_key));
4879 #undef CHECK_TSS_API
4880 PyThread_tss_delete(&tss_key);
4881 if (PyThread_tss_is_created(&tss_key)) {
4882 return raiseTestError("test_pythread_tss_key_state",
4883 "PyThread_tss_delete called, but did not "
4884 "set the key state to uninitialized");
4885 }
4886
4887 Py_tss_t *ptr_key = PyThread_tss_alloc();
4888 if (ptr_key == NULL) {
4889 PyErr_SetString(PyExc_RuntimeError, "PyThread_tss_alloc failed");
4890 return NULL;
4891 }
4892 if (PyThread_tss_is_created(ptr_key)) {
4893 return raiseTestError("test_pythread_tss_key_state",
4894 "TSS key not in an uninitialized state at "
4895 "allocation time");
4896 }
4897 PyThread_tss_free(ptr_key);
4898 ptr_key = NULL;
4899 Py_RETURN_NONE;
4900 }
4901
4902
4903 static PyObject*
new_hamt(PyObject * self,PyObject * args)4904 new_hamt(PyObject *self, PyObject *args)
4905 {
4906 return _PyContext_NewHamtForTests();
4907 }
4908
4909
4910 /* def bad_get(self, obj, cls):
4911 cls()
4912 return repr(self)
4913 */
4914 static PyObject*
bad_get(PyObject * module,PyObject * const * args,Py_ssize_t nargs)4915 bad_get(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
4916 {
4917 PyObject *self, *obj, *cls;
4918 if (!_PyArg_UnpackStack(args, nargs, "bad_get", 3, 3, &self, &obj, &cls)) {
4919 return NULL;
4920 }
4921
4922 PyObject *res = PyObject_CallObject(cls, NULL);
4923 if (res == NULL) {
4924 return NULL;
4925 }
4926 Py_DECREF(res);
4927
4928 return PyObject_Repr(self);
4929 }
4930
4931
4932 static PyObject *
encode_locale_ex(PyObject * self,PyObject * args)4933 encode_locale_ex(PyObject *self, PyObject *args)
4934 {
4935 PyObject *unicode;
4936 int current_locale = 0;
4937 wchar_t *wstr;
4938 PyObject *res = NULL;
4939 const char *errors = NULL;
4940
4941 if (!PyArg_ParseTuple(args, "U|is", &unicode, ¤t_locale, &errors)) {
4942 return NULL;
4943 }
4944 wstr = PyUnicode_AsWideCharString(unicode, NULL);
4945 if (wstr == NULL) {
4946 return NULL;
4947 }
4948 _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
4949
4950 char *str = NULL;
4951 size_t error_pos;
4952 const char *reason = NULL;
4953 int ret = _Py_EncodeLocaleEx(wstr,
4954 &str, &error_pos, &reason,
4955 current_locale, error_handler);
4956 PyMem_Free(wstr);
4957
4958 switch(ret) {
4959 case 0:
4960 res = PyBytes_FromString(str);
4961 PyMem_RawFree(str);
4962 break;
4963 case -1:
4964 PyErr_NoMemory();
4965 break;
4966 case -2:
4967 PyErr_Format(PyExc_RuntimeError, "encode error: pos=%zu, reason=%s",
4968 error_pos, reason);
4969 break;
4970 case -3:
4971 PyErr_SetString(PyExc_ValueError, "unsupported error handler");
4972 break;
4973 default:
4974 PyErr_SetString(PyExc_ValueError, "unknow error code");
4975 break;
4976 }
4977 return res;
4978 }
4979
4980
4981 static PyObject *
decode_locale_ex(PyObject * self,PyObject * args)4982 decode_locale_ex(PyObject *self, PyObject *args)
4983 {
4984 char *str;
4985 int current_locale = 0;
4986 PyObject *res = NULL;
4987 const char *errors = NULL;
4988
4989 if (!PyArg_ParseTuple(args, "y|is", &str, ¤t_locale, &errors)) {
4990 return NULL;
4991 }
4992 _Py_error_handler error_handler = _Py_GetErrorHandler(errors);
4993
4994 wchar_t *wstr = NULL;
4995 size_t wlen = 0;
4996 const char *reason = NULL;
4997 int ret = _Py_DecodeLocaleEx(str,
4998 &wstr, &wlen, &reason,
4999 current_locale, error_handler);
5000
5001 switch(ret) {
5002 case 0:
5003 res = PyUnicode_FromWideChar(wstr, wlen);
5004 PyMem_RawFree(wstr);
5005 break;
5006 case -1:
5007 PyErr_NoMemory();
5008 break;
5009 case -2:
5010 PyErr_Format(PyExc_RuntimeError, "decode error: pos=%zu, reason=%s",
5011 wlen, reason);
5012 break;
5013 case -3:
5014 PyErr_SetString(PyExc_ValueError, "unsupported error handler");
5015 break;
5016 default:
5017 PyErr_SetString(PyExc_ValueError, "unknow error code");
5018 break;
5019 }
5020 return res;
5021 }
5022
5023
5024 #ifdef Py_REF_DEBUG
5025 static PyObject *
negative_refcount(PyObject * self,PyObject * Py_UNUSED (args))5026 negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
5027 {
5028 PyObject *obj = PyUnicode_FromString("negative_refcount");
5029 if (obj == NULL) {
5030 return NULL;
5031 }
5032 assert(Py_REFCNT(obj) == 1);
5033
5034 Py_REFCNT(obj) = 0;
5035 /* Py_DECREF() must call _Py_NegativeRefcount() and abort Python */
5036 Py_DECREF(obj);
5037
5038 Py_RETURN_NONE;
5039 }
5040 #endif
5041
5042
5043 static PyObject*
test_write_unraisable_exc(PyObject * self,PyObject * args)5044 test_write_unraisable_exc(PyObject *self, PyObject *args)
5045 {
5046 PyObject *exc, *err_msg, *obj;
5047 if (!PyArg_ParseTuple(args, "OOO", &exc, &err_msg, &obj)) {
5048 return NULL;
5049 }
5050
5051 const char *err_msg_utf8;
5052 if (err_msg != Py_None) {
5053 err_msg_utf8 = PyUnicode_AsUTF8(err_msg);
5054 if (err_msg_utf8 == NULL) {
5055 return NULL;
5056 }
5057 }
5058 else {
5059 err_msg_utf8 = NULL;
5060 }
5061
5062 PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
5063 _PyErr_WriteUnraisableMsg(err_msg_utf8, obj);
5064 Py_RETURN_NONE;
5065 }
5066
5067
5068 static PyObject*
pynumber_tobase(PyObject * module,PyObject * args)5069 pynumber_tobase(PyObject *module, PyObject *args)
5070 {
5071 PyObject *obj;
5072 int base;
5073 if (!PyArg_ParseTuple(args, "Oi:pynumber_tobase",
5074 &obj, &base)) {
5075 return NULL;
5076 }
5077 return PyNumber_ToBase(obj, base);
5078 }
5079
5080
5081 static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
5082
5083 static PyMethodDef TestMethods[] = {
5084 {"raise_exception", raise_exception, METH_VARARGS},
5085 {"raise_memoryerror", raise_memoryerror, METH_NOARGS},
5086 {"set_errno", set_errno, METH_VARARGS},
5087 {"test_config", test_config, METH_NOARGS},
5088 {"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS},
5089 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
5090 {"datetime_check_date", datetime_check_date, METH_VARARGS},
5091 {"datetime_check_time", datetime_check_time, METH_VARARGS},
5092 {"datetime_check_datetime", datetime_check_datetime, METH_VARARGS},
5093 {"datetime_check_delta", datetime_check_delta, METH_VARARGS},
5094 {"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS},
5095 {"make_timezones_capi", make_timezones_capi, METH_NOARGS},
5096 {"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS},
5097 {"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},
5098 {"get_date_fromdate", get_date_fromdate, METH_VARARGS},
5099 {"get_datetime_fromdateandtime", get_datetime_fromdateandtime, METH_VARARGS},
5100 {"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS},
5101 {"get_time_fromtime", get_time_fromtime, METH_VARARGS},
5102 {"get_time_fromtimeandfold", get_time_fromtimeandfold, METH_VARARGS},
5103 {"get_delta_fromdsu", get_delta_fromdsu, METH_VARARGS},
5104 {"get_date_fromtimestamp", get_date_fromtimestamp, METH_VARARGS},
5105 {"get_datetime_fromtimestamp", get_datetime_fromtimestamp, METH_VARARGS},
5106 {"test_list_api", test_list_api, METH_NOARGS},
5107 {"test_dict_iteration", test_dict_iteration, METH_NOARGS},
5108 {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
5109 {"dict_hassplittable", dict_hassplittable, METH_O},
5110 {"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS},
5111 {"test_long_api", test_long_api, METH_NOARGS},
5112 {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS},
5113 {"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS},
5114 {"test_xdecref_doesnt_leak",test_xdecref_doesnt_leak, METH_NOARGS},
5115 {"test_decref_doesnt_leak", test_decref_doesnt_leak, METH_NOARGS},
5116 {"test_structseq_newtype_doesnt_leak",
5117 test_structseq_newtype_doesnt_leak, METH_NOARGS},
5118 {"test_incref_decref_API", test_incref_decref_API, METH_NOARGS},
5119 {"test_long_and_overflow", test_long_and_overflow, METH_NOARGS},
5120 {"test_long_as_double", test_long_as_double, METH_NOARGS},
5121 {"test_long_as_size_t", test_long_as_size_t, METH_NOARGS},
5122 {"test_long_as_unsigned_long_long_mask",
5123 test_long_as_unsigned_long_long_mask, METH_NOARGS},
5124 {"test_long_numbits", test_long_numbits, METH_NOARGS},
5125 {"test_k_code", test_k_code, METH_NOARGS},
5126 {"test_empty_argparse", test_empty_argparse, METH_NOARGS},
5127 {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
5128 {"test_null_strings", test_null_strings, METH_NOARGS},
5129 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
5130 {"test_with_docstring", test_with_docstring, METH_NOARGS,
5131 PyDoc_STR("This is a pretty normal docstring.")},
5132 {"test_string_to_double", test_string_to_double, METH_NOARGS},
5133 {"test_unicode_compare_with_ascii", test_unicode_compare_with_ascii,
5134 METH_NOARGS},
5135 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
5136 {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
5137 #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
5138 {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
5139 #endif
5140 {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
5141 {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
5142 {"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
5143 {"get_args", get_args, METH_VARARGS},
5144 {"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS},
5145 {"getargs_tuple", getargs_tuple, METH_VARARGS},
5146 {"getargs_keywords", (PyCFunction)(void(*)(void))getargs_keywords,
5147 METH_VARARGS|METH_KEYWORDS},
5148 {"getargs_keyword_only", (PyCFunction)(void(*)(void))getargs_keyword_only,
5149 METH_VARARGS|METH_KEYWORDS},
5150 {"getargs_positional_only_and_keywords",
5151 (PyCFunction)(void(*)(void))getargs_positional_only_and_keywords,
5152 METH_VARARGS|METH_KEYWORDS},
5153 {"getargs_b", getargs_b, METH_VARARGS},
5154 {"getargs_B", getargs_B, METH_VARARGS},
5155 {"getargs_h", getargs_h, METH_VARARGS},
5156 {"getargs_H", getargs_H, METH_VARARGS},
5157 {"getargs_I", getargs_I, METH_VARARGS},
5158 {"getargs_k", getargs_k, METH_VARARGS},
5159 {"getargs_i", getargs_i, METH_VARARGS},
5160 {"getargs_l", getargs_l, METH_VARARGS},
5161 {"getargs_n", getargs_n, METH_VARARGS},
5162 {"getargs_p", getargs_p, METH_VARARGS},
5163 {"getargs_L", getargs_L, METH_VARARGS},
5164 {"getargs_K", getargs_K, METH_VARARGS},
5165 {"test_longlong_api", test_longlong_api, METH_NOARGS},
5166 {"test_long_long_and_overflow",test_long_long_and_overflow, METH_NOARGS},
5167 {"test_L_code", test_L_code, METH_NOARGS},
5168 {"getargs_f", getargs_f, METH_VARARGS},
5169 {"getargs_d", getargs_d, METH_VARARGS},
5170 {"getargs_D", getargs_D, METH_VARARGS},
5171 {"getargs_S", getargs_S, METH_VARARGS},
5172 {"getargs_Y", getargs_Y, METH_VARARGS},
5173 {"getargs_U", getargs_U, METH_VARARGS},
5174 {"getargs_c", getargs_c, METH_VARARGS},
5175 {"getargs_C", getargs_C, METH_VARARGS},
5176 {"getargs_s", getargs_s, METH_VARARGS},
5177 {"getargs_s_star", getargs_s_star, METH_VARARGS},
5178 {"getargs_s_hash", getargs_s_hash, METH_VARARGS},
5179 {"getargs_z", getargs_z, METH_VARARGS},
5180 {"getargs_z_star", getargs_z_star, METH_VARARGS},
5181 {"getargs_z_hash", getargs_z_hash, METH_VARARGS},
5182 {"getargs_y", getargs_y, METH_VARARGS},
5183 {"getargs_y_star", getargs_y_star, METH_VARARGS},
5184 {"getargs_y_hash", getargs_y_hash, METH_VARARGS},
5185 {"getargs_u", getargs_u, METH_VARARGS},
5186 {"getargs_u_hash", getargs_u_hash, METH_VARARGS},
5187 {"getargs_Z", getargs_Z, METH_VARARGS},
5188 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS},
5189 {"getargs_w_star", getargs_w_star, METH_VARARGS},
5190 {"getargs_es", getargs_es, METH_VARARGS},
5191 {"getargs_et", getargs_et, METH_VARARGS},
5192 {"getargs_es_hash", getargs_es_hash, METH_VARARGS},
5193 {"getargs_et_hash", getargs_et_hash, METH_VARARGS},
5194 {"codec_incrementalencoder",
5195 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
5196 {"codec_incrementaldecoder",
5197 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
5198 {"test_s_code", test_s_code, METH_NOARGS},
5199 {"test_u_code", test_u_code, METH_NOARGS},
5200 {"test_Z_code", test_Z_code, METH_NOARGS},
5201 {"test_widechar", test_widechar, METH_NOARGS},
5202 {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
5203 {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
5204 {"unicode_asucs4", unicode_asucs4, METH_VARARGS},
5205 {"unicode_findchar", unicode_findchar, METH_VARARGS},
5206 {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
5207 {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
5208 {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
5209 {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS},
5210 {"_test_thread_state", test_thread_state, METH_VARARGS},
5211 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
5212 #ifdef HAVE_GETTIMEOFDAY
5213 {"profile_int", profile_int, METH_NOARGS},
5214 #endif
5215 {"traceback_print", traceback_print, METH_VARARGS},
5216 {"exception_print", exception_print, METH_VARARGS},
5217 {"set_exc_info", test_set_exc_info, METH_VARARGS},
5218 {"argparsing", argparsing, METH_VARARGS},
5219 {"code_newempty", code_newempty, METH_VARARGS},
5220 {"make_exception_with_doc", (PyCFunction)(void(*)(void))make_exception_with_doc,
5221 METH_VARARGS | METH_KEYWORDS},
5222 {"make_memoryview_from_NULL_pointer", make_memoryview_from_NULL_pointer,
5223 METH_NOARGS},
5224 {"crash_no_current_thread", crash_no_current_thread, METH_NOARGS},
5225 {"run_in_subinterp", run_in_subinterp, METH_VARARGS},
5226 {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS},
5227 {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS},
5228 {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS},
5229 {"with_tp_del", with_tp_del, METH_VARARGS},
5230 {"create_cfunction", create_cfunction, METH_NOARGS},
5231 {"test_pymem_alloc0", test_pymem_alloc0, METH_NOARGS},
5232 {"test_pymem_setrawallocators",test_pymem_setrawallocators, METH_NOARGS},
5233 {"test_pymem_setallocators",test_pymem_setallocators, METH_NOARGS},
5234 {"test_pyobject_setallocators",test_pyobject_setallocators, METH_NOARGS},
5235 {"set_nomemory", (PyCFunction)set_nomemory, METH_VARARGS,
5236 PyDoc_STR("set_nomemory(start:int, stop:int = 0)")},
5237 {"remove_mem_hooks", remove_mem_hooks, METH_NOARGS,
5238 PyDoc_STR("Remove memory hooks.")},
5239 {"no_docstring",
5240 (PyCFunction)test_with_docstring, METH_NOARGS},
5241 {"docstring_empty",
5242 (PyCFunction)test_with_docstring, METH_NOARGS,
5243 docstring_empty},
5244 {"docstring_no_signature",
5245 (PyCFunction)test_with_docstring, METH_NOARGS,
5246 docstring_no_signature},
5247 {"docstring_with_invalid_signature",
5248 (PyCFunction)test_with_docstring, METH_NOARGS,
5249 docstring_with_invalid_signature},
5250 {"docstring_with_invalid_signature2",
5251 (PyCFunction)test_with_docstring, METH_NOARGS,
5252 docstring_with_invalid_signature2},
5253 {"docstring_with_signature",
5254 (PyCFunction)test_with_docstring, METH_NOARGS,
5255 docstring_with_signature},
5256 {"docstring_with_signature_but_no_doc",
5257 (PyCFunction)test_with_docstring, METH_NOARGS,
5258 docstring_with_signature_but_no_doc},
5259 {"docstring_with_signature_and_extra_newlines",
5260 (PyCFunction)test_with_docstring, METH_NOARGS,
5261 docstring_with_signature_and_extra_newlines},
5262 {"docstring_with_signature_with_defaults",
5263 (PyCFunction)test_with_docstring, METH_NOARGS,
5264 docstring_with_signature_with_defaults},
5265 {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O,
5266 PyDoc_STR("set_error_class(error_class) -> None")},
5267 {"pymarshal_write_long_to_file",
5268 pymarshal_write_long_to_file, METH_VARARGS},
5269 {"pymarshal_write_object_to_file",
5270 pymarshal_write_object_to_file, METH_VARARGS},
5271 {"pymarshal_read_short_from_file",
5272 pymarshal_read_short_from_file, METH_VARARGS},
5273 {"pymarshal_read_long_from_file",
5274 pymarshal_read_long_from_file, METH_VARARGS},
5275 {"pymarshal_read_last_object_from_file",
5276 pymarshal_read_last_object_from_file, METH_VARARGS},
5277 {"pymarshal_read_object_from_file",
5278 pymarshal_read_object_from_file, METH_VARARGS},
5279 {"return_null_without_error",
5280 return_null_without_error, METH_NOARGS},
5281 {"return_result_with_error",
5282 return_result_with_error, METH_NOARGS},
5283 {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
5284 {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
5285 {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
5286 {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
5287 #ifdef HAVE_CLOCK_GETTIME
5288 {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS},
5289 #endif
5290 {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
5291 {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
5292 {"get_recursion_depth", get_recursion_depth, METH_NOARGS},
5293 {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
5294 {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
5295 {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},
5296 {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS},
5297 {"check_pyobject_null_is_freed", check_pyobject_null_is_freed, METH_NOARGS},
5298 {"check_pyobject_uninitialized_is_freed", check_pyobject_uninitialized_is_freed, METH_NOARGS},
5299 {"check_pyobject_forbidden_bytes_is_freed", check_pyobject_forbidden_bytes_is_freed, METH_NOARGS},
5300 {"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS},
5301 {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
5302 {"tracemalloc_track", tracemalloc_track, METH_VARARGS},
5303 {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
5304 {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
5305 {"dict_get_version", dict_get_version, METH_VARARGS},
5306 {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
5307 {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS},
5308 {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},
5309 {"pyobject_vectorcall", test_pyobject_vectorcall, METH_VARARGS},
5310 {"pyvectorcall_call", test_pyvectorcall_call, METH_VARARGS},
5311 {"stack_pointer", stack_pointer, METH_NOARGS},
5312 #ifdef W_STOPCODE
5313 {"W_STOPCODE", py_w_stopcode, METH_VARARGS},
5314 #endif
5315 {"get_mapping_keys", get_mapping_keys, METH_O},
5316 {"get_mapping_values", get_mapping_values, METH_O},
5317 {"get_mapping_items", get_mapping_items, METH_O},
5318 {"test_pythread_tss_key_state", test_pythread_tss_key_state, METH_VARARGS},
5319 {"hamt", new_hamt, METH_NOARGS},
5320 {"bad_get", (PyCFunction)(void(*)(void))bad_get, METH_FASTCALL},
5321 {"EncodeLocaleEx", encode_locale_ex, METH_VARARGS},
5322 {"DecodeLocaleEx", decode_locale_ex, METH_VARARGS},
5323 #ifdef Py_REF_DEBUG
5324 {"negative_refcount", negative_refcount, METH_NOARGS},
5325 #endif
5326 {"write_unraisable_exc", test_write_unraisable_exc, METH_VARARGS},
5327 {"pynumber_tobase", pynumber_tobase, METH_VARARGS},
5328 {NULL, NULL} /* sentinel */
5329 };
5330
5331 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
5332
5333 typedef struct {
5334 char bool_member;
5335 char byte_member;
5336 unsigned char ubyte_member;
5337 short short_member;
5338 unsigned short ushort_member;
5339 int int_member;
5340 unsigned int uint_member;
5341 long long_member;
5342 unsigned long ulong_member;
5343 Py_ssize_t pyssizet_member;
5344 float float_member;
5345 double double_member;
5346 char inplace_member[6];
5347 long long longlong_member;
5348 unsigned long long ulonglong_member;
5349 } all_structmembers;
5350
5351 typedef struct {
5352 PyObject_HEAD
5353 all_structmembers structmembers;
5354 } test_structmembers;
5355
5356 static struct PyMemberDef test_members[] = {
5357 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
5358 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
5359 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
5360 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
5361 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
5362 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
5363 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
5364 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
5365 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
5366 {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
5367 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
5368 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
5369 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
5370 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
5371 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
5372 {NULL}
5373 };
5374
5375
5376 static PyObject *
test_structmembers_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)5377 test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5378 {
5379 static char *keywords[] = {
5380 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
5381 "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
5382 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
5383 "T_LONGLONG", "T_ULONGLONG",
5384 NULL};
5385 static const char fmt[] = "|bbBhHiIlknfds#LK";
5386 test_structmembers *ob;
5387 const char *s = NULL;
5388 Py_ssize_t string_len = 0;
5389 ob = PyObject_New(test_structmembers, type);
5390 if (ob == NULL)
5391 return NULL;
5392 memset(&ob->structmembers, 0, sizeof(all_structmembers));
5393 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
5394 &ob->structmembers.bool_member,
5395 &ob->structmembers.byte_member,
5396 &ob->structmembers.ubyte_member,
5397 &ob->structmembers.short_member,
5398 &ob->structmembers.ushort_member,
5399 &ob->structmembers.int_member,
5400 &ob->structmembers.uint_member,
5401 &ob->structmembers.long_member,
5402 &ob->structmembers.ulong_member,
5403 &ob->structmembers.pyssizet_member,
5404 &ob->structmembers.float_member,
5405 &ob->structmembers.double_member,
5406 &s, &string_len
5407 , &ob->structmembers.longlong_member,
5408 &ob->structmembers.ulonglong_member
5409 )) {
5410 Py_DECREF(ob);
5411 return NULL;
5412 }
5413 if (s != NULL) {
5414 if (string_len > 5) {
5415 Py_DECREF(ob);
5416 PyErr_SetString(PyExc_ValueError, "string too long");
5417 return NULL;
5418 }
5419 strcpy(ob->structmembers.inplace_member, s);
5420 }
5421 else {
5422 strcpy(ob->structmembers.inplace_member, "");
5423 }
5424 return (PyObject *)ob;
5425 }
5426
5427 static void
test_structmembers_free(PyObject * ob)5428 test_structmembers_free(PyObject *ob)
5429 {
5430 PyObject_FREE(ob);
5431 }
5432
5433 static PyTypeObject test_structmembersType = {
5434 PyVarObject_HEAD_INIT(NULL, 0)
5435 "test_structmembersType",
5436 sizeof(test_structmembers), /* tp_basicsize */
5437 0, /* tp_itemsize */
5438 test_structmembers_free, /* destructor tp_dealloc */
5439 0, /* tp_vectorcall_offset */
5440 0, /* tp_getattr */
5441 0, /* tp_setattr */
5442 0, /* tp_as_async */
5443 0, /* tp_repr */
5444 0, /* tp_as_number */
5445 0, /* tp_as_sequence */
5446 0, /* tp_as_mapping */
5447 0, /* tp_hash */
5448 0, /* tp_call */
5449 0, /* tp_str */
5450 PyObject_GenericGetAttr, /* tp_getattro */
5451 PyObject_GenericSetAttr, /* tp_setattro */
5452 0, /* tp_as_buffer */
5453 0, /* tp_flags */
5454 "Type containing all structmember types",
5455 0, /* traverseproc tp_traverse */
5456 0, /* tp_clear */
5457 0, /* tp_richcompare */
5458 0, /* tp_weaklistoffset */
5459 0, /* tp_iter */
5460 0, /* tp_iternext */
5461 0, /* tp_methods */
5462 test_members, /* tp_members */
5463 0,
5464 0,
5465 0,
5466 0,
5467 0,
5468 0,
5469 0,
5470 0,
5471 test_structmembers_new, /* tp_new */
5472 };
5473
5474
5475 typedef struct {
5476 PyObject_HEAD
5477 } matmulObject;
5478
5479 static PyObject *
matmulType_matmul(PyObject * self,PyObject * other)5480 matmulType_matmul(PyObject *self, PyObject *other)
5481 {
5482 return Py_BuildValue("(sOO)", "matmul", self, other);
5483 }
5484
5485 static PyObject *
matmulType_imatmul(PyObject * self,PyObject * other)5486 matmulType_imatmul(PyObject *self, PyObject *other)
5487 {
5488 return Py_BuildValue("(sOO)", "imatmul", self, other);
5489 }
5490
5491 static void
matmulType_dealloc(PyObject * self)5492 matmulType_dealloc(PyObject *self)
5493 {
5494 Py_TYPE(self)->tp_free(self);
5495 }
5496
5497 static PyNumberMethods matmulType_as_number = {
5498 0, /* nb_add */
5499 0, /* nb_subtract */
5500 0, /* nb_multiply */
5501 0, /* nb_remainde r*/
5502 0, /* nb_divmod */
5503 0, /* nb_power */
5504 0, /* nb_negative */
5505 0, /* tp_positive */
5506 0, /* tp_absolute */
5507 0, /* tp_bool */
5508 0, /* nb_invert */
5509 0, /* nb_lshift */
5510 0, /* nb_rshift */
5511 0, /* nb_and */
5512 0, /* nb_xor */
5513 0, /* nb_or */
5514 0, /* nb_int */
5515 0, /* nb_reserved */
5516 0, /* nb_float */
5517 0, /* nb_inplace_add */
5518 0, /* nb_inplace_subtract */
5519 0, /* nb_inplace_multiply */
5520 0, /* nb_inplace_remainder */
5521 0, /* nb_inplace_power */
5522 0, /* nb_inplace_lshift */
5523 0, /* nb_inplace_rshift */
5524 0, /* nb_inplace_and */
5525 0, /* nb_inplace_xor */
5526 0, /* nb_inplace_or */
5527 0, /* nb_floor_divide */
5528 0, /* nb_true_divide */
5529 0, /* nb_inplace_floor_divide */
5530 0, /* nb_inplace_true_divide */
5531 0, /* nb_index */
5532 matmulType_matmul, /* nb_matrix_multiply */
5533 matmulType_imatmul /* nb_matrix_inplace_multiply */
5534 };
5535
5536 static PyTypeObject matmulType = {
5537 PyVarObject_HEAD_INIT(NULL, 0)
5538 "matmulType",
5539 sizeof(matmulObject), /* tp_basicsize */
5540 0, /* tp_itemsize */
5541 matmulType_dealloc, /* destructor tp_dealloc */
5542 0, /* tp_vectorcall_offset */
5543 0, /* tp_getattr */
5544 0, /* tp_setattr */
5545 0, /* tp_as_async */
5546 0, /* tp_repr */
5547 &matmulType_as_number, /* tp_as_number */
5548 0, /* tp_as_sequence */
5549 0, /* tp_as_mapping */
5550 0, /* tp_hash */
5551 0, /* tp_call */
5552 0, /* tp_str */
5553 PyObject_GenericGetAttr, /* tp_getattro */
5554 PyObject_GenericSetAttr, /* tp_setattro */
5555 0, /* tp_as_buffer */
5556 0, /* tp_flags */
5557 "C level type with matrix operations defined",
5558 0, /* traverseproc tp_traverse */
5559 0, /* tp_clear */
5560 0, /* tp_richcompare */
5561 0, /* tp_weaklistoffset */
5562 0, /* tp_iter */
5563 0, /* tp_iternext */
5564 0, /* tp_methods */
5565 0, /* tp_members */
5566 0,
5567 0,
5568 0,
5569 0,
5570 0,
5571 0,
5572 0,
5573 0,
5574 PyType_GenericNew, /* tp_new */
5575 PyObject_Del, /* tp_free */
5576 };
5577
5578 typedef struct {
5579 PyObject_HEAD
5580 } ipowObject;
5581
5582 static PyObject *
ipowType_ipow(PyObject * self,PyObject * other,PyObject * mod)5583 ipowType_ipow(PyObject *self, PyObject *other, PyObject *mod)
5584 {
5585 return Py_BuildValue("OO", other, mod);
5586 }
5587
5588 static PyNumberMethods ipowType_as_number = {
5589 .nb_inplace_power = ipowType_ipow
5590 };
5591
5592 static PyTypeObject ipowType = {
5593 PyVarObject_HEAD_INIT(NULL, 0)
5594 .tp_name = "ipowType",
5595 .tp_basicsize = sizeof(ipowObject),
5596 .tp_as_number = &ipowType_as_number,
5597 .tp_new = PyType_GenericNew
5598 };
5599
5600 typedef struct {
5601 PyObject_HEAD
5602 PyObject *ao_iterator;
5603 } awaitObject;
5604
5605
5606 static PyObject *
awaitObject_new(PyTypeObject * type,PyObject * args,PyObject * kwds)5607 awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5608 {
5609 PyObject *v;
5610 awaitObject *ao;
5611
5612 if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v))
5613 return NULL;
5614
5615 ao = (awaitObject *)type->tp_alloc(type, 0);
5616 if (ao == NULL) {
5617 return NULL;
5618 }
5619
5620 Py_INCREF(v);
5621 ao->ao_iterator = v;
5622
5623 return (PyObject *)ao;
5624 }
5625
5626
5627 static void
awaitObject_dealloc(awaitObject * ao)5628 awaitObject_dealloc(awaitObject *ao)
5629 {
5630 Py_CLEAR(ao->ao_iterator);
5631 Py_TYPE(ao)->tp_free(ao);
5632 }
5633
5634
5635 static PyObject *
awaitObject_await(awaitObject * ao)5636 awaitObject_await(awaitObject *ao)
5637 {
5638 Py_INCREF(ao->ao_iterator);
5639 return ao->ao_iterator;
5640 }
5641
5642 static PyAsyncMethods awaitType_as_async = {
5643 (unaryfunc)awaitObject_await, /* am_await */
5644 0, /* am_aiter */
5645 0 /* am_anext */
5646 };
5647
5648
5649 static PyTypeObject awaitType = {
5650 PyVarObject_HEAD_INIT(NULL, 0)
5651 "awaitType",
5652 sizeof(awaitObject), /* tp_basicsize */
5653 0, /* tp_itemsize */
5654 (destructor)awaitObject_dealloc, /* destructor tp_dealloc */
5655 0, /* tp_vectorcall_offset */
5656 0, /* tp_getattr */
5657 0, /* tp_setattr */
5658 &awaitType_as_async, /* tp_as_async */
5659 0, /* tp_repr */
5660 0, /* tp_as_number */
5661 0, /* tp_as_sequence */
5662 0, /* tp_as_mapping */
5663 0, /* tp_hash */
5664 0, /* tp_call */
5665 0, /* tp_str */
5666 PyObject_GenericGetAttr, /* tp_getattro */
5667 PyObject_GenericSetAttr, /* tp_setattro */
5668 0, /* tp_as_buffer */
5669 0, /* tp_flags */
5670 "C level type with tp_as_async",
5671 0, /* traverseproc tp_traverse */
5672 0, /* tp_clear */
5673 0, /* tp_richcompare */
5674 0, /* tp_weaklistoffset */
5675 0, /* tp_iter */
5676 0, /* tp_iternext */
5677 0, /* tp_methods */
5678 0, /* tp_members */
5679 0,
5680 0,
5681 0,
5682 0,
5683 0,
5684 0,
5685 0,
5686 0,
5687 awaitObject_new, /* tp_new */
5688 PyObject_Del, /* tp_free */
5689 };
5690
5691
5692 static int recurse_infinitely_error_init(PyObject *, PyObject *, PyObject *);
5693
5694 static PyTypeObject PyRecursingInfinitelyError_Type = {
5695 PyVarObject_HEAD_INIT(NULL, 0)
5696 "RecursingInfinitelyError", /* tp_name */
5697 sizeof(PyBaseExceptionObject), /* tp_basicsize */
5698 0, /* tp_itemsize */
5699 0, /* tp_dealloc */
5700 0, /* tp_vectorcall_offset */
5701 0, /* tp_getattr */
5702 0, /* tp_setattr */
5703 0, /* tp_as_async */
5704 0, /* tp_repr */
5705 0, /* tp_as_number */
5706 0, /* tp_as_sequence */
5707 0, /* tp_as_mapping */
5708 0, /* tp_hash */
5709 0, /* tp_call */
5710 0, /* tp_str */
5711 0, /* tp_getattro */
5712 0, /* tp_setattro */
5713 0, /* tp_as_buffer */
5714 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5715 "Instantiating this exception starts infinite recursion.", /* tp_doc */
5716 0, /* tp_traverse */
5717 0, /* tp_clear */
5718 0, /* tp_richcompare */
5719 0, /* tp_weaklistoffset */
5720 0, /* tp_iter */
5721 0, /* tp_iternext */
5722 0, /* tp_methods */
5723 0, /* tp_members */
5724 0, /* tp_getset */
5725 0, /* tp_base */
5726 0, /* tp_dict */
5727 0, /* tp_descr_get */
5728 0, /* tp_descr_set */
5729 0, /* tp_dictoffset */
5730 (initproc)recurse_infinitely_error_init, /* tp_init */
5731 0, /* tp_alloc */
5732 0, /* tp_new */
5733 };
5734
5735 static int
recurse_infinitely_error_init(PyObject * self,PyObject * args,PyObject * kwds)5736 recurse_infinitely_error_init(PyObject *self, PyObject *args, PyObject *kwds)
5737 {
5738 PyObject *type = (PyObject *)&PyRecursingInfinitelyError_Type;
5739
5740 /* Instantiating this exception starts infinite recursion. */
5741 Py_INCREF(type);
5742 PyErr_SetObject(type, NULL);
5743 return -1;
5744 }
5745
5746
5747 /* Test bpo-35983: create a subclass of "list" which checks that instances
5748 * are not deallocated twice */
5749
5750 typedef struct {
5751 PyListObject list;
5752 int deallocated;
5753 } MyListObject;
5754
5755 static PyObject *
MyList_new(PyTypeObject * type,PyObject * args,PyObject * kwds)5756 MyList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5757 {
5758 PyObject* op = PyList_Type.tp_new(type, args, kwds);
5759 ((MyListObject*)op)->deallocated = 0;
5760 return op;
5761 }
5762
5763 void
MyList_dealloc(MyListObject * op)5764 MyList_dealloc(MyListObject* op)
5765 {
5766 if (op->deallocated) {
5767 /* We cannot raise exceptions here but we still want the testsuite
5768 * to fail when we hit this */
5769 Py_FatalError("MyList instance deallocated twice");
5770 }
5771 op->deallocated = 1;
5772 PyList_Type.tp_dealloc((PyObject *)op);
5773 }
5774
5775 static PyTypeObject MyList_Type = {
5776 PyVarObject_HEAD_INIT(NULL, 0)
5777 "MyList",
5778 sizeof(MyListObject),
5779 0,
5780 (destructor)MyList_dealloc, /* tp_dealloc */
5781 0, /* tp_vectorcall_offset */
5782 0, /* tp_getattr */
5783 0, /* tp_setattr */
5784 0, /* tp_as_async */
5785 0, /* tp_repr */
5786 0, /* tp_as_number */
5787 0, /* tp_as_sequence */
5788 0, /* tp_as_mapping */
5789 0, /* tp_hash */
5790 0, /* tp_call */
5791 0, /* tp_str */
5792 0, /* tp_getattro */
5793 0, /* tp_setattro */
5794 0, /* tp_as_buffer */
5795 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5796 0, /* tp_doc */
5797 0, /* tp_traverse */
5798 0, /* tp_clear */
5799 0, /* tp_richcompare */
5800 0, /* tp_weaklistoffset */
5801 0, /* tp_iter */
5802 0, /* tp_iternext */
5803 0, /* tp_methods */
5804 0, /* tp_members */
5805 0, /* tp_getset */
5806 0, /* &PyList_Type */ /* tp_base */
5807 0, /* tp_dict */
5808 0, /* tp_descr_get */
5809 0, /* tp_descr_set */
5810 0, /* tp_dictoffset */
5811 0, /* tp_init */
5812 0, /* tp_alloc */
5813 MyList_new, /* tp_new */
5814 };
5815
5816
5817 /* Test PEP 560 */
5818
5819 typedef struct {
5820 PyObject_HEAD
5821 PyObject *item;
5822 } PyGenericAliasObject;
5823
5824 static void
generic_alias_dealloc(PyGenericAliasObject * self)5825 generic_alias_dealloc(PyGenericAliasObject *self)
5826 {
5827 Py_CLEAR(self->item);
5828 Py_TYPE(self)->tp_free((PyObject *)self);
5829 }
5830
5831 static PyObject *
generic_alias_mro_entries(PyGenericAliasObject * self,PyObject * bases)5832 generic_alias_mro_entries(PyGenericAliasObject *self, PyObject *bases)
5833 {
5834 return PyTuple_Pack(1, self->item);
5835 }
5836
5837 static PyMethodDef generic_alias_methods[] = {
5838 {"__mro_entries__", (PyCFunction)(void(*)(void))generic_alias_mro_entries, METH_O, NULL},
5839 {NULL} /* sentinel */
5840 };
5841
5842 static PyTypeObject GenericAlias_Type = {
5843 PyVarObject_HEAD_INIT(NULL, 0)
5844 "GenericAlias",
5845 sizeof(PyGenericAliasObject),
5846 0,
5847 .tp_dealloc = (destructor)generic_alias_dealloc,
5848 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5849 .tp_methods = generic_alias_methods,
5850 };
5851
5852 static PyObject *
generic_alias_new(PyObject * item)5853 generic_alias_new(PyObject *item)
5854 {
5855 PyGenericAliasObject *o = PyObject_New(PyGenericAliasObject, &GenericAlias_Type);
5856 if (o == NULL) {
5857 return NULL;
5858 }
5859 Py_INCREF(item);
5860 o->item = item;
5861 return (PyObject*) o;
5862 }
5863
5864 typedef struct {
5865 PyObject_HEAD
5866 } PyGenericObject;
5867
5868 static PyObject *
generic_class_getitem(PyObject * type,PyObject * item)5869 generic_class_getitem(PyObject *type, PyObject *item)
5870 {
5871 return generic_alias_new(item);
5872 }
5873
5874 static PyMethodDef generic_methods[] = {
5875 {"__class_getitem__", generic_class_getitem, METH_O|METH_CLASS, NULL},
5876 {NULL} /* sentinel */
5877 };
5878
5879 static PyTypeObject Generic_Type = {
5880 PyVarObject_HEAD_INIT(NULL, 0)
5881 "Generic",
5882 sizeof(PyGenericObject),
5883 0,
5884 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5885 .tp_methods = generic_methods,
5886 };
5887
5888
5889 /* Test PEP 590 */
5890
5891 typedef struct {
5892 PyObject_HEAD
5893 vectorcallfunc vectorcall;
5894 } MethodDescriptorObject;
5895
5896 static PyObject *
MethodDescriptor_vectorcall(PyObject * callable,PyObject * const * args,size_t nargsf,PyObject * kwnames)5897 MethodDescriptor_vectorcall(PyObject *callable, PyObject *const *args,
5898 size_t nargsf, PyObject *kwnames)
5899 {
5900 /* True if using the vectorcall function in MethodDescriptorObject
5901 * but False for MethodDescriptor2Object */
5902 MethodDescriptorObject *md = (MethodDescriptorObject *)callable;
5903 return PyBool_FromLong(md->vectorcall != NULL);
5904 }
5905
5906 static PyObject *
MethodDescriptor_new(PyTypeObject * type,PyObject * args,PyObject * kw)5907 MethodDescriptor_new(PyTypeObject* type, PyObject* args, PyObject *kw)
5908 {
5909 MethodDescriptorObject *op = (MethodDescriptorObject *)type->tp_alloc(type, 0);
5910 op->vectorcall = MethodDescriptor_vectorcall;
5911 return (PyObject *)op;
5912 }
5913
5914 static PyObject *
func_descr_get(PyObject * func,PyObject * obj,PyObject * type)5915 func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
5916 {
5917 if (obj == Py_None || obj == NULL) {
5918 Py_INCREF(func);
5919 return func;
5920 }
5921 return PyMethod_New(func, obj);
5922 }
5923
5924 static PyObject *
nop_descr_get(PyObject * func,PyObject * obj,PyObject * type)5925 nop_descr_get(PyObject *func, PyObject *obj, PyObject *type)
5926 {
5927 Py_INCREF(func);
5928 return func;
5929 }
5930
5931 static PyObject *
call_return_args(PyObject * self,PyObject * args,PyObject * kwargs)5932 call_return_args(PyObject *self, PyObject *args, PyObject *kwargs)
5933 {
5934 Py_INCREF(args);
5935 return args;
5936 }
5937
5938 static PyTypeObject MethodDescriptorBase_Type = {
5939 PyVarObject_HEAD_INIT(NULL, 0)
5940 "MethodDescriptorBase",
5941 sizeof(MethodDescriptorObject),
5942 .tp_new = MethodDescriptor_new,
5943 .tp_call = PyVectorcall_Call,
5944 .tp_vectorcall_offset = offsetof(MethodDescriptorObject, vectorcall),
5945 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
5946 Py_TPFLAGS_METHOD_DESCRIPTOR | _Py_TPFLAGS_HAVE_VECTORCALL,
5947 .tp_descr_get = func_descr_get,
5948 };
5949
5950 static PyTypeObject MethodDescriptorDerived_Type = {
5951 PyVarObject_HEAD_INIT(NULL, 0)
5952 "MethodDescriptorDerived",
5953 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5954 };
5955
5956 static PyTypeObject MethodDescriptorNopGet_Type = {
5957 PyVarObject_HEAD_INIT(NULL, 0)
5958 "MethodDescriptorNopGet",
5959 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
5960 .tp_call = call_return_args,
5961 .tp_descr_get = nop_descr_get,
5962 };
5963
5964 typedef struct {
5965 MethodDescriptorObject base;
5966 vectorcallfunc vectorcall;
5967 } MethodDescriptor2Object;
5968
5969 static PyObject *
MethodDescriptor2_new(PyTypeObject * type,PyObject * args,PyObject * kw)5970 MethodDescriptor2_new(PyTypeObject* type, PyObject* args, PyObject *kw)
5971 {
5972 MethodDescriptor2Object *op = PyObject_New(MethodDescriptor2Object, type);
5973 op->base.vectorcall = NULL;
5974 op->vectorcall = MethodDescriptor_vectorcall;
5975 return (PyObject *)op;
5976 }
5977
5978 static PyTypeObject MethodDescriptor2_Type = {
5979 PyVarObject_HEAD_INIT(NULL, 0)
5980 "MethodDescriptor2",
5981 sizeof(MethodDescriptor2Object),
5982 .tp_new = MethodDescriptor2_new,
5983 .tp_call = PyVectorcall_Call,
5984 .tp_vectorcall_offset = offsetof(MethodDescriptor2Object, vectorcall),
5985 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | _Py_TPFLAGS_HAVE_VECTORCALL,
5986 };
5987
5988 PyDoc_STRVAR(heapgctype__doc__,
5989 "A heap type with GC, and with overridden dealloc.\n\n"
5990 "The 'value' attribute is set to 10 in __init__.");
5991
5992 typedef struct {
5993 PyObject_HEAD
5994 int value;
5995 } HeapCTypeObject;
5996
5997 static struct PyMemberDef heapctype_members[] = {
5998 {"value", T_INT, offsetof(HeapCTypeObject, value)},
5999 {NULL} /* Sentinel */
6000 };
6001
6002 static int
heapctype_init(PyObject * self,PyObject * args,PyObject * kwargs)6003 heapctype_init(PyObject *self, PyObject *args, PyObject *kwargs)
6004 {
6005 ((HeapCTypeObject *)self)->value = 10;
6006 return 0;
6007 }
6008
6009 static void
heapgcctype_dealloc(HeapCTypeObject * self)6010 heapgcctype_dealloc(HeapCTypeObject *self)
6011 {
6012 PyTypeObject *tp = Py_TYPE(self);
6013 PyObject_GC_UnTrack(self);
6014 PyObject_GC_Del(self);
6015 Py_DECREF(tp);
6016 }
6017
6018 static PyType_Slot HeapGcCType_slots[] = {
6019 {Py_tp_init, heapctype_init},
6020 {Py_tp_members, heapctype_members},
6021 {Py_tp_dealloc, heapgcctype_dealloc},
6022 {Py_tp_doc, (char*)heapgctype__doc__},
6023 {0, 0},
6024 };
6025
6026 static PyType_Spec HeapGcCType_spec = {
6027 "_testcapi.HeapGcCType",
6028 sizeof(HeapCTypeObject),
6029 0,
6030 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6031 HeapGcCType_slots
6032 };
6033
6034 PyDoc_STRVAR(heapctype__doc__,
6035 "A heap type without GC, but with overridden dealloc.\n\n"
6036 "The 'value' attribute is set to 10 in __init__.");
6037
6038 static void
heapctype_dealloc(HeapCTypeObject * self)6039 heapctype_dealloc(HeapCTypeObject *self)
6040 {
6041 PyTypeObject *tp = Py_TYPE(self);
6042 PyObject_Del(self);
6043 Py_DECREF(tp);
6044 }
6045
6046 static PyType_Slot HeapCType_slots[] = {
6047 {Py_tp_init, heapctype_init},
6048 {Py_tp_members, heapctype_members},
6049 {Py_tp_dealloc, heapctype_dealloc},
6050 {Py_tp_doc, (char*)heapctype__doc__},
6051 {0, 0},
6052 };
6053
6054 static PyType_Spec HeapCType_spec = {
6055 "_testcapi.HeapCType",
6056 sizeof(HeapCTypeObject),
6057 0,
6058 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6059 HeapCType_slots
6060 };
6061
6062 PyDoc_STRVAR(heapctypesubclass__doc__,
6063 "Subclass of HeapCType, without GC.\n\n"
6064 "__init__ sets the 'value' attribute to 10 and 'value2' to 20.");
6065
6066 typedef struct {
6067 HeapCTypeObject base;
6068 int value2;
6069 } HeapCTypeSubclassObject;
6070
6071 static int
heapctypesubclass_init(PyObject * self,PyObject * args,PyObject * kwargs)6072 heapctypesubclass_init(PyObject *self, PyObject *args, PyObject *kwargs)
6073 {
6074 /* Call __init__ of the superclass */
6075 if (heapctype_init(self, args, kwargs) < 0) {
6076 return -1;
6077 }
6078 /* Initialize additional element */
6079 ((HeapCTypeSubclassObject *)self)->value2 = 20;
6080 return 0;
6081 }
6082
6083 static struct PyMemberDef heapctypesubclass_members[] = {
6084 {"value2", T_INT, offsetof(HeapCTypeSubclassObject, value2)},
6085 {NULL} /* Sentinel */
6086 };
6087
6088 static PyType_Slot HeapCTypeSubclass_slots[] = {
6089 {Py_tp_init, heapctypesubclass_init},
6090 {Py_tp_members, heapctypesubclass_members},
6091 {Py_tp_doc, (char*)heapctypesubclass__doc__},
6092 {0, 0},
6093 };
6094
6095 static PyType_Spec HeapCTypeSubclass_spec = {
6096 "_testcapi.HeapCTypeSubclass",
6097 sizeof(HeapCTypeSubclassObject),
6098 0,
6099 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6100 HeapCTypeSubclass_slots
6101 };
6102
6103 PyDoc_STRVAR(heapctypesubclasswithfinalizer__doc__,
6104 "Subclass of HeapCType with a finalizer that reassigns __class__.\n\n"
6105 "__class__ is set to plain HeapCTypeSubclass during finalization.\n"
6106 "__init__ sets the 'value' attribute to 10 and 'value2' to 20.");
6107
6108 static int
heapctypesubclasswithfinalizer_init(PyObject * self,PyObject * args,PyObject * kwargs)6109 heapctypesubclasswithfinalizer_init(PyObject *self, PyObject *args, PyObject *kwargs)
6110 {
6111 PyTypeObject *base = (PyTypeObject *)PyType_GetSlot(Py_TYPE(self), Py_tp_base);
6112 initproc base_init = PyType_GetSlot(base, Py_tp_init);
6113 base_init(self, args, kwargs);
6114 return 0;
6115 }
6116
6117 static void
heapctypesubclasswithfinalizer_finalize(PyObject * self)6118 heapctypesubclasswithfinalizer_finalize(PyObject *self)
6119 {
6120 PyObject *error_type, *error_value, *error_traceback, *m;
6121 PyObject *oldtype = NULL, *newtype = NULL, *refcnt = NULL;
6122
6123 /* Save the current exception, if any. */
6124 PyErr_Fetch(&error_type, &error_value, &error_traceback);
6125
6126 m = PyState_FindModule(&_testcapimodule);
6127 if (m == NULL) {
6128 goto cleanup_finalize;
6129 }
6130 oldtype = PyObject_GetAttrString(m, "HeapCTypeSubclassWithFinalizer");
6131 newtype = PyObject_GetAttrString(m, "HeapCTypeSubclass");
6132 if (oldtype == NULL || newtype == NULL) {
6133 goto cleanup_finalize;
6134 }
6135
6136 if (PyObject_SetAttrString(self, "__class__", newtype) < 0) {
6137 goto cleanup_finalize;
6138 }
6139 refcnt = PyLong_FromSsize_t(Py_REFCNT(oldtype));
6140 if (refcnt == NULL) {
6141 goto cleanup_finalize;
6142 }
6143 if (PyObject_SetAttrString(oldtype, "refcnt_in_del", refcnt) < 0) {
6144 goto cleanup_finalize;
6145 }
6146 Py_DECREF(refcnt);
6147 refcnt = PyLong_FromSsize_t(Py_REFCNT(newtype));
6148 if (refcnt == NULL) {
6149 goto cleanup_finalize;
6150 }
6151 if (PyObject_SetAttrString(newtype, "refcnt_in_del", refcnt) < 0) {
6152 goto cleanup_finalize;
6153 }
6154
6155 cleanup_finalize:
6156 Py_XDECREF(oldtype);
6157 Py_XDECREF(newtype);
6158 Py_XDECREF(refcnt);
6159
6160 /* Restore the saved exception. */
6161 PyErr_Restore(error_type, error_value, error_traceback);
6162 }
6163
6164 static PyType_Slot HeapCTypeSubclassWithFinalizer_slots[] = {
6165 {Py_tp_init, heapctypesubclasswithfinalizer_init},
6166 {Py_tp_members, heapctypesubclass_members},
6167 {Py_tp_finalize, heapctypesubclasswithfinalizer_finalize},
6168 {Py_tp_doc, (char*)heapctypesubclasswithfinalizer__doc__},
6169 {0, 0},
6170 };
6171
6172 static PyType_Spec HeapCTypeSubclassWithFinalizer_spec = {
6173 "_testcapi.HeapCTypeSubclassWithFinalizer",
6174 sizeof(HeapCTypeSubclassObject),
6175 0,
6176 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
6177 HeapCTypeSubclassWithFinalizer_slots
6178 };
6179
6180 PyDoc_STRVAR(heapctypesetattr__doc__,
6181 "A heap type without GC, but with overridden __setattr__.\n\n"
6182 "The 'value' attribute is set to 10 in __init__ and updated via attribute setting.");
6183
6184 typedef struct {
6185 PyObject_HEAD
6186 long value;
6187 } HeapCTypeSetattrObject;
6188
6189 static struct PyMemberDef heapctypesetattr_members[] = {
6190 {"pvalue", T_LONG, offsetof(HeapCTypeSetattrObject, value)},
6191 {NULL} /* Sentinel */
6192 };
6193
6194 static int
heapctypesetattr_init(PyObject * self,PyObject * args,PyObject * kwargs)6195 heapctypesetattr_init(PyObject *self, PyObject *args, PyObject *kwargs)
6196 {
6197 ((HeapCTypeSetattrObject *)self)->value = 10;
6198 return 0;
6199 }
6200
6201 static void
heapctypesetattr_dealloc(HeapCTypeSetattrObject * self)6202 heapctypesetattr_dealloc(HeapCTypeSetattrObject *self)
6203 {
6204 PyTypeObject *tp = Py_TYPE(self);
6205 PyObject_Del(self);
6206 Py_DECREF(tp);
6207 }
6208
6209 static int
heapctypesetattr_setattro(HeapCTypeSetattrObject * self,PyObject * attr,PyObject * value)6210 heapctypesetattr_setattro(HeapCTypeSetattrObject *self, PyObject *attr, PyObject *value)
6211 {
6212 PyObject *svalue = PyUnicode_FromString("value");
6213 if (svalue == NULL)
6214 return -1;
6215 int eq = PyObject_RichCompareBool(svalue, attr, Py_EQ);
6216 Py_DECREF(svalue);
6217 if (eq < 0)
6218 return -1;
6219 if (!eq) {
6220 return PyObject_GenericSetAttr((PyObject*) self, attr, value);
6221 }
6222 if (value == NULL) {
6223 self->value = 0;
6224 return 0;
6225 }
6226 PyObject *ivalue = PyNumber_Long(value);
6227 if (ivalue == NULL)
6228 return -1;
6229 long v = PyLong_AsLong(ivalue);
6230 Py_DECREF(ivalue);
6231 if (v == -1 && PyErr_Occurred())
6232 return -1;
6233 self->value = v;
6234 return 0;
6235 }
6236
6237 static PyType_Slot HeapCTypeSetattr_slots[] = {
6238 {Py_tp_init, heapctypesetattr_init},
6239 {Py_tp_members, heapctypesetattr_members},
6240 {Py_tp_setattro, heapctypesetattr_setattro},
6241 {Py_tp_dealloc, heapctypesetattr_dealloc},
6242 {Py_tp_doc, (char*)heapctypesetattr__doc__},
6243 {0, 0},
6244 };
6245
6246 static PyType_Spec HeapCTypeSetattr_spec = {
6247 "_testcapi.HeapCTypeSetattr",
6248 sizeof(HeapCTypeSetattrObject),
6249 0,
6250 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
6251 HeapCTypeSetattr_slots
6252 };
6253
6254 static struct PyModuleDef _testcapimodule = {
6255 PyModuleDef_HEAD_INIT,
6256 "_testcapi",
6257 NULL,
6258 -1,
6259 TestMethods,
6260 NULL,
6261 NULL,
6262 NULL,
6263 NULL
6264 };
6265
6266 /* Per PEP 489, this module will not be converted to multi-phase initialization
6267 */
6268
6269 PyMODINIT_FUNC
PyInit__testcapi(void)6270 PyInit__testcapi(void)
6271 {
6272 PyObject *m;
6273
6274 m = PyModule_Create(&_testcapimodule);
6275 if (m == NULL)
6276 return NULL;
6277
6278 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
6279
6280 Py_TYPE(&test_structmembersType)=&PyType_Type;
6281 Py_INCREF(&test_structmembersType);
6282 /* don't use a name starting with "test", since we don't want
6283 test_capi to automatically call this */
6284 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
6285 if (PyType_Ready(&matmulType) < 0)
6286 return NULL;
6287 Py_INCREF(&matmulType);
6288 PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType);
6289 if (PyType_Ready(&ipowType) < 0) {
6290 return NULL;
6291 }
6292 Py_INCREF(&ipowType);
6293 PyModule_AddObject(m, "ipowType", (PyObject *)&ipowType);
6294
6295 if (PyType_Ready(&awaitType) < 0)
6296 return NULL;
6297 Py_INCREF(&awaitType);
6298 PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType);
6299
6300 MyList_Type.tp_base = &PyList_Type;
6301 if (PyType_Ready(&MyList_Type) < 0)
6302 return NULL;
6303 Py_INCREF(&MyList_Type);
6304 PyModule_AddObject(m, "MyList", (PyObject *)&MyList_Type);
6305
6306 /* bpo-37250: old Cython code sets tp_print to 0, we check that
6307 * this doesn't break anything. */
6308 MyList_Type.tp_print = 0;
6309
6310 if (PyType_Ready(&MethodDescriptorBase_Type) < 0)
6311 return NULL;
6312 Py_INCREF(&MethodDescriptorBase_Type);
6313 PyModule_AddObject(m, "MethodDescriptorBase", (PyObject *)&MethodDescriptorBase_Type);
6314
6315 MethodDescriptorDerived_Type.tp_base = &MethodDescriptorBase_Type;
6316 if (PyType_Ready(&MethodDescriptorDerived_Type) < 0)
6317 return NULL;
6318 Py_INCREF(&MethodDescriptorDerived_Type);
6319 PyModule_AddObject(m, "MethodDescriptorDerived", (PyObject *)&MethodDescriptorDerived_Type);
6320
6321 MethodDescriptorNopGet_Type.tp_base = &MethodDescriptorBase_Type;
6322 if (PyType_Ready(&MethodDescriptorNopGet_Type) < 0)
6323 return NULL;
6324 Py_INCREF(&MethodDescriptorNopGet_Type);
6325 PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type);
6326
6327 MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type;
6328 if (PyType_Ready(&MethodDescriptor2_Type) < 0)
6329 return NULL;
6330 Py_INCREF(&MethodDescriptor2_Type);
6331 PyModule_AddObject(m, "MethodDescriptor2", (PyObject *)&MethodDescriptor2_Type);
6332
6333 if (PyType_Ready(&GenericAlias_Type) < 0)
6334 return NULL;
6335 Py_INCREF(&GenericAlias_Type);
6336 PyModule_AddObject(m, "GenericAlias", (PyObject *)&GenericAlias_Type);
6337
6338 if (PyType_Ready(&Generic_Type) < 0)
6339 return NULL;
6340 Py_INCREF(&Generic_Type);
6341 PyModule_AddObject(m, "Generic", (PyObject *)&Generic_Type);
6342
6343 PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception;
6344 if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) {
6345 return NULL;
6346 }
6347 Py_INCREF(&PyRecursingInfinitelyError_Type);
6348 PyModule_AddObject(m, "RecursingInfinitelyError",
6349 (PyObject *)&PyRecursingInfinitelyError_Type);
6350
6351 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
6352 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
6353 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
6354 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
6355 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
6356 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
6357 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
6358 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
6359 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
6360 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
6361 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
6362 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
6363 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
6364 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
6365 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
6366 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
6367 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
6368 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
6369 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
6370 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
6371 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
6372 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
6373 PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
6374 Py_INCREF(&PyInstanceMethod_Type);
6375 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
6376
6377 PyModule_AddIntConstant(m, "the_number_three", 3);
6378 PyObject *v;
6379 #ifdef WITH_PYMALLOC
6380 v = Py_True;
6381 #else
6382 v = Py_False;
6383 #endif
6384 Py_INCREF(v);
6385 PyModule_AddObject(m, "WITH_PYMALLOC", v);
6386
6387 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
6388 Py_INCREF(TestError);
6389 PyModule_AddObject(m, "error", TestError);
6390
6391 PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec);
6392 if (HeapGcCType == NULL) {
6393 return NULL;
6394 }
6395 PyModule_AddObject(m, "HeapGcCType", HeapGcCType);
6396
6397 PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec);
6398 if (HeapCType == NULL) {
6399 return NULL;
6400 }
6401 PyObject *subclass_bases = PyTuple_Pack(1, HeapCType);
6402 if (subclass_bases == NULL) {
6403 return NULL;
6404 }
6405 PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases);
6406 if (HeapCTypeSubclass == NULL) {
6407 return NULL;
6408 }
6409 Py_DECREF(subclass_bases);
6410 PyModule_AddObject(m, "HeapCTypeSubclass", HeapCTypeSubclass);
6411
6412 PyObject *HeapCTypeSetattr = PyType_FromSpec(&HeapCTypeSetattr_spec);
6413 if (HeapCTypeSetattr == NULL) {
6414 return NULL;
6415 }
6416 PyModule_AddObject(m, "HeapCTypeSetattr", HeapCTypeSetattr);
6417
6418 PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass);
6419 if (subclass_with_finalizer_bases == NULL) {
6420 return NULL;
6421 }
6422 PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases(
6423 &HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases);
6424 if (HeapCTypeSubclassWithFinalizer == NULL) {
6425 return NULL;
6426 }
6427 Py_DECREF(subclass_with_finalizer_bases);
6428 PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer);
6429
6430 PyState_AddModule(m, &_testcapimodule);
6431 return m;
6432 }
6433
6434
6435 /* Test the C API exposed when PY_SSIZE_T_CLEAN is not defined */
6436
6437 #undef Py_BuildValue
6438 PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...);
6439
6440 static PyObject *
test_buildvalue_issue38913(PyObject * self,PyObject * Py_UNUSED (ignored))6441 test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored))
6442 {
6443 PyObject *res;
6444 const char str[] = "string";
6445 const Py_UNICODE unicode[] = L"unicode";
6446 PyErr_SetNone(PyExc_ZeroDivisionError);
6447
6448 res = Py_BuildValue("(s#O)", str, 1, Py_None);
6449 assert(res == NULL);
6450 if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
6451 return NULL;
6452 }
6453 res = Py_BuildValue("(z#O)", str, 1, Py_None);
6454 assert(res == NULL);
6455 if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
6456 return NULL;
6457 }
6458 res = Py_BuildValue("(y#O)", str, 1, Py_None);
6459 assert(res == NULL);
6460 if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
6461 return NULL;
6462 }
6463 res = Py_BuildValue("(u#O)", unicode, 1, Py_None);
6464 assert(res == NULL);
6465 if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
6466 return NULL;
6467 }
6468
6469 PyErr_Clear();
6470 Py_RETURN_NONE;
6471 }
6472