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 #include "Python.h"
9 #include <float.h>
10 #include "structmember.h"
11 #include "datetime.h"
12
13 #ifdef WITH_THREAD
14 #include "pythread.h"
15 #endif /* WITH_THREAD */
16 static PyObject *TestError; /* set to exception object in init */
17
18 /* Raise TestError with test_name + ": " + msg, and return NULL. */
19
20 static PyObject *
raiseTestError(const char * test_name,const char * msg)21 raiseTestError(const char* test_name, const char* msg)
22 {
23 char buf[2048];
24
25 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
26 PyErr_SetString(TestError, "internal error msg too large");
27 else {
28 PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
29 PyErr_SetString(TestError, buf);
30 }
31 return NULL;
32 }
33
34 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
35
36 The ones derived from autoconf on the UNIX-like OSes can be relied
37 upon (in the absence of sloppy cross-compiling), but the Windows
38 platforms have these hardcoded. Better safe than sorry.
39 */
40 static PyObject*
sizeof_error(const char * fatname,const char * typname,int expected,int got)41 sizeof_error(const char* fatname, const char* typname,
42 int expected, int got)
43 {
44 char buf[1024];
45 PyOS_snprintf(buf, sizeof(buf),
46 "%.200s #define == %d but sizeof(%.200s) == %d",
47 fatname, expected, typname, got);
48 PyErr_SetString(TestError, buf);
49 return (PyObject*)NULL;
50 }
51
52 static PyObject*
test_config(PyObject * self)53 test_config(PyObject *self)
54 {
55 #define CHECK_SIZEOF(FATNAME, TYPE) \
56 if (FATNAME != sizeof(TYPE)) \
57 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
58
59 CHECK_SIZEOF(SIZEOF_SHORT, short);
60 CHECK_SIZEOF(SIZEOF_INT, int);
61 CHECK_SIZEOF(SIZEOF_LONG, long);
62 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
63 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
64 #ifdef HAVE_LONG_LONG
65 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
66 #endif
67
68 #undef CHECK_SIZEOF
69
70 Py_INCREF(Py_None);
71 return Py_None;
72 }
73
74 static PyObject*
test_list_api(PyObject * self)75 test_list_api(PyObject *self)
76 {
77 PyObject* list;
78 int i;
79
80 /* SF bug 132008: PyList_Reverse segfaults */
81 #define NLIST 30
82 list = PyList_New(NLIST);
83 if (list == (PyObject*)NULL)
84 return (PyObject*)NULL;
85 /* list = range(NLIST) */
86 for (i = 0; i < NLIST; ++i) {
87 PyObject* anint = PyInt_FromLong(i);
88 if (anint == (PyObject*)NULL) {
89 Py_DECREF(list);
90 return (PyObject*)NULL;
91 }
92 PyList_SET_ITEM(list, i, anint);
93 }
94 /* list.reverse(), via PyList_Reverse() */
95 i = PyList_Reverse(list); /* should not blow up! */
96 if (i != 0) {
97 Py_DECREF(list);
98 return (PyObject*)NULL;
99 }
100 /* Check that list == range(29, -1, -1) now */
101 for (i = 0; i < NLIST; ++i) {
102 PyObject* anint = PyList_GET_ITEM(list, i);
103 if (PyInt_AS_LONG(anint) != NLIST-1-i) {
104 PyErr_SetString(TestError,
105 "test_list_api: reverse screwed up");
106 Py_DECREF(list);
107 return (PyObject*)NULL;
108 }
109 }
110 Py_DECREF(list);
111 #undef NLIST
112
113 Py_INCREF(Py_None);
114 return Py_None;
115 }
116
117 static int
test_dict_inner(int count)118 test_dict_inner(int count)
119 {
120 Py_ssize_t pos = 0, iterations = 0;
121 int i;
122 PyObject *dict = PyDict_New();
123 PyObject *v, *k;
124
125 if (dict == NULL)
126 return -1;
127
128 for (i = 0; i < count; i++) {
129 v = PyInt_FromLong(i);
130 PyDict_SetItem(dict, v, v);
131 Py_DECREF(v);
132 }
133
134 while (PyDict_Next(dict, &pos, &k, &v)) {
135 PyObject *o;
136 iterations++;
137
138 i = PyInt_AS_LONG(v) + 1;
139 o = PyInt_FromLong(i);
140 if (o == NULL)
141 return -1;
142 if (PyDict_SetItem(dict, k, o) < 0) {
143 Py_DECREF(o);
144 return -1;
145 }
146 Py_DECREF(o);
147 }
148
149 Py_DECREF(dict);
150
151 if (iterations != count) {
152 PyErr_SetString(
153 TestError,
154 "test_dict_iteration: dict iteration went wrong ");
155 return -1;
156 } else {
157 return 0;
158 }
159 }
160
161 static PyObject*
test_dict_iteration(PyObject * self)162 test_dict_iteration(PyObject* self)
163 {
164 int i;
165
166 for (i = 0; i < 200; i++) {
167 if (test_dict_inner(i) < 0) {
168 return NULL;
169 }
170 }
171
172 Py_INCREF(Py_None);
173 return Py_None;
174 }
175
176
177 /* Issue #4701: Check that PyObject_Hash implicitly calls
178 * PyType_Ready if it hasn't already been called
179 */
180 static PyTypeObject _HashInheritanceTester_Type = {
181 PyObject_HEAD_INIT(NULL)
182 0, /* Number of items for varobject */
183 "hashinheritancetester", /* Name of this type */
184 sizeof(PyObject), /* Basic object size */
185 0, /* Item size for varobject */
186 (destructor)PyObject_Del, /* tp_dealloc */
187 0, /* tp_print */
188 0, /* tp_getattr */
189 0, /* tp_setattr */
190 0, /* tp_compare */
191 0, /* tp_repr */
192 0, /* tp_as_number */
193 0, /* tp_as_sequence */
194 0, /* tp_as_mapping */
195 0, /* tp_hash */
196 0, /* tp_call */
197 0, /* tp_str */
198 PyObject_GenericGetAttr, /* tp_getattro */
199 0, /* tp_setattro */
200 0, /* tp_as_buffer */
201 Py_TPFLAGS_DEFAULT, /* tp_flags */
202 0, /* tp_doc */
203 0, /* tp_traverse */
204 0, /* tp_clear */
205 0, /* tp_richcompare */
206 0, /* tp_weaklistoffset */
207 0, /* tp_iter */
208 0, /* tp_iternext */
209 0, /* tp_methods */
210 0, /* tp_members */
211 0, /* tp_getset */
212 0, /* tp_base */
213 0, /* tp_dict */
214 0, /* tp_descr_get */
215 0, /* tp_descr_set */
216 0, /* tp_dictoffset */
217 0, /* tp_init */
218 0, /* tp_alloc */
219 PyType_GenericNew, /* tp_new */
220 };
221
222 static PyObject*
test_lazy_hash_inheritance(PyObject * self)223 test_lazy_hash_inheritance(PyObject* self)
224 {
225 PyTypeObject *type;
226 PyObject *obj;
227 long hash;
228
229 type = &_HashInheritanceTester_Type;
230
231 if (type->tp_dict != NULL)
232 /* The type has already been initialized. This probably means
233 -R is being used. */
234 Py_RETURN_NONE;
235
236
237 obj = PyObject_New(PyObject, type);
238 if (obj == NULL) {
239 PyErr_Clear();
240 PyErr_SetString(
241 TestError,
242 "test_lazy_hash_inheritance: failed to create object");
243 return NULL;
244 }
245
246 if (type->tp_dict != NULL) {
247 PyErr_SetString(
248 TestError,
249 "test_lazy_hash_inheritance: type initialised too soon");
250 Py_DECREF(obj);
251 return NULL;
252 }
253
254 hash = PyObject_Hash(obj);
255 if ((hash == -1) && PyErr_Occurred()) {
256 PyErr_Clear();
257 PyErr_SetString(
258 TestError,
259 "test_lazy_hash_inheritance: could not hash object");
260 Py_DECREF(obj);
261 return NULL;
262 }
263
264 if (type->tp_dict == NULL) {
265 PyErr_SetString(
266 TestError,
267 "test_lazy_hash_inheritance: type not initialised by hash()");
268 Py_DECREF(obj);
269 return NULL;
270 }
271
272 if (type->tp_hash != PyType_Type.tp_hash) {
273 PyErr_SetString(
274 TestError,
275 "test_lazy_hash_inheritance: unexpected hash function");
276 Py_DECREF(obj);
277 return NULL;
278 }
279
280 Py_DECREF(obj);
281
282 Py_RETURN_NONE;
283 }
284
285
286 /* Issue #7385: Check that memoryview() does not crash
287 * when bf_getbuffer returns an error
288 */
289
290 static int
broken_buffer_getbuffer(PyObject * self,Py_buffer * view,int flags)291 broken_buffer_getbuffer(PyObject *self, Py_buffer *view, int flags)
292 {
293 PyErr_SetString(
294 TestError,
295 "test_broken_memoryview: expected error in bf_getbuffer");
296 return -1;
297 }
298
299 static PyBufferProcs memoryviewtester_as_buffer = {
300 0, /* bf_getreadbuffer */
301 0, /* bf_getwritebuffer */
302 0, /* bf_getsegcount */
303 0, /* bf_getcharbuffer */
304 (getbufferproc)broken_buffer_getbuffer, /* bf_getbuffer */
305 0, /* bf_releasebuffer */
306 };
307
308 static PyTypeObject _MemoryViewTester_Type = {
309 PyObject_HEAD_INIT(NULL)
310 0, /* Number of items for varobject */
311 "memoryviewtester", /* Name of this type */
312 sizeof(PyObject), /* Basic object size */
313 0, /* Item size for varobject */
314 (destructor)PyObject_Del, /* tp_dealloc */
315 0, /* tp_print */
316 0, /* tp_getattr */
317 0, /* tp_setattr */
318 0, /* tp_compare */
319 0, /* tp_repr */
320 0, /* tp_as_number */
321 0, /* tp_as_sequence */
322 0, /* tp_as_mapping */
323 0, /* tp_hash */
324 0, /* tp_call */
325 0, /* tp_str */
326 PyObject_GenericGetAttr, /* tp_getattro */
327 0, /* tp_setattro */
328 &memoryviewtester_as_buffer, /* tp_as_buffer */
329 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
330 0, /* tp_doc */
331 0, /* tp_traverse */
332 0, /* tp_clear */
333 0, /* tp_richcompare */
334 0, /* tp_weaklistoffset */
335 0, /* tp_iter */
336 0, /* tp_iternext */
337 0, /* tp_methods */
338 0, /* tp_members */
339 0, /* tp_getset */
340 0, /* tp_base */
341 0, /* tp_dict */
342 0, /* tp_descr_get */
343 0, /* tp_descr_set */
344 0, /* tp_dictoffset */
345 0, /* tp_init */
346 0, /* tp_alloc */
347 PyType_GenericNew, /* tp_new */
348 };
349
350 static PyObject*
test_broken_memoryview(PyObject * self)351 test_broken_memoryview(PyObject* self)
352 {
353 PyObject *obj = PyObject_New(PyObject, &_MemoryViewTester_Type);
354 PyObject *res;
355
356 if (obj == NULL) {
357 PyErr_Clear();
358 PyErr_SetString(
359 TestError,
360 "test_broken_memoryview: failed to create object");
361 return NULL;
362 }
363
364 res = PyMemoryView_FromObject(obj);
365 if (res || !PyErr_Occurred()){
366 PyErr_SetString(
367 TestError,
368 "test_broken_memoryview: memoryview() didn't raise an Exception");
369 Py_XDECREF(res);
370 Py_DECREF(obj);
371 return NULL;
372 }
373
374 PyErr_Clear();
375 Py_DECREF(obj);
376 Py_RETURN_NONE;
377 }
378
379
380 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
381 PyLong_{As, From}{Unsigned,}LongLong().
382
383 Note that the meat of the test is contained in testcapi_long.h.
384 This is revolting, but delicate code duplication is worse: "almost
385 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
386 dependence on type names makes it impossible to use a parameterized
387 function. A giant macro would be even worse than this. A C++ template
388 would be perfect.
389
390 The "report an error" functions are deliberately not part of the #include
391 file: if the test fails, you can set a breakpoint in the appropriate
392 error function directly, and crawl back from there in the debugger.
393 */
394
395 #define UNBIND(X) Py_DECREF(X); (X) = NULL
396
397 static PyObject *
raise_test_long_error(const char * msg)398 raise_test_long_error(const char* msg)
399 {
400 return raiseTestError("test_long_api", msg);
401 }
402
403 #define TESTNAME test_long_api_inner
404 #define TYPENAME long
405 #define F_S_TO_PY PyLong_FromLong
406 #define F_PY_TO_S PyLong_AsLong
407 #define F_U_TO_PY PyLong_FromUnsignedLong
408 #define F_PY_TO_U PyLong_AsUnsignedLong
409
410 #include "testcapi_long.h"
411
412 static PyObject *
test_long_api(PyObject * self)413 test_long_api(PyObject* self)
414 {
415 return TESTNAME(raise_test_long_error);
416 }
417
418 #undef TESTNAME
419 #undef TYPENAME
420 #undef F_S_TO_PY
421 #undef F_PY_TO_S
422 #undef F_U_TO_PY
423 #undef F_PY_TO_U
424
425 #ifdef HAVE_LONG_LONG
426
427 static PyObject *
raise_test_longlong_error(const char * msg)428 raise_test_longlong_error(const char* msg)
429 {
430 return raiseTestError("test_longlong_api", msg);
431 }
432
433 #define TESTNAME test_longlong_api_inner
434 #define TYPENAME PY_LONG_LONG
435 #define F_S_TO_PY PyLong_FromLongLong
436 #define F_PY_TO_S PyLong_AsLongLong
437 #define F_U_TO_PY PyLong_FromUnsignedLongLong
438 #define F_PY_TO_U PyLong_AsUnsignedLongLong
439
440 #include "testcapi_long.h"
441
442 static PyObject *
test_longlong_api(PyObject * self,PyObject * args)443 test_longlong_api(PyObject* self, PyObject *args)
444 {
445 return TESTNAME(raise_test_longlong_error);
446 }
447
448 #undef TESTNAME
449 #undef TYPENAME
450 #undef F_S_TO_PY
451 #undef F_PY_TO_S
452 #undef F_U_TO_PY
453 #undef F_PY_TO_U
454
455 /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
456 is tested by test_long_api_inner. This test will concentrate on proper
457 handling of overflow.
458 */
459
460 static PyObject *
test_long_and_overflow(PyObject * self)461 test_long_and_overflow(PyObject *self)
462 {
463 PyObject *num, *one, *temp;
464 long value;
465 int overflow;
466
467 /* Test that overflow is set properly for a large value. */
468 /* num is a number larger than LONG_MAX even on 64-bit platforms */
469 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
470 if (num == NULL)
471 return NULL;
472 overflow = 1234;
473 value = PyLong_AsLongAndOverflow(num, &overflow);
474 Py_DECREF(num);
475 if (value == -1 && PyErr_Occurred())
476 return NULL;
477 if (value != -1)
478 return raiseTestError("test_long_and_overflow",
479 "return value was not set to -1");
480 if (overflow != 1)
481 return raiseTestError("test_long_and_overflow",
482 "overflow was not set to 1");
483
484 /* Same again, with num = LONG_MAX + 1 */
485 num = PyLong_FromLong(LONG_MAX);
486 if (num == NULL)
487 return NULL;
488 one = PyLong_FromLong(1L);
489 if (one == NULL) {
490 Py_DECREF(num);
491 return NULL;
492 }
493 temp = PyNumber_Add(num, one);
494 Py_DECREF(one);
495 Py_DECREF(num);
496 num = temp;
497 if (num == NULL)
498 return NULL;
499 overflow = 0;
500 value = PyLong_AsLongAndOverflow(num, &overflow);
501 Py_DECREF(num);
502 if (value == -1 && PyErr_Occurred())
503 return NULL;
504 if (value != -1)
505 return raiseTestError("test_long_and_overflow",
506 "return value was not set to -1");
507 if (overflow != 1)
508 return raiseTestError("test_long_and_overflow",
509 "overflow was not set to 1");
510
511 /* Test that overflow is set properly for a large negative value. */
512 /* num is a number smaller than LONG_MIN even on 64-bit platforms */
513 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
514 if (num == NULL)
515 return NULL;
516 overflow = 1234;
517 value = PyLong_AsLongAndOverflow(num, &overflow);
518 Py_DECREF(num);
519 if (value == -1 && PyErr_Occurred())
520 return NULL;
521 if (value != -1)
522 return raiseTestError("test_long_and_overflow",
523 "return value was not set to -1");
524 if (overflow != -1)
525 return raiseTestError("test_long_and_overflow",
526 "overflow was not set to -1");
527
528 /* Same again, with num = LONG_MIN - 1 */
529 num = PyLong_FromLong(LONG_MIN);
530 if (num == NULL)
531 return NULL;
532 one = PyLong_FromLong(1L);
533 if (one == NULL) {
534 Py_DECREF(num);
535 return NULL;
536 }
537 temp = PyNumber_Subtract(num, one);
538 Py_DECREF(one);
539 Py_DECREF(num);
540 num = temp;
541 if (num == NULL)
542 return NULL;
543 overflow = 0;
544 value = PyLong_AsLongAndOverflow(num, &overflow);
545 Py_DECREF(num);
546 if (value == -1 && PyErr_Occurred())
547 return NULL;
548 if (value != -1)
549 return raiseTestError("test_long_and_overflow",
550 "return value was not set to -1");
551 if (overflow != -1)
552 return raiseTestError("test_long_and_overflow",
553 "overflow was not set to -1");
554
555 /* Test that overflow is cleared properly for small values. */
556 num = PyLong_FromString("FF", NULL, 16);
557 if (num == NULL)
558 return NULL;
559 overflow = 1234;
560 value = PyLong_AsLongAndOverflow(num, &overflow);
561 Py_DECREF(num);
562 if (value == -1 && PyErr_Occurred())
563 return NULL;
564 if (value != 0xFF)
565 return raiseTestError("test_long_and_overflow",
566 "expected return value 0xFF");
567 if (overflow != 0)
568 return raiseTestError("test_long_and_overflow",
569 "overflow was not cleared");
570
571 num = PyLong_FromString("-FF", NULL, 16);
572 if (num == NULL)
573 return NULL;
574 overflow = 0;
575 value = PyLong_AsLongAndOverflow(num, &overflow);
576 Py_DECREF(num);
577 if (value == -1 && PyErr_Occurred())
578 return NULL;
579 if (value != -0xFF)
580 return raiseTestError("test_long_and_overflow",
581 "expected return value 0xFF");
582 if (overflow != 0)
583 return raiseTestError("test_long_and_overflow",
584 "overflow was set incorrectly");
585
586 num = PyLong_FromLong(LONG_MAX);
587 if (num == NULL)
588 return NULL;
589 overflow = 1234;
590 value = PyLong_AsLongAndOverflow(num, &overflow);
591 Py_DECREF(num);
592 if (value == -1 && PyErr_Occurred())
593 return NULL;
594 if (value != LONG_MAX)
595 return raiseTestError("test_long_and_overflow",
596 "expected return value LONG_MAX");
597 if (overflow != 0)
598 return raiseTestError("test_long_and_overflow",
599 "overflow was not cleared");
600
601 num = PyLong_FromLong(LONG_MIN);
602 if (num == NULL)
603 return NULL;
604 overflow = 0;
605 value = PyLong_AsLongAndOverflow(num, &overflow);
606 Py_DECREF(num);
607 if (value == -1 && PyErr_Occurred())
608 return NULL;
609 if (value != LONG_MIN)
610 return raiseTestError("test_long_and_overflow",
611 "expected return value LONG_MIN");
612 if (overflow != 0)
613 return raiseTestError("test_long_and_overflow",
614 "overflow was not cleared");
615
616 Py_INCREF(Py_None);
617 return Py_None;
618 }
619
620 /* Test the PyLong_AsLongLongAndOverflow API. General conversion to
621 PY_LONG_LONG is tested by test_long_api_inner. This test will
622 concentrate on proper handling of overflow.
623 */
624
625 static PyObject *
test_long_long_and_overflow(PyObject * self)626 test_long_long_and_overflow(PyObject *self)
627 {
628 PyObject *num, *one, *temp;
629 PY_LONG_LONG value;
630 int overflow;
631
632 /* Test that overflow is set properly for a large value. */
633 /* num is a number larger than PY_LLONG_MAX on a typical machine. */
634 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
635 if (num == NULL)
636 return NULL;
637 overflow = 1234;
638 value = PyLong_AsLongLongAndOverflow(num, &overflow);
639 Py_DECREF(num);
640 if (value == -1 && PyErr_Occurred())
641 return NULL;
642 if (value != -1)
643 return raiseTestError("test_long_long_and_overflow",
644 "return value was not set to -1");
645 if (overflow != 1)
646 return raiseTestError("test_long_long_and_overflow",
647 "overflow was not set to 1");
648
649 /* Same again, with num = PY_LLONG_MAX + 1 */
650 num = PyLong_FromLongLong(PY_LLONG_MAX);
651 if (num == NULL)
652 return NULL;
653 one = PyLong_FromLong(1L);
654 if (one == NULL) {
655 Py_DECREF(num);
656 return NULL;
657 }
658 temp = PyNumber_Add(num, one);
659 Py_DECREF(one);
660 Py_DECREF(num);
661 num = temp;
662 if (num == NULL)
663 return NULL;
664 overflow = 0;
665 value = PyLong_AsLongLongAndOverflow(num, &overflow);
666 Py_DECREF(num);
667 if (value == -1 && PyErr_Occurred())
668 return NULL;
669 if (value != -1)
670 return raiseTestError("test_long_long_and_overflow",
671 "return value was not set to -1");
672 if (overflow != 1)
673 return raiseTestError("test_long_long_and_overflow",
674 "overflow was not set to 1");
675
676 /* Test that overflow is set properly for a large negative value. */
677 /* num is a number smaller than PY_LLONG_MIN on a typical platform */
678 num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
679 if (num == NULL)
680 return NULL;
681 overflow = 1234;
682 value = PyLong_AsLongLongAndOverflow(num, &overflow);
683 Py_DECREF(num);
684 if (value == -1 && PyErr_Occurred())
685 return NULL;
686 if (value != -1)
687 return raiseTestError("test_long_long_and_overflow",
688 "return value was not set to -1");
689 if (overflow != -1)
690 return raiseTestError("test_long_long_and_overflow",
691 "overflow was not set to -1");
692
693 /* Same again, with num = PY_LLONG_MIN - 1 */
694 num = PyLong_FromLongLong(PY_LLONG_MIN);
695 if (num == NULL)
696 return NULL;
697 one = PyLong_FromLong(1L);
698 if (one == NULL) {
699 Py_DECREF(num);
700 return NULL;
701 }
702 temp = PyNumber_Subtract(num, one);
703 Py_DECREF(one);
704 Py_DECREF(num);
705 num = temp;
706 if (num == NULL)
707 return NULL;
708 overflow = 0;
709 value = PyLong_AsLongLongAndOverflow(num, &overflow);
710 Py_DECREF(num);
711 if (value == -1 && PyErr_Occurred())
712 return NULL;
713 if (value != -1)
714 return raiseTestError("test_long_long_and_overflow",
715 "return value was not set to -1");
716 if (overflow != -1)
717 return raiseTestError("test_long_long_and_overflow",
718 "overflow was not set to -1");
719
720 /* Test that overflow is cleared properly for small values. */
721 num = PyLong_FromString("FF", NULL, 16);
722 if (num == NULL)
723 return NULL;
724 overflow = 1234;
725 value = PyLong_AsLongLongAndOverflow(num, &overflow);
726 Py_DECREF(num);
727 if (value == -1 && PyErr_Occurred())
728 return NULL;
729 if (value != 0xFF)
730 return raiseTestError("test_long_long_and_overflow",
731 "expected return value 0xFF");
732 if (overflow != 0)
733 return raiseTestError("test_long_long_and_overflow",
734 "overflow was not cleared");
735
736 num = PyLong_FromString("-FF", NULL, 16);
737 if (num == NULL)
738 return NULL;
739 overflow = 0;
740 value = PyLong_AsLongLongAndOverflow(num, &overflow);
741 Py_DECREF(num);
742 if (value == -1 && PyErr_Occurred())
743 return NULL;
744 if (value != -0xFF)
745 return raiseTestError("test_long_long_and_overflow",
746 "expected return value 0xFF");
747 if (overflow != 0)
748 return raiseTestError("test_long_long_and_overflow",
749 "overflow was set incorrectly");
750
751 num = PyLong_FromLongLong(PY_LLONG_MAX);
752 if (num == NULL)
753 return NULL;
754 overflow = 1234;
755 value = PyLong_AsLongLongAndOverflow(num, &overflow);
756 Py_DECREF(num);
757 if (value == -1 && PyErr_Occurred())
758 return NULL;
759 if (value != PY_LLONG_MAX)
760 return raiseTestError("test_long_long_and_overflow",
761 "expected return value PY_LLONG_MAX");
762 if (overflow != 0)
763 return raiseTestError("test_long_long_and_overflow",
764 "overflow was not cleared");
765
766 num = PyLong_FromLongLong(PY_LLONG_MIN);
767 if (num == NULL)
768 return NULL;
769 overflow = 0;
770 value = PyLong_AsLongLongAndOverflow(num, &overflow);
771 Py_DECREF(num);
772 if (value == -1 && PyErr_Occurred())
773 return NULL;
774 if (value != PY_LLONG_MIN)
775 return raiseTestError("test_long_long_and_overflow",
776 "expected return value PY_LLONG_MIN");
777 if (overflow != 0)
778 return raiseTestError("test_long_long_and_overflow",
779 "overflow was not cleared");
780
781 Py_INCREF(Py_None);
782 return Py_None;
783 }
784
785 /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
786 for both long and int arguments. The test may leak a little memory if
787 it fails.
788 */
789 static PyObject *
test_L_code(PyObject * self)790 test_L_code(PyObject *self)
791 {
792 PyObject *tuple, *num;
793 PY_LONG_LONG value;
794
795 tuple = PyTuple_New(1);
796 if (tuple == NULL)
797 return NULL;
798
799 num = PyLong_FromLong(42);
800 if (num == NULL)
801 return NULL;
802
803 PyTuple_SET_ITEM(tuple, 0, num);
804
805 value = -1;
806 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
807 return NULL;
808 if (value != 42)
809 return raiseTestError("test_L_code",
810 "L code returned wrong value for long 42");
811
812 Py_DECREF(num);
813 num = PyInt_FromLong(42);
814 if (num == NULL)
815 return NULL;
816
817 PyTuple_SET_ITEM(tuple, 0, num);
818
819 value = -1;
820 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
821 return NULL;
822 if (value != 42)
823 return raiseTestError("test_L_code",
824 "L code returned wrong value for int 42");
825
826 Py_DECREF(tuple);
827 Py_INCREF(Py_None);
828 return Py_None;
829 }
830
831 #endif /* ifdef HAVE_LONG_LONG */
832
833 /* Test tuple argument processing */
834 static PyObject *
getargs_tuple(PyObject * self,PyObject * args)835 getargs_tuple(PyObject *self, PyObject *args)
836 {
837 int a, b, c;
838 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
839 return NULL;
840 return Py_BuildValue("iii", a, b, c);
841 }
842
843 /* test PyArg_ParseTupleAndKeywords */
getargs_keywords(PyObject * self,PyObject * args,PyObject * kwargs)844 static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
845 {
846 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
847 static char *fmt="(ii)i|(i(ii))(iii)i";
848 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
849
850 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
851 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
852 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
853 return NULL;
854 return Py_BuildValue("iiiiiiiiii",
855 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
856 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
857 }
858
859 /* Functions to call PyArg_ParseTuple with integer format codes,
860 and return the result.
861 */
862 static PyObject *
getargs_b(PyObject * self,PyObject * args)863 getargs_b(PyObject *self, PyObject *args)
864 {
865 unsigned char value;
866 if (!PyArg_ParseTuple(args, "b", &value))
867 return NULL;
868 return PyLong_FromUnsignedLong((unsigned long)value);
869 }
870
871 static PyObject *
getargs_B(PyObject * self,PyObject * args)872 getargs_B(PyObject *self, PyObject *args)
873 {
874 unsigned char value;
875 if (!PyArg_ParseTuple(args, "B", &value))
876 return NULL;
877 return PyLong_FromUnsignedLong((unsigned long)value);
878 }
879
880 static PyObject *
getargs_h(PyObject * self,PyObject * args)881 getargs_h(PyObject *self, PyObject *args)
882 {
883 short value;
884 if (!PyArg_ParseTuple(args, "h", &value))
885 return NULL;
886 return PyLong_FromLong((long)value);
887 }
888
889 static PyObject *
getargs_H(PyObject * self,PyObject * args)890 getargs_H(PyObject *self, PyObject *args)
891 {
892 unsigned short value;
893 if (!PyArg_ParseTuple(args, "H", &value))
894 return NULL;
895 return PyLong_FromUnsignedLong((unsigned long)value);
896 }
897
898 static PyObject *
getargs_I(PyObject * self,PyObject * args)899 getargs_I(PyObject *self, PyObject *args)
900 {
901 unsigned int value;
902 if (!PyArg_ParseTuple(args, "I", &value))
903 return NULL;
904 return PyLong_FromUnsignedLong((unsigned long)value);
905 }
906
907 static PyObject *
getargs_k(PyObject * self,PyObject * args)908 getargs_k(PyObject *self, PyObject *args)
909 {
910 unsigned long value;
911 if (!PyArg_ParseTuple(args, "k", &value))
912 return NULL;
913 return PyLong_FromUnsignedLong(value);
914 }
915
916 static PyObject *
getargs_i(PyObject * self,PyObject * args)917 getargs_i(PyObject *self, PyObject *args)
918 {
919 int value;
920 if (!PyArg_ParseTuple(args, "i", &value))
921 return NULL;
922 return PyLong_FromLong((long)value);
923 }
924
925 static PyObject *
getargs_l(PyObject * self,PyObject * args)926 getargs_l(PyObject *self, PyObject *args)
927 {
928 long value;
929 if (!PyArg_ParseTuple(args, "l", &value))
930 return NULL;
931 return PyLong_FromLong(value);
932 }
933
934 static PyObject *
getargs_n(PyObject * self,PyObject * args)935 getargs_n(PyObject *self, PyObject *args)
936 {
937 Py_ssize_t value;
938 if (!PyArg_ParseTuple(args, "n", &value))
939 return NULL;
940 return PyInt_FromSsize_t(value);
941 }
942
943 #ifdef HAVE_LONG_LONG
944 static PyObject *
getargs_L(PyObject * self,PyObject * args)945 getargs_L(PyObject *self, PyObject *args)
946 {
947 PY_LONG_LONG value;
948 if (!PyArg_ParseTuple(args, "L", &value))
949 return NULL;
950 return PyLong_FromLongLong(value);
951 }
952
953 static PyObject *
getargs_K(PyObject * self,PyObject * args)954 getargs_K(PyObject *self, PyObject *args)
955 {
956 unsigned PY_LONG_LONG value;
957 if (!PyArg_ParseTuple(args, "K", &value))
958 return NULL;
959 return PyLong_FromUnsignedLongLong(value);
960 }
961 #endif
962
963 /* This function not only tests the 'k' getargs code, but also the
964 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
965 static PyObject *
test_k_code(PyObject * self)966 test_k_code(PyObject *self)
967 {
968 PyObject *tuple, *num;
969 unsigned long value;
970
971 tuple = PyTuple_New(1);
972 if (tuple == NULL)
973 return NULL;
974
975 /* a number larger than ULONG_MAX even on 64-bit platforms */
976 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
977 if (num == NULL)
978 return NULL;
979
980 value = PyInt_AsUnsignedLongMask(num);
981 if (value != ULONG_MAX)
982 return raiseTestError("test_k_code",
983 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
984
985 PyTuple_SET_ITEM(tuple, 0, num);
986
987 value = 0;
988 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
989 return NULL;
990 if (value != ULONG_MAX)
991 return raiseTestError("test_k_code",
992 "k code returned wrong value for long 0xFFF...FFF");
993
994 Py_DECREF(num);
995 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
996 if (num == NULL)
997 return NULL;
998
999 value = PyInt_AsUnsignedLongMask(num);
1000 if (value != (unsigned long)-0x42)
1001 return raiseTestError("test_k_code",
1002 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
1003
1004 PyTuple_SET_ITEM(tuple, 0, num);
1005
1006 value = 0;
1007 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
1008 return NULL;
1009 if (value != (unsigned long)-0x42)
1010 return raiseTestError("test_k_code",
1011 "k code returned wrong value for long -0xFFF..000042");
1012
1013 Py_DECREF(tuple);
1014 Py_INCREF(Py_None);
1015 return Py_None;
1016 }
1017
1018 #ifdef Py_USING_UNICODE
1019
1020 static volatile int x;
1021
1022 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
1023 of an error.
1024 */
1025 static PyObject *
test_u_code(PyObject * self)1026 test_u_code(PyObject *self)
1027 {
1028 PyObject *tuple, *obj;
1029 Py_UNICODE *value;
1030 int len;
1031
1032 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
1033 /* Just use the macro and check that it compiles */
1034 x = Py_UNICODE_ISSPACE(25);
1035
1036 tuple = PyTuple_New(1);
1037 if (tuple == NULL)
1038 return NULL;
1039
1040 obj = PyUnicode_Decode("test", strlen("test"),
1041 "ascii", NULL);
1042 if (obj == NULL)
1043 return NULL;
1044
1045 PyTuple_SET_ITEM(tuple, 0, obj);
1046
1047 value = 0;
1048 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
1049 return NULL;
1050 if (value != PyUnicode_AS_UNICODE(obj))
1051 return raiseTestError("test_u_code",
1052 "u code returned wrong value for u'test'");
1053 value = 0;
1054 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
1055 return NULL;
1056 if (value != PyUnicode_AS_UNICODE(obj) ||
1057 len != PyUnicode_GET_SIZE(obj))
1058 return raiseTestError("test_u_code",
1059 "u# code returned wrong values for u'test'");
1060
1061 Py_DECREF(tuple);
1062 Py_INCREF(Py_None);
1063 return Py_None;
1064 }
1065
1066 static PyObject *
test_widechar(PyObject * self)1067 test_widechar(PyObject *self)
1068 {
1069 #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
1070 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
1071 size_t wtextlen = 1;
1072 #else
1073 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
1074 size_t wtextlen = 2;
1075 #endif
1076 PyObject *wide, *utf8;
1077
1078 wide = PyUnicode_FromWideChar(wtext, wtextlen);
1079 if (wide == NULL)
1080 return NULL;
1081
1082 utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
1083 if (utf8 == NULL) {
1084 Py_DECREF(wide);
1085 return NULL;
1086 }
1087
1088 if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) {
1089 Py_DECREF(wide);
1090 Py_DECREF(utf8);
1091 return raiseTestError("test_widechar",
1092 "wide string and utf8 string have different length");
1093 }
1094 if (PyUnicode_Compare(wide, utf8)) {
1095 Py_DECREF(wide);
1096 Py_DECREF(utf8);
1097 if (PyErr_Occurred())
1098 return NULL;
1099 return raiseTestError("test_widechar",
1100 "wide string and utf8 string are differents");
1101 }
1102
1103 Py_DECREF(wide);
1104 Py_DECREF(utf8);
1105 Py_RETURN_NONE;
1106 }
1107
1108 static PyObject *
test_empty_argparse(PyObject * self)1109 test_empty_argparse(PyObject *self)
1110 {
1111 /* Test that formats can begin with '|'. See issue #4720. */
1112 PyObject *tuple, *dict = NULL;
1113 static char *kwlist[] = {NULL};
1114 int result;
1115 tuple = PyTuple_New(0);
1116 if (!tuple)
1117 return NULL;
1118 if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
1119 goto done;
1120 dict = PyDict_New();
1121 if (!dict)
1122 goto done;
1123 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
1124 done:
1125 Py_DECREF(tuple);
1126 Py_XDECREF(dict);
1127 if (result < 0)
1128 return NULL;
1129 else {
1130 Py_RETURN_NONE;
1131 }
1132 }
1133
1134 static PyObject *
codec_incrementalencoder(PyObject * self,PyObject * args)1135 codec_incrementalencoder(PyObject *self, PyObject *args)
1136 {
1137 const char *encoding, *errors = NULL;
1138 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
1139 &encoding, &errors))
1140 return NULL;
1141 return PyCodec_IncrementalEncoder(encoding, errors);
1142 }
1143
1144 static PyObject *
codec_incrementaldecoder(PyObject * self,PyObject * args)1145 codec_incrementaldecoder(PyObject *self, PyObject *args)
1146 {
1147 const char *encoding, *errors = NULL;
1148 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
1149 &encoding, &errors))
1150 return NULL;
1151 return PyCodec_IncrementalDecoder(encoding, errors);
1152 }
1153
1154 #endif
1155
1156 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
1157 static PyObject *
test_long_numbits(PyObject * self)1158 test_long_numbits(PyObject *self)
1159 {
1160 struct triple {
1161 long input;
1162 size_t nbits;
1163 int sign;
1164 } testcases[] = {{0, 0, 0},
1165 {1L, 1, 1},
1166 {-1L, 1, -1},
1167 {2L, 2, 1},
1168 {-2L, 2, -1},
1169 {3L, 2, 1},
1170 {-3L, 2, -1},
1171 {4L, 3, 1},
1172 {-4L, 3, -1},
1173 {0x7fffL, 15, 1}, /* one Python long digit */
1174 {-0x7fffL, 15, -1},
1175 {0xffffL, 16, 1},
1176 {-0xffffL, 16, -1},
1177 {0xfffffffL, 28, 1},
1178 {-0xfffffffL, 28, -1}};
1179 int i;
1180
1181 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
1182 PyObject *plong = PyLong_FromLong(testcases[i].input);
1183 size_t nbits = _PyLong_NumBits(plong);
1184 int sign = _PyLong_Sign(plong);
1185
1186 Py_DECREF(plong);
1187 if (nbits != testcases[i].nbits)
1188 return raiseTestError("test_long_numbits",
1189 "wrong result for _PyLong_NumBits");
1190 if (sign != testcases[i].sign)
1191 return raiseTestError("test_long_numbits",
1192 "wrong result for _PyLong_Sign");
1193 }
1194 Py_INCREF(Py_None);
1195 return Py_None;
1196 }
1197
1198 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
1199
1200 static PyObject *
test_null_strings(PyObject * self)1201 test_null_strings(PyObject *self)
1202 {
1203 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
1204 PyObject *tuple = PyTuple_Pack(2, o1, o2);
1205 Py_XDECREF(o1);
1206 Py_XDECREF(o2);
1207 return tuple;
1208 }
1209
1210 static PyObject *
raise_exception(PyObject * self,PyObject * args)1211 raise_exception(PyObject *self, PyObject *args)
1212 {
1213 PyObject *exc;
1214 PyObject *exc_args, *v;
1215 int num_args, i;
1216
1217 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
1218 &exc, &num_args))
1219 return NULL;
1220 if (!PyExceptionClass_Check(exc)) {
1221 PyErr_Format(PyExc_TypeError, "an exception class is required");
1222 return NULL;
1223 }
1224
1225 exc_args = PyTuple_New(num_args);
1226 if (exc_args == NULL)
1227 return NULL;
1228 for (i = 0; i < num_args; ++i) {
1229 v = PyInt_FromLong(i);
1230 if (v == NULL) {
1231 Py_DECREF(exc_args);
1232 return NULL;
1233 }
1234 PyTuple_SET_ITEM(exc_args, i, v);
1235 }
1236 PyErr_SetObject(exc, exc_args);
1237 Py_DECREF(exc_args);
1238 return NULL;
1239 }
1240
1241
1242 static int test_run_counter = 0;
1243
1244 static PyObject *
test_datetime_capi(PyObject * self,PyObject * args)1245 test_datetime_capi(PyObject *self, PyObject *args) {
1246 if (PyDateTimeAPI) {
1247 if (test_run_counter) {
1248 /* Probably regrtest.py -R */
1249 Py_RETURN_NONE;
1250 }
1251 else {
1252 PyErr_SetString(PyExc_AssertionError,
1253 "PyDateTime_CAPI somehow initialized");
1254 return NULL;
1255 }
1256 }
1257 test_run_counter++;
1258 PyDateTime_IMPORT;
1259 if (PyDateTimeAPI)
1260 Py_RETURN_NONE;
1261 else
1262 return NULL;
1263 }
1264
1265
1266 #ifdef WITH_THREAD
1267
1268 /* test_thread_state spawns a thread of its own, and that thread releases
1269 * `thread_done` when it's finished. The driver code has to know when the
1270 * thread finishes, because the thread uses a PyObject (the callable) that
1271 * may go away when the driver finishes. The former lack of this explicit
1272 * synchronization caused rare segfaults, so rare that they were seen only
1273 * on a Mac buildbot (although they were possible on any box).
1274 */
1275 static PyThread_type_lock thread_done = NULL;
1276
1277 static int
_make_call(void * callable)1278 _make_call(void *callable)
1279 {
1280 PyObject *rc;
1281 int success;
1282 PyGILState_STATE s = PyGILState_Ensure();
1283 rc = PyObject_CallFunction((PyObject *)callable, "");
1284 success = (rc != NULL);
1285 Py_XDECREF(rc);
1286 PyGILState_Release(s);
1287 return success;
1288 }
1289
1290 /* Same thing, but releases `thread_done` when it returns. This variant
1291 * should be called only from threads spawned by test_thread_state().
1292 */
1293 static void
_make_call_from_thread(void * callable)1294 _make_call_from_thread(void *callable)
1295 {
1296 _make_call(callable);
1297 PyThread_release_lock(thread_done);
1298 }
1299
1300 static PyObject *
test_thread_state(PyObject * self,PyObject * args)1301 test_thread_state(PyObject *self, PyObject *args)
1302 {
1303 PyObject *fn;
1304 int success = 1;
1305
1306 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
1307 return NULL;
1308
1309 if (!PyCallable_Check(fn)) {
1310 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
1311 fn->ob_type->tp_name);
1312 return NULL;
1313 }
1314
1315 /* Ensure Python is set up for threading */
1316 PyEval_InitThreads();
1317 thread_done = PyThread_allocate_lock();
1318 if (thread_done == NULL)
1319 return PyErr_NoMemory();
1320 PyThread_acquire_lock(thread_done, 1);
1321
1322 /* Start a new thread with our callback. */
1323 PyThread_start_new_thread(_make_call_from_thread, fn);
1324 /* Make the callback with the thread lock held by this thread */
1325 success &= _make_call(fn);
1326 /* Do it all again, but this time with the thread-lock released */
1327 Py_BEGIN_ALLOW_THREADS
1328 success &= _make_call(fn);
1329 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1330 Py_END_ALLOW_THREADS
1331
1332 /* And once more with and without a thread
1333 XXX - should use a lock and work out exactly what we are trying
1334 to test <wink>
1335 */
1336 Py_BEGIN_ALLOW_THREADS
1337 PyThread_start_new_thread(_make_call_from_thread, fn);
1338 success &= _make_call(fn);
1339 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1340 Py_END_ALLOW_THREADS
1341
1342 /* Release lock we acquired above. This is required on HP-UX. */
1343 PyThread_release_lock(thread_done);
1344
1345 PyThread_free_lock(thread_done);
1346 if (!success)
1347 return NULL;
1348 Py_RETURN_NONE;
1349 }
1350
1351 /* test Py_AddPendingCalls using threads */
_pending_callback(void * arg)1352 static int _pending_callback(void *arg)
1353 {
1354 /* we assume the argument is callable object to which we own a reference */
1355 PyObject *callable = (PyObject *)arg;
1356 PyObject *r = PyObject_CallObject(callable, NULL);
1357 Py_DECREF(callable);
1358 Py_XDECREF(r);
1359 return r != NULL ? 0 : -1;
1360 }
1361
1362 /* The following requests n callbacks to _pending_callback. It can be
1363 * run from any python thread.
1364 */
pending_threadfunc(PyObject * self,PyObject * arg)1365 PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
1366 {
1367 PyObject *callable;
1368 int r;
1369 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
1370 return NULL;
1371
1372 /* create the reference for the callbackwhile we hold the lock */
1373 Py_INCREF(callable);
1374
1375 Py_BEGIN_ALLOW_THREADS
1376 r = Py_AddPendingCall(&_pending_callback, callable);
1377 Py_END_ALLOW_THREADS
1378
1379 if (r<0) {
1380 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
1381 Py_INCREF(Py_False);
1382 return Py_False;
1383 }
1384 Py_INCREF(Py_True);
1385 return Py_True;
1386 }
1387 #endif
1388
1389 /* Some tests of PyString_FromFormat(). This needs more tests. */
1390 static PyObject *
test_string_from_format(PyObject * self,PyObject * args)1391 test_string_from_format(PyObject *self, PyObject *args)
1392 {
1393 PyObject *result;
1394 char *msg;
1395
1396 #define CHECK_1_FORMAT(FORMAT, TYPE) \
1397 result = PyString_FromFormat(FORMAT, (TYPE)1); \
1398 if (result == NULL) \
1399 return NULL; \
1400 if (strcmp(PyString_AsString(result), "1")) { \
1401 msg = FORMAT " failed at 1"; \
1402 goto Fail; \
1403 } \
1404 Py_DECREF(result)
1405
1406 CHECK_1_FORMAT("%d", int);
1407 CHECK_1_FORMAT("%ld", long);
1408 /* The z width modifier was added in Python 2.5. */
1409 CHECK_1_FORMAT("%zd", Py_ssize_t);
1410
1411 /* The u type code was added in Python 2.5. */
1412 CHECK_1_FORMAT("%u", unsigned int);
1413 CHECK_1_FORMAT("%lu", unsigned long);
1414 CHECK_1_FORMAT("%zu", size_t);
1415
1416 /* "%lld" and "%llu" support added in Python 2.7. */
1417 #ifdef HAVE_LONG_LONG
1418 CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG);
1419 CHECK_1_FORMAT("%lld", PY_LONG_LONG);
1420 #endif
1421
1422 Py_RETURN_NONE;
1423
1424 Fail:
1425 Py_XDECREF(result);
1426 return raiseTestError("test_string_from_format", msg);
1427
1428 #undef CHECK_1_FORMAT
1429 }
1430
1431 /* Coverage testing of capsule objects. */
1432
1433 static const char *capsule_name = "capsule name";
1434 static char *capsule_pointer = "capsule pointer";
1435 static char *capsule_context = "capsule context";
1436 static const char *capsule_error = NULL;
1437 static int
1438 capsule_destructor_call_count = 0;
1439
1440 static void
capsule_destructor(PyObject * o)1441 capsule_destructor(PyObject *o) {
1442 capsule_destructor_call_count++;
1443 if (PyCapsule_GetContext(o) != capsule_context) {
1444 capsule_error = "context did not match in destructor!";
1445 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
1446 capsule_error = "destructor did not match in destructor! (woah!)";
1447 } else if (PyCapsule_GetName(o) != capsule_name) {
1448 capsule_error = "name did not match in destructor!";
1449 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
1450 capsule_error = "pointer did not match in destructor!";
1451 }
1452 }
1453
1454 typedef struct {
1455 char *name;
1456 char *module;
1457 char *attribute;
1458 } known_capsule;
1459
1460 static PyObject *
test_capsule(PyObject * self,PyObject * args)1461 test_capsule(PyObject *self, PyObject *args)
1462 {
1463 PyObject *object;
1464 const char *error = NULL;
1465 void *pointer;
1466 void *pointer2;
1467 known_capsule known_capsules[] = {
1468 #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
1469 KNOWN_CAPSULE("_socket", "CAPI"),
1470 KNOWN_CAPSULE("_curses", "_C_API"),
1471 KNOWN_CAPSULE("datetime", "datetime_CAPI"),
1472 { NULL, NULL },
1473 };
1474 known_capsule *known = &known_capsules[0];
1475
1476 #define FAIL(x) { error = (x); goto exit; }
1477
1478 #define CHECK_DESTRUCTOR \
1479 if (capsule_error) { \
1480 FAIL(capsule_error); \
1481 } \
1482 else if (!capsule_destructor_call_count) { \
1483 FAIL("destructor not called!"); \
1484 } \
1485 capsule_destructor_call_count = 0; \
1486
1487 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
1488 PyCapsule_SetContext(object, capsule_context);
1489 capsule_destructor(object);
1490 CHECK_DESTRUCTOR;
1491 Py_DECREF(object);
1492 CHECK_DESTRUCTOR;
1493
1494 object = PyCapsule_New(known, "ignored", NULL);
1495 PyCapsule_SetPointer(object, capsule_pointer);
1496 PyCapsule_SetName(object, capsule_name);
1497 PyCapsule_SetDestructor(object, capsule_destructor);
1498 PyCapsule_SetContext(object, capsule_context);
1499 capsule_destructor(object);
1500 CHECK_DESTRUCTOR;
1501 /* intentionally access using the wrong name */
1502 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
1503 if (!PyErr_Occurred()) {
1504 FAIL("PyCapsule_GetPointer should have failed but did not!");
1505 }
1506 PyErr_Clear();
1507 if (pointer2) {
1508 if (pointer2 == capsule_pointer) {
1509 FAIL("PyCapsule_GetPointer should not have"
1510 " returned the internal pointer!");
1511 } else {
1512 FAIL("PyCapsule_GetPointer should have "
1513 "returned NULL pointer but did not!");
1514 }
1515 }
1516 PyCapsule_SetDestructor(object, NULL);
1517 Py_DECREF(object);
1518 if (capsule_destructor_call_count) {
1519 FAIL("destructor called when it should not have been!");
1520 }
1521
1522 for (known = &known_capsules[0]; known->module != NULL; known++) {
1523 /* yeah, ordinarily I wouldn't do this either,
1524 but it's fine for this test harness.
1525 */
1526 static char buffer[256];
1527 #undef FAIL
1528 #define FAIL(x) \
1529 { \
1530 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
1531 x, known->module, known->attribute); \
1532 error = buffer; \
1533 goto exit; \
1534 } \
1535
1536 PyObject *module = PyImport_ImportModule(known->module);
1537 if (module) {
1538 pointer = PyCapsule_Import(known->name, 0);
1539 if (!pointer) {
1540 Py_DECREF(module);
1541 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
1542 }
1543 object = PyObject_GetAttrString(module, known->attribute);
1544 if (!object) {
1545 Py_DECREF(module);
1546 return NULL;
1547 }
1548 pointer2 = PyCapsule_GetPointer(object,
1549 "weebles wobble but they don't fall down");
1550 if (!PyErr_Occurred()) {
1551 Py_DECREF(object);
1552 Py_DECREF(module);
1553 FAIL("PyCapsule_GetPointer should have failed but did not!");
1554 }
1555 PyErr_Clear();
1556 if (pointer2) {
1557 Py_DECREF(module);
1558 Py_DECREF(object);
1559 if (pointer2 == pointer) {
1560 FAIL("PyCapsule_GetPointer should not have"
1561 " returned its internal pointer!");
1562 } else {
1563 FAIL("PyCapsule_GetPointer should have"
1564 " returned NULL pointer but did not!");
1565 }
1566 }
1567 Py_DECREF(object);
1568 Py_DECREF(module);
1569 }
1570 else
1571 PyErr_Clear();
1572 }
1573
1574 exit:
1575 if (error) {
1576 return raiseTestError("test_capsule", error);
1577 }
1578 Py_RETURN_NONE;
1579 #undef FAIL
1580 }
1581
1582 /* This is here to provide a docstring for test_descr. */
1583 static PyObject *
test_with_docstring(PyObject * self)1584 test_with_docstring(PyObject *self)
1585 {
1586 Py_RETURN_NONE;
1587 }
1588
1589 /* To test the format of tracebacks as printed out. */
1590 static PyObject *
traceback_print(PyObject * self,PyObject * args)1591 traceback_print(PyObject *self, PyObject *args)
1592 {
1593 PyObject *file;
1594 PyObject *traceback;
1595 int result;
1596
1597 if (!PyArg_ParseTuple(args, "OO:traceback_print",
1598 &traceback, &file))
1599 return NULL;
1600
1601 result = PyTraceBack_Print(traceback, file);
1602 if (result < 0)
1603 return NULL;
1604 Py_RETURN_NONE;
1605 }
1606
1607 /* To test that the result of PyCode_NewEmpty has the right members. */
1608 static PyObject *
code_newempty(PyObject * self,PyObject * args)1609 code_newempty(PyObject *self, PyObject *args)
1610 {
1611 const char *filename;
1612 const char *funcname;
1613 int firstlineno;
1614
1615 if (!PyArg_ParseTuple(args, "ssi:code_newempty",
1616 &filename, &funcname, &firstlineno))
1617 return NULL;
1618
1619 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
1620 }
1621
1622 /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
1623 Run via Lib/test/test_exceptions.py */
1624 static PyObject *
make_exception_with_doc(PyObject * self,PyObject * args,PyObject * kwargs)1625 make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
1626 {
1627 char *name;
1628 char *doc = NULL;
1629 PyObject *base = NULL;
1630 PyObject *dict = NULL;
1631
1632 static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
1633
1634 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
1635 "s|sOO:make_exception_with_doc", kwlist,
1636 &name, &doc, &base, &dict))
1637 return NULL;
1638
1639 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
1640 }
1641
1642 static PyMethodDef TestMethods[] = {
1643 {"raise_exception", raise_exception, METH_VARARGS},
1644 {"test_config", (PyCFunction)test_config, METH_NOARGS},
1645 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
1646 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
1647 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
1648 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
1649 {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS},
1650 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
1651 {"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
1652 METH_NOARGS},
1653 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
1654 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
1655 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
1656 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
1657 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
1658 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
1659 PyDoc_STR("This is a pretty normal docstring.")},
1660
1661 {"getargs_tuple", getargs_tuple, METH_VARARGS},
1662 {"getargs_keywords", (PyCFunction)getargs_keywords,
1663 METH_VARARGS|METH_KEYWORDS},
1664 {"getargs_b", getargs_b, METH_VARARGS},
1665 {"getargs_B", getargs_B, METH_VARARGS},
1666 {"getargs_h", getargs_h, METH_VARARGS},
1667 {"getargs_H", getargs_H, METH_VARARGS},
1668 {"getargs_I", getargs_I, METH_VARARGS},
1669 {"getargs_k", getargs_k, METH_VARARGS},
1670 {"getargs_i", getargs_i, METH_VARARGS},
1671 {"getargs_l", getargs_l, METH_VARARGS},
1672 {"getargs_n", getargs_n, METH_VARARGS},
1673 #ifdef HAVE_LONG_LONG
1674 {"getargs_L", getargs_L, METH_VARARGS},
1675 {"getargs_K", getargs_K, METH_VARARGS},
1676 {"test_longlong_api", test_longlong_api, METH_NOARGS},
1677 {"test_long_long_and_overflow",
1678 (PyCFunction)test_long_long_and_overflow, METH_NOARGS},
1679 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
1680 {"codec_incrementalencoder",
1681 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
1682 {"codec_incrementaldecoder",
1683 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
1684 #endif
1685 #ifdef Py_USING_UNICODE
1686 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
1687 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
1688 #endif
1689 #ifdef WITH_THREAD
1690 {"_test_thread_state", test_thread_state, METH_VARARGS},
1691 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
1692 #endif
1693 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
1694 {"traceback_print", traceback_print, METH_VARARGS},
1695 {"code_newempty", code_newempty, METH_VARARGS},
1696 {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
1697 METH_VARARGS | METH_KEYWORDS},
1698 {NULL, NULL} /* sentinel */
1699 };
1700
1701 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
1702
1703 typedef struct {
1704 char bool_member;
1705 char byte_member;
1706 unsigned char ubyte_member;
1707 short short_member;
1708 unsigned short ushort_member;
1709 int int_member;
1710 unsigned int uint_member;
1711 long long_member;
1712 unsigned long ulong_member;
1713 float float_member;
1714 double double_member;
1715 char inplace_member[6];
1716 #ifdef HAVE_LONG_LONG
1717 PY_LONG_LONG longlong_member;
1718 unsigned PY_LONG_LONG ulonglong_member;
1719 #endif
1720 } all_structmembers;
1721
1722 typedef struct {
1723 PyObject_HEAD
1724 all_structmembers structmembers;
1725 } test_structmembers;
1726
1727 static struct PyMemberDef test_members[] = {
1728 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
1729 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
1730 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
1731 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
1732 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
1733 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
1734 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
1735 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
1736 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
1737 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
1738 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
1739 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
1740 #ifdef HAVE_LONG_LONG
1741 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
1742 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
1743 #endif
1744 {NULL}
1745 };
1746
1747
1748 static PyObject *
test_structmembers_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)1749 test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1750 {
1751 static char *keywords[] = {
1752 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
1753 "T_INT", "T_UINT", "T_LONG", "T_ULONG",
1754 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
1755 #ifdef HAVE_LONG_LONG
1756 "T_LONGLONG", "T_ULONGLONG",
1757 #endif
1758 NULL};
1759 static char *fmt = "|bbBhHiIlkfds#"
1760 #ifdef HAVE_LONG_LONG
1761 "LK"
1762 #endif
1763 ;
1764 test_structmembers *ob;
1765 const char *s = NULL;
1766 Py_ssize_t string_len = 0;
1767 ob = PyObject_New(test_structmembers, type);
1768 if (ob == NULL)
1769 return NULL;
1770 memset(&ob->structmembers, 0, sizeof(all_structmembers));
1771 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
1772 &ob->structmembers.bool_member,
1773 &ob->structmembers.byte_member,
1774 &ob->structmembers.ubyte_member,
1775 &ob->structmembers.short_member,
1776 &ob->structmembers.ushort_member,
1777 &ob->structmembers.int_member,
1778 &ob->structmembers.uint_member,
1779 &ob->structmembers.long_member,
1780 &ob->structmembers.ulong_member,
1781 &ob->structmembers.float_member,
1782 &ob->structmembers.double_member,
1783 &s, &string_len
1784 #ifdef HAVE_LONG_LONG
1785 , &ob->structmembers.longlong_member,
1786 &ob->structmembers.ulonglong_member
1787 #endif
1788 )) {
1789 Py_DECREF(ob);
1790 return NULL;
1791 }
1792 if (s != NULL) {
1793 if (string_len > 5) {
1794 Py_DECREF(ob);
1795 PyErr_SetString(PyExc_ValueError, "string too long");
1796 return NULL;
1797 }
1798 strcpy(ob->structmembers.inplace_member, s);
1799 }
1800 else {
1801 strcpy(ob->structmembers.inplace_member, "");
1802 }
1803 return (PyObject *)ob;
1804 }
1805
1806 static void
test_structmembers_free(PyObject * ob)1807 test_structmembers_free(PyObject *ob)
1808 {
1809 PyObject_FREE(ob);
1810 }
1811
1812 static PyTypeObject test_structmembersType = {
1813 PyVarObject_HEAD_INIT(NULL, 0)
1814 "test_structmembersType",
1815 sizeof(test_structmembers), /* tp_basicsize */
1816 0, /* tp_itemsize */
1817 test_structmembers_free, /* destructor tp_dealloc */
1818 0, /* tp_print */
1819 0, /* tp_getattr */
1820 0, /* tp_setattr */
1821 0, /* tp_compare */
1822 0, /* tp_repr */
1823 0, /* tp_as_number */
1824 0, /* tp_as_sequence */
1825 0, /* tp_as_mapping */
1826 0, /* tp_hash */
1827 0, /* tp_call */
1828 0, /* tp_str */
1829 PyObject_GenericGetAttr, /* tp_getattro */
1830 PyObject_GenericSetAttr, /* tp_setattro */
1831 0, /* tp_as_buffer */
1832 0, /* tp_flags */
1833 "Type containing all structmember types",
1834 0, /* traverseproc tp_traverse */
1835 0, /* tp_clear */
1836 0, /* tp_richcompare */
1837 0, /* tp_weaklistoffset */
1838 0, /* tp_iter */
1839 0, /* tp_iternext */
1840 0, /* tp_methods */
1841 test_members, /* tp_members */
1842 0,
1843 0,
1844 0,
1845 0,
1846 0,
1847 0,
1848 0,
1849 0,
1850 test_structmembers_new, /* tp_new */
1851 };
1852
1853
1854 PyMODINIT_FUNC
init_testcapi(void)1855 init_testcapi(void)
1856 {
1857 PyObject *m;
1858
1859 m = Py_InitModule("_testcapi", TestMethods);
1860 if (m == NULL)
1861 return;
1862
1863 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
1864
1865 Py_TYPE(&test_structmembersType)=&PyType_Type;
1866 Py_INCREF(&test_structmembersType);
1867 /* don't use a name starting with "test", since we don't want
1868 test_capi to automatically call this */
1869 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
1870
1871 PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
1872 PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));
1873 PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
1874 PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX));
1875 PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN));
1876 PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
1877 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
1878 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
1879 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
1880 PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
1881 PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
1882 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
1883 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
1884 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
1885 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
1886 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
1887 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
1888 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
1889 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
1890 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
1891 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
1892 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head)));
1893
1894 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
1895 Py_INCREF(TestError);
1896 PyModule_AddObject(m, "error", TestError);
1897 }
1898