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_GDBM_DASH_NDBM_H)
25 #include <gdbm-ndbm.h>
26 static char *which_dbm
= "GNU gdbm";
27 #elif defined(HAVE_BERKDB_H)
29 static char *which_dbm
= "Berkeley DB";
31 #error "No ndbm.h available!"
36 int di_size
; /* -1 means recompute */
40 static PyTypeObject Dbmtype
;
42 #define is_dbmobject(v) (Py_TYPE(v) == &Dbmtype)
43 #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
44 { PyErr_SetString(DbmError, "DBM object has already been closed"); \
47 static PyObject
*DbmError
;
50 newdbmobject(char *file
, int flags
, int mode
)
54 dp
= PyObject_New(dbmobject
, &Dbmtype
);
58 if ( (dp
->di_dbm
= dbm_open(file
, flags
, mode
)) == 0 ) {
59 PyErr_SetFromErrno(DbmError
);
63 return (PyObject
*)dp
;
69 dbm_dealloc(register dbmobject
*dp
)
72 dbm_close(dp
->di_dbm
);
77 dbm_length(dbmobject
*dp
)
79 if (dp
->di_dbm
== NULL
) {
80 PyErr_SetString(DbmError
, "DBM object has already been closed");
83 if ( dp
->di_size
< 0 ) {
88 for ( key
=dbm_firstkey(dp
->di_dbm
); key
.dptr
;
89 key
= dbm_nextkey(dp
->di_dbm
))
97 dbm_subscript(dbmobject
*dp
, register PyObject
*key
)
102 if (!PyArg_Parse(key
, "s#", &krec
.dptr
, &tmp_size
) )
105 krec
.dsize
= tmp_size
;
106 check_dbmobject_open(dp
);
107 drec
= dbm_fetch(dp
->di_dbm
, krec
);
108 if ( drec
.dptr
== 0 ) {
109 PyErr_SetString(PyExc_KeyError
,
110 PyString_AS_STRING((PyStringObject
*)key
));
113 if ( dbm_error(dp
->di_dbm
) ) {
114 dbm_clearerr(dp
->di_dbm
);
115 PyErr_SetString(DbmError
, "");
118 return PyString_FromStringAndSize(drec
.dptr
, drec
.dsize
);
122 dbm_ass_sub(dbmobject
*dp
, PyObject
*v
, PyObject
*w
)
127 if ( !PyArg_Parse(v
, "s#", &krec
.dptr
, &tmp_size
) ) {
128 PyErr_SetString(PyExc_TypeError
,
129 "dbm mappings have string indices only");
132 krec
.dsize
= tmp_size
;
133 if (dp
->di_dbm
== NULL
) {
134 PyErr_SetString(DbmError
, "DBM object has already been closed");
139 if ( dbm_delete(dp
->di_dbm
, krec
) < 0 ) {
140 dbm_clearerr(dp
->di_dbm
);
141 PyErr_SetString(PyExc_KeyError
,
142 PyString_AS_STRING((PyStringObject
*)v
));
146 if ( !PyArg_Parse(w
, "s#", &drec
.dptr
, &tmp_size
) ) {
147 PyErr_SetString(PyExc_TypeError
,
148 "dbm mappings have string elements only");
151 drec
.dsize
= tmp_size
;
152 if ( dbm_store(dp
->di_dbm
, krec
, drec
, DBM_REPLACE
) < 0 ) {
153 dbm_clearerr(dp
->di_dbm
);
154 PyErr_SetString(DbmError
,
155 "cannot add item to database");
159 if ( dbm_error(dp
->di_dbm
) ) {
160 dbm_clearerr(dp
->di_dbm
);
161 PyErr_SetString(DbmError
, "");
168 dbm_contains(register dbmobject
*dp
, PyObject
*v
)
172 if (PyString_AsStringAndSize(v
, (char **)&key
.dptr
,
173 (Py_ssize_t
*)&key
.dsize
)) {
177 /* Expand check_dbmobject_open to return -1 */
178 if (dp
->di_dbm
== NULL
) {
179 PyErr_SetString(DbmError
, "DBM object has already been closed");
182 val
= dbm_fetch(dp
->di_dbm
, key
);
183 return val
.dptr
!= NULL
;
186 static PySequenceMethods dbm_as_sequence
= {
187 (lenfunc
)dbm_length
, /*_length*/
194 (objobjproc
)dbm_contains
, /*sq_contains*/
195 0, /*sq_inplace_concat*/
196 0 /*sq_inplace_repeat*/
199 static PyMappingMethods dbm_as_mapping
= {
200 (lenfunc
)dbm_length
, /*mp_length*/
201 (binaryfunc
)dbm_subscript
, /*mp_subscript*/
202 (objobjargproc
)dbm_ass_sub
, /*mp_ass_subscript*/
206 dbm__close(register dbmobject
*dp
, PyObject
*unused
)
209 dbm_close(dp
->di_dbm
);
216 dbm_keys(register dbmobject
*dp
, PyObject
*unused
)
218 register PyObject
*v
, *item
;
222 check_dbmobject_open(dp
);
226 for (key
= dbm_firstkey(dp
->di_dbm
); key
.dptr
;
227 key
= dbm_nextkey(dp
->di_dbm
)) {
228 item
= PyString_FromStringAndSize(key
.dptr
, key
.dsize
);
233 err
= PyList_Append(v
, item
);
244 dbm_has_key(register dbmobject
*dp
, PyObject
*args
)
250 if (!PyArg_ParseTuple(args
, "s#:has_key", &tmp_ptr
, &tmp_size
))
253 key
.dsize
= tmp_size
;
254 check_dbmobject_open(dp
);
255 val
= dbm_fetch(dp
->di_dbm
, key
);
256 return PyInt_FromLong(val
.dptr
!= NULL
);
260 dbm_get(register dbmobject
*dp
, PyObject
*args
)
263 PyObject
*defvalue
= Py_None
;
267 if (!PyArg_ParseTuple(args
, "s#|O:get",
268 &tmp_ptr
, &tmp_size
, &defvalue
))
271 key
.dsize
= tmp_size
;
272 check_dbmobject_open(dp
);
273 val
= dbm_fetch(dp
->di_dbm
, key
);
274 if (val
.dptr
!= NULL
)
275 return PyString_FromStringAndSize(val
.dptr
, val
.dsize
);
283 dbm_setdefault(register dbmobject
*dp
, PyObject
*args
)
286 PyObject
*defvalue
= NULL
;
290 if (!PyArg_ParseTuple(args
, "s#|S:setdefault",
291 &tmp_ptr
, &tmp_size
, &defvalue
))
294 key
.dsize
= tmp_size
;
295 check_dbmobject_open(dp
);
296 val
= dbm_fetch(dp
->di_dbm
, key
);
297 if (val
.dptr
!= NULL
)
298 return PyString_FromStringAndSize(val
.dptr
, val
.dsize
);
299 if (defvalue
== NULL
) {
300 defvalue
= PyString_FromStringAndSize(NULL
, 0);
301 if (defvalue
== NULL
)
306 val
.dptr
= PyString_AS_STRING(defvalue
);
307 val
.dsize
= PyString_GET_SIZE(defvalue
);
308 if (dbm_store(dp
->di_dbm
, key
, val
, DBM_INSERT
) < 0) {
309 dbm_clearerr(dp
->di_dbm
);
310 PyErr_SetString(DbmError
, "cannot add item to database");
316 static PyMethodDef dbm_methods
[] = {
317 {"close", (PyCFunction
)dbm__close
, METH_NOARGS
,
318 "close()\nClose the database."},
319 {"keys", (PyCFunction
)dbm_keys
, METH_NOARGS
,
320 "keys() -> list\nReturn a list of all keys in the database."},
321 {"has_key", (PyCFunction
)dbm_has_key
, METH_VARARGS
,
322 "has_key(key} -> boolean\nReturn true iff key is in the database."},
323 {"get", (PyCFunction
)dbm_get
, METH_VARARGS
,
324 "get(key[, default]) -> value\n"
325 "Return the value for key if present, otherwise default."},
326 {"setdefault", (PyCFunction
)dbm_setdefault
, METH_VARARGS
,
327 "setdefault(key[, default]) -> value\n"
328 "Return the value for key if present, otherwise default. If key\n"
329 "is not in the database, it is inserted with default as the value."},
330 {NULL
, NULL
} /* sentinel */
334 dbm_getattr(dbmobject
*dp
, char *name
)
336 return Py_FindMethod(dbm_methods
, (PyObject
*)dp
, name
);
339 static PyTypeObject Dbmtype
= {
340 PyVarObject_HEAD_INIT(NULL
, 0)
344 (destructor
)dbm_dealloc
, /*tp_dealloc*/
346 (getattrfunc
)dbm_getattr
, /*tp_getattr*/
351 &dbm_as_sequence
, /*tp_as_sequence*/
352 &dbm_as_mapping
, /*tp_as_mapping*/
359 Py_TPFLAGS_DEFAULT
, /*tp_xxx4*/
362 /* ----------------------------------------------------------------- */
365 dbmopen(PyObject
*self
, PyObject
*args
)
372 if ( !PyArg_ParseTuple(args
, "s|si:open", &name
, &flags
, &mode
) )
374 if ( strcmp(flags
, "r") == 0 )
376 else if ( strcmp(flags
, "w") == 0 )
378 else if ( strcmp(flags
, "rw") == 0 ) /* B/W compat */
379 iflags
= O_RDWR
|O_CREAT
;
380 else if ( strcmp(flags
, "c") == 0 )
381 iflags
= O_RDWR
|O_CREAT
;
382 else if ( strcmp(flags
, "n") == 0 )
383 iflags
= O_RDWR
|O_CREAT
|O_TRUNC
;
385 PyErr_SetString(DbmError
,
386 "arg 2 to open should be 'r', 'w', 'c', or 'n'");
389 return newdbmobject(name
, iflags
, mode
);
392 static PyMethodDef dbmmodule_methods
[] = {
393 { "open", (PyCFunction
)dbmopen
, METH_VARARGS
,
394 "open(path[, flag[, mode]]) -> mapping\n"
395 "Return a database object."},
403 Dbmtype
.ob_type
= &PyType_Type
;
404 m
= Py_InitModule("dbm", dbmmodule_methods
);
407 d
= PyModule_GetDict(m
);
408 if (DbmError
== NULL
)
409 DbmError
= PyErr_NewException("dbm.error", NULL
, NULL
);
410 s
= PyString_FromString(which_dbm
);
412 PyDict_SetItemString(d
, "library", s
);
415 if (DbmError
!= NULL
)
416 PyDict_SetItemString(d
, "error", DbmError
);