Fixed bug in time-to-midnight calculation.
[python.git] / Modules / xxsubtype.c
blobffbc72b1657ef7a0c4a5b8a54384d4ad6ac394d8
1 #include "Python.h"
2 #include "structmember.h"
4 PyDoc_STRVAR(xxsubtype__doc__,
5 "xxsubtype is an example module showing how to subtype builtin types from C.\n"
6 "test_descr.py in the standard test suite requires it in order to complete.\n"
7 "If you don't care about the examples, and don't intend to run the Python\n"
8 "test suite, you can recompile Python without Modules/xxsubtype.c.");
10 /* We link this module statically for convenience. If compiled as a shared
11 library instead, some compilers don't allow addresses of Python objects
12 defined in other libraries to be used in static initializers here. The
13 DEFERRED_ADDRESS macro is used to tag the slots where such addresses
14 appear; the module init function must fill in the tagged slots at runtime.
15 The argument is for documentation -- the macro ignores it.
17 #define DEFERRED_ADDRESS(ADDR) 0
19 /* spamlist -- a list subtype */
21 typedef struct {
22 PyListObject list;
23 int state;
24 } spamlistobject;
26 static PyObject *
27 spamlist_getstate(spamlistobject *self, PyObject *args)
29 if (!PyArg_ParseTuple(args, ":getstate"))
30 return NULL;
31 return PyInt_FromLong(self->state);
34 static PyObject *
35 spamlist_setstate(spamlistobject *self, PyObject *args)
37 int state;
39 if (!PyArg_ParseTuple(args, "i:setstate", &state))
40 return NULL;
41 self->state = state;
42 Py_INCREF(Py_None);
43 return Py_None;
46 static PyObject *
47 spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
49 PyObject *result = PyTuple_New(3);
51 if (result != NULL) {
52 if (self == NULL)
53 self = Py_None;
54 if (kw == NULL)
55 kw = Py_None;
56 Py_INCREF(self);
57 PyTuple_SET_ITEM(result, 0, self);
58 Py_INCREF(args);
59 PyTuple_SET_ITEM(result, 1, args);
60 Py_INCREF(kw);
61 PyTuple_SET_ITEM(result, 2, kw);
63 return result;
66 static PyMethodDef spamlist_methods[] = {
67 {"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
68 PyDoc_STR("getstate() -> state")},
69 {"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
70 PyDoc_STR("setstate(state)")},
71 /* These entries differ only in the flags; they are used by the tests
72 in test.test_descr. */
73 {"classmeth", (PyCFunction)spamlist_specialmeth,
74 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
75 PyDoc_STR("classmeth(*args, **kw)")},
76 {"staticmeth", (PyCFunction)spamlist_specialmeth,
77 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
78 PyDoc_STR("staticmeth(*args, **kw)")},
79 {NULL, NULL},
82 static PyTypeObject spamlist_type;
84 static int
85 spamlist_init(spamlistobject *self, PyObject *args, PyObject *kwds)
87 if (PyList_Type.tp_init((PyObject *)self, args, kwds) < 0)
88 return -1;
89 self->state = 0;
90 return 0;
93 static PyObject *
94 spamlist_state_get(spamlistobject *self)
96 return PyInt_FromLong(self->state);
99 static PyGetSetDef spamlist_getsets[] = {
100 {"state", (getter)spamlist_state_get, NULL,
101 PyDoc_STR("an int variable for demonstration purposes")},
105 static PyTypeObject spamlist_type = {
106 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
108 "xxsubtype.spamlist",
109 sizeof(spamlistobject),
111 0, /* tp_dealloc */
112 0, /* tp_print */
113 0, /* tp_getattr */
114 0, /* tp_setattr */
115 0, /* tp_compare */
116 0, /* tp_repr */
117 0, /* tp_as_number */
118 0, /* tp_as_sequence */
119 0, /* tp_as_mapping */
120 0, /* tp_hash */
121 0, /* tp_call */
122 0, /* tp_str */
123 0, /* tp_getattro */
124 0, /* tp_setattro */
125 0, /* tp_as_buffer */
126 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
127 0, /* tp_doc */
128 0, /* tp_traverse */
129 0, /* tp_clear */
130 0, /* tp_richcompare */
131 0, /* tp_weaklistoffset */
132 0, /* tp_iter */
133 0, /* tp_iternext */
134 spamlist_methods, /* tp_methods */
135 0, /* tp_members */
136 spamlist_getsets, /* tp_getset */
137 DEFERRED_ADDRESS(&PyList_Type), /* tp_base */
138 0, /* tp_dict */
139 0, /* tp_descr_get */
140 0, /* tp_descr_set */
141 0, /* tp_dictoffset */
142 (initproc)spamlist_init, /* tp_init */
143 0, /* tp_alloc */
144 0, /* tp_new */
147 /* spamdict -- a dict subtype */
149 typedef struct {
150 PyDictObject dict;
151 int state;
152 } spamdictobject;
154 static PyObject *
155 spamdict_getstate(spamdictobject *self, PyObject *args)
157 if (!PyArg_ParseTuple(args, ":getstate"))
158 return NULL;
159 return PyInt_FromLong(self->state);
162 static PyObject *
163 spamdict_setstate(spamdictobject *self, PyObject *args)
165 int state;
167 if (!PyArg_ParseTuple(args, "i:setstate", &state))
168 return NULL;
169 self->state = state;
170 Py_INCREF(Py_None);
171 return Py_None;
174 static PyMethodDef spamdict_methods[] = {
175 {"getstate", (PyCFunction)spamdict_getstate, METH_VARARGS,
176 PyDoc_STR("getstate() -> state")},
177 {"setstate", (PyCFunction)spamdict_setstate, METH_VARARGS,
178 PyDoc_STR("setstate(state)")},
179 {NULL, NULL},
182 static PyTypeObject spamdict_type;
184 static int
185 spamdict_init(spamdictobject *self, PyObject *args, PyObject *kwds)
187 if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
188 return -1;
189 self->state = 0;
190 return 0;
193 static PyMemberDef spamdict_members[] = {
194 {"state", T_INT, offsetof(spamdictobject, state), READONLY,
195 PyDoc_STR("an int variable for demonstration purposes")},
199 static PyTypeObject spamdict_type = {
200 PyObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type))
202 "xxsubtype.spamdict",
203 sizeof(spamdictobject),
205 0, /* tp_dealloc */
206 0, /* tp_print */
207 0, /* tp_getattr */
208 0, /* tp_setattr */
209 0, /* tp_compare */
210 0, /* tp_repr */
211 0, /* tp_as_number */
212 0, /* tp_as_sequence */
213 0, /* tp_as_mapping */
214 0, /* tp_hash */
215 0, /* tp_call */
216 0, /* tp_str */
217 0, /* tp_getattro */
218 0, /* tp_setattro */
219 0, /* tp_as_buffer */
220 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
221 0, /* tp_doc */
222 0, /* tp_traverse */
223 0, /* tp_clear */
224 0, /* tp_richcompare */
225 0, /* tp_weaklistoffset */
226 0, /* tp_iter */
227 0, /* tp_iternext */
228 spamdict_methods, /* tp_methods */
229 spamdict_members, /* tp_members */
230 0, /* tp_getset */
231 DEFERRED_ADDRESS(&PyDict_Type), /* tp_base */
232 0, /* tp_dict */
233 0, /* tp_descr_get */
234 0, /* tp_descr_set */
235 0, /* tp_dictoffset */
236 (initproc)spamdict_init, /* tp_init */
237 0, /* tp_alloc */
238 0, /* tp_new */
241 static PyObject *
242 spam_bench(PyObject *self, PyObject *args)
244 PyObject *obj, *name, *res;
245 int n = 1000;
246 time_t t0, t1;
248 if (!PyArg_ParseTuple(args, "OS|i", &obj, &name, &n))
249 return NULL;
250 t0 = clock();
251 while (--n >= 0) {
252 res = PyObject_GetAttr(obj, name);
253 if (res == NULL)
254 return NULL;
255 Py_DECREF(res);
257 t1 = clock();
258 return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
261 static PyMethodDef xxsubtype_functions[] = {
262 {"bench", spam_bench, METH_VARARGS},
263 {NULL, NULL} /* sentinel */
266 PyMODINIT_FUNC
267 initxxsubtype(void)
269 PyObject *m;
271 /* Fill in deferred data addresses. This must be done before
272 PyType_Ready() is called. Note that PyType_Ready() automatically
273 initializes the ob.ob_type field to &PyType_Type if it's NULL,
274 so it's not necessary to fill in ob_type first. */
275 spamdict_type.tp_base = &PyDict_Type;
276 if (PyType_Ready(&spamdict_type) < 0)
277 return;
279 spamlist_type.tp_base = &PyList_Type;
280 if (PyType_Ready(&spamlist_type) < 0)
281 return;
283 m = Py_InitModule3("xxsubtype",
284 xxsubtype_functions,
285 xxsubtype__doc__);
286 if (m == NULL)
287 return;
289 if (PyType_Ready(&spamlist_type) < 0)
290 return;
291 if (PyType_Ready(&spamdict_type) < 0)
292 return;
294 Py_INCREF(&spamlist_type);
295 if (PyModule_AddObject(m, "spamlist",
296 (PyObject *) &spamlist_type) < 0)
297 return;
299 Py_INCREF(&spamdict_type);
300 if (PyModule_AddObject(m, "spamdict",
301 (PyObject *) &spamdict_type) < 0)
302 return;