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"
14 #endif /* WITH_THREAD */
15 static PyObject
*TestError
; /* set to exception object in init */
17 /* Raise TestError with test_name + ": " + msg, and return NULL. */
20 raiseTestError(const char* test_name
, const char* msg
)
24 if (strlen(test_name
) + strlen(msg
) > sizeof(buf
) - 50)
25 PyErr_SetString(TestError
, "internal error msg too large");
27 PyOS_snprintf(buf
, sizeof(buf
), "%s: %s", test_name
, msg
);
28 PyErr_SetString(TestError
, buf
);
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.
40 sizeof_error(const char* fatname
, const char* typname
,
41 int expected
, int got
)
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
;
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);
64 CHECK_SIZEOF(SIZEOF_LONG_LONG
, PY_LONG_LONG
);
74 test_list_api(PyObject
*self
)
79 /* SF bug 132008: PyList_Reverse segfaults */
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
) {
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! */
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");
106 return (PyObject
*)NULL
;
117 test_dict_inner(int count
)
119 Py_ssize_t pos
= 0, iterations
= 0;
121 PyObject
*dict
= PyDict_New();
127 for (i
= 0; i
< count
; i
++) {
128 v
= PyInt_FromLong(i
);
129 PyDict_SetItem(dict
, v
, v
);
133 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
137 i
= PyInt_AS_LONG(v
) + 1;
138 o
= PyInt_FromLong(i
);
141 if (PyDict_SetItem(dict
, k
, o
) < 0) {
150 if (iterations
!= count
) {
153 "test_dict_iteration: dict iteration went wrong ");
161 test_dict_iteration(PyObject
* self
)
165 for (i
= 0; i
< 200; i
++) {
166 if (test_dict_inner(i
) < 0) {
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 */
191 0, /* tp_as_number */
192 0, /* tp_as_sequence */
193 0, /* tp_as_mapping */
197 PyObject_GenericGetAttr
, /* tp_getattro */
199 0, /* tp_as_buffer */
200 Py_TPFLAGS_DEFAULT
, /* tp_flags */
204 0, /* tp_richcompare */
205 0, /* tp_weaklistoffset */
213 0, /* tp_descr_get */
214 0, /* tp_descr_set */
215 0, /* tp_dictoffset */
218 PyType_GenericNew
, /* tp_new */
222 test_lazy_hash_inheritance(PyObject
* self
)
228 type
= &_HashInheritanceTester_Type
;
230 if (type
->tp_dict
!= NULL
)
231 /* The type has already been initialized. This probably means
236 obj
= PyObject_New(PyObject
, type
);
241 "test_lazy_hash_inheritance: failed to create object");
245 if (type
->tp_dict
!= NULL
) {
248 "test_lazy_hash_inheritance: type initialised too soon");
253 hash
= PyObject_Hash(obj
);
254 if ((hash
== -1) && PyErr_Occurred()) {
258 "test_lazy_hash_inheritance: could not hash object");
263 if (type
->tp_dict
== NULL
) {
266 "test_lazy_hash_inheritance: type not initialised by hash()");
271 if (type
->tp_hash
!= PyType_Type
.tp_hash
) {
274 "test_lazy_hash_inheritance: unexpected hash function");
285 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
286 PyLong_{As, From}{Unsigned,}LongLong().
288 Note that the meat of the test is contained in testcapi_long.h.
289 This is revolting, but delicate code duplication is worse: "almost
290 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
291 dependence on type names makes it impossible to use a parameterized
292 function. A giant macro would be even worse than this. A C++ template
295 The "report an error" functions are deliberately not part of the #include
296 file: if the test fails, you can set a breakpoint in the appropriate
297 error function directly, and crawl back from there in the debugger.
300 #define UNBIND(X) Py_DECREF(X); (X) = NULL
303 raise_test_long_error(const char* msg
)
305 return raiseTestError("test_long_api", msg
);
308 #define TESTNAME test_long_api_inner
309 #define TYPENAME long
310 #define F_S_TO_PY PyLong_FromLong
311 #define F_PY_TO_S PyLong_AsLong
312 #define F_U_TO_PY PyLong_FromUnsignedLong
313 #define F_PY_TO_U PyLong_AsUnsignedLong
315 #include "testcapi_long.h"
318 test_long_api(PyObject
* self
)
320 return TESTNAME(raise_test_long_error
);
330 #ifdef HAVE_LONG_LONG
333 raise_test_longlong_error(const char* msg
)
335 return raiseTestError("test_longlong_api", msg
);
338 #define TESTNAME test_longlong_api_inner
339 #define TYPENAME PY_LONG_LONG
340 #define F_S_TO_PY PyLong_FromLongLong
341 #define F_PY_TO_S PyLong_AsLongLong
342 #define F_U_TO_PY PyLong_FromUnsignedLongLong
343 #define F_PY_TO_U PyLong_AsUnsignedLongLong
345 #include "testcapi_long.h"
348 test_longlong_api(PyObject
* self
, PyObject
*args
)
350 return TESTNAME(raise_test_longlong_error
);
360 /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
361 for both long and int arguments. The test may leak a little memory if
365 test_L_code(PyObject
*self
)
367 PyObject
*tuple
, *num
;
370 tuple
= PyTuple_New(1);
374 num
= PyLong_FromLong(42);
378 PyTuple_SET_ITEM(tuple
, 0, num
);
381 if (PyArg_ParseTuple(tuple
, "L:test_L_code", &value
) < 0)
384 return raiseTestError("test_L_code",
385 "L code returned wrong value for long 42");
388 num
= PyInt_FromLong(42);
392 PyTuple_SET_ITEM(tuple
, 0, num
);
395 if (PyArg_ParseTuple(tuple
, "L:test_L_code", &value
) < 0)
398 return raiseTestError("test_L_code",
399 "L code returned wrong value for int 42");
406 #endif /* ifdef HAVE_LONG_LONG */
408 /* Test tuple argument processing */
410 getargs_tuple(PyObject
*self
, PyObject
*args
)
413 if (!PyArg_ParseTuple(args
, "i(ii)", &a
, &b
, &c
))
415 return Py_BuildValue("iii", a
, b
, c
);
418 /* test PyArg_ParseTupleAndKeywords */
419 static PyObject
*getargs_keywords(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
421 static char *keywords
[] = {"arg1","arg2","arg3","arg4","arg5", NULL
};
422 static char *fmt
="(ii)i|(i(ii))(iii)i";
423 int int_args
[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
425 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, fmt
, keywords
,
426 &int_args
[0], &int_args
[1], &int_args
[2], &int_args
[3], &int_args
[4],
427 &int_args
[5], &int_args
[6], &int_args
[7], &int_args
[8], &int_args
[9]))
429 return Py_BuildValue("iiiiiiiiii",
430 int_args
[0], int_args
[1], int_args
[2], int_args
[3], int_args
[4],
431 int_args
[5], int_args
[6], int_args
[7], int_args
[8], int_args
[9]);
434 /* Functions to call PyArg_ParseTuple with integer format codes,
435 and return the result.
438 getargs_b(PyObject
*self
, PyObject
*args
)
441 if (!PyArg_ParseTuple(args
, "b", &value
))
443 return PyLong_FromUnsignedLong((unsigned long)value
);
447 getargs_B(PyObject
*self
, PyObject
*args
)
450 if (!PyArg_ParseTuple(args
, "B", &value
))
452 return PyLong_FromUnsignedLong((unsigned long)value
);
456 getargs_H(PyObject
*self
, PyObject
*args
)
458 unsigned short value
;
459 if (!PyArg_ParseTuple(args
, "H", &value
))
461 return PyLong_FromUnsignedLong((unsigned long)value
);
465 getargs_I(PyObject
*self
, PyObject
*args
)
468 if (!PyArg_ParseTuple(args
, "I", &value
))
470 return PyLong_FromUnsignedLong((unsigned long)value
);
474 getargs_k(PyObject
*self
, PyObject
*args
)
477 if (!PyArg_ParseTuple(args
, "k", &value
))
479 return PyLong_FromUnsignedLong(value
);
483 getargs_i(PyObject
*self
, PyObject
*args
)
486 if (!PyArg_ParseTuple(args
, "i", &value
))
488 return PyLong_FromLong((long)value
);
492 getargs_l(PyObject
*self
, PyObject
*args
)
495 if (!PyArg_ParseTuple(args
, "l", &value
))
497 return PyLong_FromLong(value
);
501 getargs_n(PyObject
*self
, PyObject
*args
)
504 if (!PyArg_ParseTuple(args
, "n", &value
))
506 return PyInt_FromSsize_t(value
);
509 #ifdef HAVE_LONG_LONG
511 getargs_L(PyObject
*self
, PyObject
*args
)
514 if (!PyArg_ParseTuple(args
, "L", &value
))
516 return PyLong_FromLongLong(value
);
520 getargs_K(PyObject
*self
, PyObject
*args
)
522 unsigned PY_LONG_LONG value
;
523 if (!PyArg_ParseTuple(args
, "K", &value
))
525 return PyLong_FromUnsignedLongLong(value
);
529 /* This function not only tests the 'k' getargs code, but also the
530 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
532 test_k_code(PyObject
*self
)
534 PyObject
*tuple
, *num
;
537 tuple
= PyTuple_New(1);
541 /* a number larger than ULONG_MAX even on 64-bit platforms */
542 num
= PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL
, 16);
546 value
= PyInt_AsUnsignedLongMask(num
);
547 if (value
!= ULONG_MAX
)
548 return raiseTestError("test_k_code",
549 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
551 PyTuple_SET_ITEM(tuple
, 0, num
);
554 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
556 if (value
!= ULONG_MAX
)
557 return raiseTestError("test_k_code",
558 "k code returned wrong value for long 0xFFF...FFF");
561 num
= PyLong_FromString("-FFFFFFFF000000000000000042", NULL
, 16);
565 value
= PyInt_AsUnsignedLongMask(num
);
566 if (value
!= (unsigned long)-0x42)
567 return raiseTestError("test_k_code",
568 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
570 PyTuple_SET_ITEM(tuple
, 0, num
);
573 if (PyArg_ParseTuple(tuple
, "k:test_k_code", &value
) < 0)
575 if (value
!= (unsigned long)-0x42)
576 return raiseTestError("test_k_code",
577 "k code returned wrong value for long -0xFFF..000042");
584 #ifdef Py_USING_UNICODE
586 static volatile int x
;
588 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
592 test_u_code(PyObject
*self
)
594 PyObject
*tuple
, *obj
;
598 /* issue4122: Undefined reference to _Py_ascii_whitespace on Windows */
599 /* Just use the macro and check that it compiles */
600 x
= Py_UNICODE_ISSPACE(25);
602 tuple
= PyTuple_New(1);
606 obj
= PyUnicode_Decode("test", strlen("test"),
611 PyTuple_SET_ITEM(tuple
, 0, obj
);
614 if (PyArg_ParseTuple(tuple
, "u:test_u_code", &value
) < 0)
616 if (value
!= PyUnicode_AS_UNICODE(obj
))
617 return raiseTestError("test_u_code",
618 "u code returned wrong value for u'test'");
620 if (PyArg_ParseTuple(tuple
, "u#:test_u_code", &value
, &len
) < 0)
622 if (value
!= PyUnicode_AS_UNICODE(obj
) ||
623 len
!= PyUnicode_GET_SIZE(obj
))
624 return raiseTestError("test_u_code",
625 "u# code returned wrong values for u'test'");
633 test_widechar(PyObject
*self
)
635 #if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
636 const wchar_t wtext
[2] = {(wchar_t)0x10ABCDu
};
639 const wchar_t wtext
[3] = {(wchar_t)0xDBEAu
, (wchar_t)0xDFCDu
};
642 PyObject
*wide
, *utf8
;
644 wide
= PyUnicode_FromWideChar(wtext
, wtextlen
);
648 utf8
= PyUnicode_FromString("\xf4\x8a\xaf\x8d");
654 if (PyUnicode_GET_SIZE(wide
) != PyUnicode_GET_SIZE(utf8
)) {
657 return raiseTestError("test_widechar",
658 "wide string and utf8 string have different length");
660 if (PyUnicode_Compare(wide
, utf8
)) {
663 if (PyErr_Occurred())
665 return raiseTestError("test_widechar",
666 "wide string and utf8 string are differents");
675 test_empty_argparse(PyObject
*self
)
677 /* Test that formats can begin with '|'. See issue #4720. */
678 PyObject
*tuple
, *dict
= NULL
;
679 static char *kwlist
[] = {NULL
};
681 tuple
= PyTuple_New(0);
684 if ((result
= PyArg_ParseTuple(tuple
, "|:test_empty_argparse")) < 0)
689 result
= PyArg_ParseTupleAndKeywords(tuple
, dict
, "|:test_empty_argparse", kwlist
);
701 codec_incrementalencoder(PyObject
*self
, PyObject
*args
)
703 const char *encoding
, *errors
= NULL
;
704 if (!PyArg_ParseTuple(args
, "s|s:test_incrementalencoder",
707 return PyCodec_IncrementalEncoder(encoding
, errors
);
711 codec_incrementaldecoder(PyObject
*self
, PyObject
*args
)
713 const char *encoding
, *errors
= NULL
;
714 if (!PyArg_ParseTuple(args
, "s|s:test_incrementaldecoder",
717 return PyCodec_IncrementalDecoder(encoding
, errors
);
722 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
724 test_long_numbits(PyObject
*self
)
730 } testcases
[] = {{0, 0, 0},
739 {0x7fffL
, 15, 1}, /* one Python long digit */
744 {-0xfffffffL
, 28, -1}};
747 for (i
= 0; i
< sizeof(testcases
) / sizeof(struct triple
); ++i
) {
748 PyObject
*plong
= PyLong_FromLong(testcases
[i
].input
);
749 size_t nbits
= _PyLong_NumBits(plong
);
750 int sign
= _PyLong_Sign(plong
);
753 if (nbits
!= testcases
[i
].nbits
)
754 return raiseTestError("test_long_numbits",
755 "wrong result for _PyLong_NumBits");
756 if (sign
!= testcases
[i
].sign
)
757 return raiseTestError("test_long_numbits",
758 "wrong result for _PyLong_Sign");
764 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
767 test_null_strings(PyObject
*self
)
769 PyObject
*o1
= PyObject_Str(NULL
), *o2
= PyObject_Unicode(NULL
);
770 PyObject
*tuple
= PyTuple_Pack(2, o1
, o2
);
777 raise_exception(PyObject
*self
, PyObject
*args
)
780 PyObject
*exc_args
, *v
;
783 if (!PyArg_ParseTuple(args
, "Oi:raise_exception",
786 if (!PyExceptionClass_Check(exc
)) {
787 PyErr_Format(PyExc_TypeError
, "an exception class is required");
791 exc_args
= PyTuple_New(num_args
);
792 if (exc_args
== NULL
)
794 for (i
= 0; i
< num_args
; ++i
) {
795 v
= PyInt_FromLong(i
);
800 PyTuple_SET_ITEM(exc_args
, i
, v
);
802 PyErr_SetObject(exc
, exc_args
);
809 /* test_thread_state spawns a thread of its own, and that thread releases
810 * `thread_done` when it's finished. The driver code has to know when the
811 * thread finishes, because the thread uses a PyObject (the callable) that
812 * may go away when the driver finishes. The former lack of this explicit
813 * synchronization caused rare segfaults, so rare that they were seen only
814 * on a Mac buildbot (although they were possible on any box).
816 static PyThread_type_lock thread_done
= NULL
;
819 _make_call(void *callable
)
823 PyGILState_STATE s
= PyGILState_Ensure();
824 rc
= PyObject_CallFunction((PyObject
*)callable
, "");
825 success
= (rc
!= NULL
);
827 PyGILState_Release(s
);
831 /* Same thing, but releases `thread_done` when it returns. This variant
832 * should be called only from threads spawned by test_thread_state().
835 _make_call_from_thread(void *callable
)
837 _make_call(callable
);
838 PyThread_release_lock(thread_done
);
842 test_thread_state(PyObject
*self
, PyObject
*args
)
847 if (!PyArg_ParseTuple(args
, "O:test_thread_state", &fn
))
850 if (!PyCallable_Check(fn
)) {
851 PyErr_Format(PyExc_TypeError
, "'%s' object is not callable",
852 fn
->ob_type
->tp_name
);
856 /* Ensure Python is set up for threading */
857 PyEval_InitThreads();
858 thread_done
= PyThread_allocate_lock();
859 if (thread_done
== NULL
)
860 return PyErr_NoMemory();
861 PyThread_acquire_lock(thread_done
, 1);
863 /* Start a new thread with our callback. */
864 PyThread_start_new_thread(_make_call_from_thread
, fn
);
865 /* Make the callback with the thread lock held by this thread */
866 success
&= _make_call(fn
);
867 /* Do it all again, but this time with the thread-lock released */
868 Py_BEGIN_ALLOW_THREADS
869 success
&= _make_call(fn
);
870 PyThread_acquire_lock(thread_done
, 1); /* wait for thread to finish */
873 /* And once more with and without a thread
874 XXX - should use a lock and work out exactly what we are trying
877 Py_BEGIN_ALLOW_THREADS
878 PyThread_start_new_thread(_make_call_from_thread
, fn
);
879 success
&= _make_call(fn
);
880 PyThread_acquire_lock(thread_done
, 1); /* wait for thread to finish */
883 /* Release lock we acquired above. This is required on HP-UX. */
884 PyThread_release_lock(thread_done
);
886 PyThread_free_lock(thread_done
);
892 /* test Py_AddPendingCalls using threads */
893 static int _pending_callback(void *arg
)
895 /* we assume the argument is callable object to which we own a reference */
896 PyObject
*callable
= (PyObject
*)arg
;
897 PyObject
*r
= PyObject_CallObject(callable
, NULL
);
900 return r
!= NULL
? 0 : -1;
903 /* The following requests n callbacks to _pending_callback. It can be
904 * run from any python thread.
906 PyObject
*pending_threadfunc(PyObject
*self
, PyObject
*arg
)
910 if (PyArg_ParseTuple(arg
, "O", &callable
) == 0)
913 /* create the reference for the callbackwhile we hold the lock */
916 Py_BEGIN_ALLOW_THREADS
917 r
= Py_AddPendingCall(&_pending_callback
, callable
);
921 Py_DECREF(callable
); /* unsuccessful add, destroy the extra reference */
930 /* Some tests of PyString_FromFormat(). This needs more tests. */
932 test_string_from_format(PyObject
*self
, PyObject
*args
)
937 #define CHECK_1_FORMAT(FORMAT, TYPE) \
938 result = PyString_FromFormat(FORMAT, (TYPE)1); \
939 if (result == NULL) \
941 if (strcmp(PyString_AsString(result), "1")) { \
942 msg = FORMAT " failed at 1"; \
947 CHECK_1_FORMAT("%d", int);
948 CHECK_1_FORMAT("%ld", long);
949 /* The z width modifier was added in Python 2.5. */
950 CHECK_1_FORMAT("%zd", Py_ssize_t
);
952 /* The u type code was added in Python 2.5. */
953 CHECK_1_FORMAT("%u", unsigned int);
954 CHECK_1_FORMAT("%lu", unsigned long);
955 CHECK_1_FORMAT("%zu", size_t);
961 return raiseTestError("test_string_from_format", msg
);
963 #undef CHECK_1_FORMAT
966 /* This is here to provide a docstring for test_descr. */
968 test_with_docstring(PyObject
*self
)
973 /* To test the format of tracebacks as printed out. */
975 traceback_print(PyObject
*self
, PyObject
*args
)
981 if (!PyArg_ParseTuple(args
, "OO:traceback_print",
985 result
= PyTraceBack_Print(traceback
, file
);
991 /* To test that the result of PyCode_NewEmpty has the right members. */
993 code_newempty(PyObject
*self
, PyObject
*args
)
995 const char *filename
;
996 const char *funcname
;
999 if (!PyArg_ParseTuple(args
, "ssi:code_newempty",
1000 &filename
, &funcname
, &firstlineno
))
1003 return (PyObject
*)PyCode_NewEmpty(filename
, funcname
, firstlineno
);
1006 static PyMethodDef TestMethods
[] = {
1007 {"raise_exception", raise_exception
, METH_VARARGS
},
1008 {"test_config", (PyCFunction
)test_config
, METH_NOARGS
},
1009 {"test_list_api", (PyCFunction
)test_list_api
, METH_NOARGS
},
1010 {"test_dict_iteration", (PyCFunction
)test_dict_iteration
,METH_NOARGS
},
1011 {"test_lazy_hash_inheritance", (PyCFunction
)test_lazy_hash_inheritance
,METH_NOARGS
},
1012 {"test_long_api", (PyCFunction
)test_long_api
, METH_NOARGS
},
1013 {"test_long_numbits", (PyCFunction
)test_long_numbits
, METH_NOARGS
},
1014 {"test_k_code", (PyCFunction
)test_k_code
, METH_NOARGS
},
1015 {"test_empty_argparse", (PyCFunction
)test_empty_argparse
,METH_NOARGS
},
1016 {"test_null_strings", (PyCFunction
)test_null_strings
, METH_NOARGS
},
1017 {"test_string_from_format", (PyCFunction
)test_string_from_format
, METH_NOARGS
},
1018 {"test_with_docstring", (PyCFunction
)test_with_docstring
, METH_NOARGS
,
1019 PyDoc_STR("This is a pretty normal docstring.")},
1021 {"getargs_tuple", getargs_tuple
, METH_VARARGS
},
1022 {"getargs_keywords", (PyCFunction
)getargs_keywords
,
1023 METH_VARARGS
|METH_KEYWORDS
},
1024 {"getargs_b", getargs_b
, METH_VARARGS
},
1025 {"getargs_B", getargs_B
, METH_VARARGS
},
1026 {"getargs_H", getargs_H
, METH_VARARGS
},
1027 {"getargs_I", getargs_I
, METH_VARARGS
},
1028 {"getargs_k", getargs_k
, METH_VARARGS
},
1029 {"getargs_i", getargs_i
, METH_VARARGS
},
1030 {"getargs_l", getargs_l
, METH_VARARGS
},
1031 {"getargs_n", getargs_n
, METH_VARARGS
},
1032 #ifdef HAVE_LONG_LONG
1033 {"getargs_L", getargs_L
, METH_VARARGS
},
1034 {"getargs_K", getargs_K
, METH_VARARGS
},
1035 {"test_longlong_api", test_longlong_api
, METH_NOARGS
},
1036 {"test_L_code", (PyCFunction
)test_L_code
, METH_NOARGS
},
1037 {"codec_incrementalencoder",
1038 (PyCFunction
)codec_incrementalencoder
, METH_VARARGS
},
1039 {"codec_incrementaldecoder",
1040 (PyCFunction
)codec_incrementaldecoder
, METH_VARARGS
},
1042 #ifdef Py_USING_UNICODE
1043 {"test_u_code", (PyCFunction
)test_u_code
, METH_NOARGS
},
1044 {"test_widechar", (PyCFunction
)test_widechar
, METH_NOARGS
},
1047 {"_test_thread_state", test_thread_state
, METH_VARARGS
},
1048 {"_pending_threadfunc", pending_threadfunc
, METH_VARARGS
},
1050 {"traceback_print", traceback_print
, METH_VARARGS
},
1051 {"code_newempty", code_newempty
, METH_VARARGS
},
1052 {NULL
, NULL
} /* sentinel */
1055 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
1060 unsigned char ubyte_member
;
1062 unsigned short ushort_member
;
1064 unsigned int uint_member
;
1066 unsigned long ulong_member
;
1068 double double_member
;
1069 #ifdef HAVE_LONG_LONG
1070 PY_LONG_LONG longlong_member
;
1071 unsigned PY_LONG_LONG ulonglong_member
;
1073 } all_structmembers
;
1077 all_structmembers structmembers
;
1078 } test_structmembers
;
1080 static struct PyMemberDef test_members
[] = {
1081 {"T_BOOL", T_BOOL
, offsetof(test_structmembers
, structmembers
.bool_member
), 0, NULL
},
1082 {"T_BYTE", T_BYTE
, offsetof(test_structmembers
, structmembers
.byte_member
), 0, NULL
},
1083 {"T_UBYTE", T_UBYTE
, offsetof(test_structmembers
, structmembers
.ubyte_member
), 0, NULL
},
1084 {"T_SHORT", T_SHORT
, offsetof(test_structmembers
, structmembers
.short_member
), 0, NULL
},
1085 {"T_USHORT", T_USHORT
, offsetof(test_structmembers
, structmembers
.ushort_member
), 0, NULL
},
1086 {"T_INT", T_INT
, offsetof(test_structmembers
, structmembers
.int_member
), 0, NULL
},
1087 {"T_UINT", T_UINT
, offsetof(test_structmembers
, structmembers
.uint_member
), 0, NULL
},
1088 {"T_LONG", T_LONG
, offsetof(test_structmembers
, structmembers
.long_member
), 0, NULL
},
1089 {"T_ULONG", T_ULONG
, offsetof(test_structmembers
, structmembers
.ulong_member
), 0, NULL
},
1090 {"T_FLOAT", T_FLOAT
, offsetof(test_structmembers
, structmembers
.float_member
), 0, NULL
},
1091 {"T_DOUBLE", T_DOUBLE
, offsetof(test_structmembers
, structmembers
.double_member
), 0, NULL
},
1092 #ifdef HAVE_LONG_LONG
1093 {"T_LONGLONG", T_LONGLONG
, offsetof(test_structmembers
, structmembers
.longlong_member
), 0, NULL
},
1094 {"T_ULONGLONG", T_ULONGLONG
, offsetof(test_structmembers
, structmembers
.ulonglong_member
), 0, NULL
},
1101 test_structmembers_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
1103 static char *keywords
[] = {
1104 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
1105 "T_INT", "T_UINT", "T_LONG", "T_ULONG",
1106 "T_FLOAT", "T_DOUBLE",
1107 #ifdef HAVE_LONG_LONG
1108 "T_LONGLONG", "T_ULONGLONG",
1111 static char *fmt
= "|bbBhHiIlkfd"
1112 #ifdef HAVE_LONG_LONG
1116 test_structmembers
*ob
;
1117 ob
= PyObject_New(test_structmembers
, type
);
1120 memset(&ob
->structmembers
, 0, sizeof(all_structmembers
));
1121 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, fmt
, keywords
,
1122 &ob
->structmembers
.bool_member
,
1123 &ob
->structmembers
.byte_member
,
1124 &ob
->structmembers
.ubyte_member
,
1125 &ob
->structmembers
.short_member
,
1126 &ob
->structmembers
.ushort_member
,
1127 &ob
->structmembers
.int_member
,
1128 &ob
->structmembers
.uint_member
,
1129 &ob
->structmembers
.long_member
,
1130 &ob
->structmembers
.ulong_member
,
1131 &ob
->structmembers
.float_member
,
1132 &ob
->structmembers
.double_member
1133 #ifdef HAVE_LONG_LONG
1134 , &ob
->structmembers
.longlong_member
,
1135 &ob
->structmembers
.ulonglong_member
1141 return (PyObject
*)ob
;
1145 test_structmembers_free(PyObject
*ob
)
1150 static PyTypeObject test_structmembersType
= {
1151 PyVarObject_HEAD_INIT(NULL
, 0)
1152 "test_structmembersType",
1153 sizeof(test_structmembers
), /* tp_basicsize */
1154 0, /* tp_itemsize */
1155 test_structmembers_free
, /* destructor tp_dealloc */
1161 0, /* tp_as_number */
1162 0, /* tp_as_sequence */
1163 0, /* tp_as_mapping */
1167 PyObject_GenericGetAttr
, /* tp_getattro */
1168 PyObject_GenericSetAttr
, /* tp_setattro */
1169 0, /* tp_as_buffer */
1171 "Type containing all structmember types",
1172 0, /* traverseproc tp_traverse */
1174 0, /* tp_richcompare */
1175 0, /* tp_weaklistoffset */
1177 0, /* tp_iternext */
1179 test_members
, /* tp_members */
1188 test_structmembers_new
, /* tp_new */
1197 m
= Py_InitModule("_testcapi", TestMethods
);
1201 Py_TYPE(&_HashInheritanceTester_Type
)=&PyType_Type
;
1203 Py_TYPE(&test_structmembersType
)=&PyType_Type
;
1204 Py_INCREF(&test_structmembersType
);
1205 PyModule_AddObject(m
, "test_structmembersType", (PyObject
*)&test_structmembersType
);
1207 PyModule_AddObject(m
, "CHAR_MAX", PyInt_FromLong(CHAR_MAX
));
1208 PyModule_AddObject(m
, "CHAR_MIN", PyInt_FromLong(CHAR_MIN
));
1209 PyModule_AddObject(m
, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX
));
1210 PyModule_AddObject(m
, "SHRT_MAX", PyInt_FromLong(SHRT_MAX
));
1211 PyModule_AddObject(m
, "SHRT_MIN", PyInt_FromLong(SHRT_MIN
));
1212 PyModule_AddObject(m
, "USHRT_MAX", PyInt_FromLong(USHRT_MAX
));
1213 PyModule_AddObject(m
, "INT_MAX", PyLong_FromLong(INT_MAX
));
1214 PyModule_AddObject(m
, "INT_MIN", PyLong_FromLong(INT_MIN
));
1215 PyModule_AddObject(m
, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX
));
1216 PyModule_AddObject(m
, "LONG_MAX", PyInt_FromLong(LONG_MAX
));
1217 PyModule_AddObject(m
, "LONG_MIN", PyInt_FromLong(LONG_MIN
));
1218 PyModule_AddObject(m
, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX
));
1219 PyModule_AddObject(m
, "FLT_MAX", PyFloat_FromDouble(FLT_MAX
));
1220 PyModule_AddObject(m
, "FLT_MIN", PyFloat_FromDouble(FLT_MIN
));
1221 PyModule_AddObject(m
, "DBL_MAX", PyFloat_FromDouble(DBL_MAX
));
1222 PyModule_AddObject(m
, "DBL_MIN", PyFloat_FromDouble(DBL_MIN
));
1223 PyModule_AddObject(m
, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX
));
1224 PyModule_AddObject(m
, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN
));
1225 PyModule_AddObject(m
, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX
));
1226 PyModule_AddObject(m
, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX
));
1227 PyModule_AddObject(m
, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN
));
1228 PyModule_AddObject(m
, "SIZEOF_PYGC_HEAD", PyInt_FromSsize_t(sizeof(PyGC_Head
)));
1230 TestError
= PyErr_NewException("_testcapi.error", NULL
, NULL
);
1231 Py_INCREF(TestError
);
1232 PyModule_AddObject(m
, "error", TestError
);