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 */
14 static PyObject
*TestError
; /* set to exception object in init */
16 /* Raise TestError with test_name + ": " + msg, and return NULL. */
19 raiseTestError(const char* test_name
, const char* msg
)
23 if (strlen(test_name
) + strlen(msg
) > sizeof(buf
) - 50)
24 PyErr_SetString(TestError
, "internal error msg too large");
26 PyOS_snprintf(buf
, sizeof(buf
), "%s: %s", test_name
, msg
);
27 PyErr_SetString(TestError
, buf
);
32 /* Test #defines from pyconfig.h (particularly the SIZEOF_* defines).
34 The ones derived from autoconf on the UNIX-like OSes can be relied
35 upon (in the absence of sloppy cross-compiling), but the Windows
36 platforms have these hardcoded. Better safe than sorry.
39 sizeof_error(const char* fatname
, const char* typename
,
40 int expected
, int got
)
43 PyOS_snprintf(buf
, sizeof(buf
),
44 "%.200s #define == %d but sizeof(%.200s) == %d",
45 fatname
, expected
, typename
, got
);
46 PyErr_SetString(TestError
, buf
);
47 return (PyObject
*)NULL
;
51 test_config(PyObject
*self
)
53 #define CHECK_SIZEOF(FATNAME, TYPE) \
54 if (FATNAME != sizeof(TYPE)) \
55 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
57 CHECK_SIZEOF(SIZEOF_SHORT
, short);
58 CHECK_SIZEOF(SIZEOF_INT
, int);
59 CHECK_SIZEOF(SIZEOF_LONG
, long);
60 CHECK_SIZEOF(SIZEOF_VOID_P
, void*);
61 CHECK_SIZEOF(SIZEOF_TIME_T
, time_t);
63 CHECK_SIZEOF(SIZEOF_LONG_LONG
, PY_LONG_LONG
);
73 test_list_api(PyObject
*self
)
78 /* SF bug 132008: PyList_Reverse segfaults */
80 list
= PyList_New(NLIST
);
81 if (list
== (PyObject
*)NULL
)
82 return (PyObject
*)NULL
;
83 /* list = range(NLIST) */
84 for (i
= 0; i
< NLIST
; ++i
) {
85 PyObject
* anint
= PyInt_FromLong(i
);
86 if (anint
== (PyObject
*)NULL
) {
88 return (PyObject
*)NULL
;
90 PyList_SET_ITEM(list
, i
, anint
);
92 /* list.reverse(), via PyList_Reverse() */
93 i
= PyList_Reverse(list
); /* should not blow up! */
96 return (PyObject
*)NULL
;
98 /* Check that list == range(29, -1, -1) now */
99 for (i
= 0; i
< NLIST
; ++i
) {
100 PyObject
* anint
= PyList_GET_ITEM(list
, i
);
101 if (PyInt_AS_LONG(anint
) != NLIST
-1-i
) {
102 PyErr_SetString(TestError
,
103 "test_list_api: reverse screwed up");
105 return (PyObject
*)NULL
;
116 test_dict_inner(int count
)
118 int pos
= 0, iterations
= 0, i
;
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
)
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
);
363 #ifdef HAVE_LONG_LONG
365 getargs_L(PyObject
*self
, PyObject
*args
)
368 if (!PyArg_ParseTuple(args
, "L", &value
))
370 return PyLong_FromLongLong(value
);
374 getargs_K(PyObject
*self
, PyObject
*args
)
376 unsigned PY_LONG_LONG value
;
377 if (!PyArg_ParseTuple(args
, "K", &value
))
379 return PyLong_FromUnsignedLongLong(value
);
383 /* This function not only tests the 'k' getargs code, but also the
384 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
386 test_k_code(PyObject
*self
)
388 PyObject
*tuple
, *num
;
391 tuple
= PyTuple_New(1);
395 /* a number larger than ULONG_MAX even on 64-bit platforms */
396 num
= PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL
, 16);
400 value
= PyInt_AsUnsignedLongMask(num
);
401 if (value
!= ULONG_MAX
)
402 return raiseTestError("test_k_code",
403 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
405 PyTuple_SET_ITEM(tuple
, 0, num
);
408 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
410 if (value
!= ULONG_MAX
)
411 return raiseTestError("test_k_code",
412 "k code returned wrong value for long 0xFFF...FFF");
415 num
= PyLong_FromString("-FFFFFFFF000000000000000042", NULL
, 16);
419 value
= PyInt_AsUnsignedLongMask(num
);
420 if (value
!= (unsigned long)-0x42)
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
!= (unsigned long)-0x42)
430 return raiseTestError("test_k_code",
431 "k code returned wrong value for long -0xFFF..000042");
438 #ifdef Py_USING_UNICODE
440 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
444 test_u_code(PyObject
*self
)
446 PyObject
*tuple
, *obj
;
450 tuple
= PyTuple_New(1);
454 obj
= PyUnicode_Decode("test", strlen("test"),
459 PyTuple_SET_ITEM(tuple
, 0, obj
);
462 if (PyArg_ParseTuple(tuple
, "u:test_u_code", &value
) < 0)
464 if (value
!= PyUnicode_AS_UNICODE(obj
))
465 return raiseTestError("test_u_code",
466 "u code returned wrong value for u'test'");
468 if (PyArg_ParseTuple(tuple
, "u#:test_u_code", &value
, &len
) < 0)
470 if (value
!= PyUnicode_AS_UNICODE(obj
) ||
471 len
!= PyUnicode_GET_SIZE(obj
))
472 return raiseTestError("test_u_code",
473 "u# code returned wrong values for u'test'");
482 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
484 test_long_numbits(PyObject
*self
)
490 } testcases
[] = {{0, 0, 0},
499 {0x7fffL
, 15, 1}, /* one Python long digit */
504 {-0xfffffffL
, 28, -1}};
507 for (i
= 0; i
< sizeof(testcases
) / sizeof(struct triple
); ++i
) {
508 PyObject
*plong
= PyLong_FromLong(testcases
[i
].input
);
509 size_t nbits
= _PyLong_NumBits(plong
);
510 int sign
= _PyLong_Sign(plong
);
513 if (nbits
!= testcases
[i
].nbits
)
514 return raiseTestError("test_long_numbits",
515 "wrong result for _PyLong_NumBits");
516 if (sign
!= testcases
[i
].sign
)
517 return raiseTestError("test_long_numbits",
518 "wrong result for _PyLong_Sign");
525 raise_exception(PyObject
*self
, PyObject
*args
)
528 PyObject
*exc_args
, *v
;
531 if (!PyArg_ParseTuple(args
, "Oi:raise_exception",
535 exc_args
= PyTuple_New(num_args
);
536 if (exc_args
== NULL
)
538 for (i
= 0; i
< num_args
; ++i
) {
539 v
= PyInt_FromLong(i
);
544 PyTuple_SET_ITEM(exc_args
, i
, v
);
546 PyErr_SetObject(exc
, exc_args
);
553 void _make_call(void *callable
)
556 PyGILState_STATE s
= PyGILState_Ensure();
557 rc
= PyObject_CallFunction(callable
, "");
559 PyGILState_Release(s
);
563 test_thread_state(PyObject
*self
, PyObject
*args
)
566 if (!PyArg_ParseTuple(args
, "O:test_thread_state", &fn
))
568 /* Ensure Python is setup for threading */
569 PyEval_InitThreads();
570 /* Start a new thread for our callback. */
571 PyThread_start_new_thread( _make_call
, fn
);
572 /* Make the callback with the thread lock held by this thread */
574 /* Do it all again, but this time with the thread-lock released */
575 Py_BEGIN_ALLOW_THREADS
578 /* And once more with and without a thread
579 XXX - should use a lock and work out exactly what we are trying
582 Py_BEGIN_ALLOW_THREADS
583 PyThread_start_new_thread( _make_call
, fn
);
591 /* a classic-type with copyable instances */
595 /* instance tag (a string). */
599 staticforward PyTypeObject Copyable_Type
;
601 #define Copyable_CheckExact(op) ((op)->ob_type == &Copyable_Type)
603 /* -------------------------------------------------------------------- */
605 /* copyable constructor and destructor */
607 copyable_new(PyObject
* tag
)
609 CopyableObject
* self
;
611 self
= PyObject_New(CopyableObject
, &Copyable_Type
);
616 return (PyObject
*) self
;
620 copyable(PyObject
* self
, PyObject
* args
, PyObject
* kw
)
624 if (!PyArg_ParseTuple(args
, "O:Copyable", &tag
))
626 elem
= copyable_new(tag
);
631 copyable_dealloc(CopyableObject
* self
)
633 /* discard attributes */
634 Py_DECREF(self
->tag
);
638 /* copyable methods */
641 copyable_copy(CopyableObject
* self
, PyObject
* args
)
643 CopyableObject
* copyable
;
644 if (!PyArg_ParseTuple(args
, ":__copy__"))
646 copyable
= (CopyableObject
*)copyable_new(self
->tag
);
649 return (PyObject
*) copyable
;
652 PyObject
* _copy_deepcopy
;
655 copyable_deepcopy(CopyableObject
* self
, PyObject
* args
)
657 CopyableObject
* copyable
= 0;
660 if (!PyArg_ParseTuple(args
, "O:__deepcopy__", &memo
))
663 tag_copy
= PyObject_CallFunctionObjArgs(_copy_deepcopy
, self
->tag
, memo
, NULL
);
666 copyable
= (CopyableObject
*)copyable_new(tag_copy
);
669 return (PyObject
*) copyable
;
673 copyable_repr(CopyableObject
* self
)
678 repr
= PyString_FromString("<Copyable {");
680 PyString_ConcatAndDel(&repr
, PyObject_Repr(self
->tag
));
682 sprintf(buffer
, "} at %p>", self
);
683 PyString_ConcatAndDel(&repr
, PyString_FromString(buffer
));
689 copyable_compare(CopyableObject
* obj1
, CopyableObject
* obj2
)
691 return PyObject_Compare(obj1
->tag
, obj2
->tag
);
694 static PyMethodDef copyable_methods
[] = {
695 {"__copy__", (PyCFunction
) copyable_copy
, METH_VARARGS
},
696 {"__deepcopy__", (PyCFunction
) copyable_deepcopy
, METH_VARARGS
},
701 copyable_getattr(CopyableObject
* self
, char* name
)
704 res
= Py_FindMethod(copyable_methods
, (PyObject
*) self
, name
);
708 if (strcmp(name
, "tag") == 0) {
711 PyErr_SetString(PyExc_AttributeError
, name
);
721 copyable_setattr(CopyableObject
* self
, const char* name
, PyObject
* value
)
725 PyExc_AttributeError
,
726 "can't delete copyable attributes"
730 if (strcmp(name
, "tag") == 0) {
731 Py_DECREF(self
->tag
);
733 Py_INCREF(self
->tag
);
735 PyErr_SetString(PyExc_AttributeError
, name
);
741 statichere PyTypeObject Copyable_Type
= {
742 PyObject_HEAD_INIT(NULL
)
743 0, "Copyable", sizeof(CopyableObject
), 0,
745 (destructor
)copyable_dealloc
, /* tp_dealloc */
747 (getattrfunc
)copyable_getattr
, /* tp_getattr */
748 (setattrfunc
)copyable_setattr
, /* tp_setattr */
749 (cmpfunc
)copyable_compare
, /* tp_compare */
750 (reprfunc
)copyable_repr
, /* tp_repr */
751 0, /* tp_as_number */
754 static PyMethodDef TestMethods
[] = {
755 {"raise_exception", raise_exception
, METH_VARARGS
},
756 {"test_config", (PyCFunction
)test_config
, METH_NOARGS
},
757 {"test_list_api", (PyCFunction
)test_list_api
, METH_NOARGS
},
758 {"test_dict_iteration", (PyCFunction
)test_dict_iteration
,METH_NOARGS
},
759 {"test_long_api", (PyCFunction
)test_long_api
, METH_NOARGS
},
760 {"test_long_numbits", (PyCFunction
)test_long_numbits
, METH_NOARGS
},
761 {"test_k_code", (PyCFunction
)test_k_code
, METH_NOARGS
},
763 {"getargs_b", (PyCFunction
)getargs_b
, METH_VARARGS
},
764 {"getargs_B", (PyCFunction
)getargs_B
, METH_VARARGS
},
765 {"getargs_H", (PyCFunction
)getargs_H
, METH_VARARGS
},
766 {"getargs_I", (PyCFunction
)getargs_I
, METH_VARARGS
},
767 {"getargs_k", (PyCFunction
)getargs_k
, METH_VARARGS
},
768 {"getargs_i", (PyCFunction
)getargs_i
, METH_VARARGS
},
769 {"getargs_l", (PyCFunction
)getargs_l
, METH_VARARGS
},
770 #ifdef HAVE_LONG_LONG
771 {"getargs_L", (PyCFunction
)getargs_L
, METH_VARARGS
},
772 {"getargs_K", (PyCFunction
)getargs_K
, METH_VARARGS
},
773 {"test_longlong_api", (PyCFunction
)test_longlong_api
, METH_NOARGS
},
774 {"test_L_code", (PyCFunction
)test_L_code
, METH_NOARGS
},
776 #ifdef Py_USING_UNICODE
777 {"test_u_code", (PyCFunction
)test_u_code
, METH_NOARGS
},
780 {"_test_thread_state", (PyCFunction
)test_thread_state
, METH_VARARGS
},
782 {"make_copyable", (PyCFunction
) copyable
, METH_VARARGS
},
783 {NULL
, NULL
} /* sentinel */
787 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
793 PyObject
*copy_module
;
796 copy_module
= PyImport_ImportModule("copy");
799 _copy_deepcopy
= PyObject_GetAttrString(copy_module
, "deepcopy");
800 Py_DECREF(copy_module
);
801 Copyable_Type
.ob_type
= &PyType_Type
;
803 m
= Py_InitModule("_testcapi", TestMethods
);
805 PyModule_AddObject(m
, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX
));
806 PyModule_AddObject(m
, "USHRT_MAX", PyInt_FromLong(USHRT_MAX
));
807 PyModule_AddObject(m
, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX
));
808 PyModule_AddObject(m
, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX
));
809 PyModule_AddObject(m
, "INT_MIN", PyInt_FromLong(INT_MIN
));
810 PyModule_AddObject(m
, "LONG_MIN", PyInt_FromLong(LONG_MIN
));
811 PyModule_AddObject(m
, "INT_MAX", PyInt_FromLong(INT_MAX
));
812 PyModule_AddObject(m
, "LONG_MAX", PyInt_FromLong(LONG_MAX
));
814 TestError
= PyErr_NewException("_testcapi.error", NULL
, NULL
);
815 Py_INCREF(TestError
);
816 PyModule_AddObject(m
, "error", TestError
);