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.
12 #endif /* WITH_THREAD */
13 static PyObject
*TestError
; /* set to exception object in init */
15 /* Raise TestError with test_name + ": " + msg, and return NULL. */
18 raiseTestError(const char* test_name
, const char* msg
)
22 if (strlen(test_name
) + strlen(msg
) > sizeof(buf
) - 50)
23 PyErr_SetString(TestError
, "internal error msg too large");
25 PyOS_snprintf(buf
, sizeof(buf
), "%s: %s", test_name
, msg
);
26 PyErr_SetString(TestError
, buf
);
31 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
33 The ones derived from autoconf on the UNIX-like OSes can be relied
34 upon (in the absence of sloppy cross-compiling), but the Windows
35 platforms have these hardcoded. Better safe than sorry.
38 sizeof_error(const char* fatname
, const char* typename
,
39 int expected
, int got
)
42 PyOS_snprintf(buf
, sizeof(buf
),
43 "%.200s #define == %d but sizeof(%.200s) == %d",
44 fatname
, expected
, typename
, got
);
45 PyErr_SetString(TestError
, buf
);
46 return (PyObject
*)NULL
;
50 test_config(PyObject
*self
)
52 #define CHECK_SIZEOF(FATNAME, TYPE) \
53 if (FATNAME != sizeof(TYPE)) \
54 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
56 CHECK_SIZEOF(SIZEOF_SHORT
, short);
57 CHECK_SIZEOF(SIZEOF_INT
, int);
58 CHECK_SIZEOF(SIZEOF_LONG
, long);
59 CHECK_SIZEOF(SIZEOF_VOID_P
, void*);
60 CHECK_SIZEOF(SIZEOF_TIME_T
, time_t);
62 CHECK_SIZEOF(SIZEOF_LONG_LONG
, PY_LONG_LONG
);
72 test_list_api(PyObject
*self
)
77 /* SF bug 132008: PyList_Reverse segfaults */
79 list
= PyList_New(NLIST
);
80 if (list
== (PyObject
*)NULL
)
81 return (PyObject
*)NULL
;
82 /* list = range(NLIST) */
83 for (i
= 0; i
< NLIST
; ++i
) {
84 PyObject
* anint
= PyInt_FromLong(i
);
85 if (anint
== (PyObject
*)NULL
) {
87 return (PyObject
*)NULL
;
89 PyList_SET_ITEM(list
, i
, anint
);
91 /* list.reverse(), via PyList_Reverse() */
92 i
= PyList_Reverse(list
); /* should not blow up! */
95 return (PyObject
*)NULL
;
97 /* Check that list == range(29, -1, -1) now */
98 for (i
= 0; i
< NLIST
; ++i
) {
99 PyObject
* anint
= PyList_GET_ITEM(list
, i
);
100 if (PyInt_AS_LONG(anint
) != NLIST
-1-i
) {
101 PyErr_SetString(TestError
,
102 "test_list_api: reverse screwed up");
104 return (PyObject
*)NULL
;
115 test_dict_inner(int count
)
117 Py_ssize_t pos
= 0, iterations
= 0;
119 PyObject
*dict
= PyDict_New();
125 for (i
= 0; i
< count
; i
++) {
126 v
= PyInt_FromLong(i
);
127 PyDict_SetItem(dict
, v
, v
);
131 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
135 i
= PyInt_AS_LONG(v
) + 1;
136 o
= PyInt_FromLong(i
);
139 if (PyDict_SetItem(dict
, k
, o
) < 0) {
148 if (iterations
!= count
) {
151 "test_dict_iteration: dict iteration went wrong ");
159 test_dict_iteration(PyObject
* self
)
163 for (i
= 0; i
< 200; i
++) {
164 if (test_dict_inner(i
) < 0) {
174 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
175 PyLong_{As, From}{Unsigned,}LongLong().
177 Note that the meat of the test is contained in testcapi_long.h.
178 This is revolting, but delicate code duplication is worse: "almost
179 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
180 dependence on type names makes it impossible to use a parameterized
181 function. A giant macro would be even worse than this. A C++ template
184 The "report an error" functions are deliberately not part of the #include
185 file: if the test fails, you can set a breakpoint in the appropriate
186 error function directly, and crawl back from there in the debugger.
189 #define UNBIND(X) Py_DECREF(X); (X) = NULL
192 raise_test_long_error(const char* msg
)
194 return raiseTestError("test_long_api", msg
);
197 #define TESTNAME test_long_api_inner
198 #define TYPENAME long
199 #define F_S_TO_PY PyLong_FromLong
200 #define F_PY_TO_S PyLong_AsLong
201 #define F_U_TO_PY PyLong_FromUnsignedLong
202 #define F_PY_TO_U PyLong_AsUnsignedLong
204 #include "testcapi_long.h"
207 test_long_api(PyObject
* self
)
209 return TESTNAME(raise_test_long_error
);
219 #ifdef HAVE_LONG_LONG
222 raise_test_longlong_error(const char* msg
)
224 return raiseTestError("test_longlong_api", msg
);
227 #define TESTNAME test_longlong_api_inner
228 #define TYPENAME PY_LONG_LONG
229 #define F_S_TO_PY PyLong_FromLongLong
230 #define F_PY_TO_S PyLong_AsLongLong
231 #define F_U_TO_PY PyLong_FromUnsignedLongLong
232 #define F_PY_TO_U PyLong_AsUnsignedLongLong
234 #include "testcapi_long.h"
237 test_longlong_api(PyObject
* self
, PyObject
*args
)
239 return TESTNAME(raise_test_longlong_error
);
249 /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
250 for both long and int arguments. The test may leak a little memory if
254 test_L_code(PyObject
*self
)
256 PyObject
*tuple
, *num
;
259 tuple
= PyTuple_New(1);
263 num
= PyLong_FromLong(42);
267 PyTuple_SET_ITEM(tuple
, 0, num
);
270 if (PyArg_ParseTuple(tuple
, "L:test_L_code", &value
) < 0)
273 return raiseTestError("test_L_code",
274 "L code returned wrong value for long 42");
277 num
= PyInt_FromLong(42);
281 PyTuple_SET_ITEM(tuple
, 0, num
);
284 if (PyArg_ParseTuple(tuple
, "L:test_L_code", &value
) < 0)
287 return raiseTestError("test_L_code",
288 "L code returned wrong value for int 42");
295 #endif /* ifdef HAVE_LONG_LONG */
297 /* Test tuple argument processing */
299 getargs_tuple(PyObject
*self
, PyObject
*args
)
302 if (!PyArg_ParseTuple(args
, "i(ii)", &a
, &b
, &c
))
304 return Py_BuildValue("iii", a
, b
, c
);
307 /* Functions to call PyArg_ParseTuple with integer format codes,
308 and return the result.
311 getargs_b(PyObject
*self
, PyObject
*args
)
314 if (!PyArg_ParseTuple(args
, "b", &value
))
316 return PyLong_FromUnsignedLong((unsigned long)value
);
320 getargs_B(PyObject
*self
, PyObject
*args
)
323 if (!PyArg_ParseTuple(args
, "B", &value
))
325 return PyLong_FromUnsignedLong((unsigned long)value
);
329 getargs_H(PyObject
*self
, PyObject
*args
)
331 unsigned short value
;
332 if (!PyArg_ParseTuple(args
, "H", &value
))
334 return PyLong_FromUnsignedLong((unsigned long)value
);
338 getargs_I(PyObject
*self
, PyObject
*args
)
341 if (!PyArg_ParseTuple(args
, "I", &value
))
343 return PyLong_FromUnsignedLong((unsigned long)value
);
347 getargs_k(PyObject
*self
, PyObject
*args
)
350 if (!PyArg_ParseTuple(args
, "k", &value
))
352 return PyLong_FromUnsignedLong(value
);
356 getargs_i(PyObject
*self
, PyObject
*args
)
359 if (!PyArg_ParseTuple(args
, "i", &value
))
361 return PyLong_FromLong((long)value
);
365 getargs_l(PyObject
*self
, PyObject
*args
)
368 if (!PyArg_ParseTuple(args
, "l", &value
))
370 return PyLong_FromLong(value
);
374 getargs_n(PyObject
*self
, PyObject
*args
)
377 if (!PyArg_ParseTuple(args
, "n", &value
))
379 return PyInt_FromSsize_t(value
);
382 #ifdef HAVE_LONG_LONG
384 getargs_L(PyObject
*self
, PyObject
*args
)
387 if (!PyArg_ParseTuple(args
, "L", &value
))
389 return PyLong_FromLongLong(value
);
393 getargs_K(PyObject
*self
, PyObject
*args
)
395 unsigned PY_LONG_LONG value
;
396 if (!PyArg_ParseTuple(args
, "K", &value
))
398 return PyLong_FromUnsignedLongLong(value
);
402 /* This function not only tests the 'k' getargs code, but also the
403 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
405 test_k_code(PyObject
*self
)
407 PyObject
*tuple
, *num
;
410 tuple
= PyTuple_New(1);
414 /* a number larger than ULONG_MAX even on 64-bit platforms */
415 num
= PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL
, 16);
419 value
= PyInt_AsUnsignedLongMask(num
);
420 if (value
!= ULONG_MAX
)
421 return raiseTestError("test_k_code",
422 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
424 PyTuple_SET_ITEM(tuple
, 0, num
);
427 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
429 if (value
!= ULONG_MAX
)
430 return raiseTestError("test_k_code",
431 "k code returned wrong value for long 0xFFF...FFF");
434 num
= PyLong_FromString("-FFFFFFFF000000000000000042", NULL
, 16);
438 value
= PyInt_AsUnsignedLongMask(num
);
439 if (value
!= (unsigned long)-0x42)
440 return raiseTestError("test_k_code",
441 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
443 PyTuple_SET_ITEM(tuple
, 0, num
);
446 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
448 if (value
!= (unsigned long)-0x42)
449 return raiseTestError("test_k_code",
450 "k code returned wrong value for long -0xFFF..000042");
457 #ifdef Py_USING_UNICODE
459 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
463 test_u_code(PyObject
*self
)
465 PyObject
*tuple
, *obj
;
469 tuple
= PyTuple_New(1);
473 obj
= PyUnicode_Decode("test", strlen("test"),
478 PyTuple_SET_ITEM(tuple
, 0, obj
);
481 if (PyArg_ParseTuple(tuple
, "u:test_u_code", &value
) < 0)
483 if (value
!= PyUnicode_AS_UNICODE(obj
))
484 return raiseTestError("test_u_code",
485 "u code returned wrong value for u'test'");
487 if (PyArg_ParseTuple(tuple
, "u#:test_u_code", &value
, &len
) < 0)
489 if (value
!= PyUnicode_AS_UNICODE(obj
) ||
490 len
!= PyUnicode_GET_SIZE(obj
))
491 return raiseTestError("test_u_code",
492 "u# code returned wrong values for u'test'");
500 codec_incrementalencoder(PyObject
*self
, PyObject
*args
)
502 const char *encoding
, *errors
= NULL
;
503 if (!PyArg_ParseTuple(args
, "s|s:test_incrementalencoder",
506 return PyCodec_IncrementalEncoder(encoding
, errors
);
510 codec_incrementaldecoder(PyObject
*self
, PyObject
*args
)
512 const char *encoding
, *errors
= NULL
;
513 if (!PyArg_ParseTuple(args
, "s|s:test_incrementaldecoder",
516 return PyCodec_IncrementalDecoder(encoding
, errors
);
521 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
523 test_long_numbits(PyObject
*self
)
529 } testcases
[] = {{0, 0, 0},
538 {0x7fffL
, 15, 1}, /* one Python long digit */
543 {-0xfffffffL
, 28, -1}};
546 for (i
= 0; i
< sizeof(testcases
) / sizeof(struct triple
); ++i
) {
547 PyObject
*plong
= PyLong_FromLong(testcases
[i
].input
);
548 size_t nbits
= _PyLong_NumBits(plong
);
549 int sign
= _PyLong_Sign(plong
);
552 if (nbits
!= testcases
[i
].nbits
)
553 return raiseTestError("test_long_numbits",
554 "wrong result for _PyLong_NumBits");
555 if (sign
!= testcases
[i
].sign
)
556 return raiseTestError("test_long_numbits",
557 "wrong result for _PyLong_Sign");
563 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
566 test_null_strings(PyObject
*self
)
568 PyObject
*o1
= PyObject_Str(NULL
), *o2
= PyObject_Unicode(NULL
);
569 PyObject
*tuple
= PyTuple_Pack(2, o1
, o2
);
576 raise_exception(PyObject
*self
, PyObject
*args
)
579 PyObject
*exc_args
, *v
;
582 if (!PyArg_ParseTuple(args
, "Oi:raise_exception",
586 exc_args
= PyTuple_New(num_args
);
587 if (exc_args
== NULL
)
589 for (i
= 0; i
< num_args
; ++i
) {
590 v
= PyInt_FromLong(i
);
595 PyTuple_SET_ITEM(exc_args
, i
, v
);
597 PyErr_SetObject(exc
, exc_args
);
604 /* test_thread_state spawns a thread of its own, and that thread releases
605 * `thread_done` when it's finished. The driver code has to know when the
606 * thread finishes, because the thread uses a PyObject (the callable) that
607 * may go away when the driver finishes. The former lack of this explicit
608 * synchronization caused rare segfaults, so rare that they were seen only
609 * on a Mac buildbot (although they were possible on any box).
611 static PyThread_type_lock thread_done
= NULL
;
614 _make_call(void *callable
)
617 PyGILState_STATE s
= PyGILState_Ensure();
618 rc
= PyObject_CallFunction(callable
, "");
620 PyGILState_Release(s
);
623 /* Same thing, but releases `thread_done` when it returns. This variant
624 * should be called only from threads spawned by test_thread_state().
627 _make_call_from_thread(void *callable
)
629 _make_call(callable
);
630 PyThread_release_lock(thread_done
);
634 test_thread_state(PyObject
*self
, PyObject
*args
)
638 if (!PyArg_ParseTuple(args
, "O:test_thread_state", &fn
))
641 /* Ensure Python is set up for threading */
642 PyEval_InitThreads();
643 thread_done
= PyThread_allocate_lock();
644 if (thread_done
== NULL
)
645 return PyErr_NoMemory();
646 PyThread_acquire_lock(thread_done
, 1);
648 /* Start a new thread with our callback. */
649 PyThread_start_new_thread(_make_call_from_thread
, fn
);
650 /* Make the callback with the thread lock held by this thread */
652 /* Do it all again, but this time with the thread-lock released */
653 Py_BEGIN_ALLOW_THREADS
655 PyThread_acquire_lock(thread_done
, 1); /* wait for thread to finish */
658 /* And once more with and without a thread
659 XXX - should use a lock and work out exactly what we are trying
662 Py_BEGIN_ALLOW_THREADS
663 PyThread_start_new_thread(_make_call_from_thread
, fn
);
665 PyThread_acquire_lock(thread_done
, 1); /* wait for thread to finish */
668 PyThread_free_lock(thread_done
);
673 /* Some tests of PyString_FromFormat(). This needs more tests. */
675 test_string_from_format(PyObject
*self
, PyObject
*args
)
680 #define CHECK_1_FORMAT(FORMAT, TYPE) \
681 result = PyString_FromFormat(FORMAT, (TYPE)1); \
682 if (result == NULL) \
684 if (strcmp(PyString_AsString(result), "1")) { \
685 msg = FORMAT " failed at 1"; \
690 CHECK_1_FORMAT("%d", int);
691 CHECK_1_FORMAT("%ld", long);
692 /* The z width modifier was added in Python 2.5. */
693 CHECK_1_FORMAT("%zd", Py_ssize_t
);
695 /* The u type code was added in Python 2.5. */
696 CHECK_1_FORMAT("%u", unsigned int);
697 CHECK_1_FORMAT("%lu", unsigned long);
698 CHECK_1_FORMAT("%zu", size_t);
704 return raiseTestError("test_string_from_format", msg
);
706 #undef CHECK_1_FORMAT
709 /* This is here to provide a docstring for test_descr. */
711 test_with_docstring(PyObject
*self
)
716 static PyMethodDef TestMethods
[] = {
717 {"raise_exception", raise_exception
, METH_VARARGS
},
718 {"test_config", (PyCFunction
)test_config
, METH_NOARGS
},
719 {"test_list_api", (PyCFunction
)test_list_api
, METH_NOARGS
},
720 {"test_dict_iteration", (PyCFunction
)test_dict_iteration
,METH_NOARGS
},
721 {"test_long_api", (PyCFunction
)test_long_api
, METH_NOARGS
},
722 {"test_long_numbits", (PyCFunction
)test_long_numbits
, METH_NOARGS
},
723 {"test_k_code", (PyCFunction
)test_k_code
, METH_NOARGS
},
724 {"test_null_strings", (PyCFunction
)test_null_strings
, METH_NOARGS
},
725 {"test_string_from_format", (PyCFunction
)test_string_from_format
, METH_NOARGS
},
726 {"test_with_docstring", (PyCFunction
)test_with_docstring
, METH_NOARGS
,
727 PyDoc_STR("This is a pretty normal docstring.")},
729 {"getargs_tuple", getargs_tuple
, METH_VARARGS
},
730 {"getargs_b", getargs_b
, METH_VARARGS
},
731 {"getargs_B", getargs_B
, METH_VARARGS
},
732 {"getargs_H", getargs_H
, METH_VARARGS
},
733 {"getargs_I", getargs_I
, METH_VARARGS
},
734 {"getargs_k", getargs_k
, METH_VARARGS
},
735 {"getargs_i", getargs_i
, METH_VARARGS
},
736 {"getargs_l", getargs_l
, METH_VARARGS
},
737 {"getargs_n", getargs_n
, METH_VARARGS
},
738 #ifdef HAVE_LONG_LONG
739 {"getargs_L", getargs_L
, METH_VARARGS
},
740 {"getargs_K", getargs_K
, METH_VARARGS
},
741 {"test_longlong_api", test_longlong_api
, METH_NOARGS
},
742 {"test_L_code", (PyCFunction
)test_L_code
, METH_NOARGS
},
743 {"codec_incrementalencoder",
744 (PyCFunction
)codec_incrementalencoder
, METH_VARARGS
},
745 {"codec_incrementaldecoder",
746 (PyCFunction
)codec_incrementaldecoder
, METH_VARARGS
},
748 #ifdef Py_USING_UNICODE
749 {"test_u_code", (PyCFunction
)test_u_code
, METH_NOARGS
},
752 {"_test_thread_state", test_thread_state
, METH_VARARGS
},
754 {NULL
, NULL
} /* sentinel */
757 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
764 m
= Py_InitModule("_testcapi", TestMethods
);
768 PyModule_AddObject(m
, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX
));
769 PyModule_AddObject(m
, "USHRT_MAX", PyInt_FromLong(USHRT_MAX
));
770 PyModule_AddObject(m
, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX
));
771 PyModule_AddObject(m
, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX
));
772 PyModule_AddObject(m
, "INT_MIN", PyInt_FromLong(INT_MIN
));
773 PyModule_AddObject(m
, "LONG_MIN", PyInt_FromLong(LONG_MIN
));
774 PyModule_AddObject(m
, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN
));
775 PyModule_AddObject(m
, "INT_MAX", PyInt_FromLong(INT_MAX
));
776 PyModule_AddObject(m
, "LONG_MAX", PyInt_FromLong(LONG_MAX
));
777 PyModule_AddObject(m
, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX
));
779 TestError
= PyErr_NewException("_testcapi.error", NULL
, NULL
);
780 Py_INCREF(TestError
);
781 PyModule_AddObject(m
, "error", TestError
);