cbe7ae81fa0e6effe9636d010de8188765086fd9
[obnox/samba/samba-obnox.git] / lib / ntdb / pyntdb.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Python interface to ntdb.  Simply modified from tdb version.
5
6    Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7    Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
8    Copyright (C) 2011 Rusty Russell <rusty@rustcorp.com.au>
9
10      ** NOTE! The following LGPL license applies to the ntdb
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include <Python.h>
29 #include "replace.h"
30 #include "system/filesys.h"
31
32 #ifndef Py_RETURN_NONE
33 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
34 #endif
35
36 /* Include ntdb headers */
37 #include <ntdb.h>
38
39 typedef struct {
40         PyObject_HEAD
41         struct ntdb_context *ctx;
42         bool closed;
43 } PyNtdbObject;
44
45 staticforward PyTypeObject PyNtdb;
46
47 static void PyErr_SetTDBError(enum NTDB_ERROR e)
48 {
49         PyErr_SetObject(PyExc_RuntimeError,
50                 Py_BuildValue("(i,s)", e, ntdb_errorstr(e)));
51 }
52
53 static NTDB_DATA PyString_AsNtdb_Data(PyObject *data)
54 {
55         NTDB_DATA ret;
56         ret.dptr = (unsigned char *)PyString_AsString(data);
57         ret.dsize = PyString_Size(data);
58         return ret;
59 }
60
61 static PyObject *PyString_FromNtdb_Data(NTDB_DATA data)
62 {
63         PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
64                                                    data.dsize);
65         free(data.dptr);
66         return ret;
67 }
68
69 #define PyErr_NTDB_ERROR_IS_ERR_RAISE(ret) \
70         if (ret != NTDB_SUCCESS) { \
71                 PyErr_SetTDBError(ret); \
72                 return NULL; \
73         }
74
75 #define PyNtdb_CHECK_CLOSED(pyobj) \
76         if (pyobj->closed) {\
77                 PyErr_SetObject(PyExc_RuntimeError, \
78                         Py_BuildValue("(i,s)", NTDB_ERR_EINVAL, "database is closed")); \
79                 return NULL; \
80         }
81
82 static void stderr_log(struct ntdb_context *ntdb,
83                        enum ntdb_log_level level,
84                        enum NTDB_ERROR ecode,
85                        const char *message,
86                        void *data)
87 {
88         fprintf(stderr, "%s:%s:%s\n",
89                 ntdb_name(ntdb), ntdb_errorstr(ecode), message);
90 }
91
92 static PyObject *py_ntdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
93 {
94         char *name = NULL;
95         int ntdb_flags = NTDB_DEFAULT, flags = O_RDWR, mode = 0600;
96         struct ntdb_context *ctx;
97         PyNtdbObject *ret;
98         union ntdb_attribute logattr;
99         const char *kwnames[] = { "name", "ntdb_flags", "flags", "mode", NULL };
100
101         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siii", cast_const2(char **, kwnames), &name, &ntdb_flags, &flags, &mode))
102                 return NULL;
103
104         if (name == NULL) {
105                 ntdb_flags |= NTDB_INTERNAL;
106                 name = "<internal>";
107         }
108
109         logattr.log.base.attr = NTDB_ATTRIBUTE_LOG;
110         logattr.log.base.next = NULL;
111         logattr.log.fn = stderr_log;
112         ctx = ntdb_open(name, ntdb_flags, flags, mode, &logattr);
113         if (ctx == NULL) {
114                 PyErr_SetFromErrno(PyExc_IOError);
115                 return NULL;
116         }
117
118         ret = PyObject_New(PyNtdbObject, &PyNtdb);
119         if (!ret) {
120                 ntdb_close(ctx);
121                 return NULL;
122         }
123
124         ret->ctx = ctx;
125         ret->closed = false;
126         return (PyObject *)ret;
127 }
128
129 static PyObject *obj_transaction_cancel(PyNtdbObject *self)
130 {
131         PyNtdb_CHECK_CLOSED(self);
132         ntdb_transaction_cancel(self->ctx);
133         Py_RETURN_NONE;
134 }
135
136 static PyObject *obj_transaction_commit(PyNtdbObject *self)
137 {
138         enum NTDB_ERROR ret;
139         PyNtdb_CHECK_CLOSED(self);
140         ret = ntdb_transaction_commit(self->ctx);
141         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
142         Py_RETURN_NONE;
143 }
144
145 static PyObject *obj_transaction_prepare_commit(PyNtdbObject *self)
146 {
147         enum NTDB_ERROR ret;
148         PyNtdb_CHECK_CLOSED(self);
149         ret = ntdb_transaction_prepare_commit(self->ctx);
150         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
151         Py_RETURN_NONE;
152 }
153
154 static PyObject *obj_transaction_start(PyNtdbObject *self)
155 {
156         enum NTDB_ERROR ret;
157         PyNtdb_CHECK_CLOSED(self);
158         ret = ntdb_transaction_start(self->ctx);
159         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
160         Py_RETURN_NONE;
161 }
162
163 static PyObject *obj_lockall(PyNtdbObject *self)
164 {
165         enum NTDB_ERROR ret;
166         PyNtdb_CHECK_CLOSED(self);
167         ret = ntdb_lockall(self->ctx);
168         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
169         Py_RETURN_NONE;
170 }
171
172 static PyObject *obj_unlockall(PyNtdbObject *self)
173 {
174         PyNtdb_CHECK_CLOSED(self);
175         ntdb_unlockall(self->ctx);
176         Py_RETURN_NONE;
177 }
178
179 static PyObject *obj_lockall_read(PyNtdbObject *self)
180 {
181         enum NTDB_ERROR ret;
182         PyNtdb_CHECK_CLOSED(self);
183         ret = ntdb_lockall_read(self->ctx);
184         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
185         Py_RETURN_NONE;
186 }
187
188 static PyObject *obj_unlockall_read(PyNtdbObject *self)
189 {
190         PyNtdb_CHECK_CLOSED(self);
191         ntdb_unlockall_read(self->ctx);
192         Py_RETURN_NONE;
193 }
194
195 static PyObject *obj_close(PyNtdbObject *self)
196 {
197         int ret;
198         if (self->closed)
199                 Py_RETURN_NONE;
200         ret = ntdb_close(self->ctx);
201         self->closed = true;
202         if (ret != 0) {
203                 PyErr_SetTDBError(NTDB_ERR_IO);
204                 return NULL;
205         }
206         Py_RETURN_NONE;
207 }
208
209 static PyObject *obj_get(PyNtdbObject *self, PyObject *args)
210 {
211         NTDB_DATA key, data;
212         PyObject *py_key;
213         enum NTDB_ERROR ret;
214
215         PyNtdb_CHECK_CLOSED(self);
216
217         if (!PyArg_ParseTuple(args, "O", &py_key))
218                 return NULL;
219
220         key = PyString_AsNtdb_Data(py_key);
221         ret = ntdb_fetch(self->ctx, key, &data);
222         if (ret == NTDB_ERR_NOEXIST)
223                 Py_RETURN_NONE;
224         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
225         return PyString_FromNtdb_Data(data);
226 }
227
228 static PyObject *obj_append(PyNtdbObject *self, PyObject *args)
229 {
230         NTDB_DATA key, data;
231         PyObject *py_key, *py_data;
232         enum NTDB_ERROR ret;
233
234         PyNtdb_CHECK_CLOSED(self);
235
236         if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
237                 return NULL;
238
239         key = PyString_AsNtdb_Data(py_key);
240         data = PyString_AsNtdb_Data(py_data);
241
242         ret = ntdb_append(self->ctx, key, data);
243         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
244         Py_RETURN_NONE;
245 }
246
247 static PyObject *obj_firstkey(PyNtdbObject *self)
248 {
249         enum NTDB_ERROR ret;
250         NTDB_DATA key;
251
252         PyNtdb_CHECK_CLOSED(self);
253
254         ret = ntdb_firstkey(self->ctx, &key);
255         if (ret == NTDB_ERR_NOEXIST)
256                 Py_RETURN_NONE;
257         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
258
259         return PyString_FromNtdb_Data(key);
260 }
261
262 static PyObject *obj_nextkey(PyNtdbObject *self, PyObject *args)
263 {
264         NTDB_DATA key;
265         PyObject *py_key;
266         enum NTDB_ERROR ret;
267
268         PyNtdb_CHECK_CLOSED(self);
269
270         if (!PyArg_ParseTuple(args, "O", &py_key))
271                 return NULL;
272
273         /* Malloc here, since ntdb_nextkey frees. */
274         key.dsize = PyString_Size(py_key);
275         key.dptr = malloc(key.dsize);
276         memcpy(key.dptr, PyString_AsString(py_key), key.dsize);
277
278         ret = ntdb_nextkey(self->ctx, &key);
279         if (ret == NTDB_ERR_NOEXIST)
280                 Py_RETURN_NONE;
281         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
282
283         return PyString_FromNtdb_Data(key);
284 }
285
286 static PyObject *obj_delete(PyNtdbObject *self, PyObject *args)
287 {
288         NTDB_DATA key;
289         PyObject *py_key;
290         enum NTDB_ERROR ret;
291
292         PyNtdb_CHECK_CLOSED(self);
293
294         if (!PyArg_ParseTuple(args, "O", &py_key))
295                 return NULL;
296
297         key = PyString_AsNtdb_Data(py_key);
298         ret = ntdb_delete(self->ctx, key);
299         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
300         Py_RETURN_NONE;
301 }
302
303 static PyObject *obj_has_key(PyNtdbObject *self, PyObject *args)
304 {
305         NTDB_DATA key;
306         PyObject *py_key;
307
308         PyNtdb_CHECK_CLOSED(self);
309
310         if (!PyArg_ParseTuple(args, "O", &py_key))
311                 return NULL;
312
313         key = PyString_AsNtdb_Data(py_key);
314         if (ntdb_exists(self->ctx, key))
315                 return Py_True;
316         return Py_False;
317 }
318
319 static PyObject *obj_store(PyNtdbObject *self, PyObject *args)
320 {
321         NTDB_DATA key, value;
322         enum NTDB_ERROR ret;
323         int flag = NTDB_REPLACE;
324         PyObject *py_key, *py_value;
325         PyNtdb_CHECK_CLOSED(self);
326
327         if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
328                 return NULL;
329
330         key = PyString_AsNtdb_Data(py_key);
331         value = PyString_AsNtdb_Data(py_value);
332
333         ret = ntdb_store(self->ctx, key, value, flag);
334         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
335         Py_RETURN_NONE;
336 }
337
338 static PyObject *obj_add_flag(PyNtdbObject *self, PyObject *args)
339 {
340         unsigned flag;
341         PyNtdb_CHECK_CLOSED(self);
342
343         if (!PyArg_ParseTuple(args, "I", &flag))
344                 return NULL;
345
346         ntdb_add_flag(self->ctx, flag);
347         Py_RETURN_NONE;
348 }
349
350 static PyObject *obj_remove_flag(PyNtdbObject *self, PyObject *args)
351 {
352         unsigned flag;
353
354         PyNtdb_CHECK_CLOSED(self);
355
356         if (!PyArg_ParseTuple(args, "I", &flag))
357                 return NULL;
358
359         ntdb_remove_flag(self->ctx, flag);
360         Py_RETURN_NONE;
361 }
362
363 typedef struct {
364         PyObject_HEAD
365         NTDB_DATA current;
366         bool end;
367         PyNtdbObject *iteratee;
368 } PyNtdbIteratorObject;
369
370 static PyObject *ntdb_iter_next(PyNtdbIteratorObject *self)
371 {
372         enum NTDB_ERROR e;
373         PyObject *ret;
374         if (self->end)
375                 return NULL;
376         ret = PyString_FromStringAndSize((const char *)self->current.dptr,
377                                          self->current.dsize);
378         e = ntdb_nextkey(self->iteratee->ctx, &self->current);
379         if (e == NTDB_ERR_NOEXIST)
380                 self->end = true;
381         else
382                 PyErr_NTDB_ERROR_IS_ERR_RAISE(e);
383         return ret;
384 }
385
386 static void ntdb_iter_dealloc(PyNtdbIteratorObject *self)
387 {
388         Py_DECREF(self->iteratee);
389         PyObject_Del(self);
390 }
391
392 PyTypeObject PyNtdbIterator = {
393         .tp_name = "Iterator",
394         .tp_basicsize = sizeof(PyNtdbIteratorObject),
395         .tp_iternext = (iternextfunc)ntdb_iter_next,
396         .tp_dealloc = (destructor)ntdb_iter_dealloc,
397         .tp_flags = Py_TPFLAGS_DEFAULT,
398         .tp_iter = PyObject_SelfIter,
399 };
400
401 static PyObject *ntdb_object_iter(PyNtdbObject *self)
402 {
403         PyNtdbIteratorObject *ret;
404         enum NTDB_ERROR e;
405         PyNtdb_CHECK_CLOSED(self);
406
407         ret = PyObject_New(PyNtdbIteratorObject, &PyNtdbIterator);
408         if (!ret)
409                 return NULL;
410         e = ntdb_firstkey(self->ctx, &ret->current);
411         if (e == NTDB_ERR_NOEXIST) {
412                 ret->end = true;
413         } else {
414                 PyErr_NTDB_ERROR_IS_ERR_RAISE(e);
415                 ret->end = false;
416         }
417         ret->iteratee = self;
418         Py_INCREF(self);
419         return (PyObject *)ret;
420 }
421
422 static PyObject *obj_clear(PyNtdbObject *self)
423 {
424         enum NTDB_ERROR ret;
425         PyNtdb_CHECK_CLOSED(self);
426         ret = ntdb_wipe_all(self->ctx);
427         PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
428         Py_RETURN_NONE;
429 }
430
431 static PyObject *obj_enable_seqnum(PyNtdbObject *self)
432 {
433         PyNtdb_CHECK_CLOSED(self);
434         ntdb_add_flag(self->ctx, NTDB_SEQNUM);
435         Py_RETURN_NONE;
436 }
437
438 static PyMethodDef ntdb_object_methods[] = {
439         { "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
440                 "S.transaction_cancel() -> None\n"
441                 "Cancel the currently active transaction." },
442         { "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
443                 "S.transaction_commit() -> None\n"
444                 "Commit the currently active transaction." },
445         { "transaction_prepare_commit", (PyCFunction)obj_transaction_prepare_commit, METH_NOARGS,
446                 "S.transaction_prepare_commit() -> None\n"
447                 "Prepare to commit the currently active transaction" },
448         { "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
449                 "S.transaction_start() -> None\n"
450                 "Start a new transaction." },
451         { "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
452         { "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
453         { "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
454         { "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
455         { "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
456         { "get", (PyCFunction)obj_get, METH_VARARGS, "S.get(key) -> value\n"
457                 "Fetch a value." },
458         { "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
459                 "Append data to an existing key." },
460         { "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
461                 "Return the first key in this database." },
462         { "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
463                 "Return the next key in this database." },
464         { "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
465                 "Delete an entry." },
466         { "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
467                 "Check whether key exists in this database." },
468         { "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
469                 "Store data." },
470         { "add_flag", (PyCFunction)obj_add_flag, METH_VARARGS, "S.add_flag(flag) -> None" },
471         { "remove_flag", (PyCFunction)obj_remove_flag, METH_VARARGS, "S.remove_flag(flag) -> None" },
472         { "iterkeys", (PyCFunction)ntdb_object_iter, METH_NOARGS, "S.iterkeys() -> iterator" },
473         { "clear", (PyCFunction)obj_clear, METH_NOARGS, "S.clear() -> None\n"
474                 "Wipe the entire database." },
475         { "enable_seqnum", (PyCFunction)obj_enable_seqnum, METH_NOARGS,
476                 "S.enable_seqnum() -> None" },
477         { NULL }
478 };
479
480 static PyObject *obj_get_flags(PyNtdbObject *self, void *closure)
481 {
482         PyNtdb_CHECK_CLOSED(self);
483         return PyInt_FromLong(ntdb_get_flags(self->ctx));
484 }
485
486 static PyObject *obj_get_filename(PyNtdbObject *self, void *closure)
487 {
488         PyNtdb_CHECK_CLOSED(self);
489         return PyString_FromString(ntdb_name(self->ctx));
490 }
491
492 static PyObject *obj_get_seqnum(PyNtdbObject *self, void *closure)
493 {
494         PyNtdb_CHECK_CLOSED(self);
495         return PyInt_FromLong(ntdb_get_seqnum(self->ctx));
496 }
497
498
499 static PyGetSetDef ntdb_object_getsetters[] = {
500         { cast_const(char *, "flags"), (getter)obj_get_flags, NULL, NULL },
501         { cast_const(char *, "filename"), (getter)obj_get_filename, NULL,
502           cast_const(char *, "The filename of this NTDB file.")},
503         { cast_const(char *, "seqnum"), (getter)obj_get_seqnum, NULL, NULL },
504         { NULL }
505 };
506
507 static PyObject *ntdb_object_repr(PyNtdbObject *self)
508 {
509         if (ntdb_get_flags(self->ctx) & NTDB_INTERNAL) {
510                 return PyString_FromString("Ntdb(<internal>)");
511         } else {
512                 return PyString_FromFormat("Ntdb('%s')", ntdb_name(self->ctx));
513         }
514 }
515
516 static void ntdb_object_dealloc(PyNtdbObject *self)
517 {
518         if (!self->closed)
519                 ntdb_close(self->ctx);
520         self->ob_type->tp_free(self);
521 }
522
523 static PyObject *obj_getitem(PyNtdbObject *self, PyObject *key)
524 {
525         NTDB_DATA tkey, val;
526         enum NTDB_ERROR ret;
527
528         PyNtdb_CHECK_CLOSED(self);
529
530         if (!PyString_Check(key)) {
531                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
532                 return NULL;
533         }
534
535         tkey.dptr = (unsigned char *)PyString_AsString(key);
536         tkey.dsize = PyString_Size(key);
537
538         ret = ntdb_fetch(self->ctx, tkey, &val);
539         if (ret == NTDB_ERR_NOEXIST) {
540                 PyErr_SetString(PyExc_KeyError, "No such NTDB entry");
541                 return NULL;
542         } else {
543                 PyErr_NTDB_ERROR_IS_ERR_RAISE(ret);
544                 return PyString_FromNtdb_Data(val);
545         }
546 }
547
548 static int obj_setitem(PyNtdbObject *self, PyObject *key, PyObject *value)
549 {
550         NTDB_DATA tkey, tval;
551         enum NTDB_ERROR ret;
552         if (self->closed) {
553                 PyErr_SetObject(PyExc_RuntimeError,
554                         Py_BuildValue("(i,s)", NTDB_ERR_EINVAL, "database is closed"));
555                 return -1;
556         }
557
558         if (!PyString_Check(key)) {
559                 PyErr_SetString(PyExc_TypeError, "Expected string as key");
560                 return -1;
561         }
562
563         tkey = PyString_AsNtdb_Data(key);
564
565         if (value == NULL) {
566                 ret = ntdb_delete(self->ctx, tkey);
567         } else {
568                 if (!PyString_Check(value)) {
569                         PyErr_SetString(PyExc_TypeError, "Expected string as value");
570                         return -1;
571                 }
572
573                 tval = PyString_AsNtdb_Data(value);
574
575                 ret = ntdb_store(self->ctx, tkey, tval, NTDB_REPLACE);
576         }
577
578         if (ret != NTDB_SUCCESS) {
579                 PyErr_SetTDBError(ret);
580                 return -1;
581         }
582
583         return ret;
584 }
585
586 static PyMappingMethods ntdb_object_mapping = {
587         .mp_subscript = (binaryfunc)obj_getitem,
588         .mp_ass_subscript = (objobjargproc)obj_setitem,
589 };
590
591 static PyTypeObject PyNtdb = {
592         .tp_name = "ntdb.Ntdb",
593         .tp_basicsize = sizeof(PyNtdbObject),
594         .tp_methods = ntdb_object_methods,
595         .tp_getset = ntdb_object_getsetters,
596         .tp_new = py_ntdb_open,
597         .tp_doc = "A NTDB file",
598         .tp_repr = (reprfunc)ntdb_object_repr,
599         .tp_dealloc = (destructor)ntdb_object_dealloc,
600         .tp_as_mapping = &ntdb_object_mapping,
601         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_ITER,
602         .tp_iter = (getiterfunc)ntdb_object_iter,
603 };
604
605 static PyMethodDef ntdb_methods[] = {
606         { "open", (PyCFunction)py_ntdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, ntdb_flags=NTDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
607                 "Open a NTDB file." },
608         { NULL }
609 };
610
611 void initntdb(void);
612 void initntdb(void)
613 {
614         PyObject *m;
615
616         if (PyType_Ready(&PyNtdb) < 0)
617                 return;
618
619         if (PyType_Ready(&PyNtdbIterator) < 0)
620                 return;
621
622         m = Py_InitModule3("ntdb", ntdb_methods, "NTDB is a simple key-value database similar to GDBM that supports multiple writers.");
623         if (m == NULL)
624                 return;
625
626         PyModule_AddObject(m, "REPLACE", PyInt_FromLong(NTDB_REPLACE));
627         PyModule_AddObject(m, "INSERT", PyInt_FromLong(NTDB_INSERT));
628         PyModule_AddObject(m, "MODIFY", PyInt_FromLong(NTDB_MODIFY));
629
630         PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(NTDB_DEFAULT));
631         PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(NTDB_INTERNAL));
632         PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(NTDB_NOLOCK));
633         PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(NTDB_NOMMAP));
634         PyModule_AddObject(m, "CONVERT", PyInt_FromLong(NTDB_CONVERT));
635         PyModule_AddObject(m, "NOSYNC", PyInt_FromLong(NTDB_NOSYNC));
636         PyModule_AddObject(m, "SEQNUM", PyInt_FromLong(NTDB_SEQNUM));
637         PyModule_AddObject(m, "ALLOW_NESTING", PyInt_FromLong(NTDB_ALLOW_NESTING));
638
639         PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
640
641         PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));
642
643         Py_INCREF(&PyNtdb);
644         PyModule_AddObject(m, "Ntdb", (PyObject *)&PyNtdb);
645
646         Py_INCREF(&PyNtdbIterator);
647 }