No issues found
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 *
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*
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*
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*
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
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*
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*
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
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*
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 */
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
1109 unicode_encodedecimal(PyObject *self, PyObject *args)
1110 {
1111 Py_UNICODE *unicode;
1112 int length;
1113 char *errors = NULL;
1114 PyObject *decimal;
1115 Py_ssize_t decimal_length, new_length;
1116 int res;
1117
1118 if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
1119 return NULL;
1120
1121 decimal_length = length * 7; /* len('€') */
1122 decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
1123 if (decimal == NULL)
1124 return NULL;
1125
1126 res = PyUnicode_EncodeDecimal(unicode, length,
1127 PyBytes_AS_STRING(decimal),
1128 errors);
1129 if (res < 0) {
1130 Py_DECREF(decimal);
1131 return NULL;
1132 }
1133
1134 new_length = strlen(PyBytes_AS_STRING(decimal));
1135 assert(new_length <= decimal_length);
1136 res = _PyBytes_Resize(&decimal, new_length);
1137 if (res < 0)
1138 return NULL;
1139
1140 return decimal;
1141 }
1142
1143 static PyObject *
1144 test_empty_argparse(PyObject *self)
1145 {
1146 /* Test that formats can begin with '|'. See issue #4720. */
1147 PyObject *tuple, *dict = NULL;
1148 static char *kwlist[] = {NULL};
1149 int result;
1150 tuple = PyTuple_New(0);
1151 if (!tuple)
1152 return NULL;
1153 if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
1154 goto done;
1155 dict = PyDict_New();
1156 if (!dict)
1157 goto done;
1158 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
1159 done:
1160 Py_DECREF(tuple);
1161 Py_XDECREF(dict);
1162 if (result < 0)
1163 return NULL;
1164 else {
1165 Py_RETURN_NONE;
1166 }
1167 }
1168
1169 static PyObject *
1170 codec_incrementalencoder(PyObject *self, PyObject *args)
1171 {
1172 const char *encoding, *errors = NULL;
1173 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
1174 &encoding, &errors))
1175 return NULL;
1176 return PyCodec_IncrementalEncoder(encoding, errors);
1177 }
1178
1179 static PyObject *
1180 codec_incrementaldecoder(PyObject *self, PyObject *args)
1181 {
1182 const char *encoding, *errors = NULL;
1183 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
1184 &encoding, &errors))
1185 return NULL;
1186 return PyCodec_IncrementalDecoder(encoding, errors);
1187 }
1188
1189 #endif
1190
1191 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
1192 static PyObject *
1193 test_long_numbits(PyObject *self)
1194 {
1195 struct triple {
1196 long input;
1197 size_t nbits;
1198 int sign;
1199 } testcases[] = {{0, 0, 0},
1200 {1L, 1, 1},
1201 {-1L, 1, -1},
1202 {2L, 2, 1},
1203 {-2L, 2, -1},
1204 {3L, 2, 1},
1205 {-3L, 2, -1},
1206 {4L, 3, 1},
1207 {-4L, 3, -1},
1208 {0x7fffL, 15, 1}, /* one Python long digit */
1209 {-0x7fffL, 15, -1},
1210 {0xffffL, 16, 1},
1211 {-0xffffL, 16, -1},
1212 {0xfffffffL, 28, 1},
1213 {-0xfffffffL, 28, -1}};
1214 int i;
1215
1216 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
1217 PyObject *plong = PyLong_FromLong(testcases[i].input);
1218 size_t nbits = _PyLong_NumBits(plong);
1219 int sign = _PyLong_Sign(plong);
1220
1221 Py_DECREF(plong);
1222 if (nbits != testcases[i].nbits)
1223 return raiseTestError("test_long_numbits",
1224 "wrong result for _PyLong_NumBits");
1225 if (sign != testcases[i].sign)
1226 return raiseTestError("test_long_numbits",
1227 "wrong result for _PyLong_Sign");
1228 }
1229 Py_INCREF(Py_None);
1230 return Py_None;
1231 }
1232
1233 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
1234
1235 static PyObject *
1236 test_null_strings(PyObject *self)
1237 {
1238 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
1239 PyObject *tuple = PyTuple_Pack(2, o1, o2);
1240 Py_XDECREF(o1);
1241 Py_XDECREF(o2);
1242 return tuple;
1243 }
1244
1245 static PyObject *
1246 raise_exception(PyObject *self, PyObject *args)
1247 {
1248 PyObject *exc;
1249 PyObject *exc_args, *v;
1250 int num_args, i;
1251
1252 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
1253 &exc, &num_args))
1254 return NULL;
1255 if (!PyExceptionClass_Check(exc)) {
1256 PyErr_Format(PyExc_TypeError, "an exception class is required");
1257 return NULL;
1258 }
1259
1260 exc_args = PyTuple_New(num_args);
1261 if (exc_args == NULL)
1262 return NULL;
1263 for (i = 0; i < num_args; ++i) {
1264 v = PyInt_FromLong(i);
1265 if (v == NULL) {
1266 Py_DECREF(exc_args);
1267 return NULL;
1268 }
1269 PyTuple_SET_ITEM(exc_args, i, v);
1270 }
1271 PyErr_SetObject(exc, exc_args);
1272 Py_DECREF(exc_args);
1273 return NULL;
1274 }
1275
1276
1277 static int test_run_counter = 0;
1278
1279 static PyObject *
1280 test_datetime_capi(PyObject *self, PyObject *args) {
1281 if (PyDateTimeAPI) {
1282 if (test_run_counter) {
1283 /* Probably regrtest.py -R */
1284 Py_RETURN_NONE;
1285 }
1286 else {
1287 PyErr_SetString(PyExc_AssertionError,
1288 "PyDateTime_CAPI somehow initialized");
1289 return NULL;
1290 }
1291 }
1292 test_run_counter++;
1293 PyDateTime_IMPORT;
1294 if (PyDateTimeAPI)
1295 Py_RETURN_NONE;
1296 else
1297 return NULL;
1298 }
1299
1300
1301 #ifdef WITH_THREAD
1302
1303 /* test_thread_state spawns a thread of its own, and that thread releases
1304 * `thread_done` when it's finished. The driver code has to know when the
1305 * thread finishes, because the thread uses a PyObject (the callable) that
1306 * may go away when the driver finishes. The former lack of this explicit
1307 * synchronization caused rare segfaults, so rare that they were seen only
1308 * on a Mac buildbot (although they were possible on any box).
1309 */
1310 static PyThread_type_lock thread_done = NULL;
1311
1312 static int
1313 _make_call(void *callable)
1314 {
1315 PyObject *rc;
1316 int success;
1317 PyGILState_STATE s = PyGILState_Ensure();
1318 rc = PyObject_CallFunction((PyObject *)callable, "");
1319 success = (rc != NULL);
1320 Py_XDECREF(rc);
1321 PyGILState_Release(s);
1322 return success;
1323 }
1324
1325 /* Same thing, but releases `thread_done` when it returns. This variant
1326 * should be called only from threads spawned by test_thread_state().
1327 */
1328 static void
1329 _make_call_from_thread(void *callable)
1330 {
1331 _make_call(callable);
1332 PyThread_release_lock(thread_done);
1333 }
1334
1335 static PyObject *
1336 test_thread_state(PyObject *self, PyObject *args)
1337 {
1338 PyObject *fn;
1339 int success = 1;
1340
1341 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
1342 return NULL;
1343
1344 if (!PyCallable_Check(fn)) {
1345 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
1346 fn->ob_type->tp_name);
1347 return NULL;
1348 }
1349
1350 /* Ensure Python is set up for threading */
1351 PyEval_InitThreads();
1352 thread_done = PyThread_allocate_lock();
1353 if (thread_done == NULL)
1354 return PyErr_NoMemory();
1355 PyThread_acquire_lock(thread_done, 1);
1356
1357 /* Start a new thread with our callback. */
1358 PyThread_start_new_thread(_make_call_from_thread, fn);
1359 /* Make the callback with the thread lock held by this thread */
1360 success &= _make_call(fn);
1361 /* Do it all again, but this time with the thread-lock released */
1362 Py_BEGIN_ALLOW_THREADS
1363 success &= _make_call(fn);
1364 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1365 Py_END_ALLOW_THREADS
1366
1367 /* And once more with and without a thread
1368 XXX - should use a lock and work out exactly what we are trying
1369 to test <wink>
1370 */
1371 Py_BEGIN_ALLOW_THREADS
1372 PyThread_start_new_thread(_make_call_from_thread, fn);
1373 success &= _make_call(fn);
1374 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
1375 Py_END_ALLOW_THREADS
1376
1377 /* Release lock we acquired above. This is required on HP-UX. */
1378 PyThread_release_lock(thread_done);
1379
1380 PyThread_free_lock(thread_done);
1381 if (!success)
1382 return NULL;
1383 Py_RETURN_NONE;
1384 }
1385
1386 /* test Py_AddPendingCalls using threads */
1387 static int _pending_callback(void *arg)
1388 {
1389 /* we assume the argument is callable object to which we own a reference */
1390 PyObject *callable = (PyObject *)arg;
1391 PyObject *r = PyObject_CallObject(callable, NULL);
1392 Py_DECREF(callable);
1393 Py_XDECREF(r);
1394 return r != NULL ? 0 : -1;
1395 }
1396
1397 /* The following requests n callbacks to _pending_callback. It can be
1398 * run from any python thread.
1399 */
1400 PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
1401 {
1402 PyObject *callable;
1403 int r;
1404 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
1405 return NULL;
1406
1407 /* create the reference for the callbackwhile we hold the lock */
1408 Py_INCREF(callable);
1409
1410 Py_BEGIN_ALLOW_THREADS
1411 r = Py_AddPendingCall(&_pending_callback, callable);
1412 Py_END_ALLOW_THREADS
1413
1414 if (r<0) {
1415 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
1416 Py_INCREF(Py_False);
1417 return Py_False;
1418 }
1419 Py_INCREF(Py_True);
1420 return Py_True;
1421 }
1422 #endif
1423
1424 /* Some tests of PyString_FromFormat(). This needs more tests. */
1425 static PyObject *
1426 test_string_from_format(PyObject *self, PyObject *args)
1427 {
1428 PyObject *result;
1429 char *msg;
1430
1431 #define CHECK_1_FORMAT(FORMAT, TYPE) \
1432 result = PyString_FromFormat(FORMAT, (TYPE)1); \
1433 if (result == NULL) \
1434 return NULL; \
1435 if (strcmp(PyString_AsString(result), "1")) { \
1436 msg = FORMAT " failed at 1"; \
1437 goto Fail; \
1438 } \
1439 Py_DECREF(result)
1440
1441 CHECK_1_FORMAT("%d", int);
1442 CHECK_1_FORMAT("%ld", long);
1443 /* The z width modifier was added in Python 2.5. */
1444 CHECK_1_FORMAT("%zd", Py_ssize_t);
1445
1446 /* The u type code was added in Python 2.5. */
1447 CHECK_1_FORMAT("%u", unsigned int);
1448 CHECK_1_FORMAT("%lu", unsigned long);
1449 CHECK_1_FORMAT("%zu", size_t);
1450
1451 /* "%lld" and "%llu" support added in Python 2.7. */
1452 #ifdef HAVE_LONG_LONG
1453 CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG);
1454 CHECK_1_FORMAT("%lld", PY_LONG_LONG);
1455 #endif
1456
1457 Py_RETURN_NONE;
1458
1459 Fail:
1460 Py_XDECREF(result);
1461 return raiseTestError("test_string_from_format", msg);
1462
1463 #undef CHECK_1_FORMAT
1464 }
1465
1466 /* Coverage testing of capsule objects. */
1467
1468 static const char *capsule_name = "capsule name";
1469 static char *capsule_pointer = "capsule pointer";
1470 static char *capsule_context = "capsule context";
1471 static const char *capsule_error = NULL;
1472 static int
1473 capsule_destructor_call_count = 0;
1474
1475 static void
1476 capsule_destructor(PyObject *o) {
1477 capsule_destructor_call_count++;
1478 if (PyCapsule_GetContext(o) != capsule_context) {
1479 capsule_error = "context did not match in destructor!";
1480 } else if (PyCapsule_GetDestructor(o) != capsule_destructor) {
1481 capsule_error = "destructor did not match in destructor! (woah!)";
1482 } else if (PyCapsule_GetName(o) != capsule_name) {
1483 capsule_error = "name did not match in destructor!";
1484 } else if (PyCapsule_GetPointer(o, capsule_name) != capsule_pointer) {
1485 capsule_error = "pointer did not match in destructor!";
1486 }
1487 }
1488
1489 typedef struct {
1490 char *name;
1491 char *module;
1492 char *attribute;
1493 } known_capsule;
1494
1495 static PyObject *
1496 test_capsule(PyObject *self, PyObject *args)
1497 {
1498 PyObject *object;
1499 const char *error = NULL;
1500 void *pointer;
1501 void *pointer2;
1502 known_capsule known_capsules[] = {
1503 #define KNOWN_CAPSULE(module, name) { module "." name, module, name }
1504 KNOWN_CAPSULE("_socket", "CAPI"),
1505 KNOWN_CAPSULE("_curses", "_C_API"),
1506 KNOWN_CAPSULE("datetime", "datetime_CAPI"),
1507 { NULL, NULL },
1508 };
1509 known_capsule *known = &known_capsules[0];
1510
1511 #define FAIL(x) { error = (x); goto exit; }
1512
1513 #define CHECK_DESTRUCTOR \
1514 if (capsule_error) { \
1515 FAIL(capsule_error); \
1516 } \
1517 else if (!capsule_destructor_call_count) { \
1518 FAIL("destructor not called!"); \
1519 } \
1520 capsule_destructor_call_count = 0; \
1521
1522 object = PyCapsule_New(capsule_pointer, capsule_name, capsule_destructor);
1523 PyCapsule_SetContext(object, capsule_context);
1524 capsule_destructor(object);
1525 CHECK_DESTRUCTOR;
1526 Py_DECREF(object);
1527 CHECK_DESTRUCTOR;
1528
1529 object = PyCapsule_New(known, "ignored", NULL);
1530 PyCapsule_SetPointer(object, capsule_pointer);
1531 PyCapsule_SetName(object, capsule_name);
1532 PyCapsule_SetDestructor(object, capsule_destructor);
1533 PyCapsule_SetContext(object, capsule_context);
1534 capsule_destructor(object);
1535 CHECK_DESTRUCTOR;
1536 /* intentionally access using the wrong name */
1537 pointer2 = PyCapsule_GetPointer(object, "the wrong name");
1538 if (!PyErr_Occurred()) {
1539 FAIL("PyCapsule_GetPointer should have failed but did not!");
1540 }
1541 PyErr_Clear();
1542 if (pointer2) {
1543 if (pointer2 == capsule_pointer) {
1544 FAIL("PyCapsule_GetPointer should not have"
1545 " returned the internal pointer!");
1546 } else {
1547 FAIL("PyCapsule_GetPointer should have "
1548 "returned NULL pointer but did not!");
1549 }
1550 }
1551 PyCapsule_SetDestructor(object, NULL);
1552 Py_DECREF(object);
1553 if (capsule_destructor_call_count) {
1554 FAIL("destructor called when it should not have been!");
1555 }
1556
1557 for (known = &known_capsules[0]; known->module != NULL; known++) {
1558 /* yeah, ordinarily I wouldn't do this either,
1559 but it's fine for this test harness.
1560 */
1561 static char buffer[256];
1562 #undef FAIL
1563 #define FAIL(x) \
1564 { \
1565 sprintf(buffer, "%s module: \"%s\" attribute: \"%s\"", \
1566 x, known->module, known->attribute); \
1567 error = buffer; \
1568 goto exit; \
1569 } \
1570
1571 PyObject *module = PyImport_ImportModule(known->module);
1572 if (module) {
1573 pointer = PyCapsule_Import(known->name, 0);
1574 if (!pointer) {
1575 Py_DECREF(module);
1576 FAIL("PyCapsule_GetPointer returned NULL unexpectedly!");
1577 }
1578 object = PyObject_GetAttrString(module, known->attribute);
1579 if (!object) {
1580 Py_DECREF(module);
1581 return NULL;
1582 }
1583 pointer2 = PyCapsule_GetPointer(object,
1584 "weebles wobble but they don't fall down");
1585 if (!PyErr_Occurred()) {
1586 Py_DECREF(object);
1587 Py_DECREF(module);
1588 FAIL("PyCapsule_GetPointer should have failed but did not!");
1589 }
1590 PyErr_Clear();
1591 if (pointer2) {
1592 Py_DECREF(module);
1593 Py_DECREF(object);
1594 if (pointer2 == pointer) {
1595 FAIL("PyCapsule_GetPointer should not have"
1596 " returned its internal pointer!");
1597 } else {
1598 FAIL("PyCapsule_GetPointer should have"
1599 " returned NULL pointer but did not!");
1600 }
1601 }
1602 Py_DECREF(object);
1603 Py_DECREF(module);
1604 }
1605 else
1606 PyErr_Clear();
1607 }
1608
1609 exit:
1610 if (error) {
1611 return raiseTestError("test_capsule", error);
1612 }
1613 Py_RETURN_NONE;
1614 #undef FAIL
1615 }
1616
1617 /* This is here to provide a docstring for test_descr. */
1618 static PyObject *
1619 test_with_docstring(PyObject *self)
1620 {
1621 Py_RETURN_NONE;
1622 }
1623
1624 /* To test the format of tracebacks as printed out. */
1625 static PyObject *
1626 traceback_print(PyObject *self, PyObject *args)
1627 {
1628 PyObject *file;
1629 PyObject *traceback;
1630 int result;
1631
1632 if (!PyArg_ParseTuple(args, "OO:traceback_print",
1633 &traceback, &file))
1634 return NULL;
1635
1636 result = PyTraceBack_Print(traceback, file);
1637 if (result < 0)
1638 return NULL;
1639 Py_RETURN_NONE;
1640 }
1641
1642 /* To test that the result of PyCode_NewEmpty has the right members. */
1643 static PyObject *
1644 code_newempty(PyObject *self, PyObject *args)
1645 {
1646 const char *filename;
1647 const char *funcname;
1648 int firstlineno;
1649
1650 if (!PyArg_ParseTuple(args, "ssi:code_newempty",
1651 &filename, &funcname, &firstlineno))
1652 return NULL;
1653
1654 return (PyObject *)PyCode_NewEmpty(filename, funcname, firstlineno);
1655 }
1656
1657 /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
1658 Run via Lib/test/test_exceptions.py */
1659 static PyObject *
1660 make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
1661 {
1662 char *name;
1663 char *doc = NULL;
1664 PyObject *base = NULL;
1665 PyObject *dict = NULL;
1666
1667 static char *kwlist[] = {"name", "doc", "base", "dict", NULL};
1668
1669 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
1670 "s|sOO:make_exception_with_doc", kwlist,
1671 &name, &doc, &base, &dict))
1672 return NULL;
1673
1674 return PyErr_NewExceptionWithDoc(name, doc, base, dict);
1675 }
1676
1677 static PyObject *
1678 sequence_delitem(PyObject *self, PyObject *args)
1679 {
1680 PyObject *seq;
1681 Py_ssize_t i;
1682
1683 if (!PyArg_ParseTuple(args, "On", &seq, &i))
1684 return NULL;
1685 if (PySequence_DelItem(seq, i) < 0)
1686 return NULL;
1687 Py_RETURN_NONE;
1688 }
1689
1690 static PyMethodDef TestMethods[] = {
1691 {"raise_exception", raise_exception, METH_VARARGS},
1692 {"test_config", (PyCFunction)test_config, METH_NOARGS},
1693 {"test_datetime_capi", test_datetime_capi, METH_NOARGS},
1694 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
1695 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
1696 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
1697 {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS},
1698 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
1699 {"test_long_and_overflow", (PyCFunction)test_long_and_overflow,
1700 METH_NOARGS},
1701 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
1702 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
1703 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
1704 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
1705 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
1706 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
1707 PyDoc_STR("This is a pretty normal docstring.")},
1708
1709 {"getargs_tuple", getargs_tuple, METH_VARARGS},
1710 {"getargs_keywords", (PyCFunction)getargs_keywords,
1711 METH_VARARGS|METH_KEYWORDS},
1712 {"getargs_b", getargs_b, METH_VARARGS},
1713 {"getargs_B", getargs_B, METH_VARARGS},
1714 {"getargs_h", getargs_h, METH_VARARGS},
1715 {"getargs_H", getargs_H, METH_VARARGS},
1716 {"getargs_I", getargs_I, METH_VARARGS},
1717 {"getargs_k", getargs_k, METH_VARARGS},
1718 {"getargs_i", getargs_i, METH_VARARGS},
1719 {"getargs_l", getargs_l, METH_VARARGS},
1720 {"getargs_n", getargs_n, METH_VARARGS},
1721 #ifdef HAVE_LONG_LONG
1722 {"getargs_L", getargs_L, METH_VARARGS},
1723 {"getargs_K", getargs_K, METH_VARARGS},
1724 {"test_longlong_api", test_longlong_api, METH_NOARGS},
1725 {"test_long_long_and_overflow",
1726 (PyCFunction)test_long_long_and_overflow, METH_NOARGS},
1727 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
1728 {"codec_incrementalencoder",
1729 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
1730 {"codec_incrementaldecoder",
1731 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
1732 #endif
1733 #ifdef Py_USING_UNICODE
1734 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
1735 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
1736 {"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
1737 #endif
1738 #ifdef WITH_THREAD
1739 {"_test_thread_state", test_thread_state, METH_VARARGS},
1740 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
1741 #endif
1742 {"test_capsule", (PyCFunction)test_capsule, METH_NOARGS},
1743 {"traceback_print", traceback_print, METH_VARARGS},
1744 {"code_newempty", code_newempty, METH_VARARGS},
1745 {"make_exception_with_doc", (PyCFunction)make_exception_with_doc,
1746 METH_VARARGS | METH_KEYWORDS},
1747 {"sequence_delitem", (PyCFunction)sequence_delitem, METH_VARARGS},
1748 {NULL, NULL} /* sentinel */
1749 };
1750
1751 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
1752
1753 typedef struct {
1754 char bool_member;
1755 char byte_member;
1756 unsigned char ubyte_member;
1757 short short_member;
1758 unsigned short ushort_member;
1759 int int_member;
1760 unsigned int uint_member;
1761 long long_member;
1762 unsigned long ulong_member;
1763 float float_member;
1764 double double_member;
1765 char inplace_member[6];
1766 #ifdef HAVE_LONG_LONG
1767 PY_LONG_LONG longlong_member;
1768 unsigned PY_LONG_LONG ulonglong_member;
1769 #endif
1770 } all_structmembers;
1771
1772 typedef struct {
1773 PyObject_HEAD
1774 all_structmembers structmembers;
1775 } test_structmembers;
1776
1777 static struct PyMemberDef test_members[] = {
1778 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
1779 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
1780 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
1781 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
1782 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
1783 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
1784 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
1785 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
1786 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
1787 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
1788 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
1789 {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL},
1790 #ifdef HAVE_LONG_LONG
1791 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
1792 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
1793 #endif
1794 {NULL}
1795 };
1796
1797
1798 static PyObject *
1799 test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1800 {
1801 static char *keywords[] = {
1802 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
1803 "T_INT", "T_UINT", "T_LONG", "T_ULONG",
1804 "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE",
1805 #ifdef HAVE_LONG_LONG
1806 "T_LONGLONG", "T_ULONGLONG",
1807 #endif
1808 NULL};
1809 static char *fmt = "|bbBhHiIlkfds#"
1810 #ifdef HAVE_LONG_LONG
1811 "LK"
1812 #endif
1813 ;
1814 test_structmembers *ob;
1815 const char *s = NULL;
1816 int string_len = 0;
1817 ob = PyObject_New(test_structmembers, type);
1818 if (ob == NULL)
1819 return NULL;
1820 memset(&ob->structmembers, 0, sizeof(all_structmembers));
1821 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
1822 &ob->structmembers.bool_member,
1823 &ob->structmembers.byte_member,
1824 &ob->structmembers.ubyte_member,
1825 &ob->structmembers.short_member,
1826 &ob->structmembers.ushort_member,
1827 &ob->structmembers.int_member,
1828 &ob->structmembers.uint_member,
1829 &ob->structmembers.long_member,
1830 &ob->structmembers.ulong_member,
1831 &ob->structmembers.float_member,
1832 &ob->structmembers.double_member,
1833 &s, &string_len
1834 #ifdef HAVE_LONG_LONG
1835 , &ob->structmembers.longlong_member,
1836 &ob->structmembers.ulonglong_member
1837 #endif
1838 )) {
1839 Py_DECREF(ob);
1840 return NULL;
1841 }
1842 if (s != NULL) {
1843 if (string_len > 5) {
1844 Py_DECREF(ob);
1845 PyErr_SetString(PyExc_ValueError, "string too long");
1846 return NULL;
1847 }
1848 strcpy(ob->structmembers.inplace_member, s);
1849 }
1850 else {
1851 strcpy(ob->structmembers.inplace_member, "");
1852 }
1853 return (PyObject *)ob;
1854 }
1855
1856 static void
1857 test_structmembers_free(PyObject *ob)
1858 {
1859 PyObject_FREE(ob);
1860 }
1861
1862 static PyTypeObject test_structmembersType = {
1863 PyVarObject_HEAD_INIT(NULL, 0)
1864 "test_structmembersType",
1865 sizeof(test_structmembers), /* tp_basicsize */
1866 0, /* tp_itemsize */
1867 test_structmembers_free, /* destructor tp_dealloc */
1868 0, /* tp_print */
1869 0, /* tp_getattr */
1870 0, /* tp_setattr */
1871 0, /* tp_compare */
1872 0, /* tp_repr */
1873 0, /* tp_as_number */
1874 0, /* tp_as_sequence */
1875 0, /* tp_as_mapping */
1876 0, /* tp_hash */
1877 0, /* tp_call */
1878 0, /* tp_str */
1879 PyObject_GenericGetAttr, /* tp_getattro */
1880 PyObject_GenericSetAttr, /* tp_setattro */
1881 0, /* tp_as_buffer */
1882 0, /* tp_flags */
1883 "Type containing all structmember types",
1884 0, /* traverseproc tp_traverse */
1885 0, /* tp_clear */
1886 0, /* tp_richcompare */
1887 0, /* tp_weaklistoffset */
1888 0, /* tp_iter */
1889 0, /* tp_iternext */
1890 0, /* tp_methods */
1891 test_members, /* tp_members */
1892 0,
1893 0,
1894 0,
1895 0,
1896 0,
1897 0,
1898 0,
1899 0,
1900 test_structmembers_new, /* tp_new */
1901 };
1902
1903
1904 PyMODINIT_FUNC
1905 init_testcapi(void)
1906 {
1907 PyObject *m;
1908
1909 m = Py_InitModule("_testcapi", TestMethods);
1910 if (m == NULL)
1911 return;
1912
1913 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
1914
1915 Py_TYPE(&test_structmembersType)=&PyType_Type;
1916 Py_INCREF(&test_structmembersType);
1917 /* don't use a name starting with "test", since we don't want
1918 test_capi to automatically call this */
1919 PyModule_AddObject(m, "_test_structmembersType", (PyObject *)&test_structmembersType);
1920
1921 PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
1922 PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));
1923 PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
1924 PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX));
1925 PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN));
1926 PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
1927 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
1928 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
1929 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
1930 PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
1931 PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
1932 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
1933 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
1934 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
1935 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
1936 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
1937 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
1938 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
1939 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
1940 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
1941 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
1942 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head)));
1943
1944 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
1945 Py_INCREF(TestError);
1946 PyModule_AddObject(m, "error", TestError);
1947 }