Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 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 | 1100dca | 1995-08-30 23:43:03 +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 | 1100dca | 1995-08-30 23:43:03 +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 | 1100dca | 1995-08-30 23:43:03 +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 | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Berkeley DB interface. |
| 33 | Author: Michael McLay |
| 34 | Hacked: Guido van Rossum |
| 35 | Btree and Recno additions plus sequence methods: David Ely |
| 36 | |
| 37 | XXX To do: |
| 38 | - provide interface to the B-tree and record libraries too |
| 39 | - provide a way to access the various hash functions |
| 40 | - support more open flags |
| 41 | */ |
| 42 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 44 | |
| 45 | #include <sys/types.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <fcntl.h> |
| 48 | #include <db.h> |
| 49 | /* Please don't include internal header files of the Berkeley db package |
| 50 | (it messes up the info required in the Setup file) */ |
| 51 | |
| 52 | typedef struct { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 53 | PyObject_HEAD |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 54 | DB *di_bsddb; |
| 55 | int di_size; /* -1 means recompute */ |
| 56 | } bsddbobject; |
| 57 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 58 | staticforward PyTypeObject Bsddbtype; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 59 | |
| 60 | #define is_bsddbobject(v) ((v)->ob_type == &Bsddbtype) |
| 61 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 62 | static PyObject *BsddbError; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 64 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 65 | newdbhashobject(file, flags, mode, |
| 66 | bsize, ffactor, nelem, cachesize, hash, lorder) |
| 67 | char *file; |
| 68 | int flags; |
| 69 | int mode; |
| 70 | int bsize; |
| 71 | int ffactor; |
| 72 | int nelem; |
| 73 | int cachesize; |
| 74 | int hash; /* XXX ignored */ |
| 75 | int lorder; |
| 76 | { |
| 77 | bsddbobject *dp; |
| 78 | HASHINFO info; |
| 79 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 80 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 81 | return NULL; |
| 82 | |
| 83 | info.bsize = bsize; |
| 84 | info.ffactor = ffactor; |
| 85 | info.nelem = nelem; |
| 86 | info.cachesize = cachesize; |
| 87 | info.hash = NULL; /* XXX should derive from hash argument */ |
| 88 | info.lorder = lorder; |
| 89 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 90 | #ifdef O_BINARY |
| 91 | flags |= O_BINARY; |
| 92 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 93 | if ((dp->di_bsddb = dbopen(file, flags, mode, DB_HASH, &info)) == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 94 | PyErr_SetFromErrno(BsddbError); |
| 95 | Py_DECREF(dp); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | dp->di_size = -1; |
| 100 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 101 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 104 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 105 | newdbbtobject(file, flags, mode, |
| 106 | btflags, cachesize, maxkeypage, minkeypage, psize, lorder) |
| 107 | char *file; |
| 108 | int flags; |
| 109 | int mode; |
| 110 | int btflags; |
| 111 | int cachesize; |
| 112 | int maxkeypage; |
| 113 | int minkeypage; |
| 114 | int psize; |
| 115 | int lorder; |
| 116 | { |
| 117 | bsddbobject *dp; |
| 118 | BTREEINFO info; |
| 119 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 120 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 121 | return NULL; |
| 122 | |
| 123 | info.flags = btflags; |
| 124 | info.cachesize = cachesize; |
| 125 | info.maxkeypage = maxkeypage; |
| 126 | info.minkeypage = minkeypage; |
| 127 | info.psize = psize; |
| 128 | info.lorder = lorder; |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 129 | info.compare = 0; /* Use default comparison functions, for now..*/ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 130 | info.prefix = 0; |
| 131 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 132 | #ifdef O_BINARY |
| 133 | flags |= O_BINARY; |
| 134 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 135 | if ((dp->di_bsddb = dbopen(file, flags, mode, DB_BTREE, &info)) == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 136 | PyErr_SetFromErrno(BsddbError); |
| 137 | Py_DECREF(dp); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | dp->di_size = -1; |
| 142 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 143 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 146 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 147 | newdbrnobject(file, flags, mode, |
| 148 | rnflags, cachesize, psize, lorder, reclen, bval, bfname) |
| 149 | char *file; |
| 150 | int flags; |
| 151 | int mode; |
| 152 | int rnflags; |
| 153 | int cachesize; |
| 154 | int psize; |
| 155 | int lorder; |
| 156 | size_t reclen; |
| 157 | u_char bval; |
| 158 | char *bfname; |
| 159 | { |
| 160 | bsddbobject *dp; |
| 161 | RECNOINFO info; |
| 162 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 163 | if ((dp = PyObject_NEW(bsddbobject, &Bsddbtype)) == NULL) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 164 | return NULL; |
| 165 | |
| 166 | info.flags = rnflags; |
| 167 | info.cachesize = cachesize; |
| 168 | info.psize = psize; |
| 169 | info.lorder = lorder; |
| 170 | info.reclen = reclen; |
| 171 | info.bval = bval; |
| 172 | info.bfname = bfname; |
| 173 | |
Guido van Rossum | 6beb479 | 1996-09-11 23:22:25 +0000 | [diff] [blame] | 174 | #ifdef O_BINARY |
| 175 | flags |= O_BINARY; |
| 176 | #endif |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 177 | if ((dp->di_bsddb = dbopen(file, flags, mode, DB_RECNO, &info)) == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 178 | PyErr_SetFromErrno(BsddbError); |
| 179 | Py_DECREF(dp); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | dp->di_size = -1; |
| 184 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 185 | return (PyObject *)dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
| 189 | static void |
| 190 | bsddb_dealloc(dp) |
| 191 | bsddbobject *dp; |
| 192 | { |
| 193 | if (dp->di_bsddb != NULL) { |
| 194 | if ((dp->di_bsddb->close)(dp->di_bsddb) != 0) |
| 195 | fprintf(stderr, |
Guido van Rossum | 81b24c0 | 1996-12-05 23:30:16 +0000 | [diff] [blame] | 196 | "Python bsddb: close errno %d in dealloc\n", errno); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 197 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 198 | PyMem_DEL(dp); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static int |
| 202 | bsddb_length(dp) |
| 203 | bsddbobject *dp; |
| 204 | { |
| 205 | if (dp->di_size < 0) { |
| 206 | DBT krec, drec; |
| 207 | int status; |
| 208 | int size = 0; |
| 209 | for (status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec,R_FIRST); |
| 210 | status == 0; |
| 211 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT)) |
| 212 | size++; |
| 213 | if (status < 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 214 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 215 | return -1; |
| 216 | } |
| 217 | dp->di_size = size; |
| 218 | } |
| 219 | return dp->di_size; |
| 220 | } |
| 221 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 222 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 223 | bsddb_subscript(dp, key) |
| 224 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 225 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 226 | { |
| 227 | int status; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 228 | DBT krec, drec; |
| 229 | char *data; |
| 230 | int size; |
| 231 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 232 | if (!PyArg_Parse(key, "s#", &data, &size)) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 233 | return NULL; |
| 234 | krec.data = data; |
| 235 | krec.size = size; |
| 236 | |
| 237 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
| 238 | if (status != 0) { |
| 239 | if (status < 0) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 240 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 241 | else |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 242 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
| 245 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 246 | return PyString_FromStringAndSize((char *)drec.data, (int)drec.size); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static int |
| 250 | bsddb_ass_sub(dp, key, value) |
| 251 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 252 | PyObject *key, *value; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 253 | { |
| 254 | int status; |
| 255 | DBT krec, drec; |
| 256 | char *data; |
| 257 | int size; |
| 258 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 259 | if (!PyArg_Parse(key, "s#", &data, &size)) { |
| 260 | PyErr_SetString(PyExc_TypeError, "bsddb key type must be string"); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 261 | return -1; |
| 262 | } |
| 263 | krec.data = data; |
| 264 | krec.size = size; |
| 265 | dp->di_size = -1; |
| 266 | if (value == NULL) { |
| 267 | status = (dp->di_bsddb->del)(dp->di_bsddb, &krec, 0); |
| 268 | } |
| 269 | else { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 270 | if (!PyArg_Parse(value, "s#", &data, &size)) { |
| 271 | PyErr_SetString(PyExc_TypeError, "bsddb value type must be string"); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 272 | return -1; |
| 273 | } |
| 274 | drec.data = data; |
| 275 | drec.size = size; |
| 276 | status = (dp->di_bsddb->put)(dp->di_bsddb, &krec, &drec, 0); |
| 277 | } |
| 278 | if (status != 0) { |
| 279 | if (status < 0) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 280 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 281 | else |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 282 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 283 | return -1; |
| 284 | } |
| 285 | return 0; |
| 286 | } |
| 287 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 288 | static PyMappingMethods bsddb_as_mapping = { |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 289 | (inquiry)bsddb_length, /*mp_length*/ |
| 290 | (binaryfunc)bsddb_subscript, /*mp_subscript*/ |
| 291 | (objobjargproc)bsddb_ass_sub, /*mp_ass_subscript*/ |
| 292 | }; |
| 293 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 294 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 295 | bsddb_close(dp, args) |
| 296 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 297 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 298 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 299 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 300 | return NULL; |
| 301 | if (dp->di_bsddb != NULL) { |
| 302 | if ((dp->di_bsddb->close)(dp->di_bsddb) != 0) { |
| 303 | dp->di_bsddb = NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 304 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
| 307 | } |
| 308 | dp->di_bsddb = NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 309 | Py_INCREF(Py_None); |
| 310 | return Py_None; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 313 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 314 | bsddb_keys(dp, args) |
| 315 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 316 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 317 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 318 | PyObject *list, *item; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 319 | DBT krec, drec; |
| 320 | int status; |
| 321 | int err; |
| 322 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 323 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 324 | return NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 325 | list = PyList_New(0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 326 | if (list == NULL) |
| 327 | return NULL; |
| 328 | for (status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_FIRST); |
| 329 | status == 0; |
| 330 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_NEXT)) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 331 | item = PyString_FromStringAndSize((char *)krec.data, (int)krec.size); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 332 | if (item == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 333 | Py_DECREF(list); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 334 | return NULL; |
| 335 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 336 | err = PyList_Append(list, item); |
| 337 | Py_DECREF(item); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 338 | if (err != 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 339 | Py_DECREF(list); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 340 | return NULL; |
| 341 | } |
| 342 | } |
| 343 | if (status < 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 344 | PyErr_SetFromErrno(BsddbError); |
| 345 | Py_DECREF(list); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
| 348 | if (dp->di_size < 0) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 349 | dp->di_size = PyList_Size(list); /* We just did the work for this... */ |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 350 | return list; |
| 351 | } |
| 352 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 353 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 354 | bsddb_has_key(dp, args) |
| 355 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 356 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 357 | { |
| 358 | DBT krec, drec; |
| 359 | int status; |
| 360 | char *data; |
| 361 | int size; |
| 362 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 363 | if (!PyArg_Parse(args, "s#", &data, &size)) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 364 | return NULL; |
| 365 | krec.data = data; |
| 366 | krec.size = size; |
| 367 | |
| 368 | status = (dp->di_bsddb->get)(dp->di_bsddb, &krec, &drec, 0); |
| 369 | if (status < 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 370 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 371 | return NULL; |
| 372 | } |
| 373 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 374 | return PyInt_FromLong(status == 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 377 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 378 | bsddb_set_location(dp, key) |
| 379 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 380 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 381 | { |
| 382 | int status; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 383 | DBT krec, drec; |
| 384 | char *data; |
| 385 | int size; |
| 386 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 387 | if (!PyArg_Parse(key, "s#", &data, &size)) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 388 | return NULL; |
| 389 | krec.data = data; |
| 390 | krec.size = size; |
| 391 | |
| 392 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, R_CURSOR); |
| 393 | if (status != 0) { |
| 394 | if (status < 0) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 395 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 396 | else |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 397 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
| 400 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 401 | return Py_BuildValue("s#s#", krec.data, krec.size, drec.data, drec.size); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 404 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 405 | bsddb_seq(dp, args, sequence_request) |
| 406 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 407 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 408 | int sequence_request; |
| 409 | { |
| 410 | int status; |
| 411 | DBT krec, drec; |
| 412 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 413 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 414 | return NULL; |
| 415 | |
| 416 | krec.data = 0; |
| 417 | krec.size = 0; |
| 418 | |
| 419 | status = (dp->di_bsddb->seq)(dp->di_bsddb, &krec, &drec, sequence_request); |
| 420 | if (status != 0) { |
| 421 | if (status < 0) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 422 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 423 | else |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 424 | PyErr_SetObject(PyExc_KeyError, args); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
| 427 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 428 | return Py_BuildValue("s#s#", krec.data, krec.size, drec.data, drec.size); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 431 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 432 | bsddb_next(dp, key) |
| 433 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 434 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 435 | { |
| 436 | return bsddb_seq(dp, key, R_NEXT); |
| 437 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 438 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 439 | bsddb_previous(dp, key) |
| 440 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 441 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 442 | { |
| 443 | return bsddb_seq(dp, key, R_PREV); |
| 444 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 445 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 446 | bsddb_first(dp, key) |
| 447 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 448 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 449 | { |
| 450 | return bsddb_seq(dp, key, R_FIRST); |
| 451 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 452 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 453 | bsddb_last(dp, key) |
| 454 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 455 | PyObject *key; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 456 | { |
| 457 | return bsddb_seq(dp, key, R_LAST); |
| 458 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 459 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 460 | bsddb_sync(dp, args) |
| 461 | bsddbobject *dp; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 462 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 463 | { |
| 464 | int status; |
| 465 | |
| 466 | status = (dp->di_bsddb->sync)(dp->di_bsddb, 0); |
| 467 | if (status != 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 468 | PyErr_SetFromErrno(BsddbError); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 469 | return NULL; |
| 470 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 471 | return PyInt_FromLong(status = 0); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 472 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 473 | static PyMethodDef bsddb_methods[] = { |
| 474 | {"close", (PyCFunction)bsddb_close}, |
| 475 | {"keys", (PyCFunction)bsddb_keys}, |
| 476 | {"has_key", (PyCFunction)bsddb_has_key}, |
| 477 | {"set_location", (PyCFunction)bsddb_set_location}, |
| 478 | {"next", (PyCFunction)bsddb_next}, |
| 479 | {"previous", (PyCFunction)bsddb_previous}, |
| 480 | {"first", (PyCFunction)bsddb_first}, |
| 481 | {"last", (PyCFunction)bsddb_last}, |
| 482 | {"sync", (PyCFunction)bsddb_sync}, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 483 | {NULL, NULL} /* sentinel */ |
| 484 | }; |
| 485 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 486 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 487 | bsddb_getattr(dp, name) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 488 | PyObject *dp; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 489 | char *name; |
| 490 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 491 | return Py_FindMethod(bsddb_methods, dp, name); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 494 | static PyTypeObject Bsddbtype = { |
Guido van Rossum | 622f577 | 1996-09-11 23:18:43 +0000 | [diff] [blame] | 495 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 496 | 0, |
| 497 | "bsddb", |
| 498 | sizeof(bsddbobject), |
| 499 | 0, |
| 500 | (destructor)bsddb_dealloc, /*tp_dealloc*/ |
| 501 | 0, /*tp_print*/ |
| 502 | (getattrfunc)bsddb_getattr, /*tp_getattr*/ |
| 503 | 0, /*tp_setattr*/ |
| 504 | 0, /*tp_compare*/ |
| 505 | 0, /*tp_repr*/ |
| 506 | 0, /*tp_as_number*/ |
| 507 | 0, /*tp_as_sequence*/ |
| 508 | &bsddb_as_mapping, /*tp_as_mapping*/ |
| 509 | }; |
| 510 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 511 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 512 | bsdhashopen(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 513 | PyObject *self; |
| 514 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 515 | { |
| 516 | char *file; |
| 517 | char *flag = NULL; |
| 518 | int flags = O_RDONLY; |
| 519 | int mode = 0666; |
| 520 | int bsize = 0; |
| 521 | int ffactor = 0; |
| 522 | int nelem = 0; |
| 523 | int cachesize = 0; |
| 524 | int hash = 0; /* XXX currently ignored */ |
| 525 | int lorder = 0; |
| 526 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 527 | if (!PyArg_ParseTuple(args, "s|siiiiiii", |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 528 | &file, &flag, &mode, |
| 529 | &bsize, &ffactor, &nelem, &cachesize, &hash, &lorder)) |
| 530 | return NULL; |
| 531 | if (flag != NULL) { |
| 532 | /* XXX need a way to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 533 | if (flag[0] == 'r') |
| 534 | flags = O_RDONLY; |
| 535 | else if (flag[0] == 'w') |
| 536 | flags = O_RDWR; |
| 537 | else if (flag[0] == 'c') |
| 538 | flags = O_RDWR|O_CREAT; |
| 539 | else if (flag[0] == 'n') |
| 540 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 541 | else { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 542 | PyErr_SetString(BsddbError, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 543 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 544 | return NULL; |
| 545 | } |
| 546 | if (flag[1] == 'l') { |
| 547 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
| 548 | if (flag[0] == 'r') |
| 549 | flags |= O_SHLOCK; |
| 550 | else |
| 551 | flags |= O_EXLOCK; |
| 552 | #else |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 553 | PyErr_SetString(BsddbError, "locking not supported on this platform"); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 554 | return NULL; |
| 555 | #endif |
| 556 | } |
| 557 | } |
| 558 | return newdbhashobject(file, flags, mode, |
| 559 | bsize, ffactor, nelem, cachesize, hash, lorder); |
| 560 | } |
| 561 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 562 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 563 | bsdbtopen(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 564 | PyObject *self; |
| 565 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 566 | { |
| 567 | char *file; |
| 568 | char *flag = NULL; |
| 569 | int flags = O_RDONLY; |
| 570 | int mode = 0666; |
| 571 | int cachesize = 0; |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 572 | int maxkeypage = 0; |
| 573 | int minkeypage = 0; |
| 574 | int btflags = 0; |
| 575 | unsigned int psize = 0; |
| 576 | int lorder = 0; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 577 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 578 | if (!PyArg_ParseTuple(args, "s|siiiiiii", |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 579 | &file, &flag, &mode, |
| 580 | &btflags, &cachesize, &maxkeypage, &minkeypage, |
| 581 | &psize, &lorder)) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 582 | return NULL; |
| 583 | if (flag != NULL) { |
| 584 | /* XXX need a way to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 585 | if (flag[0] == 'r') |
| 586 | flags = O_RDONLY; |
| 587 | else if (flag[0] == 'w') |
| 588 | flags = O_RDWR; |
| 589 | else if (flag[0] == 'c') |
| 590 | flags = O_RDWR|O_CREAT; |
| 591 | else if (flag[0] == 'n') |
| 592 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 593 | else { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 594 | PyErr_SetString(BsddbError, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 595 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 596 | return NULL; |
| 597 | } |
| 598 | if (flag[1] == 'l') { |
| 599 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
| 600 | if (flag[0] == 'r') |
| 601 | flags |= O_SHLOCK; |
| 602 | else |
| 603 | flags |= O_EXLOCK; |
| 604 | #else |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 605 | PyErr_SetString(BsddbError, "locking not supported on this platform"); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 606 | return NULL; |
| 607 | #endif |
| 608 | } |
| 609 | } |
| 610 | return newdbbtobject(file, flags, mode, |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 611 | btflags, cachesize, maxkeypage, minkeypage, |
| 612 | psize, lorder); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 613 | } |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 614 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 615 | static PyObject * |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 616 | bsdrnopen(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 617 | PyObject *self; |
| 618 | PyObject *args; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 619 | { |
| 620 | char *file; |
| 621 | char *flag = NULL; |
| 622 | int flags = O_RDONLY; |
| 623 | int mode = 0666; |
| 624 | int cachesize = 0; |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 625 | int rnflags = 0; |
| 626 | unsigned int psize = 0; |
| 627 | int lorder = 0; |
| 628 | size_t reclen = 0; |
| 629 | char *bval = ""; |
| 630 | char *bfname = NULL; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 632 | if (!PyArg_ParseTuple(args, "s|siiiiiiss", |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 633 | &file, &flag, &mode, |
| 634 | &rnflags, &cachesize, &psize, &lorder, |
| 635 | &reclen, &bval, &bfname)) |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 636 | return NULL; |
| 637 | if (flag != NULL) { |
| 638 | /* XXX need a way to pass O_EXCL, O_EXLOCK, O_NONBLOCK, O_SHLOCK */ |
| 639 | if (flag[0] == 'r') |
| 640 | flags = O_RDONLY; |
| 641 | else if (flag[0] == 'w') |
| 642 | flags = O_RDWR; |
| 643 | else if (flag[0] == 'c') |
| 644 | flags = O_RDWR|O_CREAT; |
| 645 | else if (flag[0] == 'n') |
| 646 | flags = O_RDWR|O_CREAT|O_TRUNC; |
| 647 | else { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 648 | PyErr_SetString(BsddbError, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 649 | "Flag should begin with 'r', 'w', 'c' or 'n'"); |
| 650 | return NULL; |
| 651 | } |
| 652 | if (flag[1] == 'l') { |
| 653 | #if defined(O_EXLOCK) && defined(O_SHLOCK) |
| 654 | if (flag[0] == 'r') |
| 655 | flags |= O_SHLOCK; |
| 656 | else |
| 657 | flags |= O_EXLOCK; |
| 658 | #else |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 659 | PyErr_SetString(BsddbError, "locking not supported on this platform"); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 660 | return NULL; |
| 661 | #endif |
| 662 | } |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 663 | else if (flag[1] != '\0') { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 664 | PyErr_SetString(BsddbError, |
Guido van Rossum | dd96ca7 | 1996-05-23 22:57:54 +0000 | [diff] [blame] | 665 | "Flag char 2 should be 'l' or absent"); |
| 666 | return NULL; |
| 667 | } |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 668 | } |
| 669 | return newdbrnobject(file, flags, mode, |
| 670 | rnflags, cachesize, psize, lorder, bval[0], bfname); |
| 671 | } |
| 672 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 673 | static PyMethodDef bsddbmodule_methods[] = { |
| 674 | {"hashopen", (PyCFunction)bsdhashopen, 1}, |
| 675 | {"btopen", (PyCFunction)bsdbtopen, 1}, |
| 676 | {"rnopen", (PyCFunction)bsdrnopen, 1}, |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 677 | {0, 0}, |
| 678 | }; |
| 679 | |
| 680 | void |
| 681 | initbsddb() { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 682 | PyObject *m, *d; |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 683 | |
Guido van Rossum | 622f577 | 1996-09-11 23:18:43 +0000 | [diff] [blame] | 684 | Bsddbtype.ob_type = &PyType_Type; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 685 | m = Py_InitModule("bsddb", bsddbmodule_methods); |
| 686 | d = PyModule_GetDict(m); |
| 687 | BsddbError = PyString_FromString("bsddb.error"); |
| 688 | if (BsddbError == NULL || PyDict_SetItemString(d, "error", BsddbError)) |
| 689 | Py_FatalError("can't define bsddb.error"); |
Guido van Rossum | 1100dca | 1995-08-30 23:43:03 +0000 | [diff] [blame] | 690 | } |