blob: 40d06fc26803463d9a1d1aa67b6867715adec060 [file] [log] [blame]
Guido van Rossumdd9ed831992-06-29 17:10:40 +00001
2/* DBM module using dictionary interface */
3
4
Roger E. Masseb0dfe961996-12-10 00:07:00 +00005#include "Python.h"
Guido van Rossumdd9ed831992-06-29 17:10:40 +00006
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
Fred Draked94f7072000-09-14 15:48:06 +000010
11/* Some Linux systems install gdbm/ndbm.h, but not ndbm.h. This supports
12 * whichever configure was able to locate.
13 */
14#if defined(HAVE_NDBM_H)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000015#include <ndbm.h>
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000016#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Fred Drake1ac526d2000-09-15 21:35:14 +000017static char *which_dbm = "ndbm";
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000018#else
19static char *which_dbm = "GNU gdbm"; /* EMX port of GDBM */
20#endif
Fred Draked94f7072000-09-14 15:48:06 +000021#elif defined(HAVE_GDBM_NDBM_H)
22#include <gdbm/ndbm.h>
Fred Drake1ac526d2000-09-15 21:35:14 +000023static char *which_dbm = "GNU gdbm";
Skip Montanaro71ffc5c2002-08-02 17:13:01 +000024#elif defined(HAVE_BERKDB_H)
25#include <db.h>
26static char *which_dbm = "Berkeley DB";
Fred Draked94f7072000-09-14 15:48:06 +000027#else
28#error "No ndbm.h available!"
29#endif
Guido van Rossumdd9ed831992-06-29 17:10:40 +000030
31typedef struct {
Roger E. Masseb0dfe961996-12-10 00:07:00 +000032 PyObject_HEAD
Guido van Rossumdd9ed831992-06-29 17:10:40 +000033 int di_size; /* -1 means recompute */
34 DBM *di_dbm;
35} dbmobject;
36
Jeremy Hylton938ace62002-07-17 16:30:39 +000037static PyTypeObject Dbmtype;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000038
39#define is_dbmobject(v) ((v)->ob_type == &Dbmtype)
Guido van Rossum77eecfa1997-07-17 22:56:01 +000040#define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
41 { PyErr_SetString(DbmError, "DBM object has already been closed"); \
42 return NULL; }
Guido van Rossumdd9ed831992-06-29 17:10:40 +000043
Roger E. Masseb0dfe961996-12-10 00:07:00 +000044static PyObject *DbmError;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000045
Roger E. Masseb0dfe961996-12-10 00:07:00 +000046static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000047newdbmobject(char *file, int flags, int mode)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000048{
49 dbmobject *dp;
50
Guido van Rossumb18618d2000-05-03 23:44:39 +000051 dp = PyObject_New(dbmobject, &Dbmtype);
Guido van Rossumdd9ed831992-06-29 17:10:40 +000052 if (dp == NULL)
53 return NULL;
54 dp->di_size = -1;
55 if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +000056 PyErr_SetFromErrno(DbmError);
57 Py_DECREF(dp);
58 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000059 }
Roger E. Masseb0dfe961996-12-10 00:07:00 +000060 return (PyObject *)dp;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000061}
62
63/* Methods */
64
65static void
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000066dbm_dealloc(register dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000067{
68 if ( dp->di_dbm )
Roger E. Massee7ee8c31996-12-13 15:55:22 +000069 dbm_close(dp->di_dbm);
Guido van Rossumb18618d2000-05-03 23:44:39 +000070 PyObject_Del(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +000071}
72
73static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000074dbm_length(dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000075{
Guido van Rossum77eecfa1997-07-17 22:56:01 +000076 if (dp->di_dbm == NULL) {
77 PyErr_SetString(DbmError, "DBM object has already been closed");
78 return -1;
79 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +000080 if ( dp->di_size < 0 ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +000081 datum key;
82 int size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000083
Roger E. Massee7ee8c31996-12-13 15:55:22 +000084 size = 0;
85 for ( key=dbm_firstkey(dp->di_dbm); key.dptr;
86 key = dbm_nextkey(dp->di_dbm))
87 size++;
88 dp->di_size = size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000089 }
90 return dp->di_size;
91}
92
Roger E. Masseb0dfe961996-12-10 00:07:00 +000093static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000094dbm_subscript(dbmobject *dp, register PyObject *key)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000095{
Guido van Rossumdd9ed831992-06-29 17:10:40 +000096 datum drec, krec;
Neal Norwitz8d4c9812002-11-22 23:29:31 +000097 int tmp_size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000098
Neal Norwitz8d4c9812002-11-22 23:29:31 +000099 if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size) )
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000100 return NULL;
101
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000102 krec.dsize = tmp_size;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000103 check_dbmobject_open(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000104 drec = dbm_fetch(dp->di_dbm, krec);
105 if ( drec.dptr == 0 ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000106 PyErr_SetString(PyExc_KeyError,
107 PyString_AS_STRING((PyStringObject *)key));
108 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000109 }
110 if ( dbm_error(dp->di_dbm) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000111 dbm_clearerr(dp->di_dbm);
112 PyErr_SetString(DbmError, "");
113 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000114 }
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000115 return PyString_FromStringAndSize(drec.dptr, drec.dsize);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000116}
117
118static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000119dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000120{
121 datum krec, drec;
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000122 int tmp_size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000123
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000124 if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000125 PyErr_SetString(PyExc_TypeError,
126 "dbm mappings have string indices only");
127 return -1;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000128 }
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000129 krec.dsize = tmp_size;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000130 if (dp->di_dbm == NULL) {
131 PyErr_SetString(DbmError, "DBM object has already been closed");
132 return -1;
133 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000134 dp->di_size = -1;
135 if (w == NULL) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000136 if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
137 dbm_clearerr(dp->di_dbm);
138 PyErr_SetString(PyExc_KeyError,
Roger E. Massebd4b9611996-12-13 15:59:22 +0000139 PyString_AS_STRING((PyStringObject *)v));
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000140 return -1;
141 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000142 } else {
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000143 if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000144 PyErr_SetString(PyExc_TypeError,
Roger E. Massebd4b9611996-12-13 15:59:22 +0000145 "dbm mappings have string elements only");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000146 return -1;
147 }
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000148 drec.dsize = tmp_size;
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000149 if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
150 dbm_clearerr(dp->di_dbm);
Roger E. Massebd4b9611996-12-13 15:59:22 +0000151 PyErr_SetString(DbmError,
Fred Drake661ea262000-10-24 19:57:45 +0000152 "cannot add item to database");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000153 return -1;
154 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000155 }
156 if ( dbm_error(dp->di_dbm) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000157 dbm_clearerr(dp->di_dbm);
158 PyErr_SetString(DbmError, "");
159 return -1;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000160 }
161 return 0;
162}
163
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000164static PyMappingMethods dbm_as_mapping = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165 (inquiry)dbm_length, /*mp_length*/
166 (binaryfunc)dbm_subscript, /*mp_subscript*/
167 (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000168};
169
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000170static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000171dbm__close(register dbmobject *dp, PyObject *args)
Guido van Rossum807b7be1995-07-07 22:37:11 +0000172{
Fred Drake1ac526d2000-09-15 21:35:14 +0000173 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum807b7be1995-07-07 22:37:11 +0000174 return NULL;
Fred Drake1ac526d2000-09-15 21:35:14 +0000175 if (dp->di_dbm)
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000176 dbm_close(dp->di_dbm);
Guido van Rossum807b7be1995-07-07 22:37:11 +0000177 dp->di_dbm = NULL;
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000178 Py_INCREF(Py_None);
179 return Py_None;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000180}
181
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000182static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000183dbm_keys(register dbmobject *dp, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000184{
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000185 register PyObject *v, *item;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000186 datum key;
Guido van Rossumd5039131995-01-30 12:45:38 +0000187 int err;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000188
Fred Drake1ac526d2000-09-15 21:35:14 +0000189 if (!PyArg_ParseTuple(args, ":keys"))
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000190 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000191 check_dbmobject_open(dp);
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000192 v = PyList_New(0);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000193 if (v == NULL)
194 return NULL;
195 for (key = dbm_firstkey(dp->di_dbm); key.dptr;
Guido van Rossumd5039131995-01-30 12:45:38 +0000196 key = dbm_nextkey(dp->di_dbm)) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000197 item = PyString_FromStringAndSize(key.dptr, key.dsize);
Guido van Rossumd5039131995-01-30 12:45:38 +0000198 if (item == NULL) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000199 Py_DECREF(v);
Guido van Rossumd5039131995-01-30 12:45:38 +0000200 return NULL;
201 }
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000202 err = PyList_Append(v, item);
203 Py_DECREF(item);
Guido van Rossumd5039131995-01-30 12:45:38 +0000204 if (err != 0) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000205 Py_DECREF(v);
Guido van Rossumd5039131995-01-30 12:45:38 +0000206 return NULL;
207 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000208 }
209 return v;
210}
211
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000212static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000213dbm_has_key(register dbmobject *dp, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000214{
215 datum key, val;
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000216 int tmp_size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000217
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000218 if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &tmp_size))
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000219 return NULL;
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000220 key.dsize = tmp_size;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000221 check_dbmobject_open(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000222 val = dbm_fetch(dp->di_dbm, key);
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000223 return PyInt_FromLong(val.dptr != NULL);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000224}
225
Fred Drake1ac526d2000-09-15 21:35:14 +0000226static PyObject *
227dbm_get(register dbmobject *dp, PyObject *args)
228{
229 datum key, val;
230 PyObject *defvalue = Py_None;
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000231 int tmp_size;
Fred Drake1ac526d2000-09-15 21:35:14 +0000232
233 if (!PyArg_ParseTuple(args, "s#|O:get",
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000234 &key.dptr, &tmp_size, &defvalue))
Fred Drake1ac526d2000-09-15 21:35:14 +0000235 return NULL;
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000236 key.dsize = tmp_size;
Fred Drake1ac526d2000-09-15 21:35:14 +0000237 check_dbmobject_open(dp);
238 val = dbm_fetch(dp->di_dbm, key);
239 if (val.dptr != NULL)
240 return PyString_FromStringAndSize(val.dptr, val.dsize);
241 else {
242 Py_INCREF(defvalue);
243 return defvalue;
244 }
245}
246
247static PyObject *
248dbm_setdefault(register dbmobject *dp, PyObject *args)
249{
250 datum key, val;
251 PyObject *defvalue = NULL;
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000252 int tmp_size;
Fred Drake1ac526d2000-09-15 21:35:14 +0000253
254 if (!PyArg_ParseTuple(args, "s#|S:setdefault",
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000255 &key.dptr, &tmp_size, &defvalue))
Fred Drake1ac526d2000-09-15 21:35:14 +0000256 return NULL;
Neal Norwitz8d4c9812002-11-22 23:29:31 +0000257 key.dsize = tmp_size;
Fred Drake1ac526d2000-09-15 21:35:14 +0000258 check_dbmobject_open(dp);
259 val = dbm_fetch(dp->di_dbm, key);
260 if (val.dptr != NULL)
261 return PyString_FromStringAndSize(val.dptr, val.dsize);
262 if (defvalue == NULL) {
263 defvalue = PyString_FromStringAndSize(NULL, 0);
264 if (defvalue == NULL)
265 return NULL;
266 }
267 else
268 Py_INCREF(defvalue);
269 val.dptr = PyString_AS_STRING(defvalue);
270 val.dsize = PyString_GET_SIZE(defvalue);
271 if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) {
272 dbm_clearerr(dp->di_dbm);
Fred Drake661ea262000-10-24 19:57:45 +0000273 PyErr_SetString(DbmError, "cannot add item to database");
Fred Drake1ac526d2000-09-15 21:35:14 +0000274 return NULL;
275 }
276 return defvalue;
277}
278
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000279static PyMethodDef dbm_methods[] = {
Fred Drake1ac526d2000-09-15 21:35:14 +0000280 {"close", (PyCFunction)dbm__close, METH_VARARGS,
281 "close()\nClose the database."},
282 {"keys", (PyCFunction)dbm_keys, METH_VARARGS,
283 "keys() -> list\nReturn a list of all keys in the database."},
284 {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS,
285 "has_key(key} -> boolean\nReturn true iff key is in the database."},
286 {"get", (PyCFunction)dbm_get, METH_VARARGS,
287 "get(key[, default]) -> value\n"
288 "Return the value for key if present, otherwise default."},
289 {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
290 "setdefault(key[, default]) -> value\n"
291 "Return the value for key if present, otherwise default. If key\n"
292 "is not in the database, it is inserted with default as the value."},
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000293 {NULL, NULL} /* sentinel */
294};
295
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000296static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000297dbm_getattr(dbmobject *dp, char *name)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000298{
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000299 return Py_FindMethod(dbm_methods, (PyObject *)dp, name);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000300}
301
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000302static PyTypeObject Dbmtype = {
Guido van Rossuma120ffc2001-01-22 15:29:14 +0000303 PyObject_HEAD_INIT(NULL)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000304 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000305 "dbm.dbm",
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000306 sizeof(dbmobject),
307 0,
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000308 (destructor)dbm_dealloc, /*tp_dealloc*/
309 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000310 (getattrfunc)dbm_getattr, /*tp_getattr*/
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000311 0, /*tp_setattr*/
312 0, /*tp_compare*/
313 0, /*tp_repr*/
314 0, /*tp_as_number*/
315 0, /*tp_as_sequence*/
316 &dbm_as_mapping, /*tp_as_mapping*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000317};
318
319/* ----------------------------------------------------------------- */
320
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000321static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000322dbmopen(PyObject *self, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000323{
Guido van Rossum807b7be1995-07-07 22:37:11 +0000324 char *name;
325 char *flags = "r";
326 int iflags;
327 int mode = 0666;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000328
Guido van Rossum43713e52000-02-29 13:59:29 +0000329 if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000330 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000331 if ( strcmp(flags, "r") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000332 iflags = O_RDONLY;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000333 else if ( strcmp(flags, "w") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000334 iflags = O_RDWR;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000335 else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000336 iflags = O_RDWR|O_CREAT;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000337 else if ( strcmp(flags, "c") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000338 iflags = O_RDWR|O_CREAT;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000339 else if ( strcmp(flags, "n") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000340 iflags = O_RDWR|O_CREAT|O_TRUNC;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000341 else {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000342 PyErr_SetString(DbmError,
Fred Drake661ea262000-10-24 19:57:45 +0000343 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000344 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000345 }
346 return newdbmobject(name, iflags, mode);
347}
348
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000349static PyMethodDef dbmmodule_methods[] = {
Fred Drake1ac526d2000-09-15 21:35:14 +0000350 { "open", (PyCFunction)dbmopen, METH_VARARGS,
351 "open(path[, flag[, mode]]) -> mapping\n"
352 "Return a database object."},
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000353 { 0, 0 },
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000354};
355
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000356PyMODINIT_FUNC
Thomas Wouters58d05102000-07-24 14:43:35 +0000357initdbm(void) {
Fred Drake1ac526d2000-09-15 21:35:14 +0000358 PyObject *m, *d, *s;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000359
Guido van Rossuma120ffc2001-01-22 15:29:14 +0000360 Dbmtype.ob_type = &PyType_Type;
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000361 m = Py_InitModule("dbm", dbmmodule_methods);
362 d = PyModule_GetDict(m);
Fred Drake1ac526d2000-09-15 21:35:14 +0000363 if (DbmError == NULL)
364 DbmError = PyErr_NewException("dbm.error", NULL, NULL);
365 s = PyString_FromString(which_dbm);
366 if (s != NULL) {
367 PyDict_SetItemString(d, "library", s);
368 Py_DECREF(s);
369 }
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000370 if (DbmError != NULL)
371 PyDict_SetItemString(d, "error", DbmError);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000372}