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