blob: f9ea7d08c90c3578251c76ef36128e1cf5e97811 [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;
97
Roger E. Masseb0dfe961996-12-10 00:07:00 +000098 if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) )
Guido van Rossumdd9ed831992-06-29 17:10:40 +000099 return NULL;
100
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000101 check_dbmobject_open(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000102 drec = dbm_fetch(dp->di_dbm, krec);
103 if ( drec.dptr == 0 ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000104 PyErr_SetString(PyExc_KeyError,
105 PyString_AS_STRING((PyStringObject *)key));
106 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000107 }
108 if ( dbm_error(dp->di_dbm) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000109 dbm_clearerr(dp->di_dbm);
110 PyErr_SetString(DbmError, "");
111 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000112 }
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000113 return PyString_FromStringAndSize(drec.dptr, drec.dsize);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000114}
115
116static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000117dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000118{
119 datum krec, drec;
120
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000121 if ( !PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000122 PyErr_SetString(PyExc_TypeError,
123 "dbm mappings have string indices only");
124 return -1;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000125 }
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000126 if (dp->di_dbm == NULL) {
127 PyErr_SetString(DbmError, "DBM object has already been closed");
128 return -1;
129 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000130 dp->di_size = -1;
131 if (w == NULL) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000132 if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
133 dbm_clearerr(dp->di_dbm);
134 PyErr_SetString(PyExc_KeyError,
Roger E. Massebd4b9611996-12-13 15:59:22 +0000135 PyString_AS_STRING((PyStringObject *)v));
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000136 return -1;
137 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000138 } else {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000139 if ( !PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize) ) {
140 PyErr_SetString(PyExc_TypeError,
Roger E. Massebd4b9611996-12-13 15:59:22 +0000141 "dbm mappings have string elements only");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000142 return -1;
143 }
144 if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
145 dbm_clearerr(dp->di_dbm);
Roger E. Massebd4b9611996-12-13 15:59:22 +0000146 PyErr_SetString(DbmError,
Fred Drake661ea262000-10-24 19:57:45 +0000147 "cannot add item to database");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000148 return -1;
149 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000150 }
151 if ( dbm_error(dp->di_dbm) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000152 dbm_clearerr(dp->di_dbm);
153 PyErr_SetString(DbmError, "");
154 return -1;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000155 }
156 return 0;
157}
158
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000159static PyMappingMethods dbm_as_mapping = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000160 (inquiry)dbm_length, /*mp_length*/
161 (binaryfunc)dbm_subscript, /*mp_subscript*/
162 (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000163};
164
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000165static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000166dbm__close(register dbmobject *dp, PyObject *args)
Guido van Rossum807b7be1995-07-07 22:37:11 +0000167{
Fred Drake1ac526d2000-09-15 21:35:14 +0000168 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum807b7be1995-07-07 22:37:11 +0000169 return NULL;
Fred Drake1ac526d2000-09-15 21:35:14 +0000170 if (dp->di_dbm)
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000171 dbm_close(dp->di_dbm);
Guido van Rossum807b7be1995-07-07 22:37:11 +0000172 dp->di_dbm = NULL;
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000173 Py_INCREF(Py_None);
174 return Py_None;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000175}
176
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000177static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000178dbm_keys(register dbmobject *dp, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000179{
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000180 register PyObject *v, *item;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000181 datum key;
Guido van Rossumd5039131995-01-30 12:45:38 +0000182 int err;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000183
Fred Drake1ac526d2000-09-15 21:35:14 +0000184 if (!PyArg_ParseTuple(args, ":keys"))
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000185 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000186 check_dbmobject_open(dp);
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000187 v = PyList_New(0);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000188 if (v == NULL)
189 return NULL;
190 for (key = dbm_firstkey(dp->di_dbm); key.dptr;
Guido van Rossumd5039131995-01-30 12:45:38 +0000191 key = dbm_nextkey(dp->di_dbm)) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000192 item = PyString_FromStringAndSize(key.dptr, key.dsize);
Guido van Rossumd5039131995-01-30 12:45:38 +0000193 if (item == NULL) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000194 Py_DECREF(v);
Guido van Rossumd5039131995-01-30 12:45:38 +0000195 return NULL;
196 }
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000197 err = PyList_Append(v, item);
198 Py_DECREF(item);
Guido van Rossumd5039131995-01-30 12:45:38 +0000199 if (err != 0) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000200 Py_DECREF(v);
Guido van Rossumd5039131995-01-30 12:45:38 +0000201 return NULL;
202 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000203 }
204 return v;
205}
206
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000207static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000208dbm_has_key(register dbmobject *dp, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000209{
210 datum key, val;
211
Fred Drake1ac526d2000-09-15 21:35:14 +0000212 if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &key.dsize))
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000213 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000214 check_dbmobject_open(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000215 val = dbm_fetch(dp->di_dbm, key);
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000216 return PyInt_FromLong(val.dptr != NULL);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000217}
218
Fred Drake1ac526d2000-09-15 21:35:14 +0000219static PyObject *
220dbm_get(register dbmobject *dp, PyObject *args)
221{
222 datum key, val;
223 PyObject *defvalue = Py_None;
224
225 if (!PyArg_ParseTuple(args, "s#|O:get",
226 &key.dptr, &key.dsize, &defvalue))
227 return NULL;
228 check_dbmobject_open(dp);
229 val = dbm_fetch(dp->di_dbm, key);
230 if (val.dptr != NULL)
231 return PyString_FromStringAndSize(val.dptr, val.dsize);
232 else {
233 Py_INCREF(defvalue);
234 return defvalue;
235 }
236}
237
238static PyObject *
239dbm_setdefault(register dbmobject *dp, PyObject *args)
240{
241 datum key, val;
242 PyObject *defvalue = NULL;
243
244 if (!PyArg_ParseTuple(args, "s#|S:setdefault",
245 &key.dptr, &key.dsize, &defvalue))
246 return NULL;
247 check_dbmobject_open(dp);
248 val = dbm_fetch(dp->di_dbm, key);
249 if (val.dptr != NULL)
250 return PyString_FromStringAndSize(val.dptr, val.dsize);
251 if (defvalue == NULL) {
252 defvalue = PyString_FromStringAndSize(NULL, 0);
253 if (defvalue == NULL)
254 return NULL;
255 }
256 else
257 Py_INCREF(defvalue);
258 val.dptr = PyString_AS_STRING(defvalue);
259 val.dsize = PyString_GET_SIZE(defvalue);
260 if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) {
261 dbm_clearerr(dp->di_dbm);
Fred Drake661ea262000-10-24 19:57:45 +0000262 PyErr_SetString(DbmError, "cannot add item to database");
Fred Drake1ac526d2000-09-15 21:35:14 +0000263 return NULL;
264 }
265 return defvalue;
266}
267
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000268static PyMethodDef dbm_methods[] = {
Fred Drake1ac526d2000-09-15 21:35:14 +0000269 {"close", (PyCFunction)dbm__close, METH_VARARGS,
270 "close()\nClose the database."},
271 {"keys", (PyCFunction)dbm_keys, METH_VARARGS,
272 "keys() -> list\nReturn a list of all keys in the database."},
273 {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS,
274 "has_key(key} -> boolean\nReturn true iff key is in the database."},
275 {"get", (PyCFunction)dbm_get, METH_VARARGS,
276 "get(key[, default]) -> value\n"
277 "Return the value for key if present, otherwise default."},
278 {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
279 "setdefault(key[, default]) -> value\n"
280 "Return the value for key if present, otherwise default. If key\n"
281 "is not in the database, it is inserted with default as the value."},
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000282 {NULL, NULL} /* sentinel */
283};
284
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000285static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000286dbm_getattr(dbmobject *dp, char *name)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000287{
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000288 return Py_FindMethod(dbm_methods, (PyObject *)dp, name);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000289}
290
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000291static PyTypeObject Dbmtype = {
Guido van Rossuma120ffc2001-01-22 15:29:14 +0000292 PyObject_HEAD_INIT(NULL)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000293 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000294 "dbm.dbm",
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000295 sizeof(dbmobject),
296 0,
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000297 (destructor)dbm_dealloc, /*tp_dealloc*/
298 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299 (getattrfunc)dbm_getattr, /*tp_getattr*/
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000300 0, /*tp_setattr*/
301 0, /*tp_compare*/
302 0, /*tp_repr*/
303 0, /*tp_as_number*/
304 0, /*tp_as_sequence*/
305 &dbm_as_mapping, /*tp_as_mapping*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000306};
307
308/* ----------------------------------------------------------------- */
309
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000310static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000311dbmopen(PyObject *self, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000312{
Guido van Rossum807b7be1995-07-07 22:37:11 +0000313 char *name;
314 char *flags = "r";
315 int iflags;
316 int mode = 0666;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000317
Guido van Rossum43713e52000-02-29 13:59:29 +0000318 if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000319 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000320 if ( strcmp(flags, "r") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000321 iflags = O_RDONLY;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000322 else if ( strcmp(flags, "w") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000323 iflags = O_RDWR;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000324 else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000325 iflags = O_RDWR|O_CREAT;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000326 else if ( strcmp(flags, "c") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000327 iflags = O_RDWR|O_CREAT;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000328 else if ( strcmp(flags, "n") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000329 iflags = O_RDWR|O_CREAT|O_TRUNC;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000330 else {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000331 PyErr_SetString(DbmError,
Fred Drake661ea262000-10-24 19:57:45 +0000332 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000333 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000334 }
335 return newdbmobject(name, iflags, mode);
336}
337
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000338static PyMethodDef dbmmodule_methods[] = {
Fred Drake1ac526d2000-09-15 21:35:14 +0000339 { "open", (PyCFunction)dbmopen, METH_VARARGS,
340 "open(path[, flag[, mode]]) -> mapping\n"
341 "Return a database object."},
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000342 { 0, 0 },
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000343};
344
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000345PyMODINIT_FUNC
Thomas Wouters58d05102000-07-24 14:43:35 +0000346initdbm(void) {
Fred Drake1ac526d2000-09-15 21:35:14 +0000347 PyObject *m, *d, *s;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000348
Guido van Rossuma120ffc2001-01-22 15:29:14 +0000349 Dbmtype.ob_type = &PyType_Type;
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000350 m = Py_InitModule("dbm", dbmmodule_methods);
351 d = PyModule_GetDict(m);
Fred Drake1ac526d2000-09-15 21:35:14 +0000352 if (DbmError == NULL)
353 DbmError = PyErr_NewException("dbm.error", NULL, NULL);
354 s = PyString_FromString(which_dbm);
355 if (s != NULL) {
356 PyDict_SetItemString(d, "library", s);
357 Py_DECREF(s);
358 }
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000359 if (DbmError != NULL)
360 PyDict_SetItemString(d, "error", DbmError);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000361}