Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 1 | |
| 2 | /* DBM module using dictionary interface */ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 3 | /* Author: Anthony Baxter, after dbmmodule.c */ |
| 4 | /* Doc strings: Mitch Chapman */ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 5 | |
| 6 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 7 | #include "Python.h" |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 8 | |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <fcntl.h> |
| 12 | #include "gdbm.h" |
| 13 | |
Tim Peters | 5687ffe | 2001-02-28 16:44:18 +0000 | [diff] [blame] | 14 | #if defined(WIN32) && !defined(__CYGWIN__) |
Guido van Rossum | b6e2a99 | 1998-10-03 05:13:27 +0000 | [diff] [blame] | 15 | #include "gdbmerrno.h" |
| 16 | extern const char * gdbm_strerror(gdbm_error); |
| 17 | #endif |
| 18 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 19 | static char gdbmmodule__doc__[] = "\ |
| 20 | This module provides an interface to the GNU DBM (GDBM) library.\n\ |
| 21 | \n\ |
| 22 | This module is quite similar to the dbm module, but uses GDBM instead to\n\ |
| 23 | provide some additional functionality. Please note that the file formats\n\ |
| 24 | created by GDBM and dbm are incompatible. \n\ |
| 25 | \n\ |
| 26 | GDBM objects behave like mappings (dictionaries), except that keys and\n\ |
| 27 | values are always strings. Printing a GDBM object doesn't print the\n\ |
| 28 | keys and values, and the items() and values() methods are not\n\ |
| 29 | supported."; |
| 30 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 31 | typedef struct { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 32 | PyObject_HEAD |
| 33 | int di_size; /* -1 means recompute */ |
| 34 | GDBM_FILE di_dbm; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 35 | } dbmobject; |
| 36 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 37 | staticforward PyTypeObject Dbmtype; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 38 | |
| 39 | #define is_dbmobject(v) ((v)->ob_type == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 40 | #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \ |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 41 | { PyErr_SetString(DbmError, "GDBM object has already been closed"); \ |
| 42 | return NULL; } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 43 | |
| 44 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 45 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 46 | static PyObject *DbmError; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 48 | static char gdbm_object__doc__[] = "\ |
| 49 | This object represents a GDBM database.\n\ |
| 50 | GDBM objects behave like mappings (dictionaries), except that keys and\n\ |
| 51 | values are always strings. Printing a GDBM object doesn't print the\n\ |
| 52 | keys and values, and the items() and values() methods are not\n\ |
| 53 | supported.\n\ |
| 54 | \n\ |
| 55 | GDBM objects also support additional operations such as firstkey,\n\ |
| 56 | nextkey, reorganize, and sync."; |
| 57 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 58 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 59 | newdbmobject(char *file, int flags, int mode) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 60 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 61 | dbmobject *dp; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 62 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 63 | dp = PyObject_New(dbmobject, &Dbmtype); |
| 64 | if (dp == NULL) |
| 65 | return NULL; |
| 66 | dp->di_size = -1; |
| 67 | errno = 0; |
| 68 | if ((dp->di_dbm = gdbm_open(file, 0, flags, mode, NULL)) == 0) { |
| 69 | if (errno != 0) |
| 70 | PyErr_SetFromErrno(DbmError); |
| 71 | else |
| 72 | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
| 73 | Py_DECREF(dp); |
| 74 | return NULL; |
| 75 | } |
| 76 | return (PyObject *)dp; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* Methods */ |
| 80 | |
| 81 | static void |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 82 | dbm_dealloc(register dbmobject *dp) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 83 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 84 | if (dp->di_dbm) |
| 85 | gdbm_close(dp->di_dbm); |
| 86 | PyObject_Del(dp); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 90 | dbm_length(dbmobject *dp) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 91 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 92 | if (dp->di_dbm == NULL) { |
| 93 | PyErr_SetString(DbmError, "GDBM object has already been closed"); |
| 94 | return -1; |
| 95 | } |
| 96 | if (dp->di_size < 0) { |
| 97 | datum key,okey; |
| 98 | int size; |
| 99 | okey.dsize=0; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 100 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 101 | size = 0; |
| 102 | for (key=gdbm_firstkey(dp->di_dbm); key.dptr; |
| 103 | key = gdbm_nextkey(dp->di_dbm,okey)) { |
| 104 | size++; |
| 105 | if(okey.dsize) free(okey.dptr); |
| 106 | okey=key; |
| 107 | } |
| 108 | dp->di_size = size; |
| 109 | } |
| 110 | return dp->di_size; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 113 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 114 | dbm_subscript(dbmobject *dp, register PyObject *key) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 115 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 116 | PyObject *v; |
| 117 | datum drec, krec; |
Fred Drake | da8d216 | 2000-02-07 17:19:41 +0000 | [diff] [blame] | 118 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 119 | if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) ) |
| 120 | return NULL; |
| 121 | |
| 122 | if (dp->di_dbm == NULL) { |
| 123 | PyErr_SetString(DbmError, |
| 124 | "GDBM object has already been closed"); |
| 125 | return NULL; |
| 126 | } |
| 127 | drec = gdbm_fetch(dp->di_dbm, krec); |
| 128 | if (drec.dptr == 0) { |
| 129 | PyErr_SetString(PyExc_KeyError, |
| 130 | PyString_AS_STRING((PyStringObject *)key)); |
| 131 | return NULL; |
| 132 | } |
| 133 | v = PyString_FromStringAndSize(drec.dptr, drec.dsize); |
| 134 | free(drec.dptr); |
| 135 | return v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 139 | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 140 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 141 | datum krec, drec; |
| 142 | |
| 143 | if (!PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) { |
| 144 | PyErr_SetString(PyExc_TypeError, |
| 145 | "gdbm mappings have string indices only"); |
| 146 | return -1; |
| 147 | } |
| 148 | if (dp->di_dbm == NULL) { |
| 149 | PyErr_SetString(DbmError, |
| 150 | "GDBM object has already been closed"); |
| 151 | return -1; |
| 152 | } |
| 153 | dp->di_size = -1; |
| 154 | if (w == NULL) { |
| 155 | if (gdbm_delete(dp->di_dbm, krec) < 0) { |
| 156 | PyErr_SetString(PyExc_KeyError, |
| 157 | PyString_AS_STRING((PyStringObject *)v)); |
| 158 | return -1; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 159 | } |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 160 | } |
| 161 | else { |
| 162 | if (!PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize)) { |
| 163 | PyErr_SetString(PyExc_TypeError, |
| 164 | "gdbm mappings have string elements only"); |
| 165 | return -1; |
| 166 | } |
| 167 | errno = 0; |
| 168 | if (gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0) { |
| 169 | if (errno != 0) |
| 170 | PyErr_SetFromErrno(DbmError); |
| 171 | else |
| 172 | PyErr_SetString(DbmError, |
| 173 | gdbm_strerror(gdbm_errno)); |
| 174 | return -1; |
| 175 | } |
| 176 | } |
| 177 | return 0; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 180 | static PyMappingMethods dbm_as_mapping = { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 181 | (inquiry)dbm_length, /*mp_length*/ |
| 182 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 183 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 186 | static char dbm_close__doc__[] = "\ |
| 187 | close() -> None\n\ |
Guido van Rossum | cef113c | 1998-04-13 18:11:55 +0000 | [diff] [blame] | 188 | Closes the database."; |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 189 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 190 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 191 | dbm_close(register dbmobject *dp, PyObject *args) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 192 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 193 | if (!PyArg_ParseTuple(args, ":close")) |
| 194 | return NULL; |
| 195 | if (dp->di_dbm) |
| 196 | gdbm_close(dp->di_dbm); |
| 197 | dp->di_dbm = NULL; |
| 198 | Py_INCREF(Py_None); |
| 199 | return Py_None; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 202 | static char dbm_keys__doc__[] = "\ |
| 203 | keys() -> list_of_keys\n\ |
| 204 | Get a list of all keys in the database."; |
| 205 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 206 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 207 | dbm_keys(register dbmobject *dp, PyObject *args) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 208 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 209 | register PyObject *v, *item; |
| 210 | datum key, nextkey; |
| 211 | int err; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 212 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 213 | if (dp == NULL || !is_dbmobject(dp)) { |
| 214 | PyErr_BadInternalCall(); |
| 215 | return NULL; |
| 216 | } |
| 217 | if (!PyArg_ParseTuple(args, ":keys")) |
| 218 | return NULL; |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 219 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 220 | check_dbmobject_open(dp); |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 221 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 222 | v = PyList_New(0); |
| 223 | if (v == NULL) |
| 224 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 225 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 226 | key = gdbm_firstkey(dp->di_dbm); |
| 227 | while (key.dptr) { |
| 228 | item = PyString_FromStringAndSize(key.dptr, key.dsize); |
| 229 | if (item == NULL) { |
| 230 | free(key.dptr); |
| 231 | Py_DECREF(v); |
| 232 | return NULL; |
| 233 | } |
| 234 | err = PyList_Append(v, item); |
| 235 | Py_DECREF(item); |
| 236 | if (err != 0) { |
| 237 | free(key.dptr); |
| 238 | Py_DECREF(v); |
| 239 | return NULL; |
| 240 | } |
| 241 | nextkey = gdbm_nextkey(dp->di_dbm, key); |
| 242 | free(key.dptr); |
| 243 | key = nextkey; |
| 244 | } |
| 245 | return v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 248 | static char dbm_has_key__doc__[] = "\ |
| 249 | has_key(key) -> boolean\n\ |
| 250 | Find out whether or not the database contains a given key."; |
| 251 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 252 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 253 | dbm_has_key(register dbmobject *dp, PyObject *args) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 254 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 255 | datum key; |
| 256 | |
| 257 | if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &key.dsize)) |
| 258 | return NULL; |
| 259 | check_dbmobject_open(dp); |
| 260 | return PyInt_FromLong((long) gdbm_exists(dp->di_dbm, key)); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 263 | static char dbm_firstkey__doc__[] = "\ |
| 264 | firstkey() -> key\n\ |
| 265 | It's possible to loop over every key in the database using this method\n\ |
| 266 | and the nextkey() method. The traversal is ordered by GDBM's internal\n\ |
| 267 | hash values, and won't be sorted by the key values. This method\n\ |
| 268 | returns the starting key."; |
| 269 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 270 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 271 | dbm_firstkey(register dbmobject *dp, PyObject *args) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 272 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 273 | register PyObject *v; |
| 274 | datum key; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 275 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 276 | if (!PyArg_ParseTuple(args, ":firstkey")) |
| 277 | return NULL; |
| 278 | check_dbmobject_open(dp); |
| 279 | key = gdbm_firstkey(dp->di_dbm); |
| 280 | if (key.dptr) { |
| 281 | v = PyString_FromStringAndSize(key.dptr, key.dsize); |
| 282 | free(key.dptr); |
| 283 | return v; |
| 284 | } |
| 285 | else { |
| 286 | Py_INCREF(Py_None); |
| 287 | return Py_None; |
| 288 | } |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 291 | static char dbm_nextkey__doc__[] = "\ |
| 292 | nextkey(key) -> next_key\n\ |
| 293 | Returns the key that follows key in the traversal.\n\ |
| 294 | The following code prints every key in the database db, without having\n\ |
| 295 | to create a list in memory that contains them all:\n\ |
| 296 | \n\ |
| 297 | k = db.firstkey()\n\ |
| 298 | while k != None:\n\ |
| 299 | print k\n\ |
| 300 | k = db.nextkey(k)"; |
| 301 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 302 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 303 | dbm_nextkey(register dbmobject *dp, PyObject *args) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 304 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 305 | register PyObject *v; |
| 306 | datum key, nextkey; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 307 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 308 | if (!PyArg_ParseTuple(args, "s#:nextkey", &key.dptr, &key.dsize)) |
| 309 | return NULL; |
| 310 | check_dbmobject_open(dp); |
| 311 | nextkey = gdbm_nextkey(dp->di_dbm, key); |
| 312 | if (nextkey.dptr) { |
| 313 | v = PyString_FromStringAndSize(nextkey.dptr, nextkey.dsize); |
| 314 | free(nextkey.dptr); |
| 315 | return v; |
| 316 | } |
| 317 | else { |
| 318 | Py_INCREF(Py_None); |
| 319 | return Py_None; |
| 320 | } |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 323 | static char dbm_reorganize__doc__[] = "\ |
| 324 | reorganize() -> None\n\ |
| 325 | If you have carried out a lot of deletions and would like to shrink\n\ |
| 326 | the space used by the GDBM file, this routine will reorganize the\n\ |
| 327 | database. GDBM will not shorten the length of a database file except\n\ |
| 328 | by using this reorganization; otherwise, deleted file space will be\n\ |
| 329 | kept and reused as new (key,value) pairs are added."; |
| 330 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 331 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 332 | dbm_reorganize(register dbmobject *dp, PyObject *args) |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 333 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 334 | if (!PyArg_ParseTuple(args, ":reorganize")) |
| 335 | return NULL; |
| 336 | check_dbmobject_open(dp); |
| 337 | errno = 0; |
| 338 | if (gdbm_reorganize(dp->di_dbm) < 0) { |
| 339 | if (errno != 0) |
| 340 | PyErr_SetFromErrno(DbmError); |
| 341 | else |
| 342 | PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno)); |
| 343 | return NULL; |
| 344 | } |
| 345 | Py_INCREF(Py_None); |
| 346 | return Py_None; |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 349 | static char dbm_sync__doc__[] = "\ |
| 350 | sync() -> None\n\ |
| 351 | When the database has been opened in fast mode, this method forces\n\ |
| 352 | any unwritten data to be written to the disk."; |
| 353 | |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 354 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 355 | dbm_sync(register dbmobject *dp, PyObject *args) |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 356 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 357 | if (!PyArg_ParseTuple(args, ":sync")) |
| 358 | return NULL; |
| 359 | check_dbmobject_open(dp); |
| 360 | gdbm_sync(dp->di_dbm); |
| 361 | Py_INCREF(Py_None); |
| 362 | return Py_None; |
Roger E. Masse | e5a9c8f | 1997-03-25 17:39:56 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 365 | static PyMethodDef dbm_methods[] = { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 366 | {"close", (PyCFunction)dbm_close, METH_VARARGS, dbm_close__doc__}, |
| 367 | {"keys", (PyCFunction)dbm_keys, METH_VARARGS, dbm_keys__doc__}, |
| 368 | {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS, dbm_has_key__doc__}, |
| 369 | {"firstkey", (PyCFunction)dbm_firstkey,METH_VARARGS, dbm_firstkey__doc__}, |
| 370 | {"nextkey", (PyCFunction)dbm_nextkey, METH_VARARGS, dbm_nextkey__doc__}, |
| 371 | {"reorganize",(PyCFunction)dbm_reorganize,METH_VARARGS, dbm_reorganize__doc__}, |
| 372 | {"sync", (PyCFunction)dbm_sync, METH_VARARGS, dbm_sync__doc__}, |
| 373 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 374 | }; |
| 375 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 376 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 377 | dbm_getattr(dbmobject *dp, char *name) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 378 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 379 | return Py_FindMethod(dbm_methods, (PyObject *)dp, name); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 382 | static PyTypeObject Dbmtype = { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 383 | PyObject_HEAD_INIT(0) |
| 384 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 385 | "gdbm.gdbm", |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 386 | sizeof(dbmobject), |
| 387 | 0, |
| 388 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
| 389 | 0, /*tp_print*/ |
| 390 | (getattrfunc)dbm_getattr, /*tp_getattr*/ |
| 391 | 0, /*tp_setattr*/ |
| 392 | 0, /*tp_compare*/ |
| 393 | 0, /*tp_repr*/ |
| 394 | 0, /*tp_as_number*/ |
| 395 | 0, /*tp_as_sequence*/ |
| 396 | &dbm_as_mapping, /*tp_as_mapping*/ |
| 397 | 0, /*tp_hash*/ |
| 398 | 0, /*tp_call*/ |
| 399 | 0, /*tp_str*/ |
| 400 | 0, /*tp_getattro*/ |
| 401 | 0, /*tp_setattro*/ |
| 402 | 0, /*tp_as_buffer*/ |
| 403 | 0, /*tp_xxx4*/ |
| 404 | gdbm_object__doc__, /*tp_doc*/ |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | /* ----------------------------------------------------------------- */ |
| 408 | |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 409 | static char dbmopen__doc__[] = "\ |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 410 | open(filename, [flags, [mode]]) -> dbm_object\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 411 | Open a dbm database and return a dbm object. The filename argument is\n\ |
| 412 | the name of the database file.\n\ |
| 413 | \n\ |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 414 | The optional flags argument can be 'r' (to open an existing database\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 415 | for reading only -- default), 'w' (to open an existing database for\n\ |
| 416 | reading and writing), 'c' (which creates the database if it doesn't\n\ |
| 417 | exist), or 'n' (which always creates a new empty database).\n\ |
| 418 | \n\ |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 419 | Some versions of gdbm support additional flags which must be\n\ |
| 420 | appended to one of the flags described above. The module constant\n\ |
| 421 | 'open_flags' is a string of valid additional flags. The 'f' flag\n\ |
| 422 | opens the database in fast mode; altered data will not automatically\n\ |
| 423 | be written to the disk after every change. This results in faster\n\ |
| 424 | writes to the database, but may result in an inconsistent database\n\ |
| 425 | if the program crashes while the database is still open. Use the\n\ |
| 426 | sync() method to force any unwritten data to be written to the disk.\n\ |
| 427 | The 's' flag causes all database operations to be synchronized to\n\ |
| 428 | disk. The 'u' flag disables locking of the database file.\n\ |
Guido van Rossum | bfc49e8 | 1998-03-03 22:02:24 +0000 | [diff] [blame] | 429 | \n\ |
| 430 | The optional mode argument is the Unix mode of the file, used only\n\ |
| 431 | when the database has to be created. It defaults to octal 0666. "; |
| 432 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 433 | static PyObject * |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 434 | dbmopen(PyObject *self, PyObject *args) |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 435 | { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 436 | char *name; |
| 437 | char *flags = "r "; |
| 438 | int iflags; |
| 439 | int mode = 0666; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 440 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 441 | if (!PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode)) |
| 442 | return NULL; |
| 443 | switch (flags[0]) { |
| 444 | case 'r': |
| 445 | iflags = GDBM_READER; |
| 446 | break; |
| 447 | case 'w': |
| 448 | iflags = GDBM_WRITER; |
| 449 | break; |
| 450 | case 'c': |
| 451 | iflags = GDBM_WRCREAT; |
| 452 | break; |
| 453 | case 'n': |
| 454 | iflags = GDBM_NEWDB; |
| 455 | break; |
| 456 | default: |
| 457 | PyErr_SetString(DbmError, |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 458 | "First flag must be one of 'r', 'w', 'c' or 'n'"); |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 459 | return NULL; |
| 460 | } |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 461 | for (flags++; *flags != '\0'; flags++) { |
| 462 | char buf[40]; |
| 463 | switch (*flags) { |
| 464 | #ifdef GDBM_FAST |
| 465 | case 'f': |
| 466 | iflags |= GDBM_FAST; |
| 467 | break; |
| 468 | #endif |
| 469 | #ifdef GDBM_SYNC |
| 470 | case 's': |
| 471 | iflags |= GDBM_SYNC; |
| 472 | break; |
| 473 | #endif |
| 474 | #ifdef GDBM_NOLOCK |
| 475 | case 'u': |
| 476 | iflags |= GDBM_NOLOCK; |
| 477 | break; |
| 478 | #endif |
| 479 | default: |
Tim Peters | 885d457 | 2001-11-28 20:27:42 +0000 | [diff] [blame] | 480 | PyOS_snprintf(buf, sizeof(buf), "Flag '%c' is not supported.", |
| 481 | *flags); |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 482 | PyErr_SetString(DbmError, buf); |
| 483 | return NULL; |
| 484 | } |
| 485 | } |
| 486 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 487 | return newdbmobject(name, iflags, mode); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 490 | static char dbmmodule_open_flags[] = "rwcn" |
| 491 | #ifdef GDBM_FAST |
| 492 | "f" |
| 493 | #endif |
| 494 | #ifdef GDBM_SYNC |
| 495 | "s" |
| 496 | #endif |
| 497 | #ifdef GDBM_NOLOCK |
| 498 | "u" |
| 499 | #endif |
| 500 | ; |
| 501 | |
Roger E. Masse | b15bef8 | 1996-12-17 19:55:33 +0000 | [diff] [blame] | 502 | static PyMethodDef dbmmodule_methods[] = { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 503 | { "open", (PyCFunction)dbmopen, METH_VARARGS, dbmopen__doc__}, |
| 504 | { 0, 0 }, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 505 | }; |
| 506 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 507 | DL_EXPORT(void) |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 508 | initgdbm(void) { |
Martin v. Löwis | 00ff10c | 2001-11-11 14:24:05 +0000 | [diff] [blame] | 509 | PyObject *m, *d, *s; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 510 | |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 511 | Dbmtype.ob_type = &PyType_Type; |
| 512 | m = Py_InitModule4("gdbm", dbmmodule_methods, |
| 513 | gdbmmodule__doc__, (PyObject *)NULL, |
| 514 | PYTHON_API_VERSION); |
| 515 | d = PyModule_GetDict(m); |
| 516 | DbmError = PyErr_NewException("gdbm.error", NULL, NULL); |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 517 | if (DbmError != NULL) { |
Fred Drake | e3a41c6 | 2000-07-08 05:00:07 +0000 | [diff] [blame] | 518 | PyDict_SetItemString(d, "error", DbmError); |
Martin v. Löwis | 00ff10c | 2001-11-11 14:24:05 +0000 | [diff] [blame] | 519 | s = PyString_FromString(dbmmodule_open_flags); |
| 520 | PyDict_SetItemString(d, "open_flags", s); |
| 521 | Py_DECREF(s); |
Neil Schemenauer | e9e860f | 2000-12-17 07:14:13 +0000 | [diff] [blame] | 522 | } |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 523 | } |