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.
10 #include "structmember.h"
15 #endif /* WITH_THREAD */
16 static PyObject
*TestError
; /* set to exception object in init */
18 /* Raise TestError with test_name + ": " + msg, and return NULL. */
21 raiseTestError(const char* test_name
, const char* msg
)
25 if (strlen(test_name
) + strlen(msg
) > sizeof(buf
) - 50)
26 PyErr_SetString(TestError
, "internal error msg too large");
28 PyOS_snprintf(buf
, sizeof(buf
), "%s: %s", test_name
, msg
);
29 PyErr_SetString(TestError
, buf
);
34 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
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.
41 sizeof_error(const char* fatname
, const char* typname
,
42 int expected
, int got
)
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
;
53 test_config(PyObject
*self
)
55 #define CHECK_SIZEOF(FATNAME, TYPE) \
56 if (FATNAME != sizeof(TYPE)) \
57 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
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);
65 CHECK_SIZEOF(SIZEOF_LONG_LONG
, PY_LONG_LONG
);
75 test_list_api(PyObject
*self
)
80 /* SF bug 132008: PyList_Reverse segfaults */
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
) {
90 return (PyObject
*)NULL
;
92 PyList_SET_ITEM(list
, i
, anint
);
94 /* list.reverse(), via PyList_Reverse() */
95 i
= PyList_Reverse(list
); /* should not blow up! */
98 return (PyObject
*)NULL
;
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");
107 return (PyObject
*)NULL
;
118 test_dict_inner(int count
)
120 Py_ssize_t pos
= 0, iterations
= 0;
122 PyObject
*dict
= PyDict_New();
128 for (i
= 0; i
< count
; i
++) {
129 v
= PyInt_FromLong(i
);
130 PyDict_SetItem(dict
, v
, v
);
134 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
138 i
= PyInt_AS_LONG(v
) + 1;
139 o
= PyInt_FromLong(i
);
142 if (PyDict_SetItem(dict
, k
, o
) < 0) {
151 if (iterations
!= count
) {
154 "test_dict_iteration: dict iteration went wrong ");
162 test_dict_iteration(PyObject
* self
)
166 for (i
= 0; i
< 200; i
++) {
167 if (test_dict_inner(i
) < 0) {
177 /* Issue #4701: Check that PyObject_Hash implicitly calls
178 * PyType_Ready if it hasn't already been called
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 */
192 0, /* tp_as_number */
193 0, /* tp_as_sequence */
194 0, /* tp_as_mapping */
198 PyObject_GenericGetAttr
, /* tp_getattro */
200 0, /* tp_as_buffer */
201 Py_TPFLAGS_DEFAULT
, /* tp_flags */
205 0, /* tp_richcompare */
206 0, /* tp_weaklistoffset */
214 0, /* tp_descr_get */
215 0, /* tp_descr_set */
216 0, /* tp_dictoffset */
219 PyType_GenericNew
, /* tp_new */
223 test_lazy_hash_inheritance(PyObject
* self
)
229 type
= &_HashInheritanceTester_Type
;
231 if (type
->tp_dict
!= NULL
)
232 /* The type has already been initialized. This probably means
237 obj
= PyObject_New(PyObject
, type
);
242 "test_lazy_hash_inheritance: failed to create object");
246 if (type
->tp_dict
!= NULL
) {
249 "test_lazy_hash_inheritance: type initialised too soon");
254 hash
= PyObject_Hash(obj
);
255 if ((hash
== -1) && PyErr_Occurred()) {
259 "test_lazy_hash_inheritance: could not hash object");
264 if (type
->tp_dict
== NULL
) {
267 "test_lazy_hash_inheritance: type not initialised by hash()");
272 if (type
->tp_hash
!= PyType_Type
.tp_hash
) {
275 "test_lazy_hash_inheritance: unexpected hash function");
286 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
287 PyLong_{As, From}{Unsigned,}LongLong().
289 Note that the meat of the test is contained in testcapi_long.h.
290 This is revolting, but delicate code duplication is worse: "almost
291 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
292 dependence on type names makes it impossible to use a parameterized
293 function. A giant macro would be even worse than this. A C++ template
296 The "report an error" functions are deliberately not part of the #include
297 file: if the test fails, you can set a breakpoint in the appropriate
298 error function directly, and crawl back from there in the debugger.
301 #define UNBIND(X) Py_DECREF(X); (X) = NULL
304 raise_test_long_error(const char* msg
)
306 return raiseTestError("test_long_api", msg
);
309 #define TESTNAME test_long_api_inner
310 #define TYPENAME long
311 #define F_S_TO_PY PyLong_FromLong
312 #define F_PY_TO_S PyLong_AsLong
313 #define F_U_TO_PY PyLong_FromUnsignedLong
314 #define F_PY_TO_U PyLong_AsUnsignedLong
316 #include "testcapi_long.h"
319 test_long_api(PyObject
* self
)
321 return TESTNAME(raise_test_long_error
);
331 #ifdef HAVE_LONG_LONG
334 raise_test_longlong_error(const char* msg
)
336 return raiseTestError("test_longlong_api", msg
);
339 #define TESTNAME test_longlong_api_inner
340 #define TYPENAME PY_LONG_LONG
341 #define F_S_TO_PY PyLong_FromLongLong
342 #define F_PY_TO_S PyLong_AsLongLong
343 #define F_U_TO_PY PyLong_FromUnsignedLongLong
344 #define F_PY_TO_U PyLong_AsUnsignedLongLong
346 #include "testcapi_long.h"
349 test_longlong_api(PyObject
* self
, PyObject
*args
)
351 return TESTNAME(raise_test_longlong_error
);
361 /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG
362 is tested by test_long_api_inner. This test will concentrate on proper
363 handling of overflow.
367 test_long_and_overflow(PyObject
*self
)
369 PyObject
*num
, *one
, *temp
;
373 /* Test that overflow is set properly for a large value. */
374 /* num is a number larger than LONG_MAX even on 64-bit platforms */
375 num
= PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL
, 16);
379 value
= PyLong_AsLongAndOverflow(num
, &overflow
);
381 if (value
== -1 && PyErr_Occurred())
384 return raiseTestError("test_long_and_overflow",
385 "return value was not set to -1");
387 return raiseTestError("test_long_and_overflow",
388 "overflow was not set to 1");
390 /* Same again, with num = LONG_MAX + 1 */
391 num
= PyLong_FromLong(LONG_MAX
);
394 one
= PyLong_FromLong(1L);
399 temp
= PyNumber_Add(num
, one
);
406 value
= PyLong_AsLongAndOverflow(num
, &overflow
);
408 if (value
== -1 && PyErr_Occurred())
411 return raiseTestError("test_long_and_overflow",
412 "return value was not set to -1");
414 return raiseTestError("test_long_and_overflow",
415 "overflow was not set to 1");
417 /* Test that overflow is set properly for a large negative value. */
418 /* num is a number smaller than LONG_MIN even on 64-bit platforms */
419 num
= PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL
, 16);
423 value
= PyLong_AsLongAndOverflow(num
, &overflow
);
425 if (value
== -1 && PyErr_Occurred())
428 return raiseTestError("test_long_and_overflow",
429 "return value was not set to -1");
431 return raiseTestError("test_long_and_overflow",
432 "overflow was not set to -1");
434 /* Same again, with num = LONG_MIN - 1 */
435 num
= PyLong_FromLong(LONG_MIN
);
438 one
= PyLong_FromLong(1L);
443 temp
= PyNumber_Subtract(num
, one
);
450 value
= PyLong_AsLongAndOverflow(num
, &overflow
);
452 if (value
== -1 && PyErr_Occurred())
455 return raiseTestError("test_long_and_overflow",
456 "return value was not set to -1");
458 return raiseTestError("test_long_and_overflow",
459 "overflow was not set to -1");
461 /* Test that overflow is cleared properly for small values. */
462 num
= PyLong_FromString("FF", NULL
, 16);
466 value
= PyLong_AsLongAndOverflow(num
, &overflow
);
468 if (value
== -1 && PyErr_Occurred())
471 return raiseTestError("test_long_and_overflow",
472 "expected return value 0xFF");
474 return raiseTestError("test_long_and_overflow",
475 "overflow was not cleared");
477 num
= PyLong_FromString("-FF", NULL
, 16);
481 value
= PyLong_AsLongAndOverflow(num
, &overflow
);
483 if (value
== -1 && PyErr_Occurred())
486 return raiseTestError("test_long_and_overflow",
487 "expected return value 0xFF");
489 return raiseTestError("test_long_and_overflow",
490 "overflow was set incorrectly");
492 num
= PyLong_FromLong(LONG_MAX
);
496 value
= PyLong_AsLongAndOverflow(num
, &overflow
);
498 if (value
== -1 && PyErr_Occurred())
500 if (value
!= LONG_MAX
)
501 return raiseTestError("test_long_and_overflow",
502 "expected return value LONG_MAX");
504 return raiseTestError("test_long_and_overflow",
505 "overflow was not cleared");
507 num
= PyLong_FromLong(LONG_MIN
);
511 value
= PyLong_AsLongAndOverflow(num
, &overflow
);
513 if (value
== -1 && PyErr_Occurred())
515 if (value
!= LONG_MIN
)
516 return raiseTestError("test_long_and_overflow",
517 "expected return value LONG_MIN");
519 return raiseTestError("test_long_and_overflow",
520 "overflow was not cleared");
526 /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
527 for both long and int arguments. The test may leak a little memory if
531 test_L_code(PyObject
*self
)
533 PyObject
*tuple
, *num
;
536 tuple
= PyTuple_New(1);
540 num
= PyLong_FromLong(42);
544 PyTuple_SET_ITEM(tuple
, 0, num
);
547 if (PyArg_ParseTuple(tuple
, "L:test_L_code", &value
) < 0)
550 return raiseTestError("test_L_code",
551 "L code returned wrong value for long 42");
554 num
= PyInt_FromLong(42);
558 PyTuple_SET_ITEM(tuple
, 0, num
);
561 if (PyArg_ParseTuple(tuple
, "L:test_L_code", &value
) < 0)
564 return raiseTestError("test_L_code",
565 "L code returned wrong value for int 42");
572 #endif /* ifdef HAVE_LONG_LONG */
574 /* Test tuple argument processing */
576 getargs_tuple(PyObject
*self
, PyObject
*args
)
579 if (!PyArg_ParseTuple(args
, "i(ii)", &a
, &b
, &c
))
581 return Py_BuildValue("iii", a
, b
, c
);
584 /* test PyArg_ParseTupleAndKeywords */
585 static PyObject
*getargs_keywords(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
587 static char *keywords
[] = {"arg1","arg2","arg3","arg4","arg5", NULL
};
588 static char *fmt
="(ii)i|(i(ii))(iii)i";
589 int int_args
[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
591 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, fmt
, keywords
,
592 &int_args
[0], &int_args
[1], &int_args
[2], &int_args
[3], &int_args
[4],
593 &int_args
[5], &int_args
[6], &int_args
[7], &int_args
[8], &int_args
[9]))
595 return Py_BuildValue("iiiiiiiiii",
596 int_args
[0], int_args
[1], int_args
[2], int_args
[3], int_args
[4],
597 int_args
[5], int_args
[6], int_args
[7], int_args
[8], int_args
[9]);
600 /* Functions to call PyArg_ParseTuple with integer format codes,
601 and return the result.
604 getargs_b(PyObject
*self
, PyObject
*args
)
607 if (!PyArg_ParseTuple(args
, "b", &value
))
609 return PyLong_FromUnsignedLong((unsigned long)value
);
613 getargs_B(PyObject
*self
, PyObject
*args
)
616 if (!PyArg_ParseTuple(args
, "B", &value
))
618 return PyLong_FromUnsignedLong((unsigned long)value
);
622 getargs_h(PyObject
*self
, PyObject
*args
)
625 if (!PyArg_ParseTuple(args
, "h", &value
))
627 return PyLong_FromLong((long)value
);
631 getargs_H(PyObject
*self
, PyObject
*args
)
633 unsigned short value
;
634 if (!PyArg_ParseTuple(args
, "H", &value
))
636 return PyLong_FromUnsignedLong((unsigned long)value
);
640 getargs_I(PyObject
*self
, PyObject
*args
)
643 if (!PyArg_ParseTuple(args
, "I", &value
))
645 return PyLong_FromUnsignedLong((unsigned long)value
);
649 getargs_k(PyObject
*self
, PyObject
*args
)
652 if (!PyArg_ParseTuple(args
, "k", &value
))
654 return PyLong_FromUnsignedLong(value
);
658 getargs_i(PyObject
*self
, PyObject
*args
)
661 if (!PyArg_ParseTuple(args
, "i", &value
))
663 return PyLong_FromLong((long)value
);
667 getargs_l(PyObject
*self
, PyObject
*args
)
670 if (!PyArg_ParseTuple(args
, "l", &value
))
672 return PyLong_FromLong(value
);
676 getargs_n(PyObject
*self
, PyObject
*args
)
679 if (!PyArg_ParseTuple(args
, "n", &value
))
681 return PyInt_FromSsize_t(value
);
684 #ifdef HAVE_LONG_LONG
686 getargs_L(PyObject
*self
, PyObject
*args
)
689 if (!PyArg_ParseTuple(args
, "L", &value
))
691 return PyLong_FromLongLong(value
);
695 getargs_K(PyObject
*self
, PyObject
*args
)
697 unsigned PY_LONG_LONG value
;
698 if (!PyArg_ParseTuple(args
, "K", &value
))
700 return PyLong_FromUnsignedLongLong(value
);
704 /* This function not only tests the 'k' getargs code, but also the
705 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
707 test_k_code(PyObject
*self
)
709 PyObject
*tuple
, *num
;
712 tuple
= PyTuple_New(1);
716 /* a number larger than ULONG_MAX even on 64-bit platforms */
717 num
= PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL
, 16);
721 value
= PyInt_AsUnsignedLongMask(num
);
722 if (value
!= ULONG_MAX
)
723 return raiseTestError("test_k_code",
724 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
726 PyTuple_SET_ITEM(tuple
, 0, num
);
729 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
731 if (value
!= ULONG_MAX
)
732 return raiseTestError("test_k_code",
733 "k code returned wrong value for long 0xFFF...FFF");
736 num
= PyLong_FromString("-FFFFFFFF000000000000000042", NULL
, 16);
740 value
= PyInt_AsUnsignedLongMask(num
);
741 if (value
!= (unsigned long)-0x42)
742 return raiseTestError("test_k_code",
743 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
745 PyTuple_SET_ITEM(tuple
, 0, num
);
748 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
750 if (value
!= (unsigned long)-0x42)
751 return raiseTestError("test_k_code",
752 "k code returned wrong value for long -0xFFF..000042");
759 #ifdef Py_USING_UNICODE
761 static volatile int x
;
763 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
767 test_u_code(PyObject
*self
)
769 PyObject
*tuple
, *obj
;
773 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
774 /* Just use the macro and check that it compiles */
775 x
= Py_UNICODE_ISSPACE(25);
777 tuple
= PyTuple_New(1);
781 obj
= PyUnicode_Decode("test", strlen("test"),
786 PyTuple_SET_ITEM(tuple
, 0, obj
);
789 if (PyArg_ParseTuple(tuple
, "u:test_u_code", &value
) < 0)
791 if (value
!= PyUnicode_AS_UNICODE(obj
))
792 return raiseTestError("test_u_code",
793 "u code returned wrong value for u'test'");
795 if (PyArg_ParseTuple(tuple
, "u#:test_u_code", &value
, &len
) < 0)
797 if (value
!= PyUnicode_AS_UNICODE(obj
) ||
798 len
!= PyUnicode_GET_SIZE(obj
))
799 return raiseTestError("test_u_code",
800 "u# code returned wrong values for u'test'");
808 test_widechar(PyObject
*self
)
810 #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
811 const wchar_t wtext
[2] = {(wchar_t)0x10ABCDu
};
814 const wchar_t wtext
[3] = {(wchar_t)0xDBEAu
, (wchar_t)0xDFCDu
};
817 PyObject
*wide
, *utf8
;
819 wide
= PyUnicode_FromWideChar(wtext
, wtextlen
);
823 utf8
= PyUnicode_FromString("\xf4\x8a\xaf\x8d");
829 if (PyUnicode_GET_SIZE(wide
) != PyUnicode_GET_SIZE(utf8
)) {
832 return raiseTestError("test_widechar",
833 "wide string and utf8 string have different length");
835 if (PyUnicode_Compare(wide
, utf8
)) {
838 if (PyErr_Occurred())
840 return raiseTestError("test_widechar",
841 "wide string and utf8 string are differents");
850 test_empty_argparse(PyObject
*self
)
852 /* Test that formats can begin with '|'. See issue #4720. */
853 PyObject
*tuple
, *dict
= NULL
;
854 static char *kwlist
[] = {NULL
};
856 tuple
= PyTuple_New(0);
859 if ((result
= PyArg_ParseTuple(tuple
, "|:test_empty_argparse")) < 0)
864 result
= PyArg_ParseTupleAndKeywords(tuple
, dict
, "|:test_empty_argparse", kwlist
);
876 codec_incrementalencoder(PyObject
*self
, PyObject
*args
)
878 const char *encoding
, *errors
= NULL
;
879 if (!PyArg_ParseTuple(args
, "s|s:test_incrementalencoder",
882 return PyCodec_IncrementalEncoder(encoding
, errors
);
886 codec_incrementaldecoder(PyObject
*self
, PyObject
*args
)
888 const char *encoding
, *errors
= NULL
;
889 if (!PyArg_ParseTuple(args
, "s|s:test_incrementaldecoder",
892 return PyCodec_IncrementalDecoder(encoding
, errors
);
897 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
899 test_long_numbits(PyObject
*self
)
905 } testcases
[] = {{0, 0, 0},
914 {0x7fffL
, 15, 1}, /* one Python long digit */
919 {-0xfffffffL
, 28, -1}};
922 for (i
= 0; i
< sizeof(testcases
) / sizeof(struct triple
); ++i
) {
923 PyObject
*plong
= PyLong_FromLong(testcases
[i
].input
);
924 size_t nbits
= _PyLong_NumBits(plong
);
925 int sign
= _PyLong_Sign(plong
);
928 if (nbits
!= testcases
[i
].nbits
)
929 return raiseTestError("test_long_numbits",
930 "wrong result for _PyLong_NumBits");
931 if (sign
!= testcases
[i
].sign
)
932 return raiseTestError("test_long_numbits",
933 "wrong result for _PyLong_Sign");
939 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
942 test_null_strings(PyObject
*self
)
944 PyObject
*o1
= PyObject_Str(NULL
), *o2
= PyObject_Unicode(NULL
);
945 PyObject
*tuple
= PyTuple_Pack(2, o1
, o2
);
952 raise_exception(PyObject
*self
, PyObject
*args
)
955 PyObject
*exc_args
, *v
;
958 if (!PyArg_ParseTuple(args
, "Oi:raise_exception",
961 if (!PyExceptionClass_Check(exc
)) {
962 PyErr_Format(PyExc_TypeError
, "an exception class is required");
966 exc_args
= PyTuple_New(num_args
);
967 if (exc_args
== NULL
)
969 for (i
= 0; i
< num_args
; ++i
) {
970 v
= PyInt_FromLong(i
);
975 PyTuple_SET_ITEM(exc_args
, i
, v
);
977 PyErr_SetObject(exc
, exc_args
);
985 test_datetime_capi(PyObject
*self
, PyObject
*args
) {
987 PyErr_SetString(PyExc_AssertionError
,
988 "PyDateTime_CAPI somehow initialized");
998 /* test_thread_state spawns a thread of its own, and that thread releases
999 * `thread_done` when it's finished. The driver code has to know when the
1000 * thread finishes, because the thread uses a PyObject (the callable) that
1001 * may go away when the driver finishes. The former lack of this explicit
1002 * synchronization caused rare segfaults, so rare that they were seen only
1003 * on a Mac buildbot (although they were possible on any box).
1005 static PyThread_type_lock thread_done
= NULL
;
1008 _make_call(void *callable
)
1012 PyGILState_STATE s
= PyGILState_Ensure();
1013 rc
= PyObject_CallFunction((PyObject
*)callable
, "");
1014 success
= (rc
!= NULL
);
1016 PyGILState_Release(s
);
1020 /* Same thing, but releases `thread_done` when it returns. This variant
1021 * should be called only from threads spawned by test_thread_state().
1024 _make_call_from_thread(void *callable
)
1026 _make_call(callable
);
1027 PyThread_release_lock(thread_done
);
1031 test_thread_state(PyObject
*self
, PyObject
*args
)
1036 if (!PyArg_ParseTuple(args
, "O:test_thread_state", &fn
))
1039 if (!PyCallable_Check(fn
)) {
1040 PyErr_Format(PyExc_TypeError
, "'%s' object is not callable",
1041 fn
->ob_type
->tp_name
);
1045 /* Ensure Python is set up for threading */
1046 PyEval_InitThreads();
1047 thread_done
= PyThread_allocate_lock();
1048 if (thread_done
== NULL
)
1049 return PyErr_NoMemory();
1050 PyThread_acquire_lock(thread_done
, 1);
1052 /* Start a new thread with our callback. */
1053 PyThread_start_new_thread(_make_call_from_thread
, fn
);
1054 /* Make the callback with the thread lock held by this thread */
1055 success
&= _make_call(fn
);
1056 /* Do it all again, but this time with the thread-lock released */
1057 Py_BEGIN_ALLOW_THREADS
1058 success
&= _make_call(fn
);
1059 PyThread_acquire_lock(thread_done
, 1); /* wait for thread to finish */
1060 Py_END_ALLOW_THREADS
1062 /* And once more with and without a thread
1063 XXX - should use a lock and work out exactly what we are trying
1066 Py_BEGIN_ALLOW_THREADS
1067 PyThread_start_new_thread(_make_call_from_thread
, fn
);
1068 success
&= _make_call(fn
);
1069 PyThread_acquire_lock(thread_done
, 1); /* wait for thread to finish */
1070 Py_END_ALLOW_THREADS
1072 /* Release lock we acquired above. This is required on HP-UX. */
1073 PyThread_release_lock(thread_done
);
1075 PyThread_free_lock(thread_done
);
1081 /* test Py_AddPendingCalls using threads */
1082 static int _pending_callback(void *arg
)
1084 /* we assume the argument is callable object to which we own a reference */
1085 PyObject
*callable
= (PyObject
*)arg
;
1086 PyObject
*r
= PyObject_CallObject(callable
, NULL
);
1087 Py_DECREF(callable
);
1089 return r
!= NULL
? 0 : -1;
1092 /* The following requests n callbacks to _pending_callback. It can be
1093 * run from any python thread.
1095 PyObject
*pending_threadfunc(PyObject
*self
, PyObject
*arg
)
1099 if (PyArg_ParseTuple(arg
, "O", &callable
) == 0)
1102 /* create the reference for the callbackwhile we hold the lock */
1103 Py_INCREF(callable
);
1105 Py_BEGIN_ALLOW_THREADS
1106 r
= Py_AddPendingCall(&_pending_callback
, callable
);
1107 Py_END_ALLOW_THREADS
1110 Py_DECREF(callable
); /* unsuccessful add, destroy the extra reference */
1111 Py_INCREF(Py_False
);
1119 /* Some tests of PyString_FromFormat(). This needs more tests. */
1121 test_string_from_format(PyObject
*self
, PyObject
*args
)
1126 #define CHECK_1_FORMAT(FORMAT, TYPE) \
1127 result = PyString_FromFormat(FORMAT, (TYPE)1); \
1128 if (result == NULL) \
1130 if (strcmp(PyString_AsString(result), "1")) { \
1131 msg = FORMAT " failed at 1"; \
1136 CHECK_1_FORMAT("%d", int);
1137 CHECK_1_FORMAT("%ld", long);
1138 /* The z width modifier was added in Python 2.5. */
1139 CHECK_1_FORMAT("%zd", Py_ssize_t
);
1141 /* The u type code was added in Python 2.5. */
1142 CHECK_1_FORMAT("%u", unsigned int);
1143 CHECK_1_FORMAT("%lu", unsigned long);
1144 CHECK_1_FORMAT("%zu", size_t);
1146 /* "%lld" and "%llu" support added in Python 2.7. */
1147 #ifdef HAVE_LONG_LONG
1148 CHECK_1_FORMAT("%llu", unsigned PY_LONG_LONG
);
1149 CHECK_1_FORMAT("%lld", PY_LONG_LONG
);
1156 return raiseTestError("test_string_from_format", msg
);
1158 #undef CHECK_1_FORMAT
1161 /* This is here to provide a docstring for test_descr. */
1163 test_with_docstring(PyObject
*self
)
1168 /* To test the format of tracebacks as printed out. */
1170 traceback_print(PyObject
*self
, PyObject
*args
)
1173 PyObject
*traceback
;
1176 if (!PyArg_ParseTuple(args
, "OO:traceback_print",
1180 result
= PyTraceBack_Print(traceback
, file
);
1186 /* To test that the result of PyCode_NewEmpty has the right members. */
1188 code_newempty(PyObject
*self
, PyObject
*args
)
1190 const char *filename
;
1191 const char *funcname
;
1194 if (!PyArg_ParseTuple(args
, "ssi:code_newempty",
1195 &filename
, &funcname
, &firstlineno
))
1198 return (PyObject
*)PyCode_NewEmpty(filename
, funcname
, firstlineno
);
1201 /* Test PyErr_NewExceptionWithDoc (also exercise PyErr_NewException).
1202 Run via Lib/test/test_exceptions.py */
1204 make_exception_with_doc(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1208 PyObject
*base
= NULL
;
1209 PyObject
*dict
= NULL
;
1211 static char *kwlist
[] = {"name", "doc", "base", "dict", NULL
};
1213 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
,
1214 "s|sOO:make_exception_with_doc", kwlist
,
1215 &name
, &doc
, &base
, &dict
))
1218 return PyErr_NewExceptionWithDoc(name
, doc
, base
, dict
);
1221 static PyMethodDef TestMethods
[] = {
1222 {"raise_exception", raise_exception
, METH_VARARGS
},
1223 {"test_config", (PyCFunction
)test_config
, METH_NOARGS
},
1224 {"test_datetime_capi", test_datetime_capi
, METH_NOARGS
},
1225 {"test_list_api", (PyCFunction
)test_list_api
, METH_NOARGS
},
1226 {"test_dict_iteration", (PyCFunction
)test_dict_iteration
,METH_NOARGS
},
1227 {"test_lazy_hash_inheritance", (PyCFunction
)test_lazy_hash_inheritance
,METH_NOARGS
},
1228 {"test_long_api", (PyCFunction
)test_long_api
, METH_NOARGS
},
1229 {"test_long_and_overflow", (PyCFunction
)test_long_and_overflow
,
1231 {"test_long_numbits", (PyCFunction
)test_long_numbits
, METH_NOARGS
},
1232 {"test_k_code", (PyCFunction
)test_k_code
, METH_NOARGS
},
1233 {"test_empty_argparse", (PyCFunction
)test_empty_argparse
,METH_NOARGS
},
1234 {"test_null_strings", (PyCFunction
)test_null_strings
, METH_NOARGS
},
1235 {"test_string_from_format", (PyCFunction
)test_string_from_format
, METH_NOARGS
},
1236 {"test_with_docstring", (PyCFunction
)test_with_docstring
, METH_NOARGS
,
1237 PyDoc_STR("This is a pretty normal docstring.")},
1239 {"getargs_tuple", getargs_tuple
, METH_VARARGS
},
1240 {"getargs_keywords", (PyCFunction
)getargs_keywords
,
1241 METH_VARARGS
|METH_KEYWORDS
},
1242 {"getargs_b", getargs_b
, METH_VARARGS
},
1243 {"getargs_B", getargs_B
, METH_VARARGS
},
1244 {"getargs_h", getargs_h
, METH_VARARGS
},
1245 {"getargs_H", getargs_H
, METH_VARARGS
},
1246 {"getargs_I", getargs_I
, METH_VARARGS
},
1247 {"getargs_k", getargs_k
, METH_VARARGS
},
1248 {"getargs_i", getargs_i
, METH_VARARGS
},
1249 {"getargs_l", getargs_l
, METH_VARARGS
},
1250 {"getargs_n", getargs_n
, METH_VARARGS
},
1251 #ifdef HAVE_LONG_LONG
1252 {"getargs_L", getargs_L
, METH_VARARGS
},
1253 {"getargs_K", getargs_K
, METH_VARARGS
},
1254 {"test_longlong_api", test_longlong_api
, METH_NOARGS
},
1255 {"test_L_code", (PyCFunction
)test_L_code
, METH_NOARGS
},
1256 {"codec_incrementalencoder",
1257 (PyCFunction
)codec_incrementalencoder
, METH_VARARGS
},
1258 {"codec_incrementaldecoder",
1259 (PyCFunction
)codec_incrementaldecoder
, METH_VARARGS
},
1261 #ifdef Py_USING_UNICODE
1262 {"test_u_code", (PyCFunction
)test_u_code
, METH_NOARGS
},
1263 {"test_widechar", (PyCFunction
)test_widechar
, METH_NOARGS
},
1266 {"_test_thread_state", test_thread_state
, METH_VARARGS
},
1267 {"_pending_threadfunc", pending_threadfunc
, METH_VARARGS
},
1269 {"traceback_print", traceback_print
, METH_VARARGS
},
1270 {"code_newempty", code_newempty
, METH_VARARGS
},
1271 {"make_exception_with_doc", (PyCFunction
)make_exception_with_doc
,
1272 METH_VARARGS
| METH_KEYWORDS
},
1273 {NULL
, NULL
} /* sentinel */
1276 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
1281 unsigned char ubyte_member
;
1283 unsigned short ushort_member
;
1285 unsigned int uint_member
;
1287 unsigned long ulong_member
;
1289 double double_member
;
1290 #ifdef HAVE_LONG_LONG
1291 PY_LONG_LONG longlong_member
;
1292 unsigned PY_LONG_LONG ulonglong_member
;
1294 } all_structmembers
;
1298 all_structmembers structmembers
;
1299 } test_structmembers
;
1301 static struct PyMemberDef test_members
[] = {
1302 {"T_BOOL", T_BOOL
, offsetof(test_structmembers
, structmembers
.bool_member
), 0, NULL
},
1303 {"T_BYTE", T_BYTE
, offsetof(test_structmembers
, structmembers
.byte_member
), 0, NULL
},
1304 {"T_UBYTE", T_UBYTE
, offsetof(test_structmembers
, structmembers
.ubyte_member
), 0, NULL
},
1305 {"T_SHORT", T_SHORT
, offsetof(test_structmembers
, structmembers
.short_member
), 0, NULL
},
1306 {"T_USHORT", T_USHORT
, offsetof(test_structmembers
, structmembers
.ushort_member
), 0, NULL
},
1307 {"T_INT", T_INT
, offsetof(test_structmembers
, structmembers
.int_member
), 0, NULL
},
1308 {"T_UINT", T_UINT
, offsetof(test_structmembers
, structmembers
.uint_member
), 0, NULL
},
1309 {"T_LONG", T_LONG
, offsetof(test_structmembers
, structmembers
.long_member
), 0, NULL
},
1310 {"T_ULONG", T_ULONG
, offsetof(test_structmembers
, structmembers
.ulong_member
), 0, NULL
},
1311 {"T_FLOAT", T_FLOAT
, offsetof(test_structmembers
, structmembers
.float_member
), 0, NULL
},
1312 {"T_DOUBLE", T_DOUBLE
, offsetof(test_structmembers
, structmembers
.double_member
), 0, NULL
},
1313 #ifdef HAVE_LONG_LONG
1314 {"T_LONGLONG", T_LONGLONG
, offsetof(test_structmembers
, structmembers
.longlong_member
), 0, NULL
},
1315 {"T_ULONGLONG", T_ULONGLONG
, offsetof(test_structmembers
, structmembers
.ulonglong_member
), 0, NULL
},
1322 test_structmembers_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
1324 static char *keywords
[] = {
1325 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
1326 "T_INT", "T_UINT", "T_LONG", "T_ULONG",
1327 "T_FLOAT", "T_DOUBLE",
1328 #ifdef HAVE_LONG_LONG
1329 "T_LONGLONG", "T_ULONGLONG",
1332 static char *fmt
= "|bbBhHiIlkfd"
1333 #ifdef HAVE_LONG_LONG
1337 test_structmembers
*ob
;
1338 ob
= PyObject_New(test_structmembers
, type
);
1341 memset(&ob
->structmembers
, 0, sizeof(all_structmembers
));
1342 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, fmt
, keywords
,
1343 &ob
->structmembers
.bool_member
,
1344 &ob
->structmembers
.byte_member
,
1345 &ob
->structmembers
.ubyte_member
,
1346 &ob
->structmembers
.short_member
,
1347 &ob
->structmembers
.ushort_member
,
1348 &ob
->structmembers
.int_member
,
1349 &ob
->structmembers
.uint_member
,
1350 &ob
->structmembers
.long_member
,
1351 &ob
->structmembers
.ulong_member
,
1352 &ob
->structmembers
.float_member
,
1353 &ob
->structmembers
.double_member
1354 #ifdef HAVE_LONG_LONG
1355 , &ob
->structmembers
.longlong_member
,
1356 &ob
->structmembers
.ulonglong_member
1362 return (PyObject
*)ob
;
1366 test_structmembers_free(PyObject
*ob
)
1371 static PyTypeObject test_structmembersType
= {
1372 PyVarObject_HEAD_INIT(NULL
, 0)
1373 "test_structmembersType",
1374 sizeof(test_structmembers
), /* tp_basicsize */
1375 0, /* tp_itemsize */
1376 test_structmembers_free
, /* destructor tp_dealloc */
1382 0, /* tp_as_number */
1383 0, /* tp_as_sequence */
1384 0, /* tp_as_mapping */
1388 PyObject_GenericGetAttr
, /* tp_getattro */
1389 PyObject_GenericSetAttr
, /* tp_setattro */
1390 0, /* tp_as_buffer */
1392 "Type containing all structmember types",
1393 0, /* traverseproc tp_traverse */
1395 0, /* tp_richcompare */
1396 0, /* tp_weaklistoffset */
1398 0, /* tp_iternext */
1400 test_members
, /* tp_members */
1409 test_structmembers_new
, /* tp_new */
1418 m
= Py_InitModule("_testcapi", TestMethods
);
1422 Py_TYPE(&_HashInheritanceTester_Type
)=&PyType_Type
;
1424 Py_TYPE(&test_structmembersType
)=&PyType_Type
;
1425 Py_INCREF(&test_structmembersType
);
1426 PyModule_AddObject(m
, "test_structmembersType", (PyObject
*)&test_structmembersType
);
1428 PyModule_AddObject(m
, "CHAR_MAX", PyInt_FromLong(CHAR_MAX
));
1429 PyModule_AddObject(m
, "CHAR_MIN", PyInt_FromLong(CHAR_MIN
));
1430 PyModule_AddObject(m
, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX
));
1431 PyModule_AddObject(m
, "SHRT_MAX", PyInt_FromLong(SHRT_MAX
));
1432 PyModule_AddObject(m
, "SHRT_MIN", PyInt_FromLong(SHRT_MIN
));
1433 PyModule_AddObject(m
, "USHRT_MAX", PyInt_FromLong(USHRT_MAX
));
1434 PyModule_AddObject(m
, "INT_MAX", PyLong_FromLong(INT_MAX
));
1435 PyModule_AddObject(m
, "INT_MIN", PyLong_FromLong(INT_MIN
));
1436 PyModule_AddObject(m
, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX
));
1437 PyModule_AddObject(m
, "LONG_MAX", PyInt_FromLong(LONG_MAX
));
1438 PyModule_AddObject(m
, "LONG_MIN", PyInt_FromLong(LONG_MIN
));
1439 PyModule_AddObject(m
, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX
));
1440 PyModule_AddObject(m
, "FLT_MAX", PyFloat_FromDouble(FLT_MAX
));
1441 PyModule_AddObject(m
, "FLT_MIN", PyFloat_FromDouble(FLT_MIN
));
1442 PyModule_AddObject(m
, "DBL_MAX", PyFloat_FromDouble(DBL_MAX
));
1443 PyModule_AddObject(m
, "DBL_MIN", PyFloat_FromDouble(DBL_MIN
));
1444 PyModule_AddObject(m
, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX
));
1445 PyModule_AddObject(m
, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN
));
1446 PyModule_AddObject(m
, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX
));
1447 PyModule_AddObject(m
, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX
));
1448 PyModule_AddObject(m
, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN
));
1449 PyModule_AddObject(m
, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head
)));
1451 TestError
= PyErr_NewException("_testcapi.error", NULL
, NULL
);
1452 Py_INCREF(TestError
);
1453 PyModule_AddObject(m
, "error", TestError
);