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