python:tests: Store keys as bytes rather than as lists of ints
[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 "lib/replace/system/python.h"
28 #include "replace.h"
29 #include "system/filesys.h"
30
31 /* Include tdb headers */
32 #include <tdb.h>
33
34 /* discard signature of 'func' in favour of 'target_sig' */
35 #define PY_DISCARD_FUNC_SIG(target_sig, func) (target_sig)(void(*)(void))func
36
37 typedef struct {
38         PyObject_HEAD
39         TDB_CONTEXT *ctx;
40         bool closed;
41 } PyTdbObject;
42
43 static PyTypeObject PyTdb;
44
45 static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
46 {
47         PyErr_SetObject(PyExc_RuntimeError, 
48                 Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
49 }
50
51 static TDB_DATA PyBytes_AsTDB_DATA(PyObject *data)
52 {
53         TDB_DATA ret;
54         ret.dptr = (unsigned char *)PyBytes_AsString(data);
55         ret.dsize = PyBytes_Size(data);
56         return ret;
57 }
58
59 static PyObject *PyBytes_FromTDB_DATA(TDB_DATA data)
60 {
61         if (data.dptr == NULL && data.dsize == 0) {
62                 Py_RETURN_NONE;
63         } else {
64                 PyObject *ret = PyBytes_FromStringAndSize((const char *)data.dptr,
65                                                                                                   data.dsize);
66                 free(data.dptr);
67                 return ret;
68     }
69 }
70
71 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
72         if (ret != 0) { \
73                 PyErr_SetTDBError(tdb); \
74                 return NULL; \
75         }
76
77 #define PyErr_TDB_RAISE_IF_CLOSED(self) \
78         if (self->closed) {                                             \
79                 PyErr_SetObject(PyExc_RuntimeError,                             \
80                                 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
81                 return NULL;                                            \
82         }
83
84 #define PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self) \
85         if (self->closed) {                                             \
86                 PyErr_SetObject(PyExc_RuntimeError,                             \
87                                 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
88                 return -1;                                              \
89         }
90
91 static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
92 {
93         char *name = NULL;
94         int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
95         TDB_CONTEXT *ctx;
96         PyTdbObject *ret;
97         const char *_kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
98         char **kwnames = discard_const_p(char *, _kwnames);
99
100         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siiii", kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
101                 return NULL;
102
103         if (name == NULL) {
104                 tdb_flags |= TDB_INTERNAL;
105         }
106
107         ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
108         if (ctx == NULL) {
109                 PyErr_SetFromErrno(PyExc_IOError);
110                 return NULL;
111         }
112
113         ret = PyObject_New(PyTdbObject, &PyTdb);
114         if (!ret) {
115                 tdb_close(ctx);
116                 return NULL;
117         }
118
119         ret->ctx = ctx;
120         ret->closed = false;
121         return (PyObject *)ret;
122 }
123
124 static PyObject *obj_transaction_cancel(PyTdbObject *self,
125                 PyObject *Py_UNUSED(ignored))
126 {
127         int ret;
128
129         PyErr_TDB_RAISE_IF_CLOSED(self);
130
131         ret = tdb_transaction_cancel(self->ctx);
132         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
133         Py_RETURN_NONE;
134 }
135
136 static PyObject *obj_transaction_commit(PyTdbObject *self,
137                 PyObject *Py_UNUSED(ignored))
138 {
139         int ret;
140         PyErr_TDB_RAISE_IF_CLOSED(self);
141         ret = tdb_transaction_commit(self->ctx);
142         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
143         Py_RETURN_NONE;
144 }
145
146 static PyObject *obj_transaction_prepare_commit(PyTdbObject *self,
147                 PyObject *Py_UNUSED(ignored))
148 {
149         int ret;
150         PyErr_TDB_RAISE_IF_CLOSED(self);
151         ret = tdb_transaction_prepare_commit(self->ctx);
152         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
153         Py_RETURN_NONE;
154 }
155
156 static PyObject *obj_transaction_start(PyTdbObject *self,
157                 PyObject *Py_UNUSED(ignored))
158 {
159         int ret;
160         PyErr_TDB_RAISE_IF_CLOSED(self);
161         ret = tdb_transaction_start(self->ctx);
162         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
163         Py_RETURN_NONE;
164 }
165
166 static PyObject *obj_reopen(PyTdbObject *self,
167                 PyObject *Py_UNUSED(ignored))
168 {
169         int ret;
170         PyErr_TDB_RAISE_IF_CLOSED(self);
171         ret = tdb_reopen(self->ctx);
172         if (ret != 0) {
173                 self->closed = true;
174                 PyErr_SetObject(PyExc_RuntimeError,
175                                 Py_BuildValue("(i,s)",
176                                               TDB_ERR_IO,
177                                               "Failed to reopen database"));
178                 return NULL;
179         }
180         Py_RETURN_NONE;
181 }
182
183 static PyObject *obj_lockall(PyTdbObject *self,
184                 PyObject *Py_UNUSED(ignored))
185 {
186         int ret;
187         PyErr_TDB_RAISE_IF_CLOSED(self);
188         ret = tdb_lockall(self->ctx);
189         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
190         Py_RETURN_NONE;
191 }
192
193 static PyObject *obj_unlockall(PyTdbObject *self,
194                 PyObject *Py_UNUSED(ignored))
195 {
196         int ret;
197         PyErr_TDB_RAISE_IF_CLOSED(self);
198         ret = tdb_unlockall(self->ctx);
199         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
200         Py_RETURN_NONE;
201 }
202
203 static PyObject *obj_lockall_read(PyTdbObject *self,
204                 PyObject *Py_UNUSED(ignored))
205 {
206         int ret;
207         PyErr_TDB_RAISE_IF_CLOSED(self);
208         ret = tdb_lockall_read(self->ctx);
209         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
210         Py_RETURN_NONE;
211 }
212
213 static PyObject *obj_unlockall_read(PyTdbObject *self,
214                 PyObject *Py_UNUSED(ignored))
215 {
216         int ret = tdb_unlockall_read(self->ctx);
217         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
218         Py_RETURN_NONE;
219 }
220
221 static PyObject *obj_close(PyTdbObject *self, PyObject *Py_UNUSED(ignored))
222 {
223         int ret;
224         if (self->closed)
225                 Py_RETURN_NONE;
226         ret = tdb_close(self->ctx);
227         self->closed = true;
228         if (ret != 0) {
229                 PyErr_SetObject(PyExc_RuntimeError,
230                                 Py_BuildValue("(i,s)",
231                                               TDB_ERR_IO,
232                                               "Failed to close database"));
233                 return NULL;
234         }
235         Py_RETURN_NONE;
236 }
237
238 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
239 {
240         TDB_DATA key;
241         PyObject *py_key;
242
243         PyErr_TDB_RAISE_IF_CLOSED(self);
244
245         if (!PyArg_ParseTuple(args, "O", &py_key))
246                 return NULL;
247
248         key = PyBytes_AsTDB_DATA(py_key);
249         if (!key.dptr)
250                 return NULL;
251
252         return PyBytes_FromTDB_DATA(tdb_fetch(self->ctx, key));
253 }
254
255 static PyObject *obj_append(PyTdbObject *self, PyObject *args)
256 {
257         TDB_DATA key, data;
258         PyObject *py_key, *py_data;
259         int ret;
260
261         PyErr_TDB_RAISE_IF_CLOSED(self);
262
263         if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
264                 return NULL;
265
266         key = PyBytes_AsTDB_DATA(py_key);
267         if (!key.dptr)
268                 return NULL;
269         data = PyBytes_AsTDB_DATA(py_data);
270         if (!data.dptr)
271                 return NULL;
272
273         ret = tdb_append(self->ctx, key, data);
274         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
275         Py_RETURN_NONE;
276 }
277
278 static PyObject *obj_firstkey(PyTdbObject *self, PyObject *Py_UNUSED(ignored))
279 {
280         PyErr_TDB_RAISE_IF_CLOSED(self);
281
282         return PyBytes_FromTDB_DATA(tdb_firstkey(self->ctx));
283 }
284
285 static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
286 {
287         TDB_DATA key;
288         PyObject *py_key;
289         PyErr_TDB_RAISE_IF_CLOSED(self);
290
291         if (!PyArg_ParseTuple(args, "O", &py_key))
292                 return NULL;
293
294         key = PyBytes_AsTDB_DATA(py_key);
295         if (!key.dptr)
296                 return NULL;
297         
298         return PyBytes_FromTDB_DATA(tdb_nextkey(self->ctx, key));
299 }
300
301 static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
302 {
303         TDB_DATA key;
304         PyObject *py_key;
305         int ret;
306         PyErr_TDB_RAISE_IF_CLOSED(self);
307
308         if (!PyArg_ParseTuple(args, "O", &py_key))
309                 return NULL;
310
311         key = PyBytes_AsTDB_DATA(py_key);
312         if (!key.dptr)
313                 return NULL;
314         ret = tdb_delete(self->ctx, key);
315         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
316         Py_RETURN_NONE;
317 }
318
319 static int obj_contains(PyTdbObject *self, PyObject *py_key)
320 {
321         TDB_DATA key;
322         int ret;
323         PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self);
324
325         key = PyBytes_AsTDB_DATA(py_key);
326         if (!key.dptr) {
327                 PyErr_BadArgument();
328                 return -1;
329         }
330         ret = tdb_exists(self->ctx, key);
331         if (ret)
332                 return 1;
333         return 0;
334 }
335
336 static PyObject *obj_store(PyTdbObject *self, PyObject *args)
337 {
338         TDB_DATA key, value;
339         int ret;
340         int flag = TDB_REPLACE;
341         PyObject *py_key, *py_value;
342
343         PyErr_TDB_RAISE_IF_CLOSED(self);
344
345         if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
346                 return NULL;
347
348         key = PyBytes_AsTDB_DATA(py_key);
349         if (!key.dptr)
350                 return NULL;
351         value = PyBytes_AsTDB_DATA(py_value);
352         if (!value.dptr)
353                 return NULL;
354
355         ret = tdb_store(self->ctx, key, value, flag);
356         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
357         Py_RETURN_NONE;
358 }
359
360 static PyObject *obj_storev(PyTdbObject *self, PyObject *args)
361 {
362         TDB_DATA key, *values, value;
363         int ret;
364         int flag = TDB_REPLACE;
365         Py_ssize_t num_values, i;
366         PyObject *py_key, *py_values, *py_value;
367
368         PyErr_TDB_RAISE_IF_CLOSED(self);
369
370         if (!PyArg_ParseTuple(
371                     args, "OO!|i", &py_key, &PyList_Type, &py_values, &flag)) {
372                 return NULL;
373         }
374
375         num_values = PyList_Size(py_values);
376
377         key = PyBytes_AsTDB_DATA(py_key);
378         if (key.dptr == NULL) {
379                 return NULL;
380         }
381
382         if (SSIZE_MAX/sizeof(TDB_DATA) < num_values) {
383                 PyErr_SetFromErrno(PyExc_OverflowError);
384                 return NULL;
385         }
386         values = malloc(sizeof(TDB_DATA) * num_values);
387         if (values == NULL) {
388                 PyErr_NoMemory();
389                 return NULL;
390         }
391         for (i=0; i<num_values; i++) {
392                 py_value = PyList_GetItem(py_values, i);
393                 value = PyBytes_AsTDB_DATA(py_value);
394                 if (!value.dptr) {
395                         free(values);
396                         return NULL;
397                 }
398                 values[i] = value;
399         }
400
401         ret = tdb_storev(self->ctx, key, values, num_values, flag);
402         free(values);
403         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
404         Py_RETURN_NONE;
405 }
406
407 static PyObject *obj_add_flags(PyTdbObject *self, PyObject *args)
408 {
409         unsigned flags;
410
411         PyErr_TDB_RAISE_IF_CLOSED(self);
412
413         if (!PyArg_ParseTuple(args, "I", &flags))
414                 return NULL;
415
416         tdb_add_flags(self->ctx, flags);
417         Py_RETURN_NONE;
418 }
419
420 static PyObject *obj_remove_flags(PyTdbObject *self, PyObject *args)
421 {
422         unsigned flags;
423
424         PyErr_TDB_RAISE_IF_CLOSED(self);
425
426         if (!PyArg_ParseTuple(args, "I", &flags))
427                 return NULL;
428
429         tdb_remove_flags(self->ctx, flags);
430         Py_RETURN_NONE;
431 }
432
433 typedef struct {
434         PyObject_HEAD
435         TDB_DATA current;
436         PyTdbObject *iteratee;
437 } PyTdbIteratorObject;
438
439 static PyObject *tdb_iter_next(PyTdbIteratorObject *self)
440 {
441         TDB_DATA current;
442         PyObject *ret;
443         if (self->current.dptr == NULL && self->current.dsize == 0)
444                 return NULL;
445         current = self->current;
446         self->current = tdb_nextkey(self->iteratee->ctx, self->current);
447         ret = PyBytes_FromTDB_DATA(current);
448         return ret;
449 }
450
451 static void tdb_iter_dealloc(PyTdbIteratorObject *self)
452 {
453         Py_CLEAR(self->iteratee);
454         PyObject_Del(self);
455 }
456
457 PyTypeObject PyTdbIterator = {
458         .tp_name = "Iterator",
459         .tp_basicsize = sizeof(PyTdbIteratorObject),
460         .tp_iternext = (iternextfunc)tdb_iter_next,
461         .tp_dealloc = (destructor)tdb_iter_dealloc,
462         .tp_flags = Py_TPFLAGS_DEFAULT,
463         .tp_iter = PyObject_SelfIter,
464 };
465
466 static PyObject *tdb_object_iter(PyTdbObject *self,
467                 PyObject *Py_UNUSED(ignored))
468 {
469         PyTdbIteratorObject *ret;       
470
471         PyErr_TDB_RAISE_IF_CLOSED(self);
472
473         ret = PyObject_New(PyTdbIteratorObject, &PyTdbIterator);
474         if (!ret)
475                 return NULL;
476         ret->current = tdb_firstkey(self->ctx);
477         ret->iteratee = self;
478         Py_INCREF(self);
479         return (PyObject *)ret;
480 }
481
482 static PyObject *obj_clear(PyTdbObject *self, PyObject *Py_UNUSED(ignored))
483 {
484         int ret;
485         PyErr_TDB_RAISE_IF_CLOSED(self);
486         ret = tdb_wipe_all(self->ctx);
487         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
488         Py_RETURN_NONE;
489 }
490
491 static PyObject *obj_repack(PyTdbObject *self, PyObject *Py_UNUSED(ignored))
492 {
493         int ret;
494         PyErr_TDB_RAISE_IF_CLOSED(self);
495         ret = tdb_repack(self->ctx);
496         PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
497         Py_RETURN_NONE;
498 }
499
500 static PyObject *obj_enable_seqnum(PyTdbObject *self,
501                 PyObject *Py_UNUSED(ignored))
502 {
503         PyErr_TDB_RAISE_IF_CLOSED(self);
504         tdb_enable_seqnum(self->ctx);
505         Py_RETURN_NONE;
506 }
507
508 static PyObject *obj_increment_seqnum_nonblock(PyTdbObject *self,
509                 PyObject *Py_UNUSED(ignored))
510 {
511         PyErr_TDB_RAISE_IF_CLOSED(self);
512         tdb_increment_seqnum_nonblock(self->ctx);
513         Py_RETURN_NONE;
514 }
515
516 static PyMethodDef tdb_object_methods[] = {
517         { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS, 
518                 "S.transaction_cancel() -> None\n"
519                 "Cancel the currently active transaction." },
520         { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
521                 "S.transaction_commit() -> None\n"
522                 "Commit the currently active transaction." },
523         { "transaction_prepare_commit", (PyCFunction)obj_transaction_prepare_commit, METH_NOARGS,
524                 "S.transaction_prepare_commit() -> None\n"
525                 "Prepare to commit the currently active transaction" },
526         { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
527                 "S.transaction_start() -> None\n"
528                 "Start a new transaction." },
529         { "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
530         { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
531         { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
532         { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
533         { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
534         { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
535         { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
536                 "Fetch a value." },
537         { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
538                 "Append data to an existing key." },
539         { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
540                 "Return the first key in this database." },
541         { "nextkey", (PyCFunction)obj_nextkey, METH_VARARGS, "S.nextkey(key) -> data\n"
542                 "Return the next key in this database." },
543         { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
544                 "Delete an entry." },
545         { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
546                 "Store data." },
547         { "storev", (PyCFunction)obj_storev, METH_VARARGS, "S.storev(key, data, flag=REPLACE) -> None"
548                 "Store several data." },
549         { "add_flags", (PyCFunction)obj_add_flags, METH_VARARGS, "S.add_flags(flags) -> None" },
550         { "remove_flags", (PyCFunction)obj_remove_flags, METH_VARARGS, "S.remove_flags(flags) -> None" },
551         { "keys", (PyCFunction)tdb_object_iter, METH_NOARGS, "S.keys() -> iterator" },
552         { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
553                 "Wipe the entire database." },
554         { "repack", (PyCFunction)obj_repack, METH_NOARGS, "S.repack() -> None\n"
555                 "Repack the entire database." },
556         { "enable_seqnum", (PyCFunction)obj_enable_seqnum, METH_NOARGS,
557                 "S.enable_seqnum() -> None" },
558         { "increment_seqnum_nonblock", (PyCFunction)obj_increment_seqnum_nonblock, METH_NOARGS,
559                 "S.increment_seqnum_nonblock() -> None" },
560         {0}
561 };
562
563 static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
564 {
565         PyErr_TDB_RAISE_IF_CLOSED(self);
566         return PyLong_FromLong(tdb_hash_size(self->ctx));
567 }
568
569 static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
570 {
571         PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self);
572         if (!PyLong_Check(max_dead))
573                 return -1;
574         tdb_set_max_dead(self->ctx, PyLong_AsLong(max_dead));
575         return 0;
576 }
577
578 static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
579 {
580         PyErr_TDB_RAISE_IF_CLOSED(self);
581         return PyLong_FromLong(tdb_map_size(self->ctx));
582 }
583
584 static PyObject *obj_get_freelist_size(PyTdbObject *self, void *closure)
585 {
586         PyErr_TDB_RAISE_IF_CLOSED(self);
587         return PyLong_FromLong(tdb_freelist_size(self->ctx));
588 }
589
590 static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
591 {
592         PyErr_TDB_RAISE_IF_CLOSED(self);
593         return PyLong_FromLong(tdb_get_flags(self->ctx));
594 }
595
596 static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
597 {
598         PyErr_TDB_RAISE_IF_CLOSED(self);
599         return PyBytes_FromString(tdb_name(self->ctx));
600 }
601
602 static PyObject *obj_get_seqnum(PyTdbObject *self, void *closure)
603 {
604         PyErr_TDB_RAISE_IF_CLOSED(self);
605         return PyLong_FromLong(tdb_get_seqnum(self->ctx));
606 }
607
608 static PyObject *obj_get_text(PyTdbObject *self, void *closure)
609 {
610         PyObject *mod, *cls, *inst;
611         mod = PyImport_ImportModule("_tdb_text");
612         if (mod == NULL)
613                 return NULL;
614         cls = PyObject_GetAttrString(mod, "TdbTextWrapper");
615         if (cls == NULL) {
616                 Py_DECREF(mod);
617                 return NULL;
618         }
619         inst = PyObject_CallFunction(cls, discard_const_p(char, "O"), self);
620         Py_DECREF(mod);
621         Py_DECREF(cls);
622         return inst;
623 }
624
625 static PyGetSetDef tdb_object_getsetters[] = {
626         {
627                 .name    = discard_const_p(char, "hash_size"),
628                 .get     = (getter)obj_get_hash_size,
629         },
630         {
631                 .name    = discard_const_p(char, "map_size"),
632                 .get     = (getter)obj_get_map_size,
633         },
634         {
635                 .name    = discard_const_p(char, "freelist_size"),
636                 .get     = (getter)obj_get_freelist_size,
637         },
638         {
639                 .name    = discard_const_p(char, "flags"),
640                 .get     = (getter)obj_get_flags,
641         },
642         {
643                 .name    = discard_const_p(char, "max_dead"),
644                 .set     = (setter)obj_set_max_dead,
645         },
646         {
647                 .name    = discard_const_p(char, "filename"),
648                 .get     = (getter)obj_get_filename,
649                 .doc     = discard_const_p(char, "The filename of this TDB file."),
650         },
651         {
652                 .name    = discard_const_p(char, "seqnum"),
653                 .get     = (getter)obj_get_seqnum,
654         },
655         {
656                 .name    = discard_const_p(char, "text"),
657                 .get     = (getter)obj_get_text,
658         },
659         { .name = NULL }
660 };
661
662 static PyObject *tdb_object_repr(PyTdbObject *self)
663 {
664         PyErr_TDB_RAISE_IF_CLOSED(self);
665         if (tdb_get_flags(self->ctx) & TDB_INTERNAL) {
666                 return PyUnicode_FromString("Tdb(<internal>)");
667         } else {
668                 return PyUnicode_FromFormat("Tdb('%s')", tdb_name(self->ctx));
669         }
670 }
671
672 static void tdb_object_dealloc(PyTdbObject *self)
673 {
674         if (!self->closed)
675                 tdb_close(self->ctx);
676         Py_TYPE(self)->tp_free(self);
677 }
678
679 static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
680 {
681         TDB_DATA tkey, val;
682         PyErr_TDB_RAISE_IF_CLOSED(self);
683         if (!PyBytes_Check(key)) {
684                 PyErr_SetString(PyExc_TypeError, "Expected bytestring as key");
685                 return NULL;
686         }
687
688         tkey.dptr = (unsigned char *)PyBytes_AsString(key);
689         tkey.dsize = PyBytes_Size(key);
690
691         val = tdb_fetch(self->ctx, tkey);
692         if (val.dptr == NULL) {
693                 /*
694                  * if the key doesn't exist raise KeyError(key) to be
695                  * consistent with python dict
696                  */
697                 PyErr_SetObject(PyExc_KeyError, key);
698                 return NULL;
699         } else {
700                 return PyBytes_FromTDB_DATA(val);
701         }
702 }
703
704 static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
705 {
706         TDB_DATA tkey, tval;
707         int ret;
708         PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self);
709         if (!PyBytes_Check(key)) {
710                 PyErr_SetString(PyExc_TypeError, "Expected bytestring as key");
711                 return -1;
712         }
713
714         tkey = PyBytes_AsTDB_DATA(key);
715
716         if (value == NULL) { 
717                 ret = tdb_delete(self->ctx, tkey);
718         } else { 
719                 if (!PyBytes_Check(value)) {
720                         PyErr_SetString(PyExc_TypeError, "Expected string as value");
721                         return -1;
722                 }
723
724                 tval = PyBytes_AsTDB_DATA(value);
725
726                 ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
727         }
728
729         if (ret != 0) {
730                 PyErr_SetTDBError(self->ctx);
731                 return -1;
732         } 
733
734         return ret;
735 }
736
737 static PyMappingMethods tdb_object_mapping = {
738         .mp_subscript = (binaryfunc)obj_getitem,
739         .mp_ass_subscript = (objobjargproc)obj_setitem,
740 };
741 static PySequenceMethods tdb_object_seq = {
742         .sq_contains = (objobjproc)obj_contains,
743 };
744 static PyTypeObject PyTdb = {
745         .tp_name = "tdb.Tdb",
746         .tp_basicsize = sizeof(PyTdbObject),
747         .tp_methods = tdb_object_methods,
748         .tp_getset = tdb_object_getsetters,
749         .tp_new = py_tdb_open,
750         .tp_doc = "A TDB file",
751         .tp_repr = (reprfunc)tdb_object_repr,
752         .tp_dealloc = (destructor)tdb_object_dealloc,
753         .tp_as_mapping = &tdb_object_mapping,
754         .tp_as_sequence = &tdb_object_seq,
755         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
756         .tp_iter = PY_DISCARD_FUNC_SIG(getiterfunc,tdb_object_iter),
757 };
758
759 static PyMethodDef tdb_methods[] = {
760         {
761                 .ml_name  = "open",
762                 .ml_meth  = PY_DISCARD_FUNC_SIG(PyCFunction, py_tdb_open),
763                 .ml_flags = METH_VARARGS|METH_KEYWORDS,
764                 .ml_doc   = "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, "
765                             "flags=O_RDWR, mode=0600)\nOpen a TDB file."
766         },
767         { .ml_name = NULL }
768 };
769
770 #define MODULE_DOC "simple key-value database that supports multiple writers."
771
772 static struct PyModuleDef moduledef = {
773     PyModuleDef_HEAD_INIT,
774     .m_name = "tdb",
775     .m_doc = MODULE_DOC,
776     .m_size = -1,
777     .m_methods = tdb_methods,
778 };
779
780 PyObject* module_init(void);
781 PyObject* module_init(void)
782 {
783         PyObject *m;
784
785         if (PyType_Ready(&PyTdb) < 0)
786                 return NULL;
787
788         if (PyType_Ready(&PyTdbIterator) < 0)
789                 return NULL;
790
791         m = PyModule_Create(&moduledef);
792         if (m == NULL)
793                 return NULL;
794
795         PyModule_AddIntConstant(m, "REPLACE", TDB_REPLACE);
796         PyModule_AddIntConstant(m, "INSERT", TDB_INSERT);
797         PyModule_AddIntConstant(m, "MODIFY", TDB_MODIFY);
798
799         PyModule_AddIntConstant(m, "DEFAULT", TDB_DEFAULT);
800         PyModule_AddIntConstant(m, "CLEAR_IF_FIRST", TDB_CLEAR_IF_FIRST);
801         PyModule_AddIntConstant(m, "INTERNAL", TDB_INTERNAL);
802         PyModule_AddIntConstant(m, "NOLOCK", TDB_NOLOCK);
803         PyModule_AddIntConstant(m, "NOMMAP", TDB_NOMMAP);
804         PyModule_AddIntConstant(m, "CONVERT", TDB_CONVERT);
805         PyModule_AddIntConstant(m, "BIGENDIAN", TDB_BIGENDIAN);
806         PyModule_AddIntConstant(m, "NOSYNC", TDB_NOSYNC);
807         PyModule_AddIntConstant(m, "SEQNUM", TDB_SEQNUM);
808         PyModule_AddIntConstant(m, "VOLATILE", TDB_VOLATILE);
809         PyModule_AddIntConstant(m, "ALLOW_NESTING", TDB_ALLOW_NESTING);
810         PyModule_AddIntConstant(m, "DISALLOW_NESTING", TDB_DISALLOW_NESTING);
811         PyModule_AddIntConstant(m, "INCOMPATIBLE_HASH", TDB_INCOMPATIBLE_HASH);
812
813         PyModule_AddStringConstant(m, "__docformat__", "restructuredText");
814
815         PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
816
817         Py_INCREF(&PyTdb);
818         PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
819
820         Py_INCREF(&PyTdbIterator);
821
822     return m;
823 }
824
825
826 PyMODINIT_FUNC PyInit_tdb(void);
827 PyMODINIT_FUNC PyInit_tdb(void)
828 {
829     return module_init();
830 }