2 Unix SMB/CIFS implementation.
4 Python interface to tdb.
6 Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
9 ** NOTE! The following LGPL license applies to the tdb
10 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
27 #include "lib/replace/system/python.h"
29 #include "system/filesys.h"
31 /* Include tdb headers */
34 /* discard signature of 'func' in favour of 'target_sig' */
35 #define PY_DISCARD_FUNC_SIG(target_sig, func) (target_sig)(void(*)(void))func
43 static PyTypeObject PyTdb
;
45 static void PyErr_SetTDBError(TDB_CONTEXT
*tdb
)
47 PyErr_SetObject(PyExc_RuntimeError
,
48 Py_BuildValue("(i,s)", tdb_error(tdb
), tdb_errorstr(tdb
)));
51 static TDB_DATA
PyBytes_AsTDB_DATA(PyObject
*data
)
54 ret
.dptr
= (unsigned char *)PyBytes_AsString(data
);
55 ret
.dsize
= PyBytes_Size(data
);
59 static PyObject
*PyBytes_FromTDB_DATA(TDB_DATA data
)
61 if (data
.dptr
== NULL
&& data
.dsize
== 0) {
64 PyObject
*ret
= PyBytes_FromStringAndSize((const char *)data
.dptr
,
71 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
73 PyErr_SetTDBError(tdb); \
77 #define PyErr_TDB_RAISE_IF_CLOSED(self) \
79 PyErr_SetObject(PyExc_RuntimeError, \
80 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
84 #define PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self) \
86 PyErr_SetObject(PyExc_RuntimeError, \
87 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
91 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
94 int hash_size
= 0, tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
97 const char *_kwnames
[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL
};
98 char **kwnames
= discard_const_p(char *, _kwnames
);
100 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|siiii", kwnames
, &name
, &hash_size
, &tdb_flags
, &flags
, &mode
))
104 tdb_flags
|= TDB_INTERNAL
;
107 ctx
= tdb_open(name
, hash_size
, tdb_flags
, flags
, mode
);
109 PyErr_SetFromErrno(PyExc_IOError
);
113 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
121 return (PyObject
*)ret
;
124 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
,
125 PyObject
*Py_UNUSED(ignored
))
129 PyErr_TDB_RAISE_IF_CLOSED(self
);
131 ret
= tdb_transaction_cancel(self
->ctx
);
132 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
136 static PyObject
*obj_transaction_commit(PyTdbObject
*self
,
137 PyObject
*Py_UNUSED(ignored
))
140 PyErr_TDB_RAISE_IF_CLOSED(self
);
141 ret
= tdb_transaction_commit(self
->ctx
);
142 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
146 static PyObject
*obj_transaction_prepare_commit(PyTdbObject
*self
,
147 PyObject
*Py_UNUSED(ignored
))
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
);
156 static PyObject
*obj_transaction_start(PyTdbObject
*self
,
157 PyObject
*Py_UNUSED(ignored
))
160 PyErr_TDB_RAISE_IF_CLOSED(self
);
161 ret
= tdb_transaction_start(self
->ctx
);
162 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
166 static PyObject
*obj_reopen(PyTdbObject
*self
,
167 PyObject
*Py_UNUSED(ignored
))
170 PyErr_TDB_RAISE_IF_CLOSED(self
);
171 ret
= tdb_reopen(self
->ctx
);
174 PyErr_SetObject(PyExc_RuntimeError
,
175 Py_BuildValue("(i,s)",
177 "Failed to reopen database"));
183 static PyObject
*obj_lockall(PyTdbObject
*self
,
184 PyObject
*Py_UNUSED(ignored
))
187 PyErr_TDB_RAISE_IF_CLOSED(self
);
188 ret
= tdb_lockall(self
->ctx
);
189 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
193 static PyObject
*obj_unlockall(PyTdbObject
*self
,
194 PyObject
*Py_UNUSED(ignored
))
197 PyErr_TDB_RAISE_IF_CLOSED(self
);
198 ret
= tdb_unlockall(self
->ctx
);
199 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
203 static PyObject
*obj_lockall_read(PyTdbObject
*self
,
204 PyObject
*Py_UNUSED(ignored
))
207 PyErr_TDB_RAISE_IF_CLOSED(self
);
208 ret
= tdb_lockall_read(self
->ctx
);
209 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
213 static PyObject
*obj_unlockall_read(PyTdbObject
*self
,
214 PyObject
*Py_UNUSED(ignored
))
216 int ret
= tdb_unlockall_read(self
->ctx
);
217 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
221 static PyObject
*obj_close(PyTdbObject
*self
, PyObject
*Py_UNUSED(ignored
))
226 ret
= tdb_close(self
->ctx
);
229 PyErr_SetObject(PyExc_RuntimeError
,
230 Py_BuildValue("(i,s)",
232 "Failed to close database"));
238 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
243 PyErr_TDB_RAISE_IF_CLOSED(self
);
245 if (!PyArg_ParseTuple(args
, "O", &py_key
))
248 key
= PyBytes_AsTDB_DATA(py_key
);
252 return PyBytes_FromTDB_DATA(tdb_fetch(self
->ctx
, key
));
255 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
258 PyObject
*py_key
, *py_data
;
261 PyErr_TDB_RAISE_IF_CLOSED(self
);
263 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
266 key
= PyBytes_AsTDB_DATA(py_key
);
269 data
= PyBytes_AsTDB_DATA(py_data
);
273 ret
= tdb_append(self
->ctx
, key
, data
);
274 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
278 static PyObject
*obj_firstkey(PyTdbObject
*self
, PyObject
*Py_UNUSED(ignored
))
280 PyErr_TDB_RAISE_IF_CLOSED(self
);
282 return PyBytes_FromTDB_DATA(tdb_firstkey(self
->ctx
));
285 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
289 PyErr_TDB_RAISE_IF_CLOSED(self
);
291 if (!PyArg_ParseTuple(args
, "O", &py_key
))
294 key
= PyBytes_AsTDB_DATA(py_key
);
298 return PyBytes_FromTDB_DATA(tdb_nextkey(self
->ctx
, key
));
301 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
306 PyErr_TDB_RAISE_IF_CLOSED(self
);
308 if (!PyArg_ParseTuple(args
, "O", &py_key
))
311 key
= PyBytes_AsTDB_DATA(py_key
);
314 ret
= tdb_delete(self
->ctx
, key
);
315 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
319 static int obj_contains(PyTdbObject
*self
, PyObject
*py_key
)
323 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
325 key
= PyBytes_AsTDB_DATA(py_key
);
330 ret
= tdb_exists(self
->ctx
, key
);
336 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
340 int flag
= TDB_REPLACE
;
341 PyObject
*py_key
, *py_value
;
343 PyErr_TDB_RAISE_IF_CLOSED(self
);
345 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
348 key
= PyBytes_AsTDB_DATA(py_key
);
351 value
= PyBytes_AsTDB_DATA(py_value
);
355 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
356 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
360 static PyObject
*obj_storev(PyTdbObject
*self
, PyObject
*args
)
362 TDB_DATA key
, *values
, value
;
364 int flag
= TDB_REPLACE
;
365 Py_ssize_t num_values
, i
;
366 PyObject
*py_key
, *py_values
, *py_value
;
368 PyErr_TDB_RAISE_IF_CLOSED(self
);
370 if (!PyArg_ParseTuple(
371 args
, "OO!|i", &py_key
, &PyList_Type
, &py_values
, &flag
)) {
375 num_values
= PyList_Size(py_values
);
377 key
= PyBytes_AsTDB_DATA(py_key
);
378 if (key
.dptr
== NULL
) {
382 if (SSIZE_MAX
/sizeof(TDB_DATA
) < num_values
) {
383 PyErr_SetFromErrno(PyExc_OverflowError
);
386 if (num_values
> INT_MAX
) {
387 PyErr_SetFromErrno(PyExc_OverflowError
);
390 values
= malloc(sizeof(TDB_DATA
) * num_values
);
391 if (values
== NULL
) {
395 for (i
=0; i
<num_values
; i
++) {
396 py_value
= PyList_GetItem(py_values
, i
);
397 value
= PyBytes_AsTDB_DATA(py_value
);
405 ret
= tdb_storev(self
->ctx
, key
, values
, (int)num_values
, flag
);
407 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
411 static PyObject
*obj_add_flags(PyTdbObject
*self
, PyObject
*args
)
415 PyErr_TDB_RAISE_IF_CLOSED(self
);
417 if (!PyArg_ParseTuple(args
, "I", &flags
))
420 tdb_add_flags(self
->ctx
, flags
);
424 static PyObject
*obj_remove_flags(PyTdbObject
*self
, PyObject
*args
)
428 PyErr_TDB_RAISE_IF_CLOSED(self
);
430 if (!PyArg_ParseTuple(args
, "I", &flags
))
433 tdb_remove_flags(self
->ctx
, flags
);
440 PyTdbObject
*iteratee
;
441 } PyTdbIteratorObject
;
443 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
447 if (self
->current
.dptr
== NULL
&& self
->current
.dsize
== 0)
449 current
= self
->current
;
450 self
->current
= tdb_nextkey(self
->iteratee
->ctx
, self
->current
);
451 ret
= PyBytes_FromTDB_DATA(current
);
455 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
457 Py_CLEAR(self
->iteratee
);
461 PyTypeObject PyTdbIterator
= {
462 .tp_name
= "Iterator",
463 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
464 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
465 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
466 .tp_flags
= Py_TPFLAGS_DEFAULT
,
467 .tp_iter
= PyObject_SelfIter
,
470 static PyObject
*tdb_object_iter(PyTdbObject
*self
,
471 PyObject
*Py_UNUSED(ignored
))
473 PyTdbIteratorObject
*ret
;
475 PyErr_TDB_RAISE_IF_CLOSED(self
);
477 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
480 ret
->current
= tdb_firstkey(self
->ctx
);
481 ret
->iteratee
= self
;
483 return (PyObject
*)ret
;
486 static PyObject
*obj_clear(PyTdbObject
*self
, PyObject
*Py_UNUSED(ignored
))
489 PyErr_TDB_RAISE_IF_CLOSED(self
);
490 ret
= tdb_wipe_all(self
->ctx
);
491 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
495 static PyObject
*obj_repack(PyTdbObject
*self
, PyObject
*Py_UNUSED(ignored
))
498 PyErr_TDB_RAISE_IF_CLOSED(self
);
499 ret
= tdb_repack(self
->ctx
);
500 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
504 static PyObject
*obj_enable_seqnum(PyTdbObject
*self
,
505 PyObject
*Py_UNUSED(ignored
))
507 PyErr_TDB_RAISE_IF_CLOSED(self
);
508 tdb_enable_seqnum(self
->ctx
);
512 static PyObject
*obj_increment_seqnum_nonblock(PyTdbObject
*self
,
513 PyObject
*Py_UNUSED(ignored
))
515 PyErr_TDB_RAISE_IF_CLOSED(self
);
516 tdb_increment_seqnum_nonblock(self
->ctx
);
520 static PyMethodDef tdb_object_methods
[] = {
521 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
522 "S.transaction_cancel() -> None\n"
523 "Cancel the currently active transaction." },
524 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
525 "S.transaction_commit() -> None\n"
526 "Commit the currently active transaction." },
527 { "transaction_prepare_commit", (PyCFunction
)obj_transaction_prepare_commit
, METH_NOARGS
,
528 "S.transaction_prepare_commit() -> None\n"
529 "Prepare to commit the currently active transaction" },
530 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
531 "S.transaction_start() -> None\n"
532 "Start a new transaction." },
533 { "reopen", (PyCFunction
)obj_reopen
, METH_NOARGS
, "Reopen this file." },
534 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
535 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
536 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
537 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
538 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
539 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.get(key) -> value\n"
541 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
542 "Append data to an existing key." },
543 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
544 "Return the first key in this database." },
545 { "nextkey", (PyCFunction
)obj_nextkey
, METH_VARARGS
, "S.nextkey(key) -> data\n"
546 "Return the next key in this database." },
547 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
548 "Delete an entry." },
549 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
551 { "storev", (PyCFunction
)obj_storev
, METH_VARARGS
, "S.storev(key, data, flag=REPLACE) -> None"
552 "Store several data." },
553 { "add_flags", (PyCFunction
)obj_add_flags
, METH_VARARGS
, "S.add_flags(flags) -> None" },
554 { "remove_flags", (PyCFunction
)obj_remove_flags
, METH_VARARGS
, "S.remove_flags(flags) -> None" },
555 { "keys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.keys() -> iterator" },
556 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
557 "Wipe the entire database." },
558 { "repack", (PyCFunction
)obj_repack
, METH_NOARGS
, "S.repack() -> None\n"
559 "Repack the entire database." },
560 { "enable_seqnum", (PyCFunction
)obj_enable_seqnum
, METH_NOARGS
,
561 "S.enable_seqnum() -> None" },
562 { "increment_seqnum_nonblock", (PyCFunction
)obj_increment_seqnum_nonblock
, METH_NOARGS
,
563 "S.increment_seqnum_nonblock() -> None" },
567 static PyObject
*obj_get_hash_size(PyTdbObject
*self
, void *closure
)
569 PyErr_TDB_RAISE_IF_CLOSED(self
);
570 return PyLong_FromLong(tdb_hash_size(self
->ctx
));
573 static int obj_set_max_dead(PyTdbObject
*self
, PyObject
*max_dead
, void *closure
)
575 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
576 if (!PyLong_Check(max_dead
))
578 tdb_set_max_dead(self
->ctx
, PyLong_AsLong(max_dead
));
582 static PyObject
*obj_get_map_size(PyTdbObject
*self
, void *closure
)
584 PyErr_TDB_RAISE_IF_CLOSED(self
);
585 return PyLong_FromLong(tdb_map_size(self
->ctx
));
588 static PyObject
*obj_get_freelist_size(PyTdbObject
*self
, void *closure
)
590 PyErr_TDB_RAISE_IF_CLOSED(self
);
591 return PyLong_FromLong(tdb_freelist_size(self
->ctx
));
594 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
596 PyErr_TDB_RAISE_IF_CLOSED(self
);
597 return PyLong_FromLong(tdb_get_flags(self
->ctx
));
600 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
602 PyErr_TDB_RAISE_IF_CLOSED(self
);
603 return PyBytes_FromString(tdb_name(self
->ctx
));
606 static PyObject
*obj_get_seqnum(PyTdbObject
*self
, void *closure
)
608 PyErr_TDB_RAISE_IF_CLOSED(self
);
609 return PyLong_FromLong(tdb_get_seqnum(self
->ctx
));
612 static PyObject
*obj_get_text(PyTdbObject
*self
, void *closure
)
614 PyObject
*mod
, *cls
, *inst
;
615 mod
= PyImport_ImportModule("_tdb_text");
618 cls
= PyObject_GetAttrString(mod
, "TdbTextWrapper");
623 inst
= PyObject_CallFunction(cls
, discard_const_p(char, "O"), self
);
629 static PyGetSetDef tdb_object_getsetters
[] = {
631 .name
= discard_const_p(char, "hash_size"),
632 .get
= (getter
)obj_get_hash_size
,
635 .name
= discard_const_p(char, "map_size"),
636 .get
= (getter
)obj_get_map_size
,
639 .name
= discard_const_p(char, "freelist_size"),
640 .get
= (getter
)obj_get_freelist_size
,
643 .name
= discard_const_p(char, "flags"),
644 .get
= (getter
)obj_get_flags
,
647 .name
= discard_const_p(char, "max_dead"),
648 .set
= (setter
)obj_set_max_dead
,
651 .name
= discard_const_p(char, "filename"),
652 .get
= (getter
)obj_get_filename
,
653 .doc
= discard_const_p(char, "The filename of this TDB file."),
656 .name
= discard_const_p(char, "seqnum"),
657 .get
= (getter
)obj_get_seqnum
,
660 .name
= discard_const_p(char, "text"),
661 .get
= (getter
)obj_get_text
,
666 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
668 PyErr_TDB_RAISE_IF_CLOSED(self
);
669 if (tdb_get_flags(self
->ctx
) & TDB_INTERNAL
) {
670 return PyUnicode_FromString("Tdb(<internal>)");
672 return PyUnicode_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
676 static void tdb_object_dealloc(PyTdbObject
*self
)
679 tdb_close(self
->ctx
);
680 Py_TYPE(self
)->tp_free(self
);
683 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
686 PyErr_TDB_RAISE_IF_CLOSED(self
);
687 if (!PyBytes_Check(key
)) {
688 PyErr_SetString(PyExc_TypeError
, "Expected bytestring as key");
692 tkey
.dptr
= (unsigned char *)PyBytes_AsString(key
);
693 tkey
.dsize
= PyBytes_Size(key
);
695 val
= tdb_fetch(self
->ctx
, tkey
);
696 if (val
.dptr
== NULL
) {
698 * if the key doesn't exist raise KeyError(key) to be
699 * consistent with python dict
701 PyErr_SetObject(PyExc_KeyError
, key
);
704 return PyBytes_FromTDB_DATA(val
);
708 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
712 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
713 if (!PyBytes_Check(key
)) {
714 PyErr_SetString(PyExc_TypeError
, "Expected bytestring as key");
718 tkey
= PyBytes_AsTDB_DATA(key
);
721 ret
= tdb_delete(self
->ctx
, tkey
);
723 if (!PyBytes_Check(value
)) {
724 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
728 tval
= PyBytes_AsTDB_DATA(value
);
730 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
734 PyErr_SetTDBError(self
->ctx
);
741 static PyMappingMethods tdb_object_mapping
= {
742 .mp_subscript
= (binaryfunc
)obj_getitem
,
743 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
745 static PySequenceMethods tdb_object_seq
= {
746 .sq_contains
= (objobjproc
)obj_contains
,
748 static PyTypeObject PyTdb
= {
749 .tp_name
= "tdb.Tdb",
750 .tp_basicsize
= sizeof(PyTdbObject
),
751 .tp_methods
= tdb_object_methods
,
752 .tp_getset
= tdb_object_getsetters
,
753 .tp_new
= py_tdb_open
,
754 .tp_doc
= "A TDB file",
755 .tp_repr
= (reprfunc
)tdb_object_repr
,
756 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
757 .tp_as_mapping
= &tdb_object_mapping
,
758 .tp_as_sequence
= &tdb_object_seq
,
759 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
760 .tp_iter
= PY_DISCARD_FUNC_SIG(getiterfunc
,tdb_object_iter
),
763 static PyMethodDef tdb_methods
[] = {
766 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
, py_tdb_open
),
767 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
768 .ml_doc
= "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, "
769 "flags=O_RDWR, mode=0600)\nOpen a TDB file."
774 #define MODULE_DOC "simple key-value database that supports multiple writers."
776 static struct PyModuleDef moduledef
= {
777 PyModuleDef_HEAD_INIT
,
781 .m_methods
= tdb_methods
,
784 PyObject
* module_init(void);
785 PyObject
* module_init(void)
789 if (PyType_Ready(&PyTdb
) < 0)
792 if (PyType_Ready(&PyTdbIterator
) < 0)
795 m
= PyModule_Create(&moduledef
);
799 PyModule_AddIntConstant(m
, "REPLACE", TDB_REPLACE
);
800 PyModule_AddIntConstant(m
, "INSERT", TDB_INSERT
);
801 PyModule_AddIntConstant(m
, "MODIFY", TDB_MODIFY
);
803 PyModule_AddIntConstant(m
, "DEFAULT", TDB_DEFAULT
);
804 PyModule_AddIntConstant(m
, "CLEAR_IF_FIRST", TDB_CLEAR_IF_FIRST
);
805 PyModule_AddIntConstant(m
, "INTERNAL", TDB_INTERNAL
);
806 PyModule_AddIntConstant(m
, "NOLOCK", TDB_NOLOCK
);
807 PyModule_AddIntConstant(m
, "NOMMAP", TDB_NOMMAP
);
808 PyModule_AddIntConstant(m
, "CONVERT", TDB_CONVERT
);
809 PyModule_AddIntConstant(m
, "BIGENDIAN", TDB_BIGENDIAN
);
810 PyModule_AddIntConstant(m
, "NOSYNC", TDB_NOSYNC
);
811 PyModule_AddIntConstant(m
, "SEQNUM", TDB_SEQNUM
);
812 PyModule_AddIntConstant(m
, "VOLATILE", TDB_VOLATILE
);
813 PyModule_AddIntConstant(m
, "ALLOW_NESTING", TDB_ALLOW_NESTING
);
814 PyModule_AddIntConstant(m
, "DISALLOW_NESTING", TDB_DISALLOW_NESTING
);
815 PyModule_AddIntConstant(m
, "INCOMPATIBLE_HASH", TDB_INCOMPATIBLE_HASH
);
817 PyModule_AddStringConstant(m
, "__docformat__", "restructuredText");
819 PyModule_AddStringConstant(m
, "__version__", PACKAGE_VERSION
);
822 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
824 Py_INCREF(&PyTdbIterator
);
830 PyMODINIT_FUNC
PyInit_tdb(void);
831 PyMODINIT_FUNC
PyInit_tdb(void)
833 return module_init();