Issue #5768: Change to Unicode output logic and test case for same.
[python.git] / Modules / _testcapimodule.c
blobd187c5b68e7e61a2bdc9ef6814ff2b3c07d17041
1 /*
2 * C Extension module to test Python interpreter C APIs.
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 */
8 #include "Python.h"
9 #include <float.h>
10 #include "structmember.h"
12 #ifdef WITH_THREAD
13 #include "pythread.h"
14 #endif /* WITH_THREAD */
15 static PyObject *TestError; /* set to exception object in init */
17 /* Raise TestError with test_name + ": " + msg, and return NULL. */
19 static PyObject *
20 raiseTestError(const char* test_name, const char* msg)
22 char buf[2048];
24 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
25 PyErr_SetString(TestError, "internal error msg too large");
26 else {
27 PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
28 PyErr_SetString(TestError, buf);
30 return NULL;
33 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
35 The ones derived from autoconf on the UNIX-like OSes can be relied
36 upon (in the absence of sloppy cross-compiling), but the Windows
37 platforms have these hardcoded. Better safe than sorry.
39 static PyObject*
40 sizeof_error(const char* fatname, const char* typname,
41 int expected, int got)
43 char buf[1024];
44 PyOS_snprintf(buf, sizeof(buf),
45 "%.200s #define == %d but sizeof(%.200s) == %d",
46 fatname, expected, typname, got);
47 PyErr_SetString(TestError, buf);
48 return (PyObject*)NULL;
51 static PyObject*
52 test_config(PyObject *self)
54 #define CHECK_SIZEOF(FATNAME, TYPE) \
55 if (FATNAME != sizeof(TYPE)) \
56 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
58 CHECK_SIZEOF(SIZEOF_SHORT, short);
59 CHECK_SIZEOF(SIZEOF_INT, int);
60 CHECK_SIZEOF(SIZEOF_LONG, long);
61 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
62 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
63 #ifdef HAVE_LONG_LONG
64 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
65 #endif
67 #undef CHECK_SIZEOF
69 Py_INCREF(Py_None);
70 return Py_None;
73 static PyObject*
74 test_list_api(PyObject *self)
76 PyObject* list;
77 int i;
79 /* SF bug 132008: PyList_Reverse segfaults */
80 #define NLIST 30
81 list = PyList_New(NLIST);
82 if (list == (PyObject*)NULL)
83 return (PyObject*)NULL;
84 /* list = range(NLIST) */
85 for (i = 0; i < NLIST; ++i) {
86 PyObject* anint = PyInt_FromLong(i);
87 if (anint == (PyObject*)NULL) {
88 Py_DECREF(list);
89 return (PyObject*)NULL;
91 PyList_SET_ITEM(list, i, anint);
93 /* list.reverse(), via PyList_Reverse() */
94 i = PyList_Reverse(list); /* should not blow up! */
95 if (i != 0) {
96 Py_DECREF(list);
97 return (PyObject*)NULL;
99 /* Check that list == range(29, -1, -1) now */
100 for (i = 0; i < NLIST; ++i) {
101 PyObject* anint = PyList_GET_ITEM(list, i);
102 if (PyInt_AS_LONG(anint) != NLIST-1-i) {
103 PyErr_SetString(TestError,
104 "test_list_api: reverse screwed up");
105 Py_DECREF(list);
106 return (PyObject*)NULL;
109 Py_DECREF(list);
110 #undef NLIST
112 Py_INCREF(Py_None);
113 return Py_None;
116 static int
117 test_dict_inner(int count)
119 Py_ssize_t pos = 0, iterations = 0;
120 int i;
121 PyObject *dict = PyDict_New();
122 PyObject *v, *k;
124 if (dict == NULL)
125 return -1;
127 for (i = 0; i < count; i++) {
128 v = PyInt_FromLong(i);
129 PyDict_SetItem(dict, v, v);
130 Py_DECREF(v);
133 while (PyDict_Next(dict, &pos, &k, &v)) {
134 PyObject *o;
135 iterations++;
137 i = PyInt_AS_LONG(v) + 1;
138 o = PyInt_FromLong(i);
139 if (o == NULL)
140 return -1;
141 if (PyDict_SetItem(dict, k, o) < 0) {
142 Py_DECREF(o);
143 return -1;
145 Py_DECREF(o);
148 Py_DECREF(dict);
150 if (iterations != count) {
151 PyErr_SetString(
152 TestError,
153 "test_dict_iteration: dict iteration went wrong ");
154 return -1;
155 } else {
156 return 0;
160 static PyObject*
161 test_dict_iteration(PyObject* self)
163 int i;
165 for (i = 0; i < 200; i++) {
166 if (test_dict_inner(i) < 0) {
167 return NULL;
171 Py_INCREF(Py_None);
172 return Py_None;
176 /* Issue #4701: Check that PyObject_Hash implicitly calls
177 * PyType_Ready if it hasn't already been called
179 static PyTypeObject _HashInheritanceTester_Type = {
180 PyObject_HEAD_INIT(NULL)
181 0, /* Number of items for varobject */
182 "hashinheritancetester", /* Name of this type */
183 sizeof(PyObject), /* Basic object size */
184 0, /* Item size for varobject */
185 (destructor)PyObject_Del, /* tp_dealloc */
186 0, /* tp_print */
187 0, /* tp_getattr */
188 0, /* tp_setattr */
189 0, /* tp_compare */
190 0, /* tp_repr */
191 0, /* tp_as_number */
192 0, /* tp_as_sequence */
193 0, /* tp_as_mapping */
194 0, /* tp_hash */
195 0, /* tp_call */
196 0, /* tp_str */
197 PyObject_GenericGetAttr, /* tp_getattro */
198 0, /* tp_setattro */
199 0, /* tp_as_buffer */
200 Py_TPFLAGS_DEFAULT, /* tp_flags */
201 0, /* tp_doc */
202 0, /* tp_traverse */
203 0, /* tp_clear */
204 0, /* tp_richcompare */
205 0, /* tp_weaklistoffset */
206 0, /* tp_iter */
207 0, /* tp_iternext */
208 0, /* tp_methods */
209 0, /* tp_members */
210 0, /* tp_getset */
211 0, /* tp_base */
212 0, /* tp_dict */
213 0, /* tp_descr_get */
214 0, /* tp_descr_set */
215 0, /* tp_dictoffset */
216 0, /* tp_init */
217 0, /* tp_alloc */
218 PyType_GenericNew, /* tp_new */
221 static PyObject*
222 test_lazy_hash_inheritance(PyObject* self)
224 PyTypeObject *type;
225 PyObject *obj;
226 long hash;
228 type = &_HashInheritanceTester_Type;
229 obj = PyObject_New(PyObject, type);
230 if (obj == NULL) {
231 PyErr_Clear();
232 PyErr_SetString(
233 TestError,
234 "test_lazy_hash_inheritance: failed to create object");
235 return NULL;
238 if (type->tp_dict != NULL) {
239 PyErr_SetString(
240 TestError,
241 "test_lazy_hash_inheritance: type initialised too soon");
242 Py_DECREF(obj);
243 return NULL;
246 hash = PyObject_Hash(obj);
247 if ((hash == -1) && PyErr_Occurred()) {
248 PyErr_Clear();
249 PyErr_SetString(
250 TestError,
251 "test_lazy_hash_inheritance: could not hash object");
252 Py_DECREF(obj);
253 return NULL;
256 if (type->tp_dict == NULL) {
257 PyErr_SetString(
258 TestError,
259 "test_lazy_hash_inheritance: type not initialised by hash()");
260 Py_DECREF(obj);
261 return NULL;
264 if (type->tp_hash != PyType_Type.tp_hash) {
265 PyErr_SetString(
266 TestError,
267 "test_lazy_hash_inheritance: unexpected hash function");
268 Py_DECREF(obj);
269 return NULL;
272 Py_RETURN_NONE;
276 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
277 PyLong_{As, From}{Unsigned,}LongLong().
279 Note that the meat of the test is contained in testcapi_long.h.
280 This is revolting, but delicate code duplication is worse: "almost
281 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
282 dependence on type names makes it impossible to use a parameterized
283 function. A giant macro would be even worse than this. A C++ template
284 would be perfect.
286 The "report an error" functions are deliberately not part of the #include
287 file: if the test fails, you can set a breakpoint in the appropriate
288 error function directly, and crawl back from there in the debugger.
291 #define UNBIND(X) Py_DECREF(X); (X) = NULL
293 static PyObject *
294 raise_test_long_error(const char* msg)
296 return raiseTestError("test_long_api", msg);
299 #define TESTNAME test_long_api_inner
300 #define TYPENAME long
301 #define F_S_TO_PY PyLong_FromLong
302 #define F_PY_TO_S PyLong_AsLong
303 #define F_U_TO_PY PyLong_FromUnsignedLong
304 #define F_PY_TO_U PyLong_AsUnsignedLong
306 #include "testcapi_long.h"
308 static PyObject *
309 test_long_api(PyObject* self)
311 return TESTNAME(raise_test_long_error);
314 #undef TESTNAME
315 #undef TYPENAME
316 #undef F_S_TO_PY
317 #undef F_PY_TO_S
318 #undef F_U_TO_PY
319 #undef F_PY_TO_U
321 #ifdef HAVE_LONG_LONG
323 static PyObject *
324 raise_test_longlong_error(const char* msg)
326 return raiseTestError("test_longlong_api", msg);
329 #define TESTNAME test_longlong_api_inner
330 #define TYPENAME PY_LONG_LONG
331 #define F_S_TO_PY PyLong_FromLongLong
332 #define F_PY_TO_S PyLong_AsLongLong
333 #define F_U_TO_PY PyLong_FromUnsignedLongLong
334 #define F_PY_TO_U PyLong_AsUnsignedLongLong
336 #include "testcapi_long.h"
338 static PyObject *
339 test_longlong_api(PyObject* self, PyObject *args)
341 return TESTNAME(raise_test_longlong_error);
344 #undef TESTNAME
345 #undef TYPENAME
346 #undef F_S_TO_PY
347 #undef F_PY_TO_S
348 #undef F_U_TO_PY
349 #undef F_PY_TO_U
351 /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
352 for both long and int arguments. The test may leak a little memory if
353 it fails.
355 static PyObject *
356 test_L_code(PyObject *self)
358 PyObject *tuple, *num;
359 PY_LONG_LONG value;
361 tuple = PyTuple_New(1);
362 if (tuple == NULL)
363 return NULL;
365 num = PyLong_FromLong(42);
366 if (num == NULL)
367 return NULL;
369 PyTuple_SET_ITEM(tuple, 0, num);
371 value = -1;
372 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
373 return NULL;
374 if (value != 42)
375 return raiseTestError("test_L_code",
376 "L code returned wrong value for long 42");
378 Py_DECREF(num);
379 num = PyInt_FromLong(42);
380 if (num == NULL)
381 return NULL;
383 PyTuple_SET_ITEM(tuple, 0, num);
385 value = -1;
386 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
387 return NULL;
388 if (value != 42)
389 return raiseTestError("test_L_code",
390 "L code returned wrong value for int 42");
392 Py_DECREF(tuple);
393 Py_INCREF(Py_None);
394 return Py_None;
397 #endif /* ifdef HAVE_LONG_LONG */
399 /* Test tuple argument processing */
400 static PyObject *
401 getargs_tuple(PyObject *self, PyObject *args)
403 int a, b, c;
404 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
405 return NULL;
406 return Py_BuildValue("iii", a, b, c);
409 /* test PyArg_ParseTupleAndKeywords */
410 static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
412 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
413 static char *fmt="(ii)i|(i(ii))(iii)i";
414 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
416 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
417 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
418 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
419 return NULL;
420 return Py_BuildValue("iiiiiiiiii",
421 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
422 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
425 /* Functions to call PyArg_ParseTuple with integer format codes,
426 and return the result.
428 static PyObject *
429 getargs_b(PyObject *self, PyObject *args)
431 unsigned char value;
432 if (!PyArg_ParseTuple(args, "b", &value))
433 return NULL;
434 return PyLong_FromUnsignedLong((unsigned long)value);
437 static PyObject *
438 getargs_B(PyObject *self, PyObject *args)
440 unsigned char value;
441 if (!PyArg_ParseTuple(args, "B", &value))
442 return NULL;
443 return PyLong_FromUnsignedLong((unsigned long)value);
446 static PyObject *
447 getargs_H(PyObject *self, PyObject *args)
449 unsigned short value;
450 if (!PyArg_ParseTuple(args, "H", &value))
451 return NULL;
452 return PyLong_FromUnsignedLong((unsigned long)value);
455 static PyObject *
456 getargs_I(PyObject *self, PyObject *args)
458 unsigned int value;
459 if (!PyArg_ParseTuple(args, "I", &value))
460 return NULL;
461 return PyLong_FromUnsignedLong((unsigned long)value);
464 static PyObject *
465 getargs_k(PyObject *self, PyObject *args)
467 unsigned long value;
468 if (!PyArg_ParseTuple(args, "k", &value))
469 return NULL;
470 return PyLong_FromUnsignedLong(value);
473 static PyObject *
474 getargs_i(PyObject *self, PyObject *args)
476 int value;
477 if (!PyArg_ParseTuple(args, "i", &value))
478 return NULL;
479 return PyLong_FromLong((long)value);
482 static PyObject *
483 getargs_l(PyObject *self, PyObject *args)
485 long value;
486 if (!PyArg_ParseTuple(args, "l", &value))
487 return NULL;
488 return PyLong_FromLong(value);
491 static PyObject *
492 getargs_n(PyObject *self, PyObject *args)
494 Py_ssize_t value;
495 if (!PyArg_ParseTuple(args, "n", &value))
496 return NULL;
497 return PyInt_FromSsize_t(value);
500 #ifdef HAVE_LONG_LONG
501 static PyObject *
502 getargs_L(PyObject *self, PyObject *args)
504 PY_LONG_LONG value;
505 if (!PyArg_ParseTuple(args, "L", &value))
506 return NULL;
507 return PyLong_FromLongLong(value);
510 static PyObject *
511 getargs_K(PyObject *self, PyObject *args)
513 unsigned PY_LONG_LONG value;
514 if (!PyArg_ParseTuple(args, "K", &value))
515 return NULL;
516 return PyLong_FromUnsignedLongLong(value);
518 #endif
520 /* This function not only tests the 'k' getargs code, but also the
521 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
522 static PyObject *
523 test_k_code(PyObject *self)
525 PyObject *tuple, *num;
526 unsigned long value;
528 tuple = PyTuple_New(1);
529 if (tuple == NULL)
530 return NULL;
532 /* a number larger than ULONG_MAX even on 64-bit platforms */
533 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
534 if (num == NULL)
535 return NULL;
537 value = PyInt_AsUnsignedLongMask(num);
538 if (value != ULONG_MAX)
539 return raiseTestError("test_k_code",
540 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
542 PyTuple_SET_ITEM(tuple, 0, num);
544 value = 0;
545 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
546 return NULL;
547 if (value != ULONG_MAX)
548 return raiseTestError("test_k_code",
549 "k code returned wrong value for long 0xFFF...FFF");
551 Py_DECREF(num);
552 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
553 if (num == NULL)
554 return NULL;
556 value = PyInt_AsUnsignedLongMask(num);
557 if (value != (unsigned long)-0x42)
558 return raiseTestError("test_k_code",
559 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
561 PyTuple_SET_ITEM(tuple, 0, num);
563 value = 0;
564 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
565 return NULL;
566 if (value != (unsigned long)-0x42)
567 return raiseTestError("test_k_code",
568 "k code returned wrong value for long -0xFFF..000042");
570 Py_DECREF(tuple);
571 Py_INCREF(Py_None);
572 return Py_None;
575 #ifdef Py_USING_UNICODE
577 static volatile int x;
579 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
580 of an error.
582 static PyObject *
583 test_u_code(PyObject *self)
585 PyObject *tuple, *obj;
586 Py_UNICODE *value;
587 int len;
589 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
590 /* Just use the macro and check that it compiles */
591 x = Py_UNICODE_ISSPACE(25);
593 tuple = PyTuple_New(1);
594 if (tuple == NULL)
595 return NULL;
597 obj = PyUnicode_Decode("test", strlen("test"),
598 "ascii", NULL);
599 if (obj == NULL)
600 return NULL;
602 PyTuple_SET_ITEM(tuple, 0, obj);
604 value = 0;
605 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
606 return NULL;
607 if (value != PyUnicode_AS_UNICODE(obj))
608 return raiseTestError("test_u_code",
609 "u code returned wrong value for u'test'");
610 value = 0;
611 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
612 return NULL;
613 if (value != PyUnicode_AS_UNICODE(obj) ||
614 len != PyUnicode_GET_SIZE(obj))
615 return raiseTestError("test_u_code",
616 "u# code returned wrong values for u'test'");
618 Py_DECREF(tuple);
619 Py_INCREF(Py_None);
620 return Py_None;
623 static PyObject *
624 test_widechar(PyObject *self)
626 #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
627 const wchar_t wtext[2] = {(wchar_t)0x10ABCDu};
628 size_t wtextlen = 1;
629 #else
630 const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu};
631 size_t wtextlen = 2;
632 #endif
633 PyObject *wide, *utf8;
635 wide = PyUnicode_FromWideChar(wtext, wtextlen);
636 if (wide == NULL)
637 return NULL;
639 utf8 = PyUnicode_FromString("\xf4\x8a\xaf\x8d");
640 if (utf8 == NULL) {
641 Py_DECREF(wide);
642 return NULL;
645 if (PyUnicode_GET_SIZE(wide) != PyUnicode_GET_SIZE(utf8)) {
646 Py_DECREF(wide);
647 Py_DECREF(utf8);
648 return raiseTestError("test_widechar",
649 "wide string and utf8 string have different length");
651 if (PyUnicode_Compare(wide, utf8)) {
652 Py_DECREF(wide);
653 Py_DECREF(utf8);
654 if (PyErr_Occurred())
655 return NULL;
656 return raiseTestError("test_widechar",
657 "wide string and utf8 string are differents");
660 Py_DECREF(wide);
661 Py_DECREF(utf8);
662 Py_RETURN_NONE;
665 static PyObject *
666 test_empty_argparse(PyObject *self)
668 /* Test that formats can begin with '|'. See issue #4720. */
669 PyObject *tuple, *dict = NULL;
670 static char *kwlist[] = {NULL};
671 int result;
672 tuple = PyTuple_New(0);
673 if (!tuple)
674 return NULL;
675 if ((result = PyArg_ParseTuple(tuple, "|:test_empty_argparse")) < 0)
676 goto done;
677 dict = PyDict_New();
678 if (!dict)
679 goto done;
680 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse", kwlist);
681 done:
682 Py_DECREF(tuple);
683 Py_XDECREF(dict);
684 if (result < 0)
685 return NULL;
686 else {
687 Py_RETURN_NONE;
691 static PyObject *
692 codec_incrementalencoder(PyObject *self, PyObject *args)
694 const char *encoding, *errors = NULL;
695 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
696 &encoding, &errors))
697 return NULL;
698 return PyCodec_IncrementalEncoder(encoding, errors);
701 static PyObject *
702 codec_incrementaldecoder(PyObject *self, PyObject *args)
704 const char *encoding, *errors = NULL;
705 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
706 &encoding, &errors))
707 return NULL;
708 return PyCodec_IncrementalDecoder(encoding, errors);
711 #endif
713 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
714 static PyObject *
715 test_long_numbits(PyObject *self)
717 struct triple {
718 long input;
719 size_t nbits;
720 int sign;
721 } testcases[] = {{0, 0, 0},
722 {1L, 1, 1},
723 {-1L, 1, -1},
724 {2L, 2, 1},
725 {-2L, 2, -1},
726 {3L, 2, 1},
727 {-3L, 2, -1},
728 {4L, 3, 1},
729 {-4L, 3, -1},
730 {0x7fffL, 15, 1}, /* one Python long digit */
731 {-0x7fffL, 15, -1},
732 {0xffffL, 16, 1},
733 {-0xffffL, 16, -1},
734 {0xfffffffL, 28, 1},
735 {-0xfffffffL, 28, -1}};
736 int i;
738 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
739 PyObject *plong = PyLong_FromLong(testcases[i].input);
740 size_t nbits = _PyLong_NumBits(plong);
741 int sign = _PyLong_Sign(plong);
743 Py_DECREF(plong);
744 if (nbits != testcases[i].nbits)
745 return raiseTestError("test_long_numbits",
746 "wrong result for _PyLong_NumBits");
747 if (sign != testcases[i].sign)
748 return raiseTestError("test_long_numbits",
749 "wrong result for _PyLong_Sign");
751 Py_INCREF(Py_None);
752 return Py_None;
755 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
757 static PyObject *
758 test_null_strings(PyObject *self)
760 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
761 PyObject *tuple = PyTuple_Pack(2, o1, o2);
762 Py_XDECREF(o1);
763 Py_XDECREF(o2);
764 return tuple;
767 static PyObject *
768 raise_exception(PyObject *self, PyObject *args)
770 PyObject *exc;
771 PyObject *exc_args, *v;
772 int num_args, i;
774 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
775 &exc, &num_args))
776 return NULL;
777 if (!PyExceptionClass_Check(exc)) {
778 PyErr_Format(PyExc_TypeError, "an exception class is required");
779 return NULL;
782 exc_args = PyTuple_New(num_args);
783 if (exc_args == NULL)
784 return NULL;
785 for (i = 0; i < num_args; ++i) {
786 v = PyInt_FromLong(i);
787 if (v == NULL) {
788 Py_DECREF(exc_args);
789 return NULL;
791 PyTuple_SET_ITEM(exc_args, i, v);
793 PyErr_SetObject(exc, exc_args);
794 Py_DECREF(exc_args);
795 return NULL;
798 #ifdef WITH_THREAD
800 /* test_thread_state spawns a thread of its own, and that thread releases
801 * `thread_done` when it's finished. The driver code has to know when the
802 * thread finishes, because the thread uses a PyObject (the callable) that
803 * may go away when the driver finishes. The former lack of this explicit
804 * synchronization caused rare segfaults, so rare that they were seen only
805 * on a Mac buildbot (although they were possible on any box).
807 static PyThread_type_lock thread_done = NULL;
809 static int
810 _make_call(void *callable)
812 PyObject *rc;
813 int success;
814 PyGILState_STATE s = PyGILState_Ensure();
815 rc = PyObject_CallFunction((PyObject *)callable, "");
816 success = (rc != NULL);
817 Py_XDECREF(rc);
818 PyGILState_Release(s);
819 return success;
822 /* Same thing, but releases `thread_done` when it returns. This variant
823 * should be called only from threads spawned by test_thread_state().
825 static void
826 _make_call_from_thread(void *callable)
828 _make_call(callable);
829 PyThread_release_lock(thread_done);
832 static PyObject *
833 test_thread_state(PyObject *self, PyObject *args)
835 PyObject *fn;
836 int success = 1;
838 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
839 return NULL;
841 if (!PyCallable_Check(fn)) {
842 PyErr_Format(PyExc_TypeError, "'%s' object is not callable",
843 fn->ob_type->tp_name);
844 return NULL;
847 /* Ensure Python is set up for threading */
848 PyEval_InitThreads();
849 thread_done = PyThread_allocate_lock();
850 if (thread_done == NULL)
851 return PyErr_NoMemory();
852 PyThread_acquire_lock(thread_done, 1);
854 /* Start a new thread with our callback. */
855 PyThread_start_new_thread(_make_call_from_thread, fn);
856 /* Make the callback with the thread lock held by this thread */
857 success &= _make_call(fn);
858 /* Do it all again, but this time with the thread-lock released */
859 Py_BEGIN_ALLOW_THREADS
860 success &= _make_call(fn);
861 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
862 Py_END_ALLOW_THREADS
864 /* And once more with and without a thread
865 XXX - should use a lock and work out exactly what we are trying
866 to test <wink>
868 Py_BEGIN_ALLOW_THREADS
869 PyThread_start_new_thread(_make_call_from_thread, fn);
870 success &= _make_call(fn);
871 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
872 Py_END_ALLOW_THREADS
874 /* Release lock we acquired above. This is required on HP-UX. */
875 PyThread_release_lock(thread_done);
877 PyThread_free_lock(thread_done);
878 if (!success)
879 return NULL;
880 Py_RETURN_NONE;
883 /* test Py_AddPendingCalls using threads */
884 static int _pending_callback(void *arg)
886 /* we assume the argument is callable object to which we own a reference */
887 PyObject *callable = (PyObject *)arg;
888 PyObject *r = PyObject_CallObject(callable, NULL);
889 Py_DECREF(callable);
890 Py_XDECREF(r);
891 return r != NULL ? 0 : -1;
894 /* The following requests n callbacks to _pending_callback. It can be
895 * run from any python thread.
897 PyObject *pending_threadfunc(PyObject *self, PyObject *arg)
899 PyObject *callable;
900 int r;
901 if (PyArg_ParseTuple(arg, "O", &callable) == 0)
902 return NULL;
904 /* create the reference for the callbackwhile we hold the lock */
905 Py_INCREF(callable);
907 Py_BEGIN_ALLOW_THREADS
908 r = Py_AddPendingCall(&_pending_callback, callable);
909 Py_END_ALLOW_THREADS
911 if (r<0) {
912 Py_DECREF(callable); /* unsuccessful add, destroy the extra reference */
913 Py_INCREF(Py_False);
914 return Py_False;
916 Py_INCREF(Py_True);
917 return Py_True;
919 #endif
921 /* Some tests of PyString_FromFormat(). This needs more tests. */
922 static PyObject *
923 test_string_from_format(PyObject *self, PyObject *args)
925 PyObject *result;
926 char *msg;
928 #define CHECK_1_FORMAT(FORMAT, TYPE) \
929 result = PyString_FromFormat(FORMAT, (TYPE)1); \
930 if (result == NULL) \
931 return NULL; \
932 if (strcmp(PyString_AsString(result), "1")) { \
933 msg = FORMAT " failed at 1"; \
934 goto Fail; \
936 Py_DECREF(result)
938 CHECK_1_FORMAT("%d", int);
939 CHECK_1_FORMAT("%ld", long);
940 /* The z width modifier was added in Python 2.5. */
941 CHECK_1_FORMAT("%zd", Py_ssize_t);
943 /* The u type code was added in Python 2.5. */
944 CHECK_1_FORMAT("%u", unsigned int);
945 CHECK_1_FORMAT("%lu", unsigned long);
946 CHECK_1_FORMAT("%zu", size_t);
948 Py_RETURN_NONE;
950 Fail:
951 Py_XDECREF(result);
952 return raiseTestError("test_string_from_format", msg);
954 #undef CHECK_1_FORMAT
957 /* This is here to provide a docstring for test_descr. */
958 static PyObject *
959 test_with_docstring(PyObject *self)
961 Py_RETURN_NONE;
964 /* To test the format of tracebacks as printed out. */
965 static PyObject *
966 traceback_print(PyObject *self, PyObject *args)
968 PyObject *file;
969 PyObject *traceback;
970 int result;
972 if (!PyArg_ParseTuple(args, "OO:traceback_print",
973 &traceback, &file))
974 return NULL;
976 result = PyTraceBack_Print(traceback, file);
977 if (result < 0)
978 return NULL;
979 Py_RETURN_NONE;
982 static PyMethodDef TestMethods[] = {
983 {"raise_exception", raise_exception, METH_VARARGS},
984 {"test_config", (PyCFunction)test_config, METH_NOARGS},
985 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
986 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
987 {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS},
988 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
989 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
990 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
991 {"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
992 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
993 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
994 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
995 PyDoc_STR("This is a pretty normal docstring.")},
997 {"getargs_tuple", getargs_tuple, METH_VARARGS},
998 {"getargs_keywords", (PyCFunction)getargs_keywords,
999 METH_VARARGS|METH_KEYWORDS},
1000 {"getargs_b", getargs_b, METH_VARARGS},
1001 {"getargs_B", getargs_B, METH_VARARGS},
1002 {"getargs_H", getargs_H, METH_VARARGS},
1003 {"getargs_I", getargs_I, METH_VARARGS},
1004 {"getargs_k", getargs_k, METH_VARARGS},
1005 {"getargs_i", getargs_i, METH_VARARGS},
1006 {"getargs_l", getargs_l, METH_VARARGS},
1007 {"getargs_n", getargs_n, METH_VARARGS},
1008 #ifdef HAVE_LONG_LONG
1009 {"getargs_L", getargs_L, METH_VARARGS},
1010 {"getargs_K", getargs_K, METH_VARARGS},
1011 {"test_longlong_api", test_longlong_api, METH_NOARGS},
1012 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
1013 {"codec_incrementalencoder",
1014 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
1015 {"codec_incrementaldecoder",
1016 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
1017 #endif
1018 #ifdef Py_USING_UNICODE
1019 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
1020 {"test_widechar", (PyCFunction)test_widechar, METH_NOARGS},
1021 #endif
1022 #ifdef WITH_THREAD
1023 {"_test_thread_state", test_thread_state, METH_VARARGS},
1024 {"_pending_threadfunc", pending_threadfunc, METH_VARARGS},
1025 #endif
1026 {"traceback_print", traceback_print, METH_VARARGS},
1027 {NULL, NULL} /* sentinel */
1030 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
1032 typedef struct {
1033 char bool_member;
1034 char byte_member;
1035 unsigned char ubyte_member;
1036 short short_member;
1037 unsigned short ushort_member;
1038 int int_member;
1039 unsigned int uint_member;
1040 long long_member;
1041 unsigned long ulong_member;
1042 float float_member;
1043 double double_member;
1044 #ifdef HAVE_LONG_LONG
1045 PY_LONG_LONG longlong_member;
1046 unsigned PY_LONG_LONG ulonglong_member;
1047 #endif
1048 } all_structmembers;
1050 typedef struct {
1051 PyObject_HEAD
1052 all_structmembers structmembers;
1053 } test_structmembers;
1055 static struct PyMemberDef test_members[] = {
1056 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
1057 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
1058 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
1059 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
1060 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
1061 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
1062 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
1063 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
1064 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
1065 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
1066 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
1067 #ifdef HAVE_LONG_LONG
1068 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
1069 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
1070 #endif
1071 {NULL}
1075 static PyObject *
1076 test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1078 static char *keywords[] = {
1079 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
1080 "T_INT", "T_UINT", "T_LONG", "T_ULONG",
1081 "T_FLOAT", "T_DOUBLE",
1082 #ifdef HAVE_LONG_LONG
1083 "T_LONGLONG", "T_ULONGLONG",
1084 #endif
1085 NULL};
1086 static char *fmt = "|bbBhHiIlkfd"
1087 #ifdef HAVE_LONG_LONG
1088 "LK"
1089 #endif
1091 test_structmembers *ob;
1092 ob = PyObject_New(test_structmembers, type);
1093 if (ob == NULL)
1094 return NULL;
1095 memset(&ob->structmembers, 0, sizeof(all_structmembers));
1096 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
1097 &ob->structmembers.bool_member,
1098 &ob->structmembers.byte_member,
1099 &ob->structmembers.ubyte_member,
1100 &ob->structmembers.short_member,
1101 &ob->structmembers.ushort_member,
1102 &ob->structmembers.int_member,
1103 &ob->structmembers.uint_member,
1104 &ob->structmembers.long_member,
1105 &ob->structmembers.ulong_member,
1106 &ob->structmembers.float_member,
1107 &ob->structmembers.double_member
1108 #ifdef HAVE_LONG_LONG
1109 , &ob->structmembers.longlong_member,
1110 &ob->structmembers.ulonglong_member
1111 #endif
1112 )) {
1113 Py_DECREF(ob);
1114 return NULL;
1116 return (PyObject *)ob;
1119 static void
1120 test_structmembers_free(PyObject *ob)
1122 PyObject_FREE(ob);
1125 static PyTypeObject test_structmembersType = {
1126 PyVarObject_HEAD_INIT(NULL, 0)
1127 "test_structmembersType",
1128 sizeof(test_structmembers), /* tp_basicsize */
1129 0, /* tp_itemsize */
1130 test_structmembers_free, /* destructor tp_dealloc */
1131 0, /* tp_print */
1132 0, /* tp_getattr */
1133 0, /* tp_setattr */
1134 0, /* tp_compare */
1135 0, /* tp_repr */
1136 0, /* tp_as_number */
1137 0, /* tp_as_sequence */
1138 0, /* tp_as_mapping */
1139 0, /* tp_hash */
1140 0, /* tp_call */
1141 0, /* tp_str */
1142 PyObject_GenericGetAttr, /* tp_getattro */
1143 PyObject_GenericSetAttr, /* tp_setattro */
1144 0, /* tp_as_buffer */
1145 0, /* tp_flags */
1146 "Type containing all structmember types",
1147 0, /* traverseproc tp_traverse */
1148 0, /* tp_clear */
1149 0, /* tp_richcompare */
1150 0, /* tp_weaklistoffset */
1151 0, /* tp_iter */
1152 0, /* tp_iternext */
1153 0, /* tp_methods */
1154 test_members, /* tp_members */
1163 test_structmembers_new, /* tp_new */
1167 PyMODINIT_FUNC
1168 init_testcapi(void)
1170 PyObject *m;
1172 m = Py_InitModule("_testcapi", TestMethods);
1173 if (m == NULL)
1174 return;
1176 Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
1178 Py_TYPE(&test_structmembersType)=&PyType_Type;
1179 Py_INCREF(&test_structmembersType);
1180 PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType);
1182 PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
1183 PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));
1184 PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
1185 PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX));
1186 PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN));
1187 PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
1188 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
1189 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
1190 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
1191 PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
1192 PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
1193 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
1194 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
1195 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
1196 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
1197 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
1198 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
1199 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
1200 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
1201 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
1202 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
1203 PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head)));
1205 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
1206 Py_INCREF(TestError);
1207 PyModule_AddObject(m, "error", TestError);