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