Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* DBM module using dictionary interface */ |
| 33 | |
| 34 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 35 | #include "Python.h" |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 36 | |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <ndbm.h> |
| 41 | |
| 42 | typedef struct { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 43 | PyObject_HEAD |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 44 | int di_size; /* -1 means recompute */ |
| 45 | DBM *di_dbm; |
| 46 | } dbmobject; |
| 47 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 48 | staticforward PyTypeObject Dbmtype; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 49 | |
| 50 | #define is_dbmobject(v) ((v)->ob_type == &Dbmtype) |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 51 | #define check_dbmobject_open(v) if ((v)->di_dbm == NULL) \ |
| 52 | { PyErr_SetString(DbmError, "DBM object has already been closed"); \ |
| 53 | return NULL; } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 54 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 55 | static PyObject *DbmError; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 56 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 57 | static PyObject * |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 58 | newdbmobject(file, flags, mode) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 59 | char *file; |
| 60 | int flags; |
| 61 | int mode; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 62 | { |
| 63 | dbmobject *dp; |
| 64 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 65 | dp = PyObject_NEW(dbmobject, &Dbmtype); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 66 | if (dp == NULL) |
| 67 | return NULL; |
| 68 | dp->di_size = -1; |
| 69 | if ( (dp->di_dbm = dbm_open(file, flags, mode)) == 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 70 | PyErr_SetFromErrno(DbmError); |
| 71 | Py_DECREF(dp); |
| 72 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 73 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 74 | return (PyObject *)dp; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* Methods */ |
| 78 | |
| 79 | static void |
| 80 | dbm_dealloc(dp) |
| 81 | register dbmobject *dp; |
| 82 | { |
| 83 | if ( dp->di_dbm ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 84 | dbm_close(dp->di_dbm); |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 85 | PyMem_DEL(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int |
| 89 | dbm_length(dp) |
| 90 | dbmobject *dp; |
| 91 | { |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 92 | if (dp->di_dbm == NULL) { |
| 93 | PyErr_SetString(DbmError, "DBM object has already been closed"); |
| 94 | return -1; |
| 95 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 96 | if ( dp->di_size < 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 97 | datum key; |
| 98 | int size; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 99 | |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 100 | size = 0; |
| 101 | for ( key=dbm_firstkey(dp->di_dbm); key.dptr; |
| 102 | key = dbm_nextkey(dp->di_dbm)) |
| 103 | size++; |
| 104 | dp->di_size = size; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 105 | } |
| 106 | return dp->di_size; |
| 107 | } |
| 108 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 109 | static PyObject * |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 110 | dbm_subscript(dp, key) |
| 111 | dbmobject *dp; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 112 | register PyObject *key; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 113 | { |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 114 | datum drec, krec; |
| 115 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 116 | if (!PyArg_Parse(key, "s#", &krec.dptr, &krec.dsize) ) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 117 | return NULL; |
| 118 | |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 119 | check_dbmobject_open(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 120 | drec = dbm_fetch(dp->di_dbm, krec); |
| 121 | if ( drec.dptr == 0 ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 122 | PyErr_SetString(PyExc_KeyError, |
| 123 | PyString_AS_STRING((PyStringObject *)key)); |
| 124 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 125 | } |
| 126 | if ( dbm_error(dp->di_dbm) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 127 | dbm_clearerr(dp->di_dbm); |
| 128 | PyErr_SetString(DbmError, ""); |
| 129 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 130 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 131 | return PyString_FromStringAndSize(drec.dptr, drec.dsize); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static int |
| 135 | dbm_ass_sub(dp, v, w) |
| 136 | dbmobject *dp; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 137 | PyObject *v, *w; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 138 | { |
| 139 | datum krec, drec; |
| 140 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 141 | if ( !PyArg_Parse(v, "s#", &krec.dptr, &krec.dsize) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 142 | PyErr_SetString(PyExc_TypeError, |
| 143 | "dbm mappings have string indices only"); |
| 144 | return -1; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 145 | } |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 146 | if (dp->di_dbm == NULL) { |
| 147 | PyErr_SetString(DbmError, "DBM object has already been closed"); |
| 148 | return -1; |
| 149 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 150 | dp->di_size = -1; |
| 151 | if (w == NULL) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 152 | if ( dbm_delete(dp->di_dbm, krec) < 0 ) { |
| 153 | dbm_clearerr(dp->di_dbm); |
| 154 | PyErr_SetString(PyExc_KeyError, |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 155 | PyString_AS_STRING((PyStringObject *)v)); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 156 | return -1; |
| 157 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 158 | } else { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 159 | if ( !PyArg_Parse(w, "s#", &drec.dptr, &drec.dsize) ) { |
| 160 | PyErr_SetString(PyExc_TypeError, |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 161 | "dbm mappings have string elements only"); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 162 | return -1; |
| 163 | } |
| 164 | if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) { |
| 165 | dbm_clearerr(dp->di_dbm); |
Roger E. Masse | bd4b961 | 1996-12-13 15:59:22 +0000 | [diff] [blame] | 166 | PyErr_SetString(DbmError, |
| 167 | "Cannot add item to database"); |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 168 | return -1; |
| 169 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 170 | } |
| 171 | if ( dbm_error(dp->di_dbm) ) { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 172 | dbm_clearerr(dp->di_dbm); |
| 173 | PyErr_SetString(DbmError, ""); |
| 174 | return -1; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 179 | static PyMappingMethods dbm_as_mapping = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 180 | (inquiry)dbm_length, /*mp_length*/ |
| 181 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 182 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 185 | static PyObject * |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 186 | dbm__close(dp, args) |
| 187 | register dbmobject *dp; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 188 | PyObject *args; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 189 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 190 | if ( !PyArg_NoArgs(args) ) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | if ( dp->di_dbm ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 193 | dbm_close(dp->di_dbm); |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 194 | dp->di_dbm = NULL; |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 195 | Py_INCREF(Py_None); |
| 196 | return Py_None; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 199 | static PyObject * |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 200 | dbm_keys(dp, args) |
| 201 | register dbmobject *dp; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 202 | PyObject *args; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 203 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 204 | register PyObject *v, *item; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 205 | datum key; |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 206 | int err; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 207 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 208 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 209 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 210 | check_dbmobject_open(dp); |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 211 | v = PyList_New(0); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 212 | if (v == NULL) |
| 213 | return NULL; |
| 214 | for (key = dbm_firstkey(dp->di_dbm); key.dptr; |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 215 | key = dbm_nextkey(dp->di_dbm)) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 216 | item = PyString_FromStringAndSize(key.dptr, key.dsize); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 217 | if (item == NULL) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 218 | Py_DECREF(v); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 221 | err = PyList_Append(v, item); |
| 222 | Py_DECREF(item); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 223 | if (err != 0) { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 224 | Py_DECREF(v); |
Guido van Rossum | d503913 | 1995-01-30 12:45:38 +0000 | [diff] [blame] | 225 | return NULL; |
| 226 | } |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 227 | } |
| 228 | return v; |
| 229 | } |
| 230 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 231 | static PyObject * |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 232 | dbm_has_key(dp, args) |
| 233 | register dbmobject *dp; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 234 | PyObject *args; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 235 | { |
| 236 | datum key, val; |
| 237 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 238 | if (!PyArg_Parse(args, "s#", &key.dptr, &key.dsize)) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 239 | return NULL; |
Guido van Rossum | 77eecfa | 1997-07-17 22:56:01 +0000 | [diff] [blame] | 240 | check_dbmobject_open(dp); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 241 | val = dbm_fetch(dp->di_dbm, key); |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 242 | return PyInt_FromLong(val.dptr != NULL); |
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 PyMethodDef dbm_methods[] = { |
| 246 | {"close", (PyCFunction)dbm__close}, |
| 247 | {"keys", (PyCFunction)dbm_keys}, |
| 248 | {"has_key", (PyCFunction)dbm_has_key}, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 249 | {NULL, NULL} /* sentinel */ |
| 250 | }; |
| 251 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 252 | static PyObject * |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 253 | dbm_getattr(dp, name) |
| 254 | dbmobject *dp; |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 255 | char *name; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 256 | { |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 257 | return Py_FindMethod(dbm_methods, (PyObject *)dp, name); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 260 | static PyTypeObject Dbmtype = { |
| 261 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 262 | 0, |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 263 | "dbm", |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 264 | sizeof(dbmobject), |
| 265 | 0, |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 266 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
| 267 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | (getattrfunc)dbm_getattr, /*tp_getattr*/ |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 269 | 0, /*tp_setattr*/ |
| 270 | 0, /*tp_compare*/ |
| 271 | 0, /*tp_repr*/ |
| 272 | 0, /*tp_as_number*/ |
| 273 | 0, /*tp_as_sequence*/ |
| 274 | &dbm_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | /* ----------------------------------------------------------------- */ |
| 278 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 279 | static PyObject * |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 280 | dbmopen(self, args) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 281 | PyObject *self; |
| 282 | PyObject *args; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 283 | { |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 284 | char *name; |
| 285 | char *flags = "r"; |
| 286 | int iflags; |
| 287 | int mode = 0666; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 289 | if ( !PyArg_ParseTuple(args, "s|si:open", &name, &flags, &mode) ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 290 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 291 | if ( strcmp(flags, "r") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 292 | iflags = O_RDONLY; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 293 | else if ( strcmp(flags, "w") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 294 | iflags = O_RDWR; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 295 | else if ( strcmp(flags, "rw") == 0 ) /* B/W compat */ |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 296 | iflags = O_RDWR|O_CREAT; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 297 | else if ( strcmp(flags, "c") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 298 | iflags = O_RDWR|O_CREAT; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 299 | else if ( strcmp(flags, "n") == 0 ) |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 300 | iflags = O_RDWR|O_CREAT|O_TRUNC; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 301 | else { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 302 | PyErr_SetString(DbmError, |
| 303 | "Flags should be one of 'r', 'w', 'c' or 'n'"); |
| 304 | return NULL; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 305 | } |
| 306 | return newdbmobject(name, iflags, mode); |
| 307 | } |
| 308 | |
Roger E. Masse | b0dfe96 | 1996-12-10 00:07:00 +0000 | [diff] [blame] | 309 | static PyMethodDef dbmmodule_methods[] = { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 310 | { "open", (PyCFunction)dbmopen, 1 }, |
| 311 | { 0, 0 }, |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 314 | DL_EXPORT(void) |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 315 | initdbm() { |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 316 | PyObject *m, *d; |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 317 | |
Roger E. Masse | e7ee8c3 | 1996-12-13 15:55:22 +0000 | [diff] [blame] | 318 | m = Py_InitModule("dbm", dbmmodule_methods); |
| 319 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 320 | DbmError = PyErr_NewException("dbm.error", NULL, NULL); |
| 321 | if (DbmError != NULL) |
| 322 | PyDict_SetItemString(d, "error", DbmError); |
Guido van Rossum | dd9ed83 | 1992-06-29 17:10:40 +0000 | [diff] [blame] | 323 | } |