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