Tim Peters | 9ea17ac | 2001-02-02 05:57:15 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 10 | static 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 | */ |
| 18 | static PyObject* |
| 19 | sizeof_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 | |
| 29 | static PyObject* |
| 30 | test_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 | |
| 53 | static PyMethodDef TestMethods[] = { |
| 54 | {"test_config", test_config, METH_VARARGS}, |
| 55 | {NULL, NULL} /* sentinel */ |
| 56 | }; |
| 57 | |
| 58 | DL_EXPORT(void) |
| 59 | init_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 | } |