2 /* DBM module using dictionary interface */
11 /* Some Linux systems install gdbm/ndbm.h, but not ndbm.h. This supports
12 * whichever configure was able to locate.
14 #if defined(HAVE_NDBM_H)
16 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
17 static char *which_dbm
= "ndbm";
19 static char *which_dbm
= "GNU gdbm"; /* EMX port of GDBM */
21 #elif defined(HAVE_GDBM_NDBM_H)
22 #include <gdbm/ndbm.h>
23 static char *which_dbm
= "GNU gdbm";
24 #elif defined(HAVE_BERKDB_H)
26 static char *which_dbm
= "Berkeley DB";
28 #error "No ndbm.h available!"
33 int di_size
; /* -1 means recompute */
37 static PyTypeObject Dbmtype
;
39 #define is_dbmobject(v) ((v)->ob_type == &Dbmtype)
40 #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
41 { PyErr_SetString(DbmError, "DBM object has already been closed"); \
44 static PyObject
*DbmError
;
47 newdbmobject(char *file
, int flags
, int mode
)
51 dp
= PyObject_New(dbmobject
, &Dbmtype
);
55 if ( (dp
->di_dbm
= dbm_open(file
, flags
, mode
)) == 0 ) {
56 PyErr_SetFromErrno(DbmError
);
60 return (PyObject
*)dp
;
66 dbm_dealloc(register dbmobject
*dp
)
69 dbm_close(dp
->di_dbm
);
74 dbm_length(dbmobject
*dp
)
76 if (dp
->di_dbm
== NULL
) {
77 PyErr_SetString(DbmError
, "DBM object has already been closed");
80 if ( dp
->di_size
< 0 ) {
85 for ( key
=dbm_firstkey(dp
->di_dbm
); key
.dptr
;
86 key
= dbm_nextkey(dp
->di_dbm
))
94 dbm_subscript(dbmobject
*dp
, register PyObject
*key
)
99 if (!PyArg_Parse(key
, "s#", &krec
.dptr
, &tmp_size
) )
102 krec
.dsize
= tmp_size
;
103 check_dbmobject_open(dp
);
104 drec
= dbm_fetch(dp
->di_dbm
, krec
);
105 if ( drec
.dptr
== 0 ) {
106 PyErr_SetString(PyExc_KeyError
,
107 PyString_AS_STRING((PyStringObject
*)key
));
110 if ( dbm_error(dp
->di_dbm
) ) {
111 dbm_clearerr(dp
->di_dbm
);
112 PyErr_SetString(DbmError
, "");
115 return PyString_FromStringAndSize(drec
.dptr
, drec
.dsize
);
119 dbm_ass_sub(dbmobject
*dp
, PyObject
*v
, PyObject
*w
)
124 if ( !PyArg_Parse(v
, "s#", &krec
.dptr
, &tmp_size
) ) {
125 PyErr_SetString(PyExc_TypeError
,
126 "dbm mappings have string indices only");
129 krec
.dsize
= tmp_size
;
130 if (dp
->di_dbm
== NULL
) {
131 PyErr_SetString(DbmError
, "DBM object has already been closed");
136 if ( dbm_delete(dp
->di_dbm
, krec
) < 0 ) {
137 dbm_clearerr(dp
->di_dbm
);
138 PyErr_SetString(PyExc_KeyError
,
139 PyString_AS_STRING((PyStringObject
*)v
));
143 if ( !PyArg_Parse(w
, "s#", &drec
.dptr
, &tmp_size
) ) {
144 PyErr_SetString(PyExc_TypeError
,
145 "dbm mappings have string elements only");
148 drec
.dsize
= tmp_size
;
149 if ( dbm_store(dp
->di_dbm
, krec
, drec
, DBM_REPLACE
) < 0 ) {
150 dbm_clearerr(dp
->di_dbm
);
151 PyErr_SetString(DbmError
,
152 "cannot add item to database");
156 if ( dbm_error(dp
->di_dbm
) ) {
157 dbm_clearerr(dp
->di_dbm
);
158 PyErr_SetString(DbmError
, "");
164 static PyMappingMethods dbm_as_mapping
= {
165 (inquiry
)dbm_length
, /*mp_length*/
166 (binaryfunc
)dbm_subscript
, /*mp_subscript*/
167 (objobjargproc
)dbm_ass_sub
, /*mp_ass_subscript*/
171 dbm__close(register dbmobject
*dp
, PyObject
*args
)
173 if (!PyArg_ParseTuple(args
, ":close"))
176 dbm_close(dp
->di_dbm
);
183 dbm_keys(register dbmobject
*dp
, PyObject
*args
)
185 register PyObject
*v
, *item
;
189 if (!PyArg_ParseTuple(args
, ":keys"))
191 check_dbmobject_open(dp
);
195 for (key
= dbm_firstkey(dp
->di_dbm
); key
.dptr
;
196 key
= dbm_nextkey(dp
->di_dbm
)) {
197 item
= PyString_FromStringAndSize(key
.dptr
, key
.dsize
);
202 err
= PyList_Append(v
, item
);
213 dbm_has_key(register dbmobject
*dp
, PyObject
*args
)
218 if (!PyArg_ParseTuple(args
, "s#:has_key", &key
.dptr
, &tmp_size
))
220 key
.dsize
= tmp_size
;
221 check_dbmobject_open(dp
);
222 val
= dbm_fetch(dp
->di_dbm
, key
);
223 return PyInt_FromLong(val
.dptr
!= NULL
);
227 dbm_get(register dbmobject
*dp
, PyObject
*args
)
230 PyObject
*defvalue
= Py_None
;
233 if (!PyArg_ParseTuple(args
, "s#|O:get",
234 &key
.dptr
, &tmp_size
, &defvalue
))
236 key
.dsize
= tmp_size
;
237 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
);
248 dbm_setdefault(register dbmobject
*dp
, PyObject
*args
)
251 PyObject
*defvalue
= NULL
;
254 if (!PyArg_ParseTuple(args
, "s#|S:setdefault",
255 &key
.dptr
, &tmp_size
, &defvalue
))
257 key
.dsize
= tmp_size
;
258 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
)
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
);
273 PyErr_SetString(DbmError
, "cannot add item to database");
279 static PyMethodDef dbm_methods
[] = {
280 {"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."},
293 {NULL
, NULL
} /* sentinel */
297 dbm_getattr(dbmobject
*dp
, char *name
)
299 return Py_FindMethod(dbm_methods
, (PyObject
*)dp
, name
);
302 static PyTypeObject Dbmtype
= {
303 PyObject_HEAD_INIT(NULL
)
308 (destructor
)dbm_dealloc
, /*tp_dealloc*/
310 (getattrfunc
)dbm_getattr
, /*tp_getattr*/
315 0, /*tp_as_sequence*/
316 &dbm_as_mapping
, /*tp_as_mapping*/
319 /* ----------------------------------------------------------------- */
322 dbmopen(PyObject
*self
, PyObject
*args
)
329 if ( !PyArg_ParseTuple(args
, "s|si:open", &name
, &flags
, &mode
) )
331 if ( strcmp(flags
, "r") == 0 )
333 else if ( strcmp(flags
, "w") == 0 )
335 else if ( strcmp(flags
, "rw") == 0 ) /* B/W compat */
336 iflags
= O_RDWR
|O_CREAT
;
337 else if ( strcmp(flags
, "c") == 0 )
338 iflags
= O_RDWR
|O_CREAT
;
339 else if ( strcmp(flags
, "n") == 0 )
340 iflags
= O_RDWR
|O_CREAT
|O_TRUNC
;
342 PyErr_SetString(DbmError
,
343 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
346 return newdbmobject(name
, iflags
, mode
);
349 static PyMethodDef dbmmodule_methods
[] = {
350 { "open", (PyCFunction
)dbmopen
, METH_VARARGS
,
351 "open(path[, flag[, mode]]) -> mapping\n"
352 "Return a database object."},
360 Dbmtype
.ob_type
= &PyType_Type
;
361 m
= Py_InitModule("dbm", dbmmodule_methods
);
362 d
= PyModule_GetDict(m
);
363 if (DbmError
== NULL
)
364 DbmError
= PyErr_NewException("dbm.error", NULL
, NULL
);
365 s
= PyString_FromString(which_dbm
);
367 PyDict_SetItemString(d
, "library", s
);
370 if (DbmError
!= NULL
)
371 PyDict_SetItemString(d
, "error", DbmError
);