Updated with fix for #3126.
[python.git] / Modules / _testcapimodule.c
blob4a00fb1975bfbf0125dcf7b1082932871f23b0ee
1 /*
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.
6 */
8 #include "Python.h"
9 #include <float.h>
10 #include "structmember.h"
12 #ifdef WITH_THREAD
13 #include "pythread.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. */
19 static PyObject *
20 raiseTestError(const char* test_name, const char* msg)
22 char buf[2048];
24 if (strlen(test_name) + strlen(msg) > sizeof(buf) - 50)
25 PyErr_SetString(TestError, "internal error msg too large");
26 else {
27 PyOS_snprintf(buf, sizeof(buf), "%s: %s", test_name, msg);
28 PyErr_SetString(TestError, buf);
30 return NULL;
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.
39 static PyObject*
40 sizeof_error(const char* fatname, const char* typname,
41 int expected, int got)
43 char buf[1024];
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;
51 static PyObject*
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);
63 #ifdef HAVE_LONG_LONG
64 CHECK_SIZEOF(SIZEOF_LONG_LONG, PY_LONG_LONG);
65 #endif
67 #undef CHECK_SIZEOF
69 Py_INCREF(Py_None);
70 return Py_None;
73 static PyObject*
74 test_list_api(PyObject *self)
76 PyObject* list;
77 int i;
79 /* SF bug 132008: PyList_Reverse segfaults */
80 #define NLIST 30
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) {
88 Py_DECREF(list);
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! */
95 if (i != 0) {
96 Py_DECREF(list);
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");
105 Py_DECREF(list);
106 return (PyObject*)NULL;
109 Py_DECREF(list);
110 #undef NLIST
112 Py_INCREF(Py_None);
113 return Py_None;
116 static int
117 test_dict_inner(int count)
119 Py_ssize_t pos = 0, iterations = 0;
120 int i;
121 PyObject *dict = PyDict_New();
122 PyObject *v, *k;
124 if (dict == NULL)
125 return -1;
127 for (i = 0; i < count; i++) {
128 v = PyInt_FromLong(i);
129 PyDict_SetItem(dict, v, v);
130 Py_DECREF(v);
133 while (PyDict_Next(dict, &pos, &k, &v)) {
134 PyObject *o;
135 iterations++;
137 i = PyInt_AS_LONG(v) + 1;
138 o = PyInt_FromLong(i);
139 if (o == NULL)
140 return -1;
141 if (PyDict_SetItem(dict, k, o) < 0) {
142 Py_DECREF(o);
143 return -1;
145 Py_DECREF(o);
148 Py_DECREF(dict);
150 if (iterations != count) {
151 PyErr_SetString(
152 TestError,
153 "test_dict_iteration: dict iteration went wrong ");
154 return -1;
155 } else {
156 return 0;
160 static PyObject*
161 test_dict_iteration(PyObject* self)
163 int i;
165 for (i = 0; i < 200; i++) {
166 if (test_dict_inner(i) < 0) {
167 return NULL;
171 Py_INCREF(Py_None);
172 return Py_None;
176 /* Tests of PyLong_{As, From}{Unsigned,}Long(), and (#ifdef HAVE_LONG_LONG)
177 PyLong_{As, From}{Unsigned,}LongLong().
179 Note that the meat of the test is contained in testcapi_long.h.
180 This is revolting, but delicate code duplication is worse: "almost
181 exactly the same" code is needed to test PY_LONG_LONG, but the ubiquitous
182 dependence on type names makes it impossible to use a parameterized
183 function. A giant macro would be even worse than this. A C++ template
184 would be perfect.
186 The "report an error" functions are deliberately not part of the #include
187 file: if the test fails, you can set a breakpoint in the appropriate
188 error function directly, and crawl back from there in the debugger.
191 #define UNBIND(X) Py_DECREF(X); (X) = NULL
193 static PyObject *
194 raise_test_long_error(const char* msg)
196 return raiseTestError("test_long_api", msg);
199 #define TESTNAME test_long_api_inner
200 #define TYPENAME long
201 #define F_S_TO_PY PyLong_FromLong
202 #define F_PY_TO_S PyLong_AsLong
203 #define F_U_TO_PY PyLong_FromUnsignedLong
204 #define F_PY_TO_U PyLong_AsUnsignedLong
206 #include "testcapi_long.h"
208 static PyObject *
209 test_long_api(PyObject* self)
211 return TESTNAME(raise_test_long_error);
214 #undef TESTNAME
215 #undef TYPENAME
216 #undef F_S_TO_PY
217 #undef F_PY_TO_S
218 #undef F_U_TO_PY
219 #undef F_PY_TO_U
221 #ifdef HAVE_LONG_LONG
223 static PyObject *
224 raise_test_longlong_error(const char* msg)
226 return raiseTestError("test_longlong_api", msg);
229 #define TESTNAME test_longlong_api_inner
230 #define TYPENAME PY_LONG_LONG
231 #define F_S_TO_PY PyLong_FromLongLong
232 #define F_PY_TO_S PyLong_AsLongLong
233 #define F_U_TO_PY PyLong_FromUnsignedLongLong
234 #define F_PY_TO_U PyLong_AsUnsignedLongLong
236 #include "testcapi_long.h"
238 static PyObject *
239 test_longlong_api(PyObject* self, PyObject *args)
241 return TESTNAME(raise_test_longlong_error);
244 #undef TESTNAME
245 #undef TYPENAME
246 #undef F_S_TO_PY
247 #undef F_PY_TO_S
248 #undef F_U_TO_PY
249 #undef F_PY_TO_U
251 /* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
252 for both long and int arguments. The test may leak a little memory if
253 it fails.
255 static PyObject *
256 test_L_code(PyObject *self)
258 PyObject *tuple, *num;
259 PY_LONG_LONG value;
261 tuple = PyTuple_New(1);
262 if (tuple == NULL)
263 return NULL;
265 num = PyLong_FromLong(42);
266 if (num == NULL)
267 return NULL;
269 PyTuple_SET_ITEM(tuple, 0, num);
271 value = -1;
272 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
273 return NULL;
274 if (value != 42)
275 return raiseTestError("test_L_code",
276 "L code returned wrong value for long 42");
278 Py_DECREF(num);
279 num = PyInt_FromLong(42);
280 if (num == NULL)
281 return NULL;
283 PyTuple_SET_ITEM(tuple, 0, num);
285 value = -1;
286 if (PyArg_ParseTuple(tuple, "L:test_L_code", &value) < 0)
287 return NULL;
288 if (value != 42)
289 return raiseTestError("test_L_code",
290 "L code returned wrong value for int 42");
292 Py_DECREF(tuple);
293 Py_INCREF(Py_None);
294 return Py_None;
297 #endif /* ifdef HAVE_LONG_LONG */
299 /* Test tuple argument processing */
300 static PyObject *
301 getargs_tuple(PyObject *self, PyObject *args)
303 int a, b, c;
304 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c))
305 return NULL;
306 return Py_BuildValue("iii", a, b, c);
309 /* test PyArg_ParseTupleAndKeywords */
310 static PyObject *getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
312 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
313 static char *fmt="(ii)i|(i(ii))(iii)i";
314 int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
316 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
317 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
318 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
319 return NULL;
320 return Py_BuildValue("iiiiiiiiii",
321 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
322 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
325 /* Functions to call PyArg_ParseTuple with integer format codes,
326 and return the result.
328 static PyObject *
329 getargs_b(PyObject *self, PyObject *args)
331 unsigned char value;
332 if (!PyArg_ParseTuple(args, "b", &value))
333 return NULL;
334 return PyLong_FromUnsignedLong((unsigned long)value);
337 static PyObject *
338 getargs_B(PyObject *self, PyObject *args)
340 unsigned char value;
341 if (!PyArg_ParseTuple(args, "B", &value))
342 return NULL;
343 return PyLong_FromUnsignedLong((unsigned long)value);
346 static PyObject *
347 getargs_H(PyObject *self, PyObject *args)
349 unsigned short value;
350 if (!PyArg_ParseTuple(args, "H", &value))
351 return NULL;
352 return PyLong_FromUnsignedLong((unsigned long)value);
355 static PyObject *
356 getargs_I(PyObject *self, PyObject *args)
358 unsigned int value;
359 if (!PyArg_ParseTuple(args, "I", &value))
360 return NULL;
361 return PyLong_FromUnsignedLong((unsigned long)value);
364 static PyObject *
365 getargs_k(PyObject *self, PyObject *args)
367 unsigned long value;
368 if (!PyArg_ParseTuple(args, "k", &value))
369 return NULL;
370 return PyLong_FromUnsignedLong(value);
373 static PyObject *
374 getargs_i(PyObject *self, PyObject *args)
376 int value;
377 if (!PyArg_ParseTuple(args, "i", &value))
378 return NULL;
379 return PyLong_FromLong((long)value);
382 static PyObject *
383 getargs_l(PyObject *self, PyObject *args)
385 long value;
386 if (!PyArg_ParseTuple(args, "l", &value))
387 return NULL;
388 return PyLong_FromLong(value);
391 static PyObject *
392 getargs_n(PyObject *self, PyObject *args)
394 Py_ssize_t value;
395 if (!PyArg_ParseTuple(args, "n", &value))
396 return NULL;
397 return PyInt_FromSsize_t(value);
400 #ifdef HAVE_LONG_LONG
401 static PyObject *
402 getargs_L(PyObject *self, PyObject *args)
404 PY_LONG_LONG value;
405 if (!PyArg_ParseTuple(args, "L", &value))
406 return NULL;
407 return PyLong_FromLongLong(value);
410 static PyObject *
411 getargs_K(PyObject *self, PyObject *args)
413 unsigned PY_LONG_LONG value;
414 if (!PyArg_ParseTuple(args, "K", &value))
415 return NULL;
416 return PyLong_FromUnsignedLongLong(value);
418 #endif
420 /* This function not only tests the 'k' getargs code, but also the
421 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
422 static PyObject *
423 test_k_code(PyObject *self)
425 PyObject *tuple, *num;
426 unsigned long value;
428 tuple = PyTuple_New(1);
429 if (tuple == NULL)
430 return NULL;
432 /* a number larger than ULONG_MAX even on 64-bit platforms */
433 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
434 if (num == NULL)
435 return NULL;
437 value = PyInt_AsUnsignedLongMask(num);
438 if (value != ULONG_MAX)
439 return raiseTestError("test_k_code",
440 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
442 PyTuple_SET_ITEM(tuple, 0, num);
444 value = 0;
445 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
446 return NULL;
447 if (value != ULONG_MAX)
448 return raiseTestError("test_k_code",
449 "k code returned wrong value for long 0xFFF...FFF");
451 Py_DECREF(num);
452 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
453 if (num == NULL)
454 return NULL;
456 value = PyInt_AsUnsignedLongMask(num);
457 if (value != (unsigned long)-0x42)
458 return raiseTestError("test_k_code",
459 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
461 PyTuple_SET_ITEM(tuple, 0, num);
463 value = 0;
464 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
465 return NULL;
466 if (value != (unsigned long)-0x42)
467 return raiseTestError("test_k_code",
468 "k code returned wrong value for long -0xFFF..000042");
470 Py_DECREF(tuple);
471 Py_INCREF(Py_None);
472 return Py_None;
475 #ifdef Py_USING_UNICODE
477 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
478 of an error.
480 static PyObject *
481 test_u_code(PyObject *self)
483 PyObject *tuple, *obj;
484 Py_UNICODE *value;
485 int len;
487 tuple = PyTuple_New(1);
488 if (tuple == NULL)
489 return NULL;
491 obj = PyUnicode_Decode("test", strlen("test"),
492 "ascii", NULL);
493 if (obj == NULL)
494 return NULL;
496 PyTuple_SET_ITEM(tuple, 0, obj);
498 value = 0;
499 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
500 return NULL;
501 if (value != PyUnicode_AS_UNICODE(obj))
502 return raiseTestError("test_u_code",
503 "u code returned wrong value for u'test'");
504 value = 0;
505 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
506 return NULL;
507 if (value != PyUnicode_AS_UNICODE(obj) ||
508 len != PyUnicode_GET_SIZE(obj))
509 return raiseTestError("test_u_code",
510 "u# code returned wrong values for u'test'");
512 Py_DECREF(tuple);
513 Py_INCREF(Py_None);
514 return Py_None;
517 static PyObject *
518 codec_incrementalencoder(PyObject *self, PyObject *args)
520 const char *encoding, *errors = NULL;
521 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
522 &encoding, &errors))
523 return NULL;
524 return PyCodec_IncrementalEncoder(encoding, errors);
527 static PyObject *
528 codec_incrementaldecoder(PyObject *self, PyObject *args)
530 const char *encoding, *errors = NULL;
531 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
532 &encoding, &errors))
533 return NULL;
534 return PyCodec_IncrementalDecoder(encoding, errors);
537 #endif
539 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
540 static PyObject *
541 test_long_numbits(PyObject *self)
543 struct triple {
544 long input;
545 size_t nbits;
546 int sign;
547 } testcases[] = {{0, 0, 0},
548 {1L, 1, 1},
549 {-1L, 1, -1},
550 {2L, 2, 1},
551 {-2L, 2, -1},
552 {3L, 2, 1},
553 {-3L, 2, -1},
554 {4L, 3, 1},
555 {-4L, 3, -1},
556 {0x7fffL, 15, 1}, /* one Python long digit */
557 {-0x7fffL, 15, -1},
558 {0xffffL, 16, 1},
559 {-0xffffL, 16, -1},
560 {0xfffffffL, 28, 1},
561 {-0xfffffffL, 28, -1}};
562 int i;
564 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
565 PyObject *plong = PyLong_FromLong(testcases[i].input);
566 size_t nbits = _PyLong_NumBits(plong);
567 int sign = _PyLong_Sign(plong);
569 Py_DECREF(plong);
570 if (nbits != testcases[i].nbits)
571 return raiseTestError("test_long_numbits",
572 "wrong result for _PyLong_NumBits");
573 if (sign != testcases[i].sign)
574 return raiseTestError("test_long_numbits",
575 "wrong result for _PyLong_Sign");
577 Py_INCREF(Py_None);
578 return Py_None;
581 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
583 static PyObject *
584 test_null_strings(PyObject *self)
586 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
587 PyObject *tuple = PyTuple_Pack(2, o1, o2);
588 Py_XDECREF(o1);
589 Py_XDECREF(o2);
590 return tuple;
593 static PyObject *
594 raise_exception(PyObject *self, PyObject *args)
596 PyObject *exc;
597 PyObject *exc_args, *v;
598 int num_args, i;
600 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
601 &exc, &num_args))
602 return NULL;
604 exc_args = PyTuple_New(num_args);
605 if (exc_args == NULL)
606 return NULL;
607 for (i = 0; i < num_args; ++i) {
608 v = PyInt_FromLong(i);
609 if (v == NULL) {
610 Py_DECREF(exc_args);
611 return NULL;
613 PyTuple_SET_ITEM(exc_args, i, v);
615 PyErr_SetObject(exc, exc_args);
616 Py_DECREF(exc_args);
617 return NULL;
620 #ifdef WITH_THREAD
622 /* test_thread_state spawns a thread of its own, and that thread releases
623 * `thread_done` when it's finished. The driver code has to know when the
624 * thread finishes, because the thread uses a PyObject (the callable) that
625 * may go away when the driver finishes. The former lack of this explicit
626 * synchronization caused rare segfaults, so rare that they were seen only
627 * on a Mac buildbot (although they were possible on any box).
629 static PyThread_type_lock thread_done = NULL;
631 static void
632 _make_call(void *callable)
634 PyObject *rc;
635 PyGILState_STATE s = PyGILState_Ensure();
636 rc = PyObject_CallFunction((PyObject *)callable, "");
637 Py_XDECREF(rc);
638 PyGILState_Release(s);
641 /* Same thing, but releases `thread_done` when it returns. This variant
642 * should be called only from threads spawned by test_thread_state().
644 static void
645 _make_call_from_thread(void *callable)
647 _make_call(callable);
648 PyThread_release_lock(thread_done);
651 static PyObject *
652 test_thread_state(PyObject *self, PyObject *args)
654 PyObject *fn;
656 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
657 return NULL;
659 /* Ensure Python is set up for threading */
660 PyEval_InitThreads();
661 thread_done = PyThread_allocate_lock();
662 if (thread_done == NULL)
663 return PyErr_NoMemory();
664 PyThread_acquire_lock(thread_done, 1);
666 /* Start a new thread with our callback. */
667 PyThread_start_new_thread(_make_call_from_thread, fn);
668 /* Make the callback with the thread lock held by this thread */
669 _make_call(fn);
670 /* Do it all again, but this time with the thread-lock released */
671 Py_BEGIN_ALLOW_THREADS
672 _make_call(fn);
673 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
674 Py_END_ALLOW_THREADS
676 /* And once more with and without a thread
677 XXX - should use a lock and work out exactly what we are trying
678 to test <wink>
680 Py_BEGIN_ALLOW_THREADS
681 PyThread_start_new_thread(_make_call_from_thread, fn);
682 _make_call(fn);
683 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
684 Py_END_ALLOW_THREADS
686 /* Release lock we acquired above. This is required on HP-UX. */
687 PyThread_release_lock(thread_done);
689 PyThread_free_lock(thread_done);
690 Py_RETURN_NONE;
692 #endif
694 /* Some tests of PyString_FromFormat(). This needs more tests. */
695 static PyObject *
696 test_string_from_format(PyObject *self, PyObject *args)
698 PyObject *result;
699 char *msg;
701 #define CHECK_1_FORMAT(FORMAT, TYPE) \
702 result = PyString_FromFormat(FORMAT, (TYPE)1); \
703 if (result == NULL) \
704 return NULL; \
705 if (strcmp(PyString_AsString(result), "1")) { \
706 msg = FORMAT " failed at 1"; \
707 goto Fail; \
709 Py_DECREF(result)
711 CHECK_1_FORMAT("%d", int);
712 CHECK_1_FORMAT("%ld", long);
713 /* The z width modifier was added in Python 2.5. */
714 CHECK_1_FORMAT("%zd", Py_ssize_t);
716 /* The u type code was added in Python 2.5. */
717 CHECK_1_FORMAT("%u", unsigned int);
718 CHECK_1_FORMAT("%lu", unsigned long);
719 CHECK_1_FORMAT("%zu", size_t);
721 Py_RETURN_NONE;
723 Fail:
724 Py_XDECREF(result);
725 return raiseTestError("test_string_from_format", msg);
727 #undef CHECK_1_FORMAT
730 /* This is here to provide a docstring for test_descr. */
731 static PyObject *
732 test_with_docstring(PyObject *self)
734 Py_RETURN_NONE;
737 /* To test the format of tracebacks as printed out. */
738 static PyObject *
739 traceback_print(PyObject *self, PyObject *args)
741 PyObject *file;
742 PyObject *traceback;
743 int result;
745 if (!PyArg_ParseTuple(args, "OO:traceback_print",
746 &traceback, &file))
747 return NULL;
749 result = PyTraceBack_Print(traceback, file);
750 if (result < 0)
751 return NULL;
752 Py_RETURN_NONE;
755 static PyMethodDef TestMethods[] = {
756 {"raise_exception", raise_exception, METH_VARARGS},
757 {"test_config", (PyCFunction)test_config, METH_NOARGS},
758 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
759 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
760 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
761 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
762 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
763 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
764 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
765 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
766 PyDoc_STR("This is a pretty normal docstring.")},
768 {"getargs_tuple", getargs_tuple, METH_VARARGS},
769 {"getargs_keywords", (PyCFunction)getargs_keywords,
770 METH_VARARGS|METH_KEYWORDS},
771 {"getargs_b", getargs_b, METH_VARARGS},
772 {"getargs_B", getargs_B, METH_VARARGS},
773 {"getargs_H", getargs_H, METH_VARARGS},
774 {"getargs_I", getargs_I, METH_VARARGS},
775 {"getargs_k", getargs_k, METH_VARARGS},
776 {"getargs_i", getargs_i, METH_VARARGS},
777 {"getargs_l", getargs_l, METH_VARARGS},
778 {"getargs_n", getargs_n, METH_VARARGS},
779 #ifdef HAVE_LONG_LONG
780 {"getargs_L", getargs_L, METH_VARARGS},
781 {"getargs_K", getargs_K, METH_VARARGS},
782 {"test_longlong_api", test_longlong_api, METH_NOARGS},
783 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
784 {"codec_incrementalencoder",
785 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
786 {"codec_incrementaldecoder",
787 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
788 #endif
789 #ifdef Py_USING_UNICODE
790 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
791 #endif
792 #ifdef WITH_THREAD
793 {"_test_thread_state", test_thread_state, METH_VARARGS},
794 #endif
795 {"traceback_print", traceback_print, METH_VARARGS},
796 {NULL, NULL} /* sentinel */
799 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
801 typedef struct {
802 char bool_member;
803 char byte_member;
804 unsigned char ubyte_member;
805 short short_member;
806 unsigned short ushort_member;
807 int int_member;
808 unsigned int uint_member;
809 long long_member;
810 unsigned long ulong_member;
811 float float_member;
812 double double_member;
813 #ifdef HAVE_LONG_LONG
814 PY_LONG_LONG longlong_member;
815 unsigned PY_LONG_LONG ulonglong_member;
816 #endif
817 } all_structmembers;
819 typedef struct {
820 PyObject_HEAD
821 all_structmembers structmembers;
822 } test_structmembers;
824 static struct PyMemberDef test_members[] = {
825 {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL},
826 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
827 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
828 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
829 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
830 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
831 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
832 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
833 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
834 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
835 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
836 #ifdef HAVE_LONG_LONG
837 {"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
838 {"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
839 #endif
840 {NULL}
844 static PyObject *
845 test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
847 static char *keywords[] = {
848 "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT",
849 "T_INT", "T_UINT", "T_LONG", "T_ULONG",
850 "T_FLOAT", "T_DOUBLE",
851 #ifdef HAVE_LONG_LONG
852 "T_LONGLONG", "T_ULONGLONG",
853 #endif
854 NULL};
855 static char *fmt = "|bbBhHiIlkfd"
856 #ifdef HAVE_LONG_LONG
857 "LK"
858 #endif
860 test_structmembers *ob;
861 ob = PyObject_New(test_structmembers, type);
862 if (ob == NULL)
863 return NULL;
864 memset(&ob->structmembers, 0, sizeof(all_structmembers));
865 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
866 &ob->structmembers.bool_member,
867 &ob->structmembers.byte_member,
868 &ob->structmembers.ubyte_member,
869 &ob->structmembers.short_member,
870 &ob->structmembers.ushort_member,
871 &ob->structmembers.int_member,
872 &ob->structmembers.uint_member,
873 &ob->structmembers.long_member,
874 &ob->structmembers.ulong_member,
875 &ob->structmembers.float_member,
876 &ob->structmembers.double_member
877 #ifdef HAVE_LONG_LONG
878 , &ob->structmembers.longlong_member,
879 &ob->structmembers.ulonglong_member
880 #endif
881 )) {
882 Py_DECREF(ob);
883 return NULL;
885 return (PyObject *)ob;
888 static void
889 test_structmembers_free(PyObject *ob)
891 PyObject_FREE(ob);
894 static PyTypeObject test_structmembersType = {
895 PyVarObject_HEAD_INIT(NULL, 0)
896 "test_structmembersType",
897 sizeof(test_structmembers), /* tp_basicsize */
898 0, /* tp_itemsize */
899 test_structmembers_free, /* destructor tp_dealloc */
900 0, /* tp_print */
901 0, /* tp_getattr */
902 0, /* tp_setattr */
903 0, /* tp_compare */
904 0, /* tp_repr */
905 0, /* tp_as_number */
906 0, /* tp_as_sequence */
907 0, /* tp_as_mapping */
908 0, /* tp_hash */
909 0, /* tp_call */
910 0, /* tp_str */
911 PyObject_GenericGetAttr, /* tp_getattro */
912 PyObject_GenericSetAttr, /* tp_setattro */
913 0, /* tp_as_buffer */
914 0, /* tp_flags */
915 "Type containing all structmember types",
916 0, /* traverseproc tp_traverse */
917 0, /* tp_clear */
918 0, /* tp_richcompare */
919 0, /* tp_weaklistoffset */
920 0, /* tp_iter */
921 0, /* tp_iternext */
922 0, /* tp_methods */
923 test_members, /* tp_members */
932 test_structmembers_new, /* tp_new */
936 PyMODINIT_FUNC
937 init_testcapi(void)
939 PyObject *m;
941 m = Py_InitModule("_testcapi", TestMethods);
942 if (m == NULL)
943 return;
945 Py_TYPE(&test_structmembersType)=&PyType_Type;
946 Py_INCREF(&test_structmembersType);
947 PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType);
949 PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
950 PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));
951 PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
952 PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX));
953 PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN));
954 PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
955 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
956 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
957 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
958 PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
959 PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
960 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
961 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
962 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
963 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
964 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
965 PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
966 PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
967 PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
968 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
969 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
971 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
972 Py_INCREF(TestError);
973 PyModule_AddObject(m, "error", TestError);