Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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 |
| 15 | permission. |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 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. |
| 20 | |
| 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. |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* DBM module using dictionary interface */ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 33 | /* Author: Anthony Baxter, after dbmmodule.c */ |
| 34 | /* Doc strings: Mitch Chapman */ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 35 | |
| 36 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 37 | #include "Python.h" |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 38 | |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <fcntl.h> |
| 42 | #include "gdbm.h" |
| 43 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 56 | typedef struct { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 57 | PyObject_HEAD |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 58 | int di_size; /* -1 means recompute */ |
| 59 | GDBM_FILE di_dbm; |
| 60 | } dbmobject; |
| 61 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 62 | staticforward PyTypeObject Dbmtype; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 63 | |
| 64 | #define is_dbmobject(v) ((v)->ob_type == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 65 | #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \ |
| 66 | { PyErr_SetString(DbmError, "GDBM object has already been closed"); \ |
| 67 | return NULL; } |
| 68 | |
| 69 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 70 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 71 | static PyObject *DbmError; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 83 | static PyObject * |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 84 | newdbmobject(file, flags, mode) |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 85 | char *file; |
| 86 | int flags; |
| 87 | int mode; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 88 | { |
| 89 | dbmobject *dp; |
| 90 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 91 | dp = PyObject_NEW(dbmobject, &Dbmtype); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 92 | if (dp == NULL) |
| 93 | return NULL; |
| 94 | dp->di_size = -1; |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 95 | errno = 0; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 96 | if ( (dp->di_dbm = gdbm_open(file, 0, flags, mode, NULL)) == 0 ) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 97 | if (errno != 0) |
| 98 | PyErr_SetFromErrno(DbmError); |
| 99 | else |
| 100 | PyErr_SetString(DbmError, |
| 101 | (char *) gdbm_strerror(gdbm_errno)); |
| 102 | Py_DECREF(dp); |
| 103 | return NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 104 | } |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 105 | return (PyObject *)dp; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* Methods */ |
| 109 | |
| 110 | static void |
| 111 | dbm_dealloc(dp) |
| 112 | register dbmobject *dp; |
| 113 | { |
| 114 | if ( dp->di_dbm ) |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 115 | gdbm_close(dp->di_dbm); |
| 116 | PyMem_DEL(dp); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int |
| 120 | dbm_length(dp) |
| 121 | dbmobject *dp; |
| 122 | { |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 123 | if (dp->di_dbm == NULL) { |
| 124 | PyErr_SetString(DbmError, "GDBM object has already been closed"); |
| 125 | return -1; |
| 126 | } |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 127 | if ( dp->di_size < 0 ) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 128 | datum key,okey; |
| 129 | int size; |
| 130 | okey.dsize=0; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 131 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 132 | size = 0; |
| 133 | for ( key=gdbm_firstkey(dp->di_dbm); key.dptr; |
| 134 | key = gdbm_nextkey(dp->di_dbm,okey)) { |
| 135 | size++; |
| 136 | if(okey.dsize) free(okey.dptr); |
| 137 | okey=key; |
| 138 | } |
| 139 | dp->di_size = size; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 140 | } |
| 141 | return dp->di_size; |
| 142 | } |
| 143 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 144 | static PyObject * |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 145 | dbm_subscript(dp, key) |
| 146 | dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 147 | register PyObject *key; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 148 | { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 149 | PyObject *v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 150 | datum drec, krec; |
| 151 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 152 | if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) ) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 153 | return NULL; |
| 154 | |
| 155 | drec = gdbm_fetch(dp->di_dbm, krec); |
| 156 | if ( drec.dptr == 0 ) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 157 | PyErr_SetString(PyExc_KeyError, |
| 158 | PyString_AS_STRING((PyStringObject *)key)); |
| 159 | return NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 160 | } |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 161 | v = PyString_FromStringAndSize(drec.dptr, drec.dsize); |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 162 | free(drec.dptr); |
| 163 | return v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static int |
| 167 | dbm_ass_sub(dp, v, w) |
| 168 | dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 169 | PyObject *v, *w; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 170 | { |
| 171 | datum krec, drec; |
| 172 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 173 | if ( !PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) { |
| 174 | PyErr_SetString(PyExc_TypeError, |
| 175 | "gdbm mappings have string indices only"); |
| 176 | return -1; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 177 | } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 178 | if (dp->di_dbm == NULL) { |
| 179 | PyErr_SetString(DbmError, "GDBM object has already been closed"); |
| 180 | return -1; |
| 181 | } |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 182 | dp->di_size = -1; |
| 183 | if (w == NULL) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 184 | if ( gdbm_delete(dp->di_dbm, krec) < 0 ) { |
| 185 | PyErr_SetString(PyExc_KeyError, |
| 186 | PyString_AS_STRING((PyStringObject *)v)); |
| 187 | return -1; |
| 188 | } |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 189 | } else { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 190 | if ( !PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize) ) { |
| 191 | PyErr_SetString(PyExc_TypeError, |
| 192 | "gdbm mappings have string elements only"); |
| 193 | return -1; |
| 194 | } |
| 195 | errno = 0; |
| 196 | if ( gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0 ) { |
| 197 | if (errno != 0) |
| 198 | PyErr_SetFromErrno(DbmError); |
| 199 | else |
| 200 | PyErr_SetString(DbmError, |
| 201 | (char *) gdbm_strerror(gdbm_errno)); |
| 202 | return -1; |
| 203 | } |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 204 | } |
| 205 | return 0; |
| 206 | } |
| 207 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 208 | static PyMappingMethods dbm_as_mapping = { |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 209 | (inquiry)dbm_length, /*mp_length*/ |
| 210 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 211 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
| 212 | }; |
| 213 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 214 | static char dbm_close__doc__[] = "\ |
| 215 | close() -> None\n\ |
| 216 | Closes the database. |
| 217 | "; |
| 218 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 219 | static PyObject * |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 220 | dbm_close(dp, args) |
| 221 | register dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 222 | PyObject *args; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 223 | { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 224 | if ( !PyArg_NoArgs(args) ) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 225 | return NULL; |
| 226 | if ( dp->di_dbm ) |
| 227 | gdbm_close(dp->di_dbm); |
| 228 | dp->di_dbm = NULL; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 229 | Py_INCREF(Py_None); |
| 230 | return Py_None; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 233 | static char dbm_keys__doc__[] = "\ |
| 234 | keys() -> list_of_keys\n\ |
| 235 | Get a list of all keys in the database."; |
| 236 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 237 | static PyObject * |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 238 | dbm_keys(dp, args) |
| 239 | register dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 240 | PyObject *args; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 241 | { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 242 | register PyObject *v, *item; |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 243 | datum key, nextkey; |
Guido van Rossum | 66017aa | 1995-08-28 02:58:00 +0000 | [diff] [blame] | 244 | int err; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 245 | |
| 246 | if (dp == NULL || !is_dbmobject(dp)) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 247 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 248 | return NULL; |
| 249 | } |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 250 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 251 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 252 | return NULL; |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 254 | check_dbmobject_open(dp); |
| 255 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 256 | v = PyList_New(0); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 257 | if (v == NULL) |
| 258 | return NULL; |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 259 | |
| 260 | key = gdbm_firstkey(dp->di_dbm); |
| 261 | while (key.dptr) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 262 | item = PyString_FromStringAndSize(key.dptr, key.dsize); |
| 263 | if (item == NULL) { |
| 264 | free(key.dptr); |
| 265 | Py_DECREF(v); |
| 266 | return NULL; |
| 267 | } |
| 268 | err = PyList_Append(v, item); |
| 269 | Py_DECREF(item); |
| 270 | if (err != 0) { |
| 271 | free(key.dptr); |
| 272 | Py_DECREF(v); |
| 273 | return NULL; |
| 274 | } |
| 275 | nextkey = gdbm_nextkey(dp->di_dbm, key); |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 276 | free(key.dptr); |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 277 | key = nextkey; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 278 | } |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 279 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 280 | return v; |
| 281 | } |
| 282 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 287 | static PyObject * |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 288 | dbm_has_key(dp, args) |
| 289 | register dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 290 | PyObject *args; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 291 | { |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 292 | datum key; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 293 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 294 | if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize)) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 295 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 296 | check_dbmobject_open(dp); |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 297 | return PyInt_FromLong((long) gdbm_exists(dp->di_dbm, key)); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 307 | static PyObject * |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 308 | dbm_firstkey(dp, args) |
| 309 | register dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 310 | PyObject *args; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 311 | { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 312 | register PyObject *v; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 313 | datum key; |
| 314 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 315 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 316 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 317 | check_dbmobject_open(dp); |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 318 | key = gdbm_firstkey(dp->di_dbm); |
| 319 | if (key.dptr) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 320 | v = PyString_FromStringAndSize(key.dptr, key.dsize); |
| 321 | free(key.dptr); |
| 322 | return v; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 323 | } else { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 324 | Py_INCREF(Py_None); |
| 325 | return Py_None; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 340 | static PyObject * |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 341 | dbm_nextkey(dp, args) |
| 342 | register dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 343 | PyObject *args; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 344 | { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 345 | register PyObject *v; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 346 | datum key, nextkey; |
| 347 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 348 | if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize)) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 349 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 350 | check_dbmobject_open(dp); |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 351 | nextkey = gdbm_nextkey(dp->di_dbm, key); |
| 352 | if (nextkey.dptr) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 353 | v = PyString_FromStringAndSize(nextkey.dptr, nextkey.dsize); |
| 354 | free(nextkey.dptr); |
| 355 | return v; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 356 | } else { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 357 | Py_INCREF(Py_None); |
| 358 | return Py_None; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 370 | static PyObject * |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 371 | dbm_reorganize(dp, args) |
| 372 | register dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 373 | PyObject *args; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 374 | { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 375 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 376 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 377 | check_dbmobject_open(dp); |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 378 | errno = 0; |
| 379 | if (gdbm_reorganize(dp->di_dbm) < 0) { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 380 | if (errno != 0) |
| 381 | PyErr_SetFromErrno(DbmError); |
| 382 | else |
| 383 | PyErr_SetString(DbmError, |
| 384 | (char *) gdbm_strerror(gdbm_errno)); |
| 385 | return NULL; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 386 | } |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 387 | Py_INCREF(Py_None); |
| 388 | return Py_None; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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 | |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 396 | static PyObject * |
| 397 | dbm_sync(dp, args) |
| 398 | register dbmobject *dp; |
| 399 | PyObject *args; |
| 400 | { |
| 401 | if (!PyArg_NoArgs(args)) |
| 402 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 403 | check_dbmobject_open(dp); |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 404 | gdbm_sync(dp->di_dbm); |
| 405 | Py_INCREF(Py_None); |
| 406 | return Py_None; |
| 407 | } |
| 408 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 409 | static PyMethodDef dbm_methods[] = { |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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__}, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 417 | {NULL, NULL} /* sentinel */ |
| 418 | }; |
| 419 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 420 | static PyObject * |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 421 | dbm_getattr(dp, name) |
| 422 | dbmobject *dp; |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 423 | char *name; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 424 | { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 425 | return Py_FindMethod(dbm_methods, (PyObject *)dp, name); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 428 | static PyTypeObject Dbmtype = { |
| 429 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 430 | 0, |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 431 | "gdbm", |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 432 | sizeof(dbmobject), |
| 433 | 0, |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 434 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
| 435 | 0, /*tp_print*/ |
| 436 | (getattrfunc)dbm_getattr, /*tp_getattr*/ |
| 437 | 0, /*tp_setattr*/ |
| 438 | 0, /*tp_compare*/ |
| 439 | 0, /*tp_repr*/ |
| 440 | 0, /*tp_as_number*/ |
| 441 | 0, /*tp_as_sequence*/ |
| 442 | &dbm_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 451 | }; |
| 452 | |
| 453 | /* ----------------------------------------------------------------- */ |
| 454 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 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 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 475 | static PyObject * |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 476 | dbmopen(self, args) |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 477 | PyObject *self; |
| 478 | PyObject *args; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 479 | { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 480 | char *name; |
| 481 | char *flags = "r "; |
| 482 | int iflags; |
| 483 | int mode = 0666; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 484 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 485 | if ( !PyArg_ParseTuple(args, "s|si", &name, &flags, &mode) ) |
| 486 | return NULL; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 487 | switch (flags[0]) { |
| 488 | case 'r': |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 489 | iflags = GDBM_READER; |
| 490 | break; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 491 | case 'w': |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 492 | iflags = GDBM_WRITER; |
| 493 | break; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 494 | case 'c': |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 495 | iflags = GDBM_WRCREAT; |
| 496 | break; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 497 | case 'n': |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 498 | iflags = GDBM_NEWDB; |
| 499 | break; |
| 500 | default: |
| 501 | PyErr_SetString(DbmError, |
| 502 | "Flags should be one of 'r', 'w', 'c' or 'n'"); |
| 503 | return NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 504 | } |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 505 | if (flags[1] == 'f') |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 506 | iflags |= GDBM_FAST; |
| 507 | return newdbmobject(name, iflags, mode); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 510 | static PyMethodDef dbmmodule_methods[] = { |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 511 | { "open", (PyCFunction)dbmopen, 1, dbmopen__doc__}, |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 512 | { 0, 0 }, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 513 | }; |
| 514 | |
| 515 | void |
| 516 | initgdbm() { |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 517 | PyObject *m, *d; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 518 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 519 | m = Py_InitModule4("gdbm", dbmmodule_methods, |
| 520 | gdbmmodule__doc__, (PyObject *)NULL, |
| 521 | PYTHON_API_VERSION); |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 522 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 523 | DbmError = PyErr_NewException("gdbm.error", NULL, NULL); |
| 524 | if (DbmError != NULL) |
| 525 | PyDict_SetItemString(d, "error", DbmError); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 526 | } |