Updated documentation for findCaller() to indicate that a 3-tuple is now returned...
[python.git] / Modules / _testcapimodule.c
blobb11f0aebfc1135a0a52ffc2f40d3ceaa1c9f55b4
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 /* Functions to call PyArg_ParseTuple with integer format codes,
310 and return the result.
312 static PyObject *
313 getargs_b(PyObject *self, PyObject *args)
315 unsigned char value;
316 if (!PyArg_ParseTuple(args, "b", &value))
317 return NULL;
318 return PyLong_FromUnsignedLong((unsigned long)value);
321 static PyObject *
322 getargs_B(PyObject *self, PyObject *args)
324 unsigned char value;
325 if (!PyArg_ParseTuple(args, "B", &value))
326 return NULL;
327 return PyLong_FromUnsignedLong((unsigned long)value);
330 static PyObject *
331 getargs_H(PyObject *self, PyObject *args)
333 unsigned short value;
334 if (!PyArg_ParseTuple(args, "H", &value))
335 return NULL;
336 return PyLong_FromUnsignedLong((unsigned long)value);
339 static PyObject *
340 getargs_I(PyObject *self, PyObject *args)
342 unsigned int value;
343 if (!PyArg_ParseTuple(args, "I", &value))
344 return NULL;
345 return PyLong_FromUnsignedLong((unsigned long)value);
348 static PyObject *
349 getargs_k(PyObject *self, PyObject *args)
351 unsigned long value;
352 if (!PyArg_ParseTuple(args, "k", &value))
353 return NULL;
354 return PyLong_FromUnsignedLong(value);
357 static PyObject *
358 getargs_i(PyObject *self, PyObject *args)
360 int value;
361 if (!PyArg_ParseTuple(args, "i", &value))
362 return NULL;
363 return PyLong_FromLong((long)value);
366 static PyObject *
367 getargs_l(PyObject *self, PyObject *args)
369 long value;
370 if (!PyArg_ParseTuple(args, "l", &value))
371 return NULL;
372 return PyLong_FromLong(value);
375 static PyObject *
376 getargs_n(PyObject *self, PyObject *args)
378 Py_ssize_t value;
379 if (!PyArg_ParseTuple(args, "n", &value))
380 return NULL;
381 return PyInt_FromSsize_t(value);
384 #ifdef HAVE_LONG_LONG
385 static PyObject *
386 getargs_L(PyObject *self, PyObject *args)
388 PY_LONG_LONG value;
389 if (!PyArg_ParseTuple(args, "L", &value))
390 return NULL;
391 return PyLong_FromLongLong(value);
394 static PyObject *
395 getargs_K(PyObject *self, PyObject *args)
397 unsigned PY_LONG_LONG value;
398 if (!PyArg_ParseTuple(args, "K", &value))
399 return NULL;
400 return PyLong_FromUnsignedLongLong(value);
402 #endif
404 /* This function not only tests the 'k' getargs code, but also the
405 PyInt_AsUnsignedLongMask() and PyInt_AsUnsignedLongMask() functions. */
406 static PyObject *
407 test_k_code(PyObject *self)
409 PyObject *tuple, *num;
410 unsigned long value;
412 tuple = PyTuple_New(1);
413 if (tuple == NULL)
414 return NULL;
416 /* a number larger than ULONG_MAX even on 64-bit platforms */
417 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
418 if (num == NULL)
419 return NULL;
421 value = PyInt_AsUnsignedLongMask(num);
422 if (value != ULONG_MAX)
423 return raiseTestError("test_k_code",
424 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
426 PyTuple_SET_ITEM(tuple, 0, num);
428 value = 0;
429 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
430 return NULL;
431 if (value != ULONG_MAX)
432 return raiseTestError("test_k_code",
433 "k code returned wrong value for long 0xFFF...FFF");
435 Py_DECREF(num);
436 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
437 if (num == NULL)
438 return NULL;
440 value = PyInt_AsUnsignedLongMask(num);
441 if (value != (unsigned long)-0x42)
442 return raiseTestError("test_k_code",
443 "PyInt_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
445 PyTuple_SET_ITEM(tuple, 0, num);
447 value = 0;
448 if (PyArg_ParseTuple(tuple, "k:test_k_code", &value) < 0)
449 return NULL;
450 if (value != (unsigned long)-0x42)
451 return raiseTestError("test_k_code",
452 "k code returned wrong value for long -0xFFF..000042");
454 Py_DECREF(tuple);
455 Py_INCREF(Py_None);
456 return Py_None;
459 #ifdef Py_USING_UNICODE
461 /* Test the u and u# codes for PyArg_ParseTuple. May leak memory in case
462 of an error.
464 static PyObject *
465 test_u_code(PyObject *self)
467 PyObject *tuple, *obj;
468 Py_UNICODE *value;
469 int len;
471 tuple = PyTuple_New(1);
472 if (tuple == NULL)
473 return NULL;
475 obj = PyUnicode_Decode("test", strlen("test"),
476 "ascii", NULL);
477 if (obj == NULL)
478 return NULL;
480 PyTuple_SET_ITEM(tuple, 0, obj);
482 value = 0;
483 if (PyArg_ParseTuple(tuple, "u:test_u_code", &value) < 0)
484 return NULL;
485 if (value != PyUnicode_AS_UNICODE(obj))
486 return raiseTestError("test_u_code",
487 "u code returned wrong value for u'test'");
488 value = 0;
489 if (PyArg_ParseTuple(tuple, "u#:test_u_code", &value, &len) < 0)
490 return NULL;
491 if (value != PyUnicode_AS_UNICODE(obj) ||
492 len != PyUnicode_GET_SIZE(obj))
493 return raiseTestError("test_u_code",
494 "u# code returned wrong values for u'test'");
496 Py_DECREF(tuple);
497 Py_INCREF(Py_None);
498 return Py_None;
501 static PyObject *
502 codec_incrementalencoder(PyObject *self, PyObject *args)
504 const char *encoding, *errors = NULL;
505 if (!PyArg_ParseTuple(args, "s|s:test_incrementalencoder",
506 &encoding, &errors))
507 return NULL;
508 return PyCodec_IncrementalEncoder(encoding, errors);
511 static PyObject *
512 codec_incrementaldecoder(PyObject *self, PyObject *args)
514 const char *encoding, *errors = NULL;
515 if (!PyArg_ParseTuple(args, "s|s:test_incrementaldecoder",
516 &encoding, &errors))
517 return NULL;
518 return PyCodec_IncrementalDecoder(encoding, errors);
521 #endif
523 /* Simple test of _PyLong_NumBits and _PyLong_Sign. */
524 static PyObject *
525 test_long_numbits(PyObject *self)
527 struct triple {
528 long input;
529 size_t nbits;
530 int sign;
531 } testcases[] = {{0, 0, 0},
532 {1L, 1, 1},
533 {-1L, 1, -1},
534 {2L, 2, 1},
535 {-2L, 2, -1},
536 {3L, 2, 1},
537 {-3L, 2, -1},
538 {4L, 3, 1},
539 {-4L, 3, -1},
540 {0x7fffL, 15, 1}, /* one Python long digit */
541 {-0x7fffL, 15, -1},
542 {0xffffL, 16, 1},
543 {-0xffffL, 16, -1},
544 {0xfffffffL, 28, 1},
545 {-0xfffffffL, 28, -1}};
546 int i;
548 for (i = 0; i < sizeof(testcases) / sizeof(struct triple); ++i) {
549 PyObject *plong = PyLong_FromLong(testcases[i].input);
550 size_t nbits = _PyLong_NumBits(plong);
551 int sign = _PyLong_Sign(plong);
553 Py_DECREF(plong);
554 if (nbits != testcases[i].nbits)
555 return raiseTestError("test_long_numbits",
556 "wrong result for _PyLong_NumBits");
557 if (sign != testcases[i].sign)
558 return raiseTestError("test_long_numbits",
559 "wrong result for _PyLong_Sign");
561 Py_INCREF(Py_None);
562 return Py_None;
565 /* Example passing NULLs to PyObject_Str(NULL) and PyObject_Unicode(NULL). */
567 static PyObject *
568 test_null_strings(PyObject *self)
570 PyObject *o1 = PyObject_Str(NULL), *o2 = PyObject_Unicode(NULL);
571 PyObject *tuple = PyTuple_Pack(2, o1, o2);
572 Py_XDECREF(o1);
573 Py_XDECREF(o2);
574 return tuple;
577 static PyObject *
578 raise_exception(PyObject *self, PyObject *args)
580 PyObject *exc;
581 PyObject *exc_args, *v;
582 int num_args, i;
584 if (!PyArg_ParseTuple(args, "Oi:raise_exception",
585 &exc, &num_args))
586 return NULL;
588 exc_args = PyTuple_New(num_args);
589 if (exc_args == NULL)
590 return NULL;
591 for (i = 0; i < num_args; ++i) {
592 v = PyInt_FromLong(i);
593 if (v == NULL) {
594 Py_DECREF(exc_args);
595 return NULL;
597 PyTuple_SET_ITEM(exc_args, i, v);
599 PyErr_SetObject(exc, exc_args);
600 Py_DECREF(exc_args);
601 return NULL;
604 #ifdef WITH_THREAD
606 /* test_thread_state spawns a thread of its own, and that thread releases
607 * `thread_done` when it's finished. The driver code has to know when the
608 * thread finishes, because the thread uses a PyObject (the callable) that
609 * may go away when the driver finishes. The former lack of this explicit
610 * synchronization caused rare segfaults, so rare that they were seen only
611 * on a Mac buildbot (although they were possible on any box).
613 static PyThread_type_lock thread_done = NULL;
615 static void
616 _make_call(void *callable)
618 PyObject *rc;
619 PyGILState_STATE s = PyGILState_Ensure();
620 rc = PyObject_CallFunction((PyObject *)callable, "");
621 Py_XDECREF(rc);
622 PyGILState_Release(s);
625 /* Same thing, but releases `thread_done` when it returns. This variant
626 * should be called only from threads spawned by test_thread_state().
628 static void
629 _make_call_from_thread(void *callable)
631 _make_call(callable);
632 PyThread_release_lock(thread_done);
635 static PyObject *
636 test_thread_state(PyObject *self, PyObject *args)
638 PyObject *fn;
640 if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn))
641 return NULL;
643 /* Ensure Python is set up for threading */
644 PyEval_InitThreads();
645 thread_done = PyThread_allocate_lock();
646 if (thread_done == NULL)
647 return PyErr_NoMemory();
648 PyThread_acquire_lock(thread_done, 1);
650 /* Start a new thread with our callback. */
651 PyThread_start_new_thread(_make_call_from_thread, fn);
652 /* Make the callback with the thread lock held by this thread */
653 _make_call(fn);
654 /* Do it all again, but this time with the thread-lock released */
655 Py_BEGIN_ALLOW_THREADS
656 _make_call(fn);
657 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
658 Py_END_ALLOW_THREADS
660 /* And once more with and without a thread
661 XXX - should use a lock and work out exactly what we are trying
662 to test <wink>
664 Py_BEGIN_ALLOW_THREADS
665 PyThread_start_new_thread(_make_call_from_thread, fn);
666 _make_call(fn);
667 PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */
668 Py_END_ALLOW_THREADS
670 /* Release lock we acquired above. This is required on HP-UX. */
671 PyThread_release_lock(thread_done);
673 PyThread_free_lock(thread_done);
674 Py_RETURN_NONE;
676 #endif
678 /* Some tests of PyString_FromFormat(). This needs more tests. */
679 static PyObject *
680 test_string_from_format(PyObject *self, PyObject *args)
682 PyObject *result;
683 char *msg;
685 #define CHECK_1_FORMAT(FORMAT, TYPE) \
686 result = PyString_FromFormat(FORMAT, (TYPE)1); \
687 if (result == NULL) \
688 return NULL; \
689 if (strcmp(PyString_AsString(result), "1")) { \
690 msg = FORMAT " failed at 1"; \
691 goto Fail; \
693 Py_DECREF(result)
695 CHECK_1_FORMAT("%d", int);
696 CHECK_1_FORMAT("%ld", long);
697 /* The z width modifier was added in Python 2.5. */
698 CHECK_1_FORMAT("%zd", Py_ssize_t);
700 /* The u type code was added in Python 2.5. */
701 CHECK_1_FORMAT("%u", unsigned int);
702 CHECK_1_FORMAT("%lu", unsigned long);
703 CHECK_1_FORMAT("%zu", size_t);
705 Py_RETURN_NONE;
707 Fail:
708 Py_XDECREF(result);
709 return raiseTestError("test_string_from_format", msg);
711 #undef CHECK_1_FORMAT
714 /* This is here to provide a docstring for test_descr. */
715 static PyObject *
716 test_with_docstring(PyObject *self)
718 Py_RETURN_NONE;
721 static PyMethodDef TestMethods[] = {
722 {"raise_exception", raise_exception, METH_VARARGS},
723 {"test_config", (PyCFunction)test_config, METH_NOARGS},
724 {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
725 {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
726 {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS},
727 {"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
728 {"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
729 {"test_null_strings", (PyCFunction)test_null_strings, METH_NOARGS},
730 {"test_string_from_format", (PyCFunction)test_string_from_format, METH_NOARGS},
731 {"test_with_docstring", (PyCFunction)test_with_docstring, METH_NOARGS,
732 PyDoc_STR("This is a pretty normal docstring.")},
734 {"getargs_tuple", getargs_tuple, METH_VARARGS},
735 {"getargs_b", getargs_b, METH_VARARGS},
736 {"getargs_B", getargs_B, METH_VARARGS},
737 {"getargs_H", getargs_H, METH_VARARGS},
738 {"getargs_I", getargs_I, METH_VARARGS},
739 {"getargs_k", getargs_k, METH_VARARGS},
740 {"getargs_i", getargs_i, METH_VARARGS},
741 {"getargs_l", getargs_l, METH_VARARGS},
742 {"getargs_n", getargs_n, METH_VARARGS},
743 #ifdef HAVE_LONG_LONG
744 {"getargs_L", getargs_L, METH_VARARGS},
745 {"getargs_K", getargs_K, METH_VARARGS},
746 {"test_longlong_api", test_longlong_api, METH_NOARGS},
747 {"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
748 {"codec_incrementalencoder",
749 (PyCFunction)codec_incrementalencoder, METH_VARARGS},
750 {"codec_incrementaldecoder",
751 (PyCFunction)codec_incrementaldecoder, METH_VARARGS},
752 #endif
753 #ifdef Py_USING_UNICODE
754 {"test_u_code", (PyCFunction)test_u_code, METH_NOARGS},
755 #endif
756 #ifdef WITH_THREAD
757 {"_test_thread_state", test_thread_state, METH_VARARGS},
758 #endif
759 {NULL, NULL} /* sentinel */
762 #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);}
764 typedef struct {
765 char byte_member;
766 unsigned char ubyte_member;
767 short short_member;
768 unsigned short ushort_member;
769 int int_member;
770 unsigned int uint_member;
771 long long_member;
772 unsigned long ulong_member;
773 float float_member;
774 double double_member;
775 } all_structmembers;
777 typedef struct {
778 PyObject_HEAD
779 all_structmembers structmembers;
780 } test_structmembers;
782 static struct PyMemberDef test_members[] = {
783 {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL},
784 {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL},
785 {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL},
786 {"T_USHORT", T_USHORT, offsetof(test_structmembers, structmembers.ushort_member), 0, NULL},
787 {"T_INT", T_INT, offsetof(test_structmembers, structmembers.int_member), 0, NULL},
788 {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL},
789 {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL},
790 {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
791 {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
792 {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
793 {NULL}
797 static PyObject *test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs){
798 static char *keywords[]={"T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", "T_INT", "T_UINT",
799 "T_LONG", "T_ULONG", "T_FLOAT", "T_DOUBLE", NULL};
800 test_structmembers *ob=PyObject_New(test_structmembers, type);
801 if (ob==NULL)
802 return NULL;
803 memset(&ob->structmembers, 0, sizeof(all_structmembers));
804 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|bBhHiIlkfd", keywords,
805 &ob->structmembers.byte_member, &ob->structmembers.ubyte_member,
806 &ob->structmembers.short_member, &ob->structmembers.ushort_member,
807 &ob->structmembers.int_member, &ob->structmembers.uint_member,
808 &ob->structmembers.long_member, &ob->structmembers.ulong_member,
809 &ob->structmembers.float_member, &ob->structmembers.double_member)){
810 Py_DECREF(ob);
811 return NULL;
813 return (PyObject *)ob;
816 static void test_structmembers_free(PyObject *ob){
817 PyObject_FREE(ob);
820 static PyTypeObject test_structmembersType = {
821 PyObject_HEAD_INIT(NULL)
823 "test_structmembersType",
824 sizeof(test_structmembers), /* tp_basicsize */
825 0, /* tp_itemsize */
826 test_structmembers_free, /* destructor tp_dealloc */
827 0, /* tp_print */
828 0, /* tp_getattr */
829 0, /* tp_setattr */
830 0, /* tp_compare */
831 0, /* tp_repr */
832 0, /* tp_as_number */
833 0, /* tp_as_sequence */
834 0, /* tp_as_mapping */
835 0, /* tp_hash */
836 0, /* tp_call */
837 0, /* tp_str */
838 PyObject_GenericGetAttr,
839 PyObject_GenericSetAttr,
840 0, /* tp_as_buffer */
841 0, /* tp_flags */
842 "Type containing all structmember types",
843 0, /* traverseproc tp_traverse */
844 0, /* tp_clear */
845 0, /* tp_richcompare */
846 0, /* tp_weaklistoffset */
847 0, /* tp_iter */
848 0, /* tp_iternext */
849 0, /* tp_methods */
850 test_members, /* tp_members */
859 test_structmembers_new, /* tp_new */
863 PyMODINIT_FUNC
864 init_testcapi(void)
866 PyObject *m;
868 m = Py_InitModule("_testcapi", TestMethods);
869 if (m == NULL)
870 return;
872 test_structmembersType.ob_type=&PyType_Type;
873 Py_INCREF(&test_structmembersType);
874 PyModule_AddObject(m, "test_structmembersType", (PyObject *)&test_structmembersType);
876 PyModule_AddObject(m, "CHAR_MAX", PyInt_FromLong(CHAR_MAX));
877 PyModule_AddObject(m, "CHAR_MIN", PyInt_FromLong(CHAR_MIN));
878 PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
879 PyModule_AddObject(m, "SHRT_MAX", PyInt_FromLong(SHRT_MAX));
880 PyModule_AddObject(m, "SHRT_MIN", PyInt_FromLong(SHRT_MIN));
881 PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
882 PyModule_AddObject(m, "INT_MAX", PyLong_FromLong(INT_MAX));
883 PyModule_AddObject(m, "INT_MIN", PyLong_FromLong(INT_MIN));
884 PyModule_AddObject(m, "UINT_MAX", PyLong_FromUnsignedLong(UINT_MAX));
885 PyModule_AddObject(m, "LONG_MAX", PyInt_FromLong(LONG_MAX));
886 PyModule_AddObject(m, "LONG_MIN", PyInt_FromLong(LONG_MIN));
887 PyModule_AddObject(m, "ULONG_MAX", PyLong_FromUnsignedLong(ULONG_MAX));
888 PyModule_AddObject(m, "FLT_MAX", PyFloat_FromDouble(FLT_MAX));
889 PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
890 PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
891 PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
892 PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
893 PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
895 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
896 Py_INCREF(TestError);
897 PyModule_AddObject(m, "error", TestError);