Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 1 | |
| 2 | /* DBM module using dictionary interface */ |
| 3 | |
| 4 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 6 | |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <fcntl.h> |
Fred Drake | d94f707 | 2000-09-14 15:48:06 +0000 | [diff] [blame] | 10 | |
| 11 | /* Some Linux systems install gdbm/ndbm.h, but not ndbm.h. This supports |
| 12 | * whichever configure was able to locate. |
| 13 | */ |
| 14 | #if defined(HAVE_NDBM_H) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 15 | #include <ndbm.h> |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 16 | static char *which_dbm = "ndbm"; |
Fred Drake | ae90f8d | 2000-09-15 03:38:12 +0000 | [diff] [blame] | 17 | #elif defined(HAVE_DB1_NDBM_H) |
| 18 | #include <db1/ndbm.h> |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 19 | static char *which_dbm = "BSD db"; |
Fred Drake | d94f707 | 2000-09-14 15:48:06 +0000 | [diff] [blame] | 20 | #elif defined(HAVE_GDBM_NDBM_H) |
| 21 | #include <gdbm/ndbm.h> |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 22 | static char *which_dbm = "GNU gdbm"; |
Fred Drake | d94f707 | 2000-09-14 15:48:06 +0000 | [diff] [blame] | 23 | #else |
| 24 | #error "No ndbm.h available!" |
| 25 | #endif |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 26 | |
| 27 | typedef struct { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 28 | PyObject_HEAD |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 29 | int di_size; /* -1 means recompute */ |
| 30 | DBM *di_dbm; |
| 31 | } dbmobject; |
| 32 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 33 | staticforward PyTypeObject Dbmtype; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 34 | |
| 35 | #define is_dbmobject(v) ((v)->ob_type == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 36 | #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \ |
| 37 | { PyErr_SetString(DbmError, "DBM object has already been closed"); \ |
| 38 | return NULL; } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 39 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 40 | static PyObject *DbmError; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 41 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 42 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 43 | newdbmobject(char *file, int flags, int mode) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 44 | { |
| 45 | dbmobject *dp; |
| 46 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 47 | dp = PyObject_New(dbmobject, &Dbmtype); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 48 | if (dp == NULL) |
| 49 | return NULL; |
| 50 | dp->di_size = -1; |
| 51 | if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 52 | PyErr_SetFromErrno(DbmError); |
| 53 | Py_DECREF(dp); |
| 54 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 55 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 56 | return (PyObject *)dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* Methods */ |
| 60 | |
| 61 | static void |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 62 | dbm_dealloc(register dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 63 | { |
| 64 | if ( dp->di_dbm ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 65 | dbm_close(dp->di_dbm); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 66 | PyObject_Del(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 70 | dbm_length(dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 71 | { |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 72 | if (dp->di_dbm == NULL) { |
| 73 | PyErr_SetString(DbmError, "DBM object has already been closed"); |
| 74 | return -1; |
| 75 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 76 | if ( dp->di_size < 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 77 | datum key; |
| 78 | int size; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 79 | |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 80 | size = 0; |
| 81 | for ( key=dbm_firstkey(dp->di_dbm); key.dptr; |
| 82 | key = dbm_nextkey(dp->di_dbm)) |
| 83 | size++; |
| 84 | dp->di_size = size; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 85 | } |
| 86 | return dp->di_size; |
| 87 | } |
| 88 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 89 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 90 | dbm_subscript(dbmobject *dp, register PyObject *key) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 91 | { |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 92 | datum drec, krec; |
| 93 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 94 | if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) ) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 95 | return NULL; |
| 96 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 97 | check_dbmobject_open(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 98 | drec = dbm_fetch(dp->di_dbm, krec); |
| 99 | if ( drec.dptr == 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 100 | PyErr_SetString(PyExc_KeyError, |
| 101 | PyString_AS_STRING((PyStringObject *)key)); |
| 102 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 103 | } |
| 104 | if ( dbm_error(dp->di_dbm) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 105 | dbm_clearerr(dp->di_dbm); |
| 106 | PyErr_SetString(DbmError, ""); |
| 107 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 108 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 109 | return PyString_FromStringAndSize(drec.dptr, drec.dsize); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 113 | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 114 | { |
| 115 | datum krec, drec; |
| 116 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 117 | if ( !PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 118 | PyErr_SetString(PyExc_TypeError, |
| 119 | "dbm mappings have string indices only"); |
| 120 | return -1; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 121 | } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 122 | if (dp->di_dbm == NULL) { |
| 123 | PyErr_SetString(DbmError, "DBM object has already been closed"); |
| 124 | return -1; |
| 125 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 126 | dp->di_size = -1; |
| 127 | if (w == NULL) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 128 | if ( dbm_delete(dp->di_dbm, krec) < 0 ) { |
| 129 | dbm_clearerr(dp->di_dbm); |
| 130 | PyErr_SetString(PyExc_KeyError, |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 131 | PyString_AS_STRING((PyStringObject *)v)); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 132 | return -1; |
| 133 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 134 | } else { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 135 | if ( !PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize) ) { |
| 136 | PyErr_SetString(PyExc_TypeError, |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 137 | "dbm mappings have string elements only"); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| 140 | if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) { |
| 141 | dbm_clearerr(dp->di_dbm); |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 142 | PyErr_SetString(DbmError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 143 | "cannot add item to database"); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 144 | return -1; |
| 145 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 146 | } |
| 147 | if ( dbm_error(dp->di_dbm) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 148 | dbm_clearerr(dp->di_dbm); |
| 149 | PyErr_SetString(DbmError, ""); |
| 150 | return -1; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 155 | static PyMappingMethods dbm_as_mapping = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 156 | (inquiry)dbm_length, /*mp_length*/ |
| 157 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 158 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 161 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 162 | dbm__close(register dbmobject *dp, PyObject *args) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 163 | { |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 164 | if (!PyArg_ParseTuple(args, ":close")) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 165 | return NULL; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 166 | if (dp->di_dbm) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 167 | dbm_close(dp->di_dbm); |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 168 | dp->di_dbm = NULL; |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 169 | Py_INCREF(Py_None); |
| 170 | return Py_None; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 173 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 174 | dbm_keys(register dbmobject *dp, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 175 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 176 | register PyObject *v, *item; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 177 | datum key; |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 178 | int err; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 179 | |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 180 | if (!PyArg_ParseTuple(args, ":keys")) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 181 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 182 | check_dbmobject_open(dp); |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 183 | v = PyList_New(0); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 184 | if (v == NULL) |
| 185 | return NULL; |
| 186 | for (key = dbm_firstkey(dp->di_dbm); key.dptr; |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 187 | key = dbm_nextkey(dp->di_dbm)) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 188 | item = PyString_FromStringAndSize(key.dptr, key.dsize); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 189 | if (item == NULL) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 190 | Py_DECREF(v); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 193 | err = PyList_Append(v, item); |
| 194 | Py_DECREF(item); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 195 | if (err != 0) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 196 | Py_DECREF(v); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 197 | return NULL; |
| 198 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 199 | } |
| 200 | return v; |
| 201 | } |
| 202 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 203 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 204 | dbm_has_key(register dbmobject *dp, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 205 | { |
| 206 | datum key, val; |
| 207 | |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 208 | if (!PyArg_ParseTuple(args, "s#:has_key", &key.dptr, &key.dsize)) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 209 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 210 | check_dbmobject_open(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 211 | val = dbm_fetch(dp->di_dbm, key); |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 212 | return PyInt_FromLong(val.dptr != NULL); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 215 | static PyObject * |
| 216 | dbm_get(register dbmobject *dp, PyObject *args) |
| 217 | { |
| 218 | datum key, val; |
| 219 | PyObject *defvalue = Py_None; |
| 220 | |
| 221 | if (!PyArg_ParseTuple(args, "s#|O:get", |
| 222 | &key.dptr, &key.dsize, &defvalue)) |
| 223 | return NULL; |
| 224 | check_dbmobject_open(dp); |
| 225 | val = dbm_fetch(dp->di_dbm, key); |
| 226 | if (val.dptr != NULL) |
| 227 | return PyString_FromStringAndSize(val.dptr, val.dsize); |
| 228 | else { |
| 229 | Py_INCREF(defvalue); |
| 230 | return defvalue; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static PyObject * |
| 235 | dbm_setdefault(register dbmobject *dp, PyObject *args) |
| 236 | { |
| 237 | datum key, val; |
| 238 | PyObject *defvalue = NULL; |
| 239 | |
| 240 | if (!PyArg_ParseTuple(args, "s#|S:setdefault", |
| 241 | &key.dptr, &key.dsize, &defvalue)) |
| 242 | return NULL; |
| 243 | check_dbmobject_open(dp); |
| 244 | val = dbm_fetch(dp->di_dbm, key); |
| 245 | if (val.dptr != NULL) |
| 246 | return PyString_FromStringAndSize(val.dptr, val.dsize); |
| 247 | if (defvalue == NULL) { |
| 248 | defvalue = PyString_FromStringAndSize(NULL, 0); |
| 249 | if (defvalue == NULL) |
| 250 | return NULL; |
| 251 | } |
| 252 | else |
| 253 | Py_INCREF(defvalue); |
| 254 | val.dptr = PyString_AS_STRING(defvalue); |
| 255 | val.dsize = PyString_GET_SIZE(defvalue); |
| 256 | if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) { |
| 257 | dbm_clearerr(dp->di_dbm); |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 258 | PyErr_SetString(DbmError, "cannot add item to database"); |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 259 | return NULL; |
| 260 | } |
| 261 | return defvalue; |
| 262 | } |
| 263 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 264 | static PyMethodDef dbm_methods[] = { |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 265 | {"close", (PyCFunction)dbm__close, METH_VARARGS, |
| 266 | "close()\nClose the database."}, |
| 267 | {"keys", (PyCFunction)dbm_keys, METH_VARARGS, |
| 268 | "keys() -> list\nReturn a list of all keys in the database."}, |
| 269 | {"has_key", (PyCFunction)dbm_has_key, METH_VARARGS, |
| 270 | "has_key(key} -> boolean\nReturn true iff key is in the database."}, |
| 271 | {"get", (PyCFunction)dbm_get, METH_VARARGS, |
| 272 | "get(key[, default]) -> value\n" |
| 273 | "Return the value for key if present, otherwise default."}, |
| 274 | {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS, |
| 275 | "setdefault(key[, default]) -> value\n" |
| 276 | "Return the value for key if present, otherwise default. If key\n" |
| 277 | "is not in the database, it is inserted with default as the value."}, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 278 | {NULL, NULL} /* sentinel */ |
| 279 | }; |
| 280 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 281 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 282 | dbm_getattr(dbmobject *dp, char *name) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 283 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 284 | return Py_FindMethod(dbm_methods, (PyObject *)dp, name); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 287 | static PyTypeObject Dbmtype = { |
Guido van Rossum | a120ffc | 2001-01-22 15:29:14 +0000 | [diff] [blame] | 288 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 289 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 290 | "dbm.dbm", |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 291 | sizeof(dbmobject), |
| 292 | 0, |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 293 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
| 294 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 295 | (getattrfunc)dbm_getattr, /*tp_getattr*/ |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 296 | 0, /*tp_setattr*/ |
| 297 | 0, /*tp_compare*/ |
| 298 | 0, /*tp_repr*/ |
| 299 | 0, /*tp_as_number*/ |
| 300 | 0, /*tp_as_sequence*/ |
| 301 | &dbm_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | /* ----------------------------------------------------------------- */ |
| 305 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 306 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 307 | dbmopen(PyObject *self, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 308 | { |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 309 | char *name; |
| 310 | char *flags = "r"; |
| 311 | int iflags; |
| 312 | int mode = 0666; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 314 | if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 315 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 316 | if ( strcmp(flags, "r") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 317 | iflags = O_RDONLY; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 318 | else if ( strcmp(flags, "w") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 319 | iflags = O_RDWR; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 320 | else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */ |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 321 | iflags = O_RDWR|O_CREAT; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 322 | else if ( strcmp(flags, "c") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 323 | iflags = O_RDWR|O_CREAT; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 324 | else if ( strcmp(flags, "n") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 325 | iflags = O_RDWR|O_CREAT|O_TRUNC; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 326 | else { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 327 | PyErr_SetString(DbmError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 328 | "arg 2 to open should be 'r', 'w', 'c', or 'n'"); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 329 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 330 | } |
| 331 | return newdbmobject(name, iflags, mode); |
| 332 | } |
| 333 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 334 | static PyMethodDef dbmmodule_methods[] = { |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 335 | { "open", (PyCFunction)dbmopen, METH_VARARGS, |
| 336 | "open(path[, flag[, mode]]) -> mapping\n" |
| 337 | "Return a database object."}, |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 338 | { 0, 0 }, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 339 | }; |
| 340 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 341 | DL_EXPORT(void) |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 342 | initdbm(void) { |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 343 | PyObject *m, *d, *s; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | a120ffc | 2001-01-22 15:29:14 +0000 | [diff] [blame] | 345 | Dbmtype.ob_type = &PyType_Type; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 346 | m = Py_InitModule("dbm", dbmmodule_methods); |
| 347 | d = PyModule_GetDict(m); |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 348 | if (DbmError == NULL) |
| 349 | DbmError = PyErr_NewException("dbm.error", NULL, NULL); |
| 350 | s = PyString_FromString(which_dbm); |
| 351 | if (s != NULL) { |
| 352 | PyDict_SetItemString(d, "library", s); |
| 353 | Py_DECREF(s); |
| 354 | } |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 355 | if (DbmError != NULL) |
| 356 | PyDict_SetItemString(d, "error", DbmError); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 357 | } |