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/>.
29 #include "system/filesys.h"
31 /* Include tdb headers */
34 #if PY_MAJOR_VERSION >= 3
35 #define PyInt_FromLong PyLong_FromLong
36 #define PyInt_Check PyLong_Check
37 #define PyInt_AsLong PyLong_AsLong
38 #define Py_TPFLAGS_HAVE_ITER 0
41 /* discard signature of 'func' in favour of 'target_sig' */
42 #define PY_DISCARD_FUNC_SIG(target_sig, func) (target_sig)(void(*)(void))func
50 static PyTypeObject PyTdb
;
52 static void PyErr_SetTDBError(TDB_CONTEXT
*tdb
)
54 PyErr_SetObject(PyExc_RuntimeError
,
55 Py_BuildValue("(i,s)", tdb_error(tdb
), tdb_errorstr(tdb
)));
58 static TDB_DATA
PyBytes_AsTDB_DATA(PyObject
*data
)
61 ret
.dptr
= (unsigned char *)PyBytes_AsString(data
);
62 ret
.dsize
= PyBytes_Size(data
);
66 static PyObject
*PyBytes_FromTDB_DATA(TDB_DATA data
)
68 if (data
.dptr
== NULL
&& data
.dsize
== 0) {
71 PyObject
*ret
= PyBytes_FromStringAndSize((const char *)data
.dptr
,
78 #define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
80 PyErr_SetTDBError(tdb); \
84 #define PyErr_TDB_RAISE_IF_CLOSED(self) \
86 PyErr_SetObject(PyExc_RuntimeError, \
87 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
91 #define PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self) \
93 PyErr_SetObject(PyExc_RuntimeError, \
94 Py_BuildValue("(i,s)", TDB_ERR_IO, "Database is already closed")); \
98 static PyObject
*py_tdb_open(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
101 int hash_size
= 0, tdb_flags
= TDB_DEFAULT
, flags
= O_RDWR
, mode
= 0600;
104 const char *_kwnames
[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL
};
105 char **kwnames
= discard_const_p(char *, _kwnames
);
107 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|siiii", kwnames
, &name
, &hash_size
, &tdb_flags
, &flags
, &mode
))
111 tdb_flags
|= TDB_INTERNAL
;
114 ctx
= tdb_open(name
, hash_size
, tdb_flags
, flags
, mode
);
116 PyErr_SetFromErrno(PyExc_IOError
);
120 ret
= PyObject_New(PyTdbObject
, &PyTdb
);
128 return (PyObject
*)ret
;
131 static PyObject
*obj_transaction_cancel(PyTdbObject
*self
,
132 PyObject
*Py_UNUSED(ignored
))
136 PyErr_TDB_RAISE_IF_CLOSED(self
);
138 ret
= tdb_transaction_cancel(self
->ctx
);
139 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
143 static PyObject
*obj_transaction_commit(PyTdbObject
*self
,
144 PyObject
*Py_UNUSED(ignored
))
147 PyErr_TDB_RAISE_IF_CLOSED(self
);
148 ret
= tdb_transaction_commit(self
->ctx
);
149 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
153 static PyObject
*obj_transaction_prepare_commit(PyTdbObject
*self
,
154 PyObject
*Py_UNUSED(ignored
))
157 PyErr_TDB_RAISE_IF_CLOSED(self
);
158 ret
= tdb_transaction_prepare_commit(self
->ctx
);
159 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
163 static PyObject
*obj_transaction_start(PyTdbObject
*self
,
164 PyObject
*Py_UNUSED(ignored
))
167 PyErr_TDB_RAISE_IF_CLOSED(self
);
168 ret
= tdb_transaction_start(self
->ctx
);
169 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
173 static PyObject
*obj_reopen(PyTdbObject
*self
,
174 PyObject
*Py_UNUSED(ignored
))
177 PyErr_TDB_RAISE_IF_CLOSED(self
);
178 ret
= tdb_reopen(self
->ctx
);
181 PyErr_SetObject(PyExc_RuntimeError
,
182 Py_BuildValue("(i,s)",
184 "Failed to reopen database"));
190 static PyObject
*obj_lockall(PyTdbObject
*self
,
191 PyObject
*Py_UNUSED(ignored
))
194 PyErr_TDB_RAISE_IF_CLOSED(self
);
195 ret
= tdb_lockall(self
->ctx
);
196 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
200 static PyObject
*obj_unlockall(PyTdbObject
*self
,
201 PyObject
*Py_UNUSED(ignored
))
204 PyErr_TDB_RAISE_IF_CLOSED(self
);
205 ret
= tdb_unlockall(self
->ctx
);
206 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
210 static PyObject
*obj_lockall_read(PyTdbObject
*self
,
211 PyObject
*Py_UNUSED(ignored
))
214 PyErr_TDB_RAISE_IF_CLOSED(self
);
215 ret
= tdb_lockall_read(self
->ctx
);
216 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
220 static PyObject
*obj_unlockall_read(PyTdbObject
*self
,
221 PyObject
*Py_UNUSED(ignored
))
223 int ret
= tdb_unlockall_read(self
->ctx
);
224 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
228 static PyObject
*obj_close(PyTdbObject
*self
, PyObject
*Py_UNUSED(ignored
))
233 ret
= tdb_close(self
->ctx
);
236 PyErr_SetObject(PyExc_RuntimeError
,
237 Py_BuildValue("(i,s)",
239 "Failed to close database"));
245 static PyObject
*obj_get(PyTdbObject
*self
, PyObject
*args
)
250 PyErr_TDB_RAISE_IF_CLOSED(self
);
252 if (!PyArg_ParseTuple(args
, "O", &py_key
))
255 key
= PyBytes_AsTDB_DATA(py_key
);
259 return PyBytes_FromTDB_DATA(tdb_fetch(self
->ctx
, key
));
262 static PyObject
*obj_append(PyTdbObject
*self
, PyObject
*args
)
265 PyObject
*py_key
, *py_data
;
268 PyErr_TDB_RAISE_IF_CLOSED(self
);
270 if (!PyArg_ParseTuple(args
, "OO", &py_key
, &py_data
))
273 key
= PyBytes_AsTDB_DATA(py_key
);
276 data
= PyBytes_AsTDB_DATA(py_data
);
280 ret
= tdb_append(self
->ctx
, key
, data
);
281 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
285 static PyObject
*obj_firstkey(PyTdbObject
*self
, PyObject
*Py_UNUSED(ignored
))
287 PyErr_TDB_RAISE_IF_CLOSED(self
);
289 return PyBytes_FromTDB_DATA(tdb_firstkey(self
->ctx
));
292 static PyObject
*obj_nextkey(PyTdbObject
*self
, PyObject
*args
)
296 PyErr_TDB_RAISE_IF_CLOSED(self
);
298 if (!PyArg_ParseTuple(args
, "O", &py_key
))
301 key
= PyBytes_AsTDB_DATA(py_key
);
305 return PyBytes_FromTDB_DATA(tdb_nextkey(self
->ctx
, key
));
308 static PyObject
*obj_delete(PyTdbObject
*self
, PyObject
*args
)
313 PyErr_TDB_RAISE_IF_CLOSED(self
);
315 if (!PyArg_ParseTuple(args
, "O", &py_key
))
318 key
= PyBytes_AsTDB_DATA(py_key
);
321 ret
= tdb_delete(self
->ctx
, key
);
322 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
326 static int obj_contains(PyTdbObject
*self
, PyObject
*py_key
)
330 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
332 key
= PyBytes_AsTDB_DATA(py_key
);
337 ret
= tdb_exists(self
->ctx
, key
);
343 #if PY_MAJOR_VERSION < 3
344 static PyObject
*obj_has_key(PyTdbObject
*self
, PyObject
*args
)
348 PyErr_TDB_RAISE_IF_CLOSED(self
);
350 if (!PyArg_ParseTuple(args
, "O", &py_key
))
353 ret
= obj_contains(self
, py_key
);
363 static PyObject
*obj_store(PyTdbObject
*self
, PyObject
*args
)
367 int flag
= TDB_REPLACE
;
368 PyObject
*py_key
, *py_value
;
370 PyErr_TDB_RAISE_IF_CLOSED(self
);
372 if (!PyArg_ParseTuple(args
, "OO|i", &py_key
, &py_value
, &flag
))
375 key
= PyBytes_AsTDB_DATA(py_key
);
378 value
= PyBytes_AsTDB_DATA(py_value
);
382 ret
= tdb_store(self
->ctx
, key
, value
, flag
);
383 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
387 static PyObject
*obj_add_flags(PyTdbObject
*self
, PyObject
*args
)
391 PyErr_TDB_RAISE_IF_CLOSED(self
);
393 if (!PyArg_ParseTuple(args
, "I", &flags
))
396 tdb_add_flags(self
->ctx
, flags
);
400 static PyObject
*obj_remove_flags(PyTdbObject
*self
, PyObject
*args
)
404 PyErr_TDB_RAISE_IF_CLOSED(self
);
406 if (!PyArg_ParseTuple(args
, "I", &flags
))
409 tdb_remove_flags(self
->ctx
, flags
);
416 PyTdbObject
*iteratee
;
417 } PyTdbIteratorObject
;
419 static PyObject
*tdb_iter_next(PyTdbIteratorObject
*self
)
423 if (self
->current
.dptr
== NULL
&& self
->current
.dsize
== 0)
425 current
= self
->current
;
426 self
->current
= tdb_nextkey(self
->iteratee
->ctx
, self
->current
);
427 ret
= PyBytes_FromTDB_DATA(current
);
431 static void tdb_iter_dealloc(PyTdbIteratorObject
*self
)
433 Py_DECREF(self
->iteratee
);
437 PyTypeObject PyTdbIterator
= {
438 .tp_name
= "Iterator",
439 .tp_basicsize
= sizeof(PyTdbIteratorObject
),
440 .tp_iternext
= (iternextfunc
)tdb_iter_next
,
441 .tp_dealloc
= (destructor
)tdb_iter_dealloc
,
442 .tp_flags
= Py_TPFLAGS_DEFAULT
,
443 .tp_iter
= PyObject_SelfIter
,
446 static PyObject
*tdb_object_iter(PyTdbObject
*self
,
447 PyObject
*Py_UNUSED(ignored
))
449 PyTdbIteratorObject
*ret
;
451 PyErr_TDB_RAISE_IF_CLOSED(self
);
453 ret
= PyObject_New(PyTdbIteratorObject
, &PyTdbIterator
);
456 ret
->current
= tdb_firstkey(self
->ctx
);
457 ret
->iteratee
= self
;
459 return (PyObject
*)ret
;
462 static PyObject
*obj_clear(PyTdbObject
*self
, PyObject
*Py_UNUSED(ignored
))
465 PyErr_TDB_RAISE_IF_CLOSED(self
);
466 ret
= tdb_wipe_all(self
->ctx
);
467 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
471 static PyObject
*obj_repack(PyTdbObject
*self
, PyObject
*Py_UNUSED(ignored
))
474 PyErr_TDB_RAISE_IF_CLOSED(self
);
475 ret
= tdb_repack(self
->ctx
);
476 PyErr_TDB_ERROR_IS_ERR_RAISE(ret
, self
->ctx
);
480 static PyObject
*obj_enable_seqnum(PyTdbObject
*self
,
481 PyObject
*Py_UNUSED(ignored
))
483 PyErr_TDB_RAISE_IF_CLOSED(self
);
484 tdb_enable_seqnum(self
->ctx
);
488 static PyObject
*obj_increment_seqnum_nonblock(PyTdbObject
*self
,
489 PyObject
*Py_UNUSED(ignored
))
491 PyErr_TDB_RAISE_IF_CLOSED(self
);
492 tdb_increment_seqnum_nonblock(self
->ctx
);
496 static PyMethodDef tdb_object_methods
[] = {
497 { "transaction_cancel", (PyCFunction
)obj_transaction_cancel
, METH_NOARGS
,
498 "S.transaction_cancel() -> None\n"
499 "Cancel the currently active transaction." },
500 { "transaction_commit", (PyCFunction
)obj_transaction_commit
, METH_NOARGS
,
501 "S.transaction_commit() -> None\n"
502 "Commit the currently active transaction." },
503 { "transaction_prepare_commit", (PyCFunction
)obj_transaction_prepare_commit
, METH_NOARGS
,
504 "S.transaction_prepare_commit() -> None\n"
505 "Prepare to commit the currently active transaction" },
506 { "transaction_start", (PyCFunction
)obj_transaction_start
, METH_NOARGS
,
507 "S.transaction_start() -> None\n"
508 "Start a new transaction." },
509 { "reopen", (PyCFunction
)obj_reopen
, METH_NOARGS
, "Reopen this file." },
510 { "lock_all", (PyCFunction
)obj_lockall
, METH_NOARGS
, NULL
},
511 { "unlock_all", (PyCFunction
)obj_unlockall
, METH_NOARGS
, NULL
},
512 { "read_lock_all", (PyCFunction
)obj_lockall_read
, METH_NOARGS
, NULL
},
513 { "read_unlock_all", (PyCFunction
)obj_unlockall_read
, METH_NOARGS
, NULL
},
514 { "close", (PyCFunction
)obj_close
, METH_NOARGS
, NULL
},
515 { "get", (PyCFunction
)obj_get
, METH_VARARGS
, "S.get(key) -> value\n"
517 { "append", (PyCFunction
)obj_append
, METH_VARARGS
, "S.append(key, value) -> None\n"
518 "Append data to an existing key." },
519 { "firstkey", (PyCFunction
)obj_firstkey
, METH_NOARGS
, "S.firstkey() -> data\n"
520 "Return the first key in this database." },
521 { "nextkey", (PyCFunction
)obj_nextkey
, METH_VARARGS
, "S.nextkey(key) -> data\n"
522 "Return the next key in this database." },
523 { "delete", (PyCFunction
)obj_delete
, METH_VARARGS
, "S.delete(key) -> None\n"
524 "Delete an entry." },
525 #if PY_MAJOR_VERSION < 3
526 { "has_key", (PyCFunction
)obj_has_key
, METH_VARARGS
, "S.has_key(key) -> None\n"
527 "Check whether key exists in this database." },
529 { "store", (PyCFunction
)obj_store
, METH_VARARGS
, "S.store(key, data, flag=REPLACE) -> None"
531 { "add_flags", (PyCFunction
)obj_add_flags
, METH_VARARGS
, "S.add_flags(flags) -> None" },
532 { "remove_flags", (PyCFunction
)obj_remove_flags
, METH_VARARGS
, "S.remove_flags(flags) -> None" },
533 #if PY_MAJOR_VERSION >= 3
534 { "keys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
536 { "iterkeys", (PyCFunction
)tdb_object_iter
, METH_NOARGS
, "S.iterkeys() -> iterator" },
538 { "clear", (PyCFunction
)obj_clear
, METH_NOARGS
, "S.clear() -> None\n"
539 "Wipe the entire database." },
540 { "repack", (PyCFunction
)obj_repack
, METH_NOARGS
, "S.repack() -> None\n"
541 "Repack the entire database." },
542 { "enable_seqnum", (PyCFunction
)obj_enable_seqnum
, METH_NOARGS
,
543 "S.enable_seqnum() -> None" },
544 { "increment_seqnum_nonblock", (PyCFunction
)obj_increment_seqnum_nonblock
, METH_NOARGS
,
545 "S.increment_seqnum_nonblock() -> None" },
549 static PyObject
*obj_get_hash_size(PyTdbObject
*self
, void *closure
)
551 PyErr_TDB_RAISE_IF_CLOSED(self
);
552 return PyInt_FromLong(tdb_hash_size(self
->ctx
));
555 static int obj_set_max_dead(PyTdbObject
*self
, PyObject
*max_dead
, void *closure
)
557 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
558 if (!PyInt_Check(max_dead
))
560 tdb_set_max_dead(self
->ctx
, PyInt_AsLong(max_dead
));
564 static PyObject
*obj_get_map_size(PyTdbObject
*self
, void *closure
)
566 PyErr_TDB_RAISE_IF_CLOSED(self
);
567 return PyInt_FromLong(tdb_map_size(self
->ctx
));
570 static PyObject
*obj_get_freelist_size(PyTdbObject
*self
, void *closure
)
572 PyErr_TDB_RAISE_IF_CLOSED(self
);
573 return PyInt_FromLong(tdb_freelist_size(self
->ctx
));
576 static PyObject
*obj_get_flags(PyTdbObject
*self
, void *closure
)
578 PyErr_TDB_RAISE_IF_CLOSED(self
);
579 return PyInt_FromLong(tdb_get_flags(self
->ctx
));
582 static PyObject
*obj_get_filename(PyTdbObject
*self
, void *closure
)
584 PyErr_TDB_RAISE_IF_CLOSED(self
);
585 return PyBytes_FromString(tdb_name(self
->ctx
));
588 static PyObject
*obj_get_seqnum(PyTdbObject
*self
, void *closure
)
590 PyErr_TDB_RAISE_IF_CLOSED(self
);
591 return PyInt_FromLong(tdb_get_seqnum(self
->ctx
));
594 static PyObject
*obj_get_text(PyTdbObject
*self
, void *closure
)
596 PyObject
*mod
, *cls
, *inst
;
597 mod
= PyImport_ImportModule("_tdb_text");
600 cls
= PyObject_GetAttrString(mod
, "TdbTextWrapper");
605 inst
= PyObject_CallFunction(cls
, discard_const_p(char, "O"), self
);
611 static PyGetSetDef tdb_object_getsetters
[] = {
613 .name
= discard_const_p(char, "hash_size"),
614 .get
= (getter
)obj_get_hash_size
,
617 .name
= discard_const_p(char, "map_size"),
618 .get
= (getter
)obj_get_map_size
,
621 .name
= discard_const_p(char, "freelist_size"),
622 .get
= (getter
)obj_get_freelist_size
,
625 .name
= discard_const_p(char, "flags"),
626 .get
= (getter
)obj_get_flags
,
629 .name
= discard_const_p(char, "max_dead"),
630 .set
= (setter
)obj_set_max_dead
,
633 .name
= discard_const_p(char, "filename"),
634 .get
= (getter
)obj_get_filename
,
635 .doc
= discard_const_p(char, "The filename of this TDB file."),
638 .name
= discard_const_p(char, "seqnum"),
639 .get
= (getter
)obj_get_seqnum
,
642 .name
= discard_const_p(char, "text"),
643 .get
= (getter
)obj_get_text
,
648 static PyObject
*tdb_object_repr(PyTdbObject
*self
)
650 PyErr_TDB_RAISE_IF_CLOSED(self
);
651 if (tdb_get_flags(self
->ctx
) & TDB_INTERNAL
) {
652 return PyUnicode_FromString("Tdb(<internal>)");
654 return PyUnicode_FromFormat("Tdb('%s')", tdb_name(self
->ctx
));
658 static void tdb_object_dealloc(PyTdbObject
*self
)
661 tdb_close(self
->ctx
);
662 Py_TYPE(self
)->tp_free(self
);
665 static PyObject
*obj_getitem(PyTdbObject
*self
, PyObject
*key
)
668 PyErr_TDB_RAISE_IF_CLOSED(self
);
669 if (!PyBytes_Check(key
)) {
670 PyErr_SetString(PyExc_TypeError
, "Expected bytestring as key");
674 tkey
.dptr
= (unsigned char *)PyBytes_AsString(key
);
675 tkey
.dsize
= PyBytes_Size(key
);
677 val
= tdb_fetch(self
->ctx
, tkey
);
678 if (val
.dptr
== NULL
) {
680 * if the key doesn't exist raise KeyError(key) to be
681 * consistent with python dict
683 PyErr_SetObject(PyExc_KeyError
, key
);
686 return PyBytes_FromTDB_DATA(val
);
690 static int obj_setitem(PyTdbObject
*self
, PyObject
*key
, PyObject
*value
)
694 PyErr_TDB_RAISE_RETURN_MINUS_1_IF_CLOSED(self
);
695 if (!PyBytes_Check(key
)) {
696 PyErr_SetString(PyExc_TypeError
, "Expected bytestring as key");
700 tkey
= PyBytes_AsTDB_DATA(key
);
703 ret
= tdb_delete(self
->ctx
, tkey
);
705 if (!PyBytes_Check(value
)) {
706 PyErr_SetString(PyExc_TypeError
, "Expected string as value");
710 tval
= PyBytes_AsTDB_DATA(value
);
712 ret
= tdb_store(self
->ctx
, tkey
, tval
, TDB_REPLACE
);
716 PyErr_SetTDBError(self
->ctx
);
723 static PyMappingMethods tdb_object_mapping
= {
724 .mp_subscript
= (binaryfunc
)obj_getitem
,
725 .mp_ass_subscript
= (objobjargproc
)obj_setitem
,
727 static PySequenceMethods tdb_object_seq
= {
728 .sq_contains
= (objobjproc
)obj_contains
,
730 static PyTypeObject PyTdb
= {
731 .tp_name
= "tdb.Tdb",
732 .tp_basicsize
= sizeof(PyTdbObject
),
733 .tp_methods
= tdb_object_methods
,
734 .tp_getset
= tdb_object_getsetters
,
735 .tp_new
= py_tdb_open
,
736 .tp_doc
= "A TDB file",
737 .tp_repr
= (reprfunc
)tdb_object_repr
,
738 .tp_dealloc
= (destructor
)tdb_object_dealloc
,
739 .tp_as_mapping
= &tdb_object_mapping
,
740 .tp_as_sequence
= &tdb_object_seq
,
741 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
|Py_TPFLAGS_HAVE_ITER
,
742 .tp_iter
= PY_DISCARD_FUNC_SIG(getiterfunc
,tdb_object_iter
),
745 static PyMethodDef tdb_methods
[] = {
748 .ml_meth
= PY_DISCARD_FUNC_SIG(PyCFunction
, py_tdb_open
),
749 .ml_flags
= METH_VARARGS
|METH_KEYWORDS
,
750 .ml_doc
= "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, "
751 "flags=O_RDWR, mode=0600)\nOpen a TDB file."
756 #define MODULE_DOC "simple key-value database that supports multiple writers."
758 #if PY_MAJOR_VERSION >= 3
759 static struct PyModuleDef moduledef
= {
760 PyModuleDef_HEAD_INIT
,
764 .m_methods
= tdb_methods
,
768 PyObject
* module_init(void);
769 PyObject
* module_init(void)
773 if (PyType_Ready(&PyTdb
) < 0)
776 if (PyType_Ready(&PyTdbIterator
) < 0)
779 #if PY_MAJOR_VERSION >= 3
780 m
= PyModule_Create(&moduledef
);
782 m
= Py_InitModule3("tdb", tdb_methods
, MODULE_DOC
);
787 PyModule_AddIntConstant(m
, "REPLACE", TDB_REPLACE
);
788 PyModule_AddIntConstant(m
, "INSERT", TDB_INSERT
);
789 PyModule_AddIntConstant(m
, "MODIFY", TDB_MODIFY
);
791 PyModule_AddIntConstant(m
, "DEFAULT", TDB_DEFAULT
);
792 PyModule_AddIntConstant(m
, "CLEAR_IF_FIRST", TDB_CLEAR_IF_FIRST
);
793 PyModule_AddIntConstant(m
, "INTERNAL", TDB_INTERNAL
);
794 PyModule_AddIntConstant(m
, "NOLOCK", TDB_NOLOCK
);
795 PyModule_AddIntConstant(m
, "NOMMAP", TDB_NOMMAP
);
796 PyModule_AddIntConstant(m
, "CONVERT", TDB_CONVERT
);
797 PyModule_AddIntConstant(m
, "BIGENDIAN", TDB_BIGENDIAN
);
798 PyModule_AddIntConstant(m
, "NOSYNC", TDB_NOSYNC
);
799 PyModule_AddIntConstant(m
, "SEQNUM", TDB_SEQNUM
);
800 PyModule_AddIntConstant(m
, "VOLATILE", TDB_VOLATILE
);
801 PyModule_AddIntConstant(m
, "ALLOW_NESTING", TDB_ALLOW_NESTING
);
802 PyModule_AddIntConstant(m
, "DISALLOW_NESTING", TDB_DISALLOW_NESTING
);
803 PyModule_AddIntConstant(m
, "INCOMPATIBLE_HASH", TDB_INCOMPATIBLE_HASH
);
805 PyModule_AddStringConstant(m
, "__docformat__", "restructuredText");
807 PyModule_AddStringConstant(m
, "__version__", PACKAGE_VERSION
);
810 PyModule_AddObject(m
, "Tdb", (PyObject
*)&PyTdb
);
812 Py_INCREF(&PyTdbIterator
);
818 #if PY_MAJOR_VERSION >= 3
819 PyMODINIT_FUNC
PyInit_tdb(void);
820 PyMODINIT_FUNC
PyInit_tdb(void)
822 return module_init();