blob: 233487d6122462669997fa8b7da97b98f270777e [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>
Fred Drake1ac526d2000-09-15 21:35:14 +000016static char *which_dbm = "ndbm";
Fred Drakeae90f8d2000-09-15 03:38:12 +000017#elif defined(HAVE_DB1_NDBM_H)
18#include <db1/ndbm.h>
Fred Drake1ac526d2000-09-15 21:35:14 +000019static char *which_dbm = "BSD db";
Fred Draked94f7072000-09-14 15:48:06 +000020#elif defined(HAVE_GDBM_NDBM_H)
21#include <gdbm/ndbm.h>
Fred Drake1ac526d2000-09-15 21:35:14 +000022static char *which_dbm = "GNU gdbm";
Fred Draked94f7072000-09-14 15:48:06 +000023#else
24#error "No ndbm.h available!"
25#endif
Guido van Rossumdd9ed831992-06-29 17:10:40 +000026
27typedef struct {
Roger E. Masseb0dfe961996-12-10 00:07:00 +000028 PyObject_HEAD
Guido van Rossumdd9ed831992-06-29 17:10:40 +000029 int di_size; /* -1 means recompute */
30 DBM *di_dbm;
31} dbmobject;
32
Roger E. Masseb0dfe961996-12-10 00:07:00 +000033staticforward PyTypeObject Dbmtype;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000034
35#define is_dbmobject(v) ((v)->ob_type == &Dbmtype)
Guido van Rossum77eecfa1997-07-17 22:56:01 +000036#define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
37 { PyErr_SetString(DbmError, "DBM object has already been closed"); \
38 return NULL; }
Guido van Rossumdd9ed831992-06-29 17:10:40 +000039
Roger E. Masseb0dfe961996-12-10 00:07:00 +000040static PyObject *DbmError;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000041
Roger E. Masseb0dfe961996-12-10 00:07:00 +000042static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000043newdbmobject(char *file, int flags, int mode)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000044{
45 dbmobject *dp;
46
Guido van Rossumb18618d2000-05-03 23:44:39 +000047 dp = PyObject_New(dbmobject, &Dbmtype);
Guido van Rossumdd9ed831992-06-29 17:10:40 +000048 if (dp == NULL)
49 return NULL;
50 dp->di_size = -1;
51 if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +000052 PyErr_SetFromErrno(DbmError);
53 Py_DECREF(dp);
54 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000055 }
Roger E. Masseb0dfe961996-12-10 00:07:00 +000056 return (PyObject *)dp;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000057}
58
59/* Methods */
60
61static void
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000062dbm_dealloc(register dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000063{
64 if ( dp->di_dbm )
Roger E. Massee7ee8c31996-12-13 15:55:22 +000065 dbm_close(dp->di_dbm);
Guido van Rossumb18618d2000-05-03 23:44:39 +000066 PyObject_Del(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +000067}
68
69static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000070dbm_length(dbmobject *dp)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000071{
Guido van Rossum77eecfa1997-07-17 22:56:01 +000072 if (dp->di_dbm == NULL) {
73 PyErr_SetString(DbmError, "DBM object has already been closed");
74 return -1;
75 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +000076 if ( dp->di_size < 0 ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +000077 datum key;
78 int size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000079
Roger E. Massee7ee8c31996-12-13 15:55:22 +000080 size = 0;
81 for ( key=dbm_firstkey(dp->di_dbm); key.dptr;
82 key = dbm_nextkey(dp->di_dbm))
83 size++;
84 dp->di_size = size;
Guido van Rossumdd9ed831992-06-29 17:10:40 +000085 }
86 return dp->di_size;
87}
88
Roger E. Masseb0dfe961996-12-10 00:07:00 +000089static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +000090dbm_subscript(dbmobject *dp, register PyObject *key)
Guido van Rossumdd9ed831992-06-29 17:10:40 +000091{
Guido van Rossumdd9ed831992-06-29 17:10:40 +000092 datum drec, krec;
93
Roger E. Masseb0dfe961996-12-10 00:07:00 +000094 if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) )
Guido van Rossumdd9ed831992-06-29 17:10:40 +000095 return NULL;
96
Guido van Rossum77eecfa1997-07-17 22:56:01 +000097 check_dbmobject_open(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +000098 drec = dbm_fetch(dp->di_dbm, krec);
99 if ( drec.dptr == 0 ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000100 PyErr_SetString(PyExc_KeyError,
101 PyString_AS_STRING((PyStringObject *)key));
102 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000103 }
104 if ( dbm_error(dp->di_dbm) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000105 dbm_clearerr(dp->di_dbm);
106 PyErr_SetString(DbmError, "");
107 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000108 }
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000109 return PyString_FromStringAndSize(drec.dptr, drec.dsize);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000110}
111
112static int
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000113dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000114{
115 datum krec, drec;
116
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000117 if ( !PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000118 PyErr_SetString(PyExc_TypeError,
119 "dbm mappings have string indices only");
120 return -1;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000121 }
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000122 if (dp->di_dbm == NULL) {
123 PyErr_SetString(DbmError, "DBM object has already been closed");
124 return -1;
125 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000126 dp->di_size = -1;
127 if (w == NULL) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000128 if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
129 dbm_clearerr(dp->di_dbm);
130 PyErr_SetString(PyExc_KeyError,
Roger E. Massebd4b9611996-12-13 15:59:22 +0000131 PyString_AS_STRING((PyStringObject *)v));
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000132 return -1;
133 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000134 } else {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000135 if ( !PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize) ) {
136 PyErr_SetString(PyExc_TypeError,
Roger E. Massebd4b9611996-12-13 15:59:22 +0000137 "dbm mappings have string elements only");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000138 return -1;
139 }
140 if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
141 dbm_clearerr(dp->di_dbm);
Roger E. Massebd4b9611996-12-13 15:59:22 +0000142 PyErr_SetString(DbmError,
Fred Drake661ea262000-10-24 19:57:45 +0000143 "cannot add item to database");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000144 return -1;
145 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000146 }
147 if ( dbm_error(dp->di_dbm) ) {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000148 dbm_clearerr(dp->di_dbm);
149 PyErr_SetString(DbmError, "");
150 return -1;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000151 }
152 return 0;
153}
154
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000155static PyMappingMethods dbm_as_mapping = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156 (inquiry)dbm_length, /*mp_length*/
157 (binaryfunc)dbm_subscript, /*mp_subscript*/
158 (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000159};
160
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000161static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000162dbm__close(register dbmobject *dp, PyObject *args)
Guido van Rossum807b7be1995-07-07 22:37:11 +0000163{
Fred Drake1ac526d2000-09-15 21:35:14 +0000164 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum807b7be1995-07-07 22:37:11 +0000165 return NULL;
Fred Drake1ac526d2000-09-15 21:35:14 +0000166 if (dp->di_dbm)
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000167 dbm_close(dp->di_dbm);
Guido van Rossum807b7be1995-07-07 22:37:11 +0000168 dp->di_dbm = NULL;
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000169 Py_INCREF(Py_None);
170 return Py_None;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000171}
172
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000173static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000174dbm_keys(register dbmobject *dp, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000175{
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000176 register PyObject *v, *item;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000177 datum key;
Guido van Rossumd5039131995-01-30 12:45:38 +0000178 int err;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000179
Fred Drake1ac526d2000-09-15 21:35:14 +0000180 if (!PyArg_ParseTuple(args, ":keys"))
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000181 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000182 check_dbmobject_open(dp);
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000183 v = PyList_New(0);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000184 if (v == NULL)
185 return NULL;
186 for (key = dbm_firstkey(dp->di_dbm); key.dptr;
Guido van Rossumd5039131995-01-30 12:45:38 +0000187 key = dbm_nextkey(dp->di_dbm)) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000188 item = PyString_FromStringAndSize(key.dptr, key.dsize);
Guido van Rossumd5039131995-01-30 12:45:38 +0000189 if (item == NULL) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000190 Py_DECREF(v);
Guido van Rossumd5039131995-01-30 12:45:38 +0000191 return NULL;
192 }
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000193 err = PyList_Append(v, item);
194 Py_DECREF(item);
Guido van Rossumd5039131995-01-30 12:45:38 +0000195 if (err != 0) {
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000196 Py_DECREF(v);
Guido van Rossumd5039131995-01-30 12:45:38 +0000197 return NULL;
198 }
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000199 }
200 return v;
201}
202
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000203static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000204dbm_has_key(register dbmobject *dp, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000205{
206 datum key, val;
207
Fred Drake1ac526d2000-09-15 21:35:14 +0000208 if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &key.dsize))
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000209 return NULL;
Guido van Rossum77eecfa1997-07-17 22:56:01 +0000210 check_dbmobject_open(dp);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000211 val = dbm_fetch(dp->di_dbm, key);
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000212 return PyInt_FromLong(val.dptr != NULL);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000213}
214
Fred Drake1ac526d2000-09-15 21:35:14 +0000215static PyObject *
216dbm_get(register dbmobject *dp, PyObject *args)
217{
218 datum key, val;
219 PyObject *defvalue = Py_None;
220
221 if (!PyArg_ParseTuple(args, "s#|O:get",
222 &key.dptr, &key.dsize, &defvalue))
223 return NULL;
224 check_dbmobject_open(dp);
225 val = dbm_fetch(dp->di_dbm, key);
226 if (val.dptr != NULL)
227 return PyString_FromStringAndSize(val.dptr, val.dsize);
228 else {
229 Py_INCREF(defvalue);
230 return defvalue;
231 }
232}
233
234static PyObject *
235dbm_setdefault(register dbmobject *dp, PyObject *args)
236{
237 datum key, val;
238 PyObject *defvalue = NULL;
239
240 if (!PyArg_ParseTuple(args, "s#|S:setdefault",
241 &key.dptr, &key.dsize, &defvalue))
242 return NULL;
243 check_dbmobject_open(dp);
244 val = dbm_fetch(dp->di_dbm, key);
245 if (val.dptr != NULL)
246 return PyString_FromStringAndSize(val.dptr, val.dsize);
247 if (defvalue == NULL) {
248 defvalue = PyString_FromStringAndSize(NULL, 0);
249 if (defvalue == NULL)
250 return NULL;
251 }
252 else
253 Py_INCREF(defvalue);
254 val.dptr = PyString_AS_STRING(defvalue);
255 val.dsize = PyString_GET_SIZE(defvalue);
256 if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) {
257 dbm_clearerr(dp->di_dbm);
Fred Drake661ea262000-10-24 19:57:45 +0000258 PyErr_SetString(DbmError, "cannot add item to database");
Fred Drake1ac526d2000-09-15 21:35:14 +0000259 return NULL;
260 }
261 return defvalue;
262}
263
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000264static PyMethodDef dbm_methods[] = {
Fred Drake1ac526d2000-09-15 21:35:14 +0000265 {"close", (PyCFunction)dbm__close, METH_VARARGS,
266 "close()\nClose the database."},
267 {"keys", (PyCFunction)dbm_keys, METH_VARARGS,
268 "keys() -> list\nReturn a list of all keys in the database."},
269 {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS,
270 "has_key(key} -> boolean\nReturn true iff key is in the database."},
271 {"get", (PyCFunction)dbm_get, METH_VARARGS,
272 "get(key[, default]) -> value\n"
273 "Return the value for key if present, otherwise default."},
274 {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
275 "setdefault(key[, default]) -> value\n"
276 "Return the value for key if present, otherwise default. If key\n"
277 "is not in the database, it is inserted with default as the value."},
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000278 {NULL, NULL} /* sentinel */
279};
280
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000281static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000282dbm_getattr(dbmobject *dp, char *name)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000283{
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000284 return Py_FindMethod(dbm_methods, (PyObject *)dp, name);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000285}
286
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000287static PyTypeObject Dbmtype = {
Guido van Rossuma120ffc2001-01-22 15:29:14 +0000288 PyObject_HEAD_INIT(NULL)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000289 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000290 "dbm.dbm",
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000291 sizeof(dbmobject),
292 0,
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000293 (destructor)dbm_dealloc, /*tp_dealloc*/
294 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295 (getattrfunc)dbm_getattr, /*tp_getattr*/
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000296 0, /*tp_setattr*/
297 0, /*tp_compare*/
298 0, /*tp_repr*/
299 0, /*tp_as_number*/
300 0, /*tp_as_sequence*/
301 &dbm_as_mapping, /*tp_as_mapping*/
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000302};
303
304/* ----------------------------------------------------------------- */
305
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000306static PyObject *
Peter Schneider-Kampcb27c352000-07-10 17:06:38 +0000307dbmopen(PyObject *self, PyObject *args)
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000308{
Guido van Rossum807b7be1995-07-07 22:37:11 +0000309 char *name;
310 char *flags = "r";
311 int iflags;
312 int mode = 0666;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000313
Guido van Rossum43713e52000-02-29 13:59:29 +0000314 if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000315 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000316 if ( strcmp(flags, "r") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000317 iflags = O_RDONLY;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000318 else if ( strcmp(flags, "w") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000319 iflags = O_RDWR;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000320 else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000321 iflags = O_RDWR|O_CREAT;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000322 else if ( strcmp(flags, "c") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000323 iflags = O_RDWR|O_CREAT;
Guido van Rossum807b7be1995-07-07 22:37:11 +0000324 else if ( strcmp(flags, "n") == 0 )
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000325 iflags = O_RDWR|O_CREAT|O_TRUNC;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000326 else {
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000327 PyErr_SetString(DbmError,
Fred Drake661ea262000-10-24 19:57:45 +0000328 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000329 return NULL;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000330 }
331 return newdbmobject(name, iflags, mode);
332}
333
Roger E. Masseb0dfe961996-12-10 00:07:00 +0000334static PyMethodDef dbmmodule_methods[] = {
Fred Drake1ac526d2000-09-15 21:35:14 +0000335 { "open", (PyCFunction)dbmopen, METH_VARARGS,
336 "open(path[, flag[, mode]]) -> mapping\n"
337 "Return a database object."},
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000338 { 0, 0 },
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000339};
340
Guido van Rossum3886bb61998-12-04 18:50:17 +0000341DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +0000342initdbm(void) {
Fred Drake1ac526d2000-09-15 21:35:14 +0000343 PyObject *m, *d, *s;
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000344
Guido van Rossuma120ffc2001-01-22 15:29:14 +0000345 Dbmtype.ob_type = &PyType_Type;
Roger E. Massee7ee8c31996-12-13 15:55:22 +0000346 m = Py_InitModule("dbm", dbmmodule_methods);
347 d = PyModule_GetDict(m);
Fred Drake1ac526d2000-09-15 21:35:14 +0000348 if (DbmError == NULL)
349 DbmError = PyErr_NewException("dbm.error", NULL, NULL);
350 s = PyString_FromString(which_dbm);
351 if (s != NULL) {
352 PyDict_SetItemString(d, "library", s);
353 Py_DECREF(s);
354 }
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000355 if (DbmError != NULL)
356 PyDict_SetItemString(d, "error", DbmError);
Guido van Rossumdd9ed831992-06-29 17:10:40 +0000357}