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 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 73 | static Py_ssize_t |
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 = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 165 | (lenfunc)dbm_length, /*mp_length*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 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 * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 171 | dbm__close(register dbmobject *dp, PyObject *unused) |
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 (dp->di_dbm) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 174 | dbm_close(dp->di_dbm); |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 175 | dp->di_dbm = NULL; |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 176 | Py_INCREF(Py_None); |
| 177 | return Py_None; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 180 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 181 | dbm_keys(register dbmobject *dp, PyObject *unused) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 182 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 183 | register PyObject *v, *item; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 184 | datum key; |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 185 | int err; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 187 | check_dbmobject_open(dp); |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 188 | v = PyList_New(0); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 189 | if (v == NULL) |
| 190 | return NULL; |
| 191 | for (key = dbm_firstkey(dp->di_dbm); key.dptr; |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 192 | key = dbm_nextkey(dp->di_dbm)) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 193 | item = PyString_FromStringAndSize(key.dptr, key.dsize); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 194 | if (item == NULL) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 195 | Py_DECREF(v); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 196 | return NULL; |
| 197 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 198 | err = PyList_Append(v, item); |
| 199 | Py_DECREF(item); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 200 | if (err != 0) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 201 | Py_DECREF(v); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 202 | return NULL; |
| 203 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 204 | } |
| 205 | return v; |
| 206 | } |
| 207 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 208 | static int |
| 209 | dbm_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 210 | { |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 211 | dbmobject *dp = (dbmobject *)self; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 212 | datum key, val; |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 213 | |
| 214 | if ((dp)->di_dbm == NULL) { |
| 215 | PyErr_SetString(DbmError, |
| 216 | "DBM object has already been closed"); |
| 217 | return -1; |
| 218 | } |
| 219 | if (!PyString_Check(arg)) { |
| 220 | PyErr_Format(PyExc_TypeError, |
| 221 | "dbm key must be string, not %.100s", |
| 222 | arg->ob_type->tp_name); |
| 223 | return -1; |
| 224 | } |
| 225 | key.dptr = PyString_AS_STRING(arg); |
| 226 | key.dsize = PyString_GET_SIZE(arg); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 227 | val = dbm_fetch(dp->di_dbm, key); |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 228 | return val.dptr != NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 231 | static PySequenceMethods dbm_as_sequence = { |
| 232 | 0, /* sq_length */ |
| 233 | 0, /* sq_concat */ |
| 234 | 0, /* sq_repeat */ |
| 235 | 0, /* sq_item */ |
| 236 | 0, /* sq_slice */ |
| 237 | 0, /* sq_ass_item */ |
| 238 | 0, /* sq_ass_slice */ |
| 239 | dbm_contains, /* sq_contains */ |
| 240 | 0, /* sq_inplace_concat */ |
| 241 | 0, /* sq_inplace_repeat */ |
| 242 | }; |
| 243 | |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 244 | static PyObject * |
| 245 | dbm_get(register dbmobject *dp, PyObject *args) |
| 246 | { |
| 247 | datum key, val; |
| 248 | PyObject *defvalue = Py_None; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 249 | char *tmp_ptr; |
Neal Norwitz | 8d4c981 | 2002-11-22 23:29:31 +0000 | [diff] [blame] | 250 | int tmp_size; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 251 | |
| 252 | if (!PyArg_ParseTuple(args, "s#|O:get", |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 253 | &tmp_ptr, &tmp_size, &defvalue)) |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 254 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 255 | key.dptr = tmp_ptr; |
Neal Norwitz | 8d4c981 | 2002-11-22 23:29:31 +0000 | [diff] [blame] | 256 | key.dsize = tmp_size; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 257 | check_dbmobject_open(dp); |
| 258 | val = dbm_fetch(dp->di_dbm, key); |
| 259 | if (val.dptr != NULL) |
| 260 | return PyString_FromStringAndSize(val.dptr, val.dsize); |
| 261 | else { |
| 262 | Py_INCREF(defvalue); |
| 263 | return defvalue; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static PyObject * |
| 268 | dbm_setdefault(register dbmobject *dp, PyObject *args) |
| 269 | { |
| 270 | datum key, val; |
| 271 | PyObject *defvalue = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 272 | char *tmp_ptr; |
Neal Norwitz | 8d4c981 | 2002-11-22 23:29:31 +0000 | [diff] [blame] | 273 | int tmp_size; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 274 | |
| 275 | if (!PyArg_ParseTuple(args, "s#|S:setdefault", |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 276 | &tmp_ptr, &tmp_size, &defvalue)) |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 277 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 278 | key.dptr = tmp_ptr; |
Neal Norwitz | 8d4c981 | 2002-11-22 23:29:31 +0000 | [diff] [blame] | 279 | key.dsize = tmp_size; |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 280 | check_dbmobject_open(dp); |
| 281 | val = dbm_fetch(dp->di_dbm, key); |
| 282 | if (val.dptr != NULL) |
| 283 | return PyString_FromStringAndSize(val.dptr, val.dsize); |
| 284 | if (defvalue == NULL) { |
| 285 | defvalue = PyString_FromStringAndSize(NULL, 0); |
| 286 | if (defvalue == NULL) |
| 287 | return NULL; |
| 288 | } |
| 289 | else |
| 290 | Py_INCREF(defvalue); |
| 291 | val.dptr = PyString_AS_STRING(defvalue); |
| 292 | val.dsize = PyString_GET_SIZE(defvalue); |
| 293 | if (dbm_store(dp->di_dbm, key, val, DBM_INSERT) < 0) { |
| 294 | dbm_clearerr(dp->di_dbm); |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 295 | PyErr_SetString(DbmError, "cannot add item to database"); |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 296 | return NULL; |
| 297 | } |
| 298 | return defvalue; |
| 299 | } |
| 300 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 301 | static PyMethodDef dbm_methods[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 302 | {"close", (PyCFunction)dbm__close, METH_NOARGS, |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 303 | "close()\nClose the database."}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 304 | {"keys", (PyCFunction)dbm_keys, METH_NOARGS, |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 305 | "keys() -> list\nReturn a list of all keys in the database."}, |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 306 | {"get", (PyCFunction)dbm_get, METH_VARARGS, |
| 307 | "get(key[, default]) -> value\n" |
| 308 | "Return the value for key if present, otherwise default."}, |
| 309 | {"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS, |
| 310 | "setdefault(key[, default]) -> value\n" |
| 311 | "Return the value for key if present, otherwise default. If key\n" |
| 312 | "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] | 313 | {NULL, NULL} /* sentinel */ |
| 314 | }; |
| 315 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 316 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 317 | dbm_getattr(dbmobject *dp, char *name) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 318 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 319 | return Py_FindMethod(dbm_methods, (PyObject *)dp, name); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 322 | static PyTypeObject Dbmtype = { |
Guido van Rossum | a120ffc | 2001-01-22 15:29:14 +0000 | [diff] [blame] | 323 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 324 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 325 | "dbm.dbm", |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 326 | sizeof(dbmobject), |
| 327 | 0, |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 328 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
| 329 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 330 | (getattrfunc)dbm_getattr, /*tp_getattr*/ |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 331 | 0, /*tp_setattr*/ |
| 332 | 0, /*tp_compare*/ |
| 333 | 0, /*tp_repr*/ |
| 334 | 0, /*tp_as_number*/ |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 335 | &dbm_as_sequence, /*tp_as_sequence*/ |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 336 | &dbm_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /* ----------------------------------------------------------------- */ |
| 340 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 341 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 342 | dbmopen(PyObject *self, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 343 | { |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 344 | char *name; |
| 345 | char *flags = "r"; |
| 346 | int iflags; |
| 347 | int mode = 0666; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 348 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 349 | if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 350 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 351 | if ( strcmp(flags, "r") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 352 | iflags = O_RDONLY; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 353 | else if ( strcmp(flags, "w") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 354 | iflags = O_RDWR; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 355 | else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */ |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 356 | iflags = O_RDWR|O_CREAT; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 357 | else if ( strcmp(flags, "c") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 358 | iflags = O_RDWR|O_CREAT; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 359 | else if ( strcmp(flags, "n") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 360 | iflags = O_RDWR|O_CREAT|O_TRUNC; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 361 | else { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 362 | PyErr_SetString(DbmError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 363 | "arg 2 to open should be 'r', 'w', 'c', or 'n'"); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 364 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 365 | } |
| 366 | return newdbmobject(name, iflags, mode); |
| 367 | } |
| 368 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 369 | static PyMethodDef dbmmodule_methods[] = { |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 370 | { "open", (PyCFunction)dbmopen, METH_VARARGS, |
| 371 | "open(path[, flag[, mode]]) -> mapping\n" |
| 372 | "Return a database object."}, |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 373 | { 0, 0 }, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 374 | }; |
| 375 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 376 | PyMODINIT_FUNC |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 377 | initdbm(void) { |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 378 | PyObject *m, *d, *s; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | ef67111 | 2006-08-19 23:11:47 +0000 | [diff] [blame] | 380 | if (PyType_Ready(&Dbmtype) < 0) |
| 381 | return; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 382 | m = Py_InitModule("dbm", dbmmodule_methods); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 383 | if (m == NULL) |
| 384 | return; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 385 | d = PyModule_GetDict(m); |
Fred Drake | 1ac526d | 2000-09-15 21:35:14 +0000 | [diff] [blame] | 386 | if (DbmError == NULL) |
| 387 | DbmError = PyErr_NewException("dbm.error", NULL, NULL); |
| 388 | s = PyString_FromString(which_dbm); |
| 389 | if (s != NULL) { |
| 390 | PyDict_SetItemString(d, "library", s); |
| 391 | Py_DECREF(s); |
| 392 | } |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 393 | if (DbmError != NULL) |
| 394 | PyDict_SetItemString(d, "error", DbmError); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 395 | } |