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 /* Functions to call PyArg_ParseTuple with integer format codes,
298 and return the result.
301 getargs_b(PyObject
*self
, PyObject
*args
)
304 if (!PyArg_ParseTuple(args
, "b", &value
))
306 return PyLong_FromUnsignedLong((unsigned long)value
);
310 getargs_B(PyObject
*self
, PyObject
*args
)
313 if (!PyArg_ParseTuple(args
, "B", &value
))
315 return PyLong_FromUnsignedLong((unsigned long)value
);
319 getargs_H(PyObject
*self
, PyObject
*args
)
321 unsigned short value
;
322 if (!PyArg_ParseTuple(args
, "H", &value
))
324 return PyLong_FromUnsignedLong((unsigned long)value
);
328 getargs_I(PyObject
*self
, PyObject
*args
)
331 if (!PyArg_ParseTuple(args
, "I", &value
))
333 return PyLong_FromUnsignedLong((unsigned long)value
);
337 getargs_k(PyObject
*self
, PyObject
*args
)
340 if (!PyArg_ParseTuple(args
, "k", &value
))
342 return PyLong_FromUnsignedLong(value
);
346 getargs_i(PyObject
*self
, PyObject
*args
)
349 if (!PyArg_ParseTuple(args
, "i", &value
))
351 return PyLong_FromLong((long)value
);
355 getargs_l(PyObject
*self
, PyObject
*args
)
358 if (!PyArg_ParseTuple(args
, "l", &value
))
360 return PyLong_FromLong(value
);
364 getargs_n(PyObject
*self
, PyObject
*args
)
367 if (!PyArg_ParseTuple(args
, "n", &value
))
369 return PyInt_FromSsize_t(value
);
372 #ifdef HAVE_LONG_LONG
374 getargs_L(PyObject
*self
, PyObject
*args
)
377 if (!PyArg_ParseTuple(args
, "L", &value
))
379 return PyLong_FromLongLong(value
);
383 getargs_K(PyObject
*self
, PyObject
*args
)
385 unsigned PY_LONG_LONG value
;
386 if (!PyArg_ParseTuple(args
, "K", &value
))
388 return PyLong_FromUnsignedLongLong(value
);
392 /* This function not only tests the 'k' getargs code, but also the
393 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
395 test_k_code(PyObject
*self
)
397 PyObject
*tuple
, *num
;
400 tuple
= PyTuple_New(1);
404 /* a number larger than ULONG_MAX even on 64-bit platforms */
405 num
= PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL
, 16);
409 value
= PyInt_AsUnsignedLongMask(num
);
410 if (value
!= ULONG_MAX
)
411 return raiseTestError("test_k_code",
412 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
414 PyTuple_SET_ITEM(tuple
, 0, num
);
417 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
419 if (value
!= ULONG_MAX
)
420 return raiseTestError("test_k_code",
421 "k code returned wrong value for long 0xFFF...FFF");
424 num
= PyLong_FromString("-FFFFFFFF000000000000000042", NULL
, 16);
428 value
= PyInt_AsUnsignedLongMask(num
);
429 if (value
!= (unsigned long)-0x42)
430 return raiseTestError("test_k_code",
431 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
433 PyTuple_SET_ITEM(tuple
, 0, num
);
436 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
438 if (value
!= (unsigned long)-0x42)
439 return raiseTestError("test_k_code",
440 "k code returned wrong value for long -0xFFF..000042");
447 #ifdef Py_USING_UNICODE
449 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
453 test_u_code(PyObject
*self
)
455 PyObject
*tuple
, *obj
;
459 tuple
= PyTuple_New(1);
463 obj
= PyUnicode_Decode("test", strlen("test"),
468 PyTuple_SET_ITEM(tuple
, 0, obj
);
471 if (PyArg_ParseTuple(tuple
, "u:test_u_code", &value
) < 0)
473 if (value
!= PyUnicode_AS_UNICODE(obj
))
474 return raiseTestError("test_u_code",
475 "u code returned wrong value for u'test'");
477 if (PyArg_ParseTuple(tuple
, "u#:test_u_code", &value
, &len
) < 0)
479 if (value
!= PyUnicode_AS_UNICODE(obj
) ||
480 len
!= PyUnicode_GET_SIZE(obj
))
481 return raiseTestError("test_u_code",
482 "u# code returned wrong values for u'test'");
490 codec_incrementalencoder(PyObject
*self
, PyObject
*args
)
492 const char *encoding
, *errors
= NULL
;
493 if (!PyArg_ParseTuple(args
, "s|s:test_incrementalencoder",
496 return PyCodec_IncrementalEncoder(encoding
, errors
);
500 codec_incrementaldecoder(PyObject
*self
, PyObject
*args
)
502 const char *encoding
, *errors
= NULL
;
503 if (!PyArg_ParseTuple(args
, "s|s:test_incrementaldecoder",
506 return PyCodec_IncrementalDecoder(encoding
, errors
);
511 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
513 test_long_numbits(PyObject
*self
)
519 } testcases
[] = {{0, 0, 0},
528 {0x7fffL
, 15, 1}, /* one Python long digit */
533 {-0xfffffffL
, 28, -1}};
536 for (i
= 0; i
< sizeof(testcases
) / sizeof(struct triple
); ++i
) {
537 PyObject
*plong
= PyLong_FromLong(testcases
[i
].input
);
538 size_t nbits
= _PyLong_NumBits(plong
);
539 int sign
= _PyLong_Sign(plong
);
542 if (nbits
!= testcases
[i
].nbits
)
543 return raiseTestError("test_long_numbits",
544 "wrong result for _PyLong_NumBits");
545 if (sign
!= testcases
[i
].sign
)
546 return raiseTestError("test_long_numbits",
547 "wrong result for _PyLong_Sign");
553 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
556 test_null_strings(PyObject
*self
)
558 PyObject
*o1
= PyObject_Str(NULL
), *o2
= PyObject_Unicode(NULL
);
559 PyObject
*tuple
= PyTuple_Pack(2, o1
, o2
);
566 raise_exception(PyObject
*self
, PyObject
*args
)
569 PyObject
*exc_args
, *v
;
572 if (!PyArg_ParseTuple(args
, "Oi:raise_exception",
576 exc_args
= PyTuple_New(num_args
);
577 if (exc_args
== NULL
)
579 for (i
= 0; i
< num_args
; ++i
) {
580 v
= PyInt_FromLong(i
);
585 PyTuple_SET_ITEM(exc_args
, i
, v
);
587 PyErr_SetObject(exc
, exc_args
);
594 /* test_thread_state spawns a thread of its own, and that thread releases
595 * `thread_done` when it's finished. The driver code has to know when the
596 * thread finishes, because the thread uses a PyObject (the callable) that
597 * may go away when the driver finishes. The former lack of this explicit
598 * synchronization caused rare segfaults, so rare that they were seen only
599 * on a Mac buildbot (although they were possible on any box).
601 static PyThread_type_lock thread_done
= NULL
;
604 _make_call(void *callable
)
607 PyGILState_STATE s
= PyGILState_Ensure();
608 rc
= PyObject_CallFunction(callable
, "");
610 PyGILState_Release(s
);
613 /* Same thing, but releases `thread_done` when it returns. This variant
614 * should be called only from threads spawned by test_thread_state().
617 _make_call_from_thread(void *callable
)
619 _make_call(callable
);
620 PyThread_release_lock(thread_done
);
624 test_thread_state(PyObject
*self
, PyObject
*args
)
628 if (!PyArg_ParseTuple(args
, "O:test_thread_state", &fn
))
631 /* Ensure Python is set up for threading */
632 PyEval_InitThreads();
633 thread_done
= PyThread_allocate_lock();
634 if (thread_done
== NULL
)
635 return PyErr_NoMemory();
636 PyThread_acquire_lock(thread_done
, 1);
638 /* Start a new thread with our callback. */
639 PyThread_start_new_thread(_make_call_from_thread
, fn
);
640 /* Make the callback with the thread lock held by this thread */
642 /* Do it all again, but this time with the thread-lock released */
643 Py_BEGIN_ALLOW_THREADS
645 PyThread_acquire_lock(thread_done
, 1); /* wait for thread to finish */
648 /* And once more with and without a thread
649 XXX - should use a lock and work out exactly what we are trying
652 Py_BEGIN_ALLOW_THREADS
653 PyThread_start_new_thread(_make_call_from_thread
, fn
);
655 PyThread_acquire_lock(thread_done
, 1); /* wait for thread to finish */
658 PyThread_free_lock(thread_done
);
663 /* Some tests of PyString_FromFormat(). This needs more tests. */
665 test_string_from_format(PyObject
*self
, PyObject
*args
)
670 #define CHECK_1_FORMAT(FORMAT, TYPE) \
671 result = PyString_FromFormat(FORMAT, (TYPE)1); \
672 if (result == NULL) \
674 if (strcmp(PyString_AsString(result), "1")) { \
675 msg = FORMAT " failed at 1"; \
680 CHECK_1_FORMAT("%d", int);
681 CHECK_1_FORMAT("%ld", long);
682 /* The z width modifier was added in Python 2.5. */
683 CHECK_1_FORMAT("%zd", Py_ssize_t
);
685 /* The u type code was added in Python 2.5. */
686 CHECK_1_FORMAT("%u", unsigned int);
687 CHECK_1_FORMAT("%lu", unsigned long);
688 CHECK_1_FORMAT("%zu", size_t);
694 return raiseTestError("test_string_from_format", msg
);
696 #undef CHECK_1_FORMAT
699 static PyMethodDef TestMethods
[] = {
700 {"raise_exception", raise_exception
, METH_VARARGS
},
701 {"test_config", (PyCFunction
)test_config
, METH_NOARGS
},
702 {"test_list_api", (PyCFunction
)test_list_api
, METH_NOARGS
},
703 {"test_dict_iteration", (PyCFunction
)test_dict_iteration
,METH_NOARGS
},
704 {"test_long_api", (PyCFunction
)test_long_api
, METH_NOARGS
},
705 {"test_long_numbits", (PyCFunction
)test_long_numbits
, METH_NOARGS
},
706 {"test_k_code", (PyCFunction
)test_k_code
, METH_NOARGS
},
707 {"test_null_strings", (PyCFunction
)test_null_strings
, METH_NOARGS
},
708 {"test_string_from_format", (PyCFunction
)test_string_from_format
, METH_NOARGS
},
710 {"getargs_b", getargs_b
, METH_VARARGS
},
711 {"getargs_B", getargs_B
, METH_VARARGS
},
712 {"getargs_H", getargs_H
, METH_VARARGS
},
713 {"getargs_I", getargs_I
, METH_VARARGS
},
714 {"getargs_k", getargs_k
, METH_VARARGS
},
715 {"getargs_i", getargs_i
, METH_VARARGS
},
716 {"getargs_l", getargs_l
, METH_VARARGS
},
717 {"getargs_n", getargs_n
, METH_VARARGS
},
718 #ifdef HAVE_LONG_LONG
719 {"getargs_L", getargs_L
, METH_VARARGS
},
720 {"getargs_K", getargs_K
, METH_VARARGS
},
721 {"test_longlong_api", test_longlong_api
, METH_NOARGS
},
722 {"test_L_code", (PyCFunction
)test_L_code
, METH_NOARGS
},
723 {"codec_incrementalencoder",
724 (PyCFunction
)codec_incrementalencoder
, METH_VARARGS
},
725 {"codec_incrementaldecoder",
726 (PyCFunction
)codec_incrementaldecoder
, METH_VARARGS
},
728 #ifdef Py_USING_UNICODE
729 {"test_u_code", (PyCFunction
)test_u_code
, METH_NOARGS
},
732 {"_test_thread_state", test_thread_state
, METH_VARARGS
},
734 {NULL
, NULL
} /* sentinel */
737 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
744 m
= Py_InitModule("_testcapi", TestMethods
);
748 PyModule_AddObject(m
, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX
));
749 PyModule_AddObject(m
, "USHRT_MAX", PyInt_FromLong(USHRT_MAX
));
750 PyModule_AddObject(m
, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX
));
751 PyModule_AddObject(m
, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX
));
752 PyModule_AddObject(m
, "INT_MIN", PyInt_FromLong(INT_MIN
));
753 PyModule_AddObject(m
, "LONG_MIN", PyInt_FromLong(LONG_MIN
));
754 PyModule_AddObject(m
, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN
));
755 PyModule_AddObject(m
, "INT_MAX", PyInt_FromLong(INT_MAX
));
756 PyModule_AddObject(m
, "LONG_MAX", PyInt_FromLong(LONG_MAX
));
757 PyModule_AddObject(m
, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX
));
759 TestError
= PyErr_NewException("_testcapi.error", NULL
, NULL
);
760 Py_INCREF(TestError
);
761 PyModule_AddObject(m
, "error", TestError
);