1- /* GDBM module, hacked from the still-breathing corpse of the
2- DBM module by [email protected] . Original copyright 3- follows:
4- */
51/***********************************************************
62Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
73The Netherlands.
@@ -32,13 +28,10 @@ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
3228PERFORMANCE OF THIS SOFTWARE.
3329
3430******************************************************************/
35- /*****************************************************************
36- Modification History:
3731
38- Added support for 'gdbm_sync' method. Roger E. Masse 3/25/97
39-
40- *****************************************************************/
4132/* DBM module using dictionary interface */
33+ /* Author: Anthony Baxter, after dbmmodule.c */
34+ /* Doc strings: Mitch Chapman */
4235
4336
4437#include "Python.h"
@@ -48,6 +41,18 @@ PERFORMANCE OF THIS SOFTWARE.
4841#include <fcntl.h>
4942#include "gdbm.h"
5043
44+ static char gdbmmodule__doc__ [] = "\
45+ This module provides an interface to the GNU DBM (GDBM) library.\n\
46+ \n\
47+ This module is quite similar to the dbm module, but uses GDBM instead to\n\
48+ provide some additional functionality. Please note that the file formats\n\
49+ created by GDBM and dbm are incompatible. \n\
50+ \n\
51+ GDBM objects behave like mappings (dictionaries), except that keys and\n\
52+ values are always strings. Printing a GDBM object doesn't print the\n\
53+ keys and values, and the items() and values() methods are not\n\
54+ supported." ;
55+
5156typedef struct {
5257 PyObject_HEAD
5358 int di_size ; /* -1 means recompute */
@@ -65,6 +70,16 @@ staticforward PyTypeObject Dbmtype;
6570
6671static PyObject * DbmError ;
6772
73+ static char gdbm_object__doc__ [] = "\
74+ This object represents a GDBM database.\n\
75+ GDBM objects behave like mappings (dictionaries), except that keys and\n\
76+ values are always strings. Printing a GDBM object doesn't print the\n\
77+ keys and values, and the items() and values() methods are not\n\
78+ supported.\n\
79+ \n\
80+ GDBM objects also support additional operations such as firstkey,\n\
81+ nextkey, reorganize, and sync." ;
82+
6883static PyObject *
6984newdbmobject (file , flags , mode )
7085 char * file ;
@@ -196,6 +211,11 @@ static PyMappingMethods dbm_as_mapping = {
196211 (objobjargproc )dbm_ass_sub , /*mp_ass_subscript*/
197212};
198213
214+ static char dbm_close__doc__ [] = "\
215+ close() -> None\n\
216+ Closes the database.
217+ " ;
218+
199219static PyObject *
200220dbm_close (dp , args )
201221 register dbmobject * dp ;
@@ -210,6 +230,10 @@ PyObject *args;
210230 return Py_None ;
211231}
212232
233+ static char dbm_keys__doc__ [] = "\
234+ keys() -> list_of_keys\n\
235+ Get a list of all keys in the database." ;
236+
213237static PyObject *
214238dbm_keys (dp , args )
215239 register dbmobject * dp ;
@@ -256,6 +280,10 @@ PyObject *args;
256280 return v ;
257281}
258282
283+ static char dbm_has_key__doc__ [] = "\
284+ has_key(key) -> boolean\n\
285+ Find out whether or not the database contains a given key." ;
286+
259287static PyObject *
260288dbm_has_key (dp , args )
261289 register dbmobject * dp ;
@@ -269,6 +297,13 @@ PyObject *args;
269297 return PyInt_FromLong ((long ) gdbm_exists (dp -> di_dbm , key ));
270298}
271299
300+ static char dbm_firstkey__doc__ [] = "\
301+ firstkey() -> key\n\
302+ It's possible to loop over every key in the database using this method\n\
303+ and the nextkey() method. The traversal is ordered by GDBM's internal\n\
304+ hash values, and won't be sorted by the key values. This method\n\
305+ returns the starting key." ;
306+
272307static PyObject *
273308dbm_firstkey (dp , args )
274309 register dbmobject * dp ;
@@ -291,6 +326,17 @@ PyObject *args;
291326 }
292327}
293328
329+ static char dbm_nextkey__doc__ [] = "\
330+ nextkey(key) -> next_key\n\
331+ Returns the key that follows key in the traversal.\n\
332+ The following code prints every key in the database db, without having\n\
333+ to create a list in memory that contains them all:\n\
334+ \n\
335+ k = db.firstkey()\n\
336+ while k != None:\n\
337+ print k\n\
338+ k = db.nextkey(k)" ;
339+
294340static PyObject *
295341dbm_nextkey (dp , args )
296342 register dbmobject * dp ;
@@ -313,6 +359,14 @@ PyObject *args;
313359 }
314360}
315361
362+ static char dbm_reorganize__doc__ [] = "\
363+ reorganize() -> None\n\
364+ If you have carried out a lot of deletions and would like to shrink\n\
365+ the space used by the GDBM file, this routine will reorganize the\n\
366+ database. GDBM will not shorten the length of a database file except\n\
367+ by using this reorganization; otherwise, deleted file space will be\n\
368+ kept and reused as new (key,value) pairs are added." ;
369+
316370static PyObject *
317371dbm_reorganize (dp , args )
318372 register dbmobject * dp ;
@@ -334,6 +388,11 @@ PyObject *args;
334388 return Py_None ;
335389}
336390
391+ static char dbm_sync__doc__ [] = "\
392+ sync() -> None\n\
393+ When the database has been opened in fast mode, this method forces\n\
394+ any unwritten data to be written to the disk." ;
395+
337396static PyObject *
338397dbm_sync (dp , args )
339398 register dbmobject * dp ;
@@ -348,13 +407,13 @@ dbm_sync(dp, args)
348407}
349408
350409static PyMethodDef dbm_methods [] = {
351- {"close" , (PyCFunction )dbm_close },
352- {"keys" , (PyCFunction )dbm_keys },
353- {"has_key" , (PyCFunction )dbm_has_key },
354- {"firstkey" , (PyCFunction )dbm_firstkey },
355- {"nextkey" , (PyCFunction )dbm_nextkey },
356- {"reorganize" , (PyCFunction )dbm_reorganize },
357- {"sync" , (PyCFunction )dbm_sync },
410+ {"close" , (PyCFunction )dbm_close , 0 , dbm_close__doc__ },
411+ {"keys" , (PyCFunction )dbm_keys , 0 , dbm_keys__doc__ },
412+ {"has_key" , (PyCFunction )dbm_has_key , 0 , dbm_has_key__doc__ },
413+ {"firstkey" , (PyCFunction )dbm_firstkey , 0 , dbm_firstkey__doc__ },
414+ {"nextkey" , (PyCFunction )dbm_nextkey , 0 , dbm_nextkey__doc__ },
415+ {"reorganize" , (PyCFunction )dbm_reorganize , 0 , dbm_reorganize__doc__ },
416+ {"sync" , (PyCFunction )dbm_sync , 0 , dbm_sync__doc__ },
358417 {NULL , NULL } /* sentinel */
359418};
360419
@@ -381,10 +440,38 @@ static PyTypeObject Dbmtype = {
381440 0 , /*tp_as_number*/
382441 0 , /*tp_as_sequence*/
383442 & dbm_as_mapping , /*tp_as_mapping*/
443+ 0 , /*tp_hash*/
444+ 0 , /*tp_call*/
445+ 0 , /*tp_str*/
446+ 0 , /*tp_getattro*/
447+ 0 , /*tp_setattro*/
448+ 0 , /*tp_as_buffer*/
449+ 0 , /*tp_xxx4*/
450+ gdbm_object__doc__ , /*tp_doc*/
384451};
385452
386453/* ----------------------------------------------------------------- */
387454
455+ static char dbmopen__doc__ [] = "\
456+ open(filename, [flag, [mode]]) -> dbm_object\n\
457+ Open a dbm database and return a dbm object. The filename argument is\n\
458+ the name of the database file.\n\
459+ \n\
460+ The optional flag argument can be 'r' (to open an existing database\n\
461+ for reading only -- default), 'w' (to open an existing database for\n\
462+ reading and writing), 'c' (which creates the database if it doesn't\n\
463+ exist), or 'n' (which always creates a new empty database).\n\
464+ \n\
465+ Appending f to the flag opens the database in fast mode; altered\n\
466+ data will not automatically be written to the disk after every\n\
467+ change. This results in faster writes to the database, but may\n\
468+ result in an inconsistent database if the program crashes while the\n\
469+ database is still open. Use the sync() method to force any\n\
470+ unwritten data to be written to the disk.\n\
471+ \n\
472+ The optional mode argument is the Unix mode of the file, used only\n\
473+ when the database has to be created. It defaults to octal 0666. " ;
474+
388475static PyObject *
389476dbmopen (self , args )
390477 PyObject * self ;
@@ -395,7 +482,6 @@ PyObject *args;
395482 int iflags ;
396483 int mode = 0666 ;
397484
398- /* XXXX add other flags. 2nd character can be "f" meaning open in fast mode. */
399485 if ( !PyArg_ParseTuple (args , "s|si" , & name , & flags , & mode ) )
400486 return NULL ;
401487 switch (flags [0 ]) {
@@ -422,15 +508,17 @@ PyObject *args;
422508}
423509
424510static PyMethodDef dbmmodule_methods [] = {
425- { "open" , (PyCFunction )dbmopen , 1 },
511+ { "open" , (PyCFunction )dbmopen , 1 , dbmopen__doc__ },
426512 { 0 , 0 },
427513};
428514
429515void
430516initgdbm () {
431517 PyObject * m , * d ;
432518
433- m = Py_InitModule ("gdbm" , dbmmodule_methods );
519+ m = Py_InitModule4 ("gdbm" , dbmmodule_methods ,
520+ gdbmmodule__doc__ , (PyObject * )NULL ,
521+ PYTHON_API_VERSION );
434522 d = PyModule_GetDict (m );
435523 DbmError = PyErr_NewException ("gdbm.error" , NULL , NULL );
436524 if (DbmError != NULL )
0 commit comments