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 #define PY_SSIZE_T_CLEAN
9
10 #include "Python.h"
11 #include <float.h>
12 #include "structmember.h"
13 #include "datetime.h"
14 #include "marshal.h"
15 #include <signal.h>
16
17 #ifdef MS_WINDOWS
18 # include <winsock2.h> /* struct timeval */
19 #endif
20
21 #ifdef WITH_THREAD
22 #include "pythread.h"
23 #endif /* WITH_THREAD */
24 static PyObject *TestError; /* set to exception object in init */
25
26 /* Raise TestError with test_name + ": " + msg, and return NULL. */
27
28 static PyObject *
raiseTestError(const char * test_name,const char * msg)29 raiseTestError(const char* test_name, const char* msg)
30 {
31 PyErr_Format(TestError, "%s: %s", test_name, msg);
32 return NULL;
33 }
34
35 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
36
37 The ones derived from autoconf on the UNIX-like OSes can be relied
38 upon (in the absence of sloppy cross-compiling), but the Windows
39 platforms have these hardcoded. Better safe than sorry.
40 */
41 static PyObject*
sizeof_error(const char * fatname,const char * typname,int expected,int got)42 sizeof_error(const char* fatname, const char* typname,
43 int expected, int got)
44 {
45 PyErr_Format(TestError,
46 "%s #define == %d but sizeof(%s) == %d",
47 fatname, expected, typname, got);
48 return (PyObject*)NULL;
49 }
50
51 static PyObject*
test_config(PyObject * self)52 test_config(PyObject *self)
53 {
54 #define CHECK_SIZEOF(FATNAME, TYPE) \
55 if (FATNAME != sizeof(TYPE)) \
56 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
57
58 CHECK_SIZEOF(SIZEOF_SHORT, short);
59 CHECK_SIZEOF(SIZEOF_INT, int);
60 CHECK_SIZEOF(SIZEOF_LONG, long);
61 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
62 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
63 CHECK_SIZEOF(SIZEOF_LONG_LONG, long long);
64
65 #undef CHECK_SIZEOF
66
67 Py_INCREF(Py_None);
68 return Py_None;
69 }
70
71 static PyObject*
test_sizeof_c_types(PyObject * self)72 test_sizeof_c_types(PyObject *self)
73 {
74 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
75 #pragma GCC diagnostic push
76 #pragma GCC diagnostic ignored "-Wtype-limits"
77 #endif
78 #define CHECK_SIZEOF(TYPE, EXPECTED) \
79 if (EXPECTED != sizeof(TYPE)) { \
80 PyErr_Format(TestError, \
81 "sizeof(%s) = %u instead of %u", \
82 #TYPE, sizeof(TYPE), EXPECTED); \
83 return (PyObject*)NULL; \
84 }
85 #define IS_SIGNED(TYPE) (((TYPE)-1) < (TYPE)0)
86 #define CHECK_SIGNNESS(TYPE, SIGNED) \
87 if (IS_SIGNED(TYPE) != SIGNED) { \
88 PyErr_Format(TestError, \
89 "%s signness is, instead of %i", \
90 #TYPE, IS_SIGNED(TYPE), SIGNED); \
91 return (PyObject*)NULL; \
92 }
93
94 /* integer types */
95 CHECK_SIZEOF(Py_UCS1, 1);
96 CHECK_SIZEOF(Py_UCS2, 2);
97 CHECK_SIZEOF(Py_UCS4, 4);
98 CHECK_SIGNNESS(Py_UCS1, 0);
99 CHECK_SIGNNESS(Py_UCS2, 0);
100 CHECK_SIGNNESS(Py_UCS4, 0);
101 CHECK_SIZEOF(int32_t, 4);
102 CHECK_SIGNNESS(int32_t, 1);
103 CHECK_SIZEOF(uint32_t, 4);
104 CHECK_SIGNNESS(uint32_t, 0);
105 CHECK_SIZEOF(int64_t, 8);
106 CHECK_SIGNNESS(int64_t, 1);
107 CHECK_SIZEOF(uint64_t, 8);
108 CHECK_SIGNNESS(uint64_t, 0);
109
110 /* pointer/size types */
111 CHECK_SIZEOF(size_t, sizeof(void *));
112 CHECK_SIGNNESS(size_t, 0);
113 CHECK_SIZEOF(Py_ssize_t, sizeof(void *));
114 CHECK_SIGNNESS(Py_ssize_t, 1);
115
116 CHECK_SIZEOF(uintptr_t, sizeof(void *));
117 CHECK_SIGNNESS(uintptr_t, 0);
118 CHECK_SIZEOF(intptr_t, sizeof(void *));
119 CHECK_SIGNNESS(intptr_t, 1);
120
121 Py_INCREF(Py_None);
122 return Py_None;
123
124 #undef IS_SIGNED
125 #undef CHECK_SIGNESS
126 #undef CHECK_SIZEOF
127 #if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))
128 #pragma GCC diagnostic pop
129 #endif
130 }
131
132
133 static PyObject*
test_list_api(PyObject * self)134 test_list_api(PyObject *self)
135 {
136 PyObject* list;
137 int i;
138
139 /* SF bug 132008: PyList_Reverse segfaults */
140 #define NLIST 30
141 list = PyList_New(NLIST);
142 if (list == (PyObject*)NULL)
143 return (PyObject*)NULL;
144 /* list = range(NLIST) */
145 for (i = 0; i < NLIST; ++i) {
146 PyObject* anint = PyLong_FromLong(i);
147 if (anint == (PyObject*)NULL) {
148 Py_DECREF(list);
149 return (PyObject*)NULL;
150 }
151 PyList_SET_ITEM(list, i, anint);
152 }
153 /* list.reverse(), via PyList_Reverse() */
154 i = PyList_Reverse(list); /* should not blow up! */
155 if (i != 0) {
156 Py_DECREF(list);
157 return (PyObject*)NULL;
158 }
159 /* Check that list == range(29, -1, -1) now */
160 for (i = 0; i < NLIST; ++i) {
161 PyObject* anint = PyList_GET_ITEM(list, i);
162 if (PyLong_AS_LONG(anint) != NLIST-1-i) {
163 PyErr_SetString(TestError,
164 "test_list_api: reverse screwed up");
165 Py_DECREF(list);
166 return (PyObject*)NULL;
167 }
168 }
169 Py_DECREF(list);
170 #undef NLIST
171
172 Py_INCREF(Py_None);
173 return Py_None;
174 }
175
176 static int
test_dict_inner(int count)177 test_dict_inner(int count)
178 {
179 Py_ssize_t pos = 0, iterations = 0;
180 int i;
181 PyObject *dict = PyDict_New();
182 PyObject *v, *k;
183
184 if (dict == NULL)
185 return -1;
186
187 for (i = 0; i < count; i++) {
188 v = PyLong_FromLong(i);
189 if (v == NULL) {
190 return -1;
191 }
192 if (PyDict_SetItem(dict, v, v) < 0) {
193 Py_DECREF(v);
194 return -1;
195 }
196 Py_DECREF(v);
197 }
198
199 while (PyDict_Next(dict, &pos, &k, &v)) {
200 PyObject *o;
201 iterations++;
202
203 i = PyLong_AS_LONG(v) + 1;
204 o = PyLong_FromLong(i);
205 if (o == NULL)
206 return -1;
207 if (PyDict_SetItem(dict, k, o) < 0) {
208 Py_DECREF(o);
209 return -1;
210 }
211 Py_DECREF(o);
212 }
213
214 Py_DECREF(dict);
215
216 if (iterations != count) {
217 PyErr_SetString(
218 TestError,
219 "test_dict_iteration: dict iteration went wrong ");
220 return -1;
221 } else {
222 return 0;
223 }
224 }
225
226 static PyObject*
test_dict_iteration(PyObject * self)227 test_dict_iteration(PyObject* self)
228 {
229 int i;
230
231 for (i = 0; i < 200; i++) {
232 if (test_dict_inner(i) < 0) {
233 return NULL;
234 }
235 }
236
237 Py_INCREF(Py_None);
238 return Py_None;
239 }
240
241 static PyObject*
dict_getitem_knownhash(PyObject * self,PyObject * args)242 dict_getitem_knownhash(PyObject *self, PyObject *args)
243 {
244 PyObject *mp, *key, *result;
245 Py_ssize_t hash;
246
247 if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
248 &mp, &key, &hash)) {
249 return NULL;
250 }
251
252 result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
253 if (result == NULL && !PyErr_Occurred()) {
254 _PyErr_SetKeyError(key);
255 return NULL;
256 }
257
258 Py_XINCREF(result);
259 return result;
260 }
261
262 static PyObject*
dict_hassplittable(PyObject * self,PyObject * arg)263 dict_hassplittable(PyObject *self, PyObject *arg)
264 {
265 if (!PyDict_Check(arg)) {
266 PyErr_Format(PyExc_TypeError,
267 "dict_hassplittable() argument must be dict, not '%s'",
268 arg->ob_type->tp_name);
269 return NULL;
270 }
271
272 return PyBool_FromLong(_PyDict_HasSplitTable((PyDictObject*)arg));
273 }
274
275 /* Issue #4701: Check that PyObject_Hash implicitly calls
276 * PyType_Ready if it hasn't already been called
277 */
278 static PyTypeObject _HashInheritanceTester_Type = {
279 PyVarObject_HEAD_INIT(NULL, 0)
280 "hashinheritancetester", /* Name of this type */
281 sizeof(PyObject), /* Basic object size */
282 0, /* Item size for varobject */
283 (destructor)PyObject_Del, /* tp_dealloc */
284 0, /* tp_print */
285 0, /* tp_getattr */
286 0, /* tp_setattr */
287 0, /* tp_reserved */
288 0, /* tp_repr */
289 0, /* tp_as_number */
290 0, /* tp_as_sequence */
291 0, /* tp_as_mapping */
292 0, /* tp_hash */
293 0, /* tp_call */
294 0, /* tp_str */
295 PyObject_GenericGetAttr, /* tp_getattro */
296 0, /* tp_setattro */
297 0, /* tp_as_buffer */
298 Py_TPFLAGS_DEFAULT, /* tp_flags */
299 0, /* tp_doc */
300 0, /* tp_traverse */
301 0, /* tp_clear */
302 0, /* tp_richcompare */
303 0, /* tp_weaklistoffset */
304 0, /* tp_iter */
305 0, /* tp_iternext */
306 0, /* tp_methods */
307 0, /* tp_members */
308 0, /* tp_getset */
309 0, /* tp_base */
310 0, /* tp_dict */
311 0, /* tp_descr_get */
312 0, /* tp_descr_set */
313 0, /* tp_dictoffset */
314 0, /* tp_init */
315 0, /* tp_alloc */
316 PyType_GenericNew, /* tp_new */
317 };
318
319 static PyObject*
test_lazy_hash_inheritance(PyObject * self)320 test_lazy_hash_inheritance(PyObject* self)
321 {
322 PyTypeObject *type;
323 PyObject *obj;
324 Py_hash_t hash;
325
326 type = &_HashInheritanceTester_Type;
327
328 if (type->tp_dict != NULL)
329 /* The type has already been initialized. This probably means
330 -R is being used. */
331 Py_RETURN_NONE;
332
333
334 obj = PyObject_New(PyObject, type);
335 if (obj == NULL) {
336 PyErr_Clear();
337 PyErr_SetString(
338 TestError,
339 "test_lazy_hash_inheritance: failed to create object");
340 return NULL;
341 }
342
343 if (type->tp_dict != NULL) {
344 PyErr_SetString(
345 TestError,
346 "test_lazy_hash_inheritance: type initialised too soon");
347 Py_DECREF(obj);
348 return NULL;
349 }
350
351 hash = PyObject_Hash(obj);
352 if ((hash == -1) && PyErr_Occurred()) {
353 PyErr_Clear();
354 PyErr_SetString(
355 TestError,
356 "test_lazy_hash_inheritance: could not hash object");
357 Py_DECREF(obj);
358 return NULL;
359 }
360
361 if (type->tp_dict == NULL) {
362 PyErr_SetString(
363 TestError,
364 "test_lazy_hash_inheritance: type not initialised by hash()");
365 Py_DECREF(obj);
366 return NULL;
367 }
368
369 if (type->tp_hash != PyType_Type.tp_hash) {
370 PyErr_SetString(
371 TestError,
372 "test_lazy_hash_inheritance: unexpected hash function");
373 Py_DECREF(obj);
374 return NULL;
375 }
376
377 Py_DECREF(obj);
378
379 Py_RETURN_NONE;
380 }
381
382
383 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and
384 PyLong_{As, From}{Unsigned,}LongLong().
385
386 Note that the meat of the test is contained in testcapi_long.h.
387 This is revolting, but delicate code duplication is worse: "almost
388 exactly the same" code is needed to test long long, but the ubiquitous
389 dependence on type names makes it impossible to use a parameterized
390 function. A giant macro would be even worse than this. A C++ template
391 would be perfect.
392
393 The "report an error" functions are deliberately not part of the #include
394 file: if the test fails, you can set a breakpoint in the appropriate
395 error function directly, and crawl back from there in the debugger.
396 */
397
398 #define UNBIND(X) Py_DECREF(X); (X) = NULL
399
400 static PyObject *
raise_test_long_error(const char * msg)401 raise_test_long_error(const char* msg)
402 {
403 return raiseTestError("test_long_api", msg);
404 }
405
406 #define TESTNAME test_long_api_inner
407 #define TYPENAME long
408 #define F_S_TO_PY PyLong_FromLong
409 #define F_PY_TO_S PyLong_AsLong
410 #define F_U_TO_PY PyLong_FromUnsignedLong
411 #define F_PY_TO_U PyLong_AsUnsignedLong
412
413 #include "testcapi_long.h"
414
415 static PyObject *
test_long_api(PyObject * self)416 test_long_api(PyObject* self)
417 {
418 return TESTNAME(raise_test_long_error);
419 }
420
421 #undef TESTNAME
422 #undef TYPENAME
423 #undef F_S_TO_PY
424 #undef F_PY_TO_S
425 #undef F_U_TO_PY
426 #undef F_PY_TO_U
427
428 static PyObject *
raise_test_longlong_error(const char * msg)429 raise_test_longlong_error(const char* msg)
430 {
431 return raiseTestError("test_longlong_api", msg);
432 }
433
434 #define TESTNAME test_longlong_api_inner
435 #define TYPENAME long long
436 #define F_S_TO_PY PyLong_FromLongLong
437 #define F_PY_TO_S PyLong_AsLongLong
438 #define F_U_TO_PY PyLong_FromUnsignedLongLong
439 #define F_PY_TO_U PyLong_AsUnsignedLongLong
440
441 #include "testcapi_long.h"
442
443 static PyObject *
test_longlong_api(PyObject * self,PyObject * args)444 test_longlong_api(PyObject* self, PyObject *args)
445 {
446 return TESTNAME(raise_test_longlong_error);
447 }
448
449 #undef TESTNAME
450 #undef TYPENAME
451 #undef F_S_TO_PY
452 #undef F_PY_TO_S
453 #undef F_U_TO_PY
454 #undef F_PY_TO_U
455
456 /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
457 is tested by test_long_api_inner. This test will concentrate on proper
458 handling of overflow.
459 */
460
461 static PyObject *
test_long_and_overflow(PyObject * self)462 test_long_and_overflow(PyObject *self)
463 {
464 PyObject *num, *one, *temp;
465 long value;
466 int overflow;
467
468 /* Test that overflow is set properly for a large value. */
469 /* num is a number larger than LONG_MAX even on 64-bit platforms */
470 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
471 if (num == NULL)
472 return NULL;
473 overflow = 1234;
474 value = PyLong_AsLongAndOverflow(num, &overflow);
475 Py_DECREF(num);
476 if (value == -1 && PyErr_Occurred())
477 return NULL;
478 if (value != -1)
479 return raiseTestError("test_long_and_overflow",
480 "return value was not set to -1");
481 if (overflow != 1)
482 return raiseTestError("test_long_and_overflow",
483 "overflow was not set to 1");
484
485 /* Same again, with num = LONG_MAX + 1 */
486 num = PyLong_FromLong(LONG_MAX);
487 if (num == NULL)
488 return NULL;
489 one = PyLong_FromLong(1L);
490 if (one == NULL) {
491 Py_DECREF(num);
492 return NULL;
493 }
494 temp = PyNumber_Add(num, one);
495 Py_DECREF(one);
496 Py_DECREF(num);
497 num = temp;
498 if (num == NULL)
499 return NULL;
500 overflow = 0;
501 value = PyLong_AsLongAndOverflow(num, &overflow);
502 Py_DECREF(num);
503 if (value == -1 && PyErr_Occurred())
504 return NULL;
505 if (value != -1)
506 return raiseTestError("test_long_and_overflow",
507 "return value was not set to -1");
508 if (overflow != 1)
509 return raiseTestError("test_long_and_overflow",
510 "overflow was not set to 1");
511
512 /* Test that overflow is set properly for a large negative value. */
513 /* num is a number smaller than LONG_MIN even on 64-bit platforms */
514 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
515 if (num == NULL)
516 return NULL;
517 overflow = 1234;
518 value = PyLong_AsLongAndOverflow(num, &overflow);
519 Py_DECREF(num);
520 if (value == -1 && PyErr_Occurred())
521 return NULL;
522 if (value != -1)
523 return raiseTestError("test_long_and_overflow",
524 "return value was not set to -1");
525 if (overflow != -1)
526 return raiseTestError("test_long_and_overflow",
527 "overflow was not set to -1");
528
529 /* Same again, with num = LONG_MIN - 1 */
530 num = PyLong_FromLong(LONG_MIN);
531 if (num == NULL)
532 return NULL;
533 one = PyLong_FromLong(1L);
534 if (one == NULL) {
535 Py_DECREF(num);
536 return NULL;
537 }
538 temp = PyNumber_Subtract(num, one);
539 Py_DECREF(one);
540 Py_DECREF(num);
541 num = temp;
542 if (num == NULL)
543 return NULL;
544 overflow = 0;
545 value = PyLong_AsLongAndOverflow(num, &overflow);
546 Py_DECREF(num);
547 if (value == -1 && PyErr_Occurred())
548 return NULL;
549 if (value != -1)
550 return raiseTestError("test_long_and_overflow",
551 "return value was not set to -1");
552 if (overflow != -1)
553 return raiseTestError("test_long_and_overflow",
554 "overflow was not set to -1");
555
556 /* Test that overflow is cleared properly for small values. */
557 num = PyLong_FromString("FF", NULL, 16);
558 if (num == NULL)
559 return NULL;
560 overflow = 1234;
561 value = PyLong_AsLongAndOverflow(num, &overflow);
562 Py_DECREF(num);
563 if (value == -1 && PyErr_Occurred())
564 return NULL;
565 if (value != 0xFF)
566 return raiseTestError("test_long_and_overflow",
567 "expected return value 0xFF");
568 if (overflow != 0)
569 return raiseTestError("test_long_and_overflow",
570 "overflow was not cleared");
571
572 num = PyLong_FromString("-FF", NULL, 16);
573 if (num == NULL)
574 return NULL;
575 overflow = 0;
576 value = PyLong_AsLongAndOverflow(num, &overflow);
577 Py_DECREF(num);
578 if (value == -1 && PyErr_Occurred())
579 return NULL;
580 if (value != -0xFF)
581 return raiseTestError("test_long_and_overflow",
582 "expected return value 0xFF");
583 if (overflow != 0)
584 return raiseTestError("test_long_and_overflow",
585 "overflow was set incorrectly");
586
587 num = PyLong_FromLong(LONG_MAX);
588 if (num == NULL)
589 return NULL;
590 overflow = 1234;
591 value = PyLong_AsLongAndOverflow(num, &overflow);
592 Py_DECREF(num);
593 if (value == -1 && PyErr_Occurred())
594 return NULL;
595 if (value != LONG_MAX)
596 return raiseTestError("test_long_and_overflow",
597 "expected return value LONG_MAX");
598 if (overflow != 0)
599 return raiseTestError("test_long_and_overflow",
600 "overflow was not cleared");
601
602 num = PyLong_FromLong(LONG_MIN);
603 if (num == NULL)
604 return NULL;
605 overflow = 0;
606 value = PyLong_AsLongAndOverflow(num, &overflow);
607 Py_DECREF(num);
608 if (value == -1 && PyErr_Occurred())
609 return NULL;
610 if (value != LONG_MIN)
611 return raiseTestError("test_long_and_overflow",
612 "expected return value LONG_MIN");
613 if (overflow != 0)
614 return raiseTestError("test_long_and_overflow",
615 "overflow was not cleared");
616
617 Py_INCREF(Py_None);
618 return Py_None;
619 }
620
621 /* Test the PyLong_AsLongLongAndOverflow API. General conversion to
622 long long is tested by test_long_api_inner. This test will
623 concentrate on proper handling of overflow.
624 */
625
626 static PyObject *
test_long_long_and_overflow(PyObject * self)627 test_long_long_and_overflow(PyObject *self)
628 {
629 PyObject *num, *one, *temp;
630 long long value;
631 int overflow;
632
633 /* Test that overflow is set properly for a large value. */
634 /* num is a number larger than PY_LLONG_MAX on a typical machine. */
635 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
636 if (num == NULL)
637 return NULL;
638 overflow = 1234;
639 value = PyLong_AsLongLongAndOverflow(num, &overflow);
640 Py_DECREF(num);
641 if (value == -1 && PyErr_Occurred())
642 return NULL;
643 if (value != -1)
644 return raiseTestError("test_long_long_and_overflow",
645 "return value was not set to -1");
646 if (overflow != 1)
647 return raiseTestError("test_long_long_and_overflow",
648 "overflow was not set to 1");
649
650 /* Same again, with num = PY_LLONG_MAX + 1 */
651 num = PyLong_FromLongLong(PY_LLONG_MAX);
652 if (num == NULL)
653 return NULL;
654 one = PyLong_FromLong(1L);
655 if (one == NULL) {
656 Py_DECREF(num);
657 return NULL;
658 }
659 temp = PyNumber_Add(num, one);
660 Py_DECREF(one);
661 Py_DECREF(num);
662 num = temp;
663 if (num == NULL)
664 return NULL;
665 overflow = 0;
666 value = PyLong_AsLongLongAndOverflow(num, &overflow);
667 Py_DECREF(num);
668 if (value == -1 && PyErr_Occurred())
669 return NULL;
670 if (value != -1)
671 return raiseTestError("test_long_long_and_overflow",
672 "return value was not set to -1");
673 if (overflow != 1)
674 return raiseTestError("test_long_long_and_overflow",
675 "overflow was not set to 1");
676
677 /* Test that overflow is set properly for a large negative value. */
678 /* num is a number smaller than PY_LLONG_MIN on a typical platform */
679 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
680 if (num == NULL)
681 return NULL;
682 overflow = 1234;
683 value = PyLong_AsLongLongAndOverflow(num, &overflow);
684 Py_DECREF(num);
685 if (value == -1 && PyErr_Occurred())
686 return NULL;
687 if (value != -1)
688 return raiseTestError("test_long_long_and_overflow",
689 "return value was not set to -1");
690 if (overflow != -1)
691 return raiseTestError("test_long_long_and_overflow",
692 "overflow was not set to -1");
693
694 /* Same again, with num = PY_LLONG_MIN - 1 */
695 num = PyLong_FromLongLong(PY_LLONG_MIN);
696 if (num == NULL)
697 return NULL;
698 one = PyLong_FromLong(1L);
699 if (one == NULL) {
700 Py_DECREF(num);
701 return NULL;
702 }
703 temp = PyNumber_Subtract(num, one);
704 Py_DECREF(one);
705 Py_DECREF(num);
706 num = temp;
707 if (num == NULL)
708 return NULL;
709 overflow = 0;
710 value = PyLong_AsLongLongAndOverflow(num, &overflow);
711 Py_DECREF(num);
712 if (value == -1 && PyErr_Occurred())
713 return NULL;
714 if (value != -1)
715 return raiseTestError("test_long_long_and_overflow",
716 "return value was not set to -1");
717 if (overflow != -1)
718 return raiseTestError("test_long_long_and_overflow",
719 "overflow was not set to -1");
720
721 /* Test that overflow is cleared properly for small values. */
722 num = PyLong_FromString("FF", NULL, 16);
723 if (num == NULL)
724 return NULL;
725 overflow = 1234;
726 value = PyLong_AsLongLongAndOverflow(num, &overflow);
727 Py_DECREF(num);
728 if (value == -1 && PyErr_Occurred())
729 return NULL;
730 if (value != 0xFF)
731 return raiseTestError("test_long_long_and_overflow",
732 "expected return value 0xFF");
733 if (overflow != 0)
734 return raiseTestError("test_long_long_and_overflow",
735 "overflow was not cleared");
736
737 num = PyLong_FromString("-FF", NULL, 16);
738 if (num == NULL)
739 return NULL;
740 overflow = 0;
741 value = PyLong_AsLongLongAndOverflow(num, &overflow);
742 Py_DECREF(num);
743 if (value == -1 && PyErr_Occurred())
744 return NULL;
745 if (value != -0xFF)
746 return raiseTestError("test_long_long_and_overflow",
747 "expected return value 0xFF");
748 if (overflow != 0)
749 return raiseTestError("test_long_long_and_overflow",
750 "overflow was set incorrectly");
751
752 num = PyLong_FromLongLong(PY_LLONG_MAX);
753 if (num == NULL)
754 return NULL;
755 overflow = 1234;
756 value = PyLong_AsLongLongAndOverflow(num, &overflow);
757 Py_DECREF(num);
758 if (value == -1 && PyErr_Occurred())
759 return NULL;
760 if (value != PY_LLONG_MAX)
761 return raiseTestError("test_long_long_and_overflow",
762 "expected return value PY_LLONG_MAX");
763 if (overflow != 0)
764 return raiseTestError("test_long_long_and_overflow",
765 "overflow was not cleared");
766
767 num = PyLong_FromLongLong(PY_LLONG_MIN);
768 if (num == NULL)
769 return NULL;
770 overflow = 0;
771 value = PyLong_AsLongLongAndOverflow(num, &overflow);
772 Py_DECREF(num);
773 if (value == -1 && PyErr_Occurred())
774 return NULL;
775 if (value != PY_LLONG_MIN)
776 return raiseTestError("test_long_long_and_overflow",
777 "expected return value PY_LLONG_MIN");
778 if (overflow != 0)
779 return raiseTestError("test_long_long_and_overflow",
780 "overflow was not cleared");
781
782 Py_INCREF(Py_None);
783 return Py_None;
784 }
785
786 /* Test the PyLong_As{Size,Ssize}_t API. At present this just tests that
787 non-integer arguments are handled correctly. It should be extended to
788 test overflow handling.
789 */
790
791 static PyObject *
test_long_as_size_t(PyObject * self)792 test_long_as_size_t(PyObject *self)
793 {
794 size_t out_u;
795 Py_ssize_t out_s;
796
797 Py_INCREF(Py_None);
798
799 out_u = PyLong_AsSize_t(Py_None);
800 if (out_u != (size_t)-1 || !PyErr_Occurred())
801 return raiseTestError("test_long_as_size_t",
802 "PyLong_AsSize_t(None) didn't complain");
803 if (!PyErr_ExceptionMatches(PyExc_TypeError))
804 return raiseTestError("test_long_as_size_t",
805 "PyLong_AsSize_t(None) raised "
806 "something other than TypeError");
807 PyErr_Clear();
808
809 out_s = PyLong_AsSsize_t(Py_None);
810 if (out_s != (Py_ssize_t)-1 || !PyErr_Occurred())
811 return raiseTestError("test_long_as_size_t",
812 "PyLong_AsSsize_t(None) didn't complain");
813 if (!PyErr_ExceptionMatches(PyExc_TypeError))
814 return raiseTestError("test_long_as_size_t",
815 "PyLong_AsSsize_t(None) raised "
816 "something other than TypeError");
817 PyErr_Clear();
818
819 /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
820 return Py_None;
821 }
822
823 /* Test the PyLong_AsDouble API. At present this just tests that
824 non-integer arguments are handled correctly.
825 */
826
827 static PyObject *
test_long_as_double(PyObject * self)828 test_long_as_double(PyObject *self)
829 {
830 double out;
831
832 Py_INCREF(Py_None);
833
834 out = PyLong_AsDouble(Py_None);
835 if (out != -1.0 || !PyErr_Occurred())
836 return raiseTestError("test_long_as_double",
837 "PyLong_AsDouble(None) didn't complain");
838 if (!PyErr_ExceptionMatches(PyExc_TypeError))
839 return raiseTestError("test_long_as_double",
840 "PyLong_AsDouble(None) raised "
841 "something other than TypeError");
842 PyErr_Clear();
843
844 /* Py_INCREF(Py_None) omitted - we already have a reference to it. */
845 return Py_None;
846 }
847
848 /* Test the L code for PyArg_ParseTuple. This should deliver a long long
849 for both long and int arguments. The test may leak a little memory if
850 it fails.
851 */
852 static PyObject *
test_L_code(PyObject * self)853 test_L_code(PyObject *self)
854 {
855 PyObject *tuple, *num;
856 long long value;
857
858 tuple = PyTuple_New(1);
859 if (tuple == NULL)
860 return NULL;
861
862 num = PyLong_FromLong(42);
863 if (num == NULL)
864 return NULL;
865
866 PyTuple_SET_ITEM(tuple, 0, num);
867
868 value = -1;
869 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
870 return NULL;
871 if (value != 42)
872 return raiseTestError("test_L_code",
873 "L code returned wrong value for long 42");
874
875 Py_DECREF(num);
876 num = PyLong_FromLong(42);
877 if (num == NULL)
878 return NULL;
879
880 PyTuple_SET_ITEM(tuple, 0, num);
881
882 value = -1;
883 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
884 return NULL;
885 if (value != 42)
886 return raiseTestError("test_L_code",
887 "L code returned wrong value for int 42");
888
889 Py_DECREF(tuple);
890 Py_INCREF(Py_None);
891 return Py_None;
892 }
893
894 static PyObject *
return_none(void * unused)895 return_none(void *unused)
896 {
897 Py_RETURN_NONE;
898 }
899
900 static PyObject *
raise_error(void * unused)901 raise_error(void *unused)
902 {
903 PyErr_SetNone(PyExc_ValueError);
904 return NULL;
905 }
906
907 static int
test_buildvalue_N_error(const char * fmt)908 test_buildvalue_N_error(const char *fmt)
909 {
910 PyObject *arg, *res;
911
912 arg = PyList_New(0);
913 if (arg == NULL) {
914 return -1;
915 }
916
917 Py_INCREF(arg);
918 res = Py_BuildValue(fmt, return_none, NULL, arg);
919 if (res == NULL) {
920 return -1;
921 }
922 Py_DECREF(res);
923 if (Py_REFCNT(arg) != 1) {
924 PyErr_Format(TestError, "test_buildvalue_N: "
925 "arg was not decrefed in successful "
926 "Py_BuildValue(\"%s\")", fmt);
927 return -1;
928 }
929
930 Py_INCREF(arg);
931 res = Py_BuildValue(fmt, raise_error, NULL, arg);
932 if (res != NULL || !PyErr_Occurred()) {
933 PyErr_Format(TestError, "test_buildvalue_N: "
934 "Py_BuildValue(\"%s\") didn't complain", fmt);
935 return -1;
936 }
937 PyErr_Clear();
938 if (Py_REFCNT(arg) != 1) {
939 PyErr_Format(TestError, "test_buildvalue_N: "
940 "arg was not decrefed in failed "
941 "Py_BuildValue(\"%s\")", fmt);
942 return -1;
943 }
944 Py_DECREF(arg);
945 return 0;
946 }
947
948 static PyObject *
test_buildvalue_N(PyObject * self,PyObject * noargs)949 test_buildvalue_N(PyObject *self, PyObject *noargs)
950 {
951 PyObject *arg, *res;
952
953 arg = PyList_New(0);
954 if (arg == NULL) {
955 return NULL;
956 }
957 Py_INCREF(arg);
958 res = Py_BuildValue("N", arg);
959 if (res == NULL) {
960 return NULL;
961 }
962 if (res != arg) {
963 return raiseTestError("test_buildvalue_N",
964 "Py_BuildValue(\"N\") returned wrong result");
965 }
966 if (Py_REFCNT(arg) != 2) {
967 return raiseTestError("test_buildvalue_N",
968 "arg was not decrefed in Py_BuildValue(\"N\")");
969 }
970 Py_DECREF(res);
971 Py_DECREF(arg);
972
973 if (test_buildvalue_N_error("O&N") < 0)
974 return NULL;
975 if (test_buildvalue_N_error("(O&N)") < 0)
976 return NULL;
977 if (test_buildvalue_N_error("[O&N]") < 0)
978 return NULL;
979 if (test_buildvalue_N_error("{O&N}") < 0)
980 return NULL;
981 if (test_buildvalue_N_error("{()O&(())N}") < 0)
982 return NULL;
983
984 Py_RETURN_NONE;
985 }
986
987
988 static PyObject *
get_args(PyObject * self,PyObject * args)989 get_args(PyObject *self, PyObject *args)
990 {
991 if (args == NULL) {
992 args = Py_None;
993 }
994 Py_INCREF(args);
995 return args;
996 }
997
998 static PyObject *
get_kwargs(PyObject * self,PyObject * args,PyObject * kwargs)999 get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
1000 {
1001 if (kwargs == NULL) {
1002 kwargs = Py_None;
1003 }
1004 Py_INCREF(kwargs);
1005 return kwargs;
1006 }
1007
1008 /* Test tuple argument processing */
1009 static PyObject *
getargs_tuple(PyObject * self,PyObject * args)1010 getargs_tuple(PyObject *self, PyObject *args)
1011 {
1012 int a, b, c;
1013 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
1014 return NULL;
1015 return Py_BuildValue("iii", a, b, c);
1016 }
1017
1018 /* test PyArg_ParseTupleAndKeywords */
1019 static PyObject *
getargs_keywords(PyObject * self,PyObject * args,PyObject * kwargs)1020 getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
1021 {
1022 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
1023 static const char fmt[] = "(ii)i|(i(ii))(iii)i";
1024 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
1025
1026 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
1027 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
1028 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
1029 return NULL;
1030 return Py_BuildValue("iiiiiiiiii",
1031 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
1032 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
1033 }
1034
1035 /* test PyArg_ParseTupleAndKeywords keyword-only arguments */
1036 static PyObject *
getargs_keyword_only(PyObject * self,PyObject * args,PyObject * kwargs)1037 getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
1038 {
1039 static char *keywords[] = {"required", "optional", "keyword_only", NULL};
1040 int required = -1;
1041 int optional = -1;
1042 int keyword_only = -1;
1043
1044 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
1045 &required, &optional, &keyword_only))
1046 return NULL;
1047 return Py_BuildValue("iii", required, optional, keyword_only);
1048 }
1049
1050 /* test PyArg_ParseTupleAndKeywords positional-only arguments */
1051 static PyObject *
getargs_positional_only_and_keywords(PyObject * self,PyObject * args,PyObject * kwargs)1052 getargs_positional_only_and_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
1053 {
1054 static char *keywords[] = {"", "", "keyword", NULL};
1055 int required = -1;
1056 int optional = -1;
1057 int keyword = -1;
1058
1059 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords,
1060 &required, &optional, &keyword))
1061 return NULL;
1062 return Py_BuildValue("iii", required, optional, keyword);
1063 }
1064
1065 /* Functions to call PyArg_ParseTuple with integer format codes,
1066 and return the result.
1067 */
1068 static PyObject *
getargs_b(PyObject * self,PyObject * args)1069 getargs_b(PyObject *self, PyObject *args)
1070 {
1071 unsigned char value;
1072 if (!PyArg_ParseTuple(args, "b", &value))
1073 return NULL;
1074 return PyLong_FromUnsignedLong((unsigned long)value);
1075 }
1076
1077 static PyObject *
getargs_B(PyObject * self,PyObject * args)1078 getargs_B(PyObject *self, PyObject *args)
1079 {
1080 unsigned char value;
1081 if (!PyArg_ParseTuple(args, "B", &value))
1082 return NULL;
1083 return PyLong_FromUnsignedLong((unsigned long)value);
1084 }
1085
1086 static PyObject *
getargs_h(PyObject * self,PyObject * args)1087 getargs_h(PyObject *self, PyObject *args)
1088 {
1089 short value;
1090 if (!PyArg_ParseTuple(args, "h", &value))
1091 return NULL;
1092 return PyLong_FromLong((long)value);
1093 }
1094
1095 static PyObject *
getargs_H(PyObject * self,PyObject * args)1096 getargs_H(PyObject *self, PyObject *args)
1097 {
1098 unsigned short value;
1099 if (!PyArg_ParseTuple(args, "H", &value))
1100 return NULL;
1101 return PyLong_FromUnsignedLong((unsigned long)value);
1102 }
1103
1104 static PyObject *
getargs_I(PyObject * self,PyObject * args)1105 getargs_I(PyObject *self, PyObject *args)
1106 {
1107 unsigned int value;
1108 if (!PyArg_ParseTuple(args, "I", &value))
1109 return NULL;
1110 return PyLong_FromUnsignedLong((unsigned long)value);
1111 }
1112
1113 static PyObject *
getargs_k(PyObject * self,PyObject * args)1114 getargs_k(PyObject *self, PyObject *args)
1115 {
1116 unsigned long value;
1117 if (!PyArg_ParseTuple(args, "k", &value))
1118 return NULL;
1119 return PyLong_FromUnsignedLong(value);
1120 }
1121
1122 static PyObject *
getargs_i(PyObject * self,PyObject * args)1123 getargs_i(PyObject *self, PyObject *args)
1124 {
1125 int value;
1126 if (!PyArg_ParseTuple(args, "i", &value))
1127 return NULL;
1128 return PyLong_FromLong((long)value);
1129 }
1130
1131 static PyObject *
getargs_l(PyObject * self,PyObject * args)1132 getargs_l(PyObject *self, PyObject *args)
1133 {
1134 long value;
1135 if (!PyArg_ParseTuple(args, "l", &value))
1136 return NULL;
1137 return PyLong_FromLong(value);
1138 }
1139
1140 static PyObject *
getargs_n(PyObject * self,PyObject * args)1141 getargs_n(PyObject *self, PyObject *args)
1142 {
1143 Py_ssize_t value;
1144 if (!PyArg_ParseTuple(args, "n", &value))
1145 return NULL;
1146 return PyLong_FromSsize_t(value);
1147 }
1148
1149 static PyObject *
getargs_p(PyObject * self,PyObject * args)1150 getargs_p(PyObject *self, PyObject *args)
1151 {
1152 int value;
1153 if (!PyArg_ParseTuple(args, "p", &value))
1154 return NULL;
1155 return PyLong_FromLong(value);
1156 }
1157
1158 static PyObject *
getargs_L(PyObject * self,PyObject * args)1159 getargs_L(PyObject *self, PyObject *args)
1160 {
1161 long long value;
1162 if (!PyArg_ParseTuple(args, "L", &value))
1163 return NULL;
1164 return PyLong_FromLongLong(value);
1165 }
1166
1167 static PyObject *
getargs_K(PyObject * self,PyObject * args)1168 getargs_K(PyObject *self, PyObject *args)
1169 {
1170 unsigned long long value;
1171 if (!PyArg_ParseTuple(args, "K", &value))
1172 return NULL;
1173 return PyLong_FromUnsignedLongLong(value);
1174 }
1175
1176 /* This function not only tests the 'k' getargs code, but also the
1177 PyLong_AsUnsignedLongMask() and PyLong_AsUnsignedLongMask() functions. */
1178 static PyObject *
test_k_code(PyObject * self)1179 test_k_code(PyObject *self)
1180 {
1181 PyObject *tuple, *num;
1182 unsigned long value;
1183
1184 tuple = PyTuple_New(1);
1185 if (tuple == NULL)
1186 return NULL;
1187
1188 /* a number larger than ULONG_MAX even on 64-bit platforms */
1189 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
1190 if (num == NULL)
1191 return NULL;
1192
1193 value = PyLong_AsUnsignedLongMask(num);
1194 if (value != ULONG_MAX)
1195 return raiseTestError("test_k_code",
1196 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
1197
1198 PyTuple_SET_ITEM(tuple, 0, num);
1199
1200 value = 0;
1201 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
1202 return NULL;
1203 if (value != ULONG_MAX)
1204 return raiseTestError("test_k_code",
1205 "k code returned wrong value for long 0xFFF...FFF");
1206
1207 Py_DECREF(num);
1208 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
1209 if (num == NULL)
1210 return NULL;
1211
1212 value = PyLong_AsUnsignedLongMask(num);
1213 if (value != (unsigned long)-0x42)
1214 return raiseTestError("test_k_code",
1215 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
1216
1217 PyTuple_SET_ITEM(tuple, 0, num);
1218
1219 value = 0;
1220 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
1221 return NULL;
1222 if (value != (unsigned long)-0x42)
1223 return raiseTestError("test_k_code",
1224 "k code returned wrong value for long -0xFFF..000042");
1225
1226 Py_DECREF(tuple);
1227 Py_INCREF(Py_None);
1228 return Py_None;
1229 }
1230
1231 static PyObject *
getargs_f(PyObject * self,PyObject * args)1232 getargs_f(PyObject *self, PyObject *args)
1233 {
1234 float f;
1235 if (!PyArg_ParseTuple(args, "f", &f))
1236 return NULL;
1237 return PyFloat_FromDouble(f);
1238 }
1239
1240 static PyObject *
getargs_d(PyObject * self,PyObject * args)1241 getargs_d(PyObject *self, PyObject *args)
1242 {
1243 double d;
1244 if (!PyArg_ParseTuple(args, "d", &d))
1245 return NULL;
1246 return PyFloat_FromDouble(d);
1247 }
1248
1249 static PyObject *
getargs_D(PyObject * self,PyObject * args)1250 getargs_D(PyObject *self, PyObject *args)
1251 {
1252 Py_complex cval;
1253 if (!PyArg_ParseTuple(args, "D", &cval))
1254 return NULL;
1255 return PyComplex_FromCComplex(cval);
1256 }
1257
1258 static PyObject *
getargs_S(PyObject * self,PyObject * args)1259 getargs_S(PyObject *self, PyObject *args)
1260 {
1261 PyObject *obj;
1262 if (!PyArg_ParseTuple(args, "S", &obj))
1263 return NULL;
1264 Py_INCREF(obj);
1265 return obj;
1266 }
1267
1268 static PyObject *
getargs_Y(PyObject * self,PyObject * args)1269 getargs_Y(PyObject *self, PyObject *args)
1270 {
1271 PyObject *obj;
1272 if (!PyArg_ParseTuple(args, "Y", &obj))
1273 return NULL;
1274 Py_INCREF(obj);
1275 return obj;
1276 }
1277
1278 static PyObject *
getargs_U(PyObject * self,PyObject * args)1279 getargs_U(PyObject *self, PyObject *args)
1280 {
1281 PyObject *obj;
1282 if (!PyArg_ParseTuple(args, "U", &obj))
1283 return NULL;
1284 Py_INCREF(obj);
1285 return obj;
1286 }
1287
1288 static PyObject *
getargs_c(PyObject * self,PyObject * args)1289 getargs_c(PyObject *self, PyObject *args)
1290 {
1291 char c;
1292 if (!PyArg_ParseTuple(args, "c", &c))
1293 return NULL;
1294 return PyLong_FromLong((unsigned char)c);
1295 }
1296
1297 static PyObject *
getargs_C(PyObject * self,PyObject * args)1298 getargs_C(PyObject *self, PyObject *args)
1299 {
1300 int c;
1301 if (!PyArg_ParseTuple(args, "C", &c))
1302 return NULL;
1303 return PyLong_FromLong(c);
1304 }
1305
1306 static PyObject *
getargs_s(PyObject * self,PyObject * args)1307 getargs_s(PyObject *self, PyObject *args)
1308 {
1309 char *str;
1310 if (!PyArg_ParseTuple(args, "s", &str))
1311 return NULL;
1312 return PyBytes_FromString(str);
1313 }
1314
1315 static PyObject *
getargs_s_star(PyObject * self,PyObject * args)1316 getargs_s_star(PyObject *self, PyObject *args)
1317 {
1318 Py_buffer buffer;
1319 PyObject *bytes;
1320 if (!PyArg_ParseTuple(args, "s*", &buffer))
1321 return NULL;
1322 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1323 PyBuffer_Release(&buffer);
1324 return bytes;
1325 }
1326
1327 static PyObject *
getargs_s_hash(PyObject * self,PyObject * args)1328 getargs_s_hash(PyObject *self, PyObject *args)
1329 {
1330 char *str;
1331 Py_ssize_t size;
1332 if (!PyArg_ParseTuple(args, "s#", &str, &size))
1333 return NULL;
1334 return PyBytes_FromStringAndSize(str, size);
1335 }
1336
1337 static PyObject *
getargs_z(PyObject * self,PyObject * args)1338 getargs_z(PyObject *self, PyObject *args)
1339 {
1340 char *str;
1341 if (!PyArg_ParseTuple(args, "z", &str))
1342 return NULL;
1343 if (str != NULL)
1344 return PyBytes_FromString(str);
1345 else
1346 Py_RETURN_NONE;
1347 }
1348
1349 static PyObject *
getargs_z_star(PyObject * self,PyObject * args)1350 getargs_z_star(PyObject *self, PyObject *args)
1351 {
1352 Py_buffer buffer;
1353 PyObject *bytes;
1354 if (!PyArg_ParseTuple(args, "z*", &buffer))
1355 return NULL;
1356 if (buffer.buf != NULL)
1357 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1358 else {
1359 Py_INCREF(Py_None);
1360 bytes = Py_None;
1361 }
1362 PyBuffer_Release(&buffer);
1363 return bytes;
1364 }
1365
1366 static PyObject *
getargs_z_hash(PyObject * self,PyObject * args)1367 getargs_z_hash(PyObject *self, PyObject *args)
1368 {
1369 char *str;
1370 Py_ssize_t size;
1371 if (!PyArg_ParseTuple(args, "z#", &str, &size))
1372 return NULL;
1373 if (str != NULL)
1374 return PyBytes_FromStringAndSize(str, size);
1375 else
1376 Py_RETURN_NONE;
1377 }
1378
1379 static PyObject *
getargs_y(PyObject * self,PyObject * args)1380 getargs_y(PyObject *self, PyObject *args)
1381 {
1382 char *str;
1383 if (!PyArg_ParseTuple(args, "y", &str))
1384 return NULL;
1385 return PyBytes_FromString(str);
1386 }
1387
1388 static PyObject *
getargs_y_star(PyObject * self,PyObject * args)1389 getargs_y_star(PyObject *self, PyObject *args)
1390 {
1391 Py_buffer buffer;
1392 PyObject *bytes;
1393 if (!PyArg_ParseTuple(args, "y*", &buffer))
1394 return NULL;
1395 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
1396 PyBuffer_Release(&buffer);
1397 return bytes;
1398 }
1399
1400 static PyObject *
getargs_y_hash(PyObject * self,PyObject * args)1401 getargs_y_hash(PyObject *self, PyObject *args)
1402 {
1403 char *str;
1404 Py_ssize_t size;
1405 if (!PyArg_ParseTuple(args, "y#", &str, &size))
1406 return NULL;
1407 return PyBytes_FromStringAndSize(str, size);
1408 }
1409
1410 static PyObject *
getargs_u(PyObject * self,PyObject * args)1411 getargs_u(PyObject *self, PyObject *args)
1412 {
1413 Py_UNICODE *str;
1414 Py_ssize_t size;
1415 if (!PyArg_ParseTuple(args, "u", &str))
1416 return NULL;
1417 size = Py_UNICODE_strlen(str);
1418 return PyUnicode_FromUnicode(str, size);
1419 }
1420
1421 static PyObject *
getargs_u_hash(PyObject * self,PyObject * args)1422 getargs_u_hash(PyObject *self, PyObject *args)
1423 {
1424 Py_UNICODE *str;
1425 Py_ssize_t size;
1426 if (!PyArg_ParseTuple(args, "u#", &str, &size))
1427 return NULL;
1428 return PyUnicode_FromUnicode(str, size);
1429 }
1430
1431 static PyObject *
getargs_Z(PyObject * self,PyObject * args)1432 getargs_Z(PyObject *self, PyObject *args)
1433 {
1434 Py_UNICODE *str;
1435 Py_ssize_t size;
1436 if (!PyArg_ParseTuple(args, "Z", &str))
1437 return NULL;
1438 if (str != NULL) {
1439 size = Py_UNICODE_strlen(str);
1440 return PyUnicode_FromUnicode(str, size);
1441 } else
1442 Py_RETURN_NONE;
1443 }
1444
1445 static PyObject *
getargs_Z_hash(PyObject * self,PyObject * args)1446 getargs_Z_hash(PyObject *self, PyObject *args)
1447 {
1448 Py_UNICODE *str;
1449 Py_ssize_t size;
1450 if (!PyArg_ParseTuple(args, "Z#", &str, &size))
1451 return NULL;
1452 if (str != NULL)
1453 return PyUnicode_FromUnicode(str, size);
1454 else
1455 Py_RETURN_NONE;
1456 }
1457
1458 static PyObject *
getargs_es(PyObject * self,PyObject * args)1459 getargs_es(PyObject *self, PyObject *args)
1460 {
1461 PyObject *arg, *result;
1462 const char *encoding = NULL;
1463 char *str;
1464
1465 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding))
1466 return NULL;
1467 if (!PyArg_Parse(arg, "es", encoding, &str))
1468 return NULL;
1469 result = PyBytes_FromString(str);
1470 PyMem_Free(str);
1471 return result;
1472 }
1473
1474 static PyObject *
getargs_et(PyObject * self,PyObject * args)1475 getargs_et(PyObject *self, PyObject *args)
1476 {
1477 PyObject *arg, *result;
1478 const char *encoding = NULL;
1479 char *str;
1480
1481 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding))
1482 return NULL;
1483 if (!PyArg_Parse(arg, "et", encoding, &str))
1484 return NULL;
1485 result = PyBytes_FromString(str);
1486 PyMem_Free(str);
1487 return result;
1488 }
1489
1490 static PyObject *
getargs_es_hash(PyObject * self,PyObject * args)1491 getargs_es_hash(PyObject *self, PyObject *args)
1492 {
1493 PyObject *arg, *result;
1494 const char *encoding = NULL;
1495 PyByteArrayObject *buffer = NULL;
1496 char *str = NULL;
1497 Py_ssize_t size;
1498
1499 if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer))
1500 return NULL;
1501 if (buffer != NULL) {
1502 str = PyByteArray_AS_STRING(buffer);
1503 size = PyByteArray_GET_SIZE(buffer);
1504 }
1505 if (!PyArg_Parse(arg, "es#", encoding, &str, &size))
1506 return NULL;
1507 result = PyBytes_FromStringAndSize(str, size);
1508 if (buffer == NULL)
1509 PyMem_Free(str);
1510 return result;
1511 }
1512
1513 static PyObject *
getargs_et_hash(PyObject * self,PyObject * args)1514 getargs_et_hash(PyObject *self, PyObject *args)
1515 {
1516 PyObject *arg, *result;
1517 const char *encoding = NULL;
1518 PyByteArrayObject *buffer = NULL;
1519 char *str = NULL;
1520 Py_ssize_t size;
1521
1522 if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer))
1523 return NULL;
1524 if (buffer != NULL) {
1525 str = PyByteArray_AS_STRING(buffer);
1526 size = PyByteArray_GET_SIZE(buffer);
1527 }
1528 if (!PyArg_Parse(arg, "et#", encoding, &str, &size))
1529 return NULL;
1530 result = PyBytes_FromStringAndSize(str, size);
1531 if (buffer == NULL)
1532 PyMem_Free(str);
1533 return result;
1534 }
1535
1536 /* Test the s and z codes for PyArg_ParseTuple.
1537 */
1538 static PyObject *
test_s_code(PyObject * self)1539 test_s_code(PyObject *self)
1540 {
1541 /* Unicode strings should be accepted */
1542 PyObject *tuple, *obj;
1543 char *value;
1544
1545 tuple = PyTuple_New(1);
1546 if (tuple == NULL)
1547 return NULL;
1548
1549 obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
1550 "latin-1", NULL);
1551 if (obj == NULL)
1552 return NULL;
1553
1554 PyTuple_SET_ITEM(tuple, 0, obj);
1555
1556 /* These two blocks used to raise a TypeError:
1557 * "argument must be string without null bytes, not str"
1558 */
1559 if (PyArg_ParseTuple(tuple, "s:test_s_code1", &value) < 0)
1560 return NULL;
1561
1562 if (PyArg_ParseTuple(tuple, "z:test_s_code2", &value) < 0)
1563 return NULL;
1564
1565 Py_DECREF(tuple);
1566 Py_RETURN_NONE;
1567 }
1568
1569 static PyObject *
parse_tuple_and_keywords(PyObject * self,PyObject * args)1570 parse_tuple_and_keywords(PyObject *self, PyObject *args)
1571 {
1572 PyObject *sub_args;
1573 PyObject *sub_kwargs;
1574 char *sub_format;
1575 PyObject *sub_keywords;
1576
1577 Py_ssize_t i, size;
1578 char *keywords[8 + 1]; /* space for NULL at end */
1579 PyObject *o;
1580 PyObject *converted[8];
1581
1582 int result;
1583 PyObject *return_value = NULL;
1584
1585 double buffers[8][4]; /* double ensures alignment where necessary */
1586
1587 if (!PyArg_ParseTuple(args, "OOyO:parse_tuple_and_keywords",
1588 &sub_args, &sub_kwargs,
1589 &sub_format, &sub_keywords))
1590 return NULL;
1591
1592 if (!(PyList_CheckExact(sub_keywords) || PyTuple_CheckExact(sub_keywords))) {
1593 PyErr_SetString(PyExc_ValueError,
1594 "parse_tuple_and_keywords: sub_keywords must be either list or tuple");
1595 return NULL;
1596 }
1597
1598 memset(buffers, 0, sizeof(buffers));
1599 memset(converted, 0, sizeof(converted));
1600 memset(keywords, 0, sizeof(keywords));
1601
1602 size = PySequence_Fast_GET_SIZE(sub_keywords);
1603 if (size > 8) {
1604 PyErr_SetString(PyExc_ValueError,
1605 "parse_tuple_and_keywords: too many keywords in sub_keywords");
1606 goto exit;
1607 }
1608
1609 for (i = 0; i < size; i++) {
1610 o = PySequence_Fast_GET_ITEM(sub_keywords, i);
1611 if (!PyUnicode_FSConverter(o, (void *)(converted + i))) {
1612 PyErr_Format(PyExc_ValueError,
1613 "parse_tuple_and_keywords: could not convert keywords[%zd] to narrow string", i);
1614 goto exit;
1615 }
1616 keywords[i] = PyBytes_AS_STRING(converted[i]);
1617 }
1618
1619 result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs,
1620 sub_format, keywords,
1621 buffers + 0, buffers + 1, buffers + 2, buffers + 3,
1622 buffers + 4, buffers + 5, buffers + 6, buffers + 7);
1623
1624 if (result) {
1625 return_value = Py_None;
1626 Py_INCREF(Py_None);
1627 }
1628
1629 exit:
1630 size = sizeof(converted) / sizeof(converted[0]);
1631 for (i = 0; i < size; i++) {
1632 Py_XDECREF(converted[i]);
1633 }
1634 return return_value;
1635 }
1636
1637 static volatile int x;
1638
1639 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
1640 of an error.
1641 */
1642 static PyObject *
test_u_code(PyObject * self)1643 test_u_code(PyObject *self)
1644 {
1645 PyObject *tuple, *obj;
1646 Py_UNICODE *value;
1647 Py_ssize_t len;
1648
1649 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
1650 /* Just use the macro and check that it compiles */
1651 x = Py_UNICODE_ISSPACE(25);
1652
1653 tuple = PyTuple_New(1);
1654 if (tuple == NULL)
1655 return NULL;
1656
1657 obj = PyUnicode_Decode("test", strlen("test"),
1658 "ascii", NULL);
1659 if (obj == NULL)
1660 return NULL;
1661
1662 PyTuple_SET_ITEM(tuple, 0, obj);
1663
1664 value = 0;
1665 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
1666 return NULL;
1667 if (value != PyUnicode_AS_UNICODE(obj))
1668 return raiseTestError("test_u_code",
1669 "u code returned wrong value for u'test'");
1670 value = 0;
1671 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
1672 return NULL;
1673 if (value != PyUnicode_AS_UNICODE(obj) ||
1674 len != PyUnicode_GET_SIZE(obj))
1675 return raiseTestError("test_u_code",
1676 "u# code returned wrong values for u'test'");
1677
1678 Py_DECREF(tuple);
1679 Py_INCREF(Py_None);
1680 return Py_None;
1681 }
1682
1683 /* Test Z and Z# codes for PyArg_ParseTuple */
1684 static PyObject *
test_Z_code(PyObject * self)1685 test_Z_code(PyObject *self)
1686 {
1687 PyObject *tuple, *obj;
1688 const Py_UNICODE *value1, *value2;
1689 Py_ssize_t len1, len2;
1690
1691 tuple = PyTuple_New(2);
1692 if (tuple == NULL)
1693 return NULL;
1694
1695 obj = PyUnicode_FromString("test");
1696 PyTuple_SET_ITEM(tuple, 0, obj);
1697 Py_INCREF(Py_None);
1698 PyTuple_SET_ITEM(tuple, 1, Py_None);
1699
1700 /* swap values on purpose */
1701 value1 = NULL;
1702 value2 = PyUnicode_AS_UNICODE(obj);
1703
1704 /* Test Z for both values */
1705 if (PyArg_ParseTuple(tuple, "ZZ:test_Z_code", &value1, &value2) < 0)
1706 return NULL;
1707 if (value1 != PyUnicode_AS_UNICODE(obj))
1708 return raiseTestError("test_Z_code",
1709 "Z code returned wrong value for 'test'");
1710 if (value2 != NULL)
1711 return raiseTestError("test_Z_code",
1712 "Z code returned wrong value for None");
1713
1714 value1 = NULL;
1715 value2 = PyUnicode_AS_UNICODE(obj);
1716 len1 = -1;
1717 len2 = -1;
1718
1719 /* Test Z# for both values */
1720 if (PyArg_ParseTuple(tuple, "Z#Z#:test_Z_code", &value1, &len1,
1721 &value2, &len2) < 0)
1722 return NULL;
1723 if (value1 != PyUnicode_AS_UNICODE(obj) ||
1724 len1 != PyUnicode_GET_SIZE(obj))
1725 return raiseTestError("test_Z_code",
1726 "Z# code returned wrong values for 'test'");
1727 if (value2 != NULL ||
1728 len2 != 0)
1729 return raiseTestError("test_Z_code",
1730 "Z# code returned wrong values for None'");
1731
1732 Py_DECREF(tuple);
1733 Py_RETURN_NONE;
1734 }
1735
1736 static PyObject *
test_widechar(PyObject * self)1737 test_widechar(PyObject *self)
1738 {
1739 #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
1740 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
1741 size_t wtextlen = 1;
1742 const wchar_t invalid[1] = {(wchar_t)0x110000u};
1743 #else
1744 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
1745 size_t wtextlen = 2;
1746 #endif
1747 PyObject *wide, *utf8;
1748
1749 wide = PyUnicode_FromWideChar(wtext, wtextlen);
1750 if (wide == NULL)
1751 return NULL;
1752
1753 utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
1754 if (utf8 == NULL) {
1755 Py_DECREF(wide);
1756 return NULL;
1757 }
1758
1759 if (PyUnicode_GET_LENGTH(wide) != PyUnicode_GET_LENGTH(utf8)) {
1760 Py_DECREF(wide);
1761 Py_DECREF(utf8);
1762 return raiseTestError("test_widechar",
1763 "wide string and utf8 string "
1764 "have different length");
1765 }
1766 if (PyUnicode_Compare(wide, utf8)) {
1767 Py_DECREF(wide);
1768 Py_DECREF(utf8);
1769 if (PyErr_Occurred())
1770 return NULL;
1771 return raiseTestError("test_widechar",
1772 "wide string and utf8 string "
1773 "are different");
1774 }
1775
1776 Py_DECREF(wide);
1777 Py_DECREF(utf8);
1778
1779 #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
1780 wide = PyUnicode_FromWideChar(invalid, 1);
1781 if (wide == NULL)
1782 PyErr_Clear();
1783 else
1784 return raiseTestError("test_widechar",
1785 "PyUnicode_FromWideChar(L\"\\U00110000\", 1) didn't fail");
1786
1787 wide = PyUnicode_FromUnicode(invalid, 1);
1788 if (wide == NULL)
1789 PyErr_Clear();
1790 else
1791 return raiseTestError("test_widechar",
1792 "PyUnicode_FromUnicode(L\"\\U00110000\", 1) didn't fail");
1793
1794 wide = PyUnicode_FromUnicode(NULL, 1);
1795 if (wide == NULL)
1796 return NULL;
1797 PyUnicode_AS_UNICODE(wide)[0] = invalid[0];
1798 if (_PyUnicode_Ready(wide) < 0) {
1799 Py_DECREF(wide);
1800 PyErr_Clear();
1801 }
1802 else {
1803 Py_DECREF(wide);
1804 return raiseTestError("test_widechar",
1805 "PyUnicode_Ready() didn't fail");
1806 }
1807 #endif
1808
1809 Py_RETURN_NONE;
1810 }
1811
1812 static PyObject *
unicode_aswidechar(PyObject * self,PyObject * args)1813 unicode_aswidechar(PyObject *self, PyObject *args)
1814 {
1815 PyObject *unicode, *result;
1816 Py_ssize_t buflen, size;
1817 wchar_t *buffer;
1818
1819 if (!PyArg_ParseTuple(args, "Un", &unicode, &buflen))
1820 return NULL;
1821 buffer = PyMem_New(wchar_t, buflen);
1822 if (buffer == NULL)
1823 return PyErr_NoMemory();
1824
1825 size = PyUnicode_AsWideChar(unicode, buffer, buflen);
1826 if (size == -1) {
1827 PyMem_Free(buffer);
1828 return NULL;
1829 }
1830
1831 if (size < buflen)
1832 buflen = size + 1;
1833 else
1834 buflen = size;
1835 result = PyUnicode_FromWideChar(buffer, buflen);
1836 PyMem_Free(buffer);
1837 if (result == NULL)
1838 return NULL;
1839
1840 return Py_BuildValue("(Nn)", result, size);
1841 }
1842
1843 static PyObject *
unicode_aswidecharstring(PyObject * self,PyObject * args)1844 unicode_aswidecharstring(PyObject *self, PyObject *args)
1845 {
1846 PyObject *unicode, *result;
1847 Py_ssize_t size;
1848 wchar_t *buffer;
1849
1850 if (!PyArg_ParseTuple(args, "U", &unicode))
1851 return NULL;
1852
1853 buffer = PyUnicode_AsWideCharString(unicode, &size);
1854 if (buffer == NULL)
1855 return NULL;
1856
1857 result = PyUnicode_FromWideChar(buffer, size + 1);
1858 PyMem_Free(buffer);
1859 if (result == NULL)
1860 return NULL;
1861 return Py_BuildValue("(Nn)", result, size);
1862 }
1863
1864 static PyObject *
unicode_asucs4(PyObject * self,PyObject * args)1865 unicode_asucs4(PyObject *self, PyObject *args)
1866 {
1867 PyObject *unicode, *result;
1868 Py_UCS4 *buffer;
1869 int copy_null;
1870 Py_ssize_t str_len, buf_len;
1871
1872 if (!PyArg_ParseTuple(args, "Unp:unicode_asucs4", &unicode, &str_len, ©_null)) {
1873 return NULL;
1874 }
1875
1876 buf_len = str_len + 1;
1877 buffer = PyMem_NEW(Py_UCS4, buf_len);
1878 if (buffer == NULL) {
1879 return PyErr_NoMemory();
1880 }
1881 memset(buffer, 0, sizeof(Py_UCS4)*buf_len);
1882 buffer[str_len] = 0xffffU;
1883
1884 if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) {
1885 PyMem_FREE(buffer);
1886 return NULL;
1887 }
1888
1889 result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len);
1890 PyMem_FREE(buffer);
1891 return result;
1892 }
1893
1894 static PyObject *
unicode_copycharacters(PyObject * self,PyObject * args)1895 unicode_copycharacters(PyObject *self, PyObject *args)
1896 {
1897 PyObject *from, *to, *to_copy;
1898 Py_ssize_t from_start, to_start, how_many, copied;
1899
1900 if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start,
1901 &from, &from_start, &how_many)) {
1902 return NULL;
1903 }
1904
1905 if (PyUnicode_READY(to) < 0) {
1906 return NULL;
1907 }
1908
1909 if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to),
1910 PyUnicode_MAX_CHAR_VALUE(to)))) {
1911 return NULL;
1912 }
1913 if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) {
1914 Py_DECREF(to_copy);
1915 return NULL;
1916 }
1917
1918 if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from,
1919 from_start, how_many)) < 0) {
1920 Py_DECREF(to_copy);
1921 return NULL;
1922 }
1923
1924 return Py_BuildValue("(Nn)", to_copy, copied);
1925 }
1926
1927 static PyObject *
unicode_encodedecimal(PyObject * self,PyObject * args)1928 unicode_encodedecimal(PyObject *self, PyObject *args)
1929 {
1930 Py_UNICODE *unicode;
1931 Py_ssize_t length;
1932 char *errors = NULL;
1933 PyObject *decimal;
1934 Py_ssize_t decimal_length, new_length;
1935 int res;
1936
1937 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
1938 return NULL;
1939
1940 decimal_length = length * 7; /* len('€') */
1941 decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
1942 if (decimal == NULL)
1943 return NULL;
1944
1945 res = PyUnicode_EncodeDecimal(unicode, length,
1946 PyBytes_AS_STRING(decimal),
1947 errors);
1948 if (res < 0) {
1949 Py_DECREF(decimal);
1950 return NULL;
1951 }
1952
1953 new_length = strlen(PyBytes_AS_STRING(decimal));
1954 assert(new_length <= decimal_length);
1955 res = _PyBytes_Resize(&decimal, new_length);
1956 if (res < 0)
1957 return NULL;
1958
1959 return decimal;
1960 }
1961
1962 static PyObject *
unicode_transformdecimaltoascii(PyObject * self,PyObject * args)1963 unicode_transformdecimaltoascii(PyObject *self, PyObject *args)
1964 {
1965 Py_UNICODE *unicode;
1966 Py_ssize_t length;
1967 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length))
1968 return NULL;
1969 return PyUnicode_TransformDecimalToASCII(unicode, length);
1970 }
1971
1972 static PyObject *
unicode_legacy_string(PyObject * self,PyObject * args)1973 unicode_legacy_string(PyObject *self, PyObject *args)
1974 {
1975 Py_UNICODE *data;
1976 Py_ssize_t len;
1977 PyObject *u;
1978
1979 if (!PyArg_ParseTuple(args, "u#", &data, &len))
1980 return NULL;
1981
1982 u = PyUnicode_FromUnicode(NULL, len);
1983 if (u == NULL)
1984 return NULL;
1985
1986 memcpy(PyUnicode_AS_UNICODE(u), data, len * sizeof(Py_UNICODE));
1987
1988 if (len > 0) { /* The empty string is always ready. */
1989 assert(!PyUnicode_IS_READY(u));
1990 }
1991
1992 return u;
1993 }
1994
1995 static PyObject *
getargs_w_star(PyObject * self,PyObject * args)1996 getargs_w_star(PyObject *self, PyObject *args)
1997 {
1998 Py_buffer buffer;
1999 PyObject *result;
2000 char *str;
2001
2002 if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))
2003 return NULL;
2004
2005 if (2 <= buffer.len) {
2006 str = buffer.buf;
2007 str[0] = '[';
2008 str[buffer.len-1] = ']';
2009 }
2010
2011 result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
2012 PyBuffer_Release(&buffer);
2013 return result;
2014 }
2015
2016
2017 static PyObject *
test_empty_argparse(PyObject * self)2018 test_empty_argparse(PyObject *self)
2019 {
2020 /* Test that formats can begin with '|'. See issue #4720. */
2021 PyObject *tuple, *dict = NULL;
2022 static char *kwlist[] = {NULL};
2023 int result;
2024 tuple = PyTuple_New(0);
2025 if (!tuple)
2026 return NULL;
2027 if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
2028 goto done;
2029 dict = PyDict_New();
2030 if (!dict)
2031 goto done;
2032 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
2033 done:
2034 Py_DECREF(tuple);
2035 Py_XDECREF(dict);
2036 if (result < 0)
2037 return NULL;
2038 else {
2039 Py_RETURN_NONE;
2040 }
2041 }
2042
2043 static PyObject *
codec_incrementalencoder(PyObject * self,PyObject * args)2044 codec_incrementalencoder(PyObject *self, PyObject *args)
2045 {
2046 const char *encoding, *errors = NULL;
2047 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
2048 &encoding, &errors))
2049 return NULL;
2050 return PyCodec_IncrementalEncoder(encoding, errors);
2051 }
2052
2053 static PyObject *
codec_incrementaldecoder(PyObject * self,PyObject * args)2054 codec_incrementaldecoder(PyObject *self, PyObject *args)
2055 {
2056 const char *encoding, *errors = NULL;
2057 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
2058 &encoding, &errors))
2059 return NULL;
2060 return PyCodec_IncrementalDecoder(encoding, errors);
2061 }
2062
2063
2064 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
2065 static PyObject *
test_long_numbits(PyObject * self)2066 test_long_numbits(PyObject *self)
2067 {
2068 struct triple {
2069 long input;
2070 size_t nbits;
2071 int sign;
2072 } testcases[] = {{0, 0, 0},
2073 {1L, 1, 1},
2074 {-1L, 1, -1},
2075 {2L, 2, 1},
2076 {-2L, 2, -1},
2077 {3L, 2, 1},
2078 {-3L, 2, -1},
2079 {4L, 3, 1},
2080 {-4L, 3, -1},
2081 {0x7fffL, 15, 1}, /* one Python int digit */
2082 {-0x7fffL, 15, -1},
2083 {0xffffL, 16, 1},
2084 {-0xffffL, 16, -1},
2085 {0xfffffffL, 28, 1},
2086 {-0xfffffffL, 28, -1}};
2087 size_t i;
2088
2089 for (i = 0; i < Py_ARRAY_LENGTH(testcases); ++i) {
2090 size_t nbits;
2091 int sign;
2092 PyObject *plong;
2093
2094 plong = PyLong_FromLong(testcases[i].input);
2095 if (plong == NULL)
2096 return NULL;
2097 nbits = _PyLong_NumBits(plong);
2098 sign = _PyLong_Sign(plong);
2099
2100 Py_DECREF(plong);
2101 if (nbits != testcases[i].nbits)
2102 return raiseTestError("test_long_numbits",
2103 "wrong result for _PyLong_NumBits");
2104 if (sign != testcases[i].sign)
2105 return raiseTestError("test_long_numbits",
2106 "wrong result for _PyLong_Sign");
2107 }
2108 Py_INCREF(Py_None);
2109 return Py_None;
2110 }
2111
2112 /* Example passing NULLs to PyObject_Str(NULL). */
2113
2114 static PyObject *
test_null_strings(PyObject * self)2115 test_null_strings(PyObject *self)
2116 {
2117 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Str(NULL);
2118 PyObject *tuple = PyTuple_Pack(2, o1, o2);
2119 Py_XDECREF(o1);
2120 Py_XDECREF(o2);
2121 return tuple;
2122 }
2123
2124 static PyObject *
raise_exception(PyObject * self,PyObject * args)2125 raise_exception(PyObject *self, PyObject *args)
2126 {
2127 PyObject *exc;
2128 PyObject *exc_args, *v;
2129 int num_args, i;
2130
2131 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
2132 &exc, &num_args))
2133 return NULL;
2134
2135 exc_args = PyTuple_New(num_args);
2136 if (exc_args == NULL)
2137 return NULL;
2138 for (i = 0; i < num_args; ++i) {
2139 v = PyLong_FromLong(i);
2140 if (v == NULL) {
2141 Py_DECREF(exc_args);
2142 return NULL;
2143 }
2144 PyTuple_SET_ITEM(exc_args, i, v);
2145 }
2146 PyErr_SetObject(exc, exc_args);
2147 Py_DECREF(exc_args);
2148 return NULL;
2149 }
2150
2151 static PyObject *
set_errno(PyObject * self,PyObject * args)2152 set_errno(PyObject *self, PyObject *args)
2153 {
2154 int new_errno;
2155
2156 if (!PyArg_ParseTuple(args, "i:set_errno", &new_errno))
2157 return NULL;
2158
2159 errno = new_errno;
2160 Py_RETURN_NONE;
2161 }
2162
2163 static PyObject *
test_set_exc_info(PyObject * self,PyObject * args)2164 test_set_exc_info(PyObject *self, PyObject *args)
2165 {
2166 PyObject *orig_exc;
2167 PyObject *new_type, *new_value, *new_tb;
2168 PyObject *type, *value, *tb;
2169 if (!PyArg_ParseTuple(args, "OOO:test_set_exc_info",
2170 &new_type, &new_value, &new_tb))
2171 return NULL;
2172
2173 PyErr_GetExcInfo(&type, &value, &tb);
2174
2175 Py_INCREF(new_type);
2176 Py_INCREF(new_value);
2177 Py_INCREF(new_tb);
2178 PyErr_SetExcInfo(new_type, new_value, new_tb);
2179
2180 orig_exc = PyTuple_Pack(3, type ? type : Py_None, value ? value : Py_None, tb ? tb : Py_None);
2181 Py_XDECREF(type);
2182 Py_XDECREF(value);
2183 Py_XDECREF(tb);
2184 return orig_exc;
2185 }
2186
2187 static int test_run_counter = 0;
2188
2189 static PyObject *
test_datetime_capi(PyObject * self,PyObject * args)2190 test_datetime_capi(PyObject *self, PyObject *args) {
2191 if (PyDateTimeAPI) {
2192 if (test_run_counter) {
2193 /* Probably regrtest.py -R */
2194 Py_RETURN_NONE;
2195 }
2196 else {
2197 PyErr_SetString(PyExc_AssertionError,
2198 "PyDateTime_CAPI somehow initialized");
2199 return NULL;
2200 }
2201 }
2202 test_run_counter++;
2203 PyDateTime_IMPORT;
2204 if (PyDateTimeAPI)
2205 Py_RETURN_NONE;
2206 else
2207 return NULL;
2208 }
2209
2210
2211 #ifdef WITH_THREAD
2212
2213 /* test_thread_state spawns a thread of its own, and that thread releases
2214 * `thread_done` when it's finished. The driver code has to know when the
2215 * thread finishes, because the thread uses a PyObject (the callable) that
2216 * may go away when the driver finishes. The former lack of this explicit
2217 * synchronization caused rare segfaults, so rare that they were seen only
2218 * on a Mac buildbot (although they were possible on any box).
2219 */
2220 static PyThread_type_lock thread_done = NULL;
2221
2222 static int
_make_call(void * callable)2223 _make_call(void *callable)
2224 {
2225 PyObject *rc;
2226 int success;
2227 PyGILState_STATE s = PyGILState_Ensure();
2228 rc = _PyObject_CallNoArg((PyObject *)callable);
2229 success = (rc != NULL);
2230 Py_XDECREF(rc);
2231 PyGILState_Release(s);
2232 return success;
2233 }
2234
2235 /* Same thing, but releases `thread_done` when it returns. This variant
2236 * should be called only from threads spawned by test_thread_state().
2237 */
2238 static void
_make_call_from_thread(void * callable)2239 _make_call_from_thread(void *callable)
2240 {
2241 _make_call(callable);
2242 PyThread_release_lock(thread_done);
2243 }
2244
2245 static PyObject *
test_thread_state(PyObject * self,PyObject * args)2246 test_thread_state(PyObject *self, PyObject *args)
2247 {
2248 PyObject *fn;
2249 int success = 1;
2250
2251 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
2252 return NULL;
2253
2254 if (!PyCallable_Check(fn)) {
2255 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
2256 fn->ob_type->tp_name);
2257 return NULL;
2258 }
2259
2260 /* Ensure Python is set up for threading */
2261 PyEval_InitThreads();
2262 thread_done = PyThread_allocate_lock();
2263 if (thread_done == NULL)
2264 return PyErr_NoMemory();
2265 PyThread_acquire_lock(thread_done, 1);
2266
2267 /* Start a new thread with our callback. */
2268 PyThread_start_new_thread(_make_call_from_thread, fn);
2269 /* Make the callback with the thread lock held by this thread */
2270 success &= _make_call(fn);
2271 /* Do it all again, but this time with the thread-lock released */
2272 Py_BEGIN_ALLOW_THREADS
2273 success &= _make_call(fn);
2274 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
2275 Py_END_ALLOW_THREADS
2276
2277 /* And once more with and without a thread
2278 XXX - should use a lock and work out exactly what we are trying
2279 to test <wink>
2280 */
2281 Py_BEGIN_ALLOW_THREADS
2282 PyThread_start_new_thread(_make_call_from_thread, fn);
2283 success &= _make_call(fn);
2284 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
2285 Py_END_ALLOW_THREADS
2286
2287 /* Release lock we acquired above. This is required on HP-UX. */
2288 PyThread_release_lock(thread_done);
2289
2290 PyThread_free_lock(thread_done);
2291 if (!success)
2292 return NULL;
2293 Py_RETURN_NONE;
2294 }
2295
2296 /* test Py_AddPendingCalls using threads */
_pending_callback(void * arg)2297 static int _pending_callback(void *arg)
2298 {
2299 /* we assume the argument is callable object to which we own a reference */
2300 PyObject *callable = (PyObject *)arg;
2301 PyObject *r = PyObject_CallObject(callable, NULL);
2302 Py_DECREF(callable);
2303 Py_XDECREF(r);
2304 return r != NULL ? 0 : -1;
2305 }
2306
2307 /* The following requests n callbacks to _pending_callback. It can be
2308 * run from any python thread.
2309 */
pending_threadfunc(PyObject * self,PyObject * arg)2310 PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
2311 {
2312 PyObject *callable;
2313 int r;
2314 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
2315 return NULL;
2316
2317 /* create the reference for the callbackwhile we hold the lock */
2318 Py_INCREF(callable);
2319
2320 Py_BEGIN_ALLOW_THREADS
2321 r = Py_AddPendingCall(&_pending_callback, callable);
2322 Py_END_ALLOW_THREADS
2323
2324 if (r<0) {
2325 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
2326 Py_INCREF(Py_False);
2327 return Py_False;
2328 }
2329 Py_INCREF(Py_True);
2330 return Py_True;
2331 }
2332 #endif
2333
2334 /* Some tests of PyUnicode_FromFormat(). This needs more tests. */
2335 static PyObject *
test_string_from_format(PyObject * self,PyObject * args)2336 test_string_from_format(PyObject *self, PyObject *args)
2337 {
2338 PyObject *result;
2339 char *msg;
2340
2341 #define CHECK_1_FORMAT(FORMAT, TYPE) \
2342 result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
2343 if (result == NULL) \
2344 return NULL; \
2345 if (!_PyUnicode_EqualToASCIIString(result, "1")) { \
2346 msg = FORMAT " failed at 1"; \
2347 goto Fail; \
2348 } \
2349 Py_DECREF(result)
2350
2351 CHECK_1_FORMAT("%d", int);
2352 CHECK_1_FORMAT("%ld", long);
2353 /* The z width modifier was added in Python 2.5. */
2354 CHECK_1_FORMAT("%zd", Py_ssize_t);
2355
2356 /* The u type code was added in Python 2.5. */
2357 CHECK_1_FORMAT("%u", unsigned int);
2358 CHECK_1_FORMAT("%lu", unsigned long);
2359 CHECK_1_FORMAT("%zu", size_t);
2360
2361 /* "%lld" and "%llu" support added in Python 2.7. */
2362 CHECK_1_FORMAT("%llu", unsigned long long);
2363 CHECK_1_FORMAT("%lld", long long);
2364
2365 Py_RETURN_NONE;
2366
2367 Fail:
2368 Py_XDECREF(result);
2369 return raiseTestError("test_string_from_format", msg);
2370
2371 #undef CHECK_1_FORMAT
2372 }
2373
2374
2375 static PyObject *
test_unicode_compare_with_ascii(PyObject * self)2376 test_unicode_compare_with_ascii(PyObject *self) {
2377 PyObject *py_s = PyUnicode_FromStringAndSize("str\0", 4);
2378 int result;
2379 if (py_s == NULL)
2380 return NULL;
2381 result = PyUnicode_CompareWithASCIIString(py_s, "str");
2382 Py_DECREF(py_s);
2383 if (!result) {
2384 PyErr_SetString(TestError, "Python string ending in NULL "
2385 "should not compare equal to c string.");
2386 return NULL;
2387 }
2388 Py_RETURN_NONE;
2389 }
2390
2391 /* This is here to provide a docstring for test_descr. */
2392 static PyObject *
test_with_docstring(PyObject * self)2393 test_with_docstring(PyObject *self)
2394 {
2395 Py_RETURN_NONE;
2396 }
2397
2398 /* Test PyOS_string_to_double. */
2399 static PyObject *
test_string_to_double(PyObject * self)2400 test_string_to_double(PyObject *self) {
2401 double result;
2402 char *msg;
2403
2404 #define CHECK_STRING(STR, expected) \
2405 result = PyOS_string_to_double(STR, NULL, NULL); \
2406 if (result == -1.0 && PyErr_Occurred()) \
2407 return NULL; \
2408 if (result != (double)expected) { \
2409 msg = "conversion of " STR " to float failed"; \
2410 goto fail; \
2411 }
2412
2413 #define CHECK_INVALID(STR) \
2414 result = PyOS_string_to_double(STR, NULL, NULL); \
2415 if (result == -1.0 && PyErr_Occurred()) { \
2416 if (PyErr_ExceptionMatches(PyExc_ValueError)) \
2417 PyErr_Clear(); \
2418 else \
2419 return NULL; \
2420 } \
2421 else { \
2422 msg = "conversion of " STR " didn't raise ValueError"; \
2423 goto fail; \
2424 }
2425
2426 CHECK_STRING("0.1", 0.1);
2427 CHECK_STRING("1.234", 1.234);
2428 CHECK_STRING("-1.35", -1.35);
2429 CHECK_STRING(".1e01", 1.0);
2430 CHECK_STRING("2.e-2", 0.02);
2431
2432 CHECK_INVALID(" 0.1");
2433 CHECK_INVALID("\t\n-3");
2434 CHECK_INVALID(".123 ");
2435 CHECK_INVALID("3\n");
2436 CHECK_INVALID("123abc");
2437
2438 Py_RETURN_NONE;
2439 fail:
2440 return raiseTestError("test_string_to_double", msg);
2441 #undef CHECK_STRING
2442 #undef CHECK_INVALID
2443 }
2444
2445
2446 /* Coverage testing of capsule objects. */
2447
2448 static const char *capsule_name = "capsule name";
2449 static char *capsule_pointer = "capsule pointer";
2450 static char *capsule_context = "capsule context";
2451 static const char *capsule_error = NULL;
2452 static int
2453 capsule_destructor_call_count = 0;
2454
2455 static void
capsule_destructor(PyObject * o)2456 capsule_destructor(PyObject *o) {
2457 capsule_destructor_call_count++;
2458 if (PyCapsule_GetContext(o) != capsule_context) {
2459 capsule_error = "context did not match in destructor!";
2460 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
2461 capsule_error = "destructor did not match in destructor! (woah!)";
2462 } else if (PyCapsule_GetName(o) != capsule_name) {
2463 capsule_error = "name did not match in destructor!";
2464 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
2465 capsule_error = "pointer did not match in destructor!";
2466 }
2467 }
2468
2469 typedef struct {
2470 char *name;
2471 char *module;
2472 char *attribute;
2473 } known_capsule;
2474
2475 static PyObject *
test_capsule(PyObject * self,PyObject * args)2476 test_capsule(PyObject *self, PyObject *args)
2477 {
2478 PyObject *object;
2479 const char *error = NULL;
2480 void *pointer;
2481 void *pointer2;
2482 known_capsule known_capsules[] = {
2483 #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
2484 KNOWN_CAPSULE("_socket", "CAPI"),
2485 KNOWN_CAPSULE("_curses", "_C_API"),
2486 KNOWN_CAPSULE("datetime", "datetime_CAPI"),
2487 { NULL, NULL },
2488 };
2489 known_capsule *known = &known_capsules[0];
2490
2491 #define FAIL(x) { error = (x); goto exit; }
2492
2493 #define CHECK_DESTRUCTOR \
2494 if (capsule_error) { \
2495 FAIL(capsule_error); \
2496 } \
2497 else if (!capsule_destructor_call_count) { \
2498 FAIL("destructor not called!"); \
2499 } \
2500 capsule_destructor_call_count = 0; \
2501
2502 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
2503 PyCapsule_SetContext(object, capsule_context);
2504 capsule_destructor(object);
2505 CHECK_DESTRUCTOR;
2506 Py_DECREF(object);
2507 CHECK_DESTRUCTOR;
2508
2509 object = PyCapsule_New(known, "ignored", NULL);
2510 PyCapsule_SetPointer(object, capsule_pointer);
2511 PyCapsule_SetName(object, capsule_name);
2512 PyCapsule_SetDestructor(object, capsule_destructor);
2513 PyCapsule_SetContext(object, capsule_context);
2514 capsule_destructor(object);
2515 CHECK_DESTRUCTOR;
2516 /* intentionally access using the wrong name */
2517 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
2518 if (!PyErr_Occurred()) {
2519 FAIL("PyCapsule_GetPointer should have failed but did not!");
2520 }
2521 PyErr_Clear();
2522 if (pointer2) {
2523 if (pointer2 == capsule_pointer) {
2524 FAIL("PyCapsule_GetPointer should not have"
2525 " returned the internal pointer!");
2526 } else {
2527 FAIL("PyCapsule_GetPointer should have "
2528 "returned NULL pointer but did not!");
2529 }
2530 }
2531 PyCapsule_SetDestructor(object, NULL);
2532 Py_DECREF(object);
2533 if (capsule_destructor_call_count) {
2534 FAIL("destructor called when it should not have been!");
2535 }
2536
2537 for (known = &known_capsules[0]; known->module != NULL; known++) {
2538 /* yeah, ordinarily I wouldn't do this either,
2539 but it's fine for this test harness.
2540 */
2541 static char buffer[256];
2542 #undef FAIL
2543 #define FAIL(x) \
2544 { \
2545 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
2546 x, known->module, known->attribute); \
2547 error = buffer; \
2548 goto exit; \
2549 } \
2550
2551 PyObject *module = PyImport_ImportModule(known->module);
2552 if (module) {
2553 pointer = PyCapsule_Import(known->name, 0);
2554 if (!pointer) {
2555 Py_DECREF(module);
2556 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
2557 }
2558 object = PyObject_GetAttrString(module, known->attribute);
2559 if (!object) {
2560 Py_DECREF(module);
2561 return NULL;
2562 }
2563 pointer2 = PyCapsule_GetPointer(object,
2564 "weebles wobble but they don't fall down");
2565 if (!PyErr_Occurred()) {
2566 Py_DECREF(object);
2567 Py_DECREF(module);
2568 FAIL("PyCapsule_GetPointer should have failed but did not!");
2569 }
2570 PyErr_Clear();
2571 if (pointer2) {
2572 Py_DECREF(module);
2573 Py_DECREF(object);
2574 if (pointer2 == pointer) {
2575 FAIL("PyCapsule_GetPointer should not have"
2576 " returned its internal pointer!");
2577 } else {
2578 FAIL("PyCapsule_GetPointer should have"
2579 " returned NULL pointer but did not!");
2580 }
2581 }
2582 Py_DECREF(object);
2583 Py_DECREF(module);
2584 }
2585 else
2586 PyErr_Clear();
2587 }
2588
2589 exit:
2590 if (error) {
2591 return raiseTestError("test_capsule", error);
2592 }
2593 Py_RETURN_NONE;
2594 #undef FAIL
2595 }
2596
2597 #ifdef HAVE_GETTIMEOFDAY
2598 /* Profiling of integer performance */
print_delta(int test,struct timeval * s,struct timeval * e)2599 static void print_delta(int test, struct timeval *s, struct timeval *e)
2600 {
2601 e->tv_sec -= s->tv_sec;
2602 e->tv_usec -= s->tv_usec;
2603 if (e->tv_usec < 0) {
2604 e->tv_sec -=1;
2605 e->tv_usec += 1000000;
2606 }
2607 printf("Test %d: %d.%06ds\n", test, (int)e->tv_sec, (int)e->tv_usec);
2608 }
2609
2610 static PyObject *
profile_int(PyObject * self,PyObject * args)2611 profile_int(PyObject *self, PyObject* args)
2612 {
2613 int i, k;
2614 struct timeval start, stop;
2615 PyObject *single, **multiple, *op1, *result;
2616
2617 /* Test 1: Allocate and immediately deallocate
2618 many small integers */
2619 gettimeofday(&start, NULL);
2620 for(k=0; k < 20000; k++)
2621 for(i=0; i < 1000; i++) {
2622 single = PyLong_FromLong(i);
2623 Py_DECREF(single);
2624 }
2625 gettimeofday(&stop, NULL);
2626 print_delta(1, &start, &stop);
2627
2628 /* Test 2: Allocate and immediately deallocate
2629 many large integers */
2630 gettimeofday(&start, NULL);
2631 for(k=0; k < 20000; k++)
2632 for(i=0; i < 1000; i++) {
2633 single = PyLong_FromLong(i+1000000);
2634 Py_DECREF(single);
2635 }
2636 gettimeofday(&stop, NULL);
2637 print_delta(2, &start, &stop);
2638
2639 /* Test 3: Allocate a few integers, then release
2640 them all simultaneously. */
2641 multiple = malloc(sizeof(PyObject*) * 1000);
2642 if (multiple == NULL)
2643 return PyErr_NoMemory();
2644 gettimeofday(&start, NULL);
2645 for(k=0; k < 20000; k++) {
2646 for(i=0; i < 1000; i++) {
2647 multiple[i] = PyLong_FromLong(i+1000000);
2648 }
2649 for(i=0; i < 1000; i++) {
2650 Py_DECREF(multiple[i]);
2651 }
2652 }
2653 gettimeofday(&stop, NULL);
2654 print_delta(3, &start, &stop);
2655 free(multiple);
2656
2657 /* Test 4: Allocate many integers, then release
2658 them all simultaneously. */
2659 multiple = malloc(sizeof(PyObject*) * 1000000);
2660 if (multiple == NULL)
2661 return PyErr_NoMemory();
2662 gettimeofday(&start, NULL);
2663 for(k=0; k < 20; k++) {
2664 for(i=0; i < 1000000; i++) {
2665 multiple[i] = PyLong_FromLong(i+1000000);
2666 }
2667 for(i=0; i < 1000000; i++) {
2668 Py_DECREF(multiple[i]);
2669 }
2670 }
2671 gettimeofday(&stop, NULL);
2672 print_delta(4, &start, &stop);
2673 free(multiple);
2674
2675 /* Test 5: Allocate many integers < 32000 */
2676 multiple = malloc(sizeof(PyObject*) * 1000000);
2677 if (multiple == NULL)
2678 return PyErr_NoMemory();
2679 gettimeofday(&start, NULL);
2680 for(k=0; k < 10; k++) {
2681 for(i=0; i < 1000000; i++) {
2682 multiple[i] = PyLong_FromLong(i+1000);
2683 }
2684 for(i=0; i < 1000000; i++) {
2685 Py_DECREF(multiple[i]);
2686 }
2687 }
2688 gettimeofday(&stop, NULL);
2689 print_delta(5, &start, &stop);
2690 free(multiple);
2691
2692 /* Test 6: Perform small int addition */
2693 op1 = PyLong_FromLong(1);
2694 gettimeofday(&start, NULL);
2695 for(i=0; i < 10000000; i++) {
2696 result = PyNumber_Add(op1, op1);
2697 Py_DECREF(result);
2698 }
2699 gettimeofday(&stop, NULL);
2700 Py_DECREF(op1);
2701 print_delta(6, &start, &stop);
2702
2703 /* Test 7: Perform medium int addition */
2704 op1 = PyLong_FromLong(1000);
2705 if (op1 == NULL)
2706 return NULL;
2707 gettimeofday(&start, NULL);
2708 for(i=0; i < 10000000; i++) {
2709 result = PyNumber_Add(op1, op1);
2710 Py_XDECREF(result);
2711 }
2712 gettimeofday(&stop, NULL);
2713 Py_DECREF(op1);
2714 print_delta(7, &start, &stop);
2715
2716 Py_INCREF(Py_None);
2717 return Py_None;
2718 }
2719 #endif
2720
2721 /* To test the format of tracebacks as printed out. */
2722 static PyObject *
traceback_print(PyObject * self,PyObject * args)2723 traceback_print(PyObject *self, PyObject *args)
2724 {
2725 PyObject *file;
2726 PyObject *traceback;
2727 int result;
2728
2729 if (!PyArg_ParseTuple(args, "OO:traceback_print",
2730 &traceback, &file))
2731 return NULL;
2732
2733 result = PyTraceBack_Print(traceback, file);
2734 if (result < 0)
2735 return NULL;
2736 Py_RETURN_NONE;
2737 }
2738
2739 /* To test the format of exceptions as printed out. */
2740 static PyObject *
exception_print(PyObject * self,PyObject * args)2741 exception_print(PyObject *self, PyObject *args)
2742 {
2743 PyObject *value;
2744 PyObject *tb;
2745
2746 if (!PyArg_ParseTuple(args, "O:exception_print",
2747 &value))
2748 return NULL;
2749 if (!PyExceptionInstance_Check(value)) {
2750 PyErr_Format(PyExc_TypeError, "an exception instance is required");
2751 return NULL;
2752 }
2753
2754 tb = PyException_GetTraceback(value);
2755 PyErr_Display((PyObject *) Py_TYPE(value), value, tb);
2756 Py_XDECREF(tb);
2757
2758 Py_RETURN_NONE;
2759 }
2760
2761
2762
2763
2764 /* reliably raise a MemoryError */
2765 static PyObject *
raise_memoryerror(PyObject * self)2766 raise_memoryerror(PyObject *self)
2767 {
2768 PyErr_NoMemory();
2769 return NULL;
2770 }
2771
2772 /* Issue 6012 */
2773 static PyObject *str1, *str2;
2774 static int
failing_converter(PyObject * obj,void * arg)2775 failing_converter(PyObject *obj, void *arg)
2776 {
2777 /* Clone str1, then let the conversion fail. */
2778 assert(str1);
2779 str2 = str1;
2780 Py_INCREF(str2);
2781 return 0;
2782 }
2783 static PyObject*
argparsing(PyObject * o,PyObject * args)2784 argparsing(PyObject *o, PyObject *args)
2785 {
2786 PyObject *res;
2787 str1 = str2 = NULL;
2788 if (!PyArg_ParseTuple(args, "O&O&",
2789 PyUnicode_FSConverter, &str1,
2790 failing_converter, &str2)) {
2791 if (!str2)
2792 /* argument converter not called? */
2793 return NULL;
2794 /* Should be 1 */
2795 res = PyLong_FromSsize_t(Py_REFCNT(str2));
2796 Py_DECREF(str2);
2797 PyErr_Clear();
2798 return res;
2799 }
2800 Py_RETURN_NONE;
2801 }
2802
2803 /* To test that the result of PyCode_NewEmpty has the right members. */
2804 static PyObject *
code_newempty(PyObject * self,PyObject * args)2805 code_newempty(PyObject *self, PyObject *args)
2806 {
2807 const char *filename;
2808 const char *funcname;
2809 int firstlineno;
2810
2811 if (!PyArg_ParseTuple(args, "ssi:code_newempty",
2812 &filename, &funcname, &firstlineno))
2813 return NULL;
2814
2815 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
2816 }
2817
2818 /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
2819 Run via Lib/test/test_exceptions.py */
2820 static PyObject *
make_exception_with_doc(PyObject * self,PyObject * args,PyObject * kwargs)2821 make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
2822 {
2823 const char *name;
2824 const char *doc = NULL;
2825 PyObject *base = NULL;
2826 PyObject *dict = NULL;
2827
2828 static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
2829
2830 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
2831 "s|sOO:make_exception_with_doc", kwlist,
2832 &name, &doc, &base, &dict))
2833 return NULL;
2834
2835 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
2836 }
2837
2838 static PyObject *
make_memoryview_from_NULL_pointer(PyObject * self)2839 make_memoryview_from_NULL_pointer(PyObject *self)
2840 {
2841 Py_buffer info;
2842 if (PyBuffer_FillInfo(&info, NULL, NULL, 1, 1, PyBUF_FULL_RO) < 0)
2843 return NULL;
2844 return PyMemoryView_FromBuffer(&info);
2845 }
2846
2847 static PyObject *
test_from_contiguous(PyObject * self,PyObject * noargs)2848 test_from_contiguous(PyObject* self, PyObject *noargs)
2849 {
2850 int data[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
2851 int init[5] = {0, 1, 2, 3, 4};
2852 Py_ssize_t itemsize = sizeof(int);
2853 Py_ssize_t shape = 5;
2854 Py_ssize_t strides = 2 * itemsize;
2855 Py_buffer view = {
2856 data,
2857 NULL,
2858 5 * itemsize,
2859 itemsize,
2860 1,
2861 1,
2862 NULL,
2863 &shape,
2864 &strides,
2865 NULL,
2866 NULL
2867 };
2868 int *ptr;
2869 int i;
2870
2871 PyBuffer_FromContiguous(&view, init, view.len, 'C');
2872 ptr = view.buf;
2873 for (i = 0; i < 5; i++) {
2874 if (ptr[2*i] != i) {
2875 PyErr_SetString(TestError,
2876 "test_from_contiguous: incorrect result");
2877 return NULL;
2878 }
2879 }
2880
2881 view.buf = &data[8];
2882 view.strides[0] = -2 * itemsize;
2883
2884 PyBuffer_FromContiguous(&view, init, view.len, 'C');
2885 ptr = view.buf;
2886 for (i = 0; i < 5; i++) {
2887 if (*(ptr-2*i) != i) {
2888 PyErr_SetString(TestError,
2889 "test_from_contiguous: incorrect result");
2890 return NULL;
2891 }
2892 }
2893
2894 Py_RETURN_NONE;
2895 }
2896
2897 #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
2898 extern PyTypeObject _PyBytesIOBuffer_Type;
2899
2900 static PyObject *
test_pep3118_obsolete_write_locks(PyObject * self,PyObject * noargs)2901 test_pep3118_obsolete_write_locks(PyObject* self, PyObject *noargs)
2902 {
2903 PyTypeObject *type = &_PyBytesIOBuffer_Type;
2904 PyObject *b;
2905 char *dummy[1];
2906 int ret, match;
2907
2908 /* PyBuffer_FillInfo() */
2909 ret = PyBuffer_FillInfo(NULL, NULL, dummy, 1, 0, PyBUF_SIMPLE);
2910 match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
2911 PyErr_Clear();
2912 if (ret != -1 || match == 0)
2913 goto error;
2914
2915 /* bytesiobuf_getbuffer() */
2916 b = type->tp_alloc(type, 0);
2917 if (b == NULL) {
2918 return NULL;
2919 }
2920
2921 ret = PyObject_GetBuffer(b, NULL, PyBUF_SIMPLE);
2922 Py_DECREF(b);
2923 match = PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_BufferError);
2924 PyErr_Clear();
2925 if (ret != -1 || match == 0)
2926 goto error;
2927
2928 Py_RETURN_NONE;
2929
2930 error:
2931 PyErr_SetString(TestError,
2932 "test_pep3118_obsolete_write_locks: failure");
2933 return NULL;
2934 }
2935 #endif
2936
2937 /* This tests functions that historically supported write locks. It is
2938 wrong to call getbuffer() with view==NULL and a compliant getbufferproc
2939 is entitled to segfault in that case. */
2940 static PyObject *
getbuffer_with_null_view(PyObject * self,PyObject * obj)2941 getbuffer_with_null_view(PyObject* self, PyObject *obj)
2942 {
2943 if (PyObject_GetBuffer(obj, NULL, PyBUF_SIMPLE) < 0)
2944 return NULL;
2945
2946 Py_RETURN_NONE;
2947 }
2948
2949 /* Test that the fatal error from not having a current thread doesn't
2950 cause an infinite loop. Run via Lib/test/test_capi.py */
2951 static PyObject *
crash_no_current_thread(PyObject * self)2952 crash_no_current_thread(PyObject *self)
2953 {
2954 Py_BEGIN_ALLOW_THREADS
2955 /* Using PyThreadState_Get() directly allows the test to pass in
2956 !pydebug mode. However, the test only actually tests anything
2957 in pydebug mode, since that's where the infinite loop was in
2958 the first place. */
2959 PyThreadState_Get();
2960 Py_END_ALLOW_THREADS
2961 return NULL;
2962 }
2963
2964 /* To run some code in a sub-interpreter. */
2965 static PyObject *
run_in_subinterp(PyObject * self,PyObject * args)2966 run_in_subinterp(PyObject *self, PyObject *args)
2967 {
2968 const char *code;
2969 int r;
2970 PyThreadState *substate, *mainstate;
2971
2972 if (!PyArg_ParseTuple(args, "s:run_in_subinterp",
2973 &code))
2974 return NULL;
2975
2976 mainstate = PyThreadState_Get();
2977
2978 PyThreadState_Swap(NULL);
2979
2980 substate = Py_NewInterpreter();
2981 if (substate == NULL) {
2982 /* Since no new thread state was created, there is no exception to
2983 propagate; raise a fresh one after swapping in the old thread
2984 state. */
2985 PyThreadState_Swap(mainstate);
2986 PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
2987 return NULL;
2988 }
2989 r = PyRun_SimpleString(code);
2990 Py_EndInterpreter(substate);
2991
2992 PyThreadState_Swap(mainstate);
2993
2994 return PyLong_FromLong(r);
2995 }
2996
2997 static int
check_time_rounding(int round)2998 check_time_rounding(int round)
2999 {
3000 if (round != _PyTime_ROUND_FLOOR
3001 && round != _PyTime_ROUND_CEILING
3002 && round != _PyTime_ROUND_HALF_EVEN) {
3003 PyErr_SetString(PyExc_ValueError, "invalid rounding");
3004 return -1;
3005 }
3006 return 0;
3007 }
3008
3009 static PyObject *
test_pytime_object_to_time_t(PyObject * self,PyObject * args)3010 test_pytime_object_to_time_t(PyObject *self, PyObject *args)
3011 {
3012 PyObject *obj;
3013 time_t sec;
3014 int round;
3015 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round))
3016 return NULL;
3017 if (check_time_rounding(round) < 0)
3018 return NULL;
3019 if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1)
3020 return NULL;
3021 return _PyLong_FromTime_t(sec);
3022 }
3023
3024 static PyObject *
test_pytime_object_to_timeval(PyObject * self,PyObject * args)3025 test_pytime_object_to_timeval(PyObject *self, PyObject *args)
3026 {
3027 PyObject *obj;
3028 time_t sec;
3029 long usec;
3030 int round;
3031 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round))
3032 return NULL;
3033 if (check_time_rounding(round) < 0)
3034 return NULL;
3035 if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1)
3036 return NULL;
3037 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
3038 }
3039
3040 static PyObject *
test_pytime_object_to_timespec(PyObject * self,PyObject * args)3041 test_pytime_object_to_timespec(PyObject *self, PyObject *args)
3042 {
3043 PyObject *obj;
3044 time_t sec;
3045 long nsec;
3046 int round;
3047 if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round))
3048 return NULL;
3049 if (check_time_rounding(round) < 0)
3050 return NULL;
3051 if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1)
3052 return NULL;
3053 return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
3054 }
3055
3056 static void
slot_tp_del(PyObject * self)3057 slot_tp_del(PyObject *self)
3058 {
3059 _Py_IDENTIFIER(__tp_del__);
3060 PyObject *del, *res;
3061 PyObject *error_type, *error_value, *error_traceback;
3062
3063 /* Temporarily resurrect the object. */
3064 assert(self->ob_refcnt == 0);
3065 self->ob_refcnt = 1;
3066
3067 /* Save the current exception, if any. */
3068 PyErr_Fetch(&error_type, &error_value, &error_traceback);
3069
3070 /* Execute __del__ method, if any. */
3071 del = _PyObject_LookupSpecial(self, &PyId___tp_del__);
3072 if (del != NULL) {
3073 res = PyEval_CallObject(del, NULL);
3074 if (res == NULL)
3075 PyErr_WriteUnraisable(del);
3076 else
3077 Py_DECREF(res);
3078 Py_DECREF(del);
3079 }
3080
3081 /* Restore the saved exception. */
3082 PyErr_Restore(error_type, error_value, error_traceback);
3083
3084 /* Undo the temporary resurrection; can't use DECREF here, it would
3085 * cause a recursive call.
3086 */
3087 assert(self->ob_refcnt > 0);
3088 if (--self->ob_refcnt == 0)
3089 return; /* this is the normal path out */
3090
3091 /* __del__ resurrected it! Make it look like the original Py_DECREF
3092 * never happened.
3093 */
3094 {
3095 Py_ssize_t refcnt = self->ob_refcnt;
3096 _Py_NewReference(self);
3097 self->ob_refcnt = refcnt;
3098 }
3099 assert(!PyType_IS_GC(Py_TYPE(self)) ||
3100 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
3101 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
3102 * we need to undo that. */
3103 _Py_DEC_REFTOTAL;
3104 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
3105 * chain, so no more to do there.
3106 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
3107 * _Py_NewReference bumped tp_allocs: both of those need to be
3108 * undone.
3109 */
3110 #ifdef COUNT_ALLOCS
3111 --Py_TYPE(self)->tp_frees;
3112 --Py_TYPE(self)->tp_allocs;
3113 #endif
3114 }
3115
3116 static PyObject *
with_tp_del(PyObject * self,PyObject * args)3117 with_tp_del(PyObject *self, PyObject *args)
3118 {
3119 PyObject *obj;
3120 PyTypeObject *tp;
3121
3122 if (!PyArg_ParseTuple(args, "O:with_tp_del", &obj))
3123 return NULL;
3124 tp = (PyTypeObject *) obj;
3125 if (!PyType_Check(obj) || !PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)) {
3126 PyErr_Format(PyExc_TypeError,
3127 "heap type expected, got %R", obj);
3128 return NULL;
3129 }
3130 tp->tp_del = slot_tp_del;
3131 Py_INCREF(obj);
3132 return obj;
3133 }
3134
3135 static PyMethodDef ml;
3136
3137 static PyObject *
create_cfunction(PyObject * self,PyObject * args)3138 create_cfunction(PyObject *self, PyObject *args)
3139 {
3140 return PyCFunction_NewEx(&ml, self, NULL);
3141 }
3142
3143 static PyMethodDef ml = {
3144 "create_cfunction",
3145 create_cfunction,
3146 METH_NOARGS,
3147 NULL
3148 };
3149
3150 static PyObject *
_test_incref(PyObject * ob)3151 _test_incref(PyObject *ob)
3152 {
3153 Py_INCREF(ob);
3154 return ob;
3155 }
3156
3157 static PyObject *
test_xincref_doesnt_leak(PyObject * ob)3158 test_xincref_doesnt_leak(PyObject *ob)
3159 {
3160 PyObject *obj = PyLong_FromLong(0);
3161 Py_XINCREF(_test_incref(obj));
3162 Py_DECREF(obj);
3163 Py_DECREF(obj);
3164 Py_DECREF(obj);
3165 Py_RETURN_NONE;
3166 }
3167
3168 static PyObject *
test_incref_doesnt_leak(PyObject * ob)3169 test_incref_doesnt_leak(PyObject *ob)
3170 {
3171 PyObject *obj = PyLong_FromLong(0);
3172 Py_INCREF(_test_incref(obj));
3173 Py_DECREF(obj);
3174 Py_DECREF(obj);
3175 Py_DECREF(obj);
3176 Py_RETURN_NONE;
3177 }
3178
3179 static PyObject *
test_xdecref_doesnt_leak(PyObject * ob)3180 test_xdecref_doesnt_leak(PyObject *ob)
3181 {
3182 Py_XDECREF(PyLong_FromLong(0));
3183 Py_RETURN_NONE;
3184 }
3185
3186 static PyObject *
test_decref_doesnt_leak(PyObject * ob)3187 test_decref_doesnt_leak(PyObject *ob)
3188 {
3189 Py_DECREF(PyLong_FromLong(0));
3190 Py_RETURN_NONE;
3191 }
3192
3193 static PyObject *
test_incref_decref_API(PyObject * ob)3194 test_incref_decref_API(PyObject *ob)
3195 {
3196 PyObject *obj = PyLong_FromLong(0);
3197 Py_IncRef(obj);
3198 Py_DecRef(obj);
3199 Py_DecRef(obj);
3200 Py_RETURN_NONE;
3201 }
3202
3203 static PyObject *
test_pymem_alloc0(PyObject * self)3204 test_pymem_alloc0(PyObject *self)
3205 {
3206 void *ptr;
3207
3208 ptr = PyMem_RawMalloc(0);
3209 if (ptr == NULL) {
3210 PyErr_SetString(PyExc_RuntimeError, "PyMem_RawMalloc(0) returns NULL");
3211 return NULL;
3212 }
3213 PyMem_RawFree(ptr);
3214
3215 ptr = PyMem_RawCalloc(0, 0);
3216 if (ptr == NULL) {
3217 PyErr_SetString(PyExc_RuntimeError, "PyMem_RawCalloc(0, 0) returns NULL");
3218 return NULL;
3219 }
3220 PyMem_RawFree(ptr);
3221
3222 ptr = PyMem_Malloc(0);
3223 if (ptr == NULL) {
3224 PyErr_SetString(PyExc_RuntimeError, "PyMem_Malloc(0) returns NULL");
3225 return NULL;
3226 }
3227 PyMem_Free(ptr);
3228
3229 ptr = PyMem_Calloc(0, 0);
3230 if (ptr == NULL) {
3231 PyErr_SetString(PyExc_RuntimeError, "PyMem_Calloc(0, 0) returns NULL");
3232 return NULL;
3233 }
3234 PyMem_Free(ptr);
3235
3236 ptr = PyObject_Malloc(0);
3237 if (ptr == NULL) {
3238 PyErr_SetString(PyExc_RuntimeError, "PyObject_Malloc(0) returns NULL");
3239 return NULL;
3240 }
3241 PyObject_Free(ptr);
3242
3243 ptr = PyObject_Calloc(0, 0);
3244 if (ptr == NULL) {
3245 PyErr_SetString(PyExc_RuntimeError, "PyObject_Calloc(0, 0) returns NULL");
3246 return NULL;
3247 }
3248 PyObject_Free(ptr);
3249
3250 Py_RETURN_NONE;
3251 }
3252
3253 typedef struct {
3254 PyMemAllocatorEx alloc;
3255
3256 size_t malloc_size;
3257 size_t calloc_nelem;
3258 size_t calloc_elsize;
3259 void *realloc_ptr;
3260 size_t realloc_new_size;
3261 void *free_ptr;
3262 } alloc_hook_t;
3263
hook_malloc(void * ctx,size_t size)3264 static void* hook_malloc (void* ctx, size_t size)
3265 {
3266 alloc_hook_t *hook = (alloc_hook_t *)ctx;
3267 hook->malloc_size = size;
3268 return hook->alloc.malloc(hook->alloc.ctx, size);
3269 }
3270
hook_calloc(void * ctx,size_t nelem,size_t elsize)3271 static void* hook_calloc (void* ctx, size_t nelem, size_t elsize)
3272 {
3273 alloc_hook_t *hook = (alloc_hook_t *)ctx;
3274 hook->calloc_nelem = nelem;
3275 hook->calloc_elsize = elsize;
3276 return hook->alloc.calloc(hook->alloc.ctx, nelem, elsize);
3277 }
3278
hook_realloc(void * ctx,void * ptr,size_t new_size)3279 static void* hook_realloc (void* ctx, void* ptr, size_t new_size)
3280 {
3281 alloc_hook_t *hook = (alloc_hook_t *)ctx;
3282 hook->realloc_ptr = ptr;
3283 hook->realloc_new_size = new_size;
3284 return hook->alloc.realloc(hook->alloc.ctx, ptr, new_size);
3285 }
3286
hook_free(void * ctx,void * ptr)3287 static void hook_free (void *ctx, void *ptr)
3288 {
3289 alloc_hook_t *hook = (alloc_hook_t *)ctx;
3290 hook->free_ptr = ptr;
3291 hook->alloc.free(hook->alloc.ctx, ptr);
3292 }
3293
3294 static PyObject *
test_setallocators(PyMemAllocatorDomain domain)3295 test_setallocators(PyMemAllocatorDomain domain)
3296 {
3297 PyObject *res = NULL;
3298 const char *error_msg;
3299 alloc_hook_t hook;
3300 PyMemAllocatorEx alloc;
3301 size_t size, size2, nelem, elsize;
3302 void *ptr, *ptr2;
3303
3304 memset(&hook, 0, sizeof(hook));
3305
3306 alloc.ctx = &hook;
3307 alloc.malloc = &hook_malloc;
3308 alloc.calloc = &hook_calloc;
3309 alloc.realloc = &hook_realloc;
3310 alloc.free = &hook_free;
3311 PyMem_GetAllocator(domain, &hook.alloc);
3312 PyMem_SetAllocator(domain, &alloc);
3313
3314 size = 42;
3315 switch(domain)
3316 {
3317 case PYMEM_DOMAIN_RAW: ptr = PyMem_RawMalloc(size); break;
3318 case PYMEM_DOMAIN_MEM: ptr = PyMem_Malloc(size); break;
3319 case PYMEM_DOMAIN_OBJ: ptr = PyObject_Malloc(size); break;
3320 default: ptr = NULL; break;
3321 }
3322
3323 if (ptr == NULL) {
3324 error_msg = "malloc failed";
3325 goto fail;
3326 }
3327
3328 if (hook.malloc_size != size) {
3329 error_msg = "malloc invalid size";
3330 goto fail;
3331 }
3332
3333 size2 = 200;
3334 switch(domain)
3335 {
3336 case PYMEM_DOMAIN_RAW: ptr2 = PyMem_RawRealloc(ptr, size2); break;
3337 case PYMEM_DOMAIN_MEM: ptr2 = PyMem_Realloc(ptr, size2); break;
3338 case PYMEM_DOMAIN_OBJ: ptr2 = PyObject_Realloc(ptr, size2); break;
3339 default: ptr2 = NULL; break;
3340 }
3341
3342 if (ptr2 == NULL) {
3343 error_msg = "realloc failed";
3344 goto fail;
3345 }
3346
3347 if (hook.realloc_ptr != ptr
3348 || hook.realloc_new_size != size2) {
3349 error_msg = "realloc invalid parameters";
3350 goto fail;
3351 }
3352
3353 switch(domain)
3354 {
3355 case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr2); break;
3356 case PYMEM_DOMAIN_MEM: PyMem_Free(ptr2); break;
3357 case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr2); break;
3358 }
3359
3360 if (hook.free_ptr != ptr2) {
3361 error_msg = "free invalid pointer";
3362 goto fail;
3363 }
3364
3365 nelem = 2;
3366 elsize = 5;
3367 switch(domain)
3368 {
3369 case PYMEM_DOMAIN_RAW: ptr = PyMem_RawCalloc(nelem, elsize); break;
3370 case PYMEM_DOMAIN_MEM: ptr = PyMem_Calloc(nelem, elsize); break;
3371 case PYMEM_DOMAIN_OBJ: ptr = PyObject_Calloc(nelem, elsize); break;
3372 default: ptr = NULL; break;
3373 }
3374
3375 if (ptr == NULL) {
3376 error_msg = "calloc failed";
3377 goto fail;
3378 }
3379
3380 if (hook.calloc_nelem != nelem || hook.calloc_elsize != elsize) {
3381 error_msg = "calloc invalid nelem or elsize";
3382 goto fail;
3383 }
3384
3385 switch(domain)
3386 {
3387 case PYMEM_DOMAIN_RAW: PyMem_RawFree(ptr); break;
3388 case PYMEM_DOMAIN_MEM: PyMem_Free(ptr); break;
3389 case PYMEM_DOMAIN_OBJ: PyObject_Free(ptr); break;
3390 }
3391
3392 Py_INCREF(Py_None);
3393 res = Py_None;
3394 goto finally;
3395
3396 fail:
3397 PyErr_SetString(PyExc_RuntimeError, error_msg);
3398
3399 finally:
3400 PyMem_SetAllocator(domain, &hook.alloc);
3401 return res;
3402 }
3403
3404 static PyObject *
test_pymem_setrawallocators(PyObject * self)3405 test_pymem_setrawallocators(PyObject *self)
3406 {
3407 return test_setallocators(PYMEM_DOMAIN_RAW);
3408 }
3409
3410 static PyObject *
test_pymem_setallocators(PyObject * self)3411 test_pymem_setallocators(PyObject *self)
3412 {
3413 return test_setallocators(PYMEM_DOMAIN_MEM);
3414 }
3415
3416 static PyObject *
test_pyobject_setallocators(PyObject * self)3417 test_pyobject_setallocators(PyObject *self)
3418 {
3419 return test_setallocators(PYMEM_DOMAIN_OBJ);
3420 }
3421
3422 PyDoc_STRVAR(docstring_empty,
3423 ""
3424 );
3425
3426 PyDoc_STRVAR(docstring_no_signature,
3427 "This docstring has no signature."
3428 );
3429
3430 PyDoc_STRVAR(docstring_with_invalid_signature,
3431 "docstring_with_invalid_signature($module, /, boo)\n"
3432 "\n"
3433 "This docstring has an invalid signature."
3434 );
3435
3436 PyDoc_STRVAR(docstring_with_invalid_signature2,
3437 "docstring_with_invalid_signature2($module, /, boo)\n"
3438 "\n"
3439 "--\n"
3440 "\n"
3441 "This docstring also has an invalid signature."
3442 );
3443
3444 PyDoc_STRVAR(docstring_with_signature,
3445 "docstring_with_signature($module, /, sig)\n"
3446 "--\n"
3447 "\n"
3448 "This docstring has a valid signature."
3449 );
3450
3451 PyDoc_STRVAR(docstring_with_signature_but_no_doc,
3452 "docstring_with_signature_but_no_doc($module, /, sig)\n"
3453 "--\n"
3454 "\n"
3455 );
3456
3457 PyDoc_STRVAR(docstring_with_signature_and_extra_newlines,
3458 "docstring_with_signature_and_extra_newlines($module, /, parameter)\n"
3459 "--\n"
3460 "\n"
3461 "\n"
3462 "This docstring has a valid signature and some extra newlines."
3463 );
3464
3465 PyDoc_STRVAR(docstring_with_signature_with_defaults,
3466 "docstring_with_signature_with_defaults(module, s='avocado',\n"
3467 " b=b'bytes', d=3.14, i=35, n=None, t=True, f=False,\n"
3468 " local=the_number_three, sys=sys.maxsize,\n"
3469 " exp=sys.maxsize - 1)\n"
3470 "--\n"
3471 "\n"
3472 "\n"
3473 "\n"
3474 "This docstring has a valid signature with parameters,\n"
3475 "and the parameters take defaults of varying types."
3476 );
3477
3478 #ifdef WITH_THREAD
3479 typedef struct {
3480 PyThread_type_lock start_event;
3481 PyThread_type_lock exit_event;
3482 PyObject *callback;
3483 } test_c_thread_t;
3484
3485 static void
temporary_c_thread(void * data)3486 temporary_c_thread(void *data)
3487 {
3488 test_c_thread_t *test_c_thread = data;
3489 PyGILState_STATE state;
3490 PyObject *res;
3491
3492 PyThread_release_lock(test_c_thread->start_event);
3493
3494 /* Allocate a Python thread state for this thread */
3495 state = PyGILState_Ensure();
3496
3497 res = _PyObject_CallNoArg(test_c_thread->callback);
3498 Py_CLEAR(test_c_thread->callback);
3499
3500 if (res == NULL) {
3501 PyErr_Print();
3502 }
3503 else {
3504 Py_DECREF(res);
3505 }
3506
3507 /* Destroy the Python thread state for this thread */
3508 PyGILState_Release(state);
3509
3510 PyThread_release_lock(test_c_thread->exit_event);
3511
3512 PyThread_exit_thread();
3513 }
3514
3515 static PyObject *
call_in_temporary_c_thread(PyObject * self,PyObject * callback)3516 call_in_temporary_c_thread(PyObject *self, PyObject *callback)
3517 {
3518 PyObject *res = NULL;
3519 test_c_thread_t test_c_thread;
3520 long thread;
3521
3522 PyEval_InitThreads();
3523
3524 test_c_thread.start_event = PyThread_allocate_lock();
3525 test_c_thread.exit_event = PyThread_allocate_lock();
3526 test_c_thread.callback = NULL;
3527 if (!test_c_thread.start_event || !test_c_thread.exit_event) {
3528 PyErr_SetString(PyExc_RuntimeError, "could not allocate lock");
3529 goto exit;
3530 }
3531
3532 Py_INCREF(callback);
3533 test_c_thread.callback = callback;
3534
3535 PyThread_acquire_lock(test_c_thread.start_event, 1);
3536 PyThread_acquire_lock(test_c_thread.exit_event, 1);
3537
3538 thread = PyThread_start_new_thread(temporary_c_thread, &test_c_thread);
3539 if (thread == -1) {
3540 PyErr_SetString(PyExc_RuntimeError, "unable to start the thread");
3541 PyThread_release_lock(test_c_thread.start_event);
3542 PyThread_release_lock(test_c_thread.exit_event);
3543 goto exit;
3544 }
3545
3546 PyThread_acquire_lock(test_c_thread.start_event, 1);
3547 PyThread_release_lock(test_c_thread.start_event);
3548
3549 Py_BEGIN_ALLOW_THREADS
3550 PyThread_acquire_lock(test_c_thread.exit_event, 1);
3551 PyThread_release_lock(test_c_thread.exit_event);
3552 Py_END_ALLOW_THREADS
3553
3554 Py_INCREF(Py_None);
3555 res = Py_None;
3556
3557 exit:
3558 Py_CLEAR(test_c_thread.callback);
3559 if (test_c_thread.start_event)
3560 PyThread_free_lock(test_c_thread.start_event);
3561 if (test_c_thread.exit_event)
3562 PyThread_free_lock(test_c_thread.exit_event);
3563 return res;
3564 }
3565 #endif /* WITH_THREAD */
3566
3567 static PyObject*
test_raise_signal(PyObject * self,PyObject * args)3568 test_raise_signal(PyObject* self, PyObject *args)
3569 {
3570 int signum, err;
3571
3572 if (PyArg_ParseTuple(args, "i:raise_signal", &signum) < 0)
3573 return NULL;
3574
3575 err = raise(signum);
3576 if (err)
3577 return PyErr_SetFromErrno(PyExc_OSError);
3578
3579 if (PyErr_CheckSignals() < 0)
3580 return NULL;
3581
3582 Py_RETURN_NONE;
3583 }
3584
3585 /* marshal */
3586
3587 static PyObject*
pymarshal_write_long_to_file(PyObject * self,PyObject * args)3588 pymarshal_write_long_to_file(PyObject* self, PyObject *args)
3589 {
3590 long value;
3591 char *filename;
3592 int version;
3593 FILE *fp;
3594
3595 if (!PyArg_ParseTuple(args, "lsi:pymarshal_write_long_to_file",
3596 &value, &filename, &version))
3597 return NULL;
3598
3599 fp = fopen(filename, "wb");
3600 if (fp == NULL) {
3601 PyErr_SetFromErrno(PyExc_OSError);
3602 return NULL;
3603 }
3604
3605 PyMarshal_WriteLongToFile(value, fp, version);
3606
3607 fclose(fp);
3608 if (PyErr_Occurred())
3609 return NULL;
3610 Py_RETURN_NONE;
3611 }
3612
3613 static PyObject*
pymarshal_write_object_to_file(PyObject * self,PyObject * args)3614 pymarshal_write_object_to_file(PyObject* self, PyObject *args)
3615 {
3616 PyObject *obj;
3617 char *filename;
3618 int version;
3619 FILE *fp;
3620
3621 if (!PyArg_ParseTuple(args, "Osi:pymarshal_write_object_to_file",
3622 &obj, &filename, &version))
3623 return NULL;
3624
3625 fp = fopen(filename, "wb");
3626 if (fp == NULL) {
3627 PyErr_SetFromErrno(PyExc_OSError);
3628 return NULL;
3629 }
3630
3631 PyMarshal_WriteObjectToFile(obj, fp, version);
3632
3633 fclose(fp);
3634 if (PyErr_Occurred())
3635 return NULL;
3636 Py_RETURN_NONE;
3637 }
3638
3639 static PyObject*
pymarshal_read_short_from_file(PyObject * self,PyObject * args)3640 pymarshal_read_short_from_file(PyObject* self, PyObject *args)
3641 {
3642 int value;
3643 long pos;
3644 char *filename;
3645 FILE *fp;
3646
3647 if (!PyArg_ParseTuple(args, "s:pymarshal_read_short_from_file", &filename))
3648 return NULL;
3649
3650 fp = fopen(filename, "rb");
3651 if (fp == NULL) {
3652 PyErr_SetFromErrno(PyExc_OSError);
3653 return NULL;
3654 }
3655
3656 value = PyMarshal_ReadShortFromFile(fp);
3657 pos = ftell(fp);
3658
3659 fclose(fp);
3660 if (PyErr_Occurred())
3661 return NULL;
3662 return Py_BuildValue("il", value, pos);
3663 }
3664
3665 static PyObject*
pymarshal_read_long_from_file(PyObject * self,PyObject * args)3666 pymarshal_read_long_from_file(PyObject* self, PyObject *args)
3667 {
3668 long value, pos;
3669 char *filename;
3670 FILE *fp;
3671
3672 if (!PyArg_ParseTuple(args, "s:pymarshal_read_long_from_file", &filename))
3673 return NULL;
3674
3675 fp = fopen(filename, "rb");
3676 if (fp == NULL) {
3677 PyErr_SetFromErrno(PyExc_OSError);
3678 return NULL;
3679 }
3680
3681 value = PyMarshal_ReadLongFromFile(fp);
3682 pos = ftell(fp);
3683
3684 fclose(fp);
3685 if (PyErr_Occurred())
3686 return NULL;
3687 return Py_BuildValue("ll", value, pos);
3688 }
3689
3690 static PyObject*
pymarshal_read_last_object_from_file(PyObject * self,PyObject * args)3691 pymarshal_read_last_object_from_file(PyObject* self, PyObject *args)
3692 {
3693 PyObject *obj;
3694 long pos;
3695 char *filename;
3696 FILE *fp;
3697
3698 if (!PyArg_ParseTuple(args, "s:pymarshal_read_last_object_from_file", &filename))
3699 return NULL;
3700
3701 fp = fopen(filename, "rb");
3702 if (fp == NULL) {
3703 PyErr_SetFromErrno(PyExc_OSError);
3704 return NULL;
3705 }
3706
3707 obj = PyMarshal_ReadLastObjectFromFile(fp);
3708 pos = ftell(fp);
3709
3710 fclose(fp);
3711 return Py_BuildValue("Nl", obj, pos);
3712 }
3713
3714 static PyObject*
pymarshal_read_object_from_file(PyObject * self,PyObject * args)3715 pymarshal_read_object_from_file(PyObject* self, PyObject *args)
3716 {
3717 PyObject *obj;
3718 long pos;
3719 char *filename;
3720 FILE *fp;
3721
3722 if (!PyArg_ParseTuple(args, "s:pymarshal_read_object_from_file", &filename))
3723 return NULL;
3724
3725 fp = fopen(filename, "rb");
3726 if (fp == NULL) {
3727 PyErr_SetFromErrno(PyExc_OSError);
3728 return NULL;
3729 }
3730
3731 obj = PyMarshal_ReadObjectFromFile(fp);
3732 pos = ftell(fp);
3733
3734 fclose(fp);
3735 return Py_BuildValue("Nl", obj, pos);
3736 }
3737
3738 static PyObject*
return_null_without_error(PyObject * self,PyObject * args)3739 return_null_without_error(PyObject *self, PyObject *args)
3740 {
3741 /* invalid call: return NULL without setting an error,
3742 * _Py_CheckFunctionResult() must detect such bug at runtime. */
3743 PyErr_Clear();
3744 return NULL;
3745 }
3746
3747 static PyObject*
return_result_with_error(PyObject * self,PyObject * args)3748 return_result_with_error(PyObject *self, PyObject *args)
3749 {
3750 /* invalid call: return a result with an error set,
3751 * _Py_CheckFunctionResult() must detect such bug at runtime. */
3752 PyErr_SetNone(PyExc_ValueError);
3753 Py_RETURN_NONE;
3754 }
3755
3756 static PyObject *
test_pytime_fromseconds(PyObject * self,PyObject * args)3757 test_pytime_fromseconds(PyObject *self, PyObject *args)
3758 {
3759 int seconds;
3760 _PyTime_t ts;
3761
3762 if (!PyArg_ParseTuple(args, "i", &seconds))
3763 return NULL;
3764 ts = _PyTime_FromSeconds(seconds);
3765 return _PyTime_AsNanosecondsObject(ts);
3766 }
3767
3768 static PyObject *
test_pytime_fromsecondsobject(PyObject * self,PyObject * args)3769 test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
3770 {
3771 PyObject *obj;
3772 int round;
3773 _PyTime_t ts;
3774
3775 if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
3776 return NULL;
3777 if (check_time_rounding(round) < 0)
3778 return NULL;
3779 if (_PyTime_FromSecondsObject(&ts, obj, round) == -1)
3780 return NULL;
3781 return _PyTime_AsNanosecondsObject(ts);
3782 }
3783
3784 static PyObject *
test_pytime_assecondsdouble(PyObject * self,PyObject * args)3785 test_pytime_assecondsdouble(PyObject *self, PyObject *args)
3786 {
3787 long long ns;
3788 _PyTime_t ts;
3789 double d;
3790
3791 if (!PyArg_ParseTuple(args, "L", &ns))
3792 return NULL;
3793 ts = _PyTime_FromNanoseconds(ns);
3794 d = _PyTime_AsSecondsDouble(ts);
3795 return PyFloat_FromDouble(d);
3796 }
3797
3798 static PyObject *
test_PyTime_AsTimeval(PyObject * self,PyObject * args)3799 test_PyTime_AsTimeval(PyObject *self, PyObject *args)
3800 {
3801 long long ns;
3802 int round;
3803 _PyTime_t t;
3804 struct timeval tv;
3805 PyObject *seconds;
3806
3807 if (!PyArg_ParseTuple(args, "Li", &ns, &round))
3808 return NULL;
3809 if (check_time_rounding(round) < 0)
3810 return NULL;
3811 t = _PyTime_FromNanoseconds(ns);
3812 if (_PyTime_AsTimeval(t, &tv, round) < 0)
3813 return NULL;
3814
3815 seconds = PyLong_FromLong((long long)tv.tv_sec);
3816 if (seconds == NULL)
3817 return NULL;
3818 return Py_BuildValue("Nl", seconds, tv.tv_usec);
3819 }
3820
3821 #ifdef HAVE_CLOCK_GETTIME
3822 static PyObject *
test_PyTime_AsTimespec(PyObject * self,PyObject * args)3823 test_PyTime_AsTimespec(PyObject *self, PyObject *args)
3824 {
3825 long long ns;
3826 _PyTime_t t;
3827 struct timespec ts;
3828
3829 if (!PyArg_ParseTuple(args, "L", &ns))
3830 return NULL;
3831 t = _PyTime_FromNanoseconds(ns);
3832 if (_PyTime_AsTimespec(t, &ts) == -1)
3833 return NULL;
3834 return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
3835 }
3836 #endif
3837
3838 static PyObject *
test_PyTime_AsMilliseconds(PyObject * self,PyObject * args)3839 test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
3840 {
3841 long long ns;
3842 int round;
3843 _PyTime_t t, ms;
3844
3845 if (!PyArg_ParseTuple(args, "Li", &ns, &round))
3846 return NULL;
3847 if (check_time_rounding(round) < 0)
3848 return NULL;
3849 t = _PyTime_FromNanoseconds(ns);
3850 ms = _PyTime_AsMilliseconds(t, round);
3851 /* This conversion rely on the fact that _PyTime_t is a number of
3852 nanoseconds */
3853 return _PyTime_AsNanosecondsObject(ms);
3854 }
3855
3856 static PyObject *
test_PyTime_AsMicroseconds(PyObject * self,PyObject * args)3857 test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
3858 {
3859 long long ns;
3860 int round;
3861 _PyTime_t t, ms;
3862
3863 if (!PyArg_ParseTuple(args, "Li", &ns, &round))
3864 return NULL;
3865 if (check_time_rounding(round) < 0)
3866 return NULL;
3867 t = _PyTime_FromNanoseconds(ns);
3868 ms = _PyTime_AsMicroseconds(t, round);
3869 /* This conversion rely on the fact that _PyTime_t is a number of
3870 nanoseconds */
3871 return _PyTime_AsNanosecondsObject(ms);
3872 }
3873
3874 static PyObject*
get_recursion_depth(PyObject * self,PyObject * args)3875 get_recursion_depth(PyObject *self, PyObject *args)
3876 {
3877 PyThreadState *tstate = PyThreadState_GET();
3878
3879 /* subtract one to ignore the frame of the get_recursion_depth() call */
3880 return PyLong_FromLong(tstate->recursion_depth - 1);
3881 }
3882
3883 static PyObject*
pymem_buffer_overflow(PyObject * self,PyObject * args)3884 pymem_buffer_overflow(PyObject *self, PyObject *args)
3885 {
3886 char *buffer;
3887
3888 /* Deliberate buffer overflow to check that PyMem_Free() detects
3889 the overflow when debug hooks are installed. */
3890 buffer = PyMem_Malloc(16);
3891 buffer[16] = 'x';
3892 PyMem_Free(buffer);
3893
3894 Py_RETURN_NONE;
3895 }
3896
3897 static PyObject*
pymem_api_misuse(PyObject * self,PyObject * args)3898 pymem_api_misuse(PyObject *self, PyObject *args)
3899 {
3900 char *buffer;
3901
3902 /* Deliberate misusage of Python allocators:
3903 allococate with PyMem but release with PyMem_Raw. */
3904 buffer = PyMem_Malloc(16);
3905 PyMem_RawFree(buffer);
3906
3907 Py_RETURN_NONE;
3908 }
3909
3910 static PyObject*
pymem_malloc_without_gil(PyObject * self,PyObject * args)3911 pymem_malloc_without_gil(PyObject *self, PyObject *args)
3912 {
3913 char *buffer;
3914
3915 /* Deliberate bug to test debug hooks on Python memory allocators:
3916 call PyMem_Malloc() without holding the GIL */
3917 Py_BEGIN_ALLOW_THREADS
3918 buffer = PyMem_Malloc(10);
3919 Py_END_ALLOW_THREADS
3920
3921 PyMem_Free(buffer);
3922
3923 Py_RETURN_NONE;
3924 }
3925
3926 static PyObject*
pyobject_malloc_without_gil(PyObject * self,PyObject * args)3927 pyobject_malloc_without_gil(PyObject *self, PyObject *args)
3928 {
3929 char *buffer;
3930
3931 /* Deliberate bug to test debug hooks on Python memory allocators:
3932 call PyObject_Malloc() without holding the GIL */
3933 Py_BEGIN_ALLOW_THREADS
3934 buffer = PyObject_Malloc(10);
3935 Py_END_ALLOW_THREADS
3936
3937 PyObject_Free(buffer);
3938
3939 Py_RETURN_NONE;
3940 }
3941
3942 static PyObject *
tracemalloc_track(PyObject * self,PyObject * args)3943 tracemalloc_track(PyObject *self, PyObject *args)
3944 {
3945 unsigned int domain;
3946 PyObject *ptr_obj;
3947 void *ptr;
3948 Py_ssize_t size;
3949 int release_gil = 0;
3950 int res;
3951
3952 if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil))
3953 return NULL;
3954 ptr = PyLong_AsVoidPtr(ptr_obj);
3955 if (PyErr_Occurred())
3956 return NULL;
3957
3958 if (release_gil) {
3959 Py_BEGIN_ALLOW_THREADS
3960 res = _PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
3961 Py_END_ALLOW_THREADS
3962 }
3963 else {
3964 res = _PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
3965 }
3966
3967 if (res < 0) {
3968 PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error");
3969 return NULL;
3970 }
3971
3972 Py_RETURN_NONE;
3973 }
3974
3975 static PyObject *
tracemalloc_untrack(PyObject * self,PyObject * args)3976 tracemalloc_untrack(PyObject *self, PyObject *args)
3977 {
3978 unsigned int domain;
3979 PyObject *ptr_obj;
3980 void *ptr;
3981 int res;
3982
3983 if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
3984 return NULL;
3985 ptr = PyLong_AsVoidPtr(ptr_obj);
3986 if (PyErr_Occurred())
3987 return NULL;
3988
3989 res = _PyTraceMalloc_Untrack(domain, (uintptr_t)ptr);
3990 if (res < 0) {
3991 PyErr_SetString(PyExc_RuntimeError, "_PyTraceMalloc_Track error");
3992 return NULL;
3993 }
3994
3995 Py_RETURN_NONE;
3996 }
3997
3998 static PyObject *
tracemalloc_get_traceback(PyObject * self,PyObject * args)3999 tracemalloc_get_traceback(PyObject *self, PyObject *args)
4000 {
4001 unsigned int domain;
4002 PyObject *ptr_obj;
4003 void *ptr;
4004
4005 if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
4006 return NULL;
4007 ptr = PyLong_AsVoidPtr(ptr_obj);
4008 if (PyErr_Occurred())
4009 return NULL;
4010
4011 return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
4012 }
4013
4014 static PyObject *
dict_get_version(PyObject * self,PyObject * args)4015 dict_get_version(PyObject *self, PyObject *args)
4016 {
4017 PyDictObject *dict;
4018 uint64_t version;
4019
4020 if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict))
4021 return NULL;
4022
4023 version = dict->ma_version_tag;
4024
4025 Py_BUILD_ASSERT(sizeof(unsigned PY_LONG_LONG) >= sizeof(version));
4026 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)version);
4027 }
4028
4029
4030 static PyMethodDef TestMethods[] = {
4031 {"raise_exception", raise_exception, METH_VARARGS},
4032 {"raise_memoryerror", (PyCFunction)raise_memoryerror, METH_NOARGS},
4033 {"set_errno", set_errno, METH_VARARGS},
4034 {"test_config", (PyCFunction)test_config, METH_NOARGS},
4035 {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS},
4036 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
4037 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
4038 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
4039 {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
4040 {"dict_hassplittable", dict_hassplittable, METH_O},
4041 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
4042 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
4043 {"test_xincref_doesnt_leak",(PyCFunction)test_xincref_doesnt_leak, METH_NOARGS},
4044 {"test_incref_doesnt_leak", (PyCFunction)test_incref_doesnt_leak, METH_NOARGS},
4045 {"test_xdecref_doesnt_leak",(PyCFunction)test_xdecref_doesnt_leak, METH_NOARGS},
4046 {"test_decref_doesnt_leak", (PyCFunction)test_decref_doesnt_leak, METH_NOARGS},
4047 {"test_incref_decref_API", (PyCFunction)test_incref_decref_API, METH_NOARGS},
4048 {"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
4049 METH_NOARGS},
4050 {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS},
4051 {"test_long_as_size_t", (PyCFunction)test_long_as_size_t,METH_NOARGS},
4052 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
4053 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
4054 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
4055 {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
4056 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
4057 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
4058 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
4059 PyDoc_STR("This is a pretty normal docstring.")},
4060 {"test_string_to_double", (PyCFunction)test_string_to_double, METH_NOARGS},
4061 {"test_unicode_compare_with_ascii", (PyCFunction)test_unicode_compare_with_ascii, METH_NOARGS},
4062 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
4063 {"test_from_contiguous", (PyCFunction)test_from_contiguous, METH_NOARGS},
4064 #if (defined(__linux__) || defined(__FreeBSD__)) && defined(__GNUC__)
4065 {"test_pep3118_obsolete_write_locks", (PyCFunction)test_pep3118_obsolete_write_locks, METH_NOARGS},
4066 #endif
4067 {"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
4068 {"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
4069 {"get_args", get_args, METH_VARARGS},
4070 {"get_kwargs", (PyCFunction)get_kwargs, METH_VARARGS|METH_KEYWORDS},
4071 {"getargs_tuple", getargs_tuple, METH_VARARGS},
4072 {"getargs_keywords", (PyCFunction)getargs_keywords,
4073 METH_VARARGS|METH_KEYWORDS},
4074 {"getargs_keyword_only", (PyCFunction)getargs_keyword_only,
4075 METH_VARARGS|METH_KEYWORDS},
4076 {"getargs_positional_only_and_keywords",
4077 (PyCFunction)getargs_positional_only_and_keywords,
4078 METH_VARARGS|METH_KEYWORDS},
4079 {"getargs_b", getargs_b, METH_VARARGS},
4080 {"getargs_B", getargs_B, METH_VARARGS},
4081 {"getargs_h", getargs_h, METH_VARARGS},
4082 {"getargs_H", getargs_H, METH_VARARGS},
4083 {"getargs_I", getargs_I, METH_VARARGS},
4084 {"getargs_k", getargs_k, METH_VARARGS},
4085 {"getargs_i", getargs_i, METH_VARARGS},
4086 {"getargs_l", getargs_l, METH_VARARGS},
4087 {"getargs_n", getargs_n, METH_VARARGS},
4088 {"getargs_p", getargs_p, METH_VARARGS},
4089 {"getargs_L", getargs_L, METH_VARARGS},
4090 {"getargs_K", getargs_K, METH_VARARGS},
4091 {"test_longlong_api", test_longlong_api, METH_NOARGS},
4092 {"test_long_long_and_overflow",
4093 (PyCFunction)test_long_long_and_overflow, METH_NOARGS},
4094 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
4095 {"getargs_f", getargs_f, METH_VARARGS},
4096 {"getargs_d", getargs_d, METH_VARARGS},
4097 {"getargs_D", getargs_D, METH_VARARGS},
4098 {"getargs_S", getargs_S, METH_VARARGS},
4099 {"getargs_Y", getargs_Y, METH_VARARGS},
4100 {"getargs_U", getargs_U, METH_VARARGS},
4101 {"getargs_c", getargs_c, METH_VARARGS},
4102 {"getargs_C", getargs_C, METH_VARARGS},
4103 {"getargs_s", getargs_s, METH_VARARGS},
4104 {"getargs_s_star", getargs_s_star, METH_VARARGS},
4105 {"getargs_s_hash", getargs_s_hash, METH_VARARGS},
4106 {"getargs_z", getargs_z, METH_VARARGS},
4107 {"getargs_z_star", getargs_z_star, METH_VARARGS},
4108 {"getargs_z_hash", getargs_z_hash, METH_VARARGS},
4109 {"getargs_y", getargs_y, METH_VARARGS},
4110 {"getargs_y_star", getargs_y_star, METH_VARARGS},
4111 {"getargs_y_hash", getargs_y_hash, METH_VARARGS},
4112 {"getargs_u", getargs_u, METH_VARARGS},
4113 {"getargs_u_hash", getargs_u_hash, METH_VARARGS},
4114 {"getargs_Z", getargs_Z, METH_VARARGS},
4115 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS},
4116 {"getargs_w_star", getargs_w_star, METH_VARARGS},
4117 {"getargs_es", getargs_es, METH_VARARGS},
4118 {"getargs_et", getargs_et, METH_VARARGS},
4119 {"getargs_es_hash", getargs_es_hash, METH_VARARGS},
4120 {"getargs_et_hash", getargs_et_hash, METH_VARARGS},
4121 {"codec_incrementalencoder",
4122 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
4123 {"codec_incrementaldecoder",
4124 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
4125 {"test_s_code", (PyCFunction)test_s_code, METH_NOARGS},
4126 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
4127 {"test_Z_code", (PyCFunction)test_Z_code, METH_NOARGS},
4128 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
4129 {"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
4130 {"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
4131 {"unicode_asucs4", unicode_asucs4, METH_VARARGS},
4132 {"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
4133 {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
4134 {"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
4135 {"unicode_legacy_string", unicode_legacy_string, METH_VARARGS},
4136 #ifdef WITH_THREAD
4137 {"_test_thread_state", test_thread_state, METH_VARARGS},
4138 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
4139 #endif
4140 #ifdef HAVE_GETTIMEOFDAY
4141 {"profile_int", profile_int, METH_NOARGS},
4142 #endif
4143 {"traceback_print", traceback_print, METH_VARARGS},
4144 {"exception_print", exception_print, METH_VARARGS},
4145 {"set_exc_info", test_set_exc_info, METH_VARARGS},
4146 {"argparsing", argparsing, METH_VARARGS},
4147 {"code_newempty", code_newempty, METH_VARARGS},
4148 {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
4149 METH_VARARGS | METH_KEYWORDS},
4150 {"make_memoryview_from_NULL_pointer", (PyCFunction)make_memoryview_from_NULL_pointer,
4151 METH_NOARGS},
4152 {"crash_no_current_thread", (PyCFunction)crash_no_current_thread, METH_NOARGS},
4153 {"run_in_subinterp", run_in_subinterp, METH_VARARGS},
4154 {"pytime_object_to_time_t", test_pytime_object_to_time_t, METH_VARARGS},
4155 {"pytime_object_to_timeval", test_pytime_object_to_timeval, METH_VARARGS},
4156 {"pytime_object_to_timespec", test_pytime_object_to_timespec, METH_VARARGS},
4157 {"with_tp_del", with_tp_del, METH_VARARGS},
4158 {"create_cfunction", create_cfunction, METH_NOARGS},
4159 {"test_pymem_alloc0",
4160 (PyCFunction)test_pymem_alloc0, METH_NOARGS},
4161 {"test_pymem_setrawallocators",
4162 (PyCFunction)test_pymem_setrawallocators, METH_NOARGS},
4163 {"test_pymem_setallocators",
4164 (PyCFunction)test_pymem_setallocators, METH_NOARGS},
4165 {"test_pyobject_setallocators",
4166 (PyCFunction)test_pyobject_setallocators, METH_NOARGS},
4167 {"no_docstring",
4168 (PyCFunction)test_with_docstring, METH_NOARGS},
4169 {"docstring_empty",
4170 (PyCFunction)test_with_docstring, METH_NOARGS,
4171 docstring_empty},
4172 {"docstring_no_signature",
4173 (PyCFunction)test_with_docstring, METH_NOARGS,
4174 docstring_no_signature},
4175 {"docstring_with_invalid_signature",
4176 (PyCFunction)test_with_docstring, METH_NOARGS,
4177 docstring_with_invalid_signature},
4178 {"docstring_with_invalid_signature2",
4179 (PyCFunction)test_with_docstring, METH_NOARGS,
4180 docstring_with_invalid_signature2},
4181 {"docstring_with_signature",
4182 (PyCFunction)test_with_docstring, METH_NOARGS,
4183 docstring_with_signature},
4184 {"docstring_with_signature_but_no_doc",
4185 (PyCFunction)test_with_docstring, METH_NOARGS,
4186 docstring_with_signature_but_no_doc},
4187 {"docstring_with_signature_and_extra_newlines",
4188 (PyCFunction)test_with_docstring, METH_NOARGS,
4189 docstring_with_signature_and_extra_newlines},
4190 {"docstring_with_signature_with_defaults",
4191 (PyCFunction)test_with_docstring, METH_NOARGS,
4192 docstring_with_signature_with_defaults},
4193 {"raise_signal",
4194 (PyCFunction)test_raise_signal, METH_VARARGS},
4195 #ifdef WITH_THREAD
4196 {"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_O,
4197 PyDoc_STR("set_error_class(error_class) -> None")},
4198 #endif
4199 {"pymarshal_write_long_to_file",
4200 pymarshal_write_long_to_file, METH_VARARGS},
4201 {"pymarshal_write_object_to_file",
4202 pymarshal_write_object_to_file, METH_VARARGS},
4203 {"pymarshal_read_short_from_file",
4204 pymarshal_read_short_from_file, METH_VARARGS},
4205 {"pymarshal_read_long_from_file",
4206 pymarshal_read_long_from_file, METH_VARARGS},
4207 {"pymarshal_read_last_object_from_file",
4208 pymarshal_read_last_object_from_file, METH_VARARGS},
4209 {"pymarshal_read_object_from_file",
4210 pymarshal_read_object_from_file, METH_VARARGS},
4211 {"return_null_without_error",
4212 return_null_without_error, METH_NOARGS},
4213 {"return_result_with_error",
4214 return_result_with_error, METH_NOARGS},
4215 {"PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
4216 {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
4217 {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
4218 {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
4219 #ifdef HAVE_CLOCK_GETTIME
4220 {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS},
4221 #endif
4222 {"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
4223 {"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
4224 {"get_recursion_depth", get_recursion_depth, METH_NOARGS},
4225 {"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
4226 {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
4227 {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},
4228 {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS},
4229 {"tracemalloc_track", tracemalloc_track, METH_VARARGS},
4230 {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
4231 {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
4232 {"dict_get_version", dict_get_version, METH_VARARGS},
4233 {NULL, NULL} /* sentinel */
4234 };
4235
4236 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
4237
4238 typedef struct {
4239 char bool_member;
4240 char byte_member;
4241 unsigned char ubyte_member;
4242 short short_member;
4243 unsigned short ushort_member;
4244 int int_member;
4245 unsigned int uint_member;
4246 long long_member;
4247 unsigned long ulong_member;
4248 Py_ssize_t pyssizet_member;
4249 float float_member;
4250 double double_member;
4251 char inplace_member[6];
4252 long long longlong_member;
4253 unsigned long long ulonglong_member;
4254 } all_structmembers;
4255
4256 typedef struct {
4257 PyObject_HEAD
4258 all_structmembers structmembers;
4259 } test_structmembers;
4260
4261 static struct PyMemberDef test_members[] = {
4262 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
4263 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
4264 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
4265 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
4266 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
4267 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
4268 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
4269 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
4270 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
4271 {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL},
4272 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
4273 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
4274 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
4275 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
4276 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
4277 {NULL}
4278 };
4279
4280
4281 static PyObject *
test_structmembers_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)4282 test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4283 {
4284 static char *keywords[] = {
4285 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
4286 "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET",
4287 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
4288 "T_LONGLONG", "T_ULONGLONG",
4289 NULL};
4290 static const char fmt[] = "|bbBhHiIlknfds#LK";
4291 test_structmembers *ob;
4292 const char *s = NULL;
4293 Py_ssize_t string_len = 0;
4294 ob = PyObject_New(test_structmembers, type);
4295 if (ob == NULL)
4296 return NULL;
4297 memset(&ob->structmembers, 0, sizeof(all_structmembers));
4298 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
4299 &ob->structmembers.bool_member,
4300 &ob->structmembers.byte_member,
4301 &ob->structmembers.ubyte_member,
4302 &ob->structmembers.short_member,
4303 &ob->structmembers.ushort_member,
4304 &ob->structmembers.int_member,
4305 &ob->structmembers.uint_member,
4306 &ob->structmembers.long_member,
4307 &ob->structmembers.ulong_member,
4308 &ob->structmembers.pyssizet_member,
4309 &ob->structmembers.float_member,
4310 &ob->structmembers.double_member,
4311 &s, &string_len
4312 , &ob->structmembers.longlong_member,
4313 &ob->structmembers.ulonglong_member
4314 )) {
4315 Py_DECREF(ob);
4316 return NULL;
4317 }
4318 if (s != NULL) {
4319 if (string_len > 5) {
4320 Py_DECREF(ob);
4321 PyErr_SetString(PyExc_ValueError, "string too long");
4322 return NULL;
4323 }
4324 strcpy(ob->structmembers.inplace_member, s);
4325 }
4326 else {
4327 strcpy(ob->structmembers.inplace_member, "");
4328 }
4329 return (PyObject *)ob;
4330 }
4331
4332 static void
test_structmembers_free(PyObject * ob)4333 test_structmembers_free(PyObject *ob)
4334 {
4335 PyObject_FREE(ob);
4336 }
4337
4338 static PyTypeObject test_structmembersType = {
4339 PyVarObject_HEAD_INIT(NULL, 0)
4340 "test_structmembersType",
4341 sizeof(test_structmembers), /* tp_basicsize */
4342 0, /* tp_itemsize */
4343 test_structmembers_free, /* destructor tp_dealloc */
4344 0, /* tp_print */
4345 0, /* tp_getattr */
4346 0, /* tp_setattr */
4347 0, /* tp_reserved */
4348 0, /* tp_repr */
4349 0, /* tp_as_number */
4350 0, /* tp_as_sequence */
4351 0, /* tp_as_mapping */
4352 0, /* tp_hash */
4353 0, /* tp_call */
4354 0, /* tp_str */
4355 PyObject_GenericGetAttr, /* tp_getattro */
4356 PyObject_GenericSetAttr, /* tp_setattro */
4357 0, /* tp_as_buffer */
4358 0, /* tp_flags */
4359 "Type containing all structmember types",
4360 0, /* traverseproc tp_traverse */
4361 0, /* tp_clear */
4362 0, /* tp_richcompare */
4363 0, /* tp_weaklistoffset */
4364 0, /* tp_iter */
4365 0, /* tp_iternext */
4366 0, /* tp_methods */
4367 test_members, /* tp_members */
4368 0,
4369 0,
4370 0,
4371 0,
4372 0,
4373 0,
4374 0,
4375 0,
4376 test_structmembers_new, /* tp_new */
4377 };
4378
4379
4380 typedef struct {
4381 PyObject_HEAD
4382 } matmulObject;
4383
4384 static PyObject *
matmulType_matmul(PyObject * self,PyObject * other)4385 matmulType_matmul(PyObject *self, PyObject *other)
4386 {
4387 return Py_BuildValue("(sOO)", "matmul", self, other);
4388 }
4389
4390 static PyObject *
matmulType_imatmul(PyObject * self,PyObject * other)4391 matmulType_imatmul(PyObject *self, PyObject *other)
4392 {
4393 return Py_BuildValue("(sOO)", "imatmul", self, other);
4394 }
4395
4396 static void
matmulType_dealloc(PyObject * self)4397 matmulType_dealloc(PyObject *self)
4398 {
4399 Py_TYPE(self)->tp_free(self);
4400 }
4401
4402 static PyNumberMethods matmulType_as_number = {
4403 0, /* nb_add */
4404 0, /* nb_subtract */
4405 0, /* nb_multiply */
4406 0, /* nb_remainde r*/
4407 0, /* nb_divmod */
4408 0, /* nb_power */
4409 0, /* nb_negative */
4410 0, /* tp_positive */
4411 0, /* tp_absolute */
4412 0, /* tp_bool */
4413 0, /* nb_invert */
4414 0, /* nb_lshift */
4415 0, /* nb_rshift */
4416 0, /* nb_and */
4417 0, /* nb_xor */
4418 0, /* nb_or */
4419 0, /* nb_int */
4420 0, /* nb_reserved */
4421 0, /* nb_float */
4422 0, /* nb_inplace_add */
4423 0, /* nb_inplace_subtract */
4424 0, /* nb_inplace_multiply */
4425 0, /* nb_inplace_remainder */
4426 0, /* nb_inplace_power */
4427 0, /* nb_inplace_lshift */
4428 0, /* nb_inplace_rshift */
4429 0, /* nb_inplace_and */
4430 0, /* nb_inplace_xor */
4431 0, /* nb_inplace_or */
4432 0, /* nb_floor_divide */
4433 0, /* nb_true_divide */
4434 0, /* nb_inplace_floor_divide */
4435 0, /* nb_inplace_true_divide */
4436 0, /* nb_index */
4437 matmulType_matmul, /* nb_matrix_multiply */
4438 matmulType_imatmul /* nb_matrix_inplace_multiply */
4439 };
4440
4441 static PyTypeObject matmulType = {
4442 PyVarObject_HEAD_INIT(NULL, 0)
4443 "matmulType",
4444 sizeof(matmulObject), /* tp_basicsize */
4445 0, /* tp_itemsize */
4446 matmulType_dealloc, /* destructor tp_dealloc */
4447 0, /* tp_print */
4448 0, /* tp_getattr */
4449 0, /* tp_setattr */
4450 0, /* tp_reserved */
4451 0, /* tp_repr */
4452 &matmulType_as_number, /* tp_as_number */
4453 0, /* tp_as_sequence */
4454 0, /* tp_as_mapping */
4455 0, /* tp_hash */
4456 0, /* tp_call */
4457 0, /* tp_str */
4458 PyObject_GenericGetAttr, /* tp_getattro */
4459 PyObject_GenericSetAttr, /* tp_setattro */
4460 0, /* tp_as_buffer */
4461 0, /* tp_flags */
4462 "C level type with matrix operations defined",
4463 0, /* traverseproc tp_traverse */
4464 0, /* tp_clear */
4465 0, /* tp_richcompare */
4466 0, /* tp_weaklistoffset */
4467 0, /* tp_iter */
4468 0, /* tp_iternext */
4469 0, /* tp_methods */
4470 0, /* tp_members */
4471 0,
4472 0,
4473 0,
4474 0,
4475 0,
4476 0,
4477 0,
4478 0,
4479 PyType_GenericNew, /* tp_new */
4480 PyObject_Del, /* tp_free */
4481 };
4482
4483
4484 typedef struct {
4485 PyObject_HEAD
4486 PyObject *ao_iterator;
4487 } awaitObject;
4488
4489
4490 static PyObject *
awaitObject_new(PyTypeObject * type,PyObject * args,PyObject * kwds)4491 awaitObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
4492 {
4493 PyObject *v;
4494 awaitObject *ao;
4495
4496 if (!PyArg_UnpackTuple(args, "awaitObject", 1, 1, &v))
4497 return NULL;
4498
4499 ao = (awaitObject *)type->tp_alloc(type, 0);
4500 if (ao == NULL) {
4501 return NULL;
4502 }
4503
4504 Py_INCREF(v);
4505 ao->ao_iterator = v;
4506
4507 return (PyObject *)ao;
4508 }
4509
4510
4511 static void
awaitObject_dealloc(awaitObject * ao)4512 awaitObject_dealloc(awaitObject *ao)
4513 {
4514 Py_CLEAR(ao->ao_iterator);
4515 Py_TYPE(ao)->tp_free(ao);
4516 }
4517
4518
4519 static PyObject *
awaitObject_await(awaitObject * ao)4520 awaitObject_await(awaitObject *ao)
4521 {
4522 Py_INCREF(ao->ao_iterator);
4523 return ao->ao_iterator;
4524 }
4525
4526 static PyAsyncMethods awaitType_as_async = {
4527 (unaryfunc)awaitObject_await, /* am_await */
4528 0, /* am_aiter */
4529 0 /* am_anext */
4530 };
4531
4532
4533 static PyTypeObject awaitType = {
4534 PyVarObject_HEAD_INIT(NULL, 0)
4535 "awaitType",
4536 sizeof(awaitObject), /* tp_basicsize */
4537 0, /* tp_itemsize */
4538 (destructor)awaitObject_dealloc, /* destructor tp_dealloc */
4539 0, /* tp_print */
4540 0, /* tp_getattr */
4541 0, /* tp_setattr */
4542 &awaitType_as_async, /* tp_as_async */
4543 0, /* tp_repr */
4544 0, /* tp_as_number */
4545 0, /* tp_as_sequence */
4546 0, /* tp_as_mapping */
4547 0, /* tp_hash */
4548 0, /* tp_call */
4549 0, /* tp_str */
4550 PyObject_GenericGetAttr, /* tp_getattro */
4551 PyObject_GenericSetAttr, /* tp_setattro */
4552 0, /* tp_as_buffer */
4553 0, /* tp_flags */
4554 "C level type with tp_as_async",
4555 0, /* traverseproc tp_traverse */
4556 0, /* tp_clear */
4557 0, /* tp_richcompare */
4558 0, /* tp_weaklistoffset */
4559 0, /* tp_iter */
4560 0, /* tp_iternext */
4561 0, /* tp_methods */
4562 0, /* tp_members */
4563 0,
4564 0,
4565 0,
4566 0,
4567 0,
4568 0,
4569 0,
4570 0,
4571 awaitObject_new, /* tp_new */
4572 PyObject_Del, /* tp_free */
4573 };
4574
4575
4576 static struct PyModuleDef _testcapimodule = {
4577 PyModuleDef_HEAD_INIT,
4578 "_testcapi",
4579 NULL,
4580 -1,
4581 TestMethods,
4582 NULL,
4583 NULL,
4584 NULL,
4585 NULL
4586 };
4587
4588 /* Per PEP 489, this module will not be converted to multi-phase initialization
4589 */
4590
4591 PyMODINIT_FUNC
PyInit__testcapi(void)4592 PyInit__testcapi(void)
4593 {
4594 PyObject *m;
4595
4596 m = PyModule_Create(&_testcapimodule);
4597 if (m == NULL)
4598 return NULL;
4599
4600 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
4601
4602 Py_TYPE(&test_structmembersType)=&PyType_Type;
4603 Py_INCREF(&test_structmembersType);
4604 /* don't use a name starting with "test", since we don't want
4605 test_capi to automatically call this */
4606 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
4607 if (PyType_Ready(&matmulType) < 0)
4608 return NULL;
4609 Py_INCREF(&matmulType);
4610 PyModule_AddObject(m, "matmulType", (PyObject *)&matmulType);
4611
4612 if (PyType_Ready(&awaitType) < 0)
4613 return NULL;
4614 Py_INCREF(&awaitType);
4615 PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType);
4616
4617 PyModule_AddObject(m, "CHAR_MAX", PyLong_FromLong(CHAR_MAX));
4618 PyModule_AddObject(m, "CHAR_MIN", PyLong_FromLong(CHAR_MIN));
4619 PyModule_AddObject(m, "UCHAR_MAX", PyLong_FromLong(UCHAR_MAX));
4620 PyModule_AddObject(m, "SHRT_MAX", PyLong_FromLong(SHRT_MAX));
4621 PyModule_AddObject(m, "SHRT_MIN", PyLong_FromLong(SHRT_MIN));
4622 PyModule_AddObject(m, "USHRT_MAX", PyLong_FromLong(USHRT_MAX));
4623 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
4624 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
4625 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
4626 PyModule_AddObject(m, "LONG_MAX", PyLong_FromLong(LONG_MAX));
4627 PyModule_AddObject(m, "LONG_MIN", PyLong_FromLong(LONG_MIN));
4628 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
4629 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
4630 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
4631 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
4632 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
4633 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
4634 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
4635 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
4636 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
4637 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
4638 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
4639 PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
4640 Py_INCREF(&PyInstanceMethod_Type);
4641 PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
4642
4643 PyModule_AddIntConstant(m, "the_number_three", 3);
4644
4645 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
4646 Py_INCREF(TestError);
4647 PyModule_AddObject(m, "error", TestError);
4648 return m;
4649 }
4650