1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* DBM module using dictionary interface */
33 /* Author: Anthony Baxter, after dbmmodule.c */
34 /* Doc strings: Mitch Chapman */
39 #include <sys/types.h>
45 #include "gdbmerrno.h"
46 extern const char * gdbm_strerror(gdbm_error
);
49 static char gdbmmodule__doc__
[] = "\
50 This module provides an interface to the GNU DBM (GDBM) library.\n\
52 This module is quite similar to the dbm module, but uses GDBM instead to\n\
53 provide some additional functionality. Please note that the file formats\n\
54 created by GDBM and dbm are incompatible. \n\
56 GDBM objects behave like mappings (dictionaries), except that keys and\n\
57 values are always strings. Printing a GDBM object doesn't print the\n\
58 keys and values, and the items() and values() methods are not\n\
63 int di_size
; /* -1 means recompute */
67 staticforward PyTypeObject Dbmtype
;
69 #define is_dbmobject(v) ((v)->ob_type == &Dbmtype)
70 #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \
71 { PyErr_SetString(DbmError, "GDBM object has already been closed"); \
76 static PyObject
*DbmError
;
78 static char gdbm_object__doc__
[] = "\
79 This object represents a GDBM database.\n\
80 GDBM objects behave like mappings (dictionaries), except that keys and\n\
81 values are always strings. Printing a GDBM object doesn't print the\n\
82 keys and values, and the items() and values() methods are not\n\
85 GDBM objects also support additional operations such as firstkey,\n\
86 nextkey, reorganize, and sync.";
89 newdbmobject(file
, flags
, mode
)
96 dp
= PyObject_NEW(dbmobject
, &Dbmtype
);
101 if ( (dp
->di_dbm
= gdbm_open(file
, 0, flags
, mode
, NULL
)) == 0 ) {
103 PyErr_SetFromErrno(DbmError
);
105 PyErr_SetString(DbmError
, gdbm_strerror(gdbm_errno
));
109 return (PyObject
*)dp
;
116 register dbmobject
*dp
;
119 gdbm_close(dp
->di_dbm
);
127 if (dp
->di_dbm
== NULL
) {
128 PyErr_SetString(DbmError
, "GDBM object has already been closed");
131 if ( dp
->di_size
< 0 ) {
137 for ( key
=gdbm_firstkey(dp
->di_dbm
); key
.dptr
;
138 key
= gdbm_nextkey(dp
->di_dbm
,okey
)) {
140 if(okey
.dsize
) free(okey
.dptr
);
149 dbm_subscript(dp
, key
)
151 register PyObject
*key
;
156 if (!PyArg_Parse(key
, "s#", &krec
.dptr
, &krec
.dsize
) )
159 drec
= gdbm_fetch(dp
->di_dbm
, krec
);
160 if ( drec
.dptr
== 0 ) {
161 PyErr_SetString(PyExc_KeyError
,
162 PyString_AS_STRING((PyStringObject
*)key
));
165 v
= PyString_FromStringAndSize(drec
.dptr
, drec
.dsize
);
171 dbm_ass_sub(dp
, v
, w
)
177 if ( !PyArg_Parse(v
, "s#", &krec
.dptr
, &krec
.dsize
) ) {
178 PyErr_SetString(PyExc_TypeError
,
179 "gdbm mappings have string indices only");
182 if (dp
->di_dbm
== NULL
) {
183 PyErr_SetString(DbmError
, "GDBM object has already been closed");
188 if ( gdbm_delete(dp
->di_dbm
, krec
) < 0 ) {
189 PyErr_SetString(PyExc_KeyError
,
190 PyString_AS_STRING((PyStringObject
*)v
));
194 if ( !PyArg_Parse(w
, "s#", &drec
.dptr
, &drec
.dsize
) ) {
195 PyErr_SetString(PyExc_TypeError
,
196 "gdbm mappings have string elements only");
200 if ( gdbm_store(dp
->di_dbm
, krec
, drec
, GDBM_REPLACE
) < 0 ) {
202 PyErr_SetFromErrno(DbmError
);
204 PyErr_SetString(DbmError
,
205 gdbm_strerror(gdbm_errno
));
212 static PyMappingMethods dbm_as_mapping
= {
213 (inquiry
)dbm_length
, /*mp_length*/
214 (binaryfunc
)dbm_subscript
, /*mp_subscript*/
215 (objobjargproc
)dbm_ass_sub
, /*mp_ass_subscript*/
218 static char dbm_close__doc__
[] = "\
220 Closes the database.";
224 register dbmobject
*dp
;
227 if ( !PyArg_NoArgs(args
) )
230 gdbm_close(dp
->di_dbm
);
236 static char dbm_keys__doc__
[] = "\
237 keys() -> list_of_keys\n\
238 Get a list of all keys in the database.";
242 register dbmobject
*dp
;
245 register PyObject
*v
, *item
;
249 if (dp
== NULL
|| !is_dbmobject(dp
)) {
250 PyErr_BadInternalCall();
254 if (!PyArg_NoArgs(args
))
257 check_dbmobject_open(dp
);
263 key
= gdbm_firstkey(dp
->di_dbm
);
265 item
= PyString_FromStringAndSize(key
.dptr
, key
.dsize
);
271 err
= PyList_Append(v
, item
);
278 nextkey
= gdbm_nextkey(dp
->di_dbm
, key
);
286 static char dbm_has_key__doc__
[] = "\
287 has_key(key) -> boolean\n\
288 Find out whether or not the database contains a given key.";
291 dbm_has_key(dp
, args
)
292 register dbmobject
*dp
;
297 if (!PyArg_Parse(args
, "s#", &key
.dptr
, &key
.dsize
))
299 check_dbmobject_open(dp
);
300 return PyInt_FromLong((long) gdbm_exists(dp
->di_dbm
, key
));
303 static char dbm_firstkey__doc__
[] = "\
305 It's possible to loop over every key in the database using this method\n\
306 and the nextkey() method. The traversal is ordered by GDBM's internal\n\
307 hash values, and won't be sorted by the key values. This method\n\
308 returns the starting key.";
311 dbm_firstkey(dp
, args
)
312 register dbmobject
*dp
;
315 register PyObject
*v
;
318 if (!PyArg_NoArgs(args
))
320 check_dbmobject_open(dp
);
321 key
= gdbm_firstkey(dp
->di_dbm
);
323 v
= PyString_FromStringAndSize(key
.dptr
, key
.dsize
);
332 static char dbm_nextkey__doc__
[] = "\
333 nextkey(key) -> next_key\n\
334 Returns the key that follows key in the traversal.\n\
335 The following code prints every key in the database db, without having\n\
336 to create a list in memory that contains them all:\n\
344 dbm_nextkey(dp
, args
)
345 register dbmobject
*dp
;
348 register PyObject
*v
;
351 if (!PyArg_Parse(args
, "s#", &key
.dptr
, &key
.dsize
))
353 check_dbmobject_open(dp
);
354 nextkey
= gdbm_nextkey(dp
->di_dbm
, key
);
356 v
= PyString_FromStringAndSize(nextkey
.dptr
, nextkey
.dsize
);
365 static char dbm_reorganize__doc__
[] = "\
366 reorganize() -> None\n\
367 If you have carried out a lot of deletions and would like to shrink\n\
368 the space used by the GDBM file, this routine will reorganize the\n\
369 database. GDBM will not shorten the length of a database file except\n\
370 by using this reorganization; otherwise, deleted file space will be\n\
371 kept and reused as new (key,value) pairs are added.";
374 dbm_reorganize(dp
, args
)
375 register dbmobject
*dp
;
378 if (!PyArg_NoArgs(args
))
380 check_dbmobject_open(dp
);
382 if (gdbm_reorganize(dp
->di_dbm
) < 0) {
384 PyErr_SetFromErrno(DbmError
);
386 PyErr_SetString(DbmError
, gdbm_strerror(gdbm_errno
));
393 static char dbm_sync__doc__
[] = "\
395 When the database has been opened in fast mode, this method forces\n\
396 any unwritten data to be written to the disk.";
400 register dbmobject
*dp
;
403 if (!PyArg_NoArgs(args
))
405 check_dbmobject_open(dp
);
406 gdbm_sync(dp
->di_dbm
);
411 static PyMethodDef dbm_methods
[] = {
412 {"close", (PyCFunction
)dbm_close
, 0, dbm_close__doc__
},
413 {"keys", (PyCFunction
)dbm_keys
, 0, dbm_keys__doc__
},
414 {"has_key", (PyCFunction
)dbm_has_key
, 0, dbm_has_key__doc__
},
415 {"firstkey", (PyCFunction
)dbm_firstkey
, 0, dbm_firstkey__doc__
},
416 {"nextkey", (PyCFunction
)dbm_nextkey
, 0, dbm_nextkey__doc__
},
417 {"reorganize", (PyCFunction
)dbm_reorganize
, 0, dbm_reorganize__doc__
},
418 {"sync", (PyCFunction
)dbm_sync
, 0, dbm_sync__doc__
},
419 {NULL
, NULL
} /* sentinel */
423 dbm_getattr(dp
, name
)
427 return Py_FindMethod(dbm_methods
, (PyObject
*)dp
, name
);
430 static PyTypeObject Dbmtype
= {
431 PyObject_HEAD_INIT(0)
436 (destructor
)dbm_dealloc
, /*tp_dealloc*/
438 (getattrfunc
)dbm_getattr
, /*tp_getattr*/
443 0, /*tp_as_sequence*/
444 &dbm_as_mapping
, /*tp_as_mapping*/
452 gdbm_object__doc__
, /*tp_doc*/
455 /* ----------------------------------------------------------------- */
457 static char dbmopen__doc__
[] = "\
458 open(filename, [flag, [mode]]) -> dbm_object\n\
459 Open a dbm database and return a dbm object. The filename argument is\n\
460 the name of the database file.\n\
462 The optional flag argument can be 'r' (to open an existing database\n\
463 for reading only -- default), 'w' (to open an existing database for\n\
464 reading and writing), 'c' (which creates the database if it doesn't\n\
465 exist), or 'n' (which always creates a new empty database).\n\
467 Appending f to the flag opens the database in fast mode; altered\n\
468 data will not automatically be written to the disk after every\n\
469 change. This results in faster writes to the database, but may\n\
470 result in an inconsistent database if the program crashes while the\n\
471 database is still open. Use the sync() method to force any\n\
472 unwritten data to be written to the disk.\n\
474 The optional mode argument is the Unix mode of the file, used only\n\
475 when the database has to be created. It defaults to octal 0666. ";
487 if ( !PyArg_ParseTuple(args
, "s|si", &name
, &flags
, &mode
) )
491 iflags
= GDBM_READER
;
494 iflags
= GDBM_WRITER
;
497 iflags
= GDBM_WRCREAT
;
503 PyErr_SetString(DbmError
,
504 "Flags should be one of 'r', 'w', 'c' or 'n'");
509 return newdbmobject(name
, iflags
, mode
);
512 static PyMethodDef dbmmodule_methods
[] = {
513 { "open", (PyCFunction
)dbmopen
, 1, dbmopen__doc__
},
521 Dbmtype
.ob_type
= &PyType_Type
;
522 m
= Py_InitModule4("gdbm", dbmmodule_methods
,
523 gdbmmodule__doc__
, (PyObject
*)NULL
,
525 d
= PyModule_GetDict(m
);
526 DbmError
= PyErr_NewException("gdbm.error", NULL
, NULL
);
527 if (DbmError
!= NULL
)
528 PyDict_SetItemString(d
, "error", DbmError
);