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> |
| 10 | #include <ndbm.h> |
| 11 | |
| 12 | typedef struct { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 13 | PyObject_HEAD |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 14 | int di_size; /* -1 means recompute */ |
| 15 | DBM *di_dbm; |
| 16 | } dbmobject; |
| 17 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 18 | staticforward PyTypeObject Dbmtype; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 19 | |
| 20 | #define is_dbmobject(v) ((v)->ob_type == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 21 | #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \ |
| 22 | { PyErr_SetString(DbmError, "DBM object has already been closed"); \ |
| 23 | return NULL; } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 24 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 25 | static PyObject *DbmError; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 26 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 27 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 28 | newdbmobject(char *file, int flags, int mode) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 29 | { |
| 30 | dbmobject *dp; |
| 31 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 32 | dp = PyObject_New(dbmobject, &Dbmtype); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 33 | if (dp == NULL) |
| 34 | return NULL; |
| 35 | dp->di_size = -1; |
| 36 | if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 37 | PyErr_SetFromErrno(DbmError); |
| 38 | Py_DECREF(dp); |
| 39 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 40 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 41 | return (PyObject *)dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* Methods */ |
| 45 | |
| 46 | static void |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 47 | dbm_dealloc(register dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 48 | { |
| 49 | if ( dp->di_dbm ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 50 | dbm_close(dp->di_dbm); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 51 | PyObject_Del(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 55 | dbm_length(dbmobject *dp) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 56 | { |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 57 | if (dp->di_dbm == NULL) { |
| 58 | PyErr_SetString(DbmError, "DBM object has already been closed"); |
| 59 | return -1; |
| 60 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 61 | if ( dp->di_size < 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 62 | datum key; |
| 63 | int size; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 64 | |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 65 | size = 0; |
| 66 | for ( key=dbm_firstkey(dp->di_dbm); key.dptr; |
| 67 | key = dbm_nextkey(dp->di_dbm)) |
| 68 | size++; |
| 69 | dp->di_size = size; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 70 | } |
| 71 | return dp->di_size; |
| 72 | } |
| 73 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 74 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 75 | dbm_subscript(dbmobject *dp, register PyObject *key) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 76 | { |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 77 | datum drec, krec; |
| 78 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 79 | if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) ) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 80 | return NULL; |
| 81 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 82 | check_dbmobject_open(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 83 | drec = dbm_fetch(dp->di_dbm, krec); |
| 84 | if ( drec.dptr == 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 85 | PyErr_SetString(PyExc_KeyError, |
| 86 | PyString_AS_STRING((PyStringObject *)key)); |
| 87 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 88 | } |
| 89 | if ( dbm_error(dp->di_dbm) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 90 | dbm_clearerr(dp->di_dbm); |
| 91 | PyErr_SetString(DbmError, ""); |
| 92 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 93 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 94 | return PyString_FromStringAndSize(drec.dptr, drec.dsize); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 98 | dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 99 | { |
| 100 | datum krec, drec; |
| 101 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 102 | if ( !PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 103 | PyErr_SetString(PyExc_TypeError, |
| 104 | "dbm mappings have string indices only"); |
| 105 | return -1; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 106 | } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 107 | if (dp->di_dbm == NULL) { |
| 108 | PyErr_SetString(DbmError, "DBM object has already been closed"); |
| 109 | return -1; |
| 110 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 111 | dp->di_size = -1; |
| 112 | if (w == NULL) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 113 | if ( dbm_delete(dp->di_dbm, krec) < 0 ) { |
| 114 | dbm_clearerr(dp->di_dbm); |
| 115 | PyErr_SetString(PyExc_KeyError, |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 116 | PyString_AS_STRING((PyStringObject *)v)); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 117 | return -1; |
| 118 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 119 | } else { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 120 | if ( !PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize) ) { |
| 121 | PyErr_SetString(PyExc_TypeError, |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 122 | "dbm mappings have string elements only"); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 123 | return -1; |
| 124 | } |
| 125 | if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) { |
| 126 | dbm_clearerr(dp->di_dbm); |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 127 | PyErr_SetString(DbmError, |
| 128 | "Cannot add item to database"); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 129 | return -1; |
| 130 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 131 | } |
| 132 | if ( dbm_error(dp->di_dbm) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 133 | dbm_clearerr(dp->di_dbm); |
| 134 | PyErr_SetString(DbmError, ""); |
| 135 | return -1; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 140 | static PyMappingMethods dbm_as_mapping = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 141 | (inquiry)dbm_length, /*mp_length*/ |
| 142 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 143 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 146 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 147 | dbm__close(register dbmobject *dp, PyObject *args) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 148 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 149 | if ( !PyArg_NoArgs(args) ) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 150 | return NULL; |
| 151 | if ( dp->di_dbm ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 152 | dbm_close(dp->di_dbm); |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 153 | dp->di_dbm = NULL; |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 154 | Py_INCREF(Py_None); |
| 155 | return Py_None; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 158 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 159 | dbm_keys(register dbmobject *dp, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 160 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 161 | register PyObject *v, *item; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 162 | datum key; |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 163 | int err; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 164 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 165 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 166 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 167 | check_dbmobject_open(dp); |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 168 | v = PyList_New(0); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 169 | if (v == NULL) |
| 170 | return NULL; |
| 171 | for (key = dbm_firstkey(dp->di_dbm); key.dptr; |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 172 | key = dbm_nextkey(dp->di_dbm)) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 173 | item = PyString_FromStringAndSize(key.dptr, key.dsize); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 174 | if (item == NULL) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 175 | Py_DECREF(v); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 178 | err = PyList_Append(v, item); |
| 179 | Py_DECREF(item); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 180 | if (err != 0) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 181 | Py_DECREF(v); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 184 | } |
| 185 | return v; |
| 186 | } |
| 187 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 188 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 189 | dbm_has_key(register dbmobject *dp, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 190 | { |
| 191 | datum key, val; |
| 192 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 193 | if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize)) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 194 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 195 | check_dbmobject_open(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 196 | val = dbm_fetch(dp->di_dbm, key); |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 197 | return PyInt_FromLong(val.dptr != NULL); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 200 | static PyMethodDef dbm_methods[] = { |
| 201 | {"close", (PyCFunction)dbm__close}, |
| 202 | {"keys", (PyCFunction)dbm_keys}, |
| 203 | {"has_key", (PyCFunction)dbm_has_key}, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 204 | {NULL, NULL} /* sentinel */ |
| 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_getattr(dbmobject *dp, char *name) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 209 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 210 | return Py_FindMethod(dbm_methods, (PyObject *)dp, name); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 213 | static PyTypeObject Dbmtype = { |
| 214 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 215 | 0, |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 216 | "dbm", |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 217 | sizeof(dbmobject), |
| 218 | 0, |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 219 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
| 220 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | (getattrfunc)dbm_getattr, /*tp_getattr*/ |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 222 | 0, /*tp_setattr*/ |
| 223 | 0, /*tp_compare*/ |
| 224 | 0, /*tp_repr*/ |
| 225 | 0, /*tp_as_number*/ |
| 226 | 0, /*tp_as_sequence*/ |
| 227 | &dbm_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | /* ----------------------------------------------------------------- */ |
| 231 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 232 | static PyObject * |
Peter Schneider-Kamp | cb27c35 | 2000-07-10 17:06:38 +0000 | [diff] [blame] | 233 | dbmopen(PyObject *self, PyObject *args) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 234 | { |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 235 | char *name; |
| 236 | char *flags = "r"; |
| 237 | int iflags; |
| 238 | int mode = 0666; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 240 | if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 241 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 242 | if ( strcmp(flags, "r") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 243 | iflags = O_RDONLY; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 244 | else if ( strcmp(flags, "w") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 245 | iflags = O_RDWR; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 246 | else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */ |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 247 | iflags = O_RDWR|O_CREAT; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 248 | else if ( strcmp(flags, "c") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 249 | iflags = O_RDWR|O_CREAT; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 250 | else if ( strcmp(flags, "n") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 251 | iflags = O_RDWR|O_CREAT|O_TRUNC; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 252 | else { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 253 | PyErr_SetString(DbmError, |
| 254 | "Flags should be one of 'r', 'w', 'c' or 'n'"); |
| 255 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 256 | } |
| 257 | return newdbmobject(name, iflags, mode); |
| 258 | } |
| 259 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 260 | static PyMethodDef dbmmodule_methods[] = { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 261 | { "open", (PyCFunction)dbmopen, 1 }, |
| 262 | { 0, 0 }, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 265 | DL_EXPORT(void) |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 266 | initdbm(void) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 267 | PyObject *m, *d; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 268 | |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 269 | m = Py_InitModule("dbm", dbmmodule_methods); |
| 270 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 271 | DbmError = PyErr_NewException("dbm.error", NULL, NULL); |
| 272 | if (DbmError != NULL) |
| 273 | PyDict_SetItemString(d, "error", DbmError); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 274 | } |