annotate Modules/_testcapimodule.c @ 16798:258dcafe2d0b legacy-trunk

Gustavo Niemeyer <niemeyer@conectiva.com>: Fixed recno support (keys are integers rather than strings). Work around DB bug that cause stdin to be closed by rnopen() when the DB file needed to exist but did not (no longer segfaults). This closes SF tracker patch #403445. Also wrapped some long lines and added whitespace around operators -- FLD.
author Fred Drake <fdrake@acm.org>
date Tue, 27 Feb 2001 18:56:46 +0000
parents 9a8e961b505f
children 0b060cf762ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16447
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1 /*
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2 * C Extension module to test Python interpreter C APIs.
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
3 *
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
4 * The 'test_*' functions exported by this module are run as part of the
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
5 * standard Python regression test, via Lib/test/test_capi.py.
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6 */
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
7
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
8 #include "Python.h"
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
9
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
10 static PyObject *TestError; /* set to exception object in init */
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
11
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
12 /* Test #defines from config.h (particularly the SIZEOF_* defines).
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
13
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
14 The ones derived from autoconf on the UNIX-like OSes can be relied
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
15 upon (in the absence of sloppy cross-compiling), but the Windows
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
16 platforms have these hardcoded. Better safe than sorry.
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17 */
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
18 static PyObject*
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
19 sizeof_error(const char* fatname, const char* typename,
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
20 int expected, int got)
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
21 {
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
22 char buf[1024];
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
23 sprintf(buf, "%s #define == %d but sizeof(%s) == %d",
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
24 fatname, expected, typename, got);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
25 PyErr_SetString(TestError, buf);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
26 return (PyObject*)NULL;
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
27 }
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
28
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
29 static PyObject*
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
30 test_config(PyObject *self, PyObject *args)
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
31 {
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
32 if (!PyArg_ParseTuple(args, ":test_config"))
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
33 return NULL;
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
34
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
35 #define CHECK_SIZEOF(FATNAME, TYPE) \
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
36 if (FATNAME != sizeof(TYPE)) \
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
37 return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
38
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
39 CHECK_SIZEOF(SIZEOF_INT, int);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
40 CHECK_SIZEOF(SIZEOF_LONG, long);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
41 CHECK_SIZEOF(SIZEOF_VOID_P, void*);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
42 CHECK_SIZEOF(SIZEOF_TIME_T, time_t);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
43 #ifdef HAVE_LONG_LONG
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
44 CHECK_SIZEOF(SIZEOF_LONG_LONG, LONG_LONG);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
45 #endif
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
46
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
47 #undef CHECK_SIZEOF
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
48
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
49 Py_INCREF(Py_None);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
50 return Py_None;
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
51 }
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
52
16582
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
53 static PyObject*
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
54 test_list_api(PyObject *self, PyObject *args)
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
55 {
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
56 PyObject* list;
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
57 int i;
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
58 if (!PyArg_ParseTuple(args, ":test_list_api"))
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
59 return NULL;
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
60
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
61 /* SF bug 132008: PyList_Reverse segfaults */
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
62 #define NLIST 30
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
63 list = PyList_New(NLIST);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
64 if (list == (PyObject*)NULL)
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
65 return (PyObject*)NULL;
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
66 /* list = range(NLIST) */
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
67 for (i = 0; i < NLIST; ++i) {
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
68 PyObject* anint = PyInt_FromLong(i);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
69 if (anint == (PyObject*)NULL) {
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
70 Py_DECREF(list);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
71 return (PyObject*)NULL;
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
72 }
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
73 PyList_SET_ITEM(list, i, anint);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
74 }
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
75 /* list.reverse(), via PyList_Reverse() */
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
76 i = PyList_Reverse(list); /* should not blow up! */
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
77 if (i != 0) {
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
78 Py_DECREF(list);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
79 return (PyObject*)NULL;
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
80 }
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
81 /* Check that list == range(29, -1, -1) now */
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
82 for (i = 0; i < NLIST; ++i) {
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
83 PyObject* anint = PyList_GET_ITEM(list, i);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
84 if (PyInt_AS_LONG(anint) != NLIST-1-i) {
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
85 PyErr_SetString(TestError,
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
86 "test_list_api: reverse screwed up");
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
87 Py_DECREF(list);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
88 return (PyObject*)NULL;
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
89 }
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
90 }
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
91 Py_DECREF(list);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
92 #undef NLIST
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
93
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
94 Py_INCREF(Py_None);
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
95 return Py_None;
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
96 }
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
97
16447
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
98 static PyMethodDef TestMethods[] = {
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
99 {"test_config", test_config, METH_VARARGS},
16582
9a8e961b505f Related to SF bug 132008 (PyList_Reverse blows up).
Tim Peters <tim.peters@gmail.com>
parents: 16447
diff changeset
100 {"test_list_api", test_list_api, METH_VARARGS},
16447
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
101 {NULL, NULL} /* sentinel */
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
102 };
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
103
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
104 DL_EXPORT(void)
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
105 init_testcapi(void)
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
106 {
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
107 PyObject *m, *d;
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
108
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
109 m = Py_InitModule("_testcapi", TestMethods);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
110
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
111 TestError = PyErr_NewException("_testcapi.error", NULL, NULL);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
112 d = PyModule_GetDict(m);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
113 PyDict_SetItemString(d, "error", TestError);
ac52d3727867 Renamed _testXXX to _testcapiXXX. Jack is my hero -- good call!
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
114 }