blob: e4c9c75b60edeee666c3aeedbe1e360fbf2aeb60 [file] [log] [blame]
Tim Peters9ea17ac2001-02-02 05:57:15 +00001/*
2 * C Extension module to test Python interpreter C APIs.
3 *
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 */
7
8#include "Python.h"
9
10static PyObject *TestError; /* set to exception object in init */
11
12/* Test #defines from config.h (particularly the SIZEOF_* defines).
13
14 The ones derived from autoconf on the UNIX-like OSes can be relied
15 upon (in the absence of sloppy cross-compiling), but the Windows
16 platforms have these hardcoded. Better safe than sorry.
17*/
18static PyObject*
19sizeof_error(const char* fatname, const char* typename,
20 int expected, int got)
21{
22 char buf[1024];
23 sprintf(buf, "%s #define == %d but sizeof(%s) == %d",
24 fatname, expected, typename, got);
25 PyErr_SetString(TestError, buf);
26 return (PyObject*)NULL;
27}
28
29static PyObject*
30test_config(PyObject *self, PyObject *args)
31{
32 if (!PyArg_ParseTuple(args, ":test_config"))
33 return NULL;
34
35#define CHECK_SIZEOF(FATNAME, TYPE) \
36 if (FATNAME != sizeof(TYPE)) \
37 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
38
39 CHECK_SIZEOF(SIZEOF_INT, int);
40 CHECK_SIZEOF(SIZEOF_LONG, long);
41 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
42 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
43#ifdef HAVE_LONG_LONG
44 CHECK_SIZEOF(SIZEOF_LONG_LONG, LONG_LONG);
45#endif
46
47#undef CHECK_SIZEOF
48
49 Py_INCREF(Py_None);
50 return Py_None;
51}
52
53static PyMethodDef TestMethods[] = {
54 {"test_config", test_config, METH_VARARGS},
55 {NULL, NULL} /* sentinel */
56};
57
58DL_EXPORT(void)
59init_test(void)
60{
61 PyObject *m, *d;
62
63 m = Py_InitModule("_test", TestMethods);
64
65 TestError = PyErr_NewException("_test.error", NULL, NULL);
66 d = PyModule_GetDict(m);
67 PyDict_SetItemString(d, "error", TestError);
68}