samba:python - Py_RETURN_NONE remove compatibility code for releases < 2.4
[samba.git] / lib / tdb / pytdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Python interface to tdb.
5
6    Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7    Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
8
9      ** NOTE! The following LGPL license applies to the tdb
10      ** library. This does NOT imply that all of Samba is released
11      ** under the LGPL
12
13    This library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 3 of the License, or (at your option) any later version.
17
18    This library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22
23    You should have received a copy of the GNU Lesser General Public
24    License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 */
26
27 #include <Python.h>
28 #include "replace.h"
29 #include "system/filesys.h"
30
31 /* Include tdb headers */
32 #include <tdb.h>
33
34 typedef struct {
35         PyObject_HEAD
36         TDB_CONTEXT *ctx;
37         bool closed;
38 } PyTdbObject;
39
40 staticforward PyTypeObject PyTdb;
41
42 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
43 {
44         PyErr_SetObject(PyExc_RuntimeError, 
45                 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
46 }
47
48 static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
49 {
50         TDB_DATA ret;
51         ret.dptr = (unsigned char *)PyString_AsString(data);
52         ret.dsize = PyString_Size(data);
53         return ret;
54 }
55
56 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
57 {
58         if (data.dptr == NULL && data.dsize == 0) {
59                 Py_RETURN_NONE;
60         } else {
61                 PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr, 
62                                                                                                    data.dsize);
63                 free(data.dptr);
64                 return ret;
65     }
66 }
67
68 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
69         if (ret != 0) { \
70                 PyErr_SetTDBError(tdb); \
71                 return NULL; \
72         }
73
74 #define PyErr_TDB_RAISE_IF_CLOSED(self) \
75         if (self->closed) {                                             \
76                 PyErr_SetObject(PyExc_RuntimeError,                             \
77                                 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
78                 return NULL;                                            \
79         }
80
81 #define PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self) \
82         if (self->closed) {                                             \
83                 PyErr_SetObject(PyExc_RuntimeError,                             \
84                                 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
85                 return -1;                                              \
86         }
87
88 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
89 {
90         char *name = NULL;
91         int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
92         TDB_CONTEXT *ctx;
93         PyTdbObject *ret;
94         const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
95
96         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
97                 return NULL;
98
99         if (name == NULL) {
100                 tdb_flags |= TDB_INTERNAL;
101         }
102
103         ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
104         if (ctx == NULL) {
105                 PyErr_SetFromErrno(PyExc_IOError);
106                 return NULL;
107         }
108
109         ret = PyObject_New(PyTdbObject, &PyTdb);
110         if (!ret) {
111                 tdb_close(ctx);
112                 return NULL;
113         }
114
115         ret->ctx = ctx;
116         ret->closed = false;
117         return (PyObject *)ret;
118 }
119
120 static PyObject *obj_transaction_cancel(PyTdbObject *self)
121 {
122         int ret;
123
124         PyErr_TDB_RAISE_IF_CLOSED(self);
125
126         ret = tdb_transaction_cancel(self->ctx);
127         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
128         Py_RETURN_NONE;
129 }
130
131 static PyObject *obj_transaction_commit(PyTdbObject *self)
132 {
133         int ret;
134         PyErr_TDB_RAISE_IF_CLOSED(self);
135         ret = tdb_transaction_commit(self->ctx);
136         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
137         Py_RETURN_NONE;
138 }
139
140 static PyObject *obj_transaction_prepare_commit(PyTdbObject *self)
141 {
142         int ret;
143         PyErr_TDB_RAISE_IF_CLOSED(self);
144         ret = tdb_transaction_prepare_commit(self->ctx);
145         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
146         Py_RETURN_NONE;
147 }
148
149 static PyObject *obj_transaction_start(PyTdbObject *self)
150 {
151         int ret;
152         PyErr_TDB_RAISE_IF_CLOSED(self);
153         ret = tdb_transaction_start(self->ctx);
154         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
155         Py_RETURN_NONE;
156 }
157
158 static PyObject *obj_reopen(PyTdbObject *self)
159 {
160         int ret;
161         PyErr_TDB_RAISE_IF_CLOSED(self);
162         ret = tdb_reopen(self->ctx);
163         if (ret != 0) {
164                 self->closed = true;
165                 PyErr_SetObject(PyExc_RuntimeError,
166                                 Py_BuildValue("(i,s)",
167                                               TDB_ERR_IO,
168                                               "Failed to reopen database"));
169                 return NULL;
170         }
171         Py_RETURN_NONE;
172 }
173
174 static PyObject *obj_lockall(PyTdbObject *self)
175 {
176         int ret;
177         PyErr_TDB_RAISE_IF_CLOSED(self);
178         ret = tdb_lockall(self->ctx);
179         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
180         Py_RETURN_NONE;
181 }
182
183 static PyObject *obj_unlockall(PyTdbObject *self)
184 {
185         int ret;
186         PyErr_TDB_RAISE_IF_CLOSED(self);
187         ret = tdb_unlockall(self->ctx);
188         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
189         Py_RETURN_NONE;
190 }
191
192 static PyObject *obj_lockall_read(PyTdbObject *self)
193 {
194         int ret;
195         PyErr_TDB_RAISE_IF_CLOSED(self);
196         ret = tdb_lockall_read(self->ctx);
197         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
198         Py_RETURN_NONE;
199 }
200
201 static PyObject *obj_unlockall_read(PyTdbObject *self)
202 {
203         int ret = tdb_unlockall_read(self->ctx);
204         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
205         Py_RETURN_NONE;
206 }
207
208 static PyObject *obj_close(PyTdbObject *self)
209 {
210         int ret;
211         if (self->closed)
212                 Py_RETURN_NONE;
213         ret = tdb_close(self->ctx);
214         self->closed = true;
215         if (ret != 0) {
216                 PyErr_SetObject(PyExc_RuntimeError,
217                                 Py_BuildValue("(i,s)",
218                                               TDB_ERR_IO,
219                                               "Failed to close database"));
220                 return NULL;
221         }
222         Py_RETURN_NONE;
223 }
224
225 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
226 {
227         TDB_DATA key;
228         PyObject *py_key;
229
230         PyErr_TDB_RAISE_IF_CLOSED(self);
231
232         if (!PyArg_ParseTuple(args, "O", &py_key))
233                 return NULL;
234
235         key = PyString_AsTDB_DATA(py_key);
236         if (!key.dptr)
237                 return NULL;
238
239         return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
240 }
241
242 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
243 {
244         TDB_DATA key, data;
245         PyObject *py_key, *py_data;
246         int ret;
247
248         PyErr_TDB_RAISE_IF_CLOSED(self);
249
250         if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
251                 return NULL;
252
253         key = PyString_AsTDB_DATA(py_key);
254         if (!key.dptr)
255                 return NULL;
256         data = PyString_AsTDB_DATA(py_data);
257         if (!data.dptr)
258                 return NULL;
259
260         ret = tdb_append(self->ctx, key, data);
261         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
262         Py_RETURN_NONE;
263 }
264
265 static PyObject *obj_firstkey(PyTdbObject *self)
266 {
267         PyErr_TDB_RAISE_IF_CLOSED(self);
268
269         return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
270 }
271
272 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
273 {
274         TDB_DATA key;
275         PyObject *py_key;
276         PyErr_TDB_RAISE_IF_CLOSED(self);
277
278         if (!PyArg_ParseTuple(args, "O", &py_key))
279                 return NULL;
280
281         key = PyString_AsTDB_DATA(py_key);
282         if (!key.dptr)
283                 return NULL;
284         
285         return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
286 }
287
288 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
289 {
290         TDB_DATA key;
291         PyObject *py_key;
292         int ret;
293         PyErr_TDB_RAISE_IF_CLOSED(self);
294
295         if (!PyArg_ParseTuple(args, "O", &py_key))
296                 return NULL;
297
298         key = PyString_AsTDB_DATA(py_key);
299         if (!key.dptr)
300                 return NULL;
301         ret = tdb_delete(self->ctx, key);
302         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
303         Py_RETURN_NONE;
304 }
305
306 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
307 {
308         TDB_DATA key;
309         int ret;
310         PyObject *py_key;
311         PyErr_TDB_RAISE_IF_CLOSED(self);
312
313         if (!PyArg_ParseTuple(args, "O", &py_key))
314                 return NULL;
315
316         key = PyString_AsTDB_DATA(py_key);
317         if (!key.dptr)
318                 return NULL;
319         ret = tdb_exists(self->ctx, key);
320         if (ret != TDB_ERR_NOEXIST) {
321                 PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
322         }
323
324         return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
325 }
326
327 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
328 {
329         TDB_DATA key, value;
330         int ret;
331         int flag = TDB_REPLACE;
332         PyObject *py_key, *py_value;
333
334         PyErr_TDB_RAISE_IF_CLOSED(self);
335
336         if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
337                 return NULL;
338
339         key = PyString_AsTDB_DATA(py_key);
340         if (!key.dptr)
341                 return NULL;
342         value = PyString_AsTDB_DATA(py_value);
343         if (!value.dptr)
344                 return NULL;
345
346         ret = tdb_store(self->ctx, key, value, flag);
347         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
348         Py_RETURN_NONE;
349 }
350
351 static PyObject *obj_add_flags(PyTdbObject *self, PyObject *args)
352 {
353         unsigned flags;
354
355         PyErr_TDB_RAISE_IF_CLOSED(self);
356
357         if (!PyArg_ParseTuple(args, "I", &flags))
358                 return NULL;
359
360         tdb_add_flags(self->ctx, flags);
361         Py_RETURN_NONE;
362 }
363
364 static PyObject *obj_remove_flags(PyTdbObject *self, PyObject *args)
365 {
366         unsigned flags;
367
368         PyErr_TDB_RAISE_IF_CLOSED(self);
369
370         if (!PyArg_ParseTuple(args, "I", &flags))
371                 return NULL;
372
373         tdb_remove_flags(self->ctx, flags);
374         Py_RETURN_NONE;
375 }
376
377 typedef struct {
378         PyObject_HEAD
379         TDB_DATA current;
380         PyTdbObject *iteratee;
381 } PyTdbIteratorObject;
382
383 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
384 {
385         TDB_DATA current;
386         PyObject *ret;
387         if (self->current.dptr == NULL && self->current.dsize == 0)
388                 return NULL;
389         current = self->current;
390         self->current = tdb_nextkey(self->iteratee->ctx, self->current);
391         ret = PyString_FromTDB_DATA(current);
392         return ret;
393 }
394
395 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
396 {
397         Py_DECREF(self->iteratee);
398         PyObject_Del(self);
399 }
400
401 PyTypeObject PyTdbIterator = {
402         .tp_name = "Iterator",
403         .tp_basicsize = sizeof(PyTdbIteratorObject),
404         .tp_iternext = (iternextfunc)tdb_iter_next,
405         .tp_dealloc = (destructor)tdb_iter_dealloc,
406         .tp_flags = Py_TPFLAGS_DEFAULT,
407         .tp_iter = PyObject_SelfIter,
408 };
409
410 static PyObject *tdb_object_iter(PyTdbObject *self)
411 {
412         PyTdbIteratorObject *ret;       
413
414         PyErr_TDB_RAISE_IF_CLOSED(self);
415
416         ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
417         if (!ret)
418                 return NULL;
419         ret->current = tdb_firstkey(self->ctx);
420         ret->iteratee = self;
421         Py_INCREF(self);
422         return (PyObject *)ret;
423 }
424
425 static PyObject *obj_clear(PyTdbObject *self)
426 {
427         int ret;
428         PyErr_TDB_RAISE_IF_CLOSED(self);
429         ret = tdb_wipe_all(self->ctx);
430         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
431         Py_RETURN_NONE;
432 }
433
434 static PyObject *obj_repack(PyTdbObject *self)
435 {
436         int ret;
437         PyErr_TDB_RAISE_IF_CLOSED(self);
438         ret = tdb_repack(self->ctx);
439         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
440         Py_RETURN_NONE;
441 }
442
443 static PyObject *obj_enable_seqnum(PyTdbObject *self)
444 {
445         PyErr_TDB_RAISE_IF_CLOSED(self);
446         tdb_enable_seqnum(self->ctx);
447         Py_RETURN_NONE;
448 }
449
450 static PyObject *obj_increment_seqnum_nonblock(PyTdbObject *self)
451 {
452         PyErr_TDB_RAISE_IF_CLOSED(self);
453         tdb_increment_seqnum_nonblock(self->ctx);
454         Py_RETURN_NONE;
455 }
456
457 static PyMethodDef tdb_object_methods[] = {
458         { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS, 
459                 "S.transaction_cancel() -> None\n"
460                 "Cancel the currently active transaction." },
461         { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
462                 "S.transaction_commit() -> None\n"
463                 "Commit the currently active transaction." },
464         { "transaction_prepare_commit", (PyCFunction)obj_transaction_prepare_commit, METH_NOARGS,
465                 "S.transaction_prepare_commit() -> None\n"
466                 "Prepare to commit the currently active transaction" },
467         { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
468                 "S.transaction_start() -> None\n"
469                 "Start a new transaction." },
470         { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
471         { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
472         { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
473         { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
474         { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
475         { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
476         { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
477                 "Fetch a value." },
478         { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
479                 "Append data to an existing key." },
480         { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
481                 "Return the first key in this database." },
482         { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
483                 "Return the next key in this database." },
484         { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
485                 "Delete an entry." },
486         { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
487                 "Check whether key exists in this database." },
488         { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
489                 "Store data." },
490         { "add_flags", (PyCFunction)obj_add_flags, METH_VARARGS, "S.add_flags(flags) -> None" },
491         { "remove_flags", (PyCFunction)obj_remove_flags, METH_VARARGS, "S.remove_flags(flags) -> None" },
492         { "iterkeys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
493         { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
494                 "Wipe the entire database." },
495         { "repack", (PyCFunction)obj_repack, METH_NOARGS, "S.repack() -> None\n"
496                 "Repack the entire database." },
497         { "enable_seqnum", (PyCFunction)obj_enable_seqnum, METH_NOARGS,
498                 "S.enable_seqnum() -> None" },
499         { "increment_seqnum_nonblock", (PyCFunction)obj_increment_seqnum_nonblock, METH_NOARGS,
500                 "S.increment_seqnum_nonblock() -> None" },
501         { NULL }
502 };
503
504 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
505 {
506         PyErr_TDB_RAISE_IF_CLOSED(self);
507         return PyInt_FromLong(tdb_hash_size(self->ctx));
508 }
509
510 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
511 {
512         PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self);
513         if (!PyInt_Check(max_dead))
514                 return -1;
515         tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
516         return 0;
517 }
518
519 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
520 {
521         PyErr_TDB_RAISE_IF_CLOSED(self);
522         return PyInt_FromLong(tdb_map_size(self->ctx));
523 }
524
525 static PyObject *obj_get_freelist_size(PyTdbObject *self, void *closure)
526 {
527         PyErr_TDB_RAISE_IF_CLOSED(self);
528         return PyInt_FromLong(tdb_freelist_size(self->ctx));
529 }
530
531 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
532 {
533         PyErr_TDB_RAISE_IF_CLOSED(self);
534         return PyInt_FromLong(tdb_get_flags(self->ctx));
535 }
536
537 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
538 {
539         PyErr_TDB_RAISE_IF_CLOSED(self);
540         return PyString_FromString(tdb_name(self->ctx));
541 }
542
543 static PyObject *obj_get_seqnum(PyTdbObject *self, void *closure)
544 {
545         PyErr_TDB_RAISE_IF_CLOSED(self);
546         return PyInt_FromLong(tdb_get_seqnum(self->ctx));
547 }
548
549
550 static PyGetSetDef tdb_object_getsetters[] = {
551         { (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
552         { (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
553         { (char *)"freelist_size", (getter)obj_get_freelist_size, NULL, NULL },
554         { (char *)"flags", (getter)obj_get_flags, NULL, NULL },
555         { (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
556         { (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
557         { (char *)"seqnum", (getter)obj_get_seqnum, NULL, NULL },
558         { NULL }
559 };
560
561 static PyObject *tdb_object_repr(PyTdbObject *self)
562 {
563         PyErr_TDB_RAISE_IF_CLOSED(self);
564         if (tdb_get_flags(self->ctx) & TDB_INTERNAL) {
565                 return PyString_FromString("Tdb(<internal>)");
566         } else {
567                 return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
568         }
569 }
570
571 static void tdb_object_dealloc(PyTdbObject *self)
572 {
573         if (!self->closed)
574                 tdb_close(self->ctx);
575         self->ob_type->tp_free(self);
576 }
577
578 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
579 {
580         TDB_DATA tkey, val;
581         PyErr_TDB_RAISE_IF_CLOSED(self);
582         if (!PyString_Check(key)) {
583                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
584                 return NULL;
585         }
586
587         tkey.dptr = (unsigned char *)PyString_AsString(key);
588         tkey.dsize = PyString_Size(key);
589
590         val = tdb_fetch(self->ctx, tkey);
591         if (val.dptr == NULL) {
592                 PyErr_SetString(PyExc_KeyError, "No such TDB entry");
593                 return NULL;
594         } else {
595                 return PyString_FromTDB_DATA(val);
596         }
597 }
598
599 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
600 {
601         TDB_DATA tkey, tval;
602         int ret;
603         PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self);
604         if (!PyString_Check(key)) {
605                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
606                 return -1;
607         }
608
609         tkey = PyString_AsTDB_DATA(key);
610
611         if (value == NULL) { 
612                 ret = tdb_delete(self->ctx, tkey);
613         } else { 
614                 if (!PyString_Check(value)) {
615                         PyErr_SetString(PyExc_TypeError, "Expected string as value");
616                         return -1;
617                 }
618
619                 tval = PyString_AsTDB_DATA(value);
620
621                 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
622         }
623
624         if (ret != 0) {
625                 PyErr_SetTDBError(self->ctx);
626                 return -1;
627         } 
628
629         return ret;
630 }
631
632 static PyMappingMethods tdb_object_mapping = {
633         .mp_subscript = (binaryfunc)obj_getitem,
634         .mp_ass_subscript = (objobjargproc)obj_setitem,
635 };
636 static PyTypeObject PyTdb = {
637         .tp_name = "tdb.Tdb",
638         .tp_basicsize = sizeof(PyTdbObject),
639         .tp_methods = tdb_object_methods,
640         .tp_getset = tdb_object_getsetters,
641         .tp_new = py_tdb_open,
642         .tp_doc = "A TDB file",
643         .tp_repr = (reprfunc)tdb_object_repr,
644         .tp_dealloc = (destructor)tdb_object_dealloc,
645         .tp_as_mapping = &tdb_object_mapping,
646         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
647         .tp_iter = (getiterfunc)tdb_object_iter,
648 };
649
650 static PyMethodDef tdb_methods[] = {
651         { "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
652                 "Open a TDB file." },
653         { NULL }
654 };
655
656 void inittdb(void);
657 void inittdb(void)
658 {
659         PyObject *m;
660
661         if (PyType_Ready(&PyTdb) < 0)
662                 return;
663
664         if (PyType_Ready(&PyTdbIterator) < 0)
665                 return;
666
667         m = Py_InitModule3("tdb", tdb_methods,
668                 "simple key-value database that supports multiple writers.");
669         if (m == NULL)
670                 return;
671
672         PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
673         PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
674         PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
675
676         PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
677         PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
678         PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
679         PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
680         PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
681         PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
682         PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
683         PyModule_AddObject(m, "NOSYNC", PyInt_FromLong(TDB_NOSYNC));
684         PyModule_AddObject(m, "SEQNUM", PyInt_FromLong(TDB_SEQNUM));
685         PyModule_AddObject(m, "VOLATILE", PyInt_FromLong(TDB_VOLATILE));
686         PyModule_AddObject(m, "ALLOW_NESTING", PyInt_FromLong(TDB_ALLOW_NESTING));
687         PyModule_AddObject(m, "DISALLOW_NESTING", PyInt_FromLong(TDB_DISALLOW_NESTING));
688         PyModule_AddObject(m, "INCOMPATIBLE_HASH", PyInt_FromLong(TDB_INCOMPATIBLE_HASH));
689
690         PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
691
692         PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
693
694         Py_INCREF(&PyTdb);
695         PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
696
697         Py_INCREF(&PyTdbIterator);
698 }